CXIndexDataConsumer.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. //===- CXIndexDataConsumer.cpp - Index data consumer for libclang----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "CXIndexDataConsumer.h"
  9. #include "CIndexDiagnostic.h"
  10. #include "CXTranslationUnit.h"
  11. #include "clang/AST/Attr.h"
  12. #include "clang/AST/DeclCXX.h"
  13. #include "clang/AST/DeclTemplate.h"
  14. #include "clang/AST/DeclVisitor.h"
  15. #include "clang/Frontend/ASTUnit.h"
  16. using namespace clang;
  17. using namespace clang::index;
  18. using namespace cxindex;
  19. using namespace cxcursor;
  20. namespace {
  21. class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
  22. CXIndexDataConsumer &DataConsumer;
  23. SourceLocation DeclLoc;
  24. const DeclContext *LexicalDC;
  25. public:
  26. IndexingDeclVisitor(CXIndexDataConsumer &dataConsumer, SourceLocation Loc,
  27. const DeclContext *lexicalDC)
  28. : DataConsumer(dataConsumer), DeclLoc(Loc), LexicalDC(lexicalDC) { }
  29. bool VisitFunctionDecl(const FunctionDecl *D) {
  30. DataConsumer.handleFunction(D);
  31. return true;
  32. }
  33. bool VisitVarDecl(const VarDecl *D) {
  34. DataConsumer.handleVar(D);
  35. return true;
  36. }
  37. bool VisitFieldDecl(const FieldDecl *D) {
  38. DataConsumer.handleField(D);
  39. return true;
  40. }
  41. bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
  42. return true;
  43. }
  44. bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
  45. DataConsumer.handleEnumerator(D);
  46. return true;
  47. }
  48. bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
  49. DataConsumer.handleTypedefName(D);
  50. return true;
  51. }
  52. bool VisitTagDecl(const TagDecl *D) {
  53. DataConsumer.handleTagDecl(D);
  54. return true;
  55. }
  56. bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  57. DataConsumer.handleObjCInterface(D);
  58. return true;
  59. }
  60. bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  61. DataConsumer.handleObjCProtocol(D);
  62. return true;
  63. }
  64. bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
  65. DataConsumer.handleObjCImplementation(D);
  66. return true;
  67. }
  68. bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  69. DataConsumer.handleObjCCategory(D);
  70. return true;
  71. }
  72. bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  73. DataConsumer.handleObjCCategoryImpl(D);
  74. return true;
  75. }
  76. bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  77. if (isa<ObjCImplDecl>(LexicalDC) && !D->isThisDeclarationADefinition())
  78. DataConsumer.handleSynthesizedObjCMethod(D, DeclLoc, LexicalDC);
  79. else
  80. DataConsumer.handleObjCMethod(D, DeclLoc);
  81. return true;
  82. }
  83. bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  84. DataConsumer.handleObjCProperty(D);
  85. return true;
  86. }
  87. bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  88. DataConsumer.handleSynthesizedObjCProperty(D);
  89. return true;
  90. }
  91. bool VisitNamespaceDecl(const NamespaceDecl *D) {
  92. DataConsumer.handleNamespace(D);
  93. return true;
  94. }
  95. bool VisitUsingDecl(const UsingDecl *D) {
  96. return true;
  97. }
  98. bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
  99. return true;
  100. }
  101. bool VisitClassTemplateDecl(const ClassTemplateDecl *D) {
  102. DataConsumer.handleClassTemplate(D);
  103. return true;
  104. }
  105. bool VisitClassTemplateSpecializationDecl(const
  106. ClassTemplateSpecializationDecl *D) {
  107. DataConsumer.handleTagDecl(D);
  108. return true;
  109. }
  110. bool VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  111. DataConsumer.handleFunctionTemplate(D);
  112. return true;
  113. }
  114. bool VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
  115. DataConsumer.handleTypeAliasTemplate(D);
  116. return true;
  117. }
  118. bool VisitImportDecl(const ImportDecl *D) {
  119. DataConsumer.importedModule(D);
  120. return true;
  121. }
  122. };
  123. CXSymbolRole getSymbolRole(SymbolRoleSet Role) {
  124. // CXSymbolRole mirrors low 9 bits of clang::index::SymbolRole.
  125. return CXSymbolRole(static_cast<uint32_t>(Role) & ((1 << 9) - 1));
  126. }
  127. }
  128. bool CXIndexDataConsumer::handleDeclOccurrence(
  129. const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations,
  130. SourceLocation Loc, ASTNodeInfo ASTNode) {
  131. Loc = getASTContext().getSourceManager().getFileLoc(Loc);
  132. if (Roles & (unsigned)SymbolRole::Reference) {
  133. const NamedDecl *ND = dyn_cast<NamedDecl>(D);
  134. if (!ND)
  135. return true;
  136. if (auto *ObjCID = dyn_cast_or_null<ObjCInterfaceDecl>(ASTNode.OrigD)) {
  137. if (!ObjCID->isThisDeclarationADefinition() &&
  138. ObjCID->getLocation() == Loc) {
  139. // The libclang API treats this as ObjCClassRef declaration.
  140. IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCID);
  141. return true;
  142. }
  143. }
  144. if (auto *ObjCPD = dyn_cast_or_null<ObjCProtocolDecl>(ASTNode.OrigD)) {
  145. if (!ObjCPD->isThisDeclarationADefinition() &&
  146. ObjCPD->getLocation() == Loc) {
  147. // The libclang API treats this as ObjCProtocolRef declaration.
  148. IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCPD);
  149. return true;
  150. }
  151. }
  152. CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct;
  153. if (Roles & (unsigned)SymbolRole::Implicit) {
  154. Kind = CXIdxEntityRef_Implicit;
  155. }
  156. CXSymbolRole CXRole = getSymbolRole(Roles);
  157. CXCursor Cursor;
  158. if (ASTNode.OrigE) {
  159. Cursor = cxcursor::MakeCXCursor(ASTNode.OrigE,
  160. cast<Decl>(ASTNode.ContainerDC),
  161. getCXTU());
  162. } else {
  163. if (ASTNode.OrigD) {
  164. if (auto *OrigND = dyn_cast<NamedDecl>(ASTNode.OrigD))
  165. Cursor = getRefCursor(OrigND, Loc);
  166. else
  167. Cursor = MakeCXCursor(ASTNode.OrigD, CXTU);
  168. } else {
  169. Cursor = getRefCursor(ND, Loc);
  170. }
  171. }
  172. handleReference(ND, Loc, Cursor,
  173. dyn_cast_or_null<NamedDecl>(ASTNode.Parent),
  174. ASTNode.ContainerDC, ASTNode.OrigE, Kind, CXRole);
  175. } else {
  176. const DeclContext *LexicalDC = ASTNode.ContainerDC;
  177. if (!LexicalDC) {
  178. for (const auto &SymRel : Relations) {
  179. if (SymRel.Roles & (unsigned)SymbolRole::RelationChildOf)
  180. LexicalDC = dyn_cast<DeclContext>(SymRel.RelatedSymbol);
  181. }
  182. }
  183. IndexingDeclVisitor(*this, Loc, LexicalDC).Visit(ASTNode.OrigD);
  184. }
  185. return !shouldAbort();
  186. }
  187. bool CXIndexDataConsumer::handleModuleOccurrence(const ImportDecl *ImportD,
  188. const Module *Mod,
  189. SymbolRoleSet Roles,
  190. SourceLocation Loc) {
  191. if (Roles & (SymbolRoleSet)SymbolRole::Declaration)
  192. IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
  193. return !shouldAbort();
  194. }
  195. void CXIndexDataConsumer::finish() {
  196. indexDiagnostics();
  197. }
  198. CXIndexDataConsumer::ObjCProtocolListInfo::ObjCProtocolListInfo(
  199. const ObjCProtocolList &ProtList,
  200. CXIndexDataConsumer &IdxCtx,
  201. ScratchAlloc &SA) {
  202. ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
  203. for (ObjCInterfaceDecl::protocol_iterator
  204. I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
  205. SourceLocation Loc = *LI;
  206. ObjCProtocolDecl *PD = *I;
  207. ProtEntities.push_back(EntityInfo());
  208. IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
  209. CXIdxObjCProtocolRefInfo ProtInfo = { nullptr,
  210. MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
  211. IdxCtx.getIndexLoc(Loc) };
  212. ProtInfos.push_back(ProtInfo);
  213. if (IdxCtx.shouldSuppressRefs())
  214. IdxCtx.markEntityOccurrenceInFile(PD, Loc);
  215. }
  216. for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
  217. ProtInfos[i].protocol = &ProtEntities[i];
  218. for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
  219. Prots.push_back(&ProtInfos[i]);
  220. }
  221. IBOutletCollectionInfo::IBOutletCollectionInfo(
  222. const IBOutletCollectionInfo &other)
  223. : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
  224. IBCollInfo.attrInfo = this;
  225. IBCollInfo.classCursor = other.IBCollInfo.classCursor;
  226. IBCollInfo.classLoc = other.IBCollInfo.classLoc;
  227. if (other.IBCollInfo.objcClass) {
  228. ClassInfo = other.ClassInfo;
  229. IBCollInfo.objcClass = &ClassInfo;
  230. } else
  231. IBCollInfo.objcClass = nullptr;
  232. }
  233. AttrListInfo::AttrListInfo(const Decl *D, CXIndexDataConsumer &IdxCtx)
  234. : SA(IdxCtx), ref_cnt(0) {
  235. if (!D->hasAttrs())
  236. return;
  237. for (const auto *A : D->attrs()) {
  238. CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU);
  239. CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation());
  240. switch (C.kind) {
  241. default:
  242. Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
  243. break;
  244. case CXCursor_IBActionAttr:
  245. Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
  246. break;
  247. case CXCursor_IBOutletAttr:
  248. Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
  249. break;
  250. case CXCursor_IBOutletCollectionAttr:
  251. IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
  252. break;
  253. }
  254. }
  255. for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) {
  256. IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
  257. CXAttrs.push_back(&IBInfo);
  258. const IBOutletCollectionAttr *
  259. IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A);
  260. SourceLocation InterfaceLocStart =
  261. IBAttr->getInterfaceLoc()->getTypeLoc().getBeginLoc();
  262. IBInfo.IBCollInfo.attrInfo = &IBInfo;
  263. IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(InterfaceLocStart);
  264. IBInfo.IBCollInfo.objcClass = nullptr;
  265. IBInfo.IBCollInfo.classCursor = clang_getNullCursor();
  266. QualType Ty = IBAttr->getInterface();
  267. if (const ObjCObjectType *ObjectTy = Ty->getAs<ObjCObjectType>()) {
  268. if (const ObjCInterfaceDecl *InterD = ObjectTy->getInterface()) {
  269. IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
  270. IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
  271. IBInfo.IBCollInfo.classCursor =
  272. MakeCursorObjCClassRef(InterD, InterfaceLocStart, IdxCtx.CXTU);
  273. }
  274. }
  275. }
  276. for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
  277. CXAttrs.push_back(&Attrs[i]);
  278. }
  279. IntrusiveRefCntPtr<AttrListInfo>
  280. AttrListInfo::create(const Decl *D, CXIndexDataConsumer &IdxCtx) {
  281. ScratchAlloc SA(IdxCtx);
  282. AttrListInfo *attrs = SA.allocate<AttrListInfo>();
  283. return new (attrs) AttrListInfo(D, IdxCtx);
  284. }
  285. CXIndexDataConsumer::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
  286. CXIndexDataConsumer &IdxCtx,
  287. ScratchAlloc &SA) {
  288. for (const auto &Base : D->bases()) {
  289. BaseEntities.push_back(EntityInfo());
  290. const NamedDecl *BaseD = nullptr;
  291. QualType T = Base.getType();
  292. SourceLocation Loc = getBaseLoc(Base);
  293. if (const TypedefType *TDT = T->getAs<TypedefType>()) {
  294. BaseD = TDT->getDecl();
  295. } else if (const TemplateSpecializationType *
  296. TST = T->getAs<TemplateSpecializationType>()) {
  297. BaseD = TST->getTemplateName().getAsTemplateDecl();
  298. } else if (const RecordType *RT = T->getAs<RecordType>()) {
  299. BaseD = RT->getDecl();
  300. }
  301. if (BaseD)
  302. IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
  303. CXIdxBaseClassInfo BaseInfo = { nullptr,
  304. MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
  305. IdxCtx.getIndexLoc(Loc) };
  306. BaseInfos.push_back(BaseInfo);
  307. }
  308. for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
  309. if (BaseEntities[i].name && BaseEntities[i].USR)
  310. BaseInfos[i].base = &BaseEntities[i];
  311. }
  312. for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i)
  313. CXBases.push_back(&BaseInfos[i]);
  314. }
  315. SourceLocation CXIndexDataConsumer::CXXBasesListInfo::getBaseLoc(
  316. const CXXBaseSpecifier &Base) const {
  317. SourceLocation Loc = Base.getSourceRange().getBegin();
  318. TypeLoc TL;
  319. if (Base.getTypeSourceInfo())
  320. TL = Base.getTypeSourceInfo()->getTypeLoc();
  321. if (TL.isNull())
  322. return Loc;
  323. if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>())
  324. TL = QL.getUnqualifiedLoc();
  325. if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>())
  326. return EL.getNamedTypeLoc().getBeginLoc();
  327. if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>())
  328. return DL.getNameLoc();
  329. if (DependentTemplateSpecializationTypeLoc DTL =
  330. TL.getAs<DependentTemplateSpecializationTypeLoc>())
  331. return DTL.getTemplateNameLoc();
  332. return Loc;
  333. }
  334. const char *ScratchAlloc::toCStr(StringRef Str) {
  335. if (Str.empty())
  336. return "";
  337. if (Str.data()[Str.size()] == '\0')
  338. return Str.data();
  339. return copyCStr(Str);
  340. }
  341. const char *ScratchAlloc::copyCStr(StringRef Str) {
  342. char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
  343. std::uninitialized_copy(Str.begin(), Str.end(), buf);
  344. buf[Str.size()] = '\0';
  345. return buf;
  346. }
  347. void CXIndexDataConsumer::setASTContext(ASTContext &ctx) {
  348. Ctx = &ctx;
  349. cxtu::getASTUnit(CXTU)->setASTContext(&ctx);
  350. }
  351. void CXIndexDataConsumer::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
  352. cxtu::getASTUnit(CXTU)->setPreprocessor(std::move(PP));
  353. }
  354. bool CXIndexDataConsumer::isFunctionLocalDecl(const Decl *D) {
  355. assert(D);
  356. if (!D->getParentFunctionOrMethod())
  357. return false;
  358. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
  359. switch (ND->getFormalLinkage()) {
  360. case NoLinkage:
  361. case InternalLinkage:
  362. return true;
  363. case VisibleNoLinkage:
  364. case ModuleInternalLinkage:
  365. case UniqueExternalLinkage:
  366. llvm_unreachable("Not a sema linkage");
  367. case ModuleLinkage:
  368. case ExternalLinkage:
  369. return false;
  370. }
  371. }
  372. return true;
  373. }
  374. bool CXIndexDataConsumer::shouldAbort() {
  375. if (!CB.abortQuery)
  376. return false;
  377. return CB.abortQuery(ClientData, nullptr);
  378. }
  379. void CXIndexDataConsumer::enteredMainFile(const FileEntry *File) {
  380. if (File && CB.enteredMainFile) {
  381. CXIdxClientFile idxFile =
  382. CB.enteredMainFile(ClientData,
  383. static_cast<CXFile>(const_cast<FileEntry *>(File)),
  384. nullptr);
  385. FileMap[File] = idxFile;
  386. }
  387. }
  388. void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc,
  389. StringRef filename,
  390. const FileEntry *File,
  391. bool isImport, bool isAngled,
  392. bool isModuleImport) {
  393. if (!CB.ppIncludedFile)
  394. return;
  395. ScratchAlloc SA(*this);
  396. CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
  397. SA.toCStr(filename),
  398. static_cast<CXFile>(
  399. const_cast<FileEntry *>(File)),
  400. isImport, isAngled, isModuleImport };
  401. CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
  402. FileMap[File] = idxFile;
  403. }
  404. void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) {
  405. if (!CB.importedASTFile)
  406. return;
  407. Module *Mod = ImportD->getImportedModule();
  408. if (!Mod)
  409. return;
  410. // If the imported module is part of the top-level module that we're
  411. // indexing, it doesn't correspond to an imported AST file.
  412. // FIXME: This assumes that AST files and top-level modules directly
  413. // correspond, which is unlikely to remain true forever.
  414. if (Module *SrcMod = ImportD->getImportedOwningModule())
  415. if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule())
  416. return;
  417. FileEntry *FE = nullptr;
  418. if (auto File = Mod->getASTFile())
  419. FE = const_cast<FileEntry *>(&File->getFileEntry());
  420. CXIdxImportedASTFileInfo Info = {static_cast<CXFile>(FE), Mod,
  421. getIndexLoc(ImportD->getLocation()),
  422. ImportD->isImplicit()};
  423. CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
  424. (void)astFile;
  425. }
  426. void CXIndexDataConsumer::importedPCH(const FileEntry *File) {
  427. if (!CB.importedASTFile)
  428. return;
  429. CXIdxImportedASTFileInfo Info = {
  430. static_cast<CXFile>(
  431. const_cast<FileEntry *>(File)),
  432. /*module=*/nullptr,
  433. getIndexLoc(SourceLocation()),
  434. /*isImplicit=*/false
  435. };
  436. CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
  437. (void)astFile;
  438. }
  439. void CXIndexDataConsumer::startedTranslationUnit() {
  440. CXIdxClientContainer idxCont = nullptr;
  441. if (CB.startedTranslationUnit)
  442. idxCont = CB.startedTranslationUnit(ClientData, nullptr);
  443. addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
  444. }
  445. void CXIndexDataConsumer::indexDiagnostics() {
  446. if (!hasDiagnosticCallback())
  447. return;
  448. CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(getCXTU());
  449. handleDiagnosticSet(DiagSet);
  450. }
  451. void CXIndexDataConsumer::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
  452. if (!CB.diagnostic)
  453. return;
  454. CB.diagnostic(ClientData, CXDiagSet, nullptr);
  455. }
  456. bool CXIndexDataConsumer::handleDecl(const NamedDecl *D,
  457. SourceLocation Loc, CXCursor Cursor,
  458. DeclInfo &DInfo,
  459. const DeclContext *LexicalDC,
  460. const DeclContext *SemaDC) {
  461. if (!CB.indexDeclaration || !D)
  462. return false;
  463. if (D->isImplicit() && shouldIgnoreIfImplicit(D))
  464. return false;
  465. ScratchAlloc SA(*this);
  466. getEntityInfo(D, DInfo.EntInfo, SA);
  467. if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
  468. || Loc.isInvalid())
  469. return false;
  470. if (!LexicalDC)
  471. LexicalDC = D->getLexicalDeclContext();
  472. if (shouldSuppressRefs())
  473. markEntityOccurrenceInFile(D, Loc);
  474. DInfo.entityInfo = &DInfo.EntInfo;
  475. DInfo.cursor = Cursor;
  476. DInfo.loc = getIndexLoc(Loc);
  477. DInfo.isImplicit = D->isImplicit();
  478. DInfo.attributes = DInfo.EntInfo.attributes;
  479. DInfo.numAttributes = DInfo.EntInfo.numAttributes;
  480. if (!SemaDC)
  481. SemaDC = D->getDeclContext();
  482. getContainerInfo(SemaDC, DInfo.SemanticContainer);
  483. DInfo.semanticContainer = &DInfo.SemanticContainer;
  484. if (LexicalDC == SemaDC) {
  485. DInfo.lexicalContainer = &DInfo.SemanticContainer;
  486. } else if (isTemplateImplicitInstantiation(D)) {
  487. // Implicit instantiations have the lexical context of where they were
  488. // instantiated first. We choose instead the semantic context because:
  489. // 1) at the time that we see the instantiation we have not seen the
  490. // function where it occurred yet.
  491. // 2) the lexical context of the first instantiation is not useful
  492. // information anyway.
  493. DInfo.lexicalContainer = &DInfo.SemanticContainer;
  494. } else {
  495. getContainerInfo(LexicalDC, DInfo.LexicalContainer);
  496. DInfo.lexicalContainer = &DInfo.LexicalContainer;
  497. }
  498. if (DInfo.isContainer) {
  499. getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
  500. DInfo.declAsContainer = &DInfo.DeclAsContainer;
  501. }
  502. CB.indexDeclaration(ClientData, &DInfo);
  503. return true;
  504. }
  505. bool CXIndexDataConsumer::handleObjCContainer(const ObjCContainerDecl *D,
  506. SourceLocation Loc, CXCursor Cursor,
  507. ObjCContainerDeclInfo &ContDInfo) {
  508. ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
  509. return handleDecl(D, Loc, Cursor, ContDInfo);
  510. }
  511. bool CXIndexDataConsumer::handleFunction(const FunctionDecl *D) {
  512. bool isDef = D->isThisDeclarationADefinition();
  513. bool isContainer = isDef;
  514. bool isSkipped = false;
  515. if (D->hasSkippedBody()) {
  516. isSkipped = true;
  517. isDef = true;
  518. isContainer = false;
  519. }
  520. DeclInfo DInfo(!D->isFirstDecl(), isDef, isContainer);
  521. if (isSkipped)
  522. DInfo.flags |= CXIdxDeclFlag_Skipped;
  523. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  524. }
  525. bool CXIndexDataConsumer::handleVar(const VarDecl *D) {
  526. DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
  527. /*isContainer=*/false);
  528. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  529. }
  530. bool CXIndexDataConsumer::handleField(const FieldDecl *D) {
  531. DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
  532. /*isContainer=*/false);
  533. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  534. }
  535. bool CXIndexDataConsumer::handleEnumerator(const EnumConstantDecl *D) {
  536. DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
  537. /*isContainer=*/false);
  538. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  539. }
  540. bool CXIndexDataConsumer::handleTagDecl(const TagDecl *D) {
  541. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
  542. return handleCXXRecordDecl(CXXRD, D);
  543. DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
  544. D->isThisDeclarationADefinition());
  545. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  546. }
  547. bool CXIndexDataConsumer::handleTypedefName(const TypedefNameDecl *D) {
  548. DeclInfo DInfo(!D->isFirstDecl(), /*isDefinition=*/true,
  549. /*isContainer=*/false);
  550. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  551. }
  552. bool CXIndexDataConsumer::handleObjCInterface(const ObjCInterfaceDecl *D) {
  553. // For @class forward declarations, suppress them the same way as references.
  554. if (!D->isThisDeclarationADefinition()) {
  555. if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
  556. return false; // already occurred.
  557. // FIXME: This seems like the wrong definition for redeclaration.
  558. bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
  559. ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
  560. /*isImplementation=*/false);
  561. return handleObjCContainer(D, D->getLocation(),
  562. MakeCursorObjCClassRef(D, D->getLocation(),
  563. CXTU),
  564. ContDInfo);
  565. }
  566. ScratchAlloc SA(*this);
  567. CXIdxBaseClassInfo BaseClass;
  568. EntityInfo BaseEntity;
  569. BaseClass.cursor = clang_getNullCursor();
  570. if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
  571. getEntityInfo(SuperD, BaseEntity, SA);
  572. SourceLocation SuperLoc = D->getSuperClassLoc();
  573. BaseClass.base = &BaseEntity;
  574. BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
  575. BaseClass.loc = getIndexLoc(SuperLoc);
  576. if (shouldSuppressRefs())
  577. markEntityOccurrenceInFile(SuperD, SuperLoc);
  578. }
  579. ObjCProtocolList EmptyProtoList;
  580. ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition()
  581. ? D->getReferencedProtocols()
  582. : EmptyProtoList,
  583. *this, SA);
  584. ObjCInterfaceDeclInfo InterInfo(D);
  585. InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
  586. InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
  587. InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass
  588. : nullptr;
  589. InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
  590. return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
  591. }
  592. bool CXIndexDataConsumer::handleObjCImplementation(
  593. const ObjCImplementationDecl *D) {
  594. ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
  595. /*isRedeclaration=*/true,
  596. /*isImplementation=*/true);
  597. return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
  598. }
  599. bool CXIndexDataConsumer::handleObjCProtocol(const ObjCProtocolDecl *D) {
  600. if (!D->isThisDeclarationADefinition()) {
  601. if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
  602. return false; // already occurred.
  603. // FIXME: This seems like the wrong definition for redeclaration.
  604. bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
  605. ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
  606. isRedeclaration,
  607. /*isImplementation=*/false);
  608. return handleObjCContainer(D, D->getLocation(),
  609. MakeCursorObjCProtocolRef(D, D->getLocation(),
  610. CXTU),
  611. ContDInfo);
  612. }
  613. ScratchAlloc SA(*this);
  614. ObjCProtocolList EmptyProtoList;
  615. ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
  616. ? D->getReferencedProtocols()
  617. : EmptyProtoList,
  618. *this, SA);
  619. ObjCProtocolDeclInfo ProtInfo(D);
  620. ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
  621. return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
  622. }
  623. bool CXIndexDataConsumer::handleObjCCategory(const ObjCCategoryDecl *D) {
  624. ScratchAlloc SA(*this);
  625. ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
  626. EntityInfo ClassEntity;
  627. const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
  628. SourceLocation ClassLoc = D->getLocation();
  629. SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
  630. : D->getCategoryNameLoc();
  631. getEntityInfo(IFaceD, ClassEntity, SA);
  632. if (shouldSuppressRefs())
  633. markEntityOccurrenceInFile(IFaceD, ClassLoc);
  634. ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
  635. CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
  636. if (IFaceD) {
  637. CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
  638. CatDInfo.ObjCCatDeclInfo.classCursor =
  639. MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
  640. } else {
  641. CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
  642. CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
  643. }
  644. CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
  645. CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
  646. CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
  647. return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
  648. }
  649. bool CXIndexDataConsumer::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
  650. ScratchAlloc SA(*this);
  651. const ObjCCategoryDecl *CatD = D->getCategoryDecl();
  652. ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
  653. EntityInfo ClassEntity;
  654. const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
  655. SourceLocation ClassLoc = D->getLocation();
  656. SourceLocation CategoryLoc = D->getCategoryNameLoc();
  657. getEntityInfo(IFaceD, ClassEntity, SA);
  658. if (shouldSuppressRefs())
  659. markEntityOccurrenceInFile(IFaceD, ClassLoc);
  660. CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
  661. if (IFaceD) {
  662. CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
  663. CatDInfo.ObjCCatDeclInfo.classCursor =
  664. MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
  665. } else {
  666. CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
  667. CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
  668. }
  669. CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
  670. CatDInfo.ObjCCatDeclInfo.protocols = nullptr;
  671. return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
  672. }
  673. bool CXIndexDataConsumer::handleObjCMethod(const ObjCMethodDecl *D,
  674. SourceLocation Loc) {
  675. bool isDef = D->isThisDeclarationADefinition();
  676. bool isContainer = isDef;
  677. bool isSkipped = false;
  678. if (D->hasSkippedBody()) {
  679. isSkipped = true;
  680. isDef = true;
  681. isContainer = false;
  682. }
  683. DeclInfo DInfo(!D->isCanonicalDecl(), isDef, isContainer);
  684. if (isSkipped)
  685. DInfo.flags |= CXIdxDeclFlag_Skipped;
  686. return handleDecl(D, Loc, getCursor(D), DInfo);
  687. }
  688. bool CXIndexDataConsumer::handleSynthesizedObjCProperty(
  689. const ObjCPropertyImplDecl *D) {
  690. ObjCPropertyDecl *PD = D->getPropertyDecl();
  691. auto *DC = D->getDeclContext();
  692. return handleReference(PD, D->getLocation(), getCursor(D),
  693. dyn_cast<NamedDecl>(DC), DC);
  694. }
  695. bool CXIndexDataConsumer::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
  696. SourceLocation Loc,
  697. const DeclContext *LexicalDC) {
  698. DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
  699. /*isContainer=*/false);
  700. return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC, D->getDeclContext());
  701. }
  702. bool CXIndexDataConsumer::handleObjCProperty(const ObjCPropertyDecl *D) {
  703. ScratchAlloc SA(*this);
  704. ObjCPropertyDeclInfo DInfo;
  705. EntityInfo GetterEntity;
  706. EntityInfo SetterEntity;
  707. DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
  708. if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
  709. getEntityInfo(Getter, GetterEntity, SA);
  710. DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
  711. } else {
  712. DInfo.ObjCPropDeclInfo.getter = nullptr;
  713. }
  714. if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
  715. getEntityInfo(Setter, SetterEntity, SA);
  716. DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
  717. } else {
  718. DInfo.ObjCPropDeclInfo.setter = nullptr;
  719. }
  720. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  721. }
  722. bool CXIndexDataConsumer::handleNamespace(const NamespaceDecl *D) {
  723. DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
  724. /*isDefinition=*/true,
  725. /*isContainer=*/true);
  726. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  727. }
  728. bool CXIndexDataConsumer::handleClassTemplate(const ClassTemplateDecl *D) {
  729. return handleCXXRecordDecl(D->getTemplatedDecl(), D);
  730. }
  731. bool CXIndexDataConsumer::handleFunctionTemplate(const FunctionTemplateDecl *D) {
  732. DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
  733. /*isDefinition=*/D->isThisDeclarationADefinition(),
  734. /*isContainer=*/D->isThisDeclarationADefinition());
  735. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  736. }
  737. bool CXIndexDataConsumer::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
  738. DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
  739. /*isDefinition=*/true, /*isContainer=*/false);
  740. return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
  741. }
  742. bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc,
  743. CXCursor Cursor,
  744. const NamedDecl *Parent,
  745. const DeclContext *DC,
  746. const Expr *E,
  747. CXIdxEntityRefKind Kind,
  748. CXSymbolRole Role) {
  749. if (!CB.indexEntityReference)
  750. return false;
  751. if (!D || !DC)
  752. return false;
  753. if (Loc.isInvalid())
  754. return false;
  755. if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
  756. return false;
  757. if (isNotFromSourceFile(D->getLocation()))
  758. return false;
  759. if (D->isImplicit() && shouldIgnoreIfImplicit(D))
  760. return false;
  761. if (shouldSuppressRefs()) {
  762. if (markEntityOccurrenceInFile(D, Loc))
  763. return false; // already occurred.
  764. }
  765. ScratchAlloc SA(*this);
  766. EntityInfo RefEntity, ParentEntity;
  767. getEntityInfo(D, RefEntity, SA);
  768. if (!RefEntity.USR)
  769. return false;
  770. getEntityInfo(Parent, ParentEntity, SA);
  771. ContainerInfo Container;
  772. getContainerInfo(DC, Container);
  773. CXIdxEntityRefInfo Info = { Kind,
  774. Cursor,
  775. getIndexLoc(Loc),
  776. &RefEntity,
  777. Parent ? &ParentEntity : nullptr,
  778. &Container,
  779. Role };
  780. CB.indexEntityReference(ClientData, &Info);
  781. return true;
  782. }
  783. bool CXIndexDataConsumer::isNotFromSourceFile(SourceLocation Loc) const {
  784. if (Loc.isInvalid())
  785. return true;
  786. SourceManager &SM = Ctx->getSourceManager();
  787. SourceLocation FileLoc = SM.getFileLoc(Loc);
  788. FileID FID = SM.getFileID(FileLoc);
  789. return SM.getFileEntryForID(FID) == nullptr;
  790. }
  791. void CXIndexDataConsumer::addContainerInMap(const DeclContext *DC,
  792. CXIdxClientContainer container) {
  793. if (!DC)
  794. return;
  795. ContainerMapTy::iterator I = ContainerMap.find(DC);
  796. if (I == ContainerMap.end()) {
  797. if (container)
  798. ContainerMap[DC] = container;
  799. return;
  800. }
  801. // Allow changing the container of a previously seen DeclContext so we
  802. // can handle invalid user code, like a function re-definition.
  803. if (container)
  804. I->second = container;
  805. else
  806. ContainerMap.erase(I);
  807. }
  808. CXIdxClientEntity CXIndexDataConsumer::getClientEntity(const Decl *D) const {
  809. if (!D)
  810. return nullptr;
  811. EntityMapTy::const_iterator I = EntityMap.find(D);
  812. if (I == EntityMap.end())
  813. return nullptr;
  814. return I->second;
  815. }
  816. void CXIndexDataConsumer::setClientEntity(const Decl *D, CXIdxClientEntity client) {
  817. if (!D)
  818. return;
  819. EntityMap[D] = client;
  820. }
  821. bool CXIndexDataConsumer::handleCXXRecordDecl(const CXXRecordDecl *RD,
  822. const NamedDecl *OrigD) {
  823. if (RD->isThisDeclarationADefinition()) {
  824. ScratchAlloc SA(*this);
  825. CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
  826. /*isDefinition=*/RD->isThisDeclarationADefinition());
  827. CXXBasesListInfo BaseList(RD, *this, SA);
  828. CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
  829. CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
  830. CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
  831. if (shouldSuppressRefs()) {
  832. // Go through bases and mark them as referenced.
  833. for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
  834. const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
  835. if (baseInfo->base) {
  836. const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
  837. SourceLocation
  838. Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
  839. markEntityOccurrenceInFile(BaseD, Loc);
  840. }
  841. }
  842. }
  843. return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
  844. }
  845. DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
  846. /*isDefinition=*/RD->isThisDeclarationADefinition(),
  847. /*isContainer=*/RD->isThisDeclarationADefinition());
  848. return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
  849. }
  850. bool CXIndexDataConsumer::markEntityOccurrenceInFile(const NamedDecl *D,
  851. SourceLocation Loc) {
  852. if (!D || Loc.isInvalid())
  853. return true;
  854. SourceManager &SM = Ctx->getSourceManager();
  855. D = getEntityDecl(D);
  856. std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
  857. FileID FID = LocInfo.first;
  858. if (FID.isInvalid())
  859. return true;
  860. const FileEntry *FE = SM.getFileEntryForID(FID);
  861. if (!FE)
  862. return true;
  863. RefFileOccurrence RefOccur(FE, D);
  864. std::pair<llvm::DenseSet<RefFileOccurrence>::iterator, bool>
  865. res = RefFileOccurrences.insert(RefOccur);
  866. return !res.second; // already in map
  867. }
  868. const NamedDecl *CXIndexDataConsumer::getEntityDecl(const NamedDecl *D) const {
  869. assert(D);
  870. D = cast<NamedDecl>(D->getCanonicalDecl());
  871. if (const ObjCImplementationDecl *
  872. ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
  873. return getEntityDecl(ImplD->getClassInterface());
  874. } else if (const ObjCCategoryImplDecl *
  875. CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
  876. return getEntityDecl(CatImplD->getCategoryDecl());
  877. } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  878. if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
  879. return getEntityDecl(TemplD);
  880. } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
  881. if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
  882. return getEntityDecl(TemplD);
  883. }
  884. return D;
  885. }
  886. const DeclContext *
  887. CXIndexDataConsumer::getEntityContainer(const Decl *D) const {
  888. const DeclContext *DC = dyn_cast<DeclContext>(D);
  889. if (DC)
  890. return DC;
  891. if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
  892. DC = ClassTempl->getTemplatedDecl();
  893. } else if (const FunctionTemplateDecl *
  894. FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
  895. DC = FuncTempl->getTemplatedDecl();
  896. }
  897. return DC;
  898. }
  899. CXIdxClientContainer
  900. CXIndexDataConsumer::getClientContainerForDC(const DeclContext *DC) const {
  901. if (!DC)
  902. return nullptr;
  903. ContainerMapTy::const_iterator I = ContainerMap.find(DC);
  904. if (I == ContainerMap.end())
  905. return nullptr;
  906. return I->second;
  907. }
  908. CXIdxClientFile CXIndexDataConsumer::getIndexFile(const FileEntry *File) {
  909. if (!File)
  910. return nullptr;
  911. FileMapTy::iterator FI = FileMap.find(File);
  912. if (FI != FileMap.end())
  913. return FI->second;
  914. return nullptr;
  915. }
  916. CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const {
  917. CXIdxLoc idxLoc = { {nullptr, nullptr}, 0 };
  918. if (Loc.isInvalid())
  919. return idxLoc;
  920. idxLoc.ptr_data[0] = const_cast<CXIndexDataConsumer *>(this);
  921. idxLoc.int_data = Loc.getRawEncoding();
  922. return idxLoc;
  923. }
  924. void CXIndexDataConsumer::translateLoc(SourceLocation Loc,
  925. CXIdxClientFile *indexFile, CXFile *file,
  926. unsigned *line, unsigned *column,
  927. unsigned *offset) {
  928. if (Loc.isInvalid())
  929. return;
  930. SourceManager &SM = Ctx->getSourceManager();
  931. Loc = SM.getFileLoc(Loc);
  932. std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
  933. FileID FID = LocInfo.first;
  934. unsigned FileOffset = LocInfo.second;
  935. if (FID.isInvalid())
  936. return;
  937. const FileEntry *FE = SM.getFileEntryForID(FID);
  938. if (indexFile)
  939. *indexFile = getIndexFile(FE);
  940. if (file)
  941. *file = const_cast<FileEntry *>(FE);
  942. if (line)
  943. *line = SM.getLineNumber(FID, FileOffset);
  944. if (column)
  945. *column = SM.getColumnNumber(FID, FileOffset);
  946. if (offset)
  947. *offset = FileOffset;
  948. }
  949. static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage L);
  950. static CXIdxEntityCXXTemplateKind
  951. getEntityKindFromSymbolProperties(SymbolPropertySet K);
  952. static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L);
  953. void CXIndexDataConsumer::getEntityInfo(const NamedDecl *D,
  954. EntityInfo &EntityInfo,
  955. ScratchAlloc &SA) {
  956. if (!D)
  957. return;
  958. D = getEntityDecl(D);
  959. EntityInfo.cursor = getCursor(D);
  960. EntityInfo.Dcl = D;
  961. EntityInfo.IndexCtx = this;
  962. SymbolInfo SymInfo = getSymbolInfo(D);
  963. EntityInfo.kind = getEntityKindFromSymbolKind(SymInfo.Kind, SymInfo.Lang);
  964. EntityInfo.templateKind = getEntityKindFromSymbolProperties(SymInfo.Properties);
  965. EntityInfo.lang = getEntityLangFromSymbolLang(SymInfo.Lang);
  966. if (D->hasAttrs()) {
  967. EntityInfo.AttrList = AttrListInfo::create(D, *this);
  968. EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
  969. EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
  970. }
  971. if (EntityInfo.kind == CXIdxEntity_Unexposed)
  972. return;
  973. if (IdentifierInfo *II = D->getIdentifier()) {
  974. EntityInfo.name = SA.toCStr(II->getName());
  975. } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
  976. EntityInfo.name = nullptr; // anonymous tag/field/namespace.
  977. } else {
  978. SmallString<256> StrBuf;
  979. {
  980. llvm::raw_svector_ostream OS(StrBuf);
  981. D->printName(OS);
  982. }
  983. EntityInfo.name = SA.copyCStr(StrBuf.str());
  984. }
  985. {
  986. SmallString<512> StrBuf;
  987. bool Ignore = getDeclCursorUSR(D, StrBuf);
  988. if (Ignore) {
  989. EntityInfo.USR = nullptr;
  990. } else {
  991. EntityInfo.USR = SA.copyCStr(StrBuf.str());
  992. }
  993. }
  994. }
  995. void CXIndexDataConsumer::getContainerInfo(const DeclContext *DC,
  996. ContainerInfo &ContInfo) {
  997. ContInfo.cursor = getCursor(cast<Decl>(DC));
  998. ContInfo.DC = DC;
  999. ContInfo.IndexCtx = this;
  1000. }
  1001. CXCursor CXIndexDataConsumer::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
  1002. if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
  1003. return MakeCursorTypeRef(TD, Loc, CXTU);
  1004. if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
  1005. return MakeCursorObjCClassRef(ID, Loc, CXTU);
  1006. if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
  1007. return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
  1008. if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
  1009. return MakeCursorTemplateRef(Template, Loc, CXTU);
  1010. if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
  1011. return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
  1012. if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
  1013. return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
  1014. if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
  1015. return MakeCursorMemberRef(Field, Loc, CXTU);
  1016. if (const VarDecl *Var = dyn_cast<VarDecl>(D))
  1017. return MakeCursorVariableRef(Var, Loc, CXTU);
  1018. return clang_getNullCursor();
  1019. }
  1020. bool CXIndexDataConsumer::shouldIgnoreIfImplicit(const Decl *D) {
  1021. if (isa<ObjCInterfaceDecl>(D))
  1022. return false;
  1023. if (isa<ObjCCategoryDecl>(D))
  1024. return false;
  1025. if (isa<ObjCIvarDecl>(D))
  1026. return false;
  1027. if (isa<ObjCMethodDecl>(D))
  1028. return false;
  1029. if (isa<ImportDecl>(D))
  1030. return false;
  1031. return true;
  1032. }
  1033. bool CXIndexDataConsumer::isTemplateImplicitInstantiation(const Decl *D) {
  1034. if (const ClassTemplateSpecializationDecl *
  1035. SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
  1036. return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
  1037. }
  1038. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  1039. return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;
  1040. }
  1041. return false;
  1042. }
  1043. static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage Lang) {
  1044. switch (K) {
  1045. case SymbolKind::Unknown:
  1046. case SymbolKind::Module:
  1047. case SymbolKind::Macro:
  1048. case SymbolKind::ClassProperty:
  1049. case SymbolKind::Using:
  1050. case SymbolKind::TemplateTypeParm:
  1051. case SymbolKind::TemplateTemplateParm:
  1052. case SymbolKind::NonTypeTemplateParm:
  1053. return CXIdxEntity_Unexposed;
  1054. case SymbolKind::Enum: return CXIdxEntity_Enum;
  1055. case SymbolKind::Struct: return CXIdxEntity_Struct;
  1056. case SymbolKind::Union: return CXIdxEntity_Union;
  1057. case SymbolKind::TypeAlias:
  1058. if (Lang == SymbolLanguage::CXX)
  1059. return CXIdxEntity_CXXTypeAlias;
  1060. return CXIdxEntity_Typedef;
  1061. case SymbolKind::Function: return CXIdxEntity_Function;
  1062. case SymbolKind::Variable: return CXIdxEntity_Variable;
  1063. case SymbolKind::Field:
  1064. if (Lang == SymbolLanguage::ObjC)
  1065. return CXIdxEntity_ObjCIvar;
  1066. return CXIdxEntity_Field;
  1067. case SymbolKind::EnumConstant: return CXIdxEntity_EnumConstant;
  1068. case SymbolKind::Class:
  1069. if (Lang == SymbolLanguage::ObjC)
  1070. return CXIdxEntity_ObjCClass;
  1071. return CXIdxEntity_CXXClass;
  1072. case SymbolKind::Protocol:
  1073. if (Lang == SymbolLanguage::ObjC)
  1074. return CXIdxEntity_ObjCProtocol;
  1075. return CXIdxEntity_CXXInterface;
  1076. case SymbolKind::Extension: return CXIdxEntity_ObjCCategory;
  1077. case SymbolKind::InstanceMethod:
  1078. if (Lang == SymbolLanguage::ObjC)
  1079. return CXIdxEntity_ObjCInstanceMethod;
  1080. return CXIdxEntity_CXXInstanceMethod;
  1081. case SymbolKind::ClassMethod: return CXIdxEntity_ObjCClassMethod;
  1082. case SymbolKind::StaticMethod: return CXIdxEntity_CXXStaticMethod;
  1083. case SymbolKind::InstanceProperty: return CXIdxEntity_ObjCProperty;
  1084. case SymbolKind::StaticProperty: return CXIdxEntity_CXXStaticVariable;
  1085. case SymbolKind::Namespace: return CXIdxEntity_CXXNamespace;
  1086. case SymbolKind::NamespaceAlias: return CXIdxEntity_CXXNamespaceAlias;
  1087. case SymbolKind::Constructor: return CXIdxEntity_CXXConstructor;
  1088. case SymbolKind::Destructor: return CXIdxEntity_CXXDestructor;
  1089. case SymbolKind::ConversionFunction: return CXIdxEntity_CXXConversionFunction;
  1090. case SymbolKind::Parameter: return CXIdxEntity_Variable;
  1091. }
  1092. llvm_unreachable("invalid symbol kind");
  1093. }
  1094. static CXIdxEntityCXXTemplateKind
  1095. getEntityKindFromSymbolProperties(SymbolPropertySet K) {
  1096. if (K & (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization)
  1097. return CXIdxEntity_TemplatePartialSpecialization;
  1098. if (K & (SymbolPropertySet)SymbolProperty::TemplateSpecialization)
  1099. return CXIdxEntity_TemplateSpecialization;
  1100. if (K & (SymbolPropertySet)SymbolProperty::Generic)
  1101. return CXIdxEntity_Template;
  1102. return CXIdxEntity_NonTemplate;
  1103. }
  1104. static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L) {
  1105. switch (L) {
  1106. case SymbolLanguage::C: return CXIdxEntityLang_C;
  1107. case SymbolLanguage::ObjC: return CXIdxEntityLang_ObjC;
  1108. case SymbolLanguage::CXX: return CXIdxEntityLang_CXX;
  1109. case SymbolLanguage::Swift: return CXIdxEntityLang_Swift;
  1110. }
  1111. llvm_unreachable("invalid symbol language");
  1112. }