Argyrios Kyrtzidis | 4e7064f | 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 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 21 | IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo( |
| 22 | const ObjCProtocolList &ProtList, |
| 23 | IndexingContext &IdxCtx, |
| 24 | StrAdapter &SA) { |
| 25 | ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin(); |
| 26 | for (ObjCInterfaceDecl::protocol_iterator |
| 27 | I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) { |
| 28 | SourceLocation Loc = *LI; |
| 29 | ObjCProtocolDecl *PD = *I; |
| 30 | ProtEntities.push_back(CXIdxEntityInfo()); |
| 31 | IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA); |
| 32 | CXIdxObjCProtocolRefInfo ProtInfo = { 0, |
| 33 | MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU), |
| 34 | IdxCtx.getIndexLoc(Loc) }; |
| 35 | ProtInfos.push_back(ProtInfo); |
| 36 | } |
| 37 | |
| 38 | for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) |
| 39 | ProtInfos[i].protocol = &ProtEntities[i]; |
| 40 | |
| 41 | for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) |
| 42 | Prots.push_back(&ProtInfos[i]); |
| 43 | } |
| 44 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 45 | const char *IndexingContext::StrAdapter::toCStr(StringRef Str) { |
| 46 | if (Str.empty()) |
| 47 | return ""; |
| 48 | if (Str.data()[Str.size()] == '\0') |
| 49 | return Str.data(); |
| 50 | Scratch += Str; |
| 51 | Scratch.push_back('\0'); |
| 52 | return Scratch.data() + (Scratch.size() - Str.size() - 1); |
| 53 | } |
| 54 | |
| 55 | void IndexingContext::setASTContext(ASTContext &ctx) { |
| 56 | Ctx = &ctx; |
| 57 | static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); |
| 58 | } |
| 59 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 60 | void IndexingContext::enteredMainFile(const FileEntry *File) { |
| 61 | if (File && CB.enteredMainFile) { |
| 62 | CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0); |
| 63 | FileMap[File] = idxFile; |
| 64 | } |
| 65 | } |
| 66 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 67 | void IndexingContext::ppIncludedFile(SourceLocation hashLoc, |
| 68 | StringRef filename, |
| 69 | const FileEntry *File, |
| 70 | bool isImport, bool isAngled) { |
| 71 | if (!CB.ppIncludedFile) |
| 72 | return; |
| 73 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 74 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 75 | CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), |
| 76 | SA.toCStr(filename), |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 77 | (CXFile)File, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 78 | isImport, isAngled }; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 79 | CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); |
| 80 | FileMap[File] = idxFile; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 83 | void IndexingContext::startedTranslationUnit() { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 84 | CXIdxClientContainer idxCont = 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 85 | if (CB.startedTranslationUnit) |
| 86 | idxCont = CB.startedTranslationUnit(ClientData, 0); |
| 87 | addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); |
| 88 | } |
| 89 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 90 | void IndexingContext::handleDiagnostic(const StoredDiagnostic &StoredDiag) { |
| 91 | if (!CB.diagnostic) |
| 92 | return; |
| 93 | |
| 94 | CXStoredDiagnostic CXDiag(StoredDiag, Ctx->getLangOptions()); |
| 95 | CB.diagnostic(ClientData, &CXDiag, 0); |
| 96 | } |
| 97 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 98 | void IndexingContext::handleDiagnostic(CXDiagnostic CXDiag) { |
| 99 | if (!CB.diagnostic) |
| 100 | return; |
| 101 | |
| 102 | CB.diagnostic(ClientData, CXDiag, 0); |
| 103 | } |
| 104 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 105 | void IndexingContext::handleDecl(const NamedDecl *D, |
| 106 | SourceLocation Loc, CXCursor Cursor, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 107 | DeclInfo &DInfo) { |
| 108 | if (!CB.indexDeclaration) |
| 109 | return; |
| 110 | |
| 111 | StrAdapter SA(*this); |
| 112 | getEntityInfo(D, DInfo.CXEntInfo, SA); |
| 113 | DInfo.entityInfo = &DInfo.CXEntInfo; |
| 114 | DInfo.cursor = Cursor; |
| 115 | DInfo.loc = getIndexLoc(Loc); |
| 116 | DInfo.container = getIndexContainer(D); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 117 | DInfo.isImplicit = D->isImplicit(); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 118 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 119 | CXIdxClientContainer clientCont = 0; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 120 | CXIdxDeclOut DeclOut = { DInfo.isContainer ? &clientCont : 0 }; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 121 | CB.indexDeclaration(ClientData, &DInfo, &DeclOut); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 122 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 123 | if (DInfo.isContainer) |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 124 | addContainerInMap(cast<DeclContext>(D), clientCont); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, |
| 128 | SourceLocation Loc, CXCursor Cursor, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 129 | ObjCContainerDeclInfo &ContDInfo) { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 130 | ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 131 | handleDecl(D, Loc, Cursor, ContDInfo); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 134 | void IndexingContext::handleFunction(const FunctionDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 135 | DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 136 | D->isThisDeclarationADefinition()); |
| 137 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void IndexingContext::handleVar(const VarDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 141 | DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 142 | /*isContainer=*/false); |
| 143 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void IndexingContext::handleField(const FieldDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 147 | DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, |
| 148 | /*isContainer=*/false); |
| 149 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void IndexingContext::handleEnumerator(const EnumConstantDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 153 | DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, |
| 154 | /*isContainer=*/false); |
| 155 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void IndexingContext::handleTagDecl(const TagDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 159 | DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), |
| 160 | D->isThisDeclarationADefinition()); |
| 161 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void IndexingContext::handleTypedef(const TypedefDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 165 | DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, |
| 166 | /*isContainer=*/false); |
| 167 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 168 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 169 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 170 | void IndexingContext::handleObjCClass(const ObjCClassDecl *D) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 171 | const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl(); |
| 172 | ObjCInterfaceDecl *IFaceD = Ref->getInterface(); |
| 173 | SourceLocation Loc = Ref->getLocation(); |
| 174 | bool isRedeclaration = IFaceD->getLocation() != Loc; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 175 | |
| 176 | ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, |
| 177 | /*isImplementation=*/false); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 178 | handleObjCContainer(IFaceD, Loc, MakeCursorObjCClassRef(IFaceD, Loc, CXTU), |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 179 | ContDInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 183 | StrAdapter SA(*this); |
| 184 | |
| 185 | CXIdxBaseClassInfo BaseClass; |
| 186 | CXIdxEntityInfo BaseEntity; |
| 187 | BaseClass.cursor = clang_getNullCursor(); |
| 188 | if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { |
| 189 | getEntityInfo(SuperD, BaseEntity, SA); |
| 190 | SourceLocation SuperLoc = D->getSuperClassLoc(); |
| 191 | BaseClass.base = &BaseEntity; |
| 192 | BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); |
| 193 | BaseClass.loc = getIndexLoc(SuperLoc); |
| 194 | } |
| 195 | |
| 196 | ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); |
| 197 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 198 | ObjCInterfaceDeclInfo InterInfo(D); |
| 199 | InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); |
| 200 | InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 201 | InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 202 | InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 203 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 204 | handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 205 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 206 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 207 | void IndexingContext::handleObjCImplementation( |
| 208 | const ObjCImplementationDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 209 | ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, |
Argyrios Kyrtzidis | e7bbab9 | 2011-11-15 06:20:24 +0000 | [diff] [blame] | 210 | /*isRedeclaration=*/true, |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 211 | /*isImplementation=*/true); |
| 212 | handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 213 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 214 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 215 | void IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D, |
| 216 | SourceLocation Loc, |
| 217 | bool isRedeclaration) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 218 | ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, |
| 219 | isRedeclaration, |
| 220 | /*isImplementation=*/false); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 221 | handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU), |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 222 | ContDInfo); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 223 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 225 | void IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 226 | StrAdapter SA(*this); |
| 227 | ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA); |
| 228 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 229 | ObjCProtocolDeclInfo ProtInfo(D); |
| 230 | ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 231 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 232 | handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 235 | void IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 236 | ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 237 | CXIdxEntityInfo ClassEntity; |
| 238 | StrAdapter SA(*this); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 239 | const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); |
| 240 | SourceLocation ClassLoc = D->getLocation(); |
| 241 | SourceLocation CategoryLoc = D->getCategoryNameLoc(); |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame^] | 242 | getEntityInfo(IFaceD, ClassEntity, SA); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 243 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 244 | CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame^] | 245 | if (IFaceD) { |
| 246 | CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; |
| 247 | CatDInfo.ObjCCatDeclInfo.classCursor = |
| 248 | MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); |
| 249 | } else { |
| 250 | CatDInfo.ObjCCatDeclInfo.objcClass = 0; |
| 251 | CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); |
| 252 | } |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 253 | CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); |
| 254 | handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 257 | void IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { |
| 258 | const ObjCCategoryDecl *CatD = D->getCategoryDecl(); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 259 | ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 260 | CXIdxEntityInfo ClassEntity; |
| 261 | StrAdapter SA(*this); |
| 262 | getEntityInfo(CatD->getClassInterface(), ClassEntity, SA); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 263 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 264 | CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; |
| 265 | CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 266 | handleObjCContainer(D, D->getLocation(), getCursor(D), CatDInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 270 | DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), |
| 271 | D->isThisDeclarationADefinition()); |
| 272 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 276 | DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false, |
| 277 | /*isContainer=*/false); |
| 278 | handleDecl(D, D->getLocation(), getCursor(D), DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, |
| 282 | const NamedDecl *Parent, |
| 283 | const DeclContext *DC, |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 284 | const Expr *E, |
| 285 | CXIdxEntityRefKind Kind) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 286 | if (Loc.isInvalid()) |
| 287 | return; |
| 288 | if (!CB.indexEntityReference) |
| 289 | return; |
| 290 | if (isNotFromSourceFile(D->getLocation())) |
| 291 | return; |
| 292 | |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame^] | 293 | D = getEntityDecl(D); |
| 294 | |
| 295 | if (onlyOneRefPerFile()) { |
| 296 | SourceManager &SM = Ctx->getSourceManager(); |
| 297 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 298 | |
| 299 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 300 | FileID FID = LocInfo.first; |
| 301 | if (FID.isInvalid()) |
| 302 | return; |
| 303 | |
| 304 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 305 | if (!FE) |
| 306 | return; |
| 307 | RefFileOccurence RefOccur(FE, D); |
| 308 | std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> |
| 309 | res = RefFileOccurences.insert(RefOccur); |
| 310 | if (!res.second) |
| 311 | return; // already in map. |
| 312 | } |
| 313 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 314 | StrAdapter SA(*this); |
Benjamin Kramer | 854625f | 2011-10-28 13:37:11 +0000 | [diff] [blame] | 315 | CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), |
| 316 | const_cast<Decl*>(cast<Decl>(DC)), CXTU) |
| 317 | : getRefCursor(D, Loc); |
| 318 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 319 | CXIdxEntityInfo RefEntity, ParentEntity; |
| 320 | getEntityInfo(D, RefEntity, SA); |
| 321 | getEntityInfo(Parent, ParentEntity, SA); |
Benjamin Kramer | 854625f | 2011-10-28 13:37:11 +0000 | [diff] [blame] | 322 | CXIdxEntityRefInfo Info = { Cursor, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 323 | getIndexLoc(Loc), |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 324 | &RefEntity, |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame^] | 325 | Parent ? &ParentEntity : 0, |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 326 | getIndexContainerForDC(DC), |
| 327 | Kind }; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 328 | CB.indexEntityReference(ClientData, &Info); |
| 329 | } |
| 330 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 331 | bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { |
| 332 | if (Loc.isInvalid()) |
| 333 | return true; |
| 334 | SourceManager &SM = Ctx->getSourceManager(); |
| 335 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 336 | FileID FID = SM.getFileID(FileLoc); |
| 337 | return SM.getFileEntryForID(FID) == 0; |
| 338 | } |
| 339 | |
| 340 | void IndexingContext::addContainerInMap(const DeclContext *DC, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 341 | CXIdxClientContainer container) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 342 | assert(getScopedContext(DC) == DC); |
| 343 | ContainerMapTy::iterator I = ContainerMap.find(DC); |
| 344 | if (I == ContainerMap.end()) { |
| 345 | if (container) |
| 346 | ContainerMap[DC] = container; |
| 347 | return; |
| 348 | } |
| 349 | // Allow changing the container of a previously seen DeclContext so we |
| 350 | // can handle invalid user code, like a function re-definition. |
| 351 | if (container) |
| 352 | I->second = container; |
| 353 | else |
| 354 | ContainerMap.erase(I); |
| 355 | } |
| 356 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 357 | const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { |
| 358 | assert(D); |
| 359 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 360 | |
| 361 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(D)) { |
| 362 | if (Cat->IsClassExtension()) |
| 363 | return getEntityDecl(Cat->getClassInterface()); |
| 364 | |
| 365 | } else if (const ObjCImplementationDecl * |
| 366 | ImplD = dyn_cast<ObjCImplementationDecl>(D)) { |
| 367 | return getEntityDecl(ImplD->getClassInterface()); |
| 368 | |
| 369 | } else if (const ObjCCategoryImplDecl * |
| 370 | CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 371 | return getEntityDecl(CatImplD->getCategoryDecl()); |
| 372 | } |
| 373 | |
| 374 | return D; |
| 375 | } |
| 376 | |
| 377 | const DeclContext * |
| 378 | IndexingContext::getScopedContext(const DeclContext *DC) const { |
| 379 | // Local contexts are ignored for indexing. |
| 380 | const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod(); |
| 381 | if (FuncCtx) |
| 382 | return FuncCtx; |
| 383 | |
| 384 | // We consider enums always scoped for indexing. |
| 385 | if (isa<TagDecl>(DC)) |
| 386 | return DC; |
| 387 | |
| 388 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 389 | if (NS->isAnonymousNamespace()) |
| 390 | return getScopedContext(NS->getParent()); |
| 391 | return NS; |
| 392 | } |
| 393 | |
| 394 | return DC->getRedeclContext(); |
| 395 | } |
| 396 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 397 | CXIdxClientContainer |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 398 | IndexingContext::getIndexContainerForDC(const DeclContext *DC) const { |
| 399 | DC = getScopedContext(DC); |
| 400 | ContainerMapTy::const_iterator I = ContainerMap.find(DC); |
| 401 | // assert(I != ContainerMap.end() && |
| 402 | // "Failed to include a scoped context in the container map"); |
| 403 | return I->second; |
| 404 | } |
| 405 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 406 | CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 407 | if (!File) |
| 408 | return 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 409 | |
| 410 | FileMapTy::iterator FI = FileMap.find(File); |
| 411 | if (FI != FileMap.end()) |
| 412 | return FI->second; |
| 413 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 414 | return 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { |
| 418 | CXIdxLoc idxLoc = { {0, 0}, 0 }; |
| 419 | if (Loc.isInvalid()) |
| 420 | return idxLoc; |
| 421 | |
| 422 | idxLoc.ptr_data[0] = (void*)this; |
| 423 | idxLoc.int_data = Loc.getRawEncoding(); |
| 424 | return idxLoc; |
| 425 | } |
| 426 | |
| 427 | void IndexingContext::translateLoc(SourceLocation Loc, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 428 | CXIdxClientFile *indexFile, CXFile *file, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 429 | unsigned *line, unsigned *column, |
| 430 | unsigned *offset) { |
| 431 | if (Loc.isInvalid()) |
| 432 | return; |
| 433 | |
| 434 | SourceManager &SM = Ctx->getSourceManager(); |
| 435 | Loc = SM.getFileLoc(Loc); |
| 436 | |
| 437 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 438 | FileID FID = LocInfo.first; |
| 439 | unsigned FileOffset = LocInfo.second; |
| 440 | |
| 441 | if (FID.isInvalid()) |
| 442 | return; |
| 443 | |
| 444 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 445 | if (indexFile) |
| 446 | *indexFile = getIndexFile(FE); |
| 447 | if (file) |
| 448 | *file = (void *)FE; |
| 449 | if (line) |
| 450 | *line = SM.getLineNumber(FID, FileOffset); |
| 451 | if (column) |
| 452 | *column = SM.getColumnNumber(FID, FileOffset); |
| 453 | if (offset) |
| 454 | *offset = FileOffset; |
| 455 | } |
| 456 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 457 | void IndexingContext::getEntityInfo(const NamedDecl *D, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 458 | CXIdxEntityInfo &EntityInfo, |
| 459 | StrAdapter &SA) { |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame^] | 460 | if (!D) |
| 461 | return; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 462 | D = getEntityDecl(D); |
| 463 | EntityInfo.kind = CXIdxEntity_Unexposed; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 464 | |
| 465 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 466 | switch (TD->getTagKind()) { |
| 467 | case TTK_Struct: |
| 468 | EntityInfo.kind = CXIdxEntity_Struct; break; |
| 469 | case TTK_Union: |
| 470 | EntityInfo.kind = CXIdxEntity_Union; break; |
| 471 | case TTK_Class: |
| 472 | EntityInfo.kind = CXIdxEntity_CXXClass; break; |
| 473 | case TTK_Enum: |
| 474 | EntityInfo.kind = CXIdxEntity_Enum; break; |
| 475 | } |
| 476 | |
| 477 | } else { |
| 478 | switch (D->getKind()) { |
| 479 | case Decl::Typedef: |
| 480 | EntityInfo.kind = CXIdxEntity_Typedef; break; |
| 481 | case Decl::Function: |
| 482 | EntityInfo.kind = CXIdxEntity_Function; break; |
| 483 | case Decl::Var: |
| 484 | EntityInfo.kind = CXIdxEntity_Variable; break; |
| 485 | case Decl::Field: |
| 486 | EntityInfo.kind = CXIdxEntity_Field; break; |
| 487 | case Decl::EnumConstant: |
| 488 | EntityInfo.kind = CXIdxEntity_EnumConstant; break; |
| 489 | case Decl::ObjCInterface: |
| 490 | EntityInfo.kind = CXIdxEntity_ObjCClass; break; |
| 491 | case Decl::ObjCProtocol: |
| 492 | EntityInfo.kind = CXIdxEntity_ObjCProtocol; break; |
| 493 | case Decl::ObjCCategory: |
| 494 | EntityInfo.kind = CXIdxEntity_ObjCCategory; break; |
| 495 | case Decl::ObjCMethod: |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 496 | if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) |
| 497 | EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; |
| 498 | else |
| 499 | EntityInfo.kind = CXIdxEntity_ObjCClassMethod; |
| 500 | break; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 501 | case Decl::ObjCProperty: |
| 502 | EntityInfo.kind = CXIdxEntity_ObjCProperty; break; |
| 503 | case Decl::ObjCIvar: |
| 504 | EntityInfo.kind = CXIdxEntity_ObjCIvar; break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 510 | if (IdentifierInfo *II = D->getIdentifier()) { |
| 511 | EntityInfo.name = SA.toCStr(II->getName()); |
| 512 | |
| 513 | } else if (isa<RecordDecl>(D) || isa<NamespaceDecl>(D)) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 514 | EntityInfo.name = 0; // anonymous record/namespace. |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 515 | |
| 516 | } else { |
| 517 | unsigned Begin = SA.getCurSize(); |
| 518 | { |
| 519 | llvm::raw_svector_ostream OS(SA.getBuffer()); |
| 520 | D->printName(OS); |
| 521 | } |
| 522 | EntityInfo.name = SA.getCStr(Begin); |
| 523 | } |
| 524 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 525 | { |
| 526 | unsigned Begin = SA.getCurSize(); |
| 527 | bool Ignore = getDeclCursorUSR(D, SA.getBuffer()); |
| 528 | if (Ignore) { |
| 529 | EntityInfo.USR = ""; |
| 530 | } else { |
| 531 | EntityInfo.USR = SA.getCStr(Begin); |
| 532 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { |
| 537 | if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) |
| 538 | return MakeCursorTypeRef(TD, Loc, CXTU); |
| 539 | if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) |
| 540 | return MakeCursorObjCClassRef(ID, Loc, CXTU); |
| 541 | if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) |
| 542 | return MakeCursorObjCProtocolRef(PD, Loc, CXTU); |
| 543 | |
| 544 | //assert(0 && "not yet"); |
| 545 | return clang_getNullCursor(); |
| 546 | } |