CXCursor.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
  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. //
  9. // This file defines routines for manipulating CXCursors. It should be the
  10. // only file that has internal knowledge of the encoding of the data in
  11. // CXCursor.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "CXCursor.h"
  15. #include "CXString.h"
  16. #include "CXTranslationUnit.h"
  17. #include "CXType.h"
  18. #include "clang-c/Index.h"
  19. #include "clang/AST/Attr.h"
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/DeclCXX.h"
  22. #include "clang/AST/DeclObjC.h"
  23. #include "clang/AST/DeclTemplate.h"
  24. #include "clang/AST/Expr.h"
  25. #include "clang/AST/ExprCXX.h"
  26. #include "clang/AST/ExprObjC.h"
  27. #include "clang/Frontend/ASTUnit.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. using namespace clang;
  30. using namespace cxcursor;
  31. CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
  32. assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
  33. CXCursor C = {K, 0, {nullptr, nullptr, TU}};
  34. return C;
  35. }
  36. static CXCursorKind GetCursorKind(const Attr *A) {
  37. assert(A && "Invalid arguments!");
  38. switch (A->getKind()) {
  39. default:
  40. break;
  41. case attr::IBAction:
  42. return CXCursor_IBActionAttr;
  43. case attr::IBOutlet:
  44. return CXCursor_IBOutletAttr;
  45. case attr::IBOutletCollection:
  46. return CXCursor_IBOutletCollectionAttr;
  47. case attr::Final:
  48. return CXCursor_CXXFinalAttr;
  49. case attr::Override:
  50. return CXCursor_CXXOverrideAttr;
  51. case attr::Annotate:
  52. return CXCursor_AnnotateAttr;
  53. case attr::AsmLabel:
  54. return CXCursor_AsmLabelAttr;
  55. case attr::Packed:
  56. return CXCursor_PackedAttr;
  57. case attr::Pure:
  58. return CXCursor_PureAttr;
  59. case attr::Const:
  60. return CXCursor_ConstAttr;
  61. case attr::NoDuplicate:
  62. return CXCursor_NoDuplicateAttr;
  63. case attr::CUDAConstant:
  64. return CXCursor_CUDAConstantAttr;
  65. case attr::CUDADevice:
  66. return CXCursor_CUDADeviceAttr;
  67. case attr::CUDAGlobal:
  68. return CXCursor_CUDAGlobalAttr;
  69. case attr::CUDAHost:
  70. return CXCursor_CUDAHostAttr;
  71. case attr::CUDAShared:
  72. return CXCursor_CUDASharedAttr;
  73. case attr::Visibility:
  74. return CXCursor_VisibilityAttr;
  75. case attr::DLLExport:
  76. return CXCursor_DLLExport;
  77. case attr::DLLImport:
  78. return CXCursor_DLLImport;
  79. case attr::NSReturnsRetained:
  80. return CXCursor_NSReturnsRetained;
  81. case attr::NSReturnsNotRetained:
  82. return CXCursor_NSReturnsNotRetained;
  83. case attr::NSReturnsAutoreleased:
  84. return CXCursor_NSReturnsAutoreleased;
  85. case attr::NSConsumesSelf:
  86. return CXCursor_NSConsumesSelf;
  87. case attr::NSConsumed:
  88. return CXCursor_NSConsumed;
  89. case attr::ObjCException:
  90. return CXCursor_ObjCException;
  91. case attr::ObjCNSObject:
  92. return CXCursor_ObjCNSObject;
  93. case attr::ObjCIndependentClass:
  94. return CXCursor_ObjCIndependentClass;
  95. case attr::ObjCPreciseLifetime:
  96. return CXCursor_ObjCPreciseLifetime;
  97. case attr::ObjCReturnsInnerPointer:
  98. return CXCursor_ObjCReturnsInnerPointer;
  99. case attr::ObjCRequiresSuper:
  100. return CXCursor_ObjCRequiresSuper;
  101. case attr::ObjCRootClass:
  102. return CXCursor_ObjCRootClass;
  103. case attr::ObjCSubclassingRestricted:
  104. return CXCursor_ObjCSubclassingRestricted;
  105. case attr::ObjCExplicitProtocolImpl:
  106. return CXCursor_ObjCExplicitProtocolImpl;
  107. case attr::ObjCDesignatedInitializer:
  108. return CXCursor_ObjCDesignatedInitializer;
  109. case attr::ObjCRuntimeVisible:
  110. return CXCursor_ObjCRuntimeVisible;
  111. case attr::ObjCBoxable:
  112. return CXCursor_ObjCBoxable;
  113. case attr::FlagEnum:
  114. return CXCursor_FlagEnum;
  115. case attr::Convergent:
  116. return CXCursor_ConvergentAttr;
  117. case attr::WarnUnused:
  118. return CXCursor_WarnUnusedAttr;
  119. case attr::WarnUnusedResult:
  120. return CXCursor_WarnUnusedResultAttr;
  121. case attr::Aligned:
  122. return CXCursor_AlignedAttr;
  123. }
  124. return CXCursor_UnexposedAttr;
  125. }
  126. CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
  127. CXTranslationUnit TU) {
  128. assert(A && Parent && TU && "Invalid arguments!");
  129. CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
  130. return C;
  131. }
  132. CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
  133. SourceRange RegionOfInterest,
  134. bool FirstInDeclGroup) {
  135. assert(D && TU && "Invalid arguments!");
  136. CXCursorKind K = getCursorKindForDecl(D);
  137. if (K == CXCursor_ObjCClassMethodDecl ||
  138. K == CXCursor_ObjCInstanceMethodDecl) {
  139. int SelectorIdIndex = -1;
  140. // Check if cursor points to a selector id.
  141. if (RegionOfInterest.isValid() &&
  142. RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
  143. SmallVector<SourceLocation, 16> SelLocs;
  144. cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
  145. SmallVectorImpl<SourceLocation>::iterator I =
  146. llvm::find(SelLocs, RegionOfInterest.getBegin());
  147. if (I != SelLocs.end())
  148. SelectorIdIndex = I - SelLocs.begin();
  149. }
  150. CXCursor C = {K,
  151. SelectorIdIndex,
  152. {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
  153. return C;
  154. }
  155. CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
  156. return C;
  157. }
  158. CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
  159. CXTranslationUnit TU,
  160. SourceRange RegionOfInterest) {
  161. assert(S && TU && "Invalid arguments!");
  162. CXCursorKind K = CXCursor_NotImplemented;
  163. switch (S->getStmtClass()) {
  164. case Stmt::NoStmtClass:
  165. break;
  166. case Stmt::CaseStmtClass:
  167. K = CXCursor_CaseStmt;
  168. break;
  169. case Stmt::DefaultStmtClass:
  170. K = CXCursor_DefaultStmt;
  171. break;
  172. case Stmt::IfStmtClass:
  173. K = CXCursor_IfStmt;
  174. break;
  175. case Stmt::SwitchStmtClass:
  176. K = CXCursor_SwitchStmt;
  177. break;
  178. case Stmt::WhileStmtClass:
  179. K = CXCursor_WhileStmt;
  180. break;
  181. case Stmt::DoStmtClass:
  182. K = CXCursor_DoStmt;
  183. break;
  184. case Stmt::ForStmtClass:
  185. K = CXCursor_ForStmt;
  186. break;
  187. case Stmt::GotoStmtClass:
  188. K = CXCursor_GotoStmt;
  189. break;
  190. case Stmt::IndirectGotoStmtClass:
  191. K = CXCursor_IndirectGotoStmt;
  192. break;
  193. case Stmt::ContinueStmtClass:
  194. K = CXCursor_ContinueStmt;
  195. break;
  196. case Stmt::BreakStmtClass:
  197. K = CXCursor_BreakStmt;
  198. break;
  199. case Stmt::ReturnStmtClass:
  200. K = CXCursor_ReturnStmt;
  201. break;
  202. case Stmt::GCCAsmStmtClass:
  203. K = CXCursor_GCCAsmStmt;
  204. break;
  205. case Stmt::MSAsmStmtClass:
  206. K = CXCursor_MSAsmStmt;
  207. break;
  208. case Stmt::ObjCAtTryStmtClass:
  209. K = CXCursor_ObjCAtTryStmt;
  210. break;
  211. case Stmt::ObjCAtCatchStmtClass:
  212. K = CXCursor_ObjCAtCatchStmt;
  213. break;
  214. case Stmt::ObjCAtFinallyStmtClass:
  215. K = CXCursor_ObjCAtFinallyStmt;
  216. break;
  217. case Stmt::ObjCAtThrowStmtClass:
  218. K = CXCursor_ObjCAtThrowStmt;
  219. break;
  220. case Stmt::ObjCAtSynchronizedStmtClass:
  221. K = CXCursor_ObjCAtSynchronizedStmt;
  222. break;
  223. case Stmt::ObjCAutoreleasePoolStmtClass:
  224. K = CXCursor_ObjCAutoreleasePoolStmt;
  225. break;
  226. case Stmt::ObjCForCollectionStmtClass:
  227. K = CXCursor_ObjCForCollectionStmt;
  228. break;
  229. case Stmt::CXXCatchStmtClass:
  230. K = CXCursor_CXXCatchStmt;
  231. break;
  232. case Stmt::CXXTryStmtClass:
  233. K = CXCursor_CXXTryStmt;
  234. break;
  235. case Stmt::CXXForRangeStmtClass:
  236. K = CXCursor_CXXForRangeStmt;
  237. break;
  238. case Stmt::SEHTryStmtClass:
  239. K = CXCursor_SEHTryStmt;
  240. break;
  241. case Stmt::SEHExceptStmtClass:
  242. K = CXCursor_SEHExceptStmt;
  243. break;
  244. case Stmt::SEHFinallyStmtClass:
  245. K = CXCursor_SEHFinallyStmt;
  246. break;
  247. case Stmt::SEHLeaveStmtClass:
  248. K = CXCursor_SEHLeaveStmt;
  249. break;
  250. case Stmt::CoroutineBodyStmtClass:
  251. case Stmt::CoreturnStmtClass:
  252. K = CXCursor_UnexposedStmt;
  253. break;
  254. case Stmt::ArrayTypeTraitExprClass:
  255. case Stmt::AsTypeExprClass:
  256. case Stmt::AtomicExprClass:
  257. case Stmt::BinaryConditionalOperatorClass:
  258. case Stmt::TypeTraitExprClass:
  259. case Stmt::CoawaitExprClass:
  260. case Stmt::DependentCoawaitExprClass:
  261. case Stmt::CoyieldExprClass:
  262. case Stmt::CXXBindTemporaryExprClass:
  263. case Stmt::CXXDefaultArgExprClass:
  264. case Stmt::CXXDefaultInitExprClass:
  265. case Stmt::CXXFoldExprClass:
  266. case Stmt::CXXRewrittenBinaryOperatorClass:
  267. case Stmt::CXXStdInitializerListExprClass:
  268. case Stmt::CXXScalarValueInitExprClass:
  269. case Stmt::CXXUuidofExprClass:
  270. case Stmt::ChooseExprClass:
  271. case Stmt::DesignatedInitExprClass:
  272. case Stmt::DesignatedInitUpdateExprClass:
  273. case Stmt::ArrayInitLoopExprClass:
  274. case Stmt::ArrayInitIndexExprClass:
  275. case Stmt::ExprWithCleanupsClass:
  276. case Stmt::ExpressionTraitExprClass:
  277. case Stmt::ExtVectorElementExprClass:
  278. case Stmt::ImplicitCastExprClass:
  279. case Stmt::ImplicitValueInitExprClass:
  280. case Stmt::NoInitExprClass:
  281. case Stmt::MaterializeTemporaryExprClass:
  282. case Stmt::ObjCIndirectCopyRestoreExprClass:
  283. case Stmt::OffsetOfExprClass:
  284. case Stmt::ParenListExprClass:
  285. case Stmt::PredefinedExprClass:
  286. case Stmt::ShuffleVectorExprClass:
  287. case Stmt::SourceLocExprClass:
  288. case Stmt::ConvertVectorExprClass:
  289. case Stmt::VAArgExprClass:
  290. case Stmt::ObjCArrayLiteralClass:
  291. case Stmt::ObjCDictionaryLiteralClass:
  292. case Stmt::ObjCBoxedExprClass:
  293. case Stmt::ObjCSubscriptRefExprClass:
  294. case Stmt::RecoveryExprClass:
  295. case Stmt::SYCLUniqueStableNameExprClass:
  296. K = CXCursor_UnexposedExpr;
  297. break;
  298. case Stmt::OpaqueValueExprClass:
  299. if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
  300. return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
  301. K = CXCursor_UnexposedExpr;
  302. break;
  303. case Stmt::PseudoObjectExprClass:
  304. return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
  305. TU, RegionOfInterest);
  306. case Stmt::CompoundStmtClass:
  307. K = CXCursor_CompoundStmt;
  308. break;
  309. case Stmt::NullStmtClass:
  310. K = CXCursor_NullStmt;
  311. break;
  312. case Stmt::LabelStmtClass:
  313. K = CXCursor_LabelStmt;
  314. break;
  315. case Stmt::AttributedStmtClass:
  316. K = CXCursor_UnexposedStmt;
  317. break;
  318. case Stmt::DeclStmtClass:
  319. K = CXCursor_DeclStmt;
  320. break;
  321. case Stmt::CapturedStmtClass:
  322. K = CXCursor_UnexposedStmt;
  323. break;
  324. case Stmt::IntegerLiteralClass:
  325. K = CXCursor_IntegerLiteral;
  326. break;
  327. case Stmt::FixedPointLiteralClass:
  328. K = CXCursor_FixedPointLiteral;
  329. break;
  330. case Stmt::FloatingLiteralClass:
  331. K = CXCursor_FloatingLiteral;
  332. break;
  333. case Stmt::ImaginaryLiteralClass:
  334. K = CXCursor_ImaginaryLiteral;
  335. break;
  336. case Stmt::StringLiteralClass:
  337. K = CXCursor_StringLiteral;
  338. break;
  339. case Stmt::CharacterLiteralClass:
  340. K = CXCursor_CharacterLiteral;
  341. break;
  342. case Stmt::ConstantExprClass:
  343. return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
  344. RegionOfInterest);
  345. case Stmt::ParenExprClass:
  346. K = CXCursor_ParenExpr;
  347. break;
  348. case Stmt::UnaryOperatorClass:
  349. K = CXCursor_UnaryOperator;
  350. break;
  351. case Stmt::UnaryExprOrTypeTraitExprClass:
  352. case Stmt::CXXNoexceptExprClass:
  353. K = CXCursor_UnaryExpr;
  354. break;
  355. case Stmt::MSPropertySubscriptExprClass:
  356. case Stmt::ArraySubscriptExprClass:
  357. K = CXCursor_ArraySubscriptExpr;
  358. break;
  359. case Stmt::MatrixSubscriptExprClass:
  360. // TODO: add support for MatrixSubscriptExpr.
  361. K = CXCursor_UnexposedExpr;
  362. break;
  363. case Stmt::OMPArraySectionExprClass:
  364. K = CXCursor_OMPArraySectionExpr;
  365. break;
  366. case Stmt::OMPArrayShapingExprClass:
  367. K = CXCursor_OMPArrayShapingExpr;
  368. break;
  369. case Stmt::OMPIteratorExprClass:
  370. K = CXCursor_OMPIteratorExpr;
  371. break;
  372. case Stmt::BinaryOperatorClass:
  373. K = CXCursor_BinaryOperator;
  374. break;
  375. case Stmt::CompoundAssignOperatorClass:
  376. K = CXCursor_CompoundAssignOperator;
  377. break;
  378. case Stmt::ConditionalOperatorClass:
  379. K = CXCursor_ConditionalOperator;
  380. break;
  381. case Stmt::CStyleCastExprClass:
  382. K = CXCursor_CStyleCastExpr;
  383. break;
  384. case Stmt::CompoundLiteralExprClass:
  385. K = CXCursor_CompoundLiteralExpr;
  386. break;
  387. case Stmt::InitListExprClass:
  388. K = CXCursor_InitListExpr;
  389. break;
  390. case Stmt::AddrLabelExprClass:
  391. K = CXCursor_AddrLabelExpr;
  392. break;
  393. case Stmt::StmtExprClass:
  394. K = CXCursor_StmtExpr;
  395. break;
  396. case Stmt::GenericSelectionExprClass:
  397. K = CXCursor_GenericSelectionExpr;
  398. break;
  399. case Stmt::GNUNullExprClass:
  400. K = CXCursor_GNUNullExpr;
  401. break;
  402. case Stmt::CXXStaticCastExprClass:
  403. K = CXCursor_CXXStaticCastExpr;
  404. break;
  405. case Stmt::CXXDynamicCastExprClass:
  406. K = CXCursor_CXXDynamicCastExpr;
  407. break;
  408. case Stmt::CXXReinterpretCastExprClass:
  409. K = CXCursor_CXXReinterpretCastExpr;
  410. break;
  411. case Stmt::CXXConstCastExprClass:
  412. K = CXCursor_CXXConstCastExpr;
  413. break;
  414. case Stmt::CXXFunctionalCastExprClass:
  415. K = CXCursor_CXXFunctionalCastExpr;
  416. break;
  417. case Stmt::CXXAddrspaceCastExprClass:
  418. K = CXCursor_CXXAddrspaceCastExpr;
  419. break;
  420. case Stmt::CXXTypeidExprClass:
  421. K = CXCursor_CXXTypeidExpr;
  422. break;
  423. case Stmt::CXXBoolLiteralExprClass:
  424. K = CXCursor_CXXBoolLiteralExpr;
  425. break;
  426. case Stmt::CXXNullPtrLiteralExprClass:
  427. K = CXCursor_CXXNullPtrLiteralExpr;
  428. break;
  429. case Stmt::CXXThisExprClass:
  430. K = CXCursor_CXXThisExpr;
  431. break;
  432. case Stmt::CXXThrowExprClass:
  433. K = CXCursor_CXXThrowExpr;
  434. break;
  435. case Stmt::CXXNewExprClass:
  436. K = CXCursor_CXXNewExpr;
  437. break;
  438. case Stmt::CXXDeleteExprClass:
  439. K = CXCursor_CXXDeleteExpr;
  440. break;
  441. case Stmt::ObjCStringLiteralClass:
  442. K = CXCursor_ObjCStringLiteral;
  443. break;
  444. case Stmt::ObjCEncodeExprClass:
  445. K = CXCursor_ObjCEncodeExpr;
  446. break;
  447. case Stmt::ObjCSelectorExprClass:
  448. K = CXCursor_ObjCSelectorExpr;
  449. break;
  450. case Stmt::ObjCProtocolExprClass:
  451. K = CXCursor_ObjCProtocolExpr;
  452. break;
  453. case Stmt::ObjCBoolLiteralExprClass:
  454. K = CXCursor_ObjCBoolLiteralExpr;
  455. break;
  456. case Stmt::ObjCAvailabilityCheckExprClass:
  457. K = CXCursor_ObjCAvailabilityCheckExpr;
  458. break;
  459. case Stmt::ObjCBridgedCastExprClass:
  460. K = CXCursor_ObjCBridgedCastExpr;
  461. break;
  462. case Stmt::BlockExprClass:
  463. K = CXCursor_BlockExpr;
  464. break;
  465. case Stmt::PackExpansionExprClass:
  466. K = CXCursor_PackExpansionExpr;
  467. break;
  468. case Stmt::SizeOfPackExprClass:
  469. K = CXCursor_SizeOfPackExpr;
  470. break;
  471. case Stmt::DeclRefExprClass:
  472. if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
  473. cast<DeclRefExpr>(S)->getDecl())) {
  474. if (const ObjCMethodDecl *MD =
  475. dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
  476. if (MD->getSelfDecl() == IPD) {
  477. K = CXCursor_ObjCSelfExpr;
  478. break;
  479. }
  480. }
  481. }
  482. K = CXCursor_DeclRefExpr;
  483. break;
  484. case Stmt::DependentScopeDeclRefExprClass:
  485. case Stmt::SubstNonTypeTemplateParmExprClass:
  486. case Stmt::SubstNonTypeTemplateParmPackExprClass:
  487. case Stmt::FunctionParmPackExprClass:
  488. case Stmt::UnresolvedLookupExprClass:
  489. case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
  490. K = CXCursor_DeclRefExpr;
  491. break;
  492. case Stmt::CXXDependentScopeMemberExprClass:
  493. case Stmt::CXXPseudoDestructorExprClass:
  494. case Stmt::MemberExprClass:
  495. case Stmt::MSPropertyRefExprClass:
  496. case Stmt::ObjCIsaExprClass:
  497. case Stmt::ObjCIvarRefExprClass:
  498. case Stmt::ObjCPropertyRefExprClass:
  499. case Stmt::UnresolvedMemberExprClass:
  500. K = CXCursor_MemberRefExpr;
  501. break;
  502. case Stmt::CallExprClass:
  503. case Stmt::CXXOperatorCallExprClass:
  504. case Stmt::CXXMemberCallExprClass:
  505. case Stmt::CUDAKernelCallExprClass:
  506. case Stmt::CXXConstructExprClass:
  507. case Stmt::CXXInheritedCtorInitExprClass:
  508. case Stmt::CXXTemporaryObjectExprClass:
  509. case Stmt::CXXUnresolvedConstructExprClass:
  510. case Stmt::UserDefinedLiteralClass:
  511. K = CXCursor_CallExpr;
  512. break;
  513. case Stmt::LambdaExprClass:
  514. K = CXCursor_LambdaExpr;
  515. break;
  516. case Stmt::ObjCMessageExprClass: {
  517. K = CXCursor_ObjCMessageExpr;
  518. int SelectorIdIndex = -1;
  519. // Check if cursor points to a selector id.
  520. if (RegionOfInterest.isValid() &&
  521. RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
  522. SmallVector<SourceLocation, 16> SelLocs;
  523. cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
  524. SmallVectorImpl<SourceLocation>::iterator I =
  525. llvm::find(SelLocs, RegionOfInterest.getBegin());
  526. if (I != SelLocs.end())
  527. SelectorIdIndex = I - SelLocs.begin();
  528. }
  529. CXCursor C = {K, 0, {Parent, S, TU}};
  530. return getSelectorIdentifierCursor(SelectorIdIndex, C);
  531. }
  532. case Stmt::ConceptSpecializationExprClass:
  533. K = CXCursor_ConceptSpecializationExpr;
  534. break;
  535. case Stmt::RequiresExprClass:
  536. K = CXCursor_RequiresExpr;
  537. break;
  538. case Stmt::CXXParenListInitExprClass:
  539. K = CXCursor_CXXParenListInitExpr;
  540. break;
  541. case Stmt::MSDependentExistsStmtClass:
  542. K = CXCursor_UnexposedStmt;
  543. break;
  544. case Stmt::OMPCanonicalLoopClass:
  545. K = CXCursor_OMPCanonicalLoop;
  546. break;
  547. case Stmt::OMPMetaDirectiveClass:
  548. K = CXCursor_OMPMetaDirective;
  549. break;
  550. case Stmt::OMPParallelDirectiveClass:
  551. K = CXCursor_OMPParallelDirective;
  552. break;
  553. case Stmt::OMPSimdDirectiveClass:
  554. K = CXCursor_OMPSimdDirective;
  555. break;
  556. case Stmt::OMPTileDirectiveClass:
  557. K = CXCursor_OMPTileDirective;
  558. break;
  559. case Stmt::OMPUnrollDirectiveClass:
  560. K = CXCursor_OMPUnrollDirective;
  561. break;
  562. case Stmt::OMPForDirectiveClass:
  563. K = CXCursor_OMPForDirective;
  564. break;
  565. case Stmt::OMPForSimdDirectiveClass:
  566. K = CXCursor_OMPForSimdDirective;
  567. break;
  568. case Stmt::OMPSectionsDirectiveClass:
  569. K = CXCursor_OMPSectionsDirective;
  570. break;
  571. case Stmt::OMPSectionDirectiveClass:
  572. K = CXCursor_OMPSectionDirective;
  573. break;
  574. case Stmt::OMPSingleDirectiveClass:
  575. K = CXCursor_OMPSingleDirective;
  576. break;
  577. case Stmt::OMPMasterDirectiveClass:
  578. K = CXCursor_OMPMasterDirective;
  579. break;
  580. case Stmt::OMPCriticalDirectiveClass:
  581. K = CXCursor_OMPCriticalDirective;
  582. break;
  583. case Stmt::OMPParallelForDirectiveClass:
  584. K = CXCursor_OMPParallelForDirective;
  585. break;
  586. case Stmt::OMPParallelForSimdDirectiveClass:
  587. K = CXCursor_OMPParallelForSimdDirective;
  588. break;
  589. case Stmt::OMPParallelMasterDirectiveClass:
  590. K = CXCursor_OMPParallelMasterDirective;
  591. break;
  592. case Stmt::OMPParallelMaskedDirectiveClass:
  593. K = CXCursor_OMPParallelMaskedDirective;
  594. break;
  595. case Stmt::OMPParallelSectionsDirectiveClass:
  596. K = CXCursor_OMPParallelSectionsDirective;
  597. break;
  598. case Stmt::OMPTaskDirectiveClass:
  599. K = CXCursor_OMPTaskDirective;
  600. break;
  601. case Stmt::OMPTaskyieldDirectiveClass:
  602. K = CXCursor_OMPTaskyieldDirective;
  603. break;
  604. case Stmt::OMPBarrierDirectiveClass:
  605. K = CXCursor_OMPBarrierDirective;
  606. break;
  607. case Stmt::OMPTaskwaitDirectiveClass:
  608. K = CXCursor_OMPTaskwaitDirective;
  609. break;
  610. case Stmt::OMPErrorDirectiveClass:
  611. K = CXCursor_OMPErrorDirective;
  612. break;
  613. case Stmt::OMPTaskgroupDirectiveClass:
  614. K = CXCursor_OMPTaskgroupDirective;
  615. break;
  616. case Stmt::OMPFlushDirectiveClass:
  617. K = CXCursor_OMPFlushDirective;
  618. break;
  619. case Stmt::OMPDepobjDirectiveClass:
  620. K = CXCursor_OMPDepobjDirective;
  621. break;
  622. case Stmt::OMPScanDirectiveClass:
  623. K = CXCursor_OMPScanDirective;
  624. break;
  625. case Stmt::OMPOrderedDirectiveClass:
  626. K = CXCursor_OMPOrderedDirective;
  627. break;
  628. case Stmt::OMPAtomicDirectiveClass:
  629. K = CXCursor_OMPAtomicDirective;
  630. break;
  631. case Stmt::OMPTargetDirectiveClass:
  632. K = CXCursor_OMPTargetDirective;
  633. break;
  634. case Stmt::OMPTargetDataDirectiveClass:
  635. K = CXCursor_OMPTargetDataDirective;
  636. break;
  637. case Stmt::OMPTargetEnterDataDirectiveClass:
  638. K = CXCursor_OMPTargetEnterDataDirective;
  639. break;
  640. case Stmt::OMPTargetExitDataDirectiveClass:
  641. K = CXCursor_OMPTargetExitDataDirective;
  642. break;
  643. case Stmt::OMPTargetParallelDirectiveClass:
  644. K = CXCursor_OMPTargetParallelDirective;
  645. break;
  646. case Stmt::OMPTargetParallelForDirectiveClass:
  647. K = CXCursor_OMPTargetParallelForDirective;
  648. break;
  649. case Stmt::OMPTargetUpdateDirectiveClass:
  650. K = CXCursor_OMPTargetUpdateDirective;
  651. break;
  652. case Stmt::OMPTeamsDirectiveClass:
  653. K = CXCursor_OMPTeamsDirective;
  654. break;
  655. case Stmt::OMPCancellationPointDirectiveClass:
  656. K = CXCursor_OMPCancellationPointDirective;
  657. break;
  658. case Stmt::OMPCancelDirectiveClass:
  659. K = CXCursor_OMPCancelDirective;
  660. break;
  661. case Stmt::OMPTaskLoopDirectiveClass:
  662. K = CXCursor_OMPTaskLoopDirective;
  663. break;
  664. case Stmt::OMPTaskLoopSimdDirectiveClass:
  665. K = CXCursor_OMPTaskLoopSimdDirective;
  666. break;
  667. case Stmt::OMPMasterTaskLoopDirectiveClass:
  668. K = CXCursor_OMPMasterTaskLoopDirective;
  669. break;
  670. case Stmt::OMPMaskedTaskLoopDirectiveClass:
  671. K = CXCursor_OMPMaskedTaskLoopDirective;
  672. break;
  673. case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
  674. K = CXCursor_OMPMasterTaskLoopSimdDirective;
  675. break;
  676. case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
  677. K = CXCursor_OMPMaskedTaskLoopSimdDirective;
  678. break;
  679. case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
  680. K = CXCursor_OMPParallelMasterTaskLoopDirective;
  681. break;
  682. case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
  683. K = CXCursor_OMPParallelMaskedTaskLoopDirective;
  684. break;
  685. case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
  686. K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
  687. break;
  688. case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
  689. K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
  690. break;
  691. case Stmt::OMPDistributeDirectiveClass:
  692. K = CXCursor_OMPDistributeDirective;
  693. break;
  694. case Stmt::OMPDistributeParallelForDirectiveClass:
  695. K = CXCursor_OMPDistributeParallelForDirective;
  696. break;
  697. case Stmt::OMPDistributeParallelForSimdDirectiveClass:
  698. K = CXCursor_OMPDistributeParallelForSimdDirective;
  699. break;
  700. case Stmt::OMPDistributeSimdDirectiveClass:
  701. K = CXCursor_OMPDistributeSimdDirective;
  702. break;
  703. case Stmt::OMPTargetParallelForSimdDirectiveClass:
  704. K = CXCursor_OMPTargetParallelForSimdDirective;
  705. break;
  706. case Stmt::OMPTargetSimdDirectiveClass:
  707. K = CXCursor_OMPTargetSimdDirective;
  708. break;
  709. case Stmt::OMPTeamsDistributeDirectiveClass:
  710. K = CXCursor_OMPTeamsDistributeDirective;
  711. break;
  712. case Stmt::OMPTeamsDistributeSimdDirectiveClass:
  713. K = CXCursor_OMPTeamsDistributeSimdDirective;
  714. break;
  715. case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
  716. K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
  717. break;
  718. case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
  719. K = CXCursor_OMPTeamsDistributeParallelForDirective;
  720. break;
  721. case Stmt::OMPTargetTeamsDirectiveClass:
  722. K = CXCursor_OMPTargetTeamsDirective;
  723. break;
  724. case Stmt::OMPTargetTeamsDistributeDirectiveClass:
  725. K = CXCursor_OMPTargetTeamsDistributeDirective;
  726. break;
  727. case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
  728. K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
  729. break;
  730. case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
  731. K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
  732. break;
  733. case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
  734. K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
  735. break;
  736. case Stmt::OMPInteropDirectiveClass:
  737. K = CXCursor_OMPInteropDirective;
  738. break;
  739. case Stmt::OMPDispatchDirectiveClass:
  740. K = CXCursor_OMPDispatchDirective;
  741. break;
  742. case Stmt::OMPMaskedDirectiveClass:
  743. K = CXCursor_OMPMaskedDirective;
  744. break;
  745. case Stmt::OMPGenericLoopDirectiveClass:
  746. K = CXCursor_OMPGenericLoopDirective;
  747. break;
  748. case Stmt::OMPTeamsGenericLoopDirectiveClass:
  749. K = CXCursor_OMPTeamsGenericLoopDirective;
  750. break;
  751. case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
  752. K = CXCursor_OMPTargetTeamsGenericLoopDirective;
  753. break;
  754. case Stmt::OMPParallelGenericLoopDirectiveClass:
  755. K = CXCursor_OMPParallelGenericLoopDirective;
  756. break;
  757. case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
  758. K = CXCursor_OMPTargetParallelGenericLoopDirective;
  759. break;
  760. case Stmt::BuiltinBitCastExprClass:
  761. K = CXCursor_BuiltinBitCastExpr;
  762. }
  763. CXCursor C = {K, 0, {Parent, S, TU}};
  764. return C;
  765. }
  766. CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
  767. SourceLocation Loc,
  768. CXTranslationUnit TU) {
  769. assert(Super && TU && "Invalid arguments!");
  770. void *RawLoc = Loc.getPtrEncoding();
  771. CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
  772. return C;
  773. }
  774. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  775. cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
  776. assert(C.kind == CXCursor_ObjCSuperClassRef);
  777. return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
  778. SourceLocation::getFromPtrEncoding(C.data[1]));
  779. }
  780. CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
  781. SourceLocation Loc,
  782. CXTranslationUnit TU) {
  783. assert(Proto && TU && "Invalid arguments!");
  784. void *RawLoc = Loc.getPtrEncoding();
  785. CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
  786. return C;
  787. }
  788. std::pair<const ObjCProtocolDecl *, SourceLocation>
  789. cxcursor::getCursorObjCProtocolRef(CXCursor C) {
  790. assert(C.kind == CXCursor_ObjCProtocolRef);
  791. return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
  792. SourceLocation::getFromPtrEncoding(C.data[1]));
  793. }
  794. CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
  795. SourceLocation Loc,
  796. CXTranslationUnit TU) {
  797. // 'Class' can be null for invalid code.
  798. if (!Class)
  799. return MakeCXCursorInvalid(CXCursor_InvalidCode);
  800. assert(TU && "Invalid arguments!");
  801. void *RawLoc = Loc.getPtrEncoding();
  802. CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
  803. return C;
  804. }
  805. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  806. cxcursor::getCursorObjCClassRef(CXCursor C) {
  807. assert(C.kind == CXCursor_ObjCClassRef);
  808. return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
  809. SourceLocation::getFromPtrEncoding(C.data[1]));
  810. }
  811. CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
  812. CXTranslationUnit TU) {
  813. assert(Type && TU && "Invalid arguments!");
  814. void *RawLoc = Loc.getPtrEncoding();
  815. CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
  816. return C;
  817. }
  818. std::pair<const TypeDecl *, SourceLocation>
  819. cxcursor::getCursorTypeRef(CXCursor C) {
  820. assert(C.kind == CXCursor_TypeRef);
  821. return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
  822. SourceLocation::getFromPtrEncoding(C.data[1]));
  823. }
  824. CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
  825. SourceLocation Loc,
  826. CXTranslationUnit TU) {
  827. assert(Template && TU && "Invalid arguments!");
  828. void *RawLoc = Loc.getPtrEncoding();
  829. CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
  830. return C;
  831. }
  832. std::pair<const TemplateDecl *, SourceLocation>
  833. cxcursor::getCursorTemplateRef(CXCursor C) {
  834. assert(C.kind == CXCursor_TemplateRef);
  835. return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
  836. SourceLocation::getFromPtrEncoding(C.data[1]));
  837. }
  838. CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
  839. SourceLocation Loc,
  840. CXTranslationUnit TU) {
  841. assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
  842. "Invalid arguments!");
  843. void *RawLoc = Loc.getPtrEncoding();
  844. CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
  845. return C;
  846. }
  847. std::pair<const NamedDecl *, SourceLocation>
  848. cxcursor::getCursorNamespaceRef(CXCursor C) {
  849. assert(C.kind == CXCursor_NamespaceRef);
  850. return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
  851. SourceLocation::getFromPtrEncoding(C.data[1]));
  852. }
  853. CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
  854. CXTranslationUnit TU) {
  855. assert(Var && TU && "Invalid arguments!");
  856. void *RawLoc = Loc.getPtrEncoding();
  857. CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
  858. return C;
  859. }
  860. std::pair<const VarDecl *, SourceLocation>
  861. cxcursor::getCursorVariableRef(CXCursor C) {
  862. assert(C.kind == CXCursor_VariableRef);
  863. return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
  864. SourceLocation::getFromPtrEncoding(C.data[1]));
  865. }
  866. CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
  867. SourceLocation Loc,
  868. CXTranslationUnit TU) {
  869. assert(Field && TU && "Invalid arguments!");
  870. void *RawLoc = Loc.getPtrEncoding();
  871. CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
  872. return C;
  873. }
  874. std::pair<const FieldDecl *, SourceLocation>
  875. cxcursor::getCursorMemberRef(CXCursor C) {
  876. assert(C.kind == CXCursor_MemberRef);
  877. return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
  878. SourceLocation::getFromPtrEncoding(C.data[1]));
  879. }
  880. CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
  881. CXTranslationUnit TU) {
  882. CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
  883. return C;
  884. }
  885. const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
  886. assert(C.kind == CXCursor_CXXBaseSpecifier);
  887. return static_cast<const CXXBaseSpecifier *>(C.data[0]);
  888. }
  889. CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
  890. CXTranslationUnit TU) {
  891. CXCursor C = {
  892. CXCursor_PreprocessingDirective,
  893. 0,
  894. {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
  895. return C;
  896. }
  897. SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
  898. assert(C.kind == CXCursor_PreprocessingDirective);
  899. SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
  900. SourceLocation::getFromPtrEncoding(C.data[1]));
  901. ASTUnit *TU = getCursorASTUnit(C);
  902. return TU->mapRangeFromPreamble(Range);
  903. }
  904. CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
  905. CXTranslationUnit TU) {
  906. CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
  907. return C;
  908. }
  909. const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
  910. assert(C.kind == CXCursor_MacroDefinition);
  911. return static_cast<const MacroDefinitionRecord *>(C.data[0]);
  912. }
  913. CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
  914. CXTranslationUnit TU) {
  915. CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
  916. return C;
  917. }
  918. CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
  919. SourceLocation Loc,
  920. CXTranslationUnit TU) {
  921. assert(Loc.isValid());
  922. CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
  923. return C;
  924. }
  925. const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
  926. if (isPseudo())
  927. return getAsMacroDefinition()->getName();
  928. return getAsMacroExpansion()->getName();
  929. }
  930. const MacroDefinitionRecord *
  931. cxcursor::MacroExpansionCursor::getDefinition() const {
  932. if (isPseudo())
  933. return getAsMacroDefinition();
  934. return getAsMacroExpansion()->getDefinition();
  935. }
  936. SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
  937. if (isPseudo())
  938. return getPseudoLoc();
  939. return getAsMacroExpansion()->getSourceRange();
  940. }
  941. CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
  942. CXTranslationUnit TU) {
  943. CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
  944. return C;
  945. }
  946. const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
  947. assert(C.kind == CXCursor_InclusionDirective);
  948. return static_cast<const InclusionDirective *>(C.data[0]);
  949. }
  950. CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
  951. CXTranslationUnit TU) {
  952. assert(Label && TU && "Invalid arguments!");
  953. void *RawLoc = Loc.getPtrEncoding();
  954. CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
  955. return C;
  956. }
  957. std::pair<const LabelStmt *, SourceLocation>
  958. cxcursor::getCursorLabelRef(CXCursor C) {
  959. assert(C.kind == CXCursor_LabelRef);
  960. return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
  961. SourceLocation::getFromPtrEncoding(C.data[1]));
  962. }
  963. CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
  964. CXTranslationUnit TU) {
  965. assert(E && TU && "Invalid arguments!");
  966. OverloadedDeclRefStorage Storage(E);
  967. void *RawLoc = E->getNameLoc().getPtrEncoding();
  968. CXCursor C = {
  969. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  970. return C;
  971. }
  972. CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
  973. SourceLocation Loc,
  974. CXTranslationUnit TU) {
  975. assert(D && TU && "Invalid arguments!");
  976. void *RawLoc = Loc.getPtrEncoding();
  977. OverloadedDeclRefStorage Storage(D);
  978. CXCursor C = {
  979. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  980. return C;
  981. }
  982. CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
  983. SourceLocation Loc,
  984. CXTranslationUnit TU) {
  985. assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
  986. void *RawLoc = Loc.getPtrEncoding();
  987. OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
  988. CXCursor C = {
  989. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  990. return C;
  991. }
  992. std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
  993. cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
  994. assert(C.kind == CXCursor_OverloadedDeclRef);
  995. return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
  996. const_cast<void *>(C.data[0])),
  997. SourceLocation::getFromPtrEncoding(C.data[1]));
  998. }
  999. const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
  1000. return static_cast<const Decl *>(Cursor.data[0]);
  1001. }
  1002. const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
  1003. return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
  1004. }
  1005. const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
  1006. if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
  1007. Cursor.kind == CXCursor_ObjCProtocolRef ||
  1008. Cursor.kind == CXCursor_ObjCClassRef)
  1009. return nullptr;
  1010. return static_cast<const Stmt *>(Cursor.data[1]);
  1011. }
  1012. const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
  1013. return static_cast<const Attr *>(Cursor.data[1]);
  1014. }
  1015. ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
  1016. return getCursorASTUnit(Cursor)->getASTContext();
  1017. }
  1018. ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
  1019. CXTranslationUnit TU = getCursorTU(Cursor);
  1020. if (!TU)
  1021. return nullptr;
  1022. return cxtu::getASTUnit(TU);
  1023. }
  1024. CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
  1025. return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
  1026. }
  1027. void cxcursor::getOverriddenCursors(CXCursor cursor,
  1028. SmallVectorImpl<CXCursor> &overridden) {
  1029. assert(clang_isDeclaration(cursor.kind));
  1030. const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
  1031. if (!D)
  1032. return;
  1033. CXTranslationUnit TU = getCursorTU(cursor);
  1034. SmallVector<const NamedDecl *, 8> OverDecls;
  1035. D->getASTContext().getOverriddenMethods(D, OverDecls);
  1036. for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
  1037. E = OverDecls.end();
  1038. I != E; ++I) {
  1039. overridden.push_back(MakeCXCursor(*I, TU));
  1040. }
  1041. }
  1042. std::pair<int, SourceLocation>
  1043. cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
  1044. if (cursor.kind == CXCursor_ObjCMessageExpr) {
  1045. if (cursor.xdata != -1)
  1046. return std::make_pair(cursor.xdata,
  1047. cast<ObjCMessageExpr>(getCursorExpr(cursor))
  1048. ->getSelectorLoc(cursor.xdata));
  1049. } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
  1050. cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
  1051. if (cursor.xdata != -1)
  1052. return std::make_pair(cursor.xdata,
  1053. cast<ObjCMethodDecl>(getCursorDecl(cursor))
  1054. ->getSelectorLoc(cursor.xdata));
  1055. }
  1056. return std::make_pair(-1, SourceLocation());
  1057. }
  1058. CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
  1059. CXCursor newCursor = cursor;
  1060. if (cursor.kind == CXCursor_ObjCMessageExpr) {
  1061. if (SelIdx == -1 ||
  1062. unsigned(SelIdx) >=
  1063. cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
  1064. newCursor.xdata = -1;
  1065. else
  1066. newCursor.xdata = SelIdx;
  1067. } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
  1068. cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
  1069. if (SelIdx == -1 ||
  1070. unsigned(SelIdx) >=
  1071. cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
  1072. newCursor.xdata = -1;
  1073. else
  1074. newCursor.xdata = SelIdx;
  1075. }
  1076. return newCursor;
  1077. }
  1078. CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
  1079. if (cursor.kind != CXCursor_CallExpr)
  1080. return cursor;
  1081. if (cursor.xdata == 0)
  1082. return cursor;
  1083. const Expr *E = getCursorExpr(cursor);
  1084. TypeSourceInfo *Type = nullptr;
  1085. if (const CXXUnresolvedConstructExpr *UnCtor =
  1086. dyn_cast<CXXUnresolvedConstructExpr>(E)) {
  1087. Type = UnCtor->getTypeSourceInfo();
  1088. } else if (const CXXTemporaryObjectExpr *Tmp =
  1089. dyn_cast<CXXTemporaryObjectExpr>(E)) {
  1090. Type = Tmp->getTypeSourceInfo();
  1091. }
  1092. if (!Type)
  1093. return cursor;
  1094. CXTranslationUnit TU = getCursorTU(cursor);
  1095. QualType Ty = Type->getType();
  1096. TypeLoc TL = Type->getTypeLoc();
  1097. SourceLocation Loc = TL.getBeginLoc();
  1098. if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
  1099. Ty = ElabT->getNamedType();
  1100. ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
  1101. Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
  1102. }
  1103. if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
  1104. return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
  1105. if (const TagType *Tag = Ty->getAs<TagType>())
  1106. return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
  1107. if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
  1108. return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
  1109. return cursor;
  1110. }
  1111. bool cxcursor::operator==(CXCursor X, CXCursor Y) {
  1112. return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
  1113. X.data[2] == Y.data[2];
  1114. }
  1115. // FIXME: Remove once we can model DeclGroups and their appropriate ranges
  1116. // properly in the ASTs.
  1117. bool cxcursor::isFirstInDeclGroup(CXCursor C) {
  1118. assert(clang_isDeclaration(C.kind));
  1119. return ((uintptr_t)(C.data[1])) != 0;
  1120. }
  1121. //===----------------------------------------------------------------------===//
  1122. // libclang CXCursor APIs
  1123. //===----------------------------------------------------------------------===//
  1124. int clang_Cursor_isNull(CXCursor cursor) {
  1125. return clang_equalCursors(cursor, clang_getNullCursor());
  1126. }
  1127. CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
  1128. return getCursorTU(cursor);
  1129. }
  1130. int clang_Cursor_getNumArguments(CXCursor C) {
  1131. if (clang_isDeclaration(C.kind)) {
  1132. const Decl *D = cxcursor::getCursorDecl(C);
  1133. if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
  1134. return MD->param_size();
  1135. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
  1136. return FD->param_size();
  1137. }
  1138. if (clang_isExpression(C.kind)) {
  1139. const Expr *E = cxcursor::getCursorExpr(C);
  1140. if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
  1141. return CE->getNumArgs();
  1142. }
  1143. if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
  1144. return CE->getNumArgs();
  1145. }
  1146. }
  1147. return -1;
  1148. }
  1149. CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
  1150. if (clang_isDeclaration(C.kind)) {
  1151. const Decl *D = cxcursor::getCursorDecl(C);
  1152. if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
  1153. if (i < MD->param_size())
  1154. return cxcursor::MakeCXCursor(MD->parameters()[i],
  1155. cxcursor::getCursorTU(C));
  1156. } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1157. if (i < FD->param_size())
  1158. return cxcursor::MakeCXCursor(FD->parameters()[i],
  1159. cxcursor::getCursorTU(C));
  1160. }
  1161. }
  1162. if (clang_isExpression(C.kind)) {
  1163. const Expr *E = cxcursor::getCursorExpr(C);
  1164. if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
  1165. if (i < CE->getNumArgs()) {
  1166. return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
  1167. cxcursor::getCursorTU(C));
  1168. }
  1169. }
  1170. if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
  1171. if (i < CE->getNumArgs()) {
  1172. return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
  1173. cxcursor::getCursorTU(C));
  1174. }
  1175. }
  1176. }
  1177. return clang_getNullCursor();
  1178. }
  1179. int clang_Cursor_getNumTemplateArguments(CXCursor C) {
  1180. CXCursorKind kind = clang_getCursorKind(C);
  1181. if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
  1182. kind != CXCursor_ClassDecl &&
  1183. kind != CXCursor_ClassTemplatePartialSpecialization) {
  1184. return -1;
  1185. }
  1186. if (const auto *FD =
  1187. llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
  1188. const FunctionTemplateSpecializationInfo *SpecInfo =
  1189. FD->getTemplateSpecializationInfo();
  1190. if (!SpecInfo) {
  1191. return -1;
  1192. }
  1193. return SpecInfo->TemplateArguments->size();
  1194. }
  1195. if (const auto *SD =
  1196. llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
  1197. getCursorDecl(C))) {
  1198. return SD->getTemplateArgs().size();
  1199. }
  1200. return -1;
  1201. }
  1202. enum CXGetTemplateArgumentStatus {
  1203. /** The operation completed successfully */
  1204. CXGetTemplateArgumentStatus_Success = 0,
  1205. /** The specified cursor did not represent a FunctionDecl or
  1206. ClassTemplateSpecializationDecl. */
  1207. CXGetTemplateArgumentStatus_CursorNotCompatibleDecl = -1,
  1208. /** The specified cursor was not castable to a FunctionDecl or
  1209. ClassTemplateSpecializationDecl. */
  1210. CXGetTemplateArgumentStatus_BadDeclCast = -2,
  1211. /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
  1212. CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
  1213. /** An invalid (OOB) argument index was specified */
  1214. CXGetTemplateArgumentStatus_InvalidIndex = -4
  1215. };
  1216. static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
  1217. TemplateArgument *TA) {
  1218. CXCursorKind kind = clang_getCursorKind(C);
  1219. if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
  1220. kind != CXCursor_ClassDecl &&
  1221. kind != CXCursor_ClassTemplatePartialSpecialization) {
  1222. return -1;
  1223. }
  1224. if (const auto *FD =
  1225. llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
  1226. const FunctionTemplateSpecializationInfo *SpecInfo =
  1227. FD->getTemplateSpecializationInfo();
  1228. if (!SpecInfo) {
  1229. return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
  1230. }
  1231. if (I >= SpecInfo->TemplateArguments->size()) {
  1232. return CXGetTemplateArgumentStatus_InvalidIndex;
  1233. }
  1234. *TA = SpecInfo->TemplateArguments->get(I);
  1235. return 0;
  1236. }
  1237. if (const auto *SD =
  1238. llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
  1239. getCursorDecl(C))) {
  1240. if (I >= SD->getTemplateArgs().size()) {
  1241. return CXGetTemplateArgumentStatus_InvalidIndex;
  1242. }
  1243. *TA = SD->getTemplateArgs()[I];
  1244. return 0;
  1245. }
  1246. return CXGetTemplateArgumentStatus_BadDeclCast;
  1247. }
  1248. enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
  1249. unsigned I) {
  1250. TemplateArgument TA;
  1251. if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
  1252. return CXTemplateArgumentKind_Invalid;
  1253. }
  1254. switch (TA.getKind()) {
  1255. case TemplateArgument::Null:
  1256. return CXTemplateArgumentKind_Null;
  1257. case TemplateArgument::Type:
  1258. return CXTemplateArgumentKind_Type;
  1259. case TemplateArgument::Declaration:
  1260. return CXTemplateArgumentKind_Declaration;
  1261. case TemplateArgument::NullPtr:
  1262. return CXTemplateArgumentKind_NullPtr;
  1263. case TemplateArgument::Integral:
  1264. return CXTemplateArgumentKind_Integral;
  1265. case TemplateArgument::Template:
  1266. return CXTemplateArgumentKind_Template;
  1267. case TemplateArgument::TemplateExpansion:
  1268. return CXTemplateArgumentKind_TemplateExpansion;
  1269. case TemplateArgument::Expression:
  1270. return CXTemplateArgumentKind_Expression;
  1271. case TemplateArgument::Pack:
  1272. return CXTemplateArgumentKind_Pack;
  1273. }
  1274. return CXTemplateArgumentKind_Invalid;
  1275. }
  1276. CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
  1277. TemplateArgument TA;
  1278. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1279. CXGetTemplateArgumentStatus_Success) {
  1280. return cxtype::MakeCXType(QualType(), getCursorTU(C));
  1281. }
  1282. if (TA.getKind() != TemplateArgument::Type) {
  1283. return cxtype::MakeCXType(QualType(), getCursorTU(C));
  1284. }
  1285. return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
  1286. }
  1287. long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
  1288. TemplateArgument TA;
  1289. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1290. CXGetTemplateArgumentStatus_Success) {
  1291. assert(0 && "Unable to retrieve TemplateArgument");
  1292. return 0;
  1293. }
  1294. if (TA.getKind() != TemplateArgument::Integral) {
  1295. assert(0 && "Passed template argument is not Integral");
  1296. return 0;
  1297. }
  1298. return TA.getAsIntegral().getSExtValue();
  1299. }
  1300. unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
  1301. unsigned I) {
  1302. TemplateArgument TA;
  1303. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1304. CXGetTemplateArgumentStatus_Success) {
  1305. assert(0 && "Unable to retrieve TemplateArgument");
  1306. return 0;
  1307. }
  1308. if (TA.getKind() != TemplateArgument::Integral) {
  1309. assert(0 && "Passed template argument is not Integral");
  1310. return 0;
  1311. }
  1312. return TA.getAsIntegral().getZExtValue();
  1313. }
  1314. //===----------------------------------------------------------------------===//
  1315. // CXCursorSet.
  1316. //===----------------------------------------------------------------------===//
  1317. typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
  1318. static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
  1319. return (CXCursorSet)setImpl;
  1320. }
  1321. static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
  1322. return (CXCursorSet_Impl *)set;
  1323. }
  1324. namespace llvm {
  1325. template <> struct DenseMapInfo<CXCursor> {
  1326. public:
  1327. static inline CXCursor getEmptyKey() {
  1328. return MakeCXCursorInvalid(CXCursor_InvalidFile);
  1329. }
  1330. static inline CXCursor getTombstoneKey() {
  1331. return MakeCXCursorInvalid(CXCursor_NoDeclFound);
  1332. }
  1333. static inline unsigned getHashValue(const CXCursor &cursor) {
  1334. return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
  1335. getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
  1336. }
  1337. static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
  1338. return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
  1339. }
  1340. };
  1341. } // namespace llvm
  1342. CXCursorSet clang_createCXCursorSet() {
  1343. return packCXCursorSet(new CXCursorSet_Impl());
  1344. }
  1345. void clang_disposeCXCursorSet(CXCursorSet set) {
  1346. delete unpackCXCursorSet(set);
  1347. }
  1348. unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
  1349. CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
  1350. if (!setImpl)
  1351. return 0;
  1352. return setImpl->find(cursor) != setImpl->end();
  1353. }
  1354. unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
  1355. // Do not insert invalid cursors into the set.
  1356. if (cursor.kind >= CXCursor_FirstInvalid &&
  1357. cursor.kind <= CXCursor_LastInvalid)
  1358. return 1;
  1359. CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
  1360. if (!setImpl)
  1361. return 1;
  1362. unsigned &entry = (*setImpl)[cursor];
  1363. unsigned flag = entry == 0 ? 1 : 0;
  1364. entry = 1;
  1365. return flag;
  1366. }
  1367. CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
  1368. enum CXCursorKind kind = clang_getCursorKind(cursor);
  1369. if (clang_isDeclaration(kind)) {
  1370. const Decl *decl = getCursorDecl(cursor);
  1371. if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
  1372. ASTUnit *unit = getCursorASTUnit(cursor);
  1373. CodeCompletionResult Result(namedDecl, CCP_Declaration);
  1374. CodeCompletionString *String = Result.CreateCodeCompletionString(
  1375. unit->getASTContext(), unit->getPreprocessor(),
  1376. CodeCompletionContext::CCC_Other,
  1377. unit->getCodeCompletionTUInfo().getAllocator(),
  1378. unit->getCodeCompletionTUInfo(), true);
  1379. return String;
  1380. }
  1381. } else if (kind == CXCursor_MacroDefinition) {
  1382. const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
  1383. const IdentifierInfo *Macro = definition->getName();
  1384. ASTUnit *unit = getCursorASTUnit(cursor);
  1385. CodeCompletionResult Result(
  1386. Macro,
  1387. unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
  1388. CodeCompletionString *String = Result.CreateCodeCompletionString(
  1389. unit->getASTContext(), unit->getPreprocessor(),
  1390. CodeCompletionContext::CCC_Other,
  1391. unit->getCodeCompletionTUInfo().getAllocator(),
  1392. unit->getCodeCompletionTUInfo(), false);
  1393. return String;
  1394. }
  1395. return nullptr;
  1396. }
  1397. namespace {
  1398. struct OverridenCursorsPool {
  1399. typedef SmallVector<CXCursor, 2> CursorVec;
  1400. std::vector<CursorVec *> AllCursors;
  1401. std::vector<CursorVec *> AvailableCursors;
  1402. ~OverridenCursorsPool() {
  1403. for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
  1404. E = AllCursors.end();
  1405. I != E; ++I) {
  1406. delete *I;
  1407. }
  1408. }
  1409. };
  1410. } // namespace
  1411. void *cxcursor::createOverridenCXCursorsPool() {
  1412. return new OverridenCursorsPool();
  1413. }
  1414. void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
  1415. delete static_cast<OverridenCursorsPool *>(pool);
  1416. }
  1417. void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
  1418. unsigned *num_overridden) {
  1419. if (overridden)
  1420. *overridden = nullptr;
  1421. if (num_overridden)
  1422. *num_overridden = 0;
  1423. CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
  1424. if (!overridden || !num_overridden || !TU)
  1425. return;
  1426. if (!clang_isDeclaration(cursor.kind))
  1427. return;
  1428. OverridenCursorsPool &pool =
  1429. *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
  1430. OverridenCursorsPool::CursorVec *Vec = nullptr;
  1431. if (!pool.AvailableCursors.empty()) {
  1432. Vec = pool.AvailableCursors.back();
  1433. pool.AvailableCursors.pop_back();
  1434. } else {
  1435. Vec = new OverridenCursorsPool::CursorVec();
  1436. pool.AllCursors.push_back(Vec);
  1437. }
  1438. // Clear out the vector, but don't free the memory contents. This
  1439. // reduces malloc() traffic.
  1440. Vec->clear();
  1441. // Use the first entry to contain a back reference to the vector.
  1442. // This is a complete hack.
  1443. CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
  1444. backRefCursor.data[0] = Vec;
  1445. assert(cxcursor::getCursorTU(backRefCursor) == TU);
  1446. Vec->push_back(backRefCursor);
  1447. // Get the overridden cursors.
  1448. cxcursor::getOverriddenCursors(cursor, *Vec);
  1449. // Did we get any overridden cursors? If not, return Vec to the pool
  1450. // of available cursor vectors.
  1451. if (Vec->size() == 1) {
  1452. pool.AvailableCursors.push_back(Vec);
  1453. return;
  1454. }
  1455. // Now tell the caller about the overridden cursors.
  1456. assert(Vec->size() > 1);
  1457. *overridden = &((*Vec)[1]);
  1458. *num_overridden = Vec->size() - 1;
  1459. }
  1460. void clang_disposeOverriddenCursors(CXCursor *overridden) {
  1461. if (!overridden)
  1462. return;
  1463. // Use pointer arithmetic to get back the first faux entry
  1464. // which has a back-reference to the TU and the vector.
  1465. --overridden;
  1466. OverridenCursorsPool::CursorVec *Vec =
  1467. static_cast<OverridenCursorsPool::CursorVec *>(
  1468. const_cast<void *>(overridden->data[0]));
  1469. CXTranslationUnit TU = getCursorTU(*overridden);
  1470. assert(Vec && TU);
  1471. OverridenCursorsPool &pool =
  1472. *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
  1473. pool.AvailableCursors.push_back(Vec);
  1474. }
  1475. int clang_Cursor_isDynamicCall(CXCursor C) {
  1476. const Expr *E = nullptr;
  1477. if (clang_isExpression(C.kind))
  1478. E = getCursorExpr(C);
  1479. if (!E)
  1480. return 0;
  1481. if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
  1482. if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
  1483. return false;
  1484. if (auto *RecE = dyn_cast<ObjCMessageExpr>(
  1485. MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
  1486. if (RecE->getMethodFamily() == OMF_alloc)
  1487. return false;
  1488. }
  1489. return true;
  1490. }
  1491. if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
  1492. return !PropRefE->isSuperReceiver();
  1493. }
  1494. const MemberExpr *ME = nullptr;
  1495. if (isa<MemberExpr>(E))
  1496. ME = cast<MemberExpr>(E);
  1497. else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
  1498. ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
  1499. if (ME) {
  1500. if (const CXXMethodDecl *MD =
  1501. dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
  1502. return MD->isVirtual() &&
  1503. ME->performsVirtualDispatch(
  1504. cxcursor::getCursorContext(C).getLangOpts());
  1505. }
  1506. return 0;
  1507. }
  1508. CXType clang_Cursor_getReceiverType(CXCursor C) {
  1509. CXTranslationUnit TU = cxcursor::getCursorTU(C);
  1510. const Expr *E = nullptr;
  1511. if (clang_isExpression(C.kind))
  1512. E = getCursorExpr(C);
  1513. if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
  1514. return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
  1515. if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
  1516. return cxtype::MakeCXType(
  1517. PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
  1518. }
  1519. const MemberExpr *ME = nullptr;
  1520. if (isa<MemberExpr>(E))
  1521. ME = cast<MemberExpr>(E);
  1522. else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
  1523. ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
  1524. if (ME) {
  1525. if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
  1526. auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
  1527. return cxtype::MakeCXType(receiverTy, TU);
  1528. }
  1529. }
  1530. return cxtype::MakeCXType(QualType(), TU);
  1531. }