Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/clang/tools/libclang/CIndex.cpp
Warning:line 8932, column 15
Value stored to 'str' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name CIndex.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _CINDEX_LIB_ -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/tools/libclang -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/clang/tools/libclang -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/clang/tools/libclang/CIndex.cpp
1//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
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 implements the main API hooks in the Clang-C Source Indexing
10// library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIndexDiagnostic.h"
15#include "CIndexer.h"
16#include "CLog.h"
17#include "CXCursor.h"
18#include "CXSourceLocation.h"
19#include "CXString.h"
20#include "CXTranslationUnit.h"
21#include "CXType.h"
22#include "CursorVisitor.h"
23#include "clang-c/FatalErrorHandler.h"
24#include "clang/AST/Attr.h"
25#include "clang/AST/DeclObjCCommon.h"
26#include "clang/AST/Mangle.h"
27#include "clang/AST/OpenMPClause.h"
28#include "clang/AST/StmtVisitor.h"
29#include "clang/Basic/Diagnostic.h"
30#include "clang/Basic/DiagnosticCategories.h"
31#include "clang/Basic/DiagnosticIDs.h"
32#include "clang/Basic/Stack.h"
33#include "clang/Basic/TargetInfo.h"
34#include "clang/Basic/Version.h"
35#include "clang/Frontend/ASTUnit.h"
36#include "clang/Frontend/CompilerInstance.h"
37#include "clang/Index/CommentToXML.h"
38#include "clang/Lex/HeaderSearch.h"
39#include "clang/Lex/Lexer.h"
40#include "clang/Lex/PreprocessingRecord.h"
41#include "clang/Lex/Preprocessor.h"
42#include "llvm/ADT/Optional.h"
43#include "llvm/ADT/STLExtras.h"
44#include "llvm/ADT/StringSwitch.h"
45#include "llvm/Config/llvm-config.h"
46#include "llvm/Support/Compiler.h"
47#include "llvm/Support/CrashRecoveryContext.h"
48#include "llvm/Support/Format.h"
49#include "llvm/Support/ManagedStatic.h"
50#include "llvm/Support/MemoryBuffer.h"
51#include "llvm/Support/Program.h"
52#include "llvm/Support/SaveAndRestore.h"
53#include "llvm/Support/Signals.h"
54#include "llvm/Support/TargetSelect.h"
55#include "llvm/Support/Threading.h"
56#include "llvm/Support/Timer.h"
57#include "llvm/Support/raw_ostream.h"
58#include "llvm/Support/thread.h"
59#include <mutex>
60
61#if LLVM_ENABLE_THREADS1 != 0 && defined(__APPLE__)
62#define USE_DARWIN_THREADS
63#endif
64
65#ifdef USE_DARWIN_THREADS
66#include <pthread.h>
67#endif
68
69using namespace clang;
70using namespace clang::cxcursor;
71using namespace clang::cxtu;
72using namespace clang::cxindex;
73
74CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx,
75 std::unique_ptr<ASTUnit> AU) {
76 if (!AU)
77 return nullptr;
78 assert(CIdx)(static_cast <bool> (CIdx) ? void (0) : __assert_fail (
"CIdx", "clang/tools/libclang/CIndex.cpp", 78, __extension__ __PRETTY_FUNCTION__
))
;
79 CXTranslationUnit D = new CXTranslationUnitImpl();
80 D->CIdx = CIdx;
81 D->TheASTUnit = AU.release();
82 D->StringPool = new cxstring::CXStringPool();
83 D->Diagnostics = nullptr;
84 D->OverridenCursorsPool = createOverridenCXCursorsPool();
85 D->CommentToXML = nullptr;
86 D->ParsingOptions = 0;
87 D->Arguments = {};
88 return D;
89}
90
91bool cxtu::isASTReadError(ASTUnit *AU) {
92 for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(),
93 DEnd = AU->stored_diag_end();
94 D != DEnd; ++D) {
95 if (D->getLevel() >= DiagnosticsEngine::Error &&
96 DiagnosticIDs::getCategoryNumberForDiag(D->getID()) ==
97 diag::DiagCat_AST_Deserialization_Issue)
98 return true;
99 }
100 return false;
101}
102
103cxtu::CXTUOwner::~CXTUOwner() {
104 if (TU)
105 clang_disposeTranslationUnit(TU);
106}
107
108/// Compare two source ranges to determine their relative position in
109/// the translation unit.
110static RangeComparisonResult RangeCompare(SourceManager &SM, SourceRange R1,
111 SourceRange R2) {
112 assert(R1.isValid() && "First range is invalid?")(static_cast <bool> (R1.isValid() && "First range is invalid?"
) ? void (0) : __assert_fail ("R1.isValid() && \"First range is invalid?\""
, "clang/tools/libclang/CIndex.cpp", 112, __extension__ __PRETTY_FUNCTION__
))
;
113 assert(R2.isValid() && "Second range is invalid?")(static_cast <bool> (R2.isValid() && "Second range is invalid?"
) ? void (0) : __assert_fail ("R2.isValid() && \"Second range is invalid?\""
, "clang/tools/libclang/CIndex.cpp", 113, __extension__ __PRETTY_FUNCTION__
))
;
114 if (R1.getEnd() != R2.getBegin() &&
115 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
116 return RangeBefore;
117 if (R2.getEnd() != R1.getBegin() &&
118 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
119 return RangeAfter;
120 return RangeOverlap;
121}
122
123/// Determine if a source location falls within, before, or after a
124/// a given source range.
125static RangeComparisonResult LocationCompare(SourceManager &SM,
126 SourceLocation L, SourceRange R) {
127 assert(R.isValid() && "First range is invalid?")(static_cast <bool> (R.isValid() && "First range is invalid?"
) ? void (0) : __assert_fail ("R.isValid() && \"First range is invalid?\""
, "clang/tools/libclang/CIndex.cpp", 127, __extension__ __PRETTY_FUNCTION__
))
;
128 assert(L.isValid() && "Second range is invalid?")(static_cast <bool> (L.isValid() && "Second range is invalid?"
) ? void (0) : __assert_fail ("L.isValid() && \"Second range is invalid?\""
, "clang/tools/libclang/CIndex.cpp", 128, __extension__ __PRETTY_FUNCTION__
))
;
129 if (L == R.getBegin() || L == R.getEnd())
130 return RangeOverlap;
131 if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
132 return RangeBefore;
133 if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
134 return RangeAfter;
135 return RangeOverlap;
136}
137
138/// Translate a Clang source range into a CIndex source range.
139///
140/// Clang internally represents ranges where the end location points to the
141/// start of the token at the end. However, for external clients it is more
142/// useful to have a CXSourceRange be a proper half-open interval. This routine
143/// does the appropriate translation.
144CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
145 const LangOptions &LangOpts,
146 const CharSourceRange &R) {
147 // We want the last character in this location, so we will adjust the
148 // location accordingly.
149 SourceLocation EndLoc = R.getEnd();
150 bool IsTokenRange = R.isTokenRange();
151 if (EndLoc.isValid() && EndLoc.isMacroID() &&
152 !SM.isMacroArgExpansion(EndLoc)) {
153 CharSourceRange Expansion = SM.getExpansionRange(EndLoc);
154 EndLoc = Expansion.getEnd();
155 IsTokenRange = Expansion.isTokenRange();
156 }
157 if (IsTokenRange && EndLoc.isValid()) {
158 unsigned Length =
159 Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), SM, LangOpts);
160 EndLoc = EndLoc.getLocWithOffset(Length);
161 }
162
163 CXSourceRange Result = {
164 {&SM, &LangOpts}, R.getBegin().getRawEncoding(), EndLoc.getRawEncoding()};
165 return Result;
166}
167
168CharSourceRange cxloc::translateCXRangeToCharRange(CXSourceRange R) {
169 return CharSourceRange::getCharRange(
170 SourceLocation::getFromRawEncoding(R.begin_int_data),
171 SourceLocation::getFromRawEncoding(R.end_int_data));
172}
173
174//===----------------------------------------------------------------------===//
175// Cursor visitor.
176//===----------------------------------------------------------------------===//
177
178static SourceRange getRawCursorExtent(CXCursor C);
179static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
180
181RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
182 return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
183}
184
185/// Visit the given cursor and, if requested by the visitor,
186/// its children.
187///
188/// \param Cursor the cursor to visit.
189///
190/// \param CheckedRegionOfInterest if true, then the caller already checked
191/// that this cursor is within the region of interest.
192///
193/// \returns true if the visitation should be aborted, false if it
194/// should continue.
195bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
196 if (clang_isInvalid(Cursor.kind))
197 return false;
198
199 if (clang_isDeclaration(Cursor.kind)) {
200 const Decl *D = getCursorDecl(Cursor);
201 if (!D) {
202 assert(0 && "Invalid declaration cursor")(static_cast <bool> (0 && "Invalid declaration cursor"
) ? void (0) : __assert_fail ("0 && \"Invalid declaration cursor\""
, "clang/tools/libclang/CIndex.cpp", 202, __extension__ __PRETTY_FUNCTION__
))
;
203 return true; // abort.
204 }
205
206 // Ignore implicit declarations, unless it's an objc method because
207 // currently we should report implicit methods for properties when indexing.
208 if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
209 return false;
210 }
211
212 // If we have a range of interest, and this cursor doesn't intersect with it,
213 // we're done.
214 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
215 SourceRange Range = getRawCursorExtent(Cursor);
216 if (Range.isInvalid() || CompareRegionOfInterest(Range))
217 return false;
218 }
219
220 switch (Visitor(Cursor, Parent, ClientData)) {
221 case CXChildVisit_Break:
222 return true;
223
224 case CXChildVisit_Continue:
225 return false;
226
227 case CXChildVisit_Recurse: {
228 bool ret = VisitChildren(Cursor);
229 if (PostChildrenVisitor)
230 if (PostChildrenVisitor(Cursor, ClientData))
231 return true;
232 return ret;
233 }
234 }
235
236 llvm_unreachable("Invalid CXChildVisitResult!")::llvm::llvm_unreachable_internal("Invalid CXChildVisitResult!"
, "clang/tools/libclang/CIndex.cpp", 236)
;
237}
238
239static bool visitPreprocessedEntitiesInRange(SourceRange R,
240 PreprocessingRecord &PPRec,
241 CursorVisitor &Visitor) {
242 SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
243 FileID FID;
244
245 if (!Visitor.shouldVisitIncludedEntities()) {
246 // If the begin/end of the range lie in the same FileID, do the optimization
247 // where we skip preprocessed entities that do not come from the same
248 // FileID.
249 FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
250 if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
251 FID = FileID();
252 }
253
254 const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R);
255 return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(),
256 PPRec, FID);
257}
258
259bool CursorVisitor::visitFileRegion() {
260 if (RegionOfInterest.isInvalid())
261 return false;
262
263 ASTUnit *Unit = cxtu::getASTUnit(TU);
264 SourceManager &SM = Unit->getSourceManager();
265
266 std::pair<FileID, unsigned> Begin = SM.getDecomposedLoc(
267 SM.getFileLoc(RegionOfInterest.getBegin())),
268 End = SM.getDecomposedLoc(
269 SM.getFileLoc(RegionOfInterest.getEnd()));
270
271 if (End.first != Begin.first) {
272 // If the end does not reside in the same file, try to recover by
273 // picking the end of the file of begin location.
274 End.first = Begin.first;
275 End.second = SM.getFileIDSize(Begin.first);
276 }
277
278 assert(Begin.first == End.first)(static_cast <bool> (Begin.first == End.first) ? void (
0) : __assert_fail ("Begin.first == End.first", "clang/tools/libclang/CIndex.cpp"
, 278, __extension__ __PRETTY_FUNCTION__))
;
279 if (Begin.second > End.second)
280 return false;
281
282 FileID File = Begin.first;
283 unsigned Offset = Begin.second;
284 unsigned Length = End.second - Begin.second;
285
286 if (!VisitDeclsOnly && !VisitPreprocessorLast)
287 if (visitPreprocessedEntitiesInRegion())
288 return true; // visitation break.
289
290 if (visitDeclsFromFileRegion(File, Offset, Length))
291 return true; // visitation break.
292
293 if (!VisitDeclsOnly && VisitPreprocessorLast)
294 return visitPreprocessedEntitiesInRegion();
295
296 return false;
297}
298
299static bool isInLexicalContext(Decl *D, DeclContext *DC) {
300 if (!DC)
301 return false;
302
303 for (DeclContext *DeclDC = D->getLexicalDeclContext(); DeclDC;
304 DeclDC = DeclDC->getLexicalParent()) {
305 if (DeclDC == DC)
306 return true;
307 }
308 return false;
309}
310
311bool CursorVisitor::visitDeclsFromFileRegion(FileID File, unsigned Offset,
312 unsigned Length) {
313 ASTUnit *Unit = cxtu::getASTUnit(TU);
314 SourceManager &SM = Unit->getSourceManager();
315 SourceRange Range = RegionOfInterest;
316
317 SmallVector<Decl *, 16> Decls;
318 Unit->findFileRegionDecls(File, Offset, Length, Decls);
319
320 // If we didn't find any file level decls for the file, try looking at the
321 // file that it was included from.
322 while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
323 bool Invalid = false;
324 const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
325 if (Invalid)
326 return false;
327
328 SourceLocation Outer;
329 if (SLEntry.isFile())
330 Outer = SLEntry.getFile().getIncludeLoc();
331 else
332 Outer = SLEntry.getExpansion().getExpansionLocStart();
333 if (Outer.isInvalid())
334 return false;
335
336 std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
337 Length = 0;
338 Unit->findFileRegionDecls(File, Offset, Length, Decls);
339 }
340
341 assert(!Decls.empty())(static_cast <bool> (!Decls.empty()) ? void (0) : __assert_fail
("!Decls.empty()", "clang/tools/libclang/CIndex.cpp", 341, __extension__
__PRETTY_FUNCTION__))
;
342
343 bool VisitedAtLeastOnce = false;
344 DeclContext *CurDC = nullptr;
345 SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
346 for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
347 Decl *D = *DIt;
348 if (D->getSourceRange().isInvalid())
349 continue;
350
351 if (isInLexicalContext(D, CurDC))
352 continue;
353
354 CurDC = dyn_cast<DeclContext>(D);
355
356 if (TagDecl *TD = dyn_cast<TagDecl>(D))
357 if (!TD->isFreeStanding())
358 continue;
359
360 RangeComparisonResult CompRes =
361 RangeCompare(SM, D->getSourceRange(), Range);
362 if (CompRes == RangeBefore)
363 continue;
364 if (CompRes == RangeAfter)
365 break;
366
367 assert(CompRes == RangeOverlap)(static_cast <bool> (CompRes == RangeOverlap) ? void (0
) : __assert_fail ("CompRes == RangeOverlap", "clang/tools/libclang/CIndex.cpp"
, 367, __extension__ __PRETTY_FUNCTION__))
;
368 VisitedAtLeastOnce = true;
369
370 if (isa<ObjCContainerDecl>(D)) {
371 FileDI_current = &DIt;
372 FileDE_current = DE;
373 } else {
374 FileDI_current = nullptr;
375 }
376
377 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
378 return true; // visitation break.
379 }
380
381 if (VisitedAtLeastOnce)
382 return false;
383
384 // No Decls overlapped with the range. Move up the lexical context until there
385 // is a context that contains the range or we reach the translation unit
386 // level.
387 DeclContext *DC = DIt == Decls.begin()
388 ? (*DIt)->getLexicalDeclContext()
389 : (*(DIt - 1))->getLexicalDeclContext();
390
391 while (DC && !DC->isTranslationUnit()) {
392 Decl *D = cast<Decl>(DC);
393 SourceRange CurDeclRange = D->getSourceRange();
394 if (CurDeclRange.isInvalid())
395 break;
396
397 if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
398 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
399 return true; // visitation break.
400 }
401
402 DC = D->getLexicalDeclContext();
403 }
404
405 return false;
406}
407
408bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
409 if (!AU->getPreprocessor().getPreprocessingRecord())
410 return false;
411
412 PreprocessingRecord &PPRec = *AU->getPreprocessor().getPreprocessingRecord();
413 SourceManager &SM = AU->getSourceManager();
414
415 if (RegionOfInterest.isValid()) {
416 SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
417 SourceLocation B = MappedRange.getBegin();
418 SourceLocation E = MappedRange.getEnd();
419
420 if (AU->isInPreambleFileID(B)) {
421 if (SM.isLoadedSourceLocation(E))
422 return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec,
423 *this);
424
425 // Beginning of range lies in the preamble but it also extends beyond
426 // it into the main file. Split the range into 2 parts, one covering
427 // the preamble and another covering the main file. This allows subsequent
428 // calls to visitPreprocessedEntitiesInRange to accept a source range that
429 // lies in the same FileID, allowing it to skip preprocessed entities that
430 // do not come from the same FileID.
431 bool breaked = visitPreprocessedEntitiesInRange(
432 SourceRange(B, AU->getEndOfPreambleFileID()), PPRec, *this);
433 if (breaked)
434 return true;
435 return visitPreprocessedEntitiesInRange(
436 SourceRange(AU->getStartOfMainFileID(), E), PPRec, *this);
437 }
438
439 return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
440 }
441
442 bool OnlyLocalDecls = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
443
444 if (OnlyLocalDecls)
445 return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
446 PPRec);
447
448 return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
449}
450
451template <typename InputIterator>
452bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
453 InputIterator Last,
454 PreprocessingRecord &PPRec,
455 FileID FID) {
456 for (; First != Last; ++First) {
457 if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
458 continue;
459
460 PreprocessedEntity *PPE = *First;
461 if (!PPE)
462 continue;
463
464 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
465 if (Visit(MakeMacroExpansionCursor(ME, TU)))
466 return true;
467
468 continue;
469 }
470
471 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) {
472 if (Visit(MakeMacroDefinitionCursor(MD, TU)))
473 return true;
474
475 continue;
476 }
477
478 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
479 if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
480 return true;
481
482 continue;
483 }
484 }
485
486 return false;
487}
488
489/// Visit the children of the given cursor.
490///
491/// \returns true if the visitation should be aborted, false if it
492/// should continue.
493bool CursorVisitor::VisitChildren(CXCursor Cursor) {
494 if (clang_isReference(Cursor.kind) &&
495 Cursor.kind != CXCursor_CXXBaseSpecifier) {
496 // By definition, references have no children.
497 return false;
498 }
499
500 // Set the Parent field to Cursor, then back to its old value once we're
501 // done.
502 SetParentRAII SetParent(Parent, StmtParent, Cursor);
503
504 if (clang_isDeclaration(Cursor.kind)) {
505 Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
506 if (!D)
507 return false;
508
509 return VisitAttributes(D) || Visit(D);
510 }
511
512 if (clang_isStatement(Cursor.kind)) {
513 if (const Stmt *S = getCursorStmt(Cursor))
514 return Visit(S);
515
516 return false;
517 }
518
519 if (clang_isExpression(Cursor.kind)) {
520 if (const Expr *E = getCursorExpr(Cursor))
521 return Visit(E);
522
523 return false;
524 }
525
526 if (clang_isTranslationUnit(Cursor.kind)) {
527 CXTranslationUnit TU = getCursorTU(Cursor);
528 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
529
530 int VisitOrder[2] = {VisitPreprocessorLast, !VisitPreprocessorLast};
531 for (unsigned I = 0; I != 2; ++I) {
532 if (VisitOrder[I]) {
533 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
534 RegionOfInterest.isInvalid()) {
535 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
536 TLEnd = CXXUnit->top_level_end();
537 TL != TLEnd; ++TL) {
538 const Optional<bool> V = handleDeclForVisitation(*TL);
539 if (!V)
540 continue;
541 return V.value();
542 }
543 } else if (VisitDeclContext(
544 CXXUnit->getASTContext().getTranslationUnitDecl()))
545 return true;
546 continue;
547 }
548
549 // Walk the preprocessing record.
550 if (CXXUnit->getPreprocessor().getPreprocessingRecord())
551 visitPreprocessedEntitiesInRegion();
552 }
553
554 return false;
555 }
556
557 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
558 if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
559 if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
560 return Visit(BaseTSInfo->getTypeLoc());
561 }
562 }
563 }
564
565 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
566 const IBOutletCollectionAttr *A =
567 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
568 if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>())
569 return Visit(cxcursor::MakeCursorObjCClassRef(
570 ObjT->getInterface(),
571 A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU));
572 }
573
574 // If pointing inside a macro definition, check if the token is an identifier
575 // that was ever defined as a macro. In such a case, create a "pseudo" macro
576 // expansion cursor for that token.
577 SourceLocation BeginLoc = RegionOfInterest.getBegin();
578 if (Cursor.kind == CXCursor_MacroDefinition &&
579 BeginLoc == RegionOfInterest.getEnd()) {
580 SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc);
581 const MacroInfo *MI =
582 getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU);
583 if (MacroDefinitionRecord *MacroDef =
584 checkForMacroInMacroDefinition(MI, Loc, TU))
585 return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU));
586 }
587
588 // Nothing to visit at the moment.
589 return false;
590}
591
592bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
593 if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
594 if (Visit(TSInfo->getTypeLoc()))
595 return true;
596
597 if (Stmt *Body = B->getBody())
598 return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
599
600 return false;
601}
602
603Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
604 if (RegionOfInterest.isValid()) {
605 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
606 if (Range.isInvalid())
607 return None;
608
609 switch (CompareRegionOfInterest(Range)) {
610 case RangeBefore:
611 // This declaration comes before the region of interest; skip it.
612 return None;
613
614 case RangeAfter:
615 // This declaration comes after the region of interest; we're done.
616 return false;
617
618 case RangeOverlap:
619 // This declaration overlaps the region of interest; visit it.
620 break;
621 }
622 }
623 return true;
624}
625
626bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
627 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
628
629 // FIXME: Eventually remove. This part of a hack to support proper
630 // iteration over all Decls contained lexically within an ObjC container.
631 SaveAndRestore<DeclContext::decl_iterator *> DI_saved(DI_current, &I);
632 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
633
634 for (; I != E; ++I) {
635 Decl *D = *I;
636 if (D->getLexicalDeclContext() != DC)
637 continue;
638 // Filter out synthesized property accessor redeclarations.
639 if (isa<ObjCImplDecl>(DC))
640 if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
641 if (OMD->isSynthesizedAccessorStub())
642 continue;
643 const Optional<bool> V = handleDeclForVisitation(D);
644 if (!V)
645 continue;
646 return V.value();
647 }
648 return false;
649}
650
651Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) {
652 CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
653
654 // Ignore synthesized ivars here, otherwise if we have something like:
655 // @synthesize prop = _prop;
656 // and '_prop' is not declared, we will encounter a '_prop' ivar before
657 // encountering the 'prop' synthesize declaration and we will think that
658 // we passed the region-of-interest.
659 if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) {
660 if (ivarD->getSynthesize())
661 return None;
662 }
663
664 // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
665 // declarations is a mismatch with the compiler semantics.
666 if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
667 auto *ID = cast<ObjCInterfaceDecl>(D);
668 if (!ID->isThisDeclarationADefinition())
669 Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
670
671 } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
672 auto *PD = cast<ObjCProtocolDecl>(D);
673 if (!PD->isThisDeclarationADefinition())
674 Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
675 }
676
677 const Optional<bool> V = shouldVisitCursor(Cursor);
678 if (!V)
679 return None;
680 if (!V.value())
681 return false;
682 if (Visit(Cursor, true))
683 return true;
684 return None;
685}
686
687bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
688 llvm_unreachable("Translation units are visited directly by Visit()")::llvm::llvm_unreachable_internal("Translation units are visited directly by Visit()"
, "clang/tools/libclang/CIndex.cpp", 688)
;
689}
690
691bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
692 if (VisitTemplateParameters(D->getTemplateParameters()))
693 return true;
694
695 return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest));
696}
697
698bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
699 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
700 return Visit(TSInfo->getTypeLoc());
701
702 return false;
703}
704
705bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
706 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
707 return Visit(TSInfo->getTypeLoc());
708
709 return false;
710}
711
712bool CursorVisitor::VisitTagDecl(TagDecl *D) { return VisitDeclContext(D); }
713
714bool CursorVisitor::VisitClassTemplateSpecializationDecl(
715 ClassTemplateSpecializationDecl *D) {
716 bool ShouldVisitBody = false;
717 switch (D->getSpecializationKind()) {
718 case TSK_Undeclared:
719 case TSK_ImplicitInstantiation:
720 // Nothing to visit
721 return false;
722
723 case TSK_ExplicitInstantiationDeclaration:
724 case TSK_ExplicitInstantiationDefinition:
725 break;
726
727 case TSK_ExplicitSpecialization:
728 ShouldVisitBody = true;
729 break;
730 }
731
732 // Visit the template arguments used in the specialization.
733 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
734 TypeLoc TL = SpecType->getTypeLoc();
735 if (TemplateSpecializationTypeLoc TSTLoc =
736 TL.getAs<TemplateSpecializationTypeLoc>()) {
737 for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I)
738 if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I)))
739 return true;
740 }
741 }
742
743 return ShouldVisitBody && VisitCXXRecordDecl(D);
744}
745
746bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
747 ClassTemplatePartialSpecializationDecl *D) {
748 // FIXME: Visit the "outer" template parameter lists on the TagDecl
749 // before visiting these template parameters.
750 if (VisitTemplateParameters(D->getTemplateParameters()))
751 return true;
752
753 // Visit the partial specialization arguments.
754 const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten();
755 const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs();
756 for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I)
757 if (VisitTemplateArgumentLoc(TemplateArgs[I]))
758 return true;
759
760 return VisitCXXRecordDecl(D);
761}
762
763bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
764 if (const auto *TC = D->getTypeConstraint()) {
765 if (VisitTypeConstraint(*TC))
766 return true;
767 }
768
769 // Visit the default argument.
770 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
771 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
772 if (Visit(DefArg->getTypeLoc()))
773 return true;
774
775 return false;
776}
777
778bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
779 if (Expr *Init = D->getInitExpr())
780 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
781 return false;
782}
783
784bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
785 unsigned NumParamList = DD->getNumTemplateParameterLists();
786 for (unsigned i = 0; i < NumParamList; i++) {
787 TemplateParameterList *Params = DD->getTemplateParameterList(i);
788 if (VisitTemplateParameters(Params))
789 return true;
790 }
791
792 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
793 if (Visit(TSInfo->getTypeLoc()))
794 return true;
795
796 // Visit the nested-name-specifier, if present.
797 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
798 if (VisitNestedNameSpecifierLoc(QualifierLoc))
799 return true;
800
801 return false;
802}
803
804static bool HasTrailingReturnType(FunctionDecl *ND) {
805 const QualType Ty = ND->getType();
806 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
807 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT))
808 return FT->hasTrailingReturn();
809 }
810
811 return false;
812}
813
814/// Compare two base or member initializers based on their source order.
815static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X,
816 CXXCtorInitializer *const *Y) {
817 return (*X)->getSourceOrder() - (*Y)->getSourceOrder();
818}
819
820bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
821 unsigned NumParamList = ND->getNumTemplateParameterLists();
822 for (unsigned i = 0; i < NumParamList; i++) {
823 TemplateParameterList *Params = ND->getTemplateParameterList(i);
824 if (VisitTemplateParameters(Params))
825 return true;
826 }
827
828 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
829 // Visit the function declaration's syntactic components in the order
830 // written. This requires a bit of work.
831 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
832 FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
833 const bool HasTrailingRT = HasTrailingReturnType(ND);
834
835 // If we have a function declared directly (without the use of a typedef),
836 // visit just the return type. Otherwise, just visit the function's type
837 // now.
838 if ((FTL && !isa<CXXConversionDecl>(ND) && !HasTrailingRT &&
839 Visit(FTL.getReturnLoc())) ||
840 (!FTL && Visit(TL)))
841 return true;
842
843 // Visit the nested-name-specifier, if present.
844 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
845 if (VisitNestedNameSpecifierLoc(QualifierLoc))
846 return true;
847
848 // Visit the declaration name.
849 if (!isa<CXXDestructorDecl>(ND))
850 if (VisitDeclarationNameInfo(ND->getNameInfo()))
851 return true;
852
853 // FIXME: Visit explicitly-specified template arguments!
854
855 // Visit the function parameters, if we have a function type.
856 if (FTL && VisitFunctionTypeLoc(FTL, true))
857 return true;
858
859 // Visit the function's trailing return type.
860 if (FTL && HasTrailingRT && Visit(FTL.getReturnLoc()))
861 return true;
862
863 // FIXME: Attributes?
864 }
865
866 if (auto *E = ND->getTrailingRequiresClause()) {
867 if (Visit(E))
868 return true;
869 }
870
871 if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
872 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
873 // Find the initializers that were written in the source.
874 SmallVector<CXXCtorInitializer *, 4> WrittenInits;
875 for (auto *I : Constructor->inits()) {
876 if (!I->isWritten())
877 continue;
878
879 WrittenInits.push_back(I);
880 }
881
882 // Sort the initializers in source order
883 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
884 &CompareCXXCtorInitializers);
885
886 // Visit the initializers in source order
887 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
888 CXXCtorInitializer *Init = WrittenInits[I];
889 if (Init->isAnyMemberInitializer()) {
890 if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
891 Init->getMemberLocation(), TU)))
892 return true;
893 } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
894 if (Visit(TInfo->getTypeLoc()))
895 return true;
896 }
897
898 // Visit the initializer value.
899 if (Expr *Initializer = Init->getInit())
900 if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
901 return true;
902 }
903 }
904
905 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
906 return true;
907 }
908
909 return false;
910}
911
912bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
913 if (VisitDeclaratorDecl(D))
914 return true;
915
916 if (Expr *BitWidth = D->getBitWidth())
917 return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
918
919 if (Expr *Init = D->getInClassInitializer())
920 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
921
922 return false;
923}
924
925bool CursorVisitor::VisitVarDecl(VarDecl *D) {
926 if (VisitDeclaratorDecl(D))
927 return true;
928
929 if (Expr *Init = D->getInit())
930 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
931
932 return false;
933}
934
935bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
936 if (VisitDeclaratorDecl(D))
937 return true;
938
939 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
940 if (Expr *DefArg = D->getDefaultArgument())
941 return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
942
943 return false;
944}
945
946bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
947 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
948 // before visiting these template parameters.
949 if (VisitTemplateParameters(D->getTemplateParameters()))
950 return true;
951
952 auto *FD = D->getTemplatedDecl();
953 return VisitAttributes(FD) || VisitFunctionDecl(FD);
954}
955
956bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
957 // FIXME: Visit the "outer" template parameter lists on the TagDecl
958 // before visiting these template parameters.
959 if (VisitTemplateParameters(D->getTemplateParameters()))
960 return true;
961
962 auto *CD = D->getTemplatedDecl();
963 return VisitAttributes(CD) || VisitCXXRecordDecl(CD);
964}
965
966bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
967 if (VisitTemplateParameters(D->getTemplateParameters()))
968 return true;
969
970 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
971 VisitTemplateArgumentLoc(D->getDefaultArgument()))
972 return true;
973
974 return false;
975}
976
977bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
978 // Visit the bound, if it's explicit.
979 if (D->hasExplicitBound()) {
980 if (auto TInfo = D->getTypeSourceInfo()) {
981 if (Visit(TInfo->getTypeLoc()))
982 return true;
983 }
984 }
985
986 return false;
987}
988
989bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
990 if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo())
991 if (Visit(TSInfo->getTypeLoc()))
992 return true;
993
994 for (const auto *P : ND->parameters()) {
995 if (Visit(MakeCXCursor(P, TU, RegionOfInterest)))
996 return true;
997 }
998
999 return ND->isThisDeclarationADefinition() &&
1000 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest));
1001}
1002
1003template <typename DeclIt>
1004static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
1005 SourceManager &SM, SourceLocation EndLoc,
1006 SmallVectorImpl<Decl *> &Decls) {
1007 DeclIt next = *DI_current;
1008 while (++next != DE_current) {
1009 Decl *D_next = *next;
1010 if (!D_next)
1011 break;
1012 SourceLocation L = D_next->getBeginLoc();
1013 if (!L.isValid())
1014 break;
1015 if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
1016 *DI_current = next;
1017 Decls.push_back(D_next);
1018 continue;
1019 }
1020 break;
1021 }
1022}
1023
1024bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
1025 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
1026 // an @implementation can lexically contain Decls that are not properly
1027 // nested in the AST. When we identify such cases, we need to retrofit
1028 // this nesting here.
1029 if (!DI_current && !FileDI_current)
1030 return VisitDeclContext(D);
1031
1032 // Scan the Decls that immediately come after the container
1033 // in the current DeclContext. If any fall within the
1034 // container's lexical region, stash them into a vector
1035 // for later processing.
1036 SmallVector<Decl *, 24> DeclsInContainer;
1037 SourceLocation EndLoc = D->getSourceRange().getEnd();
1038 SourceManager &SM = AU->getSourceManager();
1039 if (EndLoc.isValid()) {
1040 if (DI_current) {
1041 addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
1042 DeclsInContainer);
1043 } else {
1044 addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
1045 DeclsInContainer);
1046 }
1047 }
1048
1049 // The common case.
1050 if (DeclsInContainer.empty())
1051 return VisitDeclContext(D);
1052
1053 // Get all the Decls in the DeclContext, and sort them with the
1054 // additional ones we've collected. Then visit them.
1055 for (auto *SubDecl : D->decls()) {
1056 if (!SubDecl || SubDecl->getLexicalDeclContext() != D ||
1057 SubDecl->getBeginLoc().isInvalid())
1058 continue;
1059 DeclsInContainer.push_back(SubDecl);
1060 }
1061
1062 // Now sort the Decls so that they appear in lexical order.
1063 llvm::sort(DeclsInContainer, [&SM](Decl *A, Decl *B) {
1064 SourceLocation L_A = A->getBeginLoc();
1065 SourceLocation L_B = B->getBeginLoc();
1066 return L_A != L_B
1067 ? SM.isBeforeInTranslationUnit(L_A, L_B)
1068 : SM.isBeforeInTranslationUnit(A->getEndLoc(), B->getEndLoc());
1069 });
1070
1071 // Now visit the decls.
1072 for (SmallVectorImpl<Decl *>::iterator I = DeclsInContainer.begin(),
1073 E = DeclsInContainer.end();
1074 I != E; ++I) {
1075 CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
1076 const Optional<bool> &V = shouldVisitCursor(Cursor);
1077 if (!V)
1078 continue;
1079 if (!V.value())
1080 return false;
1081 if (Visit(Cursor, true))
1082 return true;
1083 }
1084 return false;
1085}
1086
1087bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
1088 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
1089 TU)))
1090 return true;
1091
1092 if (VisitObjCTypeParamList(ND->getTypeParamList()))
1093 return true;
1094
1095 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
1096 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
1097 E = ND->protocol_end();
1098 I != E; ++I, ++PL)
1099 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1100 return true;
1101
1102 return VisitObjCContainerDecl(ND);
1103}
1104
1105bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1106 if (!PID->isThisDeclarationADefinition())
1107 return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
1108
1109 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
1110 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
1111 E = PID->protocol_end();
1112 I != E; ++I, ++PL)
1113 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1114 return true;
1115
1116 return VisitObjCContainerDecl(PID);
1117}
1118
1119bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
1120 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
1121 return true;
1122
1123 // FIXME: This implements a workaround with @property declarations also being
1124 // installed in the DeclContext for the @interface. Eventually this code
1125 // should be removed.
1126 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
1127 if (!CDecl || !CDecl->IsClassExtension())
1128 return false;
1129
1130 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
1131 if (!ID)
1132 return false;
1133
1134 IdentifierInfo *PropertyId = PD->getIdentifier();
1135 ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
1136 cast<DeclContext>(ID), PropertyId, PD->getQueryKind());
1137
1138 if (!prevDecl)
1139 return false;
1140
1141 // Visit synthesized methods since they will be skipped when visiting
1142 // the @interface.
1143 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1144 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1145 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1146 return true;
1147
1148 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1149 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1150 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1151 return true;
1152
1153 return false;
1154}
1155
1156bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) {
1157 if (!typeParamList)
1158 return false;
1159
1160 for (auto *typeParam : *typeParamList) {
1161 // Visit the type parameter.
1162 if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest)))
1163 return true;
1164 }
1165
1166 return false;
1167}
1168
1169bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1170 if (!D->isThisDeclarationADefinition()) {
1171 // Forward declaration is treated like a reference.
1172 return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
1173 }
1174
1175 // Objective-C type parameters.
1176 if (VisitObjCTypeParamList(D->getTypeParamListAsWritten()))
1177 return true;
1178
1179 // Issue callbacks for super class.
1180 if (D->getSuperClass() && Visit(MakeCursorObjCSuperClassRef(
1181 D->getSuperClass(), D->getSuperClassLoc(), TU)))
1182 return true;
1183
1184 if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo())
1185 if (Visit(SuperClassTInfo->getTypeLoc()))
1186 return true;
1187
1188 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1189 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1190 E = D->protocol_end();
1191 I != E; ++I, ++PL)
1192 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1193 return true;
1194
1195 return VisitObjCContainerDecl(D);
1196}
1197
1198bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1199 return VisitObjCContainerDecl(D);
1200}
1201
1202bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1203 // 'ID' could be null when dealing with invalid code.
1204 if (ObjCInterfaceDecl *ID = D->getClassInterface())
1205 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1206 return true;
1207
1208 return VisitObjCImplDecl(D);
1209}
1210
1211bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1212#if 0
1213 // Issue callbacks for super class.
1214 // FIXME: No source location information!
1215 if (D->getSuperClass() &&
1216 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1217 D->getSuperClassLoc(),
1218 TU)))
1219 return true;
1220#endif
1221
1222 return VisitObjCImplDecl(D);
1223}
1224
1225bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1226 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1227 if (PD->isIvarNameSpecified())
1228 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1229
1230 return false;
1231}
1232
1233bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1234 return VisitDeclContext(D);
1235}
1236
1237bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1238 // Visit nested-name-specifier.
1239 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1240 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1241 return true;
1242
1243 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1244 D->getTargetNameLoc(), TU));
1245}
1246
1247bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1248 // Visit nested-name-specifier.
1249 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1250 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1251 return true;
1252 }
1253
1254 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1255 return true;
1256
1257 return VisitDeclarationNameInfo(D->getNameInfo());
1258}
1259
1260bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1261 // Visit nested-name-specifier.
1262 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1263 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1264 return true;
1265
1266 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1267 D->getIdentLocation(), TU));
1268}
1269
1270bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1271 // Visit nested-name-specifier.
1272 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1273 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1274 return true;
1275 }
1276
1277 return VisitDeclarationNameInfo(D->getNameInfo());
1278}
1279
1280bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1281 UnresolvedUsingTypenameDecl *D) {
1282 // Visit nested-name-specifier.
1283 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1284 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1285 return true;
1286
1287 return false;
1288}
1289
1290bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) {
1291 if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest)))
1292 return true;
1293 if (StringLiteral *Message = D->getMessage())
1294 if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest)))
1295 return true;
1296 return false;
1297}
1298
1299bool CursorVisitor::VisitFriendDecl(FriendDecl *D) {
1300 if (NamedDecl *FriendD = D->getFriendDecl()) {
1301 if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest)))
1302 return true;
1303 } else if (TypeSourceInfo *TI = D->getFriendType()) {
1304 if (Visit(TI->getTypeLoc()))
1305 return true;
1306 }
1307 return false;
1308}
1309
1310bool CursorVisitor::VisitDecompositionDecl(DecompositionDecl *D) {
1311 for (auto *B : D->bindings()) {
1312 if (Visit(MakeCXCursor(B, TU, RegionOfInterest)))
1313 return true;
1314 }
1315 return VisitVarDecl(D);
1316}
1317
1318bool CursorVisitor::VisitConceptDecl(ConceptDecl *D) {
1319 if (VisitTemplateParameters(D->getTemplateParameters()))
1320 return true;
1321
1322 if (auto *E = D->getConstraintExpr()) {
1323 if (Visit(MakeCXCursor(E, D, TU, RegionOfInterest)))
1324 return true;
1325 }
1326 return false;
1327}
1328
1329bool CursorVisitor::VisitTypeConstraint(const TypeConstraint &TC) {
1330 if (TC.getNestedNameSpecifierLoc()) {
1331 if (VisitNestedNameSpecifierLoc(TC.getNestedNameSpecifierLoc()))
1332 return true;
1333 }
1334 if (TC.getNamedConcept()) {
1335 if (Visit(MakeCursorTemplateRef(TC.getNamedConcept(),
1336 TC.getConceptNameLoc(), TU)))
1337 return true;
1338 }
1339 if (auto Args = TC.getTemplateArgsAsWritten()) {
1340 for (const auto &Arg : Args->arguments()) {
1341 if (VisitTemplateArgumentLoc(Arg))
1342 return true;
1343 }
1344 }
1345 return false;
1346}
1347
1348bool CursorVisitor::VisitConceptRequirement(const concepts::Requirement &R) {
1349 using namespace concepts;
1350 switch (R.getKind()) {
1351 case Requirement::RK_Type: {
1352 const TypeRequirement &TR = cast<TypeRequirement>(R);
1353 if (!TR.isSubstitutionFailure()) {
1354 if (Visit(TR.getType()->getTypeLoc()))
1355 return true;
1356 }
1357 break;
1358 }
1359 case Requirement::RK_Simple:
1360 case Requirement::RK_Compound: {
1361 const ExprRequirement &ER = cast<ExprRequirement>(R);
1362 if (!ER.isExprSubstitutionFailure()) {
1363 if (Visit(ER.getExpr()))
1364 return true;
1365 }
1366 if (ER.getKind() == Requirement::RK_Compound) {
1367 const auto &RTR = ER.getReturnTypeRequirement();
1368 if (RTR.isTypeConstraint()) {
1369 if (const auto *Cons = RTR.getTypeConstraint())
1370 VisitTypeConstraint(*Cons);
1371 }
1372 }
1373 break;
1374 }
1375 case Requirement::RK_Nested: {
1376 const NestedRequirement &NR = cast<NestedRequirement>(R);
1377 if (!NR.isSubstitutionFailure()) {
1378 if (Visit(NR.getConstraintExpr()))
1379 return true;
1380 }
1381 break;
1382 }
1383 }
1384 return false;
1385}
1386
1387bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1388 switch (Name.getName().getNameKind()) {
1389 case clang::DeclarationName::Identifier:
1390 case clang::DeclarationName::CXXLiteralOperatorName:
1391 case clang::DeclarationName::CXXDeductionGuideName:
1392 case clang::DeclarationName::CXXOperatorName:
1393 case clang::DeclarationName::CXXUsingDirective:
1394 return false;
1395
1396 case clang::DeclarationName::CXXConstructorName:
1397 case clang::DeclarationName::CXXDestructorName:
1398 case clang::DeclarationName::CXXConversionFunctionName:
1399 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1400 return Visit(TSInfo->getTypeLoc());
1401 return false;
1402
1403 case clang::DeclarationName::ObjCZeroArgSelector:
1404 case clang::DeclarationName::ObjCOneArgSelector:
1405 case clang::DeclarationName::ObjCMultiArgSelector:
1406 // FIXME: Per-identifier location info?
1407 return false;
1408 }
1409
1410 llvm_unreachable("Invalid DeclarationName::Kind!")::llvm::llvm_unreachable_internal("Invalid DeclarationName::Kind!"
, "clang/tools/libclang/CIndex.cpp", 1410)
;
1411}
1412
1413bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1414 SourceRange Range) {
1415 // FIXME: This whole routine is a hack to work around the lack of proper
1416 // source information in nested-name-specifiers (PR5791). Since we do have
1417 // a beginning source location, we can visit the first component of the
1418 // nested-name-specifier, if it's a single-token component.
1419 if (!NNS)
1420 return false;
1421
1422 // Get the first component in the nested-name-specifier.
1423 while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1424 NNS = Prefix;
1425
1426 switch (NNS->getKind()) {
1427 case NestedNameSpecifier::Namespace:
1428 return Visit(
1429 MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), TU));
1430
1431 case NestedNameSpecifier::NamespaceAlias:
1432 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1433 Range.getBegin(), TU));
1434
1435 case NestedNameSpecifier::TypeSpec: {
1436 // If the type has a form where we know that the beginning of the source
1437 // range matches up with a reference cursor. Visit the appropriate reference
1438 // cursor.
1439 const Type *T = NNS->getAsType();
1440 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1441 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1442 if (const TagType *Tag = dyn_cast<TagType>(T))
1443 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1444 if (const TemplateSpecializationType *TST =
1445 dyn_cast<TemplateSpecializationType>(T))
1446 return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1447 break;
1448 }
1449
1450 case NestedNameSpecifier::TypeSpecWithTemplate:
1451 case NestedNameSpecifier::Global:
1452 case NestedNameSpecifier::Identifier:
1453 case NestedNameSpecifier::Super:
1454 break;
1455 }
1456
1457 return false;
1458}
1459
1460bool CursorVisitor::VisitNestedNameSpecifierLoc(
1461 NestedNameSpecifierLoc Qualifier) {
1462 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1463 for (; Qualifier; Qualifier = Qualifier.getPrefix())
1464 Qualifiers.push_back(Qualifier);
1465
1466 while (!Qualifiers.empty()) {
1467 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1468 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1469 switch (NNS->getKind()) {
1470 case NestedNameSpecifier::Namespace:
1471 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
1472 Q.getLocalBeginLoc(), TU)))
1473 return true;
1474
1475 break;
1476
1477 case NestedNameSpecifier::NamespaceAlias:
1478 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1479 Q.getLocalBeginLoc(), TU)))
1480 return true;
1481
1482 break;
1483
1484 case NestedNameSpecifier::TypeSpec:
1485 case NestedNameSpecifier::TypeSpecWithTemplate:
1486 if (Visit(Q.getTypeLoc()))
1487 return true;
1488
1489 break;
1490
1491 case NestedNameSpecifier::Global:
1492 case NestedNameSpecifier::Identifier:
1493 case NestedNameSpecifier::Super:
1494 break;
1495 }
1496 }
1497
1498 return false;
1499}
1500
1501bool CursorVisitor::VisitTemplateParameters(
1502 const TemplateParameterList *Params) {
1503 if (!Params)
1504 return false;
1505
1506 for (TemplateParameterList::const_iterator P = Params->begin(),
1507 PEnd = Params->end();
1508 P != PEnd; ++P) {
1509 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
1510 return true;
1511 }
1512
1513 if (const auto *E = Params->getRequiresClause()) {
1514 if (Visit(MakeCXCursor(E, nullptr, TU, RegionOfInterest)))
1515 return true;
1516 }
1517
1518 return false;
1519}
1520
1521bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1522 switch (Name.getKind()) {
1523 case TemplateName::Template:
1524 case TemplateName::UsingTemplate:
1525 case TemplateName::QualifiedTemplate: // FIXME: Visit nested-name-specifier.
1526 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1527
1528 case TemplateName::OverloadedTemplate:
1529 // Visit the overloaded template set.
1530 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1531 return true;
1532
1533 return false;
1534
1535 case TemplateName::AssumedTemplate:
1536 // FIXME: Visit DeclarationName?
1537 return false;
1538
1539 case TemplateName::DependentTemplate:
1540 // FIXME: Visit nested-name-specifier.
1541 return false;
1542
1543 case TemplateName::SubstTemplateTemplateParm:
1544 return Visit(MakeCursorTemplateRef(
1545 Name.getAsSubstTemplateTemplateParm()->getParameter(), Loc, TU));
1546
1547 case TemplateName::SubstTemplateTemplateParmPack:
1548 return Visit(MakeCursorTemplateRef(
1549 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), Loc,
1550 TU));
1551 }
1552
1553 llvm_unreachable("Invalid TemplateName::Kind!")::llvm::llvm_unreachable_internal("Invalid TemplateName::Kind!"
, "clang/tools/libclang/CIndex.cpp", 1553)
;
1554}
1555
1556bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1557 switch (TAL.getArgument().getKind()) {
1558 case TemplateArgument::Null:
1559 case TemplateArgument::Integral:
1560 case TemplateArgument::Pack:
1561 return false;
1562
1563 case TemplateArgument::Type:
1564 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1565 return Visit(TSInfo->getTypeLoc());
1566 return false;
1567
1568 case TemplateArgument::Declaration:
1569 if (Expr *E = TAL.getSourceDeclExpression())
1570 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1571 return false;
1572
1573 case TemplateArgument::NullPtr:
1574 if (Expr *E = TAL.getSourceNullPtrExpression())
1575 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1576 return false;
1577
1578 case TemplateArgument::Expression:
1579 if (Expr *E = TAL.getSourceExpression())
1580 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1581 return false;
1582
1583 case TemplateArgument::Template:
1584 case TemplateArgument::TemplateExpansion:
1585 if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1586 return true;
1587
1588 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
1589 TAL.getTemplateNameLoc());
1590 }
1591
1592 llvm_unreachable("Invalid TemplateArgument::Kind!")::llvm::llvm_unreachable_internal("Invalid TemplateArgument::Kind!"
, "clang/tools/libclang/CIndex.cpp", 1592)
;
1593}
1594
1595bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1596 return VisitDeclContext(D);
1597}
1598
1599bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1600 return Visit(TL.getUnqualifiedLoc());
1601}
1602
1603bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1604 ASTContext &Context = AU->getASTContext();
1605
1606 // Some builtin types (such as Objective-C's "id", "sel", and
1607 // "Class") have associated declarations. Create cursors for those.
1608 QualType VisitType;
1609 switch (TL.getTypePtr()->getKind()) {
1610
1611 case BuiltinType::Void:
1612 case BuiltinType::NullPtr:
1613 case BuiltinType::Dependent:
1614#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1615 case BuiltinType::Id:
1616#include "clang/Basic/OpenCLImageTypes.def"
1617#define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) case BuiltinType::Id:
1618#include "clang/Basic/OpenCLExtensionTypes.def"
1619 case BuiltinType::OCLSampler:
1620 case BuiltinType::OCLEvent:
1621 case BuiltinType::OCLClkEvent:
1622 case BuiltinType::OCLQueue:
1623 case BuiltinType::OCLReserveID:
1624#define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1625#include "clang/Basic/AArch64SVEACLETypes.def"
1626#define PPC_VECTOR_TYPE(Name, Id, Size) case BuiltinType::Id:
1627#include "clang/Basic/PPCTypes.def"
1628#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1629#include "clang/Basic/RISCVVTypes.def"
1630#define BUILTIN_TYPE(Id, SingletonId)
1631#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1632#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1633#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1634#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1635#include "clang/AST/BuiltinTypes.def"
1636 break;
1637
1638 case BuiltinType::ObjCId:
1639 VisitType = Context.getObjCIdType();
1640 break;
1641
1642 case BuiltinType::ObjCClass:
1643 VisitType = Context.getObjCClassType();
1644 break;
1645
1646 case BuiltinType::ObjCSel:
1647 VisitType = Context.getObjCSelType();
1648 break;
1649 }
1650
1651 if (!VisitType.isNull()) {
1652 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1653 return Visit(
1654 MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), TU));
1655 }
1656
1657 return false;
1658}
1659
1660bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1661 return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
1662}
1663
1664bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1665 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1666}
1667
1668bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1669 if (TL.isDefinition())
1670 return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
1671
1672 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1673}
1674
1675bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1676 if (const auto *TC = TL.getDecl()->getTypeConstraint()) {
1677 if (VisitTypeConstraint(*TC))
1678 return true;
1679 }
1680
1681 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1682}
1683
1684bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1685 return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU));
1686}
1687
1688bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
1689 if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getBeginLoc(), TU)))
1690 return true;
1691 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1692 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1693 TU)))
1694 return true;
1695 }
1696
1697 return false;
1698}
1699
1700bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1701 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1702 return true;
1703
1704 for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) {
1705 if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc()))
1706 return true;
1707 }
1708
1709 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1710 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1711 TU)))
1712 return true;
1713 }
1714
1715 return false;
1716}
1717
1718bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1719 return Visit(TL.getPointeeLoc());
1720}
1721
1722bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1723 return Visit(TL.getInnerLoc());
1724}
1725
1726bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
1727 return Visit(TL.getInnerLoc());
1728}
1729
1730bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1731 return Visit(TL.getPointeeLoc());
1732}
1733
1734bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1735 return Visit(TL.getPointeeLoc());
1736}
1737
1738bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1739 return Visit(TL.getPointeeLoc());
1740}
1741
1742bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1743 return Visit(TL.getPointeeLoc());
1744}
1745
1746bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1747 return Visit(TL.getPointeeLoc());
1748}
1749
1750bool CursorVisitor::VisitUsingTypeLoc(UsingTypeLoc TL) { return false; }
1751
1752bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1753 return Visit(TL.getModifiedLoc());
1754}
1755
1756bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
1757 return Visit(TL.getWrappedLoc());
1758}
1759
1760bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1761 bool SkipResultType) {
1762 if (!SkipResultType && Visit(TL.getReturnLoc()))
1763 return true;
1764
1765 for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I)
1766 if (Decl *D = TL.getParam(I))
1767 if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
1768 return true;
1769
1770 return false;
1771}
1772
1773bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1774 if (Visit(TL.getElementLoc()))
1775 return true;
1776
1777 if (Expr *Size = TL.getSizeExpr())
1778 return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
1779
1780 return false;
1781}
1782
1783bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
1784 return Visit(TL.getOriginalLoc());
1785}
1786
1787bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
1788 return Visit(TL.getOriginalLoc());
1789}
1790
1791bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
1792 DeducedTemplateSpecializationTypeLoc TL) {
1793 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1794 TL.getTemplateNameLoc()))
1795 return true;
1796
1797 return false;
1798}
1799
1800bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1801 TemplateSpecializationTypeLoc TL) {
1802 // Visit the template name.
1803 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1804 TL.getTemplateNameLoc()))
1805 return true;
1806
1807 // Visit the template arguments.
1808 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1809 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1810 return true;
1811
1812 return false;
1813}
1814
1815bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1816 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1817}
1818
1819bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1820 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1821 return Visit(TSInfo->getTypeLoc());
1822
1823 return false;
1824}
1825
1826bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1827 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1828 return Visit(TSInfo->getTypeLoc());
1829
1830 return false;
1831}
1832
1833bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1834 return VisitNestedNameSpecifierLoc(TL.getQualifierLoc());
1835}
1836
1837bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1838 DependentTemplateSpecializationTypeLoc TL) {
1839 // Visit the nested-name-specifier, if there is one.
1840 if (TL.getQualifierLoc() && VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1841 return true;
1842
1843 // Visit the template arguments.
1844 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1845 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1846 return true;
1847
1848 return false;
1849}
1850
1851bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1852 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1853 return true;
1854
1855 return Visit(TL.getNamedTypeLoc());
1856}
1857
1858bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1859 return Visit(TL.getPatternLoc());
1860}
1861
1862bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1863 if (Expr *E = TL.getUnderlyingExpr())
1864 return Visit(MakeCXCursor(E, StmtParent, TU));
1865
1866 return false;
1867}
1868
1869bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1870 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1871}
1872
1873bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1874 return Visit(TL.getValueLoc());
1875}
1876
1877bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) {
1878 return Visit(TL.getValueLoc());
1879}
1880
1881#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT)bool CursorVisitor::VisitCLASSTypeLoc(CLASSTypeLoc TL) { return
VisitPARENTLoc(TL); }
\
1882 bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
1883 return Visit##PARENT##Loc(TL); \
1884 }
1885
1886DEFAULT_TYPELOC_IMPL(Complex, Type)bool CursorVisitor::VisitComplexTypeLoc(ComplexTypeLoc TL) { return
VisitTypeLoc(TL); }
1887DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)bool CursorVisitor::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc
TL) { return VisitArrayTypeLoc(TL); }
1888DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)bool CursorVisitor::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc
TL) { return VisitArrayTypeLoc(TL); }
1889DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)bool CursorVisitor::VisitVariableArrayTypeLoc(VariableArrayTypeLoc
TL) { return VisitArrayTypeLoc(TL); }
1890DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)bool CursorVisitor::VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc
TL) { return VisitArrayTypeLoc(TL); }
1891DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type)bool CursorVisitor::VisitDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc
TL) { return VisitTypeLoc(TL); }
1892DEFAULT_TYPELOC_IMPL(DependentVector, Type)bool CursorVisitor::VisitDependentVectorTypeLoc(DependentVectorTypeLoc
TL) { return VisitTypeLoc(TL); }
1893DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)bool CursorVisitor::VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc
TL) { return VisitTypeLoc(TL); }
1894DEFAULT_TYPELOC_IMPL(Vector, Type)bool CursorVisitor::VisitVectorTypeLoc(VectorTypeLoc TL) { return
VisitTypeLoc(TL); }
1895DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)bool CursorVisitor::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL
) { return VisitVectorTypeLoc(TL); }
1896DEFAULT_TYPELOC_IMPL(ConstantMatrix, MatrixType)bool CursorVisitor::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc
TL) { return VisitMatrixTypeLoc(TL); }
1897DEFAULT_TYPELOC_IMPL(DependentSizedMatrix, MatrixType)bool CursorVisitor::VisitDependentSizedMatrixTypeLoc(DependentSizedMatrixTypeLoc
TL) { return VisitMatrixTypeLoc(TL); }
1898DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)bool CursorVisitor::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc
TL) { return VisitFunctionTypeLoc(TL); }
1899DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)bool CursorVisitor::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc
TL) { return VisitFunctionTypeLoc(TL); }
1900DEFAULT_TYPELOC_IMPL(Record, TagType)bool CursorVisitor::VisitRecordTypeLoc(RecordTypeLoc TL) { return
VisitTagTypeLoc(TL); }
1901DEFAULT_TYPELOC_IMPL(Enum, TagType)bool CursorVisitor::VisitEnumTypeLoc(EnumTypeLoc TL) { return
VisitTagTypeLoc(TL); }
1902DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)bool CursorVisitor::VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc
TL) { return VisitTypeLoc(TL); }
1903DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)bool CursorVisitor::VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc
TL) { return VisitTypeLoc(TL); }
1904DEFAULT_TYPELOC_IMPL(Auto, Type)bool CursorVisitor::VisitAutoTypeLoc(AutoTypeLoc TL) { return
VisitTypeLoc(TL); }
1905DEFAULT_TYPELOC_IMPL(BitInt, Type)bool CursorVisitor::VisitBitIntTypeLoc(BitIntTypeLoc TL) { return
VisitTypeLoc(TL); }
1906DEFAULT_TYPELOC_IMPL(DependentBitInt, Type)bool CursorVisitor::VisitDependentBitIntTypeLoc(DependentBitIntTypeLoc
TL) { return VisitTypeLoc(TL); }
1907
1908bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1909 // Visit the nested-name-specifier, if present.
1910 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1911 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1912 return true;
1913
1914 if (D->isCompleteDefinition()) {
1915 for (const auto &I : D->bases()) {
1916 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU)))
1917 return true;
1918 }
1919 }
1920
1921 return VisitTagDecl(D);
1922}
1923
1924bool CursorVisitor::VisitAttributes(Decl *D) {
1925 for (const auto *I : D->attrs())
1926 if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes ||
1927 !I->isImplicit()) &&
1928 Visit(MakeCXCursor(I, D, TU)))
1929 return true;
1930
1931 return false;
1932}
1933
1934//===----------------------------------------------------------------------===//
1935// Data-recursive visitor methods.
1936//===----------------------------------------------------------------------===//
1937
1938namespace {
1939#define DEF_JOB(NAME, DATA, KIND) \
1940 class NAME : public VisitorJob { \
1941 public: \
1942 NAME(const DATA *d, CXCursor parent) \
1943 : VisitorJob(parent, VisitorJob::KIND, d) {} \
1944 static bool classof(const VisitorJob *VJ) { \
1945 return VJ->getKind() == KIND; \
1946 } \
1947 const DATA *get() const { return static_cast<const DATA *>(data[0]); } \
1948 };
1949
1950DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1951DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1952DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1953DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1954DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1955DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1956DEF_JOB(ConceptSpecializationExprVisit, ConceptSpecializationExpr,
1957 ConceptSpecializationExprVisitKind)
1958DEF_JOB(RequiresExprVisit, RequiresExpr, RequiresExprVisitKind)
1959DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
1960#undef DEF_JOB
1961
1962class ExplicitTemplateArgsVisit : public VisitorJob {
1963public:
1964 ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin,
1965 const TemplateArgumentLoc *End, CXCursor parent)
1966 : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin,
1967 End) {}
1968 static bool classof(const VisitorJob *VJ) {
1969 return VJ->getKind() == ExplicitTemplateArgsVisitKind;
1970 }
1971 const TemplateArgumentLoc *begin() const {
1972 return static_cast<const TemplateArgumentLoc *>(data[0]);
1973 }
1974 const TemplateArgumentLoc *end() {
1975 return static_cast<const TemplateArgumentLoc *>(data[1]);
1976 }
1977};
1978class DeclVisit : public VisitorJob {
1979public:
1980 DeclVisit(const Decl *D, CXCursor parent, bool isFirst)
1981 : VisitorJob(parent, VisitorJob::DeclVisitKind, D,
1982 isFirst ? (void *)1 : (void *)nullptr) {}
1983 static bool classof(const VisitorJob *VJ) {
1984 return VJ->getKind() == DeclVisitKind;
1985 }
1986 const Decl *get() const { return static_cast<const Decl *>(data[0]); }
1987 bool isFirst() const { return data[1] != nullptr; }
1988};
1989class TypeLocVisit : public VisitorJob {
1990public:
1991 TypeLocVisit(TypeLoc tl, CXCursor parent)
1992 : VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1993 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1994
1995 static bool classof(const VisitorJob *VJ) {
1996 return VJ->getKind() == TypeLocVisitKind;
1997 }
1998
1999 TypeLoc get() const {
2000 QualType T = QualType::getFromOpaquePtr(data[0]);
2001 return TypeLoc(T, const_cast<void *>(data[1]));
2002 }
2003};
2004
2005class LabelRefVisit : public VisitorJob {
2006public:
2007 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
2008 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
2009 labelLoc.getPtrEncoding()) {}
2010
2011 static bool classof(const VisitorJob *VJ) {
2012 return VJ->getKind() == VisitorJob::LabelRefVisitKind;
2013 }
2014 const LabelDecl *get() const {
2015 return static_cast<const LabelDecl *>(data[0]);
2016 }
2017 SourceLocation getLoc() const {
2018 return SourceLocation::getFromPtrEncoding(data[1]);
2019 }
2020};
2021
2022class NestedNameSpecifierLocVisit : public VisitorJob {
2023public:
2024 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
2025 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
2026 Qualifier.getNestedNameSpecifier(),
2027 Qualifier.getOpaqueData()) {}
2028
2029 static bool classof(const VisitorJob *VJ) {
2030 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
2031 }
2032
2033 NestedNameSpecifierLoc get() const {
2034 return NestedNameSpecifierLoc(
2035 const_cast<NestedNameSpecifier *>(
2036 static_cast<const NestedNameSpecifier *>(data[0])),
2037 const_cast<void *>(data[1]));
2038 }
2039};
2040
2041class DeclarationNameInfoVisit : public VisitorJob {
2042public:
2043 DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
2044 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
2045 static bool classof(const VisitorJob *VJ) {
2046 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
2047 }
2048 DeclarationNameInfo get() const {
2049 const Stmt *S = static_cast<const Stmt *>(data[0]);
2050 switch (S->getStmtClass()) {
2051 default:
2052 llvm_unreachable("Unhandled Stmt")::llvm::llvm_unreachable_internal("Unhandled Stmt", "clang/tools/libclang/CIndex.cpp"
, 2052)
;
2053 case clang::Stmt::MSDependentExistsStmtClass:
2054 return cast<MSDependentExistsStmt>(S)->getNameInfo();
2055 case Stmt::CXXDependentScopeMemberExprClass:
2056 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
2057 case Stmt::DependentScopeDeclRefExprClass:
2058 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
2059 case Stmt::OMPCriticalDirectiveClass:
2060 return cast<OMPCriticalDirective>(S)->getDirectiveName();
2061 }
2062 }
2063};
2064class MemberRefVisit : public VisitorJob {
2065public:
2066 MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
2067 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
2068 L.getPtrEncoding()) {}
2069 static bool classof(const VisitorJob *VJ) {
2070 return VJ->getKind() == VisitorJob::MemberRefVisitKind;
2071 }
2072 const FieldDecl *get() const {
2073 return static_cast<const FieldDecl *>(data[0]);
2074 }
2075 SourceLocation getLoc() const {
2076 return SourceLocation::getFromRawEncoding(
2077 (SourceLocation::UIntTy)(uintptr_t)data[1]);
2078 }
2079};
2080class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
2081 friend class OMPClauseEnqueue;
2082 VisitorWorkList &WL;
2083 CXCursor Parent;
2084
2085public:
2086 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
2087 : WL(wl), Parent(parent) {}
2088
2089 void VisitAddrLabelExpr(const AddrLabelExpr *E);
2090 void VisitBlockExpr(const BlockExpr *B);
2091 void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
2092 void VisitCompoundStmt(const CompoundStmt *S);
2093 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */
2094 }
2095 void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
2096 void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
2097 void VisitCXXNewExpr(const CXXNewExpr *E);
2098 void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
2099 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
2100 void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
2101 void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
2102 void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2103 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
2104 void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
2105 void VisitCXXCatchStmt(const CXXCatchStmt *S);
2106 void VisitCXXForRangeStmt(const CXXForRangeStmt *S);
2107 void VisitDeclRefExpr(const DeclRefExpr *D);
2108 void VisitDeclStmt(const DeclStmt *S);
2109 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
2110 void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
2111 void VisitExplicitCastExpr(const ExplicitCastExpr *E);
2112 void VisitForStmt(const ForStmt *FS);
2113 void VisitGotoStmt(const GotoStmt *GS);
2114 void VisitIfStmt(const IfStmt *If);
2115 void VisitInitListExpr(const InitListExpr *IE);
2116 void VisitMemberExpr(const MemberExpr *M);
2117 void VisitOffsetOfExpr(const OffsetOfExpr *E);
2118 void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
2119 void VisitObjCMessageExpr(const ObjCMessageExpr *M);
2120 void VisitOverloadExpr(const OverloadExpr *E);
2121 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
2122 void VisitStmt(const Stmt *S);
2123 void VisitSwitchStmt(const SwitchStmt *S);
2124 void VisitWhileStmt(const WhileStmt *W);
2125 void VisitTypeTraitExpr(const TypeTraitExpr *E);
2126 void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
2127 void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
2128 void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
2129 void VisitVAArgExpr(const VAArgExpr *E);
2130 void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
2131 void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
2132 void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
2133 void VisitLambdaExpr(const LambdaExpr *E);
2134 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
2135 void VisitRequiresExpr(const RequiresExpr *E);
2136 void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
2137 void VisitOMPLoopBasedDirective(const OMPLoopBasedDirective *D);
2138 void VisitOMPLoopDirective(const OMPLoopDirective *D);
2139 void VisitOMPParallelDirective(const OMPParallelDirective *D);
2140 void VisitOMPSimdDirective(const OMPSimdDirective *D);
2141 void
2142 VisitOMPLoopTransformationDirective(const OMPLoopTransformationDirective *D);
2143 void VisitOMPTileDirective(const OMPTileDirective *D);
2144 void VisitOMPUnrollDirective(const OMPUnrollDirective *D);
2145 void VisitOMPForDirective(const OMPForDirective *D);
2146 void VisitOMPForSimdDirective(const OMPForSimdDirective *D);
2147 void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
2148 void VisitOMPSectionDirective(const OMPSectionDirective *D);
2149 void VisitOMPSingleDirective(const OMPSingleDirective *D);
2150 void VisitOMPMasterDirective(const OMPMasterDirective *D);
2151 void VisitOMPCriticalDirective(const OMPCriticalDirective *D);
2152 void VisitOMPParallelForDirective(const OMPParallelForDirective *D);
2153 void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D);
2154 void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D);
2155 void VisitOMPParallelMaskedDirective(const OMPParallelMaskedDirective *D);
2156 void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D);
2157 void VisitOMPTaskDirective(const OMPTaskDirective *D);
2158 void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D);
2159 void VisitOMPBarrierDirective(const OMPBarrierDirective *D);
2160 void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D);
2161 void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D);
2162 void
2163 VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D);
2164 void VisitOMPCancelDirective(const OMPCancelDirective *D);
2165 void VisitOMPFlushDirective(const OMPFlushDirective *D);
2166 void VisitOMPDepobjDirective(const OMPDepobjDirective *D);
2167 void VisitOMPScanDirective(const OMPScanDirective *D);
2168 void VisitOMPOrderedDirective(const OMPOrderedDirective *D);
2169 void VisitOMPAtomicDirective(const OMPAtomicDirective *D);
2170 void VisitOMPTargetDirective(const OMPTargetDirective *D);
2171 void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D);
2172 void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D);
2173 void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D);
2174 void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D);
2175 void
2176 VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D);
2177 void VisitOMPTeamsDirective(const OMPTeamsDirective *D);
2178 void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D);
2179 void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D);
2180 void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D);
2181 void VisitOMPMaskedTaskLoopDirective(const OMPMaskedTaskLoopDirective *D);
2182 void
2183 VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D);
2184 void VisitOMPMaskedTaskLoopSimdDirective(
2185 const OMPMaskedTaskLoopSimdDirective *D);
2186 void VisitOMPParallelMasterTaskLoopDirective(
2187 const OMPParallelMasterTaskLoopDirective *D);
2188 void VisitOMPParallelMaskedTaskLoopDirective(
2189 const OMPParallelMaskedTaskLoopDirective *D);
2190 void VisitOMPParallelMasterTaskLoopSimdDirective(
2191 const OMPParallelMasterTaskLoopSimdDirective *D);
2192 void VisitOMPParallelMaskedTaskLoopSimdDirective(
2193 const OMPParallelMaskedTaskLoopSimdDirective *D);
2194 void VisitOMPDistributeDirective(const OMPDistributeDirective *D);
2195 void VisitOMPDistributeParallelForDirective(
2196 const OMPDistributeParallelForDirective *D);
2197 void VisitOMPDistributeParallelForSimdDirective(
2198 const OMPDistributeParallelForSimdDirective *D);
2199 void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D);
2200 void VisitOMPTargetParallelForSimdDirective(
2201 const OMPTargetParallelForSimdDirective *D);
2202 void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D);
2203 void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D);
2204 void VisitOMPTeamsDistributeSimdDirective(
2205 const OMPTeamsDistributeSimdDirective *D);
2206 void VisitOMPTeamsDistributeParallelForSimdDirective(
2207 const OMPTeamsDistributeParallelForSimdDirective *D);
2208 void VisitOMPTeamsDistributeParallelForDirective(
2209 const OMPTeamsDistributeParallelForDirective *D);
2210 void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D);
2211 void VisitOMPTargetTeamsDistributeDirective(
2212 const OMPTargetTeamsDistributeDirective *D);
2213 void VisitOMPTargetTeamsDistributeParallelForDirective(
2214 const OMPTargetTeamsDistributeParallelForDirective *D);
2215 void VisitOMPTargetTeamsDistributeParallelForSimdDirective(
2216 const OMPTargetTeamsDistributeParallelForSimdDirective *D);
2217 void VisitOMPTargetTeamsDistributeSimdDirective(
2218 const OMPTargetTeamsDistributeSimdDirective *D);
2219
2220private:
2221 void AddDeclarationNameInfo(const Stmt *S);
2222 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
2223 void AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2224 unsigned NumTemplateArgs);
2225 void AddMemberRef(const FieldDecl *D, SourceLocation L);
2226 void AddStmt(const Stmt *S);
2227 void AddDecl(const Decl *D, bool isFirst = true);
2228 void AddTypeLoc(TypeSourceInfo *TI);
2229 void EnqueueChildren(const Stmt *S);
2230 void EnqueueChildren(const OMPClause *S);
2231};
2232} // namespace
2233
2234void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
2235 // 'S' should always be non-null, since it comes from the
2236 // statement we are visiting.
2237 WL.push_back(DeclarationNameInfoVisit(S, Parent));
2238}
2239
2240void EnqueueVisitor::AddNestedNameSpecifierLoc(
2241 NestedNameSpecifierLoc Qualifier) {
2242 if (Qualifier)
2243 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
2244}
2245
2246void EnqueueVisitor::AddStmt(const Stmt *S) {
2247 if (S)
2248 WL.push_back(StmtVisit(S, Parent));
2249}
2250void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
2251 if (D)
2252 WL.push_back(DeclVisit(D, Parent, isFirst));
2253}
2254void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2255 unsigned NumTemplateArgs) {
2256 WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent));
2257}
2258void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
2259 if (D)
2260 WL.push_back(MemberRefVisit(D, L, Parent));
2261}
2262void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
2263 if (TI)
2264 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
2265}
2266void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
2267 unsigned size = WL.size();
2268 for (const Stmt *SubStmt : S->children()) {
2269 AddStmt(SubStmt);
2270 }
2271 if (size == WL.size())
2272 return;
2273 // Now reverse the entries we just added. This will match the DFS
2274 // ordering performed by the worklist.
2275 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2276 std::reverse(I, E);
2277}
2278namespace {
2279class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
2280 EnqueueVisitor *Visitor;
2281 /// Process clauses with list of variables.
2282 template <typename T> void VisitOMPClauseList(T *Node);
2283
2284public:
2285 OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) {}
2286#define GEN_CLANG_CLAUSE_CLASS
2287#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(const Class *C);
2288#include "llvm/Frontend/OpenMP/OMP.inc"
2289 void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C);
2290 void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C);
2291};
2292
2293void OMPClauseEnqueue::VisitOMPClauseWithPreInit(
2294 const OMPClauseWithPreInit *C) {
2295 Visitor->AddStmt(C->getPreInitStmt());
2296}
2297
2298void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate(
2299 const OMPClauseWithPostUpdate *C) {
2300 VisitOMPClauseWithPreInit(C);
2301 Visitor->AddStmt(C->getPostUpdateExpr());
2302}
2303
2304void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
2305 VisitOMPClauseWithPreInit(C);
2306 Visitor->AddStmt(C->getCondition());
2307}
2308
2309void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) {
2310 Visitor->AddStmt(C->getCondition());
2311}
2312
2313void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
2314 VisitOMPClauseWithPreInit(C);
2315 Visitor->AddStmt(C->getNumThreads());
2316}
2317
2318void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) {
2319 Visitor->AddStmt(C->getSafelen());
2320}
2321
2322void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) {
2323 Visitor->AddStmt(C->getSimdlen());
2324}
2325
2326void OMPClauseEnqueue::VisitOMPSizesClause(const OMPSizesClause *C) {
2327 for (auto E : C->getSizesRefs())
2328 Visitor->AddStmt(E);
2329}
2330
2331void OMPClauseEnqueue::VisitOMPFullClause(const OMPFullClause *C) {}
2332
2333void OMPClauseEnqueue::VisitOMPPartialClause(const OMPPartialClause *C) {
2334 Visitor->AddStmt(C->getFactor());
2335}
2336
2337void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) {
2338 Visitor->AddStmt(C->getAllocator());
2339}
2340
2341void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) {
2342 Visitor->AddStmt(C->getNumForLoops());
2343}
2344
2345void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) {}
2346
2347void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) {}
2348
2349void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) {
2350 VisitOMPClauseWithPreInit(C);
2351 Visitor->AddStmt(C->getChunkSize());
2352}
2353
2354void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) {
2355 Visitor->AddStmt(C->getNumForLoops());
2356}
2357
2358void OMPClauseEnqueue::VisitOMPDetachClause(const OMPDetachClause *C) {
2359 Visitor->AddStmt(C->getEventHandler());
2360}
2361
2362void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {}
2363
2364void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
2365
2366void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
2367
2368void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
2369
2370void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {}
2371
2372void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {}
2373
2374void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
2375
2376void OMPClauseEnqueue::VisitOMPCompareClause(const OMPCompareClause *) {}
2377
2378void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
2379
2380void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {}
2381
2382void OMPClauseEnqueue::VisitOMPAcquireClause(const OMPAcquireClause *) {}
2383
2384void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {}
2385
2386void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {}
2387
2388void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {}
2389
2390void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {}
2391
2392void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {}
2393
2394void OMPClauseEnqueue::VisitOMPInitClause(const OMPInitClause *C) {
2395 VisitOMPClauseList(C);
2396}
2397
2398void OMPClauseEnqueue::VisitOMPUseClause(const OMPUseClause *C) {
2399 Visitor->AddStmt(C->getInteropVar());
2400}
2401
2402void OMPClauseEnqueue::VisitOMPDestroyClause(const OMPDestroyClause *C) {
2403 if (C->getInteropVar())
2404 Visitor->AddStmt(C->getInteropVar());
2405}
2406
2407void OMPClauseEnqueue::VisitOMPNovariantsClause(const OMPNovariantsClause *C) {
2408 Visitor->AddStmt(C->getCondition());
2409}
2410
2411void OMPClauseEnqueue::VisitOMPNocontextClause(const OMPNocontextClause *C) {
2412 Visitor->AddStmt(C->getCondition());
2413}
2414
2415void OMPClauseEnqueue::VisitOMPFilterClause(const OMPFilterClause *C) {
2416 VisitOMPClauseWithPreInit(C);
2417 Visitor->AddStmt(C->getThreadID());
2418}
2419
2420void OMPClauseEnqueue::VisitOMPAlignClause(const OMPAlignClause *C) {
2421 Visitor->AddStmt(C->getAlignment());
2422}
2423
2424void OMPClauseEnqueue::VisitOMPUnifiedAddressClause(
2425 const OMPUnifiedAddressClause *) {}
2426
2427void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause(
2428 const OMPUnifiedSharedMemoryClause *) {}
2429
2430void OMPClauseEnqueue::VisitOMPReverseOffloadClause(
2431 const OMPReverseOffloadClause *) {}
2432
2433void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause(
2434 const OMPDynamicAllocatorsClause *) {}
2435
2436void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause(
2437 const OMPAtomicDefaultMemOrderClause *) {}
2438
2439void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) {
2440 Visitor->AddStmt(C->getDevice());
2441}
2442
2443void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) {
2444 VisitOMPClauseWithPreInit(C);
2445 Visitor->AddStmt(C->getNumTeams());
2446}
2447
2448void OMPClauseEnqueue::VisitOMPThreadLimitClause(
2449 const OMPThreadLimitClause *C) {
2450 VisitOMPClauseWithPreInit(C);
2451 Visitor->AddStmt(C->getThreadLimit());
2452}
2453
2454void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) {
2455 Visitor->AddStmt(C->getPriority());
2456}
2457
2458void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) {
2459 Visitor->AddStmt(C->getGrainsize());
2460}
2461
2462void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) {
2463 Visitor->AddStmt(C->getNumTasks());
2464}
2465
2466void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) {
2467 Visitor->AddStmt(C->getHint());
2468}
2469
2470template <typename T> void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
2471 for (const auto *I : Node->varlists()) {
2472 Visitor->AddStmt(I);
2473 }
2474}
2475
2476void OMPClauseEnqueue::VisitOMPInclusiveClause(const OMPInclusiveClause *C) {
2477 VisitOMPClauseList(C);
2478}
2479void OMPClauseEnqueue::VisitOMPExclusiveClause(const OMPExclusiveClause *C) {
2480 VisitOMPClauseList(C);
2481}
2482void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) {
2483 VisitOMPClauseList(C);
2484 Visitor->AddStmt(C->getAllocator());
2485}
2486void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
2487 VisitOMPClauseList(C);
2488 for (const auto *E : C->private_copies()) {
2489 Visitor->AddStmt(E);
2490 }
2491}
2492void OMPClauseEnqueue::VisitOMPFirstprivateClause(
2493 const OMPFirstprivateClause *C) {
2494 VisitOMPClauseList(C);
2495 VisitOMPClauseWithPreInit(C);
2496 for (const auto *E : C->private_copies()) {
2497 Visitor->AddStmt(E);
2498 }
2499 for (const auto *E : C->inits()) {
2500 Visitor->AddStmt(E);
2501 }
2502}
2503void OMPClauseEnqueue::VisitOMPLastprivateClause(
2504 const OMPLastprivateClause *C) {
2505 VisitOMPClauseList(C);
2506 VisitOMPClauseWithPostUpdate(C);
2507 for (auto *E : C->private_copies()) {
2508 Visitor->AddStmt(E);
2509 }
2510 for (auto *E : C->source_exprs()) {
2511 Visitor->AddStmt(E);
2512 }
2513 for (auto *E : C->destination_exprs()) {
2514 Visitor->AddStmt(E);
2515 }
2516 for (auto *E : C->assignment_ops()) {
2517 Visitor->AddStmt(E);
2518 }
2519}
2520void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) {
2521 VisitOMPClauseList(C);
2522}
2523void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) {
2524 VisitOMPClauseList(C);
2525 VisitOMPClauseWithPostUpdate(C);
2526 for (auto *E : C->privates()) {
2527 Visitor->AddStmt(E);
2528 }
2529 for (auto *E : C->lhs_exprs()) {
2530 Visitor->AddStmt(E);
2531 }
2532 for (auto *E : C->rhs_exprs()) {
2533 Visitor->AddStmt(E);
2534 }
2535 for (auto *E : C->reduction_ops()) {
2536 Visitor->AddStmt(E);
2537 }
2538 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
2539 for (auto *E : C->copy_ops()) {
2540 Visitor->AddStmt(E);
2541 }
2542 for (auto *E : C->copy_array_temps()) {
2543 Visitor->AddStmt(E);
2544 }
2545 for (auto *E : C->copy_array_elems()) {
2546 Visitor->AddStmt(E);
2547 }
2548 }
2549}
2550void OMPClauseEnqueue::VisitOMPTaskReductionClause(
2551 const OMPTaskReductionClause *C) {
2552 VisitOMPClauseList(C);
2553 VisitOMPClauseWithPostUpdate(C);
2554 for (auto *E : C->privates()) {
2555 Visitor->AddStmt(E);
2556 }
2557 for (auto *E : C->lhs_exprs()) {
2558 Visitor->AddStmt(E);
2559 }
2560 for (auto *E : C->rhs_exprs()) {
2561 Visitor->AddStmt(E);
2562 }
2563 for (auto *E : C->reduction_ops()) {
2564 Visitor->AddStmt(E);
2565 }
2566}
2567void OMPClauseEnqueue::VisitOMPInReductionClause(
2568 const OMPInReductionClause *C) {
2569 VisitOMPClauseList(C);
2570 VisitOMPClauseWithPostUpdate(C);
2571 for (auto *E : C->privates()) {
2572 Visitor->AddStmt(E);
2573 }
2574 for (auto *E : C->lhs_exprs()) {
2575 Visitor->AddStmt(E);
2576 }
2577 for (auto *E : C->rhs_exprs()) {
2578 Visitor->AddStmt(E);
2579 }
2580 for (auto *E : C->reduction_ops()) {
2581 Visitor->AddStmt(E);
2582 }
2583 for (auto *E : C->taskgroup_descriptors())
2584 Visitor->AddStmt(E);
2585}
2586void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) {
2587 VisitOMPClauseList(C);
2588 VisitOMPClauseWithPostUpdate(C);
2589 for (const auto *E : C->privates()) {
2590 Visitor->AddStmt(E);
2591 }
2592 for (const auto *E : C->inits()) {
2593 Visitor->AddStmt(E);
2594 }
2595 for (const auto *E : C->updates()) {
2596 Visitor->AddStmt(E);
2597 }
2598 for (const auto *E : C->finals()) {
2599 Visitor->AddStmt(E);
2600 }
2601 Visitor->AddStmt(C->getStep());
2602 Visitor->AddStmt(C->getCalcStep());
2603}
2604void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) {
2605 VisitOMPClauseList(C);
2606 Visitor->AddStmt(C->getAlignment());
2607}
2608void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) {
2609 VisitOMPClauseList(C);
2610 for (auto *E : C->source_exprs()) {
2611 Visitor->AddStmt(E);
2612 }
2613 for (auto *E : C->destination_exprs()) {
2614 Visitor->AddStmt(E);
2615 }
2616 for (auto *E : C->assignment_ops()) {
2617 Visitor->AddStmt(E);
2618 }
2619}
2620void OMPClauseEnqueue::VisitOMPCopyprivateClause(
2621 const OMPCopyprivateClause *C) {
2622 VisitOMPClauseList(C);
2623 for (auto *E : C->source_exprs()) {
2624 Visitor->AddStmt(E);
2625 }
2626 for (auto *E : C->destination_exprs()) {
2627 Visitor->AddStmt(E);
2628 }
2629 for (auto *E : C->assignment_ops()) {
2630 Visitor->AddStmt(E);
2631 }
2632}
2633void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) {
2634 VisitOMPClauseList(C);
2635}
2636void OMPClauseEnqueue::VisitOMPDepobjClause(const OMPDepobjClause *C) {
2637 Visitor->AddStmt(C->getDepobj());
2638}
2639void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) {
2640 VisitOMPClauseList(C);
2641}
2642void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) {
2643 VisitOMPClauseList(C);
2644}
2645void OMPClauseEnqueue::VisitOMPDistScheduleClause(
2646 const OMPDistScheduleClause *C) {
2647 VisitOMPClauseWithPreInit(C);
2648 Visitor->AddStmt(C->getChunkSize());
2649}
2650void OMPClauseEnqueue::VisitOMPDefaultmapClause(
2651 const OMPDefaultmapClause * /*C*/) {}
2652void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) {
2653 VisitOMPClauseList(C);
2654}
2655void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) {
2656 VisitOMPClauseList(C);
2657}
2658void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(
2659 const OMPUseDevicePtrClause *C) {
2660 VisitOMPClauseList(C);
2661}
2662void OMPClauseEnqueue::VisitOMPUseDeviceAddrClause(
2663 const OMPUseDeviceAddrClause *C) {
2664 VisitOMPClauseList(C);
2665}
2666void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(
2667 const OMPIsDevicePtrClause *C) {
2668 VisitOMPClauseList(C);
2669}
2670void OMPClauseEnqueue::VisitOMPHasDeviceAddrClause(
2671 const OMPHasDeviceAddrClause *C) {
2672 VisitOMPClauseList(C);
2673}
2674void OMPClauseEnqueue::VisitOMPNontemporalClause(
2675 const OMPNontemporalClause *C) {
2676 VisitOMPClauseList(C);
2677 for (const auto *E : C->private_refs())
2678 Visitor->AddStmt(E);
2679}
2680void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {}
2681void OMPClauseEnqueue::VisitOMPUsesAllocatorsClause(
2682 const OMPUsesAllocatorsClause *C) {
2683 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
2684 const OMPUsesAllocatorsClause::Data &D = C->getAllocatorData(I);
2685 Visitor->AddStmt(D.Allocator);
2686 Visitor->AddStmt(D.AllocatorTraits);
2687 }
2688}
2689void OMPClauseEnqueue::VisitOMPAffinityClause(const OMPAffinityClause *C) {
2690 Visitor->AddStmt(C->getModifier());
2691 for (const Expr *E : C->varlists())
2692 Visitor->AddStmt(E);
2693}
2694void OMPClauseEnqueue::VisitOMPBindClause(const OMPBindClause *C) {}
2695
2696} // namespace
2697
2698void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
2699 unsigned size = WL.size();
2700 OMPClauseEnqueue Visitor(this);
2701 Visitor.Visit(S);
2702 if (size == WL.size())
2703 return;
2704 // Now reverse the entries we just added. This will match the DFS
2705 // ordering performed by the worklist.
2706 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2707 std::reverse(I, E);
2708}
2709void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2710 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
2711}
2712void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
2713 AddDecl(B->getBlockDecl());
2714}
2715void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2716 EnqueueChildren(E);
2717 AddTypeLoc(E->getTypeSourceInfo());
2718}
2719void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
2720 for (auto &I : llvm::reverse(S->body()))
2721 AddStmt(I);
2722}
2723void EnqueueVisitor::VisitMSDependentExistsStmt(
2724 const MSDependentExistsStmt *S) {
2725 AddStmt(S->getSubStmt());
2726 AddDeclarationNameInfo(S);
2727 if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
2728 AddNestedNameSpecifierLoc(QualifierLoc);
2729}
2730
2731void EnqueueVisitor::VisitCXXDependentScopeMemberExpr(
2732 const CXXDependentScopeMemberExpr *E) {
2733 if (E->hasExplicitTemplateArgs())
2734 AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2735 AddDeclarationNameInfo(E);
2736 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2737 AddNestedNameSpecifierLoc(QualifierLoc);
2738 if (!E->isImplicitAccess())
2739 AddStmt(E->getBase());
2740}
2741void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
2742 // Enqueue the initializer , if any.
2743 AddStmt(E->getInitializer());
2744 // Enqueue the array size, if any.
2745 AddStmt(E->getArraySize().value_or(nullptr));
2746 // Enqueue the allocated type.
2747 AddTypeLoc(E->getAllocatedTypeSourceInfo());
2748 // Enqueue the placement arguments.
2749 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
2750 AddStmt(E->getPlacementArg(I - 1));
2751}
2752void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
2753 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
2754 AddStmt(CE->getArg(I - 1));
2755 AddStmt(CE->getCallee());
2756 AddStmt(CE->getArg(0));
2757}
2758void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
2759 const CXXPseudoDestructorExpr *E) {
2760 // Visit the name of the type being destroyed.
2761 AddTypeLoc(E->getDestroyedTypeInfo());
2762 // Visit the scope type that looks disturbingly like the nested-name-specifier
2763 // but isn't.
2764 AddTypeLoc(E->getScopeTypeInfo());
2765 // Visit the nested-name-specifier.
2766 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2767 AddNestedNameSpecifierLoc(QualifierLoc);
2768 // Visit base expression.
2769 AddStmt(E->getBase());
2770}
2771void EnqueueVisitor::VisitCXXScalarValueInitExpr(
2772 const CXXScalarValueInitExpr *E) {
2773 AddTypeLoc(E->getTypeSourceInfo());
2774}
2775void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
2776 const CXXTemporaryObjectExpr *E) {
2777 EnqueueChildren(E);
2778 AddTypeLoc(E->getTypeSourceInfo());
2779}
2780void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2781 EnqueueChildren(E);
2782 if (E->isTypeOperand())
2783 AddTypeLoc(E->getTypeOperandSourceInfo());
2784}
2785
2786void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
2787 const CXXUnresolvedConstructExpr *E) {
2788 EnqueueChildren(E);
2789 AddTypeLoc(E->getTypeSourceInfo());
2790}
2791void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2792 EnqueueChildren(E);
2793 if (E->isTypeOperand())
2794 AddTypeLoc(E->getTypeOperandSourceInfo());
2795}
2796
2797void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
2798 EnqueueChildren(S);
2799 AddDecl(S->getExceptionDecl());
2800}
2801
2802void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
2803 AddStmt(S->getBody());
2804 AddStmt(S->getRangeInit());
2805 AddDecl(S->getLoopVariable());
2806}
2807
2808void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
2809 if (DR->hasExplicitTemplateArgs())
2810 AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs());
2811 WL.push_back(DeclRefExprParts(DR, Parent));
2812}
2813void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
2814 const DependentScopeDeclRefExpr *E) {
2815 if (E->hasExplicitTemplateArgs())
2816 AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2817 AddDeclarationNameInfo(E);
2818 AddNestedNameSpecifierLoc(E->getQualifierLoc());
2819}
2820void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
2821 unsigned size = WL.size();
2822 bool isFirst = true;
2823 for (const auto *D : S->decls()) {
2824 AddDecl(D, isFirst);
2825 isFirst = false;
2826 }
2827 if (size == WL.size())
2828 return;
2829 // Now reverse the entries we just added. This will match the DFS
2830 // ordering performed by the worklist.
2831 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2832 std::reverse(I, E);
2833}
2834void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
2835 AddStmt(E->getInit());
2836 for (const DesignatedInitExpr::Designator &D :
2837 llvm::reverse(E->designators())) {
2838 if (D.isFieldDesignator()) {
2839 if (FieldDecl *Field = D.getField())
2840 AddMemberRef(Field, D.getFieldLoc());
2841 continue;
2842 }
2843 if (D.isArrayDesignator()) {
2844 AddStmt(E->getArrayIndex(D));
2845 continue;
2846 }
2847 assert(D.isArrayRangeDesignator() && "Unknown designator kind")(static_cast <bool> (D.isArrayRangeDesignator() &&
"Unknown designator kind") ? void (0) : __assert_fail ("D.isArrayRangeDesignator() && \"Unknown designator kind\""
, "clang/tools/libclang/CIndex.cpp", 2847, __extension__ __PRETTY_FUNCTION__
))
;
2848 AddStmt(E->getArrayRangeEnd(D));
2849 AddStmt(E->getArrayRangeStart(D));
2850 }
2851}
2852void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
2853 EnqueueChildren(E);
2854 AddTypeLoc(E->getTypeInfoAsWritten());
2855}
2856void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
2857 AddStmt(FS->getBody());
2858 AddStmt(FS->getInc());
2859 AddStmt(FS->getCond());
2860 AddDecl(FS->getConditionVariable());
2861 AddStmt(FS->getInit());
2862}
2863void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
2864 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
2865}
2866void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
2867 AddStmt(If->getElse());
2868 AddStmt(If->getThen());
2869 AddStmt(If->getCond());
2870 AddStmt(If->getInit());
2871 AddDecl(If->getConditionVariable());
2872}
2873void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
2874 // We care about the syntactic form of the initializer list, only.
2875 if (InitListExpr *Syntactic = IE->getSyntacticForm())
2876 IE = Syntactic;
2877 EnqueueChildren(IE);
2878}
2879void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
2880 WL.push_back(MemberExprParts(M, Parent));
2881
2882 // If the base of the member access expression is an implicit 'this', don't
2883 // visit it.
2884 // FIXME: If we ever want to show these implicit accesses, this will be
2885 // unfortunate. However, clang_getCursor() relies on this behavior.
2886 if (M->isImplicitAccess())
2887 return;
2888
2889 // Ignore base anonymous struct/union fields, otherwise they will shadow the
2890 // real field that we are interested in.
2891 if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) {
2892 if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) {
2893 if (FD->isAnonymousStructOrUnion()) {
2894 AddStmt(SubME->getBase());
2895 return;
2896 }
2897 }
2898 }
2899
2900 AddStmt(M->getBase());
2901}
2902void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2903 AddTypeLoc(E->getEncodedTypeSourceInfo());
2904}
2905void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
2906 EnqueueChildren(M);
2907 AddTypeLoc(M->getClassReceiverTypeInfo());
2908}
2909void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
2910 // Visit the components of the offsetof expression.
2911 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
2912 const OffsetOfNode &Node = E->getComponent(I - 1);
2913 switch (Node.getKind()) {
2914 case OffsetOfNode::Array:
2915 AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
2916 break;
2917 case OffsetOfNode::Field:
2918 AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
2919 break;
2920 case OffsetOfNode::Identifier:
2921 case OffsetOfNode::Base:
2922 continue;
2923 }
2924 }
2925 // Visit the type into which we're computing the offset.
2926 AddTypeLoc(E->getTypeSourceInfo());
2927}
2928void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
2929 if (E->hasExplicitTemplateArgs())
2930 AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2931 WL.push_back(OverloadExprParts(E, Parent));
2932}
2933void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
2934 const UnaryExprOrTypeTraitExpr *E) {
2935 EnqueueChildren(E);
2936 if (E->isArgumentType())
2937 AddTypeLoc(E->getArgumentTypeInfo());
2938}
2939void EnqueueVisitor::VisitStmt(const Stmt *S) { EnqueueChildren(S); }
2940void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
2941 AddStmt(S->getBody());
2942 AddStmt(S->getCond());
2943 AddDecl(S->getConditionVariable());
2944}
2945
2946void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
2947 AddStmt(W->getBody());
2948 AddStmt(W->getCond());
2949 AddDecl(W->getConditionVariable());
2950}
2951
2952void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
2953 for (unsigned I = E->getNumArgs(); I > 0; --I)
2954 AddTypeLoc(E->getArg(I - 1));
2955}
2956
2957void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
2958 AddTypeLoc(E->getQueriedTypeSourceInfo());
2959}
2960
2961void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
2962 EnqueueChildren(E);
2963}
2964
2965void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
2966 VisitOverloadExpr(U);
2967 if (!U->isImplicitAccess())
2968 AddStmt(U->getBase());
2969}
2970void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
2971 AddStmt(E->getSubExpr());
2972 AddTypeLoc(E->getWrittenTypeInfo());
2973}
2974void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
2975 WL.push_back(SizeOfPackExprParts(E, Parent));
2976}
2977void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2978 // If the opaque value has a source expression, just transparently
2979 // visit that. This is useful for (e.g.) pseudo-object expressions.
2980 if (Expr *SourceExpr = E->getSourceExpr())
2981 return Visit(SourceExpr);
2982}
2983void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
2984 AddStmt(E->getBody());
2985 WL.push_back(LambdaExprParts(E, Parent));
2986}
2987void EnqueueVisitor::VisitConceptSpecializationExpr(
2988 const ConceptSpecializationExpr *E) {
2989 WL.push_back(ConceptSpecializationExprVisit(E, Parent));
2990}
2991void EnqueueVisitor::VisitRequiresExpr(const RequiresExpr *E) {
2992 WL.push_back(RequiresExprVisit(E, Parent));
2993 for (ParmVarDecl *VD : E->getLocalParameters())
2994 AddDecl(VD);
2995}
2996void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
2997 // Treat the expression like its syntactic form.
2998 Visit(E->getSyntacticForm());
2999}
3000
3001void EnqueueVisitor::VisitOMPExecutableDirective(
3002 const OMPExecutableDirective *D) {
3003 EnqueueChildren(D);
3004 for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
3005 E = D->clauses().end();
3006 I != E; ++I)
3007 EnqueueChildren(*I);
3008}
3009
3010void EnqueueVisitor::VisitOMPLoopBasedDirective(
3011 const OMPLoopBasedDirective *D) {
3012 VisitOMPExecutableDirective(D);
3013}
3014
3015void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) {
3016 VisitOMPLoopBasedDirective(D);
3017}
3018
3019void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
3020 VisitOMPExecutableDirective(D);
3021}
3022
3023void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) {
3024 VisitOMPLoopDirective(D);
3025}
3026
3027void EnqueueVisitor::VisitOMPLoopTransformationDirective(
3028 const OMPLoopTransformationDirective *D) {
3029 VisitOMPLoopBasedDirective(D);
3030}
3031
3032void EnqueueVisitor::VisitOMPTileDirective(const OMPTileDirective *D) {
3033 VisitOMPLoopTransformationDirective(D);
3034}
3035
3036void EnqueueVisitor::VisitOMPUnrollDirective(const OMPUnrollDirective *D) {
3037 VisitOMPLoopTransformationDirective(D);
3038}
3039
3040void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) {
3041 VisitOMPLoopDirective(D);
3042}
3043
3044void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) {
3045 VisitOMPLoopDirective(D);
3046}
3047
3048void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
3049 VisitOMPExecutableDirective(D);
3050}
3051
3052void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
3053 VisitOMPExecutableDirective(D);
3054}
3055
3056void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) {
3057 VisitOMPExecutableDirective(D);
3058}
3059
3060void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) {
3061 VisitOMPExecutableDirective(D);
3062}
3063
3064void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) {
3065 VisitOMPExecutableDirective(D);
3066 AddDeclarationNameInfo(D);
3067}
3068
3069void EnqueueVisitor::VisitOMPParallelForDirective(
3070 const OMPParallelForDirective *D) {
3071 VisitOMPLoopDirective(D);
3072}
3073
3074void EnqueueVisitor::VisitOMPParallelForSimdDirective(
3075 const OMPParallelForSimdDirective *D) {
3076 VisitOMPLoopDirective(D);
3077}
3078
3079void EnqueueVisitor::VisitOMPParallelMasterDirective(
3080 const OMPParallelMasterDirective *D) {
3081 VisitOMPExecutableDirective(D);
3082}
3083
3084void EnqueueVisitor::VisitOMPParallelMaskedDirective(
3085 const OMPParallelMaskedDirective *D) {
3086 VisitOMPExecutableDirective(D);
3087}
3088
3089void EnqueueVisitor::VisitOMPParallelSectionsDirective(
3090 const OMPParallelSectionsDirective *D) {
3091 VisitOMPExecutableDirective(D);
3092}
3093
3094void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) {
3095 VisitOMPExecutableDirective(D);
3096}
3097
3098void EnqueueVisitor::VisitOMPTaskyieldDirective(
3099 const OMPTaskyieldDirective *D) {
3100 VisitOMPExecutableDirective(D);
3101}
3102
3103void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) {
3104 VisitOMPExecutableDirective(D);
3105}
3106
3107void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) {
3108 VisitOMPExecutableDirective(D);
3109}
3110
3111void EnqueueVisitor::VisitOMPTaskgroupDirective(
3112 const OMPTaskgroupDirective *D) {
3113 VisitOMPExecutableDirective(D);
3114 if (const Expr *E = D->getReductionRef())
3115 VisitStmt(E);
3116}
3117
3118void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) {
3119 VisitOMPExecutableDirective(D);
3120}
3121
3122void EnqueueVisitor::VisitOMPDepobjDirective(const OMPDepobjDirective *D) {
3123 VisitOMPExecutableDirective(D);
3124}
3125
3126void EnqueueVisitor::VisitOMPScanDirective(const OMPScanDirective *D) {
3127 VisitOMPExecutableDirective(D);
3128}
3129
3130void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) {
3131 VisitOMPExecutableDirective(D);
3132}
3133
3134void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) {
3135 VisitOMPExecutableDirective(D);
3136}
3137
3138void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) {
3139 VisitOMPExecutableDirective(D);
3140}
3141
3142void EnqueueVisitor::VisitOMPTargetDataDirective(
3143 const OMPTargetDataDirective *D) {
3144 VisitOMPExecutableDirective(D);
3145}
3146
3147void EnqueueVisitor::VisitOMPTargetEnterDataDirective(
3148 const OMPTargetEnterDataDirective *D) {
3149 VisitOMPExecutableDirective(D);
3150}
3151
3152void EnqueueVisitor::VisitOMPTargetExitDataDirective(
3153 const OMPTargetExitDataDirective *D) {
3154 VisitOMPExecutableDirective(D);
3155}
3156
3157void EnqueueVisitor::VisitOMPTargetParallelDirective(
3158 const OMPTargetParallelDirective *D) {
3159 VisitOMPExecutableDirective(D);
3160}
3161
3162void EnqueueVisitor::VisitOMPTargetParallelForDirective(
3163 const OMPTargetParallelForDirective *D) {
3164 VisitOMPLoopDirective(D);
3165}
3166
3167void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) {
3168 VisitOMPExecutableDirective(D);
3169}
3170
3171void EnqueueVisitor::VisitOMPCancellationPointDirective(
3172 const OMPCancellationPointDirective *D) {
3173 VisitOMPExecutableDirective(D);
3174}
3175
3176void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) {
3177 VisitOMPExecutableDirective(D);
3178}
3179
3180void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) {
3181 VisitOMPLoopDirective(D);
3182}
3183
3184void EnqueueVisitor::VisitOMPTaskLoopSimdDirective(
3185 const OMPTaskLoopSimdDirective *D) {
3186 VisitOMPLoopDirective(D);
3187}
3188
3189void EnqueueVisitor::VisitOMPMasterTaskLoopDirective(
3190 const OMPMasterTaskLoopDirective *D) {
3191 VisitOMPLoopDirective(D);
3192}
3193
3194void EnqueueVisitor::VisitOMPMaskedTaskLoopDirective(
3195 const OMPMaskedTaskLoopDirective *D) {
3196 VisitOMPLoopDirective(D);
3197}
3198
3199void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective(
3200 const OMPMasterTaskLoopSimdDirective *D) {
3201 VisitOMPLoopDirective(D);
3202}
3203
3204void EnqueueVisitor::VisitOMPMaskedTaskLoopSimdDirective(
3205 const OMPMaskedTaskLoopSimdDirective *D) {
3206 VisitOMPLoopDirective(D);
3207}
3208
3209void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective(
3210 const OMPParallelMasterTaskLoopDirective *D) {
3211 VisitOMPLoopDirective(D);
3212}
3213
3214void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopDirective(
3215 const OMPParallelMaskedTaskLoopDirective *D) {
3216 VisitOMPLoopDirective(D);
3217}
3218
3219void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective(
3220 const OMPParallelMasterTaskLoopSimdDirective *D) {
3221 VisitOMPLoopDirective(D);
3222}
3223
3224void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopSimdDirective(
3225 const OMPParallelMaskedTaskLoopSimdDirective *D) {
3226 VisitOMPLoopDirective(D);
3227}
3228
3229void EnqueueVisitor::VisitOMPDistributeDirective(
3230 const OMPDistributeDirective *D) {
3231 VisitOMPLoopDirective(D);
3232}
3233
3234void EnqueueVisitor::VisitOMPDistributeParallelForDirective(
3235 const OMPDistributeParallelForDirective *D) {
3236 VisitOMPLoopDirective(D);
3237}
3238
3239void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective(
3240 const OMPDistributeParallelForSimdDirective *D) {
3241 VisitOMPLoopDirective(D);
3242}
3243
3244void EnqueueVisitor::VisitOMPDistributeSimdDirective(
3245 const OMPDistributeSimdDirective *D) {
3246 VisitOMPLoopDirective(D);
3247}
3248
3249void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective(
3250 const OMPTargetParallelForSimdDirective *D) {
3251 VisitOMPLoopDirective(D);
3252}
3253
3254void EnqueueVisitor::VisitOMPTargetSimdDirective(
3255 const OMPTargetSimdDirective *D) {
3256 VisitOMPLoopDirective(D);
3257}
3258
3259void EnqueueVisitor::VisitOMPTeamsDistributeDirective(
3260 const OMPTeamsDistributeDirective *D) {
3261 VisitOMPLoopDirective(D);
3262}
3263
3264void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective(
3265 const OMPTeamsDistributeSimdDirective *D) {
3266 VisitOMPLoopDirective(D);
3267}
3268
3269void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective(
3270 const OMPTeamsDistributeParallelForSimdDirective *D) {
3271 VisitOMPLoopDirective(D);
3272}
3273
3274void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective(
3275 const OMPTeamsDistributeParallelForDirective *D) {
3276 VisitOMPLoopDirective(D);
3277}
3278
3279void EnqueueVisitor::VisitOMPTargetTeamsDirective(
3280 const OMPTargetTeamsDirective *D) {
3281 VisitOMPExecutableDirective(D);
3282}
3283
3284void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective(
3285 const OMPTargetTeamsDistributeDirective *D) {
3286 VisitOMPLoopDirective(D);
3287}
3288
3289void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective(
3290 const OMPTargetTeamsDistributeParallelForDirective *D) {
3291 VisitOMPLoopDirective(D);
3292}
3293
3294void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
3295 const OMPTargetTeamsDistributeParallelForSimdDirective *D) {
3296 VisitOMPLoopDirective(D);
3297}
3298
3299void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective(
3300 const OMPTargetTeamsDistributeSimdDirective *D) {
3301 VisitOMPLoopDirective(D);
3302}
3303
3304void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
3305 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU, RegionOfInterest))
3306 .Visit(S);
3307}
3308
3309bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
3310 if (RegionOfInterest.isValid()) {
3311 SourceRange Range = getRawCursorExtent(C);
3312 if (Range.isInvalid() || CompareRegionOfInterest(Range))
3313 return false;
3314 }
3315 return true;
3316}
3317
3318bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
3319 while (!WL.empty()) {
3320 // Dequeue the worklist item.
3321 VisitorJob LI = WL.pop_back_val();
3322
3323 // Set the Parent field, then back to its old value once we're done.
3324 SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
3325
3326 switch (LI.getKind()) {
3327 case VisitorJob::DeclVisitKind: {
3328 const Decl *D = cast<DeclVisit>(&LI)->get();
3329 if (!D)
3330 continue;
3331
3332 // For now, perform default visitation for Decls.
3333 if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
3334 cast<DeclVisit>(&LI)->isFirst())))
3335 return true;
3336
3337 continue;
3338 }
3339 case VisitorJob::ExplicitTemplateArgsVisitKind: {
3340 for (const TemplateArgumentLoc &Arg :
3341 *cast<ExplicitTemplateArgsVisit>(&LI)) {
3342 if (VisitTemplateArgumentLoc(Arg))
3343 return true;
3344 }
3345 continue;
3346 }
3347 case VisitorJob::TypeLocVisitKind: {
3348 // Perform default visitation for TypeLocs.
3349 if (Visit(cast<TypeLocVisit>(&LI)->get()))
3350 return true;
3351 continue;
3352 }
3353 case VisitorJob::LabelRefVisitKind: {
3354 const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
3355 if (LabelStmt *stmt = LS->getStmt()) {
3356 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
3357 TU))) {
3358 return true;
3359 }
3360 }
3361 continue;
3362 }
3363
3364 case VisitorJob::NestedNameSpecifierLocVisitKind: {
3365 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
3366 if (VisitNestedNameSpecifierLoc(V->get()))
3367 return true;
3368 continue;
3369 }
3370
3371 case VisitorJob::DeclarationNameInfoVisitKind: {
3372 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)->get()))
3373 return true;
3374 continue;
3375 }
3376 case VisitorJob::MemberRefVisitKind: {
3377 MemberRefVisit *V = cast<MemberRefVisit>(&LI);
3378 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
3379 return true;
3380 continue;
3381 }
3382 case VisitorJob::StmtVisitKind: {
3383 const Stmt *S = cast<StmtVisit>(&LI)->get();
3384 if (!S)
3385 continue;
3386
3387 // Update the current cursor.
3388 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
3389 if (!IsInRegionOfInterest(Cursor))
3390 continue;
3391 switch (Visitor(Cursor, Parent, ClientData)) {
3392 case CXChildVisit_Break:
3393 return true;
3394 case CXChildVisit_Continue:
3395 break;
3396 case CXChildVisit_Recurse:
3397 if (PostChildrenVisitor)
3398 WL.push_back(PostChildrenVisit(nullptr, Cursor));
3399 EnqueueWorkList(WL, S);
3400 break;
3401 }
3402 continue;
3403 }
3404 case VisitorJob::MemberExprPartsKind: {
3405 // Handle the other pieces in the MemberExpr besides the base.
3406 const MemberExpr *M = cast<MemberExprParts>(&LI)->get();
3407
3408 // Visit the nested-name-specifier
3409 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
3410 if (VisitNestedNameSpecifierLoc(QualifierLoc))
3411 return true;
3412
3413 // Visit the declaration name.
3414 if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
3415 return true;
3416
3417 // Visit the explicitly-specified template arguments, if any.
3418 if (M->hasExplicitTemplateArgs()) {
3419 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
3420 *ArgEnd = Arg + M->getNumTemplateArgs();
3421 Arg != ArgEnd; ++Arg) {
3422 if (VisitTemplateArgumentLoc(*Arg))
3423 return true;
3424 }
3425 }
3426 continue;
3427 }
3428 case VisitorJob::DeclRefExprPartsKind: {
3429 const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
3430 // Visit nested-name-specifier, if present.
3431 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
3432 if (VisitNestedNameSpecifierLoc(QualifierLoc))
3433 return true;
3434 // Visit declaration name.
3435 if (VisitDeclarationNameInfo(DR->getNameInfo()))
3436 return true;
3437 continue;
3438 }
3439 case VisitorJob::OverloadExprPartsKind: {
3440 const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
3441 // Visit the nested-name-specifier.
3442 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
3443 if (VisitNestedNameSpecifierLoc(QualifierLoc))
3444 return true;
3445 // Visit the declaration name.
3446 if (VisitDeclarationNameInfo(O->getNameInfo()))
3447 return true;
3448 // Visit the overloaded declaration reference.
3449 if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
3450 return true;
3451 continue;
3452 }
3453 case VisitorJob::SizeOfPackExprPartsKind: {
3454 const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
3455 NamedDecl *Pack = E->getPack();
3456 if (isa<TemplateTypeParmDecl>(Pack)) {
3457 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
3458 E->getPackLoc(), TU)))
3459 return true;
3460
3461 continue;
3462 }
3463
3464 if (isa<TemplateTemplateParmDecl>(Pack)) {
3465 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
3466 E->getPackLoc(), TU)))
3467 return true;
3468
3469 continue;
3470 }
3471
3472 // Non-type template parameter packs and function parameter packs are
3473 // treated like DeclRefExpr cursors.
3474 continue;
3475 }
3476
3477 case VisitorJob::LambdaExprPartsKind: {
3478 // Visit non-init captures.
3479 const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
3480 for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
3481 CEnd = E->explicit_capture_end();
3482 C != CEnd; ++C) {
3483 if (!C->capturesVariable())
3484 continue;
3485 // TODO: handle structured bindings here ?
3486 if (!isa<VarDecl>(C->getCapturedVar()))
3487 continue;
3488 if (Visit(MakeCursorVariableRef(cast<VarDecl>(C->getCapturedVar()),
3489 C->getLocation(), TU)))
3490 return true;
3491 }
3492 // Visit init captures
3493 for (auto InitExpr : E->capture_inits()) {
3494 if (InitExpr && Visit(InitExpr))
3495 return true;
3496 }
3497
3498 TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
3499 // Visit parameters and return type, if present.
3500 if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
3501 if (E->hasExplicitParameters()) {
3502 // Visit parameters.
3503 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
3504 if (Visit(MakeCXCursor(Proto.getParam(I), TU)))
3505 return true;
3506 }
3507 if (E->hasExplicitResultType()) {
3508 // Visit result type.
3509 if (Visit(Proto.getReturnLoc()))
3510 return true;
3511 }
3512 }
3513 break;
3514 }
3515
3516 case VisitorJob::ConceptSpecializationExprVisitKind: {
3517 const ConceptSpecializationExpr *E =
3518 cast<ConceptSpecializationExprVisit>(&LI)->get();
3519 if (NestedNameSpecifierLoc QualifierLoc =
3520 E->getNestedNameSpecifierLoc()) {
3521 if (VisitNestedNameSpecifierLoc(QualifierLoc))
3522 return true;
3523 }
3524
3525 if (E->getNamedConcept() &&
3526 Visit(MakeCursorTemplateRef(E->getNamedConcept(),
3527 E->getConceptNameLoc(), TU)))
3528 return true;
3529
3530 if (auto Args = E->getTemplateArgsAsWritten()) {
3531 for (const auto &Arg : Args->arguments()) {
3532 if (VisitTemplateArgumentLoc(Arg))
3533 return true;
3534 }
3535 }
3536 break;
3537 }
3538
3539 case VisitorJob::RequiresExprVisitKind: {
3540 const RequiresExpr *E = cast<RequiresExprVisit>(&LI)->get();
3541 for (const concepts::Requirement *R : E->getRequirements())
3542 VisitConceptRequirement(*R);
3543 break;
3544 }
3545
3546 case VisitorJob::PostChildrenVisitKind:
3547 if (PostChildrenVisitor(Parent, ClientData))
3548 return true;
3549 break;
3550 }
3551 }
3552 return false;
3553}
3554
3555bool CursorVisitor::Visit(const Stmt *S) {
3556 VisitorWorkList *WL = nullptr;
3557 if (!WorkListFreeList.empty()) {
3558 WL = WorkListFreeList.back();
3559 WL->clear();
3560 WorkListFreeList.pop_back();
3561 } else {
3562 WL = new VisitorWorkList();
3563 WorkListCache.push_back(WL);
3564 }
3565 EnqueueWorkList(*WL, S);
3566 bool result = RunVisitorWorkList(*WL);
3567 WorkListFreeList.push_back(WL);
3568 return result;
3569}
3570
3571namespace {
3572typedef SmallVector<SourceRange, 4> RefNamePieces;
3573RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
3574 const DeclarationNameInfo &NI, SourceRange QLoc,
3575 const SourceRange *TemplateArgsLoc = nullptr) {
3576 const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
3577 const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
3578 const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
3579
3580 const DeclarationName::NameKind Kind = NI.getName().getNameKind();
3581
3582 RefNamePieces Pieces;
3583
3584 if (WantQualifier && QLoc.isValid())
3585 Pieces.push_back(QLoc);
3586
3587 if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
3588 Pieces.push_back(NI.getLoc());
3589
3590 if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid())
3591 Pieces.push_back(*TemplateArgsLoc);
3592
3593 if (Kind == DeclarationName::CXXOperatorName) {
3594 Pieces.push_back(NI.getInfo().getCXXOperatorNameBeginLoc());
3595 Pieces.push_back(NI.getInfo().getCXXOperatorNameEndLoc());
3596 }
3597
3598 if (WantSinglePiece) {
3599 SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
3600 Pieces.clear();
3601 Pieces.push_back(R);
3602 }
3603
3604 return Pieces;
3605}
3606} // namespace
3607
3608//===----------------------------------------------------------------------===//
3609// Misc. API hooks.
3610//===----------------------------------------------------------------------===//
3611
3612namespace {
3613struct RegisterFatalErrorHandler {
3614 RegisterFatalErrorHandler() {
3615 clang_install_aborting_llvm_fatal_error_handler();
3616 }
3617};
3618} // namespace
3619
3620static llvm::ManagedStatic<RegisterFatalErrorHandler>
3621 RegisterFatalErrorHandlerOnce;
3622
3623CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
3624 int displayDiagnostics) {
3625 // We use crash recovery to make some of our APIs more reliable, implicitly
3626 // enable it.
3627 if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
3628 llvm::CrashRecoveryContext::Enable();
3629
3630 // Look through the managed static to trigger construction of the managed
3631 // static which registers our fatal error handler. This ensures it is only
3632 // registered once.
3633 (void)*RegisterFatalErrorHandlerOnce;
3634
3635 // Initialize targets for clang module support.
3636 llvm::InitializeAllTargets();
3637 llvm::InitializeAllTargetMCs();
3638 llvm::InitializeAllAsmPrinters();
3639 llvm::InitializeAllAsmParsers();
3640
3641 CIndexer *CIdxr = new CIndexer();
3642
3643 if (excludeDeclarationsFromPCH)
3644 CIdxr->setOnlyLocalDecls();
3645 if (displayDiagnostics)
3646 CIdxr->setDisplayDiagnostics();
3647
3648 if (getenv("LIBCLANG_BGPRIO_INDEX"))
3649 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
3650 CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
3651 if (getenv("LIBCLANG_BGPRIO_EDIT"))
3652 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
3653 CXGlobalOpt_ThreadBackgroundPriorityForEditing);
3654
3655 return CIdxr;
3656}
3657
3658void clang_disposeIndex(CXIndex CIdx) {
3659 if (CIdx)
3660 delete static_cast<CIndexer *>(CIdx);
3661}
3662
3663void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
3664 if (CIdx)
3665 static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
3666}
3667
3668unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
3669 if (CIdx)
3670 return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
3671 return 0;
3672}
3673
3674void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx,
3675 const char *Path) {
3676 if (CIdx)
3677 static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : "");
3678}
3679
3680void clang_toggleCrashRecovery(unsigned isEnabled) {
3681 if (isEnabled)
3682 llvm::CrashRecoveryContext::Enable();
3683 else
3684 llvm::CrashRecoveryContext::Disable();
3685}
3686
3687CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
3688 const char *ast_filename) {
3689 CXTranslationUnit TU;
3690 enum CXErrorCode Result =
3691 clang_createTranslationUnit2(CIdx, ast_filename, &TU);
3692 (void)Result;
3693 assert((TU && Result == CXError_Success) ||(static_cast <bool> ((TU && Result == CXError_Success
) || (!TU && Result != CXError_Success)) ? void (0) :
__assert_fail ("(TU && Result == CXError_Success) || (!TU && Result != CXError_Success)"
, "clang/tools/libclang/CIndex.cpp", 3694, __extension__ __PRETTY_FUNCTION__
))
3694 (!TU && Result != CXError_Success))(static_cast <bool> ((TU && Result == CXError_Success
) || (!TU && Result != CXError_Success)) ? void (0) :
__assert_fail ("(TU && Result == CXError_Success) || (!TU && Result != CXError_Success)"
, "clang/tools/libclang/CIndex.cpp", 3694, __extension__ __PRETTY_FUNCTION__
))
;
3695 return TU;
3696}
3697
3698enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
3699 const char *ast_filename,
3700 CXTranslationUnit *out_TU) {
3701 if (out_TU)
3702 *out_TU = nullptr;
3703
3704 if (!CIdx || !ast_filename || !out_TU)
3705 return CXError_InvalidArguments;
3706
3707 LOG_FUNC_SECTIONif (clang::cxindex::LogRef Log = clang::cxindex::Logger::make
(__func__))
{ *Log << ast_filename; }
3708
3709 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
3710 FileSystemOptions FileSystemOpts;
3711
3712 IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
3713 CompilerInstance::createDiagnostics(new DiagnosticOptions());
3714 std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
3715 ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
3716 ASTUnit::LoadEverything, Diags, FileSystemOpts, /*UseDebugInfo=*/false,
3717 CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
3718 /*AllowASTWithCompilerErrors=*/true,
3719 /*UserFilesAreVolatile=*/true);
3720 *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));
3721 return *out_TU ? CXError_Success : CXError_Failure;
3722}
3723
3724unsigned clang_defaultEditingTranslationUnitOptions() {
3725 return CXTranslationUnit_PrecompiledPreamble |
3726 CXTranslationUnit_CacheCompletionResults;
3727}
3728
3729CXTranslationUnit clang_createTranslationUnitFromSourceFile(
3730 CXIndex CIdx, const char *source_filename, int num_command_line_args,
3731 const char *const *command_line_args, unsigned num_unsaved_files,
3732 struct CXUnsavedFile *unsaved_files) {
3733 unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
3734 return clang_parseTranslationUnit(CIdx, source_filename, command_line_args,
3735 num_command_line_args, unsaved_files,
3736 num_unsaved_files, Options);
3737}
3738
3739static CXErrorCode
3740clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename,
3741 const char *const *command_line_args,
3742 int num_command_line_args,
3743 ArrayRef<CXUnsavedFile> unsaved_files,
3744 unsigned options, CXTranslationUnit *out_TU) {
3745 // Set up the initial return values.
3746 if (out_TU)
3747 *out_TU = nullptr;
3748
3749 // Check arguments.
3750 if (!CIdx || !out_TU)
3751 return CXError_InvalidArguments;
3752
3753 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
3754
3755 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
3756 setThreadBackgroundPriority();
3757
3758 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
3759 bool CreatePreambleOnFirstParse =
3760 options & CXTranslationUnit_CreatePreambleOnFirstParse;
3761 // FIXME: Add a flag for modules.
3762 TranslationUnitKind TUKind = (options & (CXTranslationUnit_Incomplete |
3763 CXTranslationUnit_SingleFileParse))
3764 ? TU_Prefix
3765 : TU_Complete;
3766 bool CacheCodeCompletionResults =
3767 options & CXTranslationUnit_CacheCompletionResults;
3768 bool IncludeBriefCommentsInCodeCompletion =
3769 options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
3770 bool SingleFileParse = options & CXTranslationUnit_SingleFileParse;
3771 bool ForSerialization = options & CXTranslationUnit_ForSerialization;
3772 bool RetainExcludedCB =
3773 options & CXTranslationUnit_RetainExcludedConditionalBlocks;
3774 SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None;
3775 if (options & CXTranslationUnit_SkipFunctionBodies) {
3776 SkipFunctionBodies =
3777 (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble)
3778 ? SkipFunctionBodiesScope::Preamble
3779 : SkipFunctionBodiesScope::PreambleAndMainFile;
3780 }
3781
3782 // Configure the diagnostics.
3783 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
3784 CompilerInstance::createDiagnostics(new DiagnosticOptions));
3785
3786 if (options & CXTranslationUnit_KeepGoing)
3787 Diags->setFatalsAsError(true);
3788
3789 CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
3790 if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
3791 CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
3792
3793 // Recover resources if we crash before exiting this function.
3794 llvm::CrashRecoveryContextCleanupRegistrar<
3795 DiagnosticsEngine,
3796 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
3797 DiagCleanup(Diags.get());
3798
3799 std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
3800 new std::vector<ASTUnit::RemappedFile>());
3801
3802 // Recover resources if we crash before exiting this function.
3803 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
3804 RemappedCleanup(RemappedFiles.get());
3805
3806 for (auto &UF : unsaved_files) {
3807 std::unique_ptr<llvm::MemoryBuffer> MB =
3808 llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
3809 RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
3810 }
3811
3812 std::unique_ptr<std::vector<const char *>> Args(
3813 new std::vector<const char *>());
3814
3815 // Recover resources if we crash before exiting this method.
3816 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char *>>
3817 ArgsCleanup(Args.get());
3818
3819 // Since the Clang C library is primarily used by batch tools dealing with
3820 // (often very broken) source code, where spell-checking can have a
3821 // significant negative impact on performance (particularly when
3822 // precompiled headers are involved), we disable it by default.
3823 // Only do this if we haven't found a spell-checking-related argument.
3824 bool FoundSpellCheckingArgument = false;
3825 for (int I = 0; I != num_command_line_args; ++I) {
3826 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
3827 strcmp(command_line_args[I], "-fspell-checking") == 0) {
3828 FoundSpellCheckingArgument = true;
3829 break;
3830 }
3831 }
3832 Args->insert(Args->end(), command_line_args,
3833 command_line_args + num_command_line_args);
3834
3835 if (!FoundSpellCheckingArgument)
3836 Args->insert(Args->begin() + 1, "-fno-spell-checking");
3837
3838 // The 'source_filename' argument is optional. If the caller does not
3839 // specify it then it is assumed that the source file is specified
3840 // in the actual argument list.
3841 // Put the source file after command_line_args otherwise if '-x' flag is
3842 // present it will be unused.
3843 if (source_filename)
3844 Args->push_back(source_filename);
3845
3846 // Do we need the detailed preprocessing record?
3847 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
3848 Args->push_back("-Xclang");
3849 Args->push_back("-detailed-preprocessing-record");
3850 }
3851
3852 // Suppress any editor placeholder diagnostics.
3853 Args->push_back("-fallow-editor-placeholders");
3854
3855 unsigned NumErrors = Diags->getClient()->getNumErrors();
3856 std::unique_ptr<ASTUnit> ErrUnit;
3857 // Unless the user specified that they want the preamble on the first parse
3858 // set it up to be created on the first reparse. This makes the first parse
3859 // faster, trading for a slower (first) reparse.
3860 unsigned PrecompilePreambleAfterNParses =
3861 !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse;
3862
3863 LibclangInvocationReporter InvocationReporter(
3864 *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation,
3865 options, llvm::makeArrayRef(*Args), /*InvocationArgs=*/None,
3866 unsaved_files);
3867 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine(
3868 Args->data(), Args->data() + Args->size(),
3869 CXXIdx->getPCHContainerOperations(), Diags,
3870 CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(),
3871 CaptureDiagnostics, *RemappedFiles.get(),
3872 /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses,
3873 TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
3874 /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse,
3875 /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedCB,
3876 CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(),
3877 &ErrUnit));
3878
3879 // Early failures in LoadFromCommandLine may return with ErrUnit unset.
3880 if (!Unit && !ErrUnit)
3881 return CXError_ASTReadError;
3882
3883 if (NumErrors != Diags->getClient()->getNumErrors()) {
3884 // Make sure to check that 'Unit' is non-NULL.
3885 if (CXXIdx->getDisplayDiagnostics())
3886 printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get());
3887 }
3888
3889 if (isASTReadError(Unit ? Unit.get() : ErrUnit.get()))
3890 return CXError_ASTReadError;
3891
3892 *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit));
3893 if (CXTranslationUnitImpl *TU = *out_TU) {
3894 TU->ParsingOptions = options;
3895 TU->Arguments.reserve(Args->size());
3896 for (const char *Arg : *Args)
3897 TU->Arguments.push_back(Arg);
3898 return CXError_Success;
3899 }
3900 return CXError_Failure;
3901}
3902
3903CXTranslationUnit
3904clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
3905 const char *const *command_line_args,
3906 int num_command_line_args,
3907 struct CXUnsavedFile *unsaved_files,
3908 unsigned num_unsaved_files, unsigned options) {
3909 CXTranslationUnit TU;
3910 enum CXErrorCode Result = clang_parseTranslationUnit2(
3911 CIdx, source_filename, command_line_args, num_command_line_args,
3912 unsaved_files, num_unsaved_files, options, &TU);
3913 (void)Result;
3914 assert((TU && Result == CXError_Success) ||(static_cast <bool> ((TU && Result == CXError_Success
) || (!TU && Result != CXError_Success)) ? void (0) :
__assert_fail ("(TU && Result == CXError_Success) || (!TU && Result != CXError_Success)"
, "clang/tools/libclang/CIndex.cpp", 3915, __extension__ __PRETTY_FUNCTION__
))
3915 (!TU && Result != CXError_Success))(static_cast <bool> ((TU && Result == CXError_Success
) || (!TU && Result != CXError_Success)) ? void (0) :
__assert_fail ("(TU && Result == CXError_Success) || (!TU && Result != CXError_Success)"
, "clang/tools/libclang/CIndex.cpp", 3915, __extension__ __PRETTY_FUNCTION__
))
;
3916 return TU;
3917}
3918
3919enum CXErrorCode clang_parseTranslationUnit2(
3920 CXIndex CIdx, const char *source_filename,
3921 const char *const *command_line_args, int num_command_line_args,
3922 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
3923 unsigned options, CXTranslationUnit *out_TU) {
3924 noteBottomOfStack();
3925 SmallVector<const char *, 4> Args;
3926 Args.push_back("clang");
3927 Args.append(command_line_args, command_line_args + num_command_line_args);
3928 return clang_parseTranslationUnit2FullArgv(
3929 CIdx, source_filename, Args.data(), Args.size(), unsaved_files,
3930 num_unsaved_files, options, out_TU);
3931}
3932
3933enum CXErrorCode clang_parseTranslationUnit2FullArgv(
3934 CXIndex CIdx, const char *source_filename,
3935 const char *const *command_line_args, int num_command_line_args,
3936 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
3937 unsigned options, CXTranslationUnit *out_TU) {
3938 LOG_FUNC_SECTIONif (clang::cxindex::LogRef Log = clang::cxindex::Logger::make
(__func__))
{
3939 *Log << source_filename << ": ";
3940 for (int i = 0; i != num_command_line_args; ++i)
3941 *Log << command_line_args[i] << " ";
3942 }
3943
3944 if (num_unsaved_files && !unsaved_files)
3945 return CXError_InvalidArguments;
3946
3947 CXErrorCode result = CXError_Failure;
3948 auto ParseTranslationUnitImpl = [=, &result] {
3949 noteBottomOfStack();
3950 result = clang_parseTranslationUnit_Impl(
3951 CIdx, source_filename, command_line_args, num_command_line_args,
3952 llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU);
3953 };
3954
3955 llvm::CrashRecoveryContext CRC;
3956
3957 if (!RunSafely(CRC, ParseTranslationUnitImpl)) {
3958 fprintf(stderrstderr, "libclang: crash detected during parsing: {\n");
3959 fprintf(stderrstderr, " 'source_filename' : '%s'\n", source_filename);
3960 fprintf(stderrstderr, " 'command_line_args' : [");
3961 for (int i = 0; i != num_command_line_args; ++i) {
3962 if (i)
3963 fprintf(stderrstderr, ", ");
3964 fprintf(stderrstderr, "'%s'", command_line_args[i]);
3965 }
3966 fprintf(stderrstderr, "],\n");
3967 fprintf(stderrstderr, " 'unsaved_files' : [");
3968 for (unsigned i = 0; i != num_unsaved_files; ++i) {
3969 if (i)
3970 fprintf(stderrstderr, ", ");
3971 fprintf(stderrstderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
3972 unsaved_files[i].Length);
3973 }
3974 fprintf(stderrstderr, "],\n");
3975 fprintf(stderrstderr, " 'options' : %d,\n", options);
3976 fprintf(stderrstderr, "}\n");
3977
3978 return CXError_Crashed;
3979 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
3980 if (CXTranslationUnit *TU = out_TU)
3981 PrintLibclangResourceUsage(*TU);
3982 }
3983
3984 return result;
3985}
3986
3987CXString clang_Type_getObjCEncoding(CXType CT) {
3988 CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]);
3989 ASTContext &Ctx = getASTUnit(tu)->getASTContext();
3990 std::string encoding;
3991 Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), encoding);
3992
3993 return cxstring::createDup(encoding);
3994}
3995
3996static const IdentifierInfo *getMacroIdentifier(CXCursor C) {
3997 if (C.kind == CXCursor_MacroDefinition) {
3998 if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C))
3999 return MDR->getName();
4000 } else if (C.kind == CXCursor_MacroExpansion) {
4001 MacroExpansionCursor ME = getCursorMacroExpansion(C);
4002 return ME.getName();
4003 }
4004 return nullptr;
4005}
4006
4007unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) {
4008 const IdentifierInfo *II = getMacroIdentifier(C);
4009 if (!II) {
4010 return false;
4011 }
4012 ASTUnit *ASTU = getCursorASTUnit(C);
4013 Preprocessor &PP = ASTU->getPreprocessor();
4014 if (const MacroInfo *MI = PP.getMacroInfo(II))
4015 return MI->isFunctionLike();
4016 return false;
4017}
4018
4019unsigned clang_Cursor_isMacroBuiltin(CXCursor C) {
4020 const IdentifierInfo *II = getMacroIdentifier(C);
4021 if (!II) {
4022 return false;
4023 }
4024 ASTUnit *ASTU = getCursorASTUnit(C);
4025 Preprocessor &PP = ASTU->getPreprocessor();
4026 if (const MacroInfo *MI = PP.getMacroInfo(II))
4027 return MI->isBuiltinMacro();
4028 return false;
4029}
4030
4031unsigned clang_Cursor_isFunctionInlined(CXCursor C) {
4032 const Decl *D = getCursorDecl(C);
4033 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
4034 if (!FD) {
4035 return false;
4036 }
4037 return FD->isInlined();
4038}
4039
4040static StringLiteral *getCFSTR_value(CallExpr *callExpr) {
4041 if (callExpr->getNumArgs() != 1) {
4042 return nullptr;
4043 }
4044
4045 StringLiteral *S = nullptr;
4046 auto *arg = callExpr->getArg(0);
4047 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
4048 ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg);
4049 auto *subExpr = I->getSubExprAsWritten();
4050
4051 if (subExpr->getStmtClass() != Stmt::StringLiteralClass) {
4052 return nullptr;
4053 }
4054
4055 S = static_cast<StringLiteral *>(I->getSubExprAsWritten());
4056 } else if (arg->getStmtClass() == Stmt::StringLiteralClass) {
4057 S = static_cast<StringLiteral *>(callExpr->getArg(0));
4058 } else {
4059 return nullptr;
4060 }
4061 return S;
4062}
4063
4064struct ExprEvalResult {
4065 CXEvalResultKind EvalType;
4066 union {
4067 unsigned long long unsignedVal;
4068 long long intVal;
4069 double floatVal;
4070 char *stringVal;
4071 } EvalData;
4072 bool IsUnsignedInt;
4073 ~ExprEvalResult() {
4074 if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float &&
4075 EvalType != CXEval_Int) {
4076 delete[] EvalData.stringVal;
4077 }
4078 }
4079};
4080
4081void clang_EvalResult_dispose(CXEvalResult E) {
4082 delete static_cast<ExprEvalResult *>(E);
4083}
4084
4085CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) {
4086 if (!E) {
4087 return CXEval_UnExposed;
4088 }
4089 return ((ExprEvalResult *)E)->EvalType;
4090}
4091
4092int clang_EvalResult_getAsInt(CXEvalResult E) {
4093 return clang_EvalResult_getAsLongLong(E);
4094}
4095
4096long long clang_EvalResult_getAsLongLong(CXEvalResult E) {
4097 if (!E) {
4098 return 0;
4099 }
4100 ExprEvalResult *Result = (ExprEvalResult *)E;
4101 if (Result->IsUnsignedInt)
4102 return Result->EvalData.unsignedVal;
4103 return Result->EvalData.intVal;
4104}
4105
4106unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) {
4107 return ((ExprEvalResult *)E)->IsUnsignedInt;
4108}
4109
4110unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) {
4111 if (!E) {
4112 return 0;
4113 }
4114
4115 ExprEvalResult *Result = (ExprEvalResult *)E;
4116 if (Result->IsUnsignedInt)
4117 return Result->EvalData.unsignedVal;
4118 return Result->EvalData.intVal;
4119}
4120
4121double clang_EvalResult_getAsDouble(CXEvalResult E) {
4122 if (!E) {
4123 return 0;
4124 }
4125 return ((ExprEvalResult *)E)->EvalData.floatVal;
4126}
4127
4128const char *clang_EvalResult_getAsStr(CXEvalResult E) {
4129 if (!E) {
4130 return nullptr;
4131 }
4132 return ((ExprEvalResult *)E)->EvalData.stringVal;
4133}
4134
4135static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
4136 Expr::EvalResult ER;
4137 ASTContext &ctx = getCursorContext(C);
4138 if (!expr)
4139 return nullptr;
4140
4141 expr = expr->IgnoreParens();
4142 if (expr->isValueDependent())
4143 return nullptr;
4144 if (!expr->EvaluateAsRValue(ER, ctx))
4145 return nullptr;
4146
4147 QualType rettype;
4148 CallExpr *callExpr;
4149 auto result = std::make_unique<ExprEvalResult>();
4150 result->EvalType = CXEval_UnExposed;
4151 result->IsUnsignedInt = false;
4152
4153 if (ER.Val.isInt()) {
4154 result->EvalType = CXEval_Int;
4155
4156 auto &val = ER.Val.getInt();
4157 if (val.isUnsigned()) {
4158 result->IsUnsignedInt = true;
4159 result->EvalData.unsignedVal = val.getZExtValue();
4160 } else {
4161 result->EvalData.intVal = val.getExtValue();
4162 }
4163
4164 return result.release();
4165 }
4166
4167 if (ER.Val.isFloat()) {
4168 llvm::SmallVector<char, 100> Buffer;
4169 ER.Val.getFloat().toString(Buffer);
4170 std::string floatStr(Buffer.data(), Buffer.size());
4171 result->EvalType = CXEval_Float;
4172 bool ignored;
4173 llvm::APFloat apFloat = ER.Val.getFloat();
4174 apFloat.convert(llvm::APFloat::IEEEdouble(),
4175 llvm::APFloat::rmNearestTiesToEven, &ignored);
4176 result->EvalData.floatVal = apFloat.convertToDouble();
4177 return result.release();
4178 }
4179
4180 if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
4181 const auto *I = cast<ImplicitCastExpr>(expr);
4182 auto *subExpr = I->getSubExprAsWritten();
4183 if (subExpr->getStmtClass() == Stmt::StringLiteralClass ||
4184 subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) {
4185 const StringLiteral *StrE = nullptr;
4186 const ObjCStringLiteral *ObjCExpr;
4187 ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr);
4188
4189 if (ObjCExpr) {
4190 StrE = ObjCExpr->getString();
4191 result->EvalType = CXEval_ObjCStrLiteral;
4192 } else {
4193 StrE = cast<StringLiteral>(I->getSubExprAsWritten());
4194 result->EvalType = CXEval_StrLiteral;
4195 }
4196
4197 std::string strRef(StrE->getString().str());
4198 result->EvalData.stringVal = new char[strRef.size() + 1];
4199 strncpy((char *)result->EvalData.stringVal, strRef.c_str(),
4200 strRef.size());
4201 result->EvalData.stringVal[strRef.size()] = '\0';
4202 return result.release();
4203 }
4204 } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass ||
4205 expr->getStmtClass() == Stmt::StringLiteralClass) {
4206 const StringLiteral *StrE = nullptr;
4207 const ObjCStringLiteral *ObjCExpr;
4208 ObjCExpr = dyn_cast<ObjCStringLiteral>(expr);
4209
4210 if (ObjCExpr) {
4211 StrE = ObjCExpr->getString();
4212 result->EvalType = CXEval_ObjCStrLiteral;
4213 } else {
4214 StrE = cast<StringLiteral>(expr);
4215 result->EvalType = CXEval_StrLiteral;
4216 }
4217
4218 std::string strRef(StrE->getString().str());
4219 result->EvalData.stringVal = new char[strRef.size() + 1];
4220 strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size());
4221 result->EvalData.stringVal[strRef.size()] = '\0';
4222 return result.release();
4223 }
4224
4225 if (expr->getStmtClass() == Stmt::CStyleCastExprClass) {
4226 CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr);
4227
4228 rettype = CC->getType();
4229 if (rettype.getAsString() == "CFStringRef" &&
4230 CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) {
4231
4232 callExpr = static_cast<CallExpr *>(CC->getSubExpr());
4233 StringLiteral *S = getCFSTR_value(callExpr);
4234 if (S) {
4235 std::string strLiteral(S->getString().str());
4236 result->EvalType = CXEval_CFStr;
4237
4238 result->EvalData.stringVal = new char[strLiteral.size() + 1];
4239 strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
4240 strLiteral.size());
4241 result->EvalData.stringVal[strLiteral.size()] = '\0';
4242 return result.release();
4243 }
4244 }
4245
4246 } else if (expr->getStmtClass() == Stmt::CallExprClass) {
4247 callExpr = static_cast<CallExpr *>(expr);
4248 rettype = callExpr->getCallReturnType(ctx);
4249
4250 if (rettype->isVectorType() || callExpr->getNumArgs() > 1)
4251 return nullptr;
4252
4253 if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) {
4254 if (callExpr->getNumArgs() == 1 &&
4255 !callExpr->getArg(0)->getType()->isIntegralType(ctx))
4256 return nullptr;
4257 } else if (rettype.getAsString() == "CFStringRef") {
4258
4259 StringLiteral *S = getCFSTR_value(callExpr);
4260 if (S) {
4261 std::string strLiteral(S->getString().str());
4262 result->EvalType = CXEval_CFStr;
4263 result->EvalData.stringVal = new char[strLiteral.size() + 1];
4264 strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
4265 strLiteral.size());
4266 result->EvalData.stringVal[strLiteral.size()] = '\0';
4267 return result.release();
4268 }
4269 }
4270 } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) {
4271 DeclRefExpr *D = static_cast<DeclRefExpr *>(expr);
4272 ValueDecl *V = D->getDecl();
4273 if (V->getKind() == Decl::Function) {
4274 std::string strName = V->getNameAsString();
4275 result->EvalType = CXEval_Other;
4276 result->EvalData.stringVal = new char[strName.size() + 1];
4277 strncpy(result->EvalData.stringVal, strName.c_str(), strName.size());
4278 result->EvalData.stringVal[strName.size()] = '\0';
4279 return result.release();
4280 }
4281 }
4282
4283 return nullptr;
4284}
4285
4286static const Expr *evaluateDeclExpr(const Decl *D) {
4287 if (!D)
4288 return nullptr;
4289 if (auto *Var = dyn_cast<VarDecl>(D))
4290 return Var->getInit();
4291 else if (auto *Field = dyn_cast<FieldDecl>(D))
4292 return Field->getInClassInitializer();
4293 return nullptr;
4294}
4295
4296static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
4297 assert(CS && "invalid compound statement")(static_cast <bool> (CS && "invalid compound statement"
) ? void (0) : __assert_fail ("CS && \"invalid compound statement\""
, "clang/tools/libclang/CIndex.cpp", 4297, __extension__ __PRETTY_FUNCTION__
))
;
4298 for (auto *bodyIterator : CS->body()) {
4299 if (const auto *E = dyn_cast<Expr>(bodyIterator))
4300 return E;
4301 }
4302 return nullptr;
4303}
4304
4305CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
4306 const Expr *E = nullptr;
4307 if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
4308 E = evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)));
4309 else if (clang_isDeclaration(C.kind))
4310 E = evaluateDeclExpr(getCursorDecl(C));
4311 else if (clang_isExpression(C.kind))
4312 E = getCursorExpr(C);
4313 if (E)
4314 return const_cast<CXEvalResult>(
4315 reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
4316 return nullptr;
4317}
4318
4319unsigned clang_Cursor_hasAttrs(CXCursor C) {
4320 const Decl *D = getCursorDecl(C);
4321 if (!D) {
4322 return 0;
4323 }
4324
4325 if (D->hasAttrs()) {
4326 return 1;
4327 }
4328
4329 return 0;
4330}
4331unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
4332 return CXSaveTranslationUnit_None;
4333}
4334
4335static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU,
4336 const char *FileName,
4337 unsigned options) {
4338 CIndexer *CXXIdx = TU->CIdx;
4339 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
4340 setThreadBackgroundPriority();
4341
4342 bool hadError = cxtu::getASTUnit(TU)->Save(FileName);
4343 return hadError ? CXSaveError_Unknown : CXSaveError_None;
4344}
4345
4346int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
4347 unsigned options) {
4348 LOG_FUNC_SECTIONif (clang::cxindex::LogRef Log = clang::cxindex::Logger::make
(__func__))
{ *Log << TU << ' ' << FileName; }
4349
4350 if (isNotUsableTU(TU)) {
4351 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4352 return CXSaveError_InvalidTU;
4353 }
4354
4355 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4356 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4357 if (!CXXUnit->hasSema())
4358 return CXSaveError_InvalidTU;
4359
4360 CXSaveError result;
4361 auto SaveTranslationUnitImpl = [=, &result]() {
4362 result = clang_saveTranslationUnit_Impl(TU, FileName, options);
4363 };
4364
4365 if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) {
4366 SaveTranslationUnitImpl();
4367
4368 if (getenv("LIBCLANG_RESOURCE_USAGE"))
4369 PrintLibclangResourceUsage(TU);
4370
4371 return result;
4372 }
4373
4374 // We have an AST that has invalid nodes due to compiler errors.
4375 // Use a crash recovery thread for protection.
4376
4377 llvm::CrashRecoveryContext CRC;
4378
4379 if (!RunSafely(CRC, SaveTranslationUnitImpl)) {
4380 fprintf(stderrstderr, "libclang: crash detected during AST saving: {\n");
4381 fprintf(stderrstderr, " 'filename' : '%s'\n", FileName);
4382 fprintf(stderrstderr, " 'options' : %d,\n", options);
4383 fprintf(stderrstderr, "}\n");
4384
4385 return CXSaveError_Unknown;
4386
4387 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
4388 PrintLibclangResourceUsage(TU);
4389 }
4390
4391 return result;
4392}
4393
4394void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
4395 if (CTUnit) {
4396 // If the translation unit has been marked as unsafe to free, just discard
4397 // it.
4398 ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
4399 if (Unit && Unit->isUnsafeToFree())
4400 return;
4401
4402 delete cxtu::getASTUnit(CTUnit);
4403 delete CTUnit->StringPool;
4404 delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
4405 disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
4406 delete CTUnit->CommentToXML;
4407 delete CTUnit;
4408 }
4409}
4410
4411unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) {
4412 if (CTUnit) {
4413 ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
4414
4415 if (Unit && Unit->isUnsafeToFree())
4416 return false;
4417
4418 Unit->ResetForParse();
4419 return true;
4420 }
4421
4422 return false;
4423}
4424
4425unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
4426 return CXReparse_None;
4427}
4428
4429static CXErrorCode
4430clang_reparseTranslationUnit_Impl(CXTranslationUnit TU,
4431 ArrayRef<CXUnsavedFile> unsaved_files,
4432 unsigned options) {
4433 // Check arguments.
4434 if (isNotUsableTU(TU)) {
4435 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4436 return CXError_InvalidArguments;
4437 }
4438
4439 // Reset the associated diagnostics.
4440 delete static_cast<CXDiagnosticSetImpl *>(TU->Diagnostics);
4441 TU->Diagnostics = nullptr;
4442
4443 CIndexer *CXXIdx = TU->CIdx;
4444 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
4445 setThreadBackgroundPriority();
4446
4447 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4448 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4449
4450 std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
4451 new std::vector<ASTUnit::RemappedFile>());
4452
4453 // Recover resources if we crash before exiting this function.
4454 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
4455 RemappedCleanup(RemappedFiles.get());
4456
4457 for (auto &UF : unsaved_files) {
4458 std::unique_ptr<llvm::MemoryBuffer> MB =
4459 llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
4460 RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
4461 }
4462
4463 if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(),
4464 *RemappedFiles.get()))
4465 return CXError_Success;
4466 if (isASTReadError(CXXUnit))
4467 return CXError_ASTReadError;
4468 return CXError_Failure;
4469}
4470
4471int clang_reparseTranslationUnit(CXTranslationUnit TU,
4472 unsigned num_unsaved_files,
4473 struct CXUnsavedFile *unsaved_files,
4474 unsigned options) {
4475 LOG_FUNC_SECTIONif (clang::cxindex::LogRef Log = clang::cxindex::Logger::make
(__func__))
{ *Log << TU; }
4476
4477 if (num_unsaved_files && !unsaved_files)
4478 return CXError_InvalidArguments;
4479
4480 CXErrorCode result;
4481 auto ReparseTranslationUnitImpl = [=, &result]() {
4482 result = clang_reparseTranslationUnit_Impl(
4483 TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options);
4484 };
4485
4486 llvm::CrashRecoveryContext CRC;
4487
4488 if (!RunSafely(CRC, ReparseTranslationUnitImpl)) {
4489 fprintf(stderrstderr, "libclang: crash detected during reparsing\n");
4490 cxtu::getASTUnit(TU)->setUnsafeToFree(true);
4491 return CXError_Crashed;
4492 } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
4493 PrintLibclangResourceUsage(TU);
4494
4495 return result;
4496}
4497
4498CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
4499 if (isNotUsableTU(CTUnit)) {
4500 LOG_BAD_TU(CTUnit)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
CTUnit; } } while(false)
;
4501 return cxstring::createEmpty();
4502 }
4503
4504 ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4505 return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
4506}
4507
4508CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
4509 if (isNotUsableTU(TU)) {
4510 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4511 return clang_getNullCursor();
4512 }
4513
4514 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4515 return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
4516}
4517
4518CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) {
4519 if (isNotUsableTU(CTUnit)) {
4520 LOG_BAD_TU(CTUnit)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
CTUnit; } } while(false)
;
4521 return nullptr;
4522 }
4523
4524 CXTargetInfoImpl *impl = new CXTargetInfoImpl();
4525 impl->TranslationUnit = CTUnit;
4526 return impl;
4527}
4528
4529CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) {
4530 if (!TargetInfo)
4531 return cxstring::createEmpty();
4532
4533 CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
4534 assert(!isNotUsableTU(CTUnit) &&(static_cast <bool> (!isNotUsableTU(CTUnit) && "Unexpected unusable translation unit in TargetInfo"
) ? void (0) : __assert_fail ("!isNotUsableTU(CTUnit) && \"Unexpected unusable translation unit in TargetInfo\""
, "clang/tools/libclang/CIndex.cpp", 4535, __extension__ __PRETTY_FUNCTION__
))
4535 "Unexpected unusable translation unit in TargetInfo")(static_cast <bool> (!isNotUsableTU(CTUnit) && "Unexpected unusable translation unit in TargetInfo"
) ? void (0) : __assert_fail ("!isNotUsableTU(CTUnit) && \"Unexpected unusable translation unit in TargetInfo\""
, "clang/tools/libclang/CIndex.cpp", 4535, __extension__ __PRETTY_FUNCTION__
))
;
4536
4537 ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4538 std::string Triple =
4539 CXXUnit->getASTContext().getTargetInfo().getTriple().normalize();
4540 return cxstring::createDup(Triple);
4541}
4542
4543int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) {
4544 if (!TargetInfo)
4545 return -1;
4546
4547 CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
4548 assert(!isNotUsableTU(CTUnit) &&(static_cast <bool> (!isNotUsableTU(CTUnit) && "Unexpected unusable translation unit in TargetInfo"
) ? void (0) : __assert_fail ("!isNotUsableTU(CTUnit) && \"Unexpected unusable translation unit in TargetInfo\""
, "clang/tools/libclang/CIndex.cpp", 4549, __extension__ __PRETTY_FUNCTION__
))
4549 "Unexpected unusable translation unit in TargetInfo")(static_cast <bool> (!isNotUsableTU(CTUnit) && "Unexpected unusable translation unit in TargetInfo"
) ? void (0) : __assert_fail ("!isNotUsableTU(CTUnit) && \"Unexpected unusable translation unit in TargetInfo\""
, "clang/tools/libclang/CIndex.cpp", 4549, __extension__ __PRETTY_FUNCTION__
))
;
4550
4551 ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4552 return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth();
4553}
4554
4555void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) {
4556 if (!TargetInfo)
4557 return;
4558
4559 delete TargetInfo;
4560}
4561
4562//===----------------------------------------------------------------------===//
4563// CXFile Operations.
4564//===----------------------------------------------------------------------===//
4565
4566CXString clang_getFileName(CXFile SFile) {
4567 if (!SFile)
4568 return cxstring::createNull();
4569
4570 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4571 return cxstring::createRef(FEnt->getName());
4572}
4573
4574time_t clang_getFileTime(CXFile SFile) {
4575 if (!SFile)
4576 return 0;
4577
4578 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4579 return FEnt->getModificationTime();
4580}
4581
4582CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
4583 if (isNotUsableTU(TU)) {
4584 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4585 return nullptr;
4586 }
4587
4588 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4589
4590 FileManager &FMgr = CXXUnit->getFileManager();
4591 auto File = FMgr.getFile(file_name);
4592 if (!File)
4593 return nullptr;
4594 return const_cast<FileEntry *>(*File);
4595}
4596
4597const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
4598 size_t *size) {
4599 if (isNotUsableTU(TU)) {
4600 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4601 return nullptr;
4602 }
4603
4604 const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
4605 FileID fid = SM.translateFile(static_cast<FileEntry *>(file));
4606 llvm::Optional<llvm::MemoryBufferRef> buf = SM.getBufferOrNone(fid);
4607 if (!buf) {
4608 if (size)
4609 *size = 0;
4610 return nullptr;
4611 }
4612 if (size)
4613 *size = buf->getBufferSize();
4614 return buf->getBufferStart();
4615}
4616
4617unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
4618 if (isNotUsableTU(TU)) {
4619 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
4620 return 0;
4621 }
4622
4623 if (!file)
4624 return 0;
4625
4626 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4627 FileEntry *FEnt = static_cast<FileEntry *>(file);
4628 return CXXUnit->getPreprocessor()
4629 .getHeaderSearchInfo()
4630 .isFileMultipleIncludeGuarded(FEnt);
4631}
4632
4633int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
4634 if (!file || !outID)
4635 return 1;
4636
4637 FileEntry *FEnt = static_cast<FileEntry *>(file);
4638 const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
4639 outID->data[0] = ID.getDevice();
4640 outID->data[1] = ID.getFile();
4641 outID->data[2] = FEnt->getModificationTime();
4642 return 0;
4643}
4644
4645int clang_File_isEqual(CXFile file1, CXFile file2) {
4646 if (file1 == file2)
4647 return true;
4648
4649 if (!file1 || !file2)
4650 return false;
4651
4652 FileEntry *FEnt1 = static_cast<FileEntry *>(file1);
4653 FileEntry *FEnt2 = static_cast<FileEntry *>(file2);
4654 return FEnt1->getUniqueID() == FEnt2->getUniqueID();
4655}
4656
4657CXString clang_File_tryGetRealPathName(CXFile SFile) {
4658 if (!SFile)
4659 return cxstring::createNull();
4660
4661 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4662 return cxstring::createRef(FEnt->tryGetRealPathName());
4663}
4664
4665//===----------------------------------------------------------------------===//
4666// CXCursor Operations.
4667//===----------------------------------------------------------------------===//
4668
4669static const Decl *getDeclFromExpr(const Stmt *E) {
4670 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
4671 return getDeclFromExpr(CE->getSubExpr());
4672
4673 if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
4674 return RefExpr->getDecl();
4675 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
4676 return ME->getMemberDecl();
4677 if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
4678 return RE->getDecl();
4679 if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) {
4680 if (PRE->isExplicitProperty())
4681 return PRE->getExplicitProperty();
4682 // It could be messaging both getter and setter as in:
4683 // ++myobj.myprop;
4684 // in which case prefer to associate the setter since it is less obvious
4685 // from inspecting the source that the setter is going to get called.
4686 if (PRE->isMessagingSetter())
4687 return PRE->getImplicitPropertySetter();
4688 return PRE->getImplicitPropertyGetter();
4689 }
4690 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
4691 return getDeclFromExpr(POE->getSyntacticForm());
4692 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
4693 if (Expr *Src = OVE->getSourceExpr())
4694 return getDeclFromExpr(Src);
4695
4696 if (const CallExpr *CE = dyn_cast<CallExpr>(E))
4697 return getDeclFromExpr(CE->getCallee());
4698 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
4699 if (!CE->isElidable())
4700 return CE->getConstructor();
4701 if (const CXXInheritedCtorInitExpr *CE =
4702 dyn_cast<CXXInheritedCtorInitExpr>(E))
4703 return CE->getConstructor();
4704 if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
4705 return OME->getMethodDecl();
4706
4707 if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
4708 return PE->getProtocol();
4709 if (const SubstNonTypeTemplateParmPackExpr *NTTP =
4710 dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
4711 return NTTP->getParameterPack();
4712 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
4713 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
4714 isa<ParmVarDecl>(SizeOfPack->getPack()))
4715 return SizeOfPack->getPack();
4716
4717 return nullptr;
4718}
4719
4720static SourceLocation getLocationFromExpr(const Expr *E) {
4721 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
4722 return getLocationFromExpr(CE->getSubExpr());
4723
4724 if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
4725 return /*FIXME:*/ Msg->getLeftLoc();
4726 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
4727 return DRE->getLocation();
4728 if (const MemberExpr *Member = dyn_cast<MemberExpr>(E))
4729 return Member->getMemberLoc();
4730 if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
4731 return Ivar->getLocation();
4732 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
4733 return SizeOfPack->getPackLoc();
4734 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E))
4735 return PropRef->getLocation();
4736
4737 return E->getBeginLoc();
4738}
4739
4740extern "C" {
4741
4742unsigned clang_visitChildren(CXCursor parent, CXCursorVisitor visitor,
4743 CXClientData client_data) {
4744 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
4745 /*VisitPreprocessorLast=*/false);
4746 return CursorVis.VisitChildren(parent);
4747}
4748
4749#ifndef __has_feature
4750#define0 __has_feature(x)0 0
4751#endif
4752#if __has_feature(blocks)0
4753typedef enum CXChildVisitResult (^CXCursorVisitorBlock)(CXCursor cursor,
4754 CXCursor parent);
4755
4756static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
4757 CXClientData client_data) {
4758 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
4759 return block(cursor, parent);
4760}
4761#else
4762// If we are compiled with a compiler that doesn't have native blocks support,
4763// define and call the block manually, so the
4764typedef struct _CXChildVisitResult {
4765 void *isa;
4766 int flags;
4767 int reserved;
4768 enum CXChildVisitResult (*invoke)(struct _CXChildVisitResult *, CXCursor,
4769 CXCursor);
4770} * CXCursorVisitorBlock;
4771
4772static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
4773 CXClientData client_data) {
4774 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
4775 return block->invoke(block, cursor, parent);
4776}
4777#endif
4778
4779unsigned clang_visitChildrenWithBlock(CXCursor parent,
4780 CXCursorVisitorBlock block) {
4781 return clang_visitChildren(parent, visitWithBlock, block);
4782}
4783
4784static CXString getDeclSpelling(const Decl *D) {
4785 if (!D)
4786 return cxstring::createEmpty();
4787
4788 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
4789 if (!ND) {
4790 if (const ObjCPropertyImplDecl *PropImpl =
4791 dyn_cast<ObjCPropertyImplDecl>(D))
4792 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
4793 return cxstring::createDup(Property->getIdentifier()->getName());
4794
4795 if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D))
4796 if (Module *Mod = ImportD->getImportedModule())
4797 return cxstring::createDup(Mod->getFullModuleName());
4798
4799 return cxstring::createEmpty();
4800 }
4801
4802 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
4803 return cxstring::createDup(OMD->getSelector().getAsString());
4804
4805 if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
4806 // No, this isn't the same as the code below. getIdentifier() is non-virtual
4807 // and returns different names. NamedDecl returns the class name and
4808 // ObjCCategoryImplDecl returns the category name.
4809 return cxstring::createRef(CIMP->getIdentifier()->getNameStart());
4810
4811 if (isa<UsingDirectiveDecl>(D))
4812 return cxstring::createEmpty();
4813
4814 SmallString<1024> S;
4815 llvm::raw_svector_ostream os(S);
4816 ND->printName(os);
4817
4818 return cxstring::createDup(os.str());
4819}
4820
4821CXString clang_getCursorSpelling(CXCursor C) {
4822 if (clang_isTranslationUnit(C.kind))
4823 return clang_getTranslationUnitSpelling(getCursorTU(C));
4824
4825 if (clang_isReference(C.kind)) {
4826 switch (C.kind) {
4827 case CXCursor_ObjCSuperClassRef: {
4828 const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
4829 return cxstring::createRef(Super->getIdentifier()->getNameStart());
4830 }
4831 case CXCursor_ObjCClassRef: {
4832 const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
4833 return cxstring::createRef(Class->getIdentifier()->getNameStart());
4834 }
4835 case CXCursor_ObjCProtocolRef: {
4836 const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
4837 assert(OID && "getCursorSpelling(): Missing protocol decl")(static_cast <bool> (OID && "getCursorSpelling(): Missing protocol decl"
) ? void (0) : __assert_fail ("OID && \"getCursorSpelling(): Missing protocol decl\""
, "clang/tools/libclang/CIndex.cpp", 4837, __extension__ __PRETTY_FUNCTION__
))
;
4838 return cxstring::createRef(OID->getIdentifier()->getNameStart());
4839 }
4840 case CXCursor_CXXBaseSpecifier: {
4841 const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
4842 return cxstring::createDup(B->getType().getAsString());
4843 }
4844 case CXCursor_TypeRef: {
4845 const TypeDecl *Type = getCursorTypeRef(C).first;
4846 assert(Type && "Missing type decl")(static_cast <bool> (Type && "Missing type decl"
) ? void (0) : __assert_fail ("Type && \"Missing type decl\""
, "clang/tools/libclang/CIndex.cpp", 4846, __extension__ __PRETTY_FUNCTION__
))
;
4847
4848 return cxstring::createDup(
4849 getCursorContext(C).getTypeDeclType(Type).getAsString());
4850 }
4851 case CXCursor_TemplateRef: {
4852 const TemplateDecl *Template = getCursorTemplateRef(C).first;
4853 assert(Template && "Missing template decl")(static_cast <bool> (Template && "Missing template decl"
) ? void (0) : __assert_fail ("Template && \"Missing template decl\""
, "clang/tools/libclang/CIndex.cpp", 4853, __extension__ __PRETTY_FUNCTION__
))
;
4854
4855 return cxstring::createDup(Template->getNameAsString());
4856 }
4857
4858 case CXCursor_NamespaceRef: {
4859 const NamedDecl *NS = getCursorNamespaceRef(C).first;
4860 assert(NS && "Missing namespace decl")(static_cast <bool> (NS && "Missing namespace decl"
) ? void (0) : __assert_fail ("NS && \"Missing namespace decl\""
, "clang/tools/libclang/CIndex.cpp", 4860, __extension__ __PRETTY_FUNCTION__
))
;
4861
4862 return cxstring::createDup(NS->getNameAsString());
4863 }
4864
4865 case CXCursor_MemberRef: {
4866 const FieldDecl *Field = getCursorMemberRef(C).first;
4867 assert(Field && "Missing member decl")(static_cast <bool> (Field && "Missing member decl"
) ? void (0) : __assert_fail ("Field && \"Missing member decl\""
, "clang/tools/libclang/CIndex.cpp", 4867, __extension__ __PRETTY_FUNCTION__
))
;
4868
4869 return cxstring::createDup(Field->getNameAsString());
4870 }
4871
4872 case CXCursor_LabelRef: {
4873 const LabelStmt *Label = getCursorLabelRef(C).first;
4874 assert(Label && "Missing label")(static_cast <bool> (Label && "Missing label") ?
void (0) : __assert_fail ("Label && \"Missing label\""
, "clang/tools/libclang/CIndex.cpp", 4874, __extension__ __PRETTY_FUNCTION__
))
;
4875
4876 return cxstring::createRef(Label->getName());
4877 }
4878
4879 case CXCursor_OverloadedDeclRef: {
4880 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
4881 if (const Decl *D = Storage.dyn_cast<const Decl *>()) {
4882 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
4883 return cxstring::createDup(ND->getNameAsString());
4884 return cxstring::createEmpty();
4885 }
4886 if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
4887 return cxstring::createDup(E->getName().getAsString());
4888 OverloadedTemplateStorage *Ovl =
4889 Storage.get<OverloadedTemplateStorage *>();
4890 if (Ovl->size() == 0)
4891 return cxstring::createEmpty();
4892 return cxstring::createDup((*Ovl->begin())->getNameAsString());
4893 }
4894
4895 case CXCursor_VariableRef: {
4896 const VarDecl *Var = getCursorVariableRef(C).first;
4897 assert(Var && "Missing variable decl")(static_cast <bool> (Var && "Missing variable decl"
) ? void (0) : __assert_fail ("Var && \"Missing variable decl\""
, "clang/tools/libclang/CIndex.cpp", 4897, __extension__ __PRETTY_FUNCTION__
))
;
4898
4899 return cxstring::createDup(Var->getNameAsString());
4900 }
4901
4902 default:
4903 return cxstring::createRef("<not implemented>");
4904 }
4905 }
4906
4907 if (clang_isExpression(C.kind)) {
4908 const Expr *E = getCursorExpr(C);
4909
4910 if (C.kind == CXCursor_ObjCStringLiteral ||
4911 C.kind == CXCursor_StringLiteral) {
4912 const StringLiteral *SLit;
4913 if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) {
4914 SLit = OSL->getString();
4915 } else {
4916 SLit = cast<StringLiteral>(E);
4917 }
4918 SmallString<256> Buf;
4919 llvm::raw_svector_ostream OS(Buf);
4920 SLit->outputString(OS);
4921 return cxstring::createDup(OS.str());
4922 }
4923
4924 const Decl *D = getDeclFromExpr(getCursorExpr(C));
4925 if (D)
4926 return getDeclSpelling(D);
4927 return cxstring::createEmpty();
4928 }
4929
4930 if (clang_isStatement(C.kind)) {
4931 const Stmt *S = getCursorStmt(C);
4932 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
4933 return cxstring::createRef(Label->getName());
4934
4935 return cxstring::createEmpty();
4936 }
4937
4938 if (C.kind == CXCursor_MacroExpansion)
4939 return cxstring::createRef(
4940 getCursorMacroExpansion(C).getName()->getNameStart());
4941
4942 if (C.kind == CXCursor_MacroDefinition)
4943 return cxstring::createRef(
4944 getCursorMacroDefinition(C)->getName()->getNameStart());
4945
4946 if (C.kind == CXCursor_InclusionDirective)
4947 return cxstring::createDup(getCursorInclusionDirective(C)->getFileName());
4948
4949 if (clang_isDeclaration(C.kind))
4950 return getDeclSpelling(getCursorDecl(C));
4951
4952 if (C.kind == CXCursor_AnnotateAttr) {
4953 const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
4954 return cxstring::createDup(AA->getAnnotation());
4955 }
4956
4957 if (C.kind == CXCursor_AsmLabelAttr) {
4958 const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
4959 return cxstring::createDup(AA->getLabel());
4960 }
4961
4962 if (C.kind == CXCursor_PackedAttr) {
4963 return cxstring::createRef("packed");
4964 }
4965
4966 if (C.kind == CXCursor_VisibilityAttr) {
4967 const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C));
4968 switch (AA->getVisibility()) {
4969 case VisibilityAttr::VisibilityType::Default:
4970 return cxstring::createRef("default");
4971 case VisibilityAttr::VisibilityType::Hidden:
4972 return cxstring::createRef("hidden");
4973 case VisibilityAttr::VisibilityType::Protected:
4974 return cxstring::createRef("protected");
4975 }
4976 llvm_unreachable("unknown visibility type")::llvm::llvm_unreachable_internal("unknown visibility type", "clang/tools/libclang/CIndex.cpp"
, 4976)
;
4977 }
4978
4979 return cxstring::createEmpty();
4980}
4981
4982CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, unsigned pieceIndex,
4983 unsigned options) {
4984 if (clang_Cursor_isNull(C))
4985 return clang_getNullRange();
4986
4987 ASTContext &Ctx = getCursorContext(C);
4988
4989 if (clang_isStatement(C.kind)) {
4990 const Stmt *S = getCursorStmt(C);
4991 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) {
4992 if (pieceIndex > 0)
4993 return clang_getNullRange();
4994 return cxloc::translateSourceRange(Ctx, Label->getIdentLoc());
4995 }
4996
4997 return clang_getNullRange();
4998 }
4999
5000 if (C.kind == CXCursor_ObjCMessageExpr) {
5001 if (const ObjCMessageExpr *ME =
5002 dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) {
5003 if (pieceIndex >= ME->getNumSelectorLocs())
5004 return clang_getNullRange();
5005 return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex));
5006 }
5007 }
5008
5009 if (C.kind == CXCursor_ObjCInstanceMethodDecl ||
5010 C.kind == CXCursor_ObjCClassMethodDecl) {
5011 if (const ObjCMethodDecl *MD =
5012 dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) {
5013 if (pieceIndex >= MD->getNumSelectorLocs())
5014 return clang_getNullRange();
5015 return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex));
5016 }
5017 }
5018
5019 if (C.kind == CXCursor_ObjCCategoryDecl ||
5020 C.kind == CXCursor_ObjCCategoryImplDecl) {
5021 if (pieceIndex > 0)
5022 return clang_getNullRange();
5023 if (const ObjCCategoryDecl *CD =
5024 dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C)))
5025 return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc());
5026 if (const ObjCCategoryImplDecl *CID =
5027 dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C)))
5028 return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc());
5029 }
5030
5031 if (C.kind == CXCursor_ModuleImportDecl) {
5032 if (pieceIndex > 0)
5033 return clang_getNullRange();
5034 if (const ImportDecl *ImportD =
5035 dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) {
5036 ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs();
5037 if (!Locs.empty())
5038 return cxloc::translateSourceRange(
5039 Ctx, SourceRange(Locs.front(), Locs.back()));
5040 }
5041 return clang_getNullRange();
5042 }
5043
5044 if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor ||
5045 C.kind == CXCursor_ConversionFunction ||
5046 C.kind == CXCursor_FunctionDecl) {
5047 if (pieceIndex > 0)
5048 return clang_getNullRange();
5049 if (const FunctionDecl *FD =
5050 dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) {
5051 DeclarationNameInfo FunctionName = FD->getNameInfo();
5052 return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange());
5053 }
5054 return clang_getNullRange();
5055 }
5056
5057 // FIXME: A CXCursor_InclusionDirective should give the location of the
5058 // filename, but we don't keep track of this.
5059
5060 // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation
5061 // but we don't keep track of this.
5062
5063 // FIXME: A CXCursor_AsmLabelAttr should give the location of the label
5064 // but we don't keep track of this.
5065
5066 // Default handling, give the location of the cursor.
5067
5068 if (pieceIndex > 0)
5069 return clang_getNullRange();
5070
5071 CXSourceLocation CXLoc = clang_getCursorLocation(C);
5072 SourceLocation Loc = cxloc::translateSourceLocation(CXLoc);
5073 return cxloc::translateSourceRange(Ctx, Loc);
5074}
5075
5076CXString clang_Cursor_getMangling(CXCursor C) {
5077 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5078 return cxstring::createEmpty();
5079
5080 // Mangling only works for functions and variables.
5081 const Decl *D = getCursorDecl(C);
5082 if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))
5083 return cxstring::createEmpty();
5084
5085 ASTContext &Ctx = D->getASTContext();
5086 ASTNameGenerator ASTNameGen(Ctx);
5087 return cxstring::createDup(ASTNameGen.getName(D));
5088}
5089
5090CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) {
5091 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5092 return nullptr;
5093
5094 const Decl *D = getCursorDecl(C);
5095 if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
5096 return nullptr;
5097
5098 ASTContext &Ctx = D->getASTContext();
5099 ASTNameGenerator ASTNameGen(Ctx);
5100 std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D);
5101 return cxstring::createSet(Manglings);
5102}
5103
5104CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) {
5105 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5106 return nullptr;
5107
5108 const Decl *D = getCursorDecl(C);
5109 if (!(isa<ObjCInterfaceDecl>(D) || isa<ObjCImplementationDecl>(D)))
5110 return nullptr;
5111
5112 ASTContext &Ctx = D->getASTContext();
5113 ASTNameGenerator ASTNameGen(Ctx);
5114 std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D);
5115 return cxstring::createSet(Manglings);
5116}
5117
5118CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) {
5119 if (clang_Cursor_isNull(C))
5120 return nullptr;
5121 return new PrintingPolicy(getCursorContext(C).getPrintingPolicy());
5122}
5123
5124void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) {
5125 if (Policy)
5126 delete static_cast<PrintingPolicy *>(Policy);
5127}
5128
5129unsigned
5130clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy,
5131 enum CXPrintingPolicyProperty Property) {
5132 if (!Policy)
5133 return 0;
5134
5135 PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy);
5136 switch (Property) {
5137 case CXPrintingPolicy_Indentation:
5138 return P->Indentation;
5139 case CXPrintingPolicy_SuppressSpecifiers:
5140 return P->SuppressSpecifiers;
5141 case CXPrintingPolicy_SuppressTagKeyword:
5142 return P->SuppressTagKeyword;
5143 case CXPrintingPolicy_IncludeTagDefinition:
5144 return P->IncludeTagDefinition;
5145 case CXPrintingPolicy_SuppressScope:
5146 return P->SuppressScope;
5147 case CXPrintingPolicy_SuppressUnwrittenScope:
5148 return P->SuppressUnwrittenScope;
5149 case CXPrintingPolicy_SuppressInitializers:
5150 return P->SuppressInitializers;
5151 case CXPrintingPolicy_ConstantArraySizeAsWritten:
5152 return P->ConstantArraySizeAsWritten;
5153 case CXPrintingPolicy_AnonymousTagLocations:
5154 return P->AnonymousTagLocations;
5155 case CXPrintingPolicy_SuppressStrongLifetime:
5156 return P->SuppressStrongLifetime;
5157 case CXPrintingPolicy_SuppressLifetimeQualifiers:
5158 return P->SuppressLifetimeQualifiers;
5159 case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors:
5160 return P->SuppressTemplateArgsInCXXConstructors;
5161 case CXPrintingPolicy_Bool:
5162 return P->Bool;
5163 case CXPrintingPolicy_Restrict:
5164 return P->Restrict;
5165 case CXPrintingPolicy_Alignof:
5166 return P->Alignof;
5167 case CXPrintingPolicy_UnderscoreAlignof:
5168 return P->UnderscoreAlignof;
5169 case CXPrintingPolicy_UseVoidForZeroParams:
5170 return P->UseVoidForZeroParams;
5171 case CXPrintingPolicy_TerseOutput:
5172 return P->TerseOutput;
5173 case CXPrintingPolicy_PolishForDeclaration:
5174 return P->PolishForDeclaration;
5175 case CXPrintingPolicy_Half:
5176 return P->Half;
5177 case CXPrintingPolicy_MSWChar:
5178 return P->MSWChar;
5179 case CXPrintingPolicy_IncludeNewlines:
5180 return P->IncludeNewlines;
5181 case CXPrintingPolicy_MSVCFormatting:
5182 return P->MSVCFormatting;
5183 case CXPrintingPolicy_ConstantsAsWritten:
5184 return P->ConstantsAsWritten;
5185 case CXPrintingPolicy_SuppressImplicitBase:
5186 return P->SuppressImplicitBase;
5187 case CXPrintingPolicy_FullyQualifiedName:
5188 return P->FullyQualifiedName;
5189 }
5190
5191 assert(false && "Invalid CXPrintingPolicyProperty")(static_cast <bool> (false && "Invalid CXPrintingPolicyProperty"
) ? void (0) : __assert_fail ("false && \"Invalid CXPrintingPolicyProperty\""
, "clang/tools/libclang/CIndex.cpp", 5191, __extension__ __PRETTY_FUNCTION__
))
;
5192 return 0;
5193}
5194
5195void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy,
5196 enum CXPrintingPolicyProperty Property,
5197 unsigned Value) {
5198 if (!Policy)
5199 return;
5200
5201 PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy);
5202 switch (Property) {
5203 case CXPrintingPolicy_Indentation:
5204 P->Indentation = Value;
5205 return;
5206 case CXPrintingPolicy_SuppressSpecifiers:
5207 P->SuppressSpecifiers = Value;
5208 return;
5209 case CXPrintingPolicy_SuppressTagKeyword:
5210 P->SuppressTagKeyword = Value;
5211 return;
5212 case CXPrintingPolicy_IncludeTagDefinition:
5213 P->IncludeTagDefinition = Value;
5214 return;
5215 case CXPrintingPolicy_SuppressScope:
5216 P->SuppressScope = Value;
5217 return;
5218 case CXPrintingPolicy_SuppressUnwrittenScope:
5219 P->SuppressUnwrittenScope = Value;
5220 return;
5221 case CXPrintingPolicy_SuppressInitializers:
5222 P->SuppressInitializers = Value;
5223 return;
5224 case CXPrintingPolicy_ConstantArraySizeAsWritten:
5225 P->ConstantArraySizeAsWritten = Value;
5226 return;
5227 case CXPrintingPolicy_AnonymousTagLocations:
5228 P->AnonymousTagLocations = Value;
5229 return;
5230 case CXPrintingPolicy_SuppressStrongLifetime:
5231 P->SuppressStrongLifetime = Value;
5232 return;
5233 case CXPrintingPolicy_SuppressLifetimeQualifiers:
5234 P->SuppressLifetimeQualifiers = Value;
5235 return;
5236 case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors:
5237 P->SuppressTemplateArgsInCXXConstructors = Value;
5238 return;
5239 case CXPrintingPolicy_Bool:
5240 P->Bool = Value;
5241 return;
5242 case CXPrintingPolicy_Restrict:
5243 P->Restrict = Value;
5244 return;
5245 case CXPrintingPolicy_Alignof:
5246 P->Alignof = Value;
5247 return;
5248 case CXPrintingPolicy_UnderscoreAlignof:
5249 P->UnderscoreAlignof = Value;
5250 return;
5251 case CXPrintingPolicy_UseVoidForZeroParams:
5252 P->UseVoidForZeroParams = Value;
5253 return;
5254 case CXPrintingPolicy_TerseOutput:
5255 P->TerseOutput = Value;
5256 return;
5257 case CXPrintingPolicy_PolishForDeclaration:
5258 P->PolishForDeclaration = Value;
5259 return;
5260 case CXPrintingPolicy_Half:
5261 P->Half = Value;
5262 return;
5263 case CXPrintingPolicy_MSWChar:
5264 P->MSWChar = Value;
5265 return;
5266 case CXPrintingPolicy_IncludeNewlines:
5267 P->IncludeNewlines = Value;
5268 return;
5269 case CXPrintingPolicy_MSVCFormatting:
5270 P->MSVCFormatting = Value;
5271 return;
5272 case CXPrintingPolicy_ConstantsAsWritten:
5273 P->ConstantsAsWritten = Value;
5274 return;
5275 case CXPrintingPolicy_SuppressImplicitBase:
5276 P->SuppressImplicitBase = Value;
5277 return;
5278 case CXPrintingPolicy_FullyQualifiedName:
5279 P->FullyQualifiedName = Value;
5280 return;
5281 }
5282
5283 assert(false && "Invalid CXPrintingPolicyProperty")(static_cast <bool> (false && "Invalid CXPrintingPolicyProperty"
) ? void (0) : __assert_fail ("false && \"Invalid CXPrintingPolicyProperty\""
, "clang/tools/libclang/CIndex.cpp", 5283, __extension__ __PRETTY_FUNCTION__
))
;
5284}
5285
5286CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) {
5287 if (clang_Cursor_isNull(C))
5288 return cxstring::createEmpty();
5289
5290 if (clang_isDeclaration(C.kind)) {
5291 const Decl *D = getCursorDecl(C);
5292 if (!D)
5293 return cxstring::createEmpty();
5294
5295 SmallString<128> Str;
5296 llvm::raw_svector_ostream OS(Str);
5297 PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
5298 D->print(OS, UserPolicy ? *UserPolicy
5299 : getCursorContext(C).getPrintingPolicy());
5300
5301 return cxstring::createDup(OS.str());
5302 }
5303
5304 return cxstring::createEmpty();
5305}
5306
5307CXString clang_getCursorDisplayName(CXCursor C) {
5308 if (!clang_isDeclaration(C.kind))
5309 return clang_getCursorSpelling(C);
5310
5311 const Decl *D = getCursorDecl(C);
5312 if (!D)
5313 return cxstring::createEmpty();
5314
5315 PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
5316 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
5317 D = FunTmpl->getTemplatedDecl();
5318
5319 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
5320 SmallString<64> Str;
5321 llvm::raw_svector_ostream OS(Str);
5322 OS << *Function;
5323 if (Function->getPrimaryTemplate())
5324 OS << "<>";
5325 OS << "(";
5326 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
5327 if (I)
5328 OS << ", ";
5329 OS << Function->getParamDecl(I)->getType().getAsString(Policy);
5330 }
5331
5332 if (Function->isVariadic()) {
5333 if (Function->getNumParams())
5334 OS << ", ";
5335 OS << "...";
5336 }
5337 OS << ")";
5338 return cxstring::createDup(OS.str());
5339 }
5340
5341 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
5342 SmallString<64> Str;
5343 llvm::raw_svector_ostream OS(Str);
5344 OS << *ClassTemplate;
5345 OS << "<";
5346 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
5347 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
5348 if (I)
5349 OS << ", ";
5350
5351 NamedDecl *Param = Params->getParam(I);
5352 if (Param->getIdentifier()) {
5353 OS << Param->getIdentifier()->getName();
5354 continue;
5355 }
5356
5357 // There is no parameter name, which makes this tricky. Try to come up
5358 // with something useful that isn't too long.
5359 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
5360 if (const auto *TC = TTP->getTypeConstraint()) {
5361 TC->getConceptNameInfo().printName(OS, Policy);
5362 if (TC->hasExplicitTemplateArgs())
5363 OS << "<...>";
5364 } else
5365 OS << (TTP->wasDeclaredWithTypename() ? "typename" : "class");
5366 else if (NonTypeTemplateParmDecl *NTTP =
5367 dyn_cast<NonTypeTemplateParmDecl>(Param))
5368 OS << NTTP->getType().getAsString(Policy);
5369 else
5370 OS << "template<...> class";
5371 }
5372
5373 OS << ">";
5374 return cxstring::createDup(OS.str());
5375 }
5376
5377 if (const ClassTemplateSpecializationDecl *ClassSpec =
5378 dyn_cast<ClassTemplateSpecializationDecl>(D)) {
5379 // If the type was explicitly written, use that.
5380 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
5381 return cxstring::createDup(TSInfo->getType().getAsString(Policy));
5382
5383 SmallString<128> Str;
5384 llvm::raw_svector_ostream OS(Str);
5385 OS << *ClassSpec;
5386 printTemplateArgumentList(
5387 OS, ClassSpec->getTemplateArgs().asArray(), Policy,
5388 ClassSpec->getSpecializedTemplate()->getTemplateParameters());
5389 return cxstring::createDup(OS.str());
5390 }
5391
5392 return clang_getCursorSpelling(C);
5393}
5394
5395CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
5396 switch (Kind) {
5397 case CXCursor_FunctionDecl:
5398 return cxstring::createRef("FunctionDecl");
5399 case CXCursor_TypedefDecl:
5400 return cxstring::createRef("TypedefDecl");
5401 case CXCursor_EnumDecl:
5402 return cxstring::createRef("EnumDecl");
5403 case CXCursor_EnumConstantDecl:
5404 return cxstring::createRef("EnumConstantDecl");
5405 case CXCursor_StructDecl:
5406 return cxstring::createRef("StructDecl");
5407 case CXCursor_UnionDecl:
5408 return cxstring::createRef("UnionDecl");
5409 case CXCursor_ClassDecl:
5410 return cxstring::createRef("ClassDecl");
5411 case CXCursor_FieldDecl:
5412 return cxstring::createRef("FieldDecl");
5413 case CXCursor_VarDecl:
5414 return cxstring::createRef("VarDecl");
5415 case CXCursor_ParmDecl:
5416 return cxstring::createRef("ParmDecl");
5417 case CXCursor_ObjCInterfaceDecl:
5418 return cxstring::createRef("ObjCInterfaceDecl");
5419 case CXCursor_ObjCCategoryDecl:
5420 return cxstring::createRef("ObjCCategoryDecl");
5421 case CXCursor_ObjCProtocolDecl:
5422 return cxstring::createRef("ObjCProtocolDecl");
5423 case CXCursor_ObjCPropertyDecl:
5424 return cxstring::createRef("ObjCPropertyDecl");
5425 case CXCursor_ObjCIvarDecl:
5426 return cxstring::createRef("ObjCIvarDecl");
5427 case CXCursor_ObjCInstanceMethodDecl:
5428 return cxstring::createRef("ObjCInstanceMethodDecl");
5429 case CXCursor_ObjCClassMethodDecl:
5430 return cxstring::createRef("ObjCClassMethodDecl");
5431 case CXCursor_ObjCImplementationDecl:
5432 return cxstring::createRef("ObjCImplementationDecl");
5433 case CXCursor_ObjCCategoryImplDecl:
5434 return cxstring::createRef("ObjCCategoryImplDecl");
5435 case CXCursor_CXXMethod:
5436 return cxstring::createRef("CXXMethod");
5437 case CXCursor_UnexposedDecl:
5438 return cxstring::createRef("UnexposedDecl");
5439 case CXCursor_ObjCSuperClassRef:
5440 return cxstring::createRef("ObjCSuperClassRef");
5441 case CXCursor_ObjCProtocolRef:
5442 return cxstring::createRef("ObjCProtocolRef");
5443 case CXCursor_ObjCClassRef:
5444 return cxstring::createRef("ObjCClassRef");
5445 case CXCursor_TypeRef:
5446 return cxstring::createRef("TypeRef");
5447 case CXCursor_TemplateRef:
5448 return cxstring::createRef("TemplateRef");
5449 case CXCursor_NamespaceRef:
5450 return cxstring::createRef("NamespaceRef");
5451 case CXCursor_MemberRef:
5452 return cxstring::createRef("MemberRef");
5453 case CXCursor_LabelRef:
5454 return cxstring::createRef("LabelRef");
5455 case CXCursor_OverloadedDeclRef:
5456 return cxstring::createRef("OverloadedDeclRef");
5457 case CXCursor_VariableRef:
5458 return cxstring::createRef("VariableRef");
5459 case CXCursor_IntegerLiteral:
5460 return cxstring::createRef("IntegerLiteral");
5461 case CXCursor_FixedPointLiteral:
5462 return cxstring::createRef("FixedPointLiteral");
5463 case CXCursor_FloatingLiteral:
5464 return cxstring::createRef("FloatingLiteral");
5465 case CXCursor_ImaginaryLiteral:
5466 return cxstring::createRef("ImaginaryLiteral");
5467 case CXCursor_StringLiteral:
5468 return cxstring::createRef("StringLiteral");
5469 case CXCursor_CharacterLiteral:
5470 return cxstring::createRef("CharacterLiteral");
5471 case CXCursor_ParenExpr:
5472 return cxstring::createRef("ParenExpr");
5473 case CXCursor_UnaryOperator:
5474 return cxstring::createRef("UnaryOperator");
5475 case CXCursor_ArraySubscriptExpr:
5476 return cxstring::createRef("ArraySubscriptExpr");
5477 case CXCursor_OMPArraySectionExpr:
5478 return cxstring::createRef("OMPArraySectionExpr");
5479 case CXCursor_OMPArrayShapingExpr:
5480 return cxstring::createRef("OMPArrayShapingExpr");
5481 case CXCursor_OMPIteratorExpr:
5482 return cxstring::createRef("OMPIteratorExpr");
5483 case CXCursor_BinaryOperator:
5484 return cxstring::createRef("BinaryOperator");
5485 case CXCursor_CompoundAssignOperator:
5486 return cxstring::createRef("CompoundAssignOperator");
5487 case CXCursor_ConditionalOperator:
5488 return cxstring::createRef("ConditionalOperator");
5489 case CXCursor_CStyleCastExpr:
5490 return cxstring::createRef("CStyleCastExpr");
5491 case CXCursor_CompoundLiteralExpr:
5492 return cxstring::createRef("CompoundLiteralExpr");
5493 case CXCursor_InitListExpr:
5494 return cxstring::createRef("InitListExpr");
5495 case CXCursor_AddrLabelExpr:
5496 return cxstring::createRef("AddrLabelExpr");
5497 case CXCursor_StmtExpr:
5498 return cxstring::createRef("StmtExpr");
5499 case CXCursor_GenericSelectionExpr:
5500 return cxstring::createRef("GenericSelectionExpr");
5501 case CXCursor_GNUNullExpr:
5502 return cxstring::createRef("GNUNullExpr");
5503 case CXCursor_CXXStaticCastExpr:
5504 return cxstring::createRef("CXXStaticCastExpr");
5505 case CXCursor_CXXDynamicCastExpr:
5506 return cxstring::createRef("CXXDynamicCastExpr");
5507 case CXCursor_CXXReinterpretCastExpr:
5508 return cxstring::createRef("CXXReinterpretCastExpr");
5509 case CXCursor_CXXConstCastExpr:
5510 return cxstring::createRef("CXXConstCastExpr");
5511 case CXCursor_CXXFunctionalCastExpr:
5512 return cxstring::createRef("CXXFunctionalCastExpr");
5513 case CXCursor_CXXAddrspaceCastExpr:
5514 return cxstring::createRef("CXXAddrspaceCastExpr");
5515 case CXCursor_CXXTypeidExpr:
5516 return cxstring::createRef("CXXTypeidExpr");
5517 case CXCursor_CXXBoolLiteralExpr:
5518 return cxstring::createRef("CXXBoolLiteralExpr");
5519 case CXCursor_CXXNullPtrLiteralExpr:
5520 return cxstring::createRef("CXXNullPtrLiteralExpr");
5521 case CXCursor_CXXThisExpr:
5522 return cxstring::createRef("CXXThisExpr");
5523 case CXCursor_CXXThrowExpr:
5524 return cxstring::createRef("CXXThrowExpr");
5525 case CXCursor_CXXNewExpr:
5526 return cxstring::createRef("CXXNewExpr");
5527 case CXCursor_CXXDeleteExpr:
5528 return cxstring::createRef("CXXDeleteExpr");
5529 case CXCursor_UnaryExpr:
5530 return cxstring::createRef("UnaryExpr");
5531 case CXCursor_ObjCStringLiteral:
5532 return cxstring::createRef("ObjCStringLiteral");
5533 case CXCursor_ObjCBoolLiteralExpr:
5534 return cxstring::createRef("ObjCBoolLiteralExpr");
5535 case CXCursor_ObjCAvailabilityCheckExpr:
5536 return cxstring::createRef("ObjCAvailabilityCheckExpr");
5537 case CXCursor_ObjCSelfExpr:
5538 return cxstring::createRef("ObjCSelfExpr");
5539 case CXCursor_ObjCEncodeExpr:
5540 return cxstring::createRef("ObjCEncodeExpr");
5541 case CXCursor_ObjCSelectorExpr:
5542 return cxstring::createRef("ObjCSelectorExpr");
5543 case CXCursor_ObjCProtocolExpr:
5544 return cxstring::createRef("ObjCProtocolExpr");
5545 case CXCursor_ObjCBridgedCastExpr:
5546 return cxstring::createRef("ObjCBridgedCastExpr");
5547 case CXCursor_BlockExpr:
5548 return cxstring::createRef("BlockExpr");
5549 case CXCursor_PackExpansionExpr:
5550 return cxstring::createRef("PackExpansionExpr");
5551 case CXCursor_SizeOfPackExpr:
5552 return cxstring::createRef("SizeOfPackExpr");
5553 case CXCursor_LambdaExpr:
5554 return cxstring::createRef("LambdaExpr");
5555 case CXCursor_UnexposedExpr:
5556 return cxstring::createRef("UnexposedExpr");
5557 case CXCursor_DeclRefExpr:
5558 return cxstring::createRef("DeclRefExpr");
5559 case CXCursor_MemberRefExpr:
5560 return cxstring::createRef("MemberRefExpr");
5561 case CXCursor_CallExpr:
5562 return cxstring::createRef("CallExpr");
5563 case CXCursor_ObjCMessageExpr:
5564 return cxstring::createRef("ObjCMessageExpr");
5565 case CXCursor_BuiltinBitCastExpr:
5566 return cxstring::createRef("BuiltinBitCastExpr");
5567 case CXCursor_ConceptSpecializationExpr:
5568 return cxstring::createRef("ConceptSpecializationExpr");
5569 case CXCursor_RequiresExpr:
5570 return cxstring::createRef("RequiresExpr");
5571 case CXCursor_UnexposedStmt:
5572 return cxstring::createRef("UnexposedStmt");
5573 case CXCursor_DeclStmt:
5574 return cxstring::createRef("DeclStmt");
5575 case CXCursor_LabelStmt:
5576 return cxstring::createRef("LabelStmt");
5577 case CXCursor_CompoundStmt:
5578 return cxstring::createRef("CompoundStmt");
5579 case CXCursor_CaseStmt:
5580 return cxstring::createRef("CaseStmt");
5581 case CXCursor_DefaultStmt:
5582 return cxstring::createRef("DefaultStmt");
5583 case CXCursor_IfStmt:
5584 return cxstring::createRef("IfStmt");
5585 case CXCursor_SwitchStmt:
5586 return cxstring::createRef("SwitchStmt");
5587 case CXCursor_WhileStmt:
5588 return cxstring::createRef("WhileStmt");
5589 case CXCursor_DoStmt:
5590 return cxstring::createRef("DoStmt");
5591 case CXCursor_ForStmt:
5592 return cxstring::createRef("ForStmt");
5593 case CXCursor_GotoStmt:
5594 return cxstring::createRef("GotoStmt");
5595 case CXCursor_IndirectGotoStmt:
5596 return cxstring::createRef("IndirectGotoStmt");
5597 case CXCursor_ContinueStmt:
5598 return cxstring::createRef("ContinueStmt");
5599 case CXCursor_BreakStmt:
5600 return cxstring::createRef("BreakStmt");
5601 case CXCursor_ReturnStmt:
5602 return cxstring::createRef("ReturnStmt");
5603 case CXCursor_GCCAsmStmt:
5604 return cxstring::createRef("GCCAsmStmt");
5605 case CXCursor_MSAsmStmt:
5606 return cxstring::createRef("MSAsmStmt");
5607 case CXCursor_ObjCAtTryStmt:
5608 return cxstring::createRef("ObjCAtTryStmt");
5609 case CXCursor_ObjCAtCatchStmt:
5610 return cxstring::createRef("ObjCAtCatchStmt");
5611 case CXCursor_ObjCAtFinallyStmt:
5612 return cxstring::createRef("ObjCAtFinallyStmt");
5613 case CXCursor_ObjCAtThrowStmt:
5614 return cxstring::createRef("ObjCAtThrowStmt");
5615 case CXCursor_ObjCAtSynchronizedStmt:
5616 return cxstring::createRef("ObjCAtSynchronizedStmt");
5617 case CXCursor_ObjCAutoreleasePoolStmt:
5618 return cxstring::createRef("ObjCAutoreleasePoolStmt");
5619 case CXCursor_ObjCForCollectionStmt:
5620 return cxstring::createRef("ObjCForCollectionStmt");
5621 case CXCursor_CXXCatchStmt:
5622 return cxstring::createRef("CXXCatchStmt");
5623 case CXCursor_CXXTryStmt:
5624 return cxstring::createRef("CXXTryStmt");
5625 case CXCursor_CXXForRangeStmt:
5626 return cxstring::createRef("CXXForRangeStmt");
5627 case CXCursor_SEHTryStmt:
5628 return cxstring::createRef("SEHTryStmt");
5629 case CXCursor_SEHExceptStmt:
5630 return cxstring::createRef("SEHExceptStmt");
5631 case CXCursor_SEHFinallyStmt:
5632 return cxstring::createRef("SEHFinallyStmt");
5633 case CXCursor_SEHLeaveStmt:
5634 return cxstring::createRef("SEHLeaveStmt");
5635 case CXCursor_NullStmt:
5636 return cxstring::createRef("NullStmt");
5637 case CXCursor_InvalidFile:
5638 return cxstring::createRef("InvalidFile");
5639 case CXCursor_InvalidCode:
5640 return cxstring::createRef("InvalidCode");
5641 case CXCursor_NoDeclFound:
5642 return cxstring::createRef("NoDeclFound");
5643 case CXCursor_NotImplemented:
5644 return cxstring::createRef("NotImplemented");
5645 case CXCursor_TranslationUnit:
5646 return cxstring::createRef("TranslationUnit");
5647 case CXCursor_UnexposedAttr:
5648 return cxstring::createRef("UnexposedAttr");
5649 case CXCursor_IBActionAttr:
5650 return cxstring::createRef("attribute(ibaction)");
5651 case CXCursor_IBOutletAttr:
5652 return cxstring::createRef("attribute(iboutlet)");
5653 case CXCursor_IBOutletCollectionAttr:
5654 return cxstring::createRef("attribute(iboutletcollection)");
5655 case CXCursor_CXXFinalAttr:
5656 return cxstring::createRef("attribute(final)");
5657 case CXCursor_CXXOverrideAttr:
5658 return cxstring::createRef("attribute(override)");
5659 case CXCursor_AnnotateAttr:
5660 return cxstring::createRef("attribute(annotate)");
5661 case CXCursor_AsmLabelAttr:
5662 return cxstring::createRef("asm label");
5663 case CXCursor_PackedAttr:
5664 return cxstring::createRef("attribute(packed)");
5665 case CXCursor_PureAttr:
5666 return cxstring::createRef("attribute(pure)");
5667 case CXCursor_ConstAttr:
5668 return cxstring::createRef("attribute(const)");
5669 case CXCursor_NoDuplicateAttr:
5670 return cxstring::createRef("attribute(noduplicate)");
5671 case CXCursor_CUDAConstantAttr:
5672 return cxstring::createRef("attribute(constant)");
5673 case CXCursor_CUDADeviceAttr:
5674 return cxstring::createRef("attribute(device)");
5675 case CXCursor_CUDAGlobalAttr:
5676 return cxstring::createRef("attribute(global)");
5677 case CXCursor_CUDAHostAttr:
5678 return cxstring::createRef("attribute(host)");
5679 case CXCursor_CUDASharedAttr:
5680 return cxstring::createRef("attribute(shared)");
5681 case CXCursor_VisibilityAttr:
5682 return cxstring::createRef("attribute(visibility)");
5683 case CXCursor_DLLExport:
5684 return cxstring::createRef("attribute(dllexport)");
5685 case CXCursor_DLLImport:
5686 return cxstring::createRef("attribute(dllimport)");
5687 case CXCursor_NSReturnsRetained:
5688 return cxstring::createRef("attribute(ns_returns_retained)");
5689 case CXCursor_NSReturnsNotRetained:
5690 return cxstring::createRef("attribute(ns_returns_not_retained)");
5691 case CXCursor_NSReturnsAutoreleased:
5692 return cxstring::createRef("attribute(ns_returns_autoreleased)");
5693 case CXCursor_NSConsumesSelf:
5694 return cxstring::createRef("attribute(ns_consumes_self)");
5695 case CXCursor_NSConsumed:
5696 return cxstring::createRef("attribute(ns_consumed)");
5697 case CXCursor_ObjCException:
5698 return cxstring::createRef("attribute(objc_exception)");
5699 case CXCursor_ObjCNSObject:
5700 return cxstring::createRef("attribute(NSObject)");
5701 case CXCursor_ObjCIndependentClass:
5702 return cxstring::createRef("attribute(objc_independent_class)");
5703 case CXCursor_ObjCPreciseLifetime:
5704 return cxstring::createRef("attribute(objc_precise_lifetime)");
5705 case CXCursor_ObjCReturnsInnerPointer:
5706 return cxstring::createRef("attribute(objc_returns_inner_pointer)");
5707 case CXCursor_ObjCRequiresSuper:
5708 return cxstring::createRef("attribute(objc_requires_super)");
5709 case CXCursor_ObjCRootClass:
5710 return cxstring::createRef("attribute(objc_root_class)");
5711 case CXCursor_ObjCSubclassingRestricted:
5712 return cxstring::createRef("attribute(objc_subclassing_restricted)");
5713 case CXCursor_ObjCExplicitProtocolImpl:
5714 return cxstring::createRef(
5715 "attribute(objc_protocol_requires_explicit_implementation)");
5716 case CXCursor_ObjCDesignatedInitializer:
5717 return cxstring::createRef("attribute(objc_designated_initializer)");
5718 case CXCursor_ObjCRuntimeVisible:
5719 return cxstring::createRef("attribute(objc_runtime_visible)");
5720 case CXCursor_ObjCBoxable:
5721 return cxstring::createRef("attribute(objc_boxable)");
5722 case CXCursor_FlagEnum:
5723 return cxstring::createRef("attribute(flag_enum)");
5724 case CXCursor_PreprocessingDirective:
5725 return cxstring::createRef("preprocessing directive");
5726 case CXCursor_MacroDefinition:
5727 return cxstring::createRef("macro definition");
5728 case CXCursor_MacroExpansion:
5729 return cxstring::createRef("macro expansion");
5730 case CXCursor_InclusionDirective:
5731 return cxstring::createRef("inclusion directive");
5732 case CXCursor_Namespace:
5733 return cxstring::createRef("Namespace");
5734 case CXCursor_LinkageSpec:
5735 return cxstring::createRef("LinkageSpec");
5736 case CXCursor_CXXBaseSpecifier:
5737 return cxstring::createRef("C++ base class specifier");
5738 case CXCursor_Constructor:
5739 return cxstring::createRef("CXXConstructor");
5740 case CXCursor_Destructor:
5741 return cxstring::createRef("CXXDestructor");
5742 case CXCursor_ConversionFunction:
5743 return cxstring::createRef("CXXConversion");
5744 case CXCursor_TemplateTypeParameter:
5745 return cxstring::createRef("TemplateTypeParameter");
5746 case CXCursor_NonTypeTemplateParameter:
5747 return cxstring::createRef("NonTypeTemplateParameter");
5748 case CXCursor_TemplateTemplateParameter:
5749 return cxstring::createRef("TemplateTemplateParameter");
5750 case CXCursor_FunctionTemplate:
5751 return cxstring::createRef("FunctionTemplate");
5752 case CXCursor_ClassTemplate:
5753 return cxstring::createRef("ClassTemplate");
5754 case CXCursor_ClassTemplatePartialSpecialization:
5755 return cxstring::createRef("ClassTemplatePartialSpecialization");
5756 case CXCursor_NamespaceAlias:
5757 return cxstring::createRef("NamespaceAlias");
5758 case CXCursor_UsingDirective:
5759 return cxstring::createRef("UsingDirective");
5760 case CXCursor_UsingDeclaration:
5761 return cxstring::createRef("UsingDeclaration");
5762 case CXCursor_TypeAliasDecl:
5763 return cxstring::createRef("TypeAliasDecl");
5764 case CXCursor_ObjCSynthesizeDecl:
5765 return cxstring::createRef("ObjCSynthesizeDecl");
5766 case CXCursor_ObjCDynamicDecl:
5767 return cxstring::createRef("ObjCDynamicDecl");
5768 case CXCursor_CXXAccessSpecifier:
5769 return cxstring::createRef("CXXAccessSpecifier");
5770 case CXCursor_ModuleImportDecl:
5771 return cxstring::createRef("ModuleImport");
5772 case CXCursor_OMPCanonicalLoop:
5773 return cxstring::createRef("OMPCanonicalLoop");
5774 case CXCursor_OMPMetaDirective:
5775 return cxstring::createRef("OMPMetaDirective");
5776 case CXCursor_OMPParallelDirective:
5777 return cxstring::createRef("OMPParallelDirective");
5778 case CXCursor_OMPSimdDirective:
5779 return cxstring::createRef("OMPSimdDirective");
5780 case CXCursor_OMPTileDirective:
5781 return cxstring::createRef("OMPTileDirective");
5782 case CXCursor_OMPUnrollDirective:
5783 return cxstring::createRef("OMPUnrollDirective");
5784 case CXCursor_OMPForDirective:
5785 return cxstring::createRef("OMPForDirective");
5786 case CXCursor_OMPForSimdDirective:
5787 return cxstring::createRef("OMPForSimdDirective");
5788 case CXCursor_OMPSectionsDirective:
5789 return cxstring::createRef("OMPSectionsDirective");
5790 case CXCursor_OMPSectionDirective:
5791 return cxstring::createRef("OMPSectionDirective");
5792 case CXCursor_OMPSingleDirective:
5793 return cxstring::createRef("OMPSingleDirective");
5794 case CXCursor_OMPMasterDirective:
5795 return cxstring::createRef("OMPMasterDirective");
5796 case CXCursor_OMPCriticalDirective:
5797 return cxstring::createRef("OMPCriticalDirective");
5798 case CXCursor_OMPParallelForDirective:
5799 return cxstring::createRef("OMPParallelForDirective");
5800 case CXCursor_OMPParallelForSimdDirective:
5801 return cxstring::createRef("OMPParallelForSimdDirective");
5802 case CXCursor_OMPParallelMasterDirective:
5803 return cxstring::createRef("OMPParallelMasterDirective");
5804 case CXCursor_OMPParallelMaskedDirective:
5805 return cxstring::createRef("OMPParallelMaskedDirective");
5806 case CXCursor_OMPParallelSectionsDirective:
5807 return cxstring::createRef("OMPParallelSectionsDirective");
5808 case CXCursor_OMPTaskDirective:
5809 return cxstring::createRef("OMPTaskDirective");
5810 case CXCursor_OMPTaskyieldDirective:
5811 return cxstring::createRef("OMPTaskyieldDirective");
5812 case CXCursor_OMPBarrierDirective:
5813 return cxstring::createRef("OMPBarrierDirective");
5814 case CXCursor_OMPTaskwaitDirective:
5815 return cxstring::createRef("OMPTaskwaitDirective");
5816 case CXCursor_OMPTaskgroupDirective:
5817 return cxstring::createRef("OMPTaskgroupDirective");
5818 case CXCursor_OMPFlushDirective:
5819 return cxstring::createRef("OMPFlushDirective");
5820 case CXCursor_OMPDepobjDirective:
5821 return cxstring::createRef("OMPDepobjDirective");
5822 case CXCursor_OMPScanDirective:
5823 return cxstring::createRef("OMPScanDirective");
5824 case CXCursor_OMPOrderedDirective:
5825 return cxstring::createRef("OMPOrderedDirective");
5826 case CXCursor_OMPAtomicDirective:
5827 return cxstring::createRef("OMPAtomicDirective");
5828 case CXCursor_OMPTargetDirective:
5829 return cxstring::createRef("OMPTargetDirective");
5830 case CXCursor_OMPTargetDataDirective:
5831 return cxstring::createRef("OMPTargetDataDirective");
5832 case CXCursor_OMPTargetEnterDataDirective:
5833 return cxstring::createRef("OMPTargetEnterDataDirective");
5834 case CXCursor_OMPTargetExitDataDirective:
5835 return cxstring::createRef("OMPTargetExitDataDirective");
5836 case CXCursor_OMPTargetParallelDirective:
5837 return cxstring::createRef("OMPTargetParallelDirective");
5838 case CXCursor_OMPTargetParallelForDirective:
5839 return cxstring::createRef("OMPTargetParallelForDirective");
5840 case CXCursor_OMPTargetUpdateDirective:
5841 return cxstring::createRef("OMPTargetUpdateDirective");
5842 case CXCursor_OMPTeamsDirective:
5843 return cxstring::createRef("OMPTeamsDirective");
5844 case CXCursor_OMPCancellationPointDirective:
5845 return cxstring::createRef("OMPCancellationPointDirective");
5846 case CXCursor_OMPCancelDirective:
5847 return cxstring::createRef("OMPCancelDirective");
5848 case CXCursor_OMPTaskLoopDirective:
5849 return cxstring::createRef("OMPTaskLoopDirective");
5850 case CXCursor_OMPTaskLoopSimdDirective:
5851 return cxstring::createRef("OMPTaskLoopSimdDirective");
5852 case CXCursor_OMPMasterTaskLoopDirective:
5853 return cxstring::createRef("OMPMasterTaskLoopDirective");
5854 case CXCursor_OMPMaskedTaskLoopDirective:
5855 return cxstring::createRef("OMPMaskedTaskLoopDirective");
5856 case CXCursor_OMPMasterTaskLoopSimdDirective:
5857 return cxstring::createRef("OMPMasterTaskLoopSimdDirective");
5858 case CXCursor_OMPMaskedTaskLoopSimdDirective:
5859 return cxstring::createRef("OMPMaskedTaskLoopSimdDirective");
5860 case CXCursor_OMPParallelMasterTaskLoopDirective:
5861 return cxstring::createRef("OMPParallelMasterTaskLoopDirective");
5862 case CXCursor_OMPParallelMaskedTaskLoopDirective:
5863 return cxstring::createRef("OMPParallelMaskedTaskLoopDirective");
5864 case CXCursor_OMPParallelMasterTaskLoopSimdDirective:
5865 return cxstring::createRef("OMPParallelMasterTaskLoopSimdDirective");
5866 case CXCursor_OMPParallelMaskedTaskLoopSimdDirective:
5867 return cxstring::createRef("OMPParallelMaskedTaskLoopSimdDirective");
5868 case CXCursor_OMPDistributeDirective:
5869 return cxstring::createRef("OMPDistributeDirective");
5870 case CXCursor_OMPDistributeParallelForDirective:
5871 return cxstring::createRef("OMPDistributeParallelForDirective");
5872 case CXCursor_OMPDistributeParallelForSimdDirective:
5873 return cxstring::createRef("OMPDistributeParallelForSimdDirective");
5874 case CXCursor_OMPDistributeSimdDirective:
5875 return cxstring::createRef("OMPDistributeSimdDirective");
5876 case CXCursor_OMPTargetParallelForSimdDirective:
5877 return cxstring::createRef("OMPTargetParallelForSimdDirective");
5878 case CXCursor_OMPTargetSimdDirective:
5879 return cxstring::createRef("OMPTargetSimdDirective");
5880 case CXCursor_OMPTeamsDistributeDirective:
5881 return cxstring::createRef("OMPTeamsDistributeDirective");
5882 case CXCursor_OMPTeamsDistributeSimdDirective:
5883 return cxstring::createRef("OMPTeamsDistributeSimdDirective");
5884 case CXCursor_OMPTeamsDistributeParallelForSimdDirective:
5885 return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective");
5886 case CXCursor_OMPTeamsDistributeParallelForDirective:
5887 return cxstring::createRef("OMPTeamsDistributeParallelForDirective");
5888 case CXCursor_OMPTargetTeamsDirective:
5889 return cxstring::createRef("OMPTargetTeamsDirective");
5890 case CXCursor_OMPTargetTeamsDistributeDirective:
5891 return cxstring::createRef("OMPTargetTeamsDistributeDirective");
5892 case CXCursor_OMPTargetTeamsDistributeParallelForDirective:
5893 return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective");
5894 case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective:
5895 return cxstring::createRef(
5896 "OMPTargetTeamsDistributeParallelForSimdDirective");
5897 case CXCursor_OMPTargetTeamsDistributeSimdDirective:
5898 return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective");
5899 case CXCursor_OMPInteropDirective:
5900 return cxstring::createRef("OMPInteropDirective");
5901 case CXCursor_OMPDispatchDirective:
5902 return cxstring::createRef("OMPDispatchDirective");
5903 case CXCursor_OMPMaskedDirective:
5904 return cxstring::createRef("OMPMaskedDirective");
5905 case CXCursor_OMPGenericLoopDirective:
5906 return cxstring::createRef("OMPGenericLoopDirective");
5907 case CXCursor_OMPTeamsGenericLoopDirective:
5908 return cxstring::createRef("OMPTeamsGenericLoopDirective");
5909 case CXCursor_OMPTargetTeamsGenericLoopDirective:
5910 return cxstring::createRef("OMPTargetTeamsGenericLoopDirective");
5911 case CXCursor_OMPParallelGenericLoopDirective:
5912 return cxstring::createRef("OMPParallelGenericLoopDirective");
5913 case CXCursor_OMPTargetParallelGenericLoopDirective:
5914 return cxstring::createRef("OMPTargetParallelGenericLoopDirective");
5915 case CXCursor_OverloadCandidate:
5916 return cxstring::createRef("OverloadCandidate");
5917 case CXCursor_TypeAliasTemplateDecl:
5918 return cxstring::createRef("TypeAliasTemplateDecl");
5919 case CXCursor_StaticAssert:
5920 return cxstring::createRef("StaticAssert");
5921 case CXCursor_FriendDecl:
5922 return cxstring::createRef("FriendDecl");
5923 case CXCursor_ConvergentAttr:
5924 return cxstring::createRef("attribute(convergent)");
5925 case CXCursor_WarnUnusedAttr:
5926 return cxstring::createRef("attribute(warn_unused)");
5927 case CXCursor_WarnUnusedResultAttr:
5928 return cxstring::createRef("attribute(warn_unused_result)");
5929 case CXCursor_AlignedAttr:
5930 return cxstring::createRef("attribute(aligned)");
5931 case CXCursor_ConceptDecl:
5932 return cxstring::createRef("ConceptDecl");
5933 }
5934
5935 llvm_unreachable("Unhandled CXCursorKind")::llvm::llvm_unreachable_internal("Unhandled CXCursorKind", "clang/tools/libclang/CIndex.cpp"
, 5935)
;
5936}
5937
5938struct GetCursorData {
5939 SourceLocation TokenBeginLoc;
5940 bool PointsAtMacroArgExpansion;
5941 bool VisitedObjCPropertyImplDecl;
5942 SourceLocation VisitedDeclaratorDeclStartLoc;
5943 CXCursor &BestCursor;
5944
5945 GetCursorData(SourceManager &SM, SourceLocation tokenBegin,
5946 CXCursor &outputCursor)
5947 : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
5948 PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
5949 VisitedObjCPropertyImplDecl = false;
5950 }
5951};
5952
5953static enum CXChildVisitResult
5954GetCursorVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {
5955 GetCursorData *Data = static_cast<GetCursorData *>(client_data);
5956 CXCursor *BestCursor = &Data->BestCursor;
5957
5958 // If we point inside a macro argument we should provide info of what the
5959 // token is so use the actual cursor, don't replace it with a macro expansion
5960 // cursor.
5961 if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
5962 return CXChildVisit_Recurse;
5963
5964 if (clang_isDeclaration(cursor.kind)) {
5965 // Avoid having the implicit methods override the property decls.
5966 if (const ObjCMethodDecl *MD =
5967 dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
5968 if (MD->isImplicit())
5969 return CXChildVisit_Break;
5970
5971 } else if (const ObjCInterfaceDecl *ID =
5972 dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) {
5973 // Check that when we have multiple @class references in the same line,
5974 // that later ones do not override the previous ones.
5975 // If we have:
5976 // @class Foo, Bar;
5977 // source ranges for both start at '@', so 'Bar' will end up overriding
5978 // 'Foo' even though the cursor location was at 'Foo'.
5979 if (BestCursor->kind == CXCursor_ObjCInterfaceDecl ||
5980 BestCursor->kind == CXCursor_ObjCClassRef)
5981 if (const ObjCInterfaceDecl *PrevID =
5982 dyn_cast_or_null<ObjCInterfaceDecl>(
5983 getCursorDecl(*BestCursor))) {
5984 if (PrevID != ID && !PrevID->isThisDeclarationADefinition() &&
5985 !ID->isThisDeclarationADefinition())
5986 return CXChildVisit_Break;
5987 }
5988
5989 } else if (const DeclaratorDecl *DD =
5990 dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) {
5991 SourceLocation StartLoc = DD->getSourceRange().getBegin();
5992 // Check that when we have multiple declarators in the same line,
5993 // that later ones do not override the previous ones.
5994 // If we have:
5995 // int Foo, Bar;
5996 // source ranges for both start at 'int', so 'Bar' will end up overriding
5997 // 'Foo' even though the cursor location was at 'Foo'.
5998 if (Data->VisitedDeclaratorDeclStartLoc == StartLoc)
5999 return CXChildVisit_Break;
6000 Data->VisitedDeclaratorDeclStartLoc = StartLoc;
6001
6002 } else if (const ObjCPropertyImplDecl *PropImp =
6003 dyn_cast_or_null<ObjCPropertyImplDecl>(
6004 getCursorDecl(cursor))) {
6005 (void)PropImp;
6006 // Check that when we have multiple @synthesize in the same line,
6007 // that later ones do not override the previous ones.
6008 // If we have:
6009 // @synthesize Foo, Bar;
6010 // source ranges for both start at '@', so 'Bar' will end up overriding
6011 // 'Foo' even though the cursor location was at 'Foo'.
6012 if (Data->VisitedObjCPropertyImplDecl)
6013 return CXChildVisit_Break;
6014 Data->VisitedObjCPropertyImplDecl = true;
6015 }
6016 }
6017
6018 if (clang_isExpression(cursor.kind) &&
6019 clang_isDeclaration(BestCursor->kind)) {
6020 if (const Decl *D = getCursorDecl(*BestCursor)) {
6021 // Avoid having the cursor of an expression replace the declaration cursor
6022 // when the expression source range overlaps the declaration range.
6023 // This can happen for C++ constructor expressions whose range generally
6024 // include the variable declaration, e.g.:
6025 // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl
6026 // cursor.
6027 if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
6028 D->getLocation() == Data->TokenBeginLoc)
6029 return CXChildVisit_Break;
6030 }
6031 }
6032
6033 // If our current best cursor is the construction of a temporary object,
6034 // don't replace that cursor with a type reference, because we want
6035 // clang_getCursor() to point at the constructor.
6036 if (clang_isExpression(BestCursor->kind) &&
6037 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
6038 cursor.kind == CXCursor_TypeRef) {
6039 // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
6040 // as having the actual point on the type reference.
6041 *BestCursor = getTypeRefedCallExprCursor(*BestCursor);
6042 return CXChildVisit_Recurse;
6043 }
6044
6045 // If we already have an Objective-C superclass reference, don't
6046 // update it further.
6047 if (BestCursor->kind == CXCursor_ObjCSuperClassRef)
6048 return CXChildVisit_Break;
6049
6050 *BestCursor = cursor;
6051 return CXChildVisit_Recurse;
6052}
6053
6054CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
6055 if (isNotUsableTU(TU)) {
6056 LOG_BAD_TU(TU)do { if (clang::cxindex::LogRef Log = clang::cxindex::Logger::
make(__func__)) { *Log << "called with a bad TU: " <<
TU; } } while(false)
;
6057 return clang_getNullCursor();
6058 }
6059
6060 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
6061 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
6062
6063 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
6064 CXCursor Result = cxcursor::getCursor(TU, SLoc);
6065
6066 LOG_FUNC_SECTIONif (clang::cxindex::LogRef Log = clang::cxindex::Logger::make
(__func__))
{
6067 CXFile SearchFile;
6068 unsigned SearchLine, SearchColumn;