using namespace clang;
using namespace clang::cxcursor;
+using namespace clang::cxstring;
using namespace idx;
//===----------------------------------------------------------------------===//
return VisitExpr(E);
}
-CXString CIndexer::createCXString(const char *String, bool DupString){
- CXString Str;
- if (DupString) {
- Str.Spelling = strdup(String);
- Str.MustFreeString = 1;
- } else {
- Str.Spelling = String;
- Str.MustFreeString = 0;
- }
- return Str;
-}
-
-CXString CIndexer::createCXString(llvm::StringRef String, bool DupString) {
- CXString Result;
- if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
- char *Spelling = (char *)malloc(String.size() + 1);
- memmove(Spelling, String.data(), String.size());
- Spelling[String.size()] = 0;
- Result.Spelling = Spelling;
- Result.MustFreeString = 1;
- } else {
- Result.Spelling = String.data();
- Result.MustFreeString = 0;
- }
- return Result;
-}
-
extern "C" {
CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
CIndexer *CIdxr = new CIndexer();
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
if (!CTUnit)
- return CIndexer::createCXString("");
+ return createCXString("");
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
- return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
- true);
+ return createCXString(CXXUnit->getOriginalSourceFileName(), true);
}
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
static CXString getDeclSpelling(Decl *D) {
NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
if (!ND)
- return CIndexer::createCXString("");
+ return createCXString("");
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
- return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
- true);
+ return createCXString(OMD->getSelector().getAsString());
if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
// No, this isn't the same as the code below. getIdentifier() is non-virtual
// and returns different names. NamedDecl returns the class name and
// ObjCCategoryImplDecl returns the category name.
- return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
+ return createCXString(CIMP->getIdentifier()->getNameStart());
if (ND->getIdentifier())
- return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
+ return createCXString(ND->getIdentifier()->getNameStart());
- return CIndexer::createCXString("");
+ return createCXString("");
}
CXString clang_getCursorSpelling(CXCursor C) {
switch (C.kind) {
case CXCursor_ObjCSuperClassRef: {
ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
- return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
+ return createCXString(Super->getIdentifier()->getNameStart());
}
case CXCursor_ObjCClassRef: {
ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
- return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
+ return createCXString(Class->getIdentifier()->getNameStart());
}
case CXCursor_ObjCProtocolRef: {
ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
assert(OID && "getCursorSpelling(): Missing protocol decl");
- return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
+ return createCXString(OID->getIdentifier()->getNameStart());
}
case CXCursor_TypeRef: {
TypeDecl *Type = getCursorTypeRef(C).first;
assert(Type && "Missing type decl");
- return CIndexer::createCXString(
- getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(),
- true);
+ return createCXString(getCursorContext(C).getTypeDeclType(Type).
+ getAsString());
}
default:
- return CIndexer::createCXString("<not implemented>");
+ return createCXString("<not implemented>");
}
}
Decl *D = getDeclFromExpr(getCursorExpr(C));
if (D)
return getDeclSpelling(D);
- return CIndexer::createCXString("");
+ return createCXString("");
}
if (clang_isDeclaration(C.kind))
return getDeclSpelling(getCursorDecl(C));
- return CIndexer::createCXString("");
+ return createCXString("");
}
const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
case CXToken_Identifier:
case CXToken_Keyword:
// We know we have an IdentifierInfo*, so use that.
- return CIndexer::createCXString(
- static_cast<IdentifierInfo *>(CXTok.ptr_data)->getNameStart());
+ return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
+ ->getNameStart());
case CXToken_Literal: {
// We have stashed the starting pointer in the ptr_data field. Use it.
const char *Text = static_cast<const char *>(CXTok.ptr_data);
- return CIndexer::createCXString(llvm::StringRef(Text, CXTok.int_data[2]),
- true);
+ return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
}
case CXToken_Punctuation:
// deconstructing the source location.
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
if (!CXXUnit)
- return CIndexer::createCXString("");
+ return createCXString("");
SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
std::pair<FileID, unsigned> LocInfo
std::pair<const char *,const char *> Buffer
= CXXUnit->getSourceManager().getBufferData(LocInfo.first);
- return CIndexer::createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
- CXTok.int_data[2]),
- true);
+ return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
+ CXTok.int_data[2]));
}
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
} // end: extern "C"
+namespace clang { namespace cxstring {
+CXString createCXString(const char *String, bool DupString){
+ CXString Str;
+ if (DupString) {
+ Str.Spelling = strdup(String);
+ Str.MustFreeString = 1;
+ } else {
+ Str.Spelling = String;
+ Str.MustFreeString = 0;
+ }
+ return Str;
+}
+
+CXString createCXString(llvm::StringRef String, bool DupString) {
+ CXString Result;
+ if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
+ char *Spelling = (char *)malloc(String.size() + 1);
+ memmove(Spelling, String.data(), String.size());
+ Spelling[String.size()] = 0;
+ Result.Spelling = Spelling;
+ Result.MustFreeString = 1;
+ } else {
+ Result.Spelling = String.data();
+ Result.MustFreeString = 0;
+ }
+ return Result;
+}
+}}
+
//===----------------------------------------------------------------------===//
// Misc. utility functions.
//===----------------------------------------------------------------------===//
extern "C" {
CXString clang_getClangVersion() {
- return CIndexer::createCXString(getClangFullVersion(), true);
+ return createCXString(getClangFullVersion());
}
} // end: extern "C"
-
using namespace clang;
using namespace clang::cxloc;
+using namespace clang::cxstring;
//-----------------------------------------------------------------------------
-// Opaque data structures
+// Opaque data structures
//-----------------------------------------------------------------------------
namespace {
/// \brief The storage behind a CXDiagnostic
struct CXStoredDiagnostic {
/// \brief The translation unit this diagnostic came from.
const LangOptions *LangOptsPtr;
-
+
/// \brief The severity level of this diagnostic.
Diagnostic::Level Level;
-
+
/// \brief A reference to the diagnostic information.
const DiagnosticInfo &Info;
};
}
//-----------------------------------------------------------------------------
-// CIndex Diagnostic Client
+// CIndex Diagnostic Client
//-----------------------------------------------------------------------------
CIndexDiagnosticClient::~CIndexDiagnosticClient() { }
}
//-----------------------------------------------------------------------------
-// C Interface Routines
+// C Interface Routines
//-----------------------------------------------------------------------------
extern "C" {
-
+
enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag)
return CXDiagnostic_Ignored;
-
+
switch (StoredDiag->Level) {
case Diagnostic::Ignored: return CXDiagnostic_Ignored;
case Diagnostic::Note: return CXDiagnostic_Note;
case Diagnostic::Error: return CXDiagnostic_Error;
case Diagnostic::Fatal: return CXDiagnostic_Fatal;
}
-
+
llvm_unreachable("Invalid diagnostic level");
return CXDiagnostic_Ignored;
}
-
+
CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
return clang_getNullLocation();
-
+
return translateSourceLocation(StoredDiag->Info.getLocation().getManager(),
*StoredDiag->LangOptsPtr,
StoredDiag->Info.getLocation());
CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag)
- return CIndexer::createCXString("");
-
+ return createCXString("");
+
llvm::SmallString<64> Spelling;
StoredDiag->Info.FormatDiagnostic(Spelling);
- return CIndexer::createCXString(Spelling.str(), true);
+ return createCXString(Spelling.str(), true);
}
unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
return 0;
-
+
return StoredDiag->Info.getNumRanges();
}
-
+
CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
- if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() ||
+ if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() ||
StoredDiag->Info.getLocation().isInvalid())
return clang_getNullRange();
-
+
return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
*StoredDiag->LangOptsPtr,
StoredDiag->Info.getRange(Range));
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag)
return 0;
-
+
return StoredDiag->Info.getNumCodeModificationHints();
}
-enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag,
+enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag,
unsigned FixIt) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
return CXFixIt_Insertion;
-
+
const CodeModificationHint &Hint
= StoredDiag->Info.getCodeModificationHint(FixIt);
if (Hint.RemoveRange.isInvalid())
return CXFixIt_Insertion;
if (Hint.InsertionLoc.isInvalid())
return CXFixIt_Removal;
-
- return CXFixIt_Replacement;
+
+ return CXFixIt_Replacement;
}
-CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag,
+CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag,
unsigned FixIt,
CXSourceLocation *Location) {
if (Location)
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
- return CIndexer::createCXString("");
-
+ return createCXString("");
+
const CodeModificationHint &Hint
= StoredDiag->Info.getCodeModificationHint(FixIt);
StoredDiag->Info.getLocation().getManager(),
*StoredDiag->LangOptsPtr,
Hint.InsertionLoc);
- return CIndexer::createCXString(Hint.CodeToInsert);
+ return createCXString(Hint.CodeToInsert);
}
-CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag,
+CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag,
unsigned FixIt) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() ||
StoredDiag->Info.getLocation().isInvalid())
return clang_getNullRange();
-
+
const CodeModificationHint &Hint
= StoredDiag->Info.getCodeModificationHint(FixIt);
return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
Hint.RemoveRange);
}
-CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag,
+CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag,
unsigned FixIt,
CXSourceRange *Range) {
if (Range)
StoredDiag->Info.getLocation().isInvalid()) {
if (Range)
*Range = clang_getNullRange();
-
- return CIndexer::createCXString("");
+
+ return createCXString("");
}
-
+
const CodeModificationHint &Hint
= StoredDiag->Info.getCodeModificationHint(FixIt);
if (Range)
*Range = translateSourceRange(StoredDiag->Info.getLocation().getManager(),
*StoredDiag->LangOptsPtr,
Hint.RemoveRange);
- return CIndexer::createCXString(Hint.CodeToInsert);
+ return createCXString(Hint.CodeToInsert);
}
-
+
} // end extern "C"
void clang::ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
StringRef Buffer = F->getBuffer();
const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size();
while (Memory != MemoryEnd) {
- DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr,
+ DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr,
Memory, MemoryEnd);
if (!DB.isActive())
return;
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
+using namespace clang::cxstring;
+
//===----------------------------------------------------------------------===//
// USR generation.
//===----------------------------------------------------------------------===//
USRGenerator UG(Out);
UG.Visit(static_cast<Decl*>(D));
if (UG.ignoreResults())
- return CIndexer::createCXString(NULL);
+ return createCXString(NULL);
}
if (StrBuf.empty())
- return CIndexer::createCXString(NULL);
+ return createCXString(NULL);
// Return a copy of the string that must be disposed by the caller.
- return CIndexer::createCXString(StrBuf.c_str(), true);
+ return createCXString(StrBuf.str(), true);
}
if (Decl *D = cxcursor::getCursorDecl(C))
return ConstructUSR(D);
- return CIndexer::createCXString(NULL);
+ return createCXString(NULL);
}
} // end extern "C"
using namespace clang;
+namespace clang {
+namespace cxstring {
+ CXString createCXString(const char *String, bool DupString = false);
+ CXString createCXString(llvm::StringRef String, bool DupString = true);
+}
+}
+
class CIndexer {
bool UseExternalASTGeneration;
bool OnlyLocalDecls;
/// \brief Get the path of the clang resource files.
std::string getClangResourcesPath();
-
- static CXString createCXString(const char *String, bool DupString = false);
- static CXString createCXString(llvm::StringRef String,
- bool DupString = false);
};
namespace clang {