From 37f40572c4c78a8c57a7b45266f8b86db172a7c1 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Wed, 23 Nov 2011 20:27:26 +0000 Subject: [PATCH] [libclang] Indexing API: Fix issues, mostly C++ related. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145107 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDeclObjC.cpp | 5 ++- tools/libclang/IndexDecl.cpp | 6 ++- tools/libclang/IndexingContext.cpp | 67 +++++++++++++++++++----------- tools/libclang/IndexingContext.h | 25 +++++------ 4 files changed, 61 insertions(+), 42 deletions(-) diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp index f4e2fb3205..ebb0cb5463 100644 --- a/lib/Sema/SemaDeclObjC.cpp +++ b/lib/Sema/SemaDeclObjC.cpp @@ -817,9 +817,10 @@ Decl *Sema::ActOnStartCategoryImplementation( if (!CatIDecl) { // Category @implementation with no corresponding @interface. // Create and install one. - CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), - SourceLocation(), SourceLocation(), + CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, + ClassLoc, CatLoc, CatName, IDecl); + CatIDecl->setImplicit(); } } diff --git a/tools/libclang/IndexDecl.cpp b/tools/libclang/IndexDecl.cpp index 777dea1d15..0970054575 100644 --- a/tools/libclang/IndexDecl.cpp +++ b/tools/libclang/IndexDecl.cpp @@ -109,6 +109,9 @@ public: bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) { const ObjCInterfaceDecl *Class = D->getClassInterface(); + if (!Class) + return true; + if (Class->isImplicitInterfaceDecl()) IndexCtx.handleObjCInterface(Class); @@ -128,7 +131,8 @@ public: } bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { - if (D->getCategoryDecl()->getLocation().isInvalid()) + const ObjCCategoryDecl *Cat = D->getCategoryDecl(); + if (!Cat) return true; IndexCtx.handleObjCCategoryImpl(D); diff --git a/tools/libclang/IndexingContext.cpp b/tools/libclang/IndexingContext.cpp index 77d4801113..07c3b4dfb2 100644 --- a/tools/libclang/IndexingContext.cpp +++ b/tools/libclang/IndexingContext.cpp @@ -99,10 +99,13 @@ IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, I = D->bases_begin(), E = D->bases_end(); I != E; ++I) { const CXXBaseSpecifier &Base = *I; BaseEntities.push_back(EntityInfo()); - const CXXRecordDecl *BaseRD = 0; + const NamedDecl *BaseD = 0; if (const RecordType *RT = Base.getType()->getAs()) - BaseRD = dyn_cast_or_null(RT->getDecl()); - IdxCtx.getEntityInfo(BaseRD, BaseEntities.back(), SA); + BaseD = RT->getDecl(); + else if (const TypedefType *TDT = Base.getType()->getAs()) + BaseD = TDT->getDecl(); + if (BaseD) + IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA); CXIdxBaseClassInfo BaseInfo = { 0, MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU), IdxCtx.getIndexLoc(Base.getSourceRange().getBegin()) }; @@ -110,7 +113,7 @@ IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, } for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) { - if (BaseEntities[i].USR) + if (BaseEntities[i].name && BaseEntities[i].USR) BaseInfos[i].base = &BaseEntities[i]; } @@ -123,9 +126,14 @@ const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { return ""; if (Str.data()[Str.size()] == '\0') return Str.data(); - Scratch += Str; - Scratch.push_back('\0'); - return Scratch.data() + (Scratch.size() - Str.size() - 1); + return copyCStr(Str); +} + +const char *IndexingContext::StrAdapter::copyCStr(StringRef Str) { + char *buf = IdxCtx.StrScratch.Allocate(Str.size() + 1); + std::uninitialized_copy(Str.begin(), Str.end(), buf); + buf[Str.size()] = '\0'; + return buf; } void IndexingContext::setASTContext(ASTContext &ctx) { @@ -516,14 +524,21 @@ void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, const NamedDecl *OrigD) { - StrAdapter SA(*this); - CXXClassDeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), - /*isDefinition=*/RD->isThisDeclarationADefinition()); - CXXBasesListInfo BaseList(RD, *this, SA); - DInfo.CXXClassInfo.declInfo = &DInfo; - DInfo.CXXClassInfo.bases = BaseList.getBases(); - DInfo.CXXClassInfo.numBases = BaseList.getNumBases(); + if (RD->isThisDeclarationADefinition()) { + StrAdapter SA(*this); + CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), + /*isDefinition=*/RD->isThisDeclarationADefinition()); + CXXBasesListInfo BaseList(RD, *this, SA); + CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; + CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); + CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); + + return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); + } + DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), + /*isDefinition=*/RD->isThisDeclarationADefinition(), + /*isContainer=*/RD->isThisDeclarationADefinition()); return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); } @@ -675,7 +690,6 @@ void IndexingContext::translateLoc(SourceLocation Loc, void IndexingContext::getEntityInfo(const NamedDecl *D, EntityInfo &EntityInfo, StrAdapter &SA) { - EntityInfo.name = EntityInfo.USR = 0; if (!D) return; @@ -699,7 +713,8 @@ void IndexingContext::getEntityInfo(const NamedDecl *D, } if (const CXXRecordDecl *CXXRec = dyn_cast(D)) { - if (TD->getTagKind() == TTK_Struct && !CXXRec->isPOD()) + if (TD->getTagKind() == TTK_Struct && + CXXRec->hasDefinition() && !CXXRec->isPOD()) EntityInfo.kind = CXIdxEntity_CXXClass; } @@ -805,25 +820,25 @@ void IndexingContext::getEntityInfo(const NamedDecl *D, if (IdentifierInfo *II = D->getIdentifier()) { EntityInfo.name = SA.toCStr(II->getName()); - } else if (isa(D) || isa(D)) { - EntityInfo.name = 0; // anonymous record/namespace. + } else if (isa(D) || isa(D) || isa(D)) { + EntityInfo.name = 0; // anonymous tag/field/namespace. } else { - unsigned Begin = SA.getCurSize(); + llvm::SmallString<256> StrBuf; { - llvm::raw_svector_ostream OS(SA.getBuffer()); + llvm::raw_svector_ostream OS(StrBuf); D->printName(OS); } - EntityInfo.name = SA.getCStr(Begin); + EntityInfo.name = SA.copyCStr(StrBuf.str()); } { - unsigned Begin = SA.getCurSize(); - bool Ignore = getDeclCursorUSR(D, SA.getBuffer()); + llvm::SmallString<512> StrBuf; + bool Ignore = getDeclCursorUSR(D, StrBuf); if (Ignore) { EntityInfo.USR = 0; } else { - EntityInfo.USR = SA.getCStr(Begin); + EntityInfo.USR = SA.copyCStr(StrBuf.str()); } } } @@ -855,6 +870,10 @@ CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { } bool IndexingContext::shouldIgnoreIfImplicit(const NamedDecl *D) { + if (isa(D)) + return false; + if (isa(D)) + return false; if (isa(D)) return false; if (isa(D)) diff --git a/tools/libclang/IndexingContext.h b/tools/libclang/IndexingContext.h index cbf96d8dbb..9a9fb3f4f0 100644 --- a/tools/libclang/IndexingContext.h +++ b/tools/libclang/IndexingContext.h @@ -28,6 +28,10 @@ namespace cxindex { struct EntityInfo : public CXIdxEntityInfo { const NamedDecl *Dcl; IndexingContext *IndexCtx; + + EntityInfo() { + name = USR = 0; + } }; struct ContainerInfo : public CXIdxContainerInfo { @@ -225,35 +229,25 @@ class IndexingContext { SmallVector TUDeclsInObjCContainer; - llvm::SmallString<256> StrScratch; + llvm::BumpPtrAllocator StrScratch; unsigned StrAdapterCount; class StrAdapter { - llvm::SmallString<256> &Scratch; IndexingContext &IdxCtx; public: - StrAdapter(IndexingContext &indexCtx) - : Scratch(indexCtx.StrScratch), IdxCtx(indexCtx) { + StrAdapter(IndexingContext &indexCtx) : IdxCtx(indexCtx) { ++IdxCtx.StrAdapterCount; } ~StrAdapter() { --IdxCtx.StrAdapterCount; if (IdxCtx.StrAdapterCount == 0) - Scratch.clear(); + IdxCtx.StrScratch.Reset(); } const char *toCStr(StringRef Str); - - unsigned getCurSize() const { return Scratch.size(); } - - const char *getCStr(unsigned CharIndex) { - Scratch.push_back('\0'); - return Scratch.data() + CharIndex; - } - - SmallVectorImpl &getBuffer() { return Scratch; } + const char *copyCStr(StringRef Str); }; struct ObjCProtocolListInfo { @@ -305,7 +299,8 @@ public: IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks, unsigned indexOptions, CXTranslationUnit cxTU) : Ctx(0), ClientData(clientData), CB(indexCallbacks), - IndexOptions(indexOptions), CXTU(cxTU), StrAdapterCount(0) { } + IndexOptions(indexOptions), CXTU(cxTU), + StrScratch(/*size=*/1024), StrAdapterCount(0) { } ASTContext &getASTContext() const { return *Ctx; } -- 2.40.0