1 //===- IndexingContext.h - Higher level API functions ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Index_Internal.h" 11 #include "CXCursor.h" 12 13 #include "clang/AST/Decl.h" 14 #include "clang/AST/DeclGroup.h" 15 #include "llvm/ADT/DenseMap.h" 16 17 namespace clang { 18 class FileEntry; 19 class ObjCPropertyDecl; 20 21 namespace cxindex { 22 class IndexingContext; 23 24 class IndexingContext { 25 ASTContext *Ctx; 26 CXClientData ClientData; 27 IndexerCallbacks &CB; 28 unsigned IndexOptions; 29 CXTranslationUnit CXTU; 30 31 typedef llvm::DenseMap<const FileEntry *, CXIdxFile> FileMapTy; 32 typedef llvm::DenseMap<const NamedDecl *, CXIdxEntity> EntityMapTy; 33 typedef llvm::DenseMap<const void *, CXIdxMacro> MacroMapTy; 34 typedef llvm::DenseMap<const DeclContext *, CXIdxContainer> ContainerMapTy; 35 FileMapTy FileMap; 36 EntityMapTy EntityMap; 37 MacroMapTy MacroMap; 38 ContainerMapTy ContainerMap; 39 40 SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer; 41 42 llvm::SmallString<256> StrScratch; 43 44 class StrAdapter { 45 llvm::SmallString<256> &Scratch; 46 47 public: 48 StrAdapter(IndexingContext *indexCtx) 49 : Scratch(indexCtx->StrScratch) {} 50 ~StrAdapter() { Scratch.clear(); } 51 52 const char *toCStr(StringRef Str); 53 54 unsigned getCurSize() const { return Scratch.size(); } 55 56 const char *getCStr(unsigned CharIndex) { 57 Scratch.push_back('\0'); 58 return Scratch.data() + CharIndex; 59 } 60 61 SmallVectorImpl<char> &getBuffer() { return Scratch; } 62 }; 63 64 public: 65 IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks, 66 unsigned indexOptions, CXTranslationUnit cxTU) 67 : Ctx(0), ClientData(clientData), CB(indexCallbacks), 68 IndexOptions(indexOptions), CXTU(cxTU) { } 69 70 ASTContext &getASTContext() const { return *Ctx; } 71 72 void setASTContext(ASTContext &ctx); 73 74 void ppIncludedFile(SourceLocation hashLoc, 75 StringRef filename, const FileEntry *File, 76 bool isImport, bool isAngled); 77 78 void ppMacroDefined(SourceLocation Loc, StringRef Name, 79 SourceLocation DefBegin, unsigned Length, 80 const void *OpaqueMacro); 81 82 void ppMacroUndefined(SourceLocation Loc, StringRef Name, 83 const void *OpaqueMacro); 84 85 void ppMacroExpanded(SourceLocation Loc, StringRef Name, 86 const void *OpaqueMacro); 87 88 void invokeStartedTranslationUnit(); 89 90 void invokeFinishedTranslationUnit(); 91 92 void indexDecl(const Decl *D); 93 94 void indexTagDecl(const TagDecl *D); 95 96 void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, 97 const DeclContext *DC = 0); 98 99 void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, 100 const DeclContext *DC); 101 102 void indexDeclContext(const DeclContext *DC); 103 104 void indexBody(const Stmt *S, const DeclContext *DC); 105 106 void handleDiagnostic(const StoredDiagnostic &StoredDiag); 107 108 void handleFunction(const FunctionDecl *FD); 109 110 void handleVar(const VarDecl *D); 111 112 void handleField(const FieldDecl *D); 113 114 void handleEnumerator(const EnumConstantDecl *D); 115 116 void handleTagDecl(const TagDecl *D); 117 118 void handleTypedef(const TypedefDecl *D); 119 120 void handleObjCInterface(const ObjCInterfaceDecl *D); 121 122 void defineObjCInterface(const ObjCInterfaceDecl *D); 123 124 void handleObjCProtocol(const ObjCProtocolDecl *D); 125 126 void handleObjCCategory(const ObjCCategoryDecl *D); 127 128 void handleObjCMethod(const ObjCMethodDecl *D); 129 130 void handleObjCProperty(const ObjCPropertyDecl *D); 131 132 void handleReference(const NamedDecl *D, SourceLocation Loc, 133 const NamedDecl *Parent, 134 const DeclContext *DC, 135 const Expr *E = 0, 136 CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct); 137 138 void invokeStartedTagTypeDefinition(const TagDecl *D); 139 140 void invokeStartedStatementBody(const NamedDecl *D, const DeclContext *DC); 141 142 void invokeStartedObjCContainer(const ObjCContainerDecl *D); 143 144 void invokeEndedContainer(const DeclContext *DC); 145 146 bool isNotFromSourceFile(SourceLocation Loc) const; 147 148 void indexTUDeclsInObjCContainer(); 149 void indexDeclGroupRef(DeclGroupRef DG); 150 151 void addTUDeclInObjCContainer(DeclGroupRef DG) { 152 TUDeclsInObjCContainer.push_back(DG); 153 } 154 155 void translateLoc(SourceLocation Loc, CXIdxFile *indexFile, CXFile *file, 156 unsigned *line, unsigned *column, unsigned *offset); 157 158 private: 159 void addEntityInMap(const NamedDecl *D, CXIdxEntity entity); 160 161 void addContainerInMap(const DeclContext *DC, CXIdxContainer container); 162 163 CXIdxEntity getIndexEntity(const NamedDecl *D); 164 165 const NamedDecl *getEntityDecl(const NamedDecl *D) const; 166 167 CXIdxContainer getIndexContainer(const NamedDecl *D) const { 168 return getIndexContainerForDC(D->getDeclContext()); 169 } 170 171 const DeclContext *getScopedContext(const DeclContext *DC) const; 172 CXIdxContainer getIndexContainerForDC(const DeclContext *DC) const; 173 174 CXIdxFile getIndexFile(const FileEntry *File); 175 176 CXIdxLoc getIndexLoc(SourceLocation Loc) const; 177 178 void getIndexedEntityInfo(const NamedDecl *D, 179 CXIdxIndexedEntityInfo &IdxEntityInfo, 180 CXIdxEntityInfo &EntityInfo, 181 CXIdxIndexedDeclInfo &IdxDeclInfo, 182 StrAdapter &SA); 183 184 void getIndexedDeclInfo(const NamedDecl *D, 185 CXIdxIndexedDeclInfo &IdxDeclInfo); 186 187 void getIndexedRedeclInfo(const NamedDecl *D, 188 CXIdxIndexedRedeclInfo &RedeclInfo, 189 CXIdxIndexedDeclInfo &IdxDeclInfo); 190 191 void getContainerInfo(const NamedDecl *D, 192 CXIdxContainerInfo &ContainerInfo); 193 194 void getEntityInfo(const NamedDecl *D, 195 CXIdxEntityInfo &EntityInfo, 196 StrAdapter &SA); 197 198 CXCursor getCursor(const NamedDecl *D) { 199 return cxcursor::MakeCXCursor(const_cast<NamedDecl*>(D), CXTU); 200 } 201 202 CXCursor getRefCursor(const NamedDecl *D, SourceLocation Loc); 203 }; 204 205 }} // end clang::cxindex 206