[cfe-commits] r144763 - in /cfe/trunk/tools: c-index-test/c-index-test.c libclang/IndexingContext.cpp libclang/IndexingContext.h
Argyrios Kyrtzidis
akyrtzi at gmail.com
Tue Nov 15 18:34:59 PST 2011
Author: akirtzidis
Date: Tue Nov 15 20:34:59 2011
New Revision: 144763
URL: http://llvm.org/viewvc/llvm-project?rev=144763&view=rev
Log:
[libclang] Indexing API: if the CXIndexOpt_OneRefPerFile option is set, only report one reference
per file.
Modified:
cfe/trunk/tools/c-index-test/c-index-test.c
cfe/trunk/tools/libclang/IndexingContext.cpp
cfe/trunk/tools/libclang/IndexingContext.h
Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=144763&r1=144762&r2=144763&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Tue Nov 15 20:34:59 2011
@@ -1645,6 +1645,11 @@
index_data = (IndexData *)client_data;
printCheck(index_data);
+ if (!info) {
+ printf("%s: <<NULL>>", cb);
+ return;
+ }
+
name = info->name;
if (!name)
name = "<anon-tag>";
@@ -1848,7 +1853,8 @@
result = clang_indexSourceFile(CIdx, &index_data,
&IndexCB,sizeof(IndexCB),
- 0, 0, argv, argc, 0, 0, 0, 0);
+ CXIndexOpt_OneRefPerFile,
+ 0, argv, argc, 0, 0, 0, 0);
if (index_data.fail_for_error)
return -1;
@@ -1890,7 +1896,8 @@
index_data.fail_for_error = 0;
result = clang_indexTranslationUnit(TU, &index_data,
- &IndexCB,sizeof(IndexCB), 0);
+ &IndexCB,sizeof(IndexCB),
+ CXIndexOpt_OneRefPerFile);
if (index_data.fail_for_error)
return -1;
Modified: cfe/trunk/tools/libclang/IndexingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/IndexingContext.cpp?rev=144763&r1=144762&r2=144763&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/IndexingContext.cpp (original)
+++ cfe/trunk/tools/libclang/IndexingContext.cpp Tue Nov 15 20:34:59 2011
@@ -239,12 +239,17 @@
const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
SourceLocation ClassLoc = D->getLocation();
SourceLocation CategoryLoc = D->getCategoryNameLoc();
- getEntityInfo(D->getClassInterface(), ClassEntity, SA);
+ getEntityInfo(IFaceD, ClassEntity, SA);
CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
- CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
- CatDInfo.ObjCCatDeclInfo.classCursor =
- MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
+ if (IFaceD) {
+ CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
+ CatDInfo.ObjCCatDeclInfo.classCursor =
+ MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
+ } else {
+ CatDInfo.ObjCCatDeclInfo.objcClass = 0;
+ CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
+ }
CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
}
@@ -285,6 +290,27 @@
if (isNotFromSourceFile(D->getLocation()))
return;
+ D = getEntityDecl(D);
+
+ if (onlyOneRefPerFile()) {
+ SourceManager &SM = Ctx->getSourceManager();
+ SourceLocation FileLoc = SM.getFileLoc(Loc);
+
+ std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
+ FileID FID = LocInfo.first;
+ if (FID.isInvalid())
+ return;
+
+ const FileEntry *FE = SM.getFileEntryForID(FID);
+ if (!FE)
+ return;
+ RefFileOccurence RefOccur(FE, D);
+ std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
+ res = RefFileOccurences.insert(RefOccur);
+ if (!res.second)
+ return; // already in map.
+ }
+
StrAdapter SA(*this);
CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
const_cast<Decl*>(cast<Decl>(DC)), CXTU)
@@ -296,7 +322,7 @@
CXIdxEntityRefInfo Info = { Cursor,
getIndexLoc(Loc),
&RefEntity,
- &ParentEntity,
+ Parent ? &ParentEntity : 0,
getIndexContainerForDC(DC),
Kind };
CB.indexEntityReference(ClientData, &Info);
@@ -431,6 +457,8 @@
void IndexingContext::getEntityInfo(const NamedDecl *D,
CXIdxEntityInfo &EntityInfo,
StrAdapter &SA) {
+ if (!D)
+ return;
D = getEntityDecl(D);
EntityInfo.kind = CXIdxEntity_Unexposed;
Modified: cfe/trunk/tools/libclang/IndexingContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/IndexingContext.h?rev=144763&r1=144762&r2=144763&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/IndexingContext.h (original)
+++ cfe/trunk/tools/libclang/IndexingContext.h Tue Nov 15 20:34:59 2011
@@ -12,7 +12,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclGroup.h"
-#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
namespace clang {
class FileEntry;
@@ -133,6 +133,14 @@
static bool classof(const ObjCCategoryDeclInfo *D) { return true; }
};
+struct RefFileOccurence {
+ const FileEntry *File;
+ const Decl *Dcl;
+
+ RefFileOccurence(const FileEntry *File, const Decl *Dcl)
+ : File(File), Dcl(Dcl) { }
+};
+
class IndexingContext {
ASTContext *Ctx;
CXClientData ClientData;
@@ -145,6 +153,8 @@
FileMapTy FileMap;
ContainerMapTy ContainerMap;
+ llvm::DenseSet<RefFileOccurence> RefFileOccurences;
+
SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer;
llvm::SmallString<256> StrScratch;
@@ -204,6 +214,10 @@
void setASTContext(ASTContext &ctx);
+ bool onlyOneRefPerFile() const {
+ return IndexOptions & CXIndexOpt_OneRefPerFile;
+ }
+
void enteredMainFile(const FileEntry *File);
void ppIncludedFile(SourceLocation hashLoc,
@@ -313,3 +327,31 @@
};
}} // end clang::cxindex
+
+namespace llvm {
+ /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
+ /// DenseSets.
+ template <>
+ struct DenseMapInfo<clang::cxindex::RefFileOccurence> {
+ static inline clang::cxindex::RefFileOccurence getEmptyKey() {
+ return clang::cxindex::RefFileOccurence(0, 0);
+ }
+
+ static inline clang::cxindex::RefFileOccurence getTombstoneKey() {
+ return clang::cxindex::RefFileOccurence((const clang::FileEntry *)~0,
+ (const clang::Decl *)~0);
+ }
+
+ static unsigned getHashValue(clang::cxindex::RefFileOccurence S) {
+ llvm::FoldingSetNodeID ID;
+ ID.AddPointer(S.File);
+ ID.AddPointer(S.Dcl);
+ return ID.ComputeHash();
+ }
+
+ static bool isEqual(clang::cxindex::RefFileOccurence LHS,
+ clang::cxindex::RefFileOccurence RHS) {
+ return LHS.File == RHS.File && LHS.Dcl == RHS.Dcl;
+ }
+ };
+}
More information about the cfe-commits
mailing list