Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - 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 "IndexingContext.h" |
| 11 | #include "CXTranslationUnit.h" |
| 12 | #include "CIndexDiagnostic.h" |
| 13 | |
| 14 | #include "clang/Frontend/ASTUnit.h" |
| 15 | #include "clang/AST/DeclObjC.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace cxindex; |
| 19 | using namespace cxcursor; |
| 20 | |
| 21 | const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { |
| 22 | if (Str.empty()) |
| 23 | return ""; |
| 24 | if (Str.data()[Str.size()] == '\0') |
| 25 | return Str.data(); |
| 26 | Scratch += Str; |
| 27 | Scratch.push_back('\0'); |
| 28 | return Scratch.data() + (Scratch.size() - Str.size() - 1); |
| 29 | } |
| 30 | |
| 31 | void IndexingContext::setASTContext(ASTContext &ctx) { |
| 32 | Ctx = &ctx; |
| 33 | static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); |
| 34 | } |
| 35 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 36 | void IndexingContext::enteredMainFile(const FileEntry *File) { |
| 37 | if (File && CB.enteredMainFile) { |
| 38 | CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0); |
| 39 | FileMap[File] = idxFile; |
| 40 | } |
| 41 | } |
| 42 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 43 | void IndexingContext::ppIncludedFile(SourceLocation hashLoc, |
| 44 | StringRef filename, |
| 45 | const FileEntry *File, |
| 46 | bool isImport, bool isAngled) { |
| 47 | if (!CB.ppIncludedFile) |
| 48 | return; |
| 49 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 50 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 51 | CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), |
| 52 | SA.toCStr(filename), |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 53 | (CXFile)File, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 54 | isImport, isAngled }; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 55 | CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); |
| 56 | FileMap[File] = idxFile; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void IndexingContext::ppMacroDefined(SourceLocation Loc, StringRef Name, |
| 60 | SourceLocation DefBegin, unsigned Length, |
| 61 | const void *OpaqueMacro) { |
| 62 | if (!CB.ppMacroDefined) |
| 63 | return; |
| 64 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 65 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 66 | CXIdxMacroInfo MacroInfo = { getIndexLoc(Loc), SA.toCStr(Name) }; |
| 67 | CXIdxMacroDefinedInfo Info = { &MacroInfo, |
| 68 | getIndexLoc(DefBegin), Length }; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 69 | CXIdxClientMacro idxMacro = CB.ppMacroDefined(ClientData, &Info); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 70 | MacroMap[OpaqueMacro] = idxMacro; |
| 71 | } |
| 72 | |
| 73 | void IndexingContext::ppMacroUndefined(SourceLocation Loc, StringRef Name, |
| 74 | const void *OpaqueMacro) { |
| 75 | if (!CB.ppMacroUndefined) |
| 76 | return; |
| 77 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 78 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 79 | CXIdxMacroUndefinedInfo Info = { getIndexLoc(Loc), |
| 80 | SA.toCStr(Name), 0 }; |
| 81 | CB.ppMacroUndefined(ClientData, &Info); |
| 82 | } |
| 83 | |
| 84 | void IndexingContext::ppMacroExpanded(SourceLocation Loc, StringRef Name, |
| 85 | const void *OpaqueMacro) { |
| 86 | if (!CB.ppMacroExpanded) |
| 87 | return; |
| 88 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 89 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 90 | CXIdxMacroExpandedInfo Info = { getIndexLoc(Loc), |
| 91 | SA.toCStr(Name), 0 }; |
| 92 | CB.ppMacroExpanded(ClientData, &Info); |
| 93 | } |
| 94 | |
| 95 | void IndexingContext::invokeStartedTranslationUnit() { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 96 | CXIdxClientContainer idxCont = 0; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 97 | if (CB.startedTranslationUnit) |
| 98 | idxCont = CB.startedTranslationUnit(ClientData, 0); |
| 99 | addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); |
| 100 | } |
| 101 | |
| 102 | void IndexingContext::invokeFinishedTranslationUnit() { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 103 | endContainer(Ctx->getTranslationUnitDecl()); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void IndexingContext::handleDiagnostic(const StoredDiagnostic &StoredDiag) { |
| 107 | if (!CB.diagnostic) |
| 108 | return; |
| 109 | |
| 110 | CXStoredDiagnostic CXDiag(StoredDiag, Ctx->getLangOptions()); |
| 111 | CB.diagnostic(ClientData, &CXDiag, 0); |
| 112 | } |
| 113 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 114 | void IndexingContext::handleDecl(const NamedDecl *D, |
| 115 | SourceLocation Loc, CXCursor Cursor, |
| 116 | bool isRedeclaration, bool isDefinition, |
| 117 | DeclInfo &DInfo) { |
| 118 | if (!CB.indexDeclaration) |
| 119 | return; |
| 120 | |
| 121 | StrAdapter SA(*this); |
| 122 | getEntityInfo(D, DInfo.CXEntInfo, SA); |
| 123 | DInfo.entityInfo = &DInfo.CXEntInfo; |
| 124 | DInfo.cursor = Cursor; |
| 125 | DInfo.loc = getIndexLoc(Loc); |
| 126 | DInfo.container = getIndexContainer(D); |
| 127 | DInfo.isRedeclaration = isRedeclaration; |
| 128 | DInfo.isDefinition = isDefinition; |
| 129 | |
| 130 | CXIdxClientEntity |
| 131 | clientEnt = CB.indexDeclaration(ClientData, &DInfo); |
| 132 | |
| 133 | if (!isRedeclaration) |
| 134 | addEntityInMap(D, clientEnt); |
| 135 | } |
| 136 | |
| 137 | void IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, |
| 138 | SourceLocation Loc, CXCursor Cursor, |
| 139 | bool isForwardRef, |
| 140 | bool isRedeclaration, |
| 141 | bool isImplementation, |
| 142 | ObjCContainerDeclInfo &ContDInfo) { |
| 143 | ContDInfo.CXObjCContDeclInfo.declInfo = &ContDInfo; |
| 144 | if (isForwardRef) |
| 145 | ContDInfo.CXObjCContDeclInfo.kind = CXIdxObjCContainer_ForwardRef; |
| 146 | else if (isImplementation) |
| 147 | ContDInfo.CXObjCContDeclInfo.kind = CXIdxObjCContainer_Implementation; |
| 148 | else |
| 149 | ContDInfo.CXObjCContDeclInfo.kind = CXIdxObjCContainer_Interface; |
| 150 | |
| 151 | handleDecl(D, Loc, Cursor, |
| 152 | isRedeclaration, /*isDefinition=*/!isForwardRef, ContDInfo); |
| 153 | } |
| 154 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 155 | void IndexingContext::handleFunction(const FunctionDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 156 | DeclInfo DInfo; |
| 157 | handleDecl(D, D->getLocation(), getCursor(D), |
| 158 | !D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 159 | DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void IndexingContext::handleVar(const VarDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 163 | DeclInfo DInfo; |
| 164 | handleDecl(D, D->getLocation(), getCursor(D), |
| 165 | !D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 166 | DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void IndexingContext::handleField(const FieldDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 170 | DeclInfo DInfo; |
| 171 | handleDecl(D, D->getLocation(), getCursor(D), |
| 172 | /*isRedeclaration=*/false, /*isDefinition=*/false, DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void IndexingContext::handleEnumerator(const EnumConstantDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 176 | DeclInfo DInfo; |
| 177 | handleDecl(D, D->getLocation(), getCursor(D), |
| 178 | /*isRedeclaration=*/false, /*isDefinition=*/true, DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void IndexingContext::handleTagDecl(const TagDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 182 | TagDeclInfo TagDInfo; |
| 183 | TagDInfo.CXTagDeclInfo.declInfo = &TagDInfo; |
| 184 | TagDInfo.CXTagDeclInfo.isAnonymous = D->getIdentifier() == 0; |
| 185 | handleDecl(D, D->getLocation(), getCursor(D), |
| 186 | !D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 187 | TagDInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void IndexingContext::handleTypedef(const TypedefDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 191 | DeclInfo DInfo; |
| 192 | handleDecl(D, D->getLocation(), getCursor(D), |
| 193 | !D->isFirstDeclaration(), /*isDefinition=*/true, DInfo); |
| 194 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 195 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 196 | void IndexingContext::handleObjCClass(const ObjCClassDecl *D) { |
| 197 | ObjCContainerDeclInfo ContDInfo; |
| 198 | const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); |
| 199 | ObjCInterfaceDecl *IFaceD = Ref->getInterface(); |
| 200 | SourceLocation Loc = Ref->getLocation(); |
| 201 | bool isRedeclaration = IFaceD->getLocation() != Loc; |
| 202 | handleObjCContainer(IFaceD, Loc, MakeCursorObjCClassRef(IFaceD, Loc, CXTU), |
| 203 | /*isForwardRef=*/true, isRedeclaration, |
| 204 | /*isImplementation=*/false, ContDInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 208 | ObjCContainerDeclInfo ContDInfo; |
| 209 | handleObjCContainer(D, D->getLocation(), getCursor(D), |
| 210 | /*isForwardRef=*/false, |
| 211 | /*isRedeclaration=*/D->isInitiallyForwardDecl(), |
| 212 | /*isImplementation=*/false, ContDInfo); |
| 213 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 214 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 215 | void IndexingContext::handleObjCImplementation( |
| 216 | const ObjCImplementationDecl *D) { |
| 217 | ObjCContainerDeclInfo ContDInfo; |
| 218 | const ObjCInterfaceDecl *Class = D->getClassInterface(); |
| 219 | handleObjCContainer(Class, D->getLocation(), getCursor(D), |
| 220 | /*isForwardRef=*/false, |
| 221 | /*isRedeclaration=*/!Class->isImplicitInterfaceDecl(), |
| 222 | /*isImplementation=*/true, ContDInfo); |
| 223 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 225 | void IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, |
| 226 | SourceLocation Loc, |
| 227 | bool isRedeclaration) { |
| 228 | ObjCContainerDeclInfo ContDInfo; |
| 229 | handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), |
| 230 | /*isForwardRef=*/true, |
| 231 | isRedeclaration, |
| 232 | /*isImplementation=*/false, ContDInfo); |
| 233 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 234 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 235 | void IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { |
| 236 | ObjCContainerDeclInfo ContDInfo; |
| 237 | handleObjCContainer(D, D->getLocation(), getCursor(D), |
| 238 | /*isForwardRef=*/false, |
| 239 | /*isRedeclaration=*/D->isInitiallyForwardDecl(), |
| 240 | /*isImplementation=*/false, ContDInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void IndexingContext::defineObjCInterface(const ObjCInterfaceDecl *D) { |
| 244 | if (!CB.defineObjCClass) |
| 245 | return; |
| 246 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 247 | StrAdapter SA(*this); |
| 248 | CXIdxObjCBaseClassInfo BaseClass; |
| 249 | CXIdxEntityInfo BaseEntity; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 250 | if (D->getSuperClass()) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 251 | getEntityInfo(D->getSuperClass(), BaseEntity, SA); |
| 252 | BaseClass.objcClass = &BaseEntity; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 253 | BaseClass.loc = getIndexLoc(D->getSuperClassLoc()); |
| 254 | } |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 255 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 256 | SmallVector<CXIdxObjCProtocolRefInfo, 4> ProtInfos; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 257 | SmallVector<CXIdxEntityInfo, 4> ProtEntities; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 258 | ObjCInterfaceDecl::protocol_loc_iterator LI = D->protocol_loc_begin(); |
| 259 | for (ObjCInterfaceDecl::protocol_iterator |
| 260 | I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I, ++LI) { |
| 261 | SourceLocation Loc = *LI; |
| 262 | ObjCProtocolDecl *PD = *I; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 263 | ProtEntities.push_back(CXIdxEntityInfo()); |
| 264 | getEntityInfo(PD, ProtEntities.back(), SA); |
| 265 | CXIdxObjCProtocolRefInfo ProtInfo = { 0, getIndexLoc(Loc) }; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 266 | ProtInfos.push_back(ProtInfo); |
| 267 | } |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 268 | |
| 269 | for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) |
| 270 | ProtInfos[i].protocol = &ProtEntities[i]; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 271 | |
| 272 | SmallVector<CXIdxObjCProtocolRefInfo *, 4> Prots; |
| 273 | for (unsigned i = 0, e = Prots.size(); i != e; ++i) |
| 274 | Prots.push_back(&ProtInfos[i]); |
| 275 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 276 | CXIdxEntityInfo ClassEntity; |
| 277 | getEntityInfo(D, ClassEntity, SA); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 278 | CXIdxObjCClassDefineInfo Info = { getCursor(D), |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 279 | &ClassEntity, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 280 | getIndexContainerForDC(D), |
| 281 | D->getSuperClass() ? &BaseClass : 0, |
| 282 | Prots.data(), |
Matt Beaumont-Gay | cb6e5c1 | 2011-10-17 22:19:09 +0000 | [diff] [blame] | 283 | static_cast<unsigned>(Prots.size()) }; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 284 | CB.defineObjCClass(ClientData, &Info); |
| 285 | } |
| 286 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 287 | void IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { |
| 288 | ObjCCategoryDeclInfo CatDInfo; |
| 289 | CXIdxEntityInfo ClassEntity; |
| 290 | StrAdapter SA(*this); |
| 291 | getEntityInfo(D->getClassInterface(), ClassEntity, SA); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 292 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 293 | CatDInfo.CXObjCCatDeclInfo.containerInfo = &CatDInfo.CXObjCContDeclInfo; |
| 294 | CatDInfo.CXObjCCatDeclInfo.objcClass = &ClassEntity; |
| 295 | handleObjCContainer(D, D->getLocation(), getCursor(D), |
| 296 | /*isForwardRef=*/false, |
| 297 | /*isRedeclaration=*/false, |
| 298 | /*isImplementation=*/false, CatDInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 301 | void IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { |
| 302 | const ObjCCategoryDecl *CatD = D->getCategoryDecl(); |
| 303 | ObjCCategoryDeclInfo CatDInfo; |
| 304 | CXIdxEntityInfo ClassEntity; |
| 305 | StrAdapter SA(*this); |
| 306 | getEntityInfo(CatD->getClassInterface(), ClassEntity, SA); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 307 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 308 | CatDInfo.CXObjCCatDeclInfo.containerInfo = &CatDInfo.CXObjCContDeclInfo; |
| 309 | CatDInfo.CXObjCCatDeclInfo.objcClass = &ClassEntity; |
| 310 | handleObjCContainer(CatD, D->getLocation(), getCursor(D), |
| 311 | /*isForwardRef=*/false, |
| 312 | /*isRedeclaration=*/true, |
| 313 | /*isImplementation=*/true, CatDInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 317 | DeclInfo DInfo; |
| 318 | handleDecl(D, D->getLocation(), getCursor(D), |
| 319 | !D->isCanonicalDecl(), D->isThisDeclarationADefinition(), |
| 320 | DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 324 | DeclInfo DInfo; |
| 325 | handleDecl(D, D->getLocation(), getCursor(D), |
| 326 | /*isRedeclaration=*/false, /*isDefinition=*/false, |
| 327 | DInfo); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, |
| 331 | const NamedDecl *Parent, |
| 332 | const DeclContext *DC, |
Argyrios Kyrtzidis | 0c7735e5 | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 333 | const Expr *E, |
| 334 | CXIdxEntityRefKind Kind) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 335 | if (Loc.isInvalid()) |
| 336 | return; |
| 337 | if (!CB.indexEntityReference) |
| 338 | return; |
| 339 | if (isNotFromSourceFile(D->getLocation())) |
| 340 | return; |
| 341 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 342 | StrAdapter SA(*this); |
Benjamin Kramer | f1b4e00 | 2011-10-28 13:37:11 +0000 | [diff] [blame] | 343 | CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), |
| 344 | const_cast<Decl*>(cast<Decl>(DC)), CXTU) |
| 345 | : getRefCursor(D, Loc); |
| 346 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 347 | CXIdxEntityInfo RefEntity, ParentEntity; |
| 348 | getEntityInfo(D, RefEntity, SA); |
| 349 | getEntityInfo(Parent, ParentEntity, SA); |
Benjamin Kramer | f1b4e00 | 2011-10-28 13:37:11 +0000 | [diff] [blame] | 350 | CXIdxEntityRefInfo Info = { Cursor, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 351 | getIndexLoc(Loc), |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 352 | &RefEntity, |
| 353 | &ParentEntity, |
Argyrios Kyrtzidis | 0c7735e5 | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 354 | getIndexContainerForDC(DC), |
| 355 | Kind }; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 356 | CB.indexEntityReference(ClientData, &Info); |
| 357 | } |
| 358 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 359 | void IndexingContext::startContainer(const NamedDecl *D, bool isStmtBody, |
| 360 | const DeclContext *DC) { |
| 361 | if (!CB.startedContainer) |
| 362 | return; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 363 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 364 | if (!DC) |
| 365 | DC = cast<DeclContext>(D); |
| 366 | |
| 367 | StrAdapter SA(*this); |
| 368 | CXIdxEntityInfo Entity; |
| 369 | getEntityInfo(D, Entity, SA); |
| 370 | CXIdxContainerInfo Info; |
| 371 | Info.entity = &Entity; |
| 372 | Info.cursor = getCursor(D); |
| 373 | Info.loc = getIndexLoc(D->getLocation()); |
| 374 | Info.isObjCImpl = isa<ObjCImplDecl>(D); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 375 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 376 | CXIdxClientContainer clientCont = CB.startedContainer(ClientData, &Info); |
| 377 | addContainerInMap(DC, clientCont); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 380 | void IndexingContext::endContainer(const DeclContext *DC) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 381 | if (CB.endedContainer) { |
| 382 | CXIdxEndContainerInfo Info = { getIndexContainerForDC(DC), |
| 383 | getIndexLoc(cast<Decl>(DC)->getLocEnd()) }; |
| 384 | CB.endedContainer(ClientData, &Info); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { |
| 389 | if (Loc.isInvalid()) |
| 390 | return true; |
| 391 | SourceManager &SM = Ctx->getSourceManager(); |
| 392 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 393 | FileID FID = SM.getFileID(FileLoc); |
| 394 | return SM.getFileEntryForID(FID) == 0; |
| 395 | } |
| 396 | |
| 397 | void IndexingContext::addContainerInMap(const DeclContext *DC, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 398 | CXIdxClientContainer container) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 399 | assert(getScopedContext(DC) == DC); |
| 400 | ContainerMapTy::iterator I = ContainerMap.find(DC); |
| 401 | if (I == ContainerMap.end()) { |
| 402 | if (container) |
| 403 | ContainerMap[DC] = container; |
| 404 | return; |
| 405 | } |
| 406 | // Allow changing the container of a previously seen DeclContext so we |
| 407 | // can handle invalid user code, like a function re-definition. |
| 408 | if (container) |
| 409 | I->second = container; |
| 410 | else |
| 411 | ContainerMap.erase(I); |
| 412 | } |
| 413 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 414 | void IndexingContext::addEntityInMap(const NamedDecl *D, |
| 415 | CXIdxClientEntity entity) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 416 | assert(getEntityDecl(D) == D && |
| 417 | "Tried to add a non-entity (canonical) decl"); |
| 418 | assert(EntityMap.find(D) == EntityMap.end()); |
| 419 | if (entity || D->isFromASTFile()) |
| 420 | EntityMap[D] = entity; |
| 421 | } |
| 422 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 423 | CXIdxClientEntity IndexingContext::getClientEntity(const NamedDecl *D) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 424 | if (!D) |
| 425 | return 0; |
| 426 | D = getEntityDecl(D); |
| 427 | EntityMapTy::const_iterator I = EntityMap.find(D); |
| 428 | if (I != EntityMap.end()) |
| 429 | return I->second; |
| 430 | |
| 431 | if (!D->isFromASTFile()) { |
| 432 | //assert(0 && "Entity not in map"); |
| 433 | return 0; |
| 434 | } |
| 435 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 436 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 437 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 438 | CXIdxClientEntity idxEntity = 0; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 439 | if (CB.importedEntity) { |
| 440 | CXIdxEntityInfo EntityInfo; |
| 441 | getEntityInfo(D, EntityInfo, SA); |
| 442 | CXIdxImportedEntityInfo Info = { &EntityInfo, |
| 443 | getCursor(D), |
| 444 | getIndexLoc(D->getLocation()), |
| 445 | /*CXIdxASTFile*/0 }; |
| 446 | idxEntity = CB.importedEntity(ClientData, &Info); |
| 447 | } |
| 448 | addEntityInMap(D, idxEntity); |
| 449 | return idxEntity; |
| 450 | } |
| 451 | |
| 452 | const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { |
| 453 | assert(D); |
| 454 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 455 | |
| 456 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(D)) { |
| 457 | if (Cat->IsClassExtension()) |
| 458 | return getEntityDecl(Cat->getClassInterface()); |
| 459 | |
| 460 | } else if (const ObjCImplementationDecl * |
| 461 | ImplD = dyn_cast<ObjCImplementationDecl>(D)) { |
| 462 | return getEntityDecl(ImplD->getClassInterface()); |
| 463 | |
| 464 | } else if (const ObjCCategoryImplDecl * |
| 465 | CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 466 | return getEntityDecl(CatImplD->getCategoryDecl()); |
| 467 | } |
| 468 | |
| 469 | return D; |
| 470 | } |
| 471 | |
| 472 | const DeclContext * |
| 473 | IndexingContext::getScopedContext(const DeclContext *DC) const { |
| 474 | // Local contexts are ignored for indexing. |
| 475 | const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); |
| 476 | if (FuncCtx) |
| 477 | return FuncCtx; |
| 478 | |
| 479 | // We consider enums always scoped for indexing. |
| 480 | if (isa<TagDecl>(DC)) |
| 481 | return DC; |
| 482 | |
| 483 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 484 | if (NS->isAnonymousNamespace()) |
| 485 | return getScopedContext(NS->getParent()); |
| 486 | return NS; |
| 487 | } |
| 488 | |
| 489 | return DC->getRedeclContext(); |
| 490 | } |
| 491 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 492 | CXIdxClientContainer |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 493 | IndexingContext::getIndexContainerForDC(const DeclContext *DC) const { |
| 494 | DC = getScopedContext(DC); |
| 495 | ContainerMapTy::const_iterator I = ContainerMap.find(DC); |
| 496 | // assert(I != ContainerMap.end() && |
| 497 | // "Failed to include a scoped context in the container map"); |
| 498 | return I->second; |
| 499 | } |
| 500 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 501 | CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 502 | if (!File) |
| 503 | return 0; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 504 | |
| 505 | FileMapTy::iterator FI = FileMap.find(File); |
| 506 | if (FI != FileMap.end()) |
| 507 | return FI->second; |
| 508 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 509 | return 0; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { |
| 513 | CXIdxLoc idxLoc = { {0, 0}, 0 }; |
| 514 | if (Loc.isInvalid()) |
| 515 | return idxLoc; |
| 516 | |
| 517 | idxLoc.ptr_data[0] = (void*)this; |
| 518 | idxLoc.int_data = Loc.getRawEncoding(); |
| 519 | return idxLoc; |
| 520 | } |
| 521 | |
| 522 | void IndexingContext::translateLoc(SourceLocation Loc, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 523 | CXIdxClientFile *indexFile, CXFile *file, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 524 | unsigned *line, unsigned *column, |
| 525 | unsigned *offset) { |
| 526 | if (Loc.isInvalid()) |
| 527 | return; |
| 528 | |
| 529 | SourceManager &SM = Ctx->getSourceManager(); |
| 530 | Loc = SM.getFileLoc(Loc); |
| 531 | |
| 532 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 533 | FileID FID = LocInfo.first; |
| 534 | unsigned FileOffset = LocInfo.second; |
| 535 | |
| 536 | if (FID.isInvalid()) |
| 537 | return; |
| 538 | |
| 539 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 540 | if (indexFile) |
| 541 | *indexFile = getIndexFile(FE); |
| 542 | if (file) |
| 543 | *file = (void *)FE; |
| 544 | if (line) |
| 545 | *line = SM.getLineNumber(FID, FileOffset); |
| 546 | if (column) |
| 547 | *column = SM.getColumnNumber(FID, FileOffset); |
| 548 | if (offset) |
| 549 | *offset = FileOffset; |
| 550 | } |
| 551 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 552 | void IndexingContext::getEntityInfo(const NamedDecl *D, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 553 | CXIdxEntityInfo &EntityInfo, |
| 554 | StrAdapter &SA) { |
| 555 | D = getEntityDecl(D); |
| 556 | EntityInfo.kind = CXIdxEntity_Unexposed; |
| 557 | EntityInfo.clientEntity = getClientEntity(D); |
| 558 | |
| 559 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 560 | switch (TD->getTagKind()) { |
| 561 | case TTK_Struct: |
| 562 | EntityInfo.kind = CXIdxEntity_Struct; break; |
| 563 | case TTK_Union: |
| 564 | EntityInfo.kind = CXIdxEntity_Union; break; |
| 565 | case TTK_Class: |
| 566 | EntityInfo.kind = CXIdxEntity_CXXClass; break; |
| 567 | case TTK_Enum: |
| 568 | EntityInfo.kind = CXIdxEntity_Enum; break; |
| 569 | } |
| 570 | |
| 571 | } else { |
| 572 | switch (D->getKind()) { |
| 573 | case Decl::Typedef: |
| 574 | EntityInfo.kind = CXIdxEntity_Typedef; break; |
| 575 | case Decl::Function: |
| 576 | EntityInfo.kind = CXIdxEntity_Function; break; |
| 577 | case Decl::Var: |
| 578 | EntityInfo.kind = CXIdxEntity_Variable; break; |
| 579 | case Decl::Field: |
| 580 | EntityInfo.kind = CXIdxEntity_Field; break; |
| 581 | case Decl::EnumConstant: |
| 582 | EntityInfo.kind = CXIdxEntity_EnumConstant; break; |
| 583 | case Decl::ObjCInterface: |
| 584 | EntityInfo.kind = CXIdxEntity_ObjCClass; break; |
| 585 | case Decl::ObjCProtocol: |
| 586 | EntityInfo.kind = CXIdxEntity_ObjCProtocol; break; |
| 587 | case Decl::ObjCCategory: |
| 588 | EntityInfo.kind = CXIdxEntity_ObjCCategory; break; |
| 589 | case Decl::ObjCMethod: |
| 590 | EntityInfo.kind = CXIdxEntity_ObjCMethod; break; |
| 591 | case Decl::ObjCProperty: |
| 592 | EntityInfo.kind = CXIdxEntity_ObjCProperty; break; |
| 593 | case Decl::ObjCIvar: |
| 594 | EntityInfo.kind = CXIdxEntity_ObjCIvar; break; |
| 595 | default: |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 600 | if (IdentifierInfo *II = D->getIdentifier()) { |
| 601 | EntityInfo.name = SA.toCStr(II->getName()); |
| 602 | |
| 603 | } else if (isa<RecordDecl>(D) || isa<NamespaceDecl>(D)) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 604 | EntityInfo.name = 0; // anonymous record/namespace. |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 605 | |
| 606 | } else { |
| 607 | unsigned Begin = SA.getCurSize(); |
| 608 | { |
| 609 | llvm::raw_svector_ostream OS(SA.getBuffer()); |
| 610 | D->printName(OS); |
| 611 | } |
| 612 | EntityInfo.name = SA.getCStr(Begin); |
| 613 | } |
| 614 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 615 | { |
| 616 | unsigned Begin = SA.getCurSize(); |
| 617 | bool Ignore = getDeclCursorUSR(D, SA.getBuffer()); |
| 618 | if (Ignore) { |
| 619 | EntityInfo.USR = ""; |
| 620 | } else { |
| 621 | EntityInfo.USR = SA.getCStr(Begin); |
| 622 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { |
| 627 | if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) |
| 628 | return MakeCursorTypeRef(TD, Loc, CXTU); |
| 629 | if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) |
| 630 | return MakeCursorObjCClassRef(ID, Loc, CXTU); |
| 631 | if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) |
| 632 | return MakeCursorObjCProtocolRef(PD, Loc, CXTU); |
| 633 | |
| 634 | //assert(0 && "not yet"); |
| 635 | return clang_getNullCursor(); |
| 636 | } |