diff options
author | Roman Divacky <rdivacky@FreeBSD.org> | 2010-03-16 16:52:15 +0000 |
---|---|---|
committer | Roman Divacky <rdivacky@FreeBSD.org> | 2010-03-16 16:52:15 +0000 |
commit | 4a37f65f1c1373c9956d118a012943de2f61edb0 (patch) | |
tree | 52aebaff3a47b97dbac434530524c30967468412 /tools/CIndex | |
parent | a16e9ac1f192503038f49e0c52edd7dcb2ce023a (diff) | |
download | src-4a37f65f1c1373c9956d118a012943de2f61edb0.tar.gz src-4a37f65f1c1373c9956d118a012943de2f61edb0.zip |
Update clang to r98631.
Notes
Notes:
svn path=/vendor/clang/dist/; revision=205219
Diffstat (limited to 'tools/CIndex')
-rw-r--r-- | tools/CIndex/CIndex.cpp | 35 | ||||
-rw-r--r-- | tools/CIndex/CIndex.exports | 6 | ||||
-rw-r--r-- | tools/CIndex/CIndexCodeCompletion.cpp | 5 | ||||
-rw-r--r-- | tools/CIndex/CIndexUSRs.cpp | 198 | ||||
-rw-r--r-- | tools/CIndex/Makefile | 2 |
5 files changed, 193 insertions, 53 deletions
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index b52a32ed9b51..663b32fa1a06 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -1133,7 +1133,8 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, // We failed to load the ASTUnit, but we can still deserialize the // diagnostics and emit them. FileManager FileMgr; - SourceManager SourceMgr; + Diagnostic Diag; + SourceManager SourceMgr(Diag); // FIXME: Faked LangOpts! LangOptions LangOpts; llvm::SmallVector<StoredDiagnostic, 4> Diags; @@ -2042,11 +2043,13 @@ CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); std::pair<FileID, unsigned> LocInfo = CXXUnit->getSourceManager().getDecomposedLoc(Loc); - std::pair<const char *,const char *> Buffer - = CXXUnit->getSourceManager().getBufferData(LocInfo.first); + bool Invalid = false; + llvm::StringRef Buffer + = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); + if (Invalid) + return createCXString(""); - return createCXString(llvm::StringRef(Buffer.first+LocInfo.second, - CXTok.int_data[2])); + return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); } CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { @@ -2095,15 +2098,17 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, return; // Create a lexer - std::pair<const char *,const char *> Buffer - = SourceMgr.getBufferData(BeginLocInfo.first); + bool Invalid = false; + llvm::StringRef Buffer + = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); + Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), CXXUnit->getASTContext().getLangOptions(), - Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second); + Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); Lex.SetCommentRetentionState(true); // Lex tokens until we hit the end of the range. - const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second; + const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; llvm::SmallVector<CXToken, 32> CXTokens; Token Tok; do { @@ -2125,12 +2130,16 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, CXTok.int_data[0] = CXToken_Literal; CXTok.ptr_data = (void *)Tok.getLiteralData(); } else if (Tok.is(tok::identifier)) { - // Lookup the identifier to determine whether we have a + // Lookup the identifier to determine whether we have a keyword. std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Tok.getLocation()); - const char *StartPos - = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first + - LocInfo.second; + bool Invalid = false; + llvm::StringRef Buf + = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); + if (Invalid) + return; + + const char *StartPos = Buf.data() + LocInfo.second; IdentifierInfo *II = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos); CXTok.int_data[0] = II->getTokenID() == tok::identifier? diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports index 5b9530052b51..fe0396d2b140 100644 --- a/tools/CIndex/CIndex.exports +++ b/tools/CIndex/CIndex.exports @@ -2,6 +2,12 @@ _clang_annotateTokens _clang_codeComplete _clang_codeCompleteGetDiagnostic _clang_codeCompleteGetNumDiagnostics +_clang_constructUSR_ObjCClass +_clang_constructUSR_ObjCCategory +_clang_constructUSR_ObjCIvar +_clang_constructUSR_ObjCMethod +_clang_constructUSR_ObjCProtocol +_clang_constructUSR_ObjCProperty _clang_createIndex _clang_createTranslationUnit _clang_createTranslationUnitFromSourceFile diff --git a/tools/CIndex/CIndexCodeCompletion.cpp b/tools/CIndex/CIndexCodeCompletion.cpp index 3b7674ec0d1d..264e5064ddb4 100644 --- a/tools/CIndex/CIndexCodeCompletion.cpp +++ b/tools/CIndex/CIndexCodeCompletion.cpp @@ -187,6 +187,9 @@ struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { /// \brief Diagnostics produced while performing code completion. llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; + /// \brief Diag object + Diagnostic Diag; + /// \brief Language options used to adjust source locations. LangOptions LangOpts; @@ -202,7 +205,7 @@ struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { }; AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() - : CXCodeCompleteResults(), Buffer(0) { } + : CXCodeCompleteResults(), Buffer(0), SourceMgr(Diag) { } AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { for (unsigned I = 0, N = NumResults; I != N; ++I) diff --git a/tools/CIndex/CIndexUSRs.cpp b/tools/CIndex/CIndexUSRs.cpp index 922f4b3fa73f..9dbbd3541e4c 100644 --- a/tools/CIndex/CIndexUSRs.cpp +++ b/tools/CIndex/CIndexUSRs.cpp @@ -29,23 +29,77 @@ class USRGenerator : public DeclVisitor<USRGenerator> { bool IgnoreResults; public: USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {} - + bool ignoreResults() const { return IgnoreResults; } - + + // Visitation methods from generating USRs from AST elements. void VisitBlockDecl(BlockDecl *D); void VisitDeclContext(DeclContext *D); void VisitFieldDecl(FieldDecl *D); void VisitFunctionDecl(FunctionDecl *D); void VisitNamedDecl(NamedDecl *D); void VisitNamespaceDecl(NamespaceDecl *D); - void VisitObjCContainerDecl(ObjCContainerDecl *CD); + void VisitObjCContainerDecl(ObjCContainerDecl *CD); void VisitObjCMethodDecl(ObjCMethodDecl *MD); void VisitObjCPropertyDecl(ObjCPropertyDecl *D); void VisitTagDecl(TagDecl *D); void VisitTypedefDecl(TypedefDecl *D); + + + /// String generation methods used both by the visitation methods + /// and from other clients that want to directly generate USRs. These + /// methods do not construct complete USRs (which incorporate the parents + /// of an AST element), but only the fragments concerning the AST element + /// itself. + + /// Generate a USR fragment for a named declaration. This does + /// not include the USR component for the parent. + void GenNamedDecl(llvm::StringRef name); + + /// Generate a USR for an Objective-C class. + void GenObjCClass(llvm::StringRef cls); + /// Generate a USR for an Objective-C class category. + void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat); + /// Generate a USR fragment for an Objective-C instance variable. The + /// complete USR can be created by concatenating the USR for the + /// encompassing class with this USR fragment. + void GenObjCIvar(llvm::StringRef ivar); + /// Generate a USR fragment for an Objective-C method. + void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod); + /// Generate a USR fragment for an Objective-C property. + void GenObjCProperty(llvm::StringRef prop); + /// Generate a USR for an Objective-C protocol. + void GenObjCProtocol(llvm::StringRef prot); +}; + +class StringUSRGenerator { +private: + llvm::SmallString<1024> StrBuf; + llvm::raw_svector_ostream Out; + USRGenerator UG; +public: + StringUSRGenerator() + : Out(StrBuf), UG(Out) {} + + llvm::StringRef str() { + return Out.str(); + } + + USRGenerator* operator->() { return &UG; } + + template <typename T> + llvm::raw_svector_ostream &operator<<(const T &x) { + Out << x; + return Out; + } }; + } // end anonymous namespace +//===----------------------------------------------------------------------===// +// Generating USRs from ASTS. +//===----------------------------------------------------------------------===// + void USRGenerator::VisitBlockDecl(BlockDecl *D) { VisitDeclContext(D->getDeclContext()); // FIXME: Better support for anonymous blocks. @@ -76,8 +130,8 @@ void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { void USRGenerator::VisitNamedDecl(NamedDecl *D) { VisitDeclContext(D->getDeclContext()); const std::string &s = D->getNameAsString(); -// assert(!s.empty()); - Out << "@^" << s; + // assert(!s.empty()); + GenNamedDecl(s); } void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { @@ -87,8 +141,8 @@ void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { Visit(cast<Decl>(D->getDeclContext())); - Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); - Out << DeclarationName(D->getSelector()).getAsString(); + GenObjCMethod(DeclarationName(D->getSelector()).getAsString(), + D->isInstanceMethod()); } void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { @@ -97,29 +151,29 @@ void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { assert(false && "Invalid ObjC container."); case Decl::ObjCInterface: case Decl::ObjCImplementation: - Out << "objc(cs)" << D->getName(); + GenObjCClass(D->getName()); break; case Decl::ObjCCategory: { ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); - Out << "objc(cy)" << CD->getClassInterface()->getName() - << '^' << CD->getName(); + GenObjCCategory(CD->getClassInterface()->getName(), + CD->getName()); break; } case Decl::ObjCCategoryImpl: { ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); - Out << "objc(cy)" << CD->getClassInterface()->getName() - << '^' << CD->getName(); + GenObjCCategory(CD->getClassInterface()->getName(), + CD->getName()); break; } case Decl::ObjCProtocol: - Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName(); + GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); break; } } void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { Visit(cast<Decl>(D->getDeclContext())); - Out << "(py)" << D->getName(); + GenObjCProperty(D->getName()); } void USRGenerator::VisitTagDecl(TagDecl *D) { @@ -130,12 +184,12 @@ void USRGenerator::VisitTagDecl(TagDecl *D) { case TagDecl::TK_union: Out << "@U^"; break; case TagDecl::TK_enum: Out << "@E^"; break; } - + // FIXME: Better support for anonymous structures and enums. const std::string &s = D->getNameAsString(); if (s.empty()) { if (TypedefDecl *TD = D->getTypedefForAnonDecl()) - Out << "^anontd^" << TD->getNameAsString(); + Out << "^anontd^" << TD->getNameAsString(); else Out << "^anon"; } @@ -146,36 +200,104 @@ void USRGenerator::VisitTagDecl(TagDecl *D) { void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { DeclContext *DC = D->getDeclContext(); if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) - Visit(DCN); + Visit(DCN); Out << "typedef@" << D->getName(); } -// FIXME: This is a skeleton implementation. It will be overhauled. -static CXString ConstructUSR(Decl *D) { - llvm::SmallString<1024> StrBuf; - { - llvm::raw_svector_ostream Out(StrBuf); - USRGenerator UG(Out); - UG.Visit(static_cast<Decl*>(D)); - if (UG.ignoreResults()) - return createCXString(NULL); - } - - if (StrBuf.empty()) - return createCXString(NULL); - - // Return a copy of the string that must be disposed by the caller. - return createCXString(StrBuf.str(), true); -} +//===----------------------------------------------------------------------===// +// General purpose USR generation methods. +//===----------------------------------------------------------------------===// + +void USRGenerator::GenNamedDecl(llvm::StringRef name) { + Out << "@^" << name; +} + +void USRGenerator::GenObjCClass(llvm::StringRef cls) { + Out << "objc(cs)" << cls; +} +void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { + Out << "objc(cy)" << cls << '^' << cat; +} + +void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { + GenNamedDecl(ivar); +} + +void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { + Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; +} + +void USRGenerator::GenObjCProperty(llvm::StringRef prop) { + Out << "(py)" << prop; +} + +void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { + Out << "objc(pl)" << prot; +} + +//===----------------------------------------------------------------------===// +// API hooks. +//===----------------------------------------------------------------------===// extern "C" { CXString clang_getCursorUSR(CXCursor C) { - if (Decl *D = cxcursor::getCursorDecl(C)) - return ConstructUSR(D); - - return createCXString(NULL); + Decl *D = cxcursor::getCursorDecl(C); + if (!D) + return createCXString(NULL); + + StringUSRGenerator SUG; + SUG->Visit(static_cast<Decl*>(D)); + + if (SUG->ignoreResults() || SUG.str().empty()) + return createCXString(NULL); + + // Return a copy of the string that must be disposed by the caller. + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { + StringUSRGenerator SUG; + SUG << clang_getCString(classUSR); + SUG->GenObjCIvar(name); + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCMethod(const char *name, + unsigned isInstanceMethod, + CXString classUSR) { + StringUSRGenerator SUG; + SUG << clang_getCString(classUSR); + SUG->GenObjCMethod(name, isInstanceMethod); + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCClass(const char *name) { + StringUSRGenerator SUG; + SUG->GenObjCClass(name); + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCProtocol(const char *name) { + StringUSRGenerator SUG; + SUG->GenObjCProtocol(name); + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCCategory(const char *class_name, + const char *category_name) { + StringUSRGenerator SUG; + SUG->GenObjCCategory(class_name, category_name); + return createCXString(SUG.str(), true); +} + +CXString clang_constructUSR_ObjCProperty(const char *property, + CXString classUSR) { + StringUSRGenerator SUG; + SUG << clang_getCString(classUSR); + SUG->GenObjCProperty(property); + return createCXString(SUG.str(), true); } } // end extern "C" diff --git a/tools/CIndex/Makefile b/tools/CIndex/Makefile index 3e288623e5eb..650bcd3645d2 100644 --- a/tools/CIndex/Makefile +++ b/tools/CIndex/Makefile @@ -10,7 +10,7 @@ LEVEL = ../../../.. LIBRARYNAME = CIndex -CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include +CPP.Flags += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include # Include this here so we can get the configuration of the targets # that have been configured for construction. We have to do this |