CXIndexDataConsumer.cpp 45 KB

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