blob: 2007951062026db5b71c9a94a210cba2474509ca [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CIndexer.h"
16#include "CIndexDiagnostic.h"
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000017#include "CLog.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000018#include "CXComment.h"
19#include "CXCursor.h"
20#include "CXSourceLocation.h"
21#include "CXString.h"
22#include "CXTranslationUnit.h"
23#include "CXType.h"
24#include "CursorVisitor.h"
Fariborz Jahanian88b95212012-12-18 23:02:59 +000025#include "SimpleFormatContext.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026#include "clang/AST/StmtVisitor.h"
27#include "clang/Basic/Diagnostic.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/ASTUnit.h"
30#include "clang/Frontend/CompilerInstance.h"
31#include "clang/Frontend/FrontendDiagnostic.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/Lexer.h"
34#include "clang/Lex/PreprocessingRecord.h"
35#include "clang/Lex/Preprocessor.h"
36#include "llvm/ADT/Optional.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/StringSwitch.h"
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000039#include "llvm/Config/config.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000040#include "llvm/Support/Compiler.h"
41#include "llvm/Support/CrashRecoveryContext.h"
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000042#include "llvm/Support/Format.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000043#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/Mutex.h"
45#include "llvm/Support/PrettyStackTrace.h"
46#include "llvm/Support/Program.h"
47#include "llvm/Support/SaveAndRestore.h"
48#include "llvm/Support/Signals.h"
49#include "llvm/Support/Threading.h"
50#include "llvm/Support/Timer.h"
51#include "llvm/Support/raw_ostream.h"
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +000052
53#if HAVE_PTHREAD_H
54#include <pthread.h>
55#endif
Guy Benyei7f92f2d2012-12-18 14:30:41 +000056
57using namespace clang;
58using namespace clang::cxcursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000059using namespace clang::cxtu;
60using namespace clang::cxindex;
61
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +000062CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *AU) {
63 if (!AU)
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064 return 0;
65 CXTranslationUnit D = new CXTranslationUnitImpl();
66 D->CIdx = CIdx;
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +000067 D->TheASTUnit = AU;
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +000068 D->StringPool = new cxstring::CXStringPool();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000069 D->Diagnostics = 0;
70 D->OverridenCursorsPool = createOverridenCXCursorsPool();
Fariborz Jahanian88b95212012-12-18 23:02:59 +000071 D->FormatContext = 0;
72 D->FormatInMemoryUniqueId = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000073 return D;
74}
75
76cxtu::CXTUOwner::~CXTUOwner() {
77 if (TU)
78 clang_disposeTranslationUnit(TU);
79}
80
81/// \brief Compare two source ranges to determine their relative position in
82/// the translation unit.
83static RangeComparisonResult RangeCompare(SourceManager &SM,
84 SourceRange R1,
85 SourceRange R2) {
86 assert(R1.isValid() && "First range is invalid?");
87 assert(R2.isValid() && "Second range is invalid?");
88 if (R1.getEnd() != R2.getBegin() &&
89 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
90 return RangeBefore;
91 if (R2.getEnd() != R1.getBegin() &&
92 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
93 return RangeAfter;
94 return RangeOverlap;
95}
96
97/// \brief Determine if a source location falls within, before, or after a
98/// a given source range.
99static RangeComparisonResult LocationCompare(SourceManager &SM,
100 SourceLocation L, SourceRange R) {
101 assert(R.isValid() && "First range is invalid?");
102 assert(L.isValid() && "Second range is invalid?");
103 if (L == R.getBegin() || L == R.getEnd())
104 return RangeOverlap;
105 if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
106 return RangeBefore;
107 if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
108 return RangeAfter;
109 return RangeOverlap;
110}
111
112/// \brief Translate a Clang source range into a CIndex source range.
113///
114/// Clang internally represents ranges where the end location points to the
115/// start of the token at the end. However, for external clients it is more
116/// useful to have a CXSourceRange be a proper half-open interval. This routine
117/// does the appropriate translation.
118CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
119 const LangOptions &LangOpts,
120 const CharSourceRange &R) {
121 // We want the last character in this location, so we will adjust the
122 // location accordingly.
123 SourceLocation EndLoc = R.getEnd();
124 if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc))
125 EndLoc = SM.getExpansionRange(EndLoc).second;
126 if (R.isTokenRange() && !EndLoc.isInvalid()) {
127 unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc),
128 SM, LangOpts);
129 EndLoc = EndLoc.getLocWithOffset(Length);
130 }
131
Bill Wendlingccdfdd72013-01-23 08:25:41 +0000132 CXSourceRange Result = {
Dmitri Gribenkoe4ea8792013-01-23 15:56:07 +0000133 { &SM, &LangOpts },
Bill Wendlingccdfdd72013-01-23 08:25:41 +0000134 R.getBegin().getRawEncoding(),
135 EndLoc.getRawEncoding()
136 };
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000137 return Result;
138}
139
140//===----------------------------------------------------------------------===//
141// Cursor visitor.
142//===----------------------------------------------------------------------===//
143
144static SourceRange getRawCursorExtent(CXCursor C);
145static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
146
147
148RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
149 return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
150}
151
152/// \brief Visit the given cursor and, if requested by the visitor,
153/// its children.
154///
155/// \param Cursor the cursor to visit.
156///
157/// \param CheckedRegionOfInterest if true, then the caller already checked
158/// that this cursor is within the region of interest.
159///
160/// \returns true if the visitation should be aborted, false if it
161/// should continue.
162bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
163 if (clang_isInvalid(Cursor.kind))
164 return false;
165
166 if (clang_isDeclaration(Cursor.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000167 const Decl *D = getCursorDecl(Cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000168 if (!D) {
169 assert(0 && "Invalid declaration cursor");
170 return true; // abort.
171 }
172
173 // Ignore implicit declarations, unless it's an objc method because
174 // currently we should report implicit methods for properties when indexing.
175 if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
176 return false;
177 }
178
179 // If we have a range of interest, and this cursor doesn't intersect with it,
180 // we're done.
181 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
182 SourceRange Range = getRawCursorExtent(Cursor);
183 if (Range.isInvalid() || CompareRegionOfInterest(Range))
184 return false;
185 }
186
187 switch (Visitor(Cursor, Parent, ClientData)) {
188 case CXChildVisit_Break:
189 return true;
190
191 case CXChildVisit_Continue:
192 return false;
193
194 case CXChildVisit_Recurse: {
195 bool ret = VisitChildren(Cursor);
196 if (PostChildrenVisitor)
197 if (PostChildrenVisitor(Cursor, ClientData))
198 return true;
199 return ret;
200 }
201 }
202
203 llvm_unreachable("Invalid CXChildVisitResult!");
204}
205
206static bool visitPreprocessedEntitiesInRange(SourceRange R,
207 PreprocessingRecord &PPRec,
208 CursorVisitor &Visitor) {
209 SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
210 FileID FID;
211
212 if (!Visitor.shouldVisitIncludedEntities()) {
213 // If the begin/end of the range lie in the same FileID, do the optimization
214 // where we skip preprocessed entities that do not come from the same FileID.
215 FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
216 if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
217 FID = FileID();
218 }
219
220 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
221 Entities = PPRec.getPreprocessedEntitiesInRange(R);
222 return Visitor.visitPreprocessedEntities(Entities.first, Entities.second,
223 PPRec, FID);
224}
225
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000226bool CursorVisitor::visitFileRegion() {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000227 if (RegionOfInterest.isInvalid())
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000228 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000229
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000230 ASTUnit *Unit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000231 SourceManager &SM = Unit->getSourceManager();
232
233 std::pair<FileID, unsigned>
234 Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())),
235 End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd()));
236
237 if (End.first != Begin.first) {
238 // If the end does not reside in the same file, try to recover by
239 // picking the end of the file of begin location.
240 End.first = Begin.first;
241 End.second = SM.getFileIDSize(Begin.first);
242 }
243
244 assert(Begin.first == End.first);
245 if (Begin.second > End.second)
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000246 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000247
248 FileID File = Begin.first;
249 unsigned Offset = Begin.second;
250 unsigned Length = End.second - Begin.second;
251
252 if (!VisitDeclsOnly && !VisitPreprocessorLast)
253 if (visitPreprocessedEntitiesInRegion())
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000254 return true; // visitation break.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000255
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000256 if (visitDeclsFromFileRegion(File, Offset, Length))
257 return true; // visitation break.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000258
259 if (!VisitDeclsOnly && VisitPreprocessorLast)
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000260 return visitPreprocessedEntitiesInRegion();
261
262 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000263}
264
265static bool isInLexicalContext(Decl *D, DeclContext *DC) {
266 if (!DC)
267 return false;
268
269 for (DeclContext *DeclDC = D->getLexicalDeclContext();
270 DeclDC; DeclDC = DeclDC->getLexicalParent()) {
271 if (DeclDC == DC)
272 return true;
273 }
274 return false;
275}
276
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000277bool CursorVisitor::visitDeclsFromFileRegion(FileID File,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000278 unsigned Offset, unsigned Length) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000279 ASTUnit *Unit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000280 SourceManager &SM = Unit->getSourceManager();
281 SourceRange Range = RegionOfInterest;
282
283 SmallVector<Decl *, 16> Decls;
284 Unit->findFileRegionDecls(File, Offset, Length, Decls);
285
286 // If we didn't find any file level decls for the file, try looking at the
287 // file that it was included from.
288 while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
289 bool Invalid = false;
290 const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
291 if (Invalid)
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000292 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000293
294 SourceLocation Outer;
295 if (SLEntry.isFile())
296 Outer = SLEntry.getFile().getIncludeLoc();
297 else
298 Outer = SLEntry.getExpansion().getExpansionLocStart();
299 if (Outer.isInvalid())
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000300 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000301
302 llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
303 Length = 0;
304 Unit->findFileRegionDecls(File, Offset, Length, Decls);
305 }
306
307 assert(!Decls.empty());
308
309 bool VisitedAtLeastOnce = false;
310 DeclContext *CurDC = 0;
Craig Topper09d19ef2013-07-04 03:08:24 +0000311 SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
312 for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000313 Decl *D = *DIt;
314 if (D->getSourceRange().isInvalid())
315 continue;
316
317 if (isInLexicalContext(D, CurDC))
318 continue;
319
320 CurDC = dyn_cast<DeclContext>(D);
321
322 if (TagDecl *TD = dyn_cast<TagDecl>(D))
323 if (!TD->isFreeStanding())
324 continue;
325
326 RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range);
327 if (CompRes == RangeBefore)
328 continue;
329 if (CompRes == RangeAfter)
330 break;
331
332 assert(CompRes == RangeOverlap);
333 VisitedAtLeastOnce = true;
334
335 if (isa<ObjCContainerDecl>(D)) {
336 FileDI_current = &DIt;
337 FileDE_current = DE;
338 } else {
339 FileDI_current = 0;
340 }
341
342 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000343 return true; // visitation break.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000344 }
345
346 if (VisitedAtLeastOnce)
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000347 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000348
349 // No Decls overlapped with the range. Move up the lexical context until there
350 // is a context that contains the range or we reach the translation unit
351 // level.
352 DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext()
353 : (*(DIt-1))->getLexicalDeclContext();
354
355 while (DC && !DC->isTranslationUnit()) {
356 Decl *D = cast<Decl>(DC);
357 SourceRange CurDeclRange = D->getSourceRange();
358 if (CurDeclRange.isInvalid())
359 break;
360
361 if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000362 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
363 return true; // visitation break.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000364 }
365
366 DC = D->getLexicalDeclContext();
367 }
Argyrios Kyrtzidis389dc562013-03-08 20:42:33 +0000368
369 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000370}
371
372bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
373 if (!AU->getPreprocessor().getPreprocessingRecord())
374 return false;
375
376 PreprocessingRecord &PPRec
377 = *AU->getPreprocessor().getPreprocessingRecord();
378 SourceManager &SM = AU->getSourceManager();
379
380 if (RegionOfInterest.isValid()) {
381 SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
382 SourceLocation B = MappedRange.getBegin();
383 SourceLocation E = MappedRange.getEnd();
384
385 if (AU->isInPreambleFileID(B)) {
386 if (SM.isLoadedSourceLocation(E))
387 return visitPreprocessedEntitiesInRange(SourceRange(B, E),
388 PPRec, *this);
389
390 // Beginning of range lies in the preamble but it also extends beyond
391 // it into the main file. Split the range into 2 parts, one covering
392 // the preamble and another covering the main file. This allows subsequent
393 // calls to visitPreprocessedEntitiesInRange to accept a source range that
394 // lies in the same FileID, allowing it to skip preprocessed entities that
395 // do not come from the same FileID.
396 bool breaked =
397 visitPreprocessedEntitiesInRange(
398 SourceRange(B, AU->getEndOfPreambleFileID()),
399 PPRec, *this);
400 if (breaked) return true;
401 return visitPreprocessedEntitiesInRange(
402 SourceRange(AU->getStartOfMainFileID(), E),
403 PPRec, *this);
404 }
405
406 return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
407 }
408
409 bool OnlyLocalDecls
410 = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
411
412 if (OnlyLocalDecls)
413 return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
414 PPRec);
415
416 return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
417}
418
419template<typename InputIterator>
420bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
421 InputIterator Last,
422 PreprocessingRecord &PPRec,
423 FileID FID) {
424 for (; First != Last; ++First) {
425 if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
426 continue;
427
428 PreprocessedEntity *PPE = *First;
Argyrios Kyrtzidis333e44c2013-05-07 20:37:17 +0000429 if (!PPE)
430 continue;
431
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000432 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
433 if (Visit(MakeMacroExpansionCursor(ME, TU)))
434 return true;
435
436 continue;
437 }
438
439 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) {
440 if (Visit(MakeMacroDefinitionCursor(MD, TU)))
441 return true;
442
443 continue;
444 }
445
446 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
447 if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
448 return true;
449
450 continue;
451 }
452 }
453
454 return false;
455}
456
457/// \brief Visit the children of the given cursor.
458///
459/// \returns true if the visitation should be aborted, false if it
460/// should continue.
461bool CursorVisitor::VisitChildren(CXCursor Cursor) {
462 if (clang_isReference(Cursor.kind) &&
463 Cursor.kind != CXCursor_CXXBaseSpecifier) {
464 // By definition, references have no children.
465 return false;
466 }
467
468 // Set the Parent field to Cursor, then back to its old value once we're
469 // done.
470 SetParentRAII SetParent(Parent, StmtParent, Cursor);
471
472 if (clang_isDeclaration(Cursor.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000473 Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000474 if (!D)
475 return false;
476
477 return VisitAttributes(D) || Visit(D);
478 }
479
480 if (clang_isStatement(Cursor.kind)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000481 if (const Stmt *S = getCursorStmt(Cursor))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000482 return Visit(S);
483
484 return false;
485 }
486
487 if (clang_isExpression(Cursor.kind)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000488 if (const Expr *E = getCursorExpr(Cursor))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000489 return Visit(E);
490
491 return false;
492 }
493
494 if (clang_isTranslationUnit(Cursor.kind)) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000495 CXTranslationUnit TU = getCursorTU(Cursor);
496 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000497
498 int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast };
499 for (unsigned I = 0; I != 2; ++I) {
500 if (VisitOrder[I]) {
501 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
502 RegionOfInterest.isInvalid()) {
503 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
504 TLEnd = CXXUnit->top_level_end();
505 TL != TLEnd; ++TL) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000506 if (Visit(MakeCXCursor(*TL, TU, RegionOfInterest), true))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000507 return true;
508 }
509 } else if (VisitDeclContext(
510 CXXUnit->getASTContext().getTranslationUnitDecl()))
511 return true;
512 continue;
513 }
514
515 // Walk the preprocessing record.
516 if (CXXUnit->getPreprocessor().getPreprocessingRecord())
517 visitPreprocessedEntitiesInRegion();
518 }
519
520 return false;
521 }
522
523 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000524 if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000525 if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
526 return Visit(BaseTSInfo->getTypeLoc());
527 }
528 }
529 }
530
531 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000532 const IBOutletCollectionAttr *A =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000533 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
534 if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>())
535 return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(),
536 A->getInterfaceLoc(), TU));
537 }
538
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000539 // If pointing inside a macro definition, check if the token is an identifier
540 // that was ever defined as a macro. In such a case, create a "pseudo" macro
541 // expansion cursor for that token.
542 SourceLocation BeginLoc = RegionOfInterest.getBegin();
543 if (Cursor.kind == CXCursor_MacroDefinition &&
544 BeginLoc == RegionOfInterest.getEnd()) {
545 SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000546 const MacroInfo *MI =
547 getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000548 if (MacroDefinition *MacroDef =
549 checkForMacroInMacroDefinition(MI, Loc, TU))
550 return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU));
551 }
552
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000553 // Nothing to visit at the moment.
554 return false;
555}
556
557bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
558 if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
559 if (Visit(TSInfo->getTypeLoc()))
560 return true;
561
562 if (Stmt *Body = B->getBody())
563 return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
564
565 return false;
566}
567
Ted Kremenek943f9092013-02-21 01:29:01 +0000568Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000569 if (RegionOfInterest.isValid()) {
570 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
571 if (Range.isInvalid())
David Blaikie66874fb2013-02-21 01:47:18 +0000572 return None;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000573
574 switch (CompareRegionOfInterest(Range)) {
575 case RangeBefore:
576 // This declaration comes before the region of interest; skip it.
David Blaikie66874fb2013-02-21 01:47:18 +0000577 return None;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000578
579 case RangeAfter:
580 // This declaration comes after the region of interest; we're done.
581 return false;
582
583 case RangeOverlap:
584 // This declaration overlaps the region of interest; visit it.
585 break;
586 }
587 }
588 return true;
589}
590
591bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
592 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
593
594 // FIXME: Eventually remove. This part of a hack to support proper
595 // iteration over all Decls contained lexically within an ObjC container.
596 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
597 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
598
599 for ( ; I != E; ++I) {
600 Decl *D = *I;
601 if (D->getLexicalDeclContext() != DC)
602 continue;
603 CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
604
605 // Ignore synthesized ivars here, otherwise if we have something like:
606 // @synthesize prop = _prop;
607 // and '_prop' is not declared, we will encounter a '_prop' ivar before
608 // encountering the 'prop' synthesize declaration and we will think that
609 // we passed the region-of-interest.
610 if (ObjCIvarDecl *ivarD = dyn_cast<ObjCIvarDecl>(D)) {
611 if (ivarD->getSynthesize())
612 continue;
613 }
614
615 // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
616 // declarations is a mismatch with the compiler semantics.
617 if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
618 ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D);
619 if (!ID->isThisDeclarationADefinition())
620 Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
621
622 } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
623 ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
624 if (!PD->isThisDeclarationADefinition())
625 Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
626 }
627
Ted Kremenek943f9092013-02-21 01:29:01 +0000628 const Optional<bool> &V = shouldVisitCursor(Cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000629 if (!V.hasValue())
630 continue;
631 if (!V.getValue())
632 return false;
633 if (Visit(Cursor, true))
634 return true;
635 }
636 return false;
637}
638
639bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
640 llvm_unreachable("Translation units are visited directly by Visit()");
641}
642
643bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
644 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
645 return Visit(TSInfo->getTypeLoc());
646
647 return false;
648}
649
650bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
651 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
652 return Visit(TSInfo->getTypeLoc());
653
654 return false;
655}
656
657bool CursorVisitor::VisitTagDecl(TagDecl *D) {
658 return VisitDeclContext(D);
659}
660
661bool CursorVisitor::VisitClassTemplateSpecializationDecl(
662 ClassTemplateSpecializationDecl *D) {
663 bool ShouldVisitBody = false;
664 switch (D->getSpecializationKind()) {
665 case TSK_Undeclared:
666 case TSK_ImplicitInstantiation:
667 // Nothing to visit
668 return false;
669
670 case TSK_ExplicitInstantiationDeclaration:
671 case TSK_ExplicitInstantiationDefinition:
672 break;
673
674 case TSK_ExplicitSpecialization:
675 ShouldVisitBody = true;
676 break;
677 }
678
679 // Visit the template arguments used in the specialization.
680 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
681 TypeLoc TL = SpecType->getTypeLoc();
David Blaikie39e6ab42013-02-18 22:06:02 +0000682 if (TemplateSpecializationTypeLoc TSTLoc =
683 TL.getAs<TemplateSpecializationTypeLoc>()) {
684 for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I)
685 if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000686 return true;
687 }
688 }
689
690 if (ShouldVisitBody && VisitCXXRecordDecl(D))
691 return true;
692
693 return false;
694}
695
696bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
697 ClassTemplatePartialSpecializationDecl *D) {
698 // FIXME: Visit the "outer" template parameter lists on the TagDecl
699 // before visiting these template parameters.
700 if (VisitTemplateParameters(D->getTemplateParameters()))
701 return true;
702
703 // Visit the partial specialization arguments.
704 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
705 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
706 if (VisitTemplateArgumentLoc(TemplateArgs[I]))
707 return true;
708
709 return VisitCXXRecordDecl(D);
710}
711
712bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
713 // Visit the default argument.
714 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
715 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
716 if (Visit(DefArg->getTypeLoc()))
717 return true;
718
719 return false;
720}
721
722bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
723 if (Expr *Init = D->getInitExpr())
724 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
725 return false;
726}
727
728bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis516143b2013-04-05 21:04:10 +0000729 unsigned NumParamList = DD->getNumTemplateParameterLists();
730 for (unsigned i = 0; i < NumParamList; i++) {
731 TemplateParameterList* Params = DD->getTemplateParameterList(i);
732 if (VisitTemplateParameters(Params))
733 return true;
734 }
735
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000736 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
737 if (Visit(TSInfo->getTypeLoc()))
738 return true;
739
740 // Visit the nested-name-specifier, if present.
741 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
742 if (VisitNestedNameSpecifierLoc(QualifierLoc))
743 return true;
744
745 return false;
746}
747
748/// \brief Compare two base or member initializers based on their source order.
749static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
750 CXXCtorInitializer const * const *X
751 = static_cast<CXXCtorInitializer const * const *>(Xp);
752 CXXCtorInitializer const * const *Y
753 = static_cast<CXXCtorInitializer const * const *>(Yp);
754
755 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
756 return -1;
757 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
758 return 1;
759 else
760 return 0;
761}
762
763bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Argyrios Kyrtzidis516143b2013-04-05 21:04:10 +0000764 unsigned NumParamList = ND->getNumTemplateParameterLists();
765 for (unsigned i = 0; i < NumParamList; i++) {
766 TemplateParameterList* Params = ND->getTemplateParameterList(i);
767 if (VisitTemplateParameters(Params))
768 return true;
769 }
770
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000771 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
772 // Visit the function declaration's syntactic components in the order
773 // written. This requires a bit of work.
774 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +0000775 FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000776
777 // If we have a function declared directly (without the use of a typedef),
778 // visit just the return type. Otherwise, just visit the function's type
779 // now.
David Blaikie39e6ab42013-02-18 22:06:02 +0000780 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getResultLoc())) ||
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000781 (!FTL && Visit(TL)))
782 return true;
783
784 // Visit the nested-name-specifier, if present.
785 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
786 if (VisitNestedNameSpecifierLoc(QualifierLoc))
787 return true;
788
789 // Visit the declaration name.
790 if (VisitDeclarationNameInfo(ND->getNameInfo()))
791 return true;
792
793 // FIXME: Visit explicitly-specified template arguments!
794
795 // Visit the function parameters, if we have a function type.
David Blaikie39e6ab42013-02-18 22:06:02 +0000796 if (FTL && VisitFunctionTypeLoc(FTL, true))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000797 return true;
798
Bill Wendlingad017fa2012-12-20 19:22:21 +0000799 // FIXME: Attributes?
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000800 }
801
802 if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
803 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
804 // Find the initializers that were written in the source.
805 SmallVector<CXXCtorInitializer *, 4> WrittenInits;
806 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
807 IEnd = Constructor->init_end();
808 I != IEnd; ++I) {
809 if (!(*I)->isWritten())
810 continue;
811
812 WrittenInits.push_back(*I);
813 }
814
815 // Sort the initializers in source order
816 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
817 &CompareCXXCtorInitializers);
818
819 // Visit the initializers in source order
820 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
821 CXXCtorInitializer *Init = WrittenInits[I];
822 if (Init->isAnyMemberInitializer()) {
823 if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
824 Init->getMemberLocation(), TU)))
825 return true;
826 } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
827 if (Visit(TInfo->getTypeLoc()))
828 return true;
829 }
830
831 // Visit the initializer value.
832 if (Expr *Initializer = Init->getInit())
833 if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
834 return true;
835 }
836 }
837
838 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
839 return true;
840 }
841
842 return false;
843}
844
845bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
846 if (VisitDeclaratorDecl(D))
847 return true;
848
849 if (Expr *BitWidth = D->getBitWidth())
850 return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
851
852 return false;
853}
854
855bool CursorVisitor::VisitVarDecl(VarDecl *D) {
856 if (VisitDeclaratorDecl(D))
857 return true;
858
859 if (Expr *Init = D->getInit())
860 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
861
862 return false;
863}
864
865bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
866 if (VisitDeclaratorDecl(D))
867 return true;
868
869 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
870 if (Expr *DefArg = D->getDefaultArgument())
871 return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
872
873 return false;
874}
875
876bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
877 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
878 // before visiting these template parameters.
879 if (VisitTemplateParameters(D->getTemplateParameters()))
880 return true;
881
882 return VisitFunctionDecl(D->getTemplatedDecl());
883}
884
885bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
886 // FIXME: Visit the "outer" template parameter lists on the TagDecl
887 // before visiting these template parameters.
888 if (VisitTemplateParameters(D->getTemplateParameters()))
889 return true;
890
891 return VisitCXXRecordDecl(D->getTemplatedDecl());
892}
893
894bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
895 if (VisitTemplateParameters(D->getTemplateParameters()))
896 return true;
897
898 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
899 VisitTemplateArgumentLoc(D->getDefaultArgument()))
900 return true;
901
902 return false;
903}
904
905bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
906 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
907 if (Visit(TSInfo->getTypeLoc()))
908 return true;
909
910 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
911 PEnd = ND->param_end();
912 P != PEnd; ++P) {
913 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
914 return true;
915 }
916
917 if (ND->isThisDeclarationADefinition() &&
918 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
919 return true;
920
921 return false;
922}
923
924template <typename DeclIt>
925static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
926 SourceManager &SM, SourceLocation EndLoc,
927 SmallVectorImpl<Decl *> &Decls) {
928 DeclIt next = *DI_current;
929 while (++next != DE_current) {
930 Decl *D_next = *next;
931 if (!D_next)
932 break;
933 SourceLocation L = D_next->getLocStart();
934 if (!L.isValid())
935 break;
936 if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
937 *DI_current = next;
938 Decls.push_back(D_next);
939 continue;
940 }
941 break;
942 }
943}
944
945namespace {
946 struct ContainerDeclsSort {
947 SourceManager &SM;
948 ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
949 bool operator()(Decl *A, Decl *B) {
950 SourceLocation L_A = A->getLocStart();
951 SourceLocation L_B = B->getLocStart();
952 assert(L_A.isValid() && L_B.isValid());
953 return SM.isBeforeInTranslationUnit(L_A, L_B);
954 }
955 };
956}
957
958bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
959 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
960 // an @implementation can lexically contain Decls that are not properly
961 // nested in the AST. When we identify such cases, we need to retrofit
962 // this nesting here.
963 if (!DI_current && !FileDI_current)
964 return VisitDeclContext(D);
965
966 // Scan the Decls that immediately come after the container
967 // in the current DeclContext. If any fall within the
968 // container's lexical region, stash them into a vector
969 // for later processing.
970 SmallVector<Decl *, 24> DeclsInContainer;
971 SourceLocation EndLoc = D->getSourceRange().getEnd();
972 SourceManager &SM = AU->getSourceManager();
973 if (EndLoc.isValid()) {
974 if (DI_current) {
975 addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
976 DeclsInContainer);
977 } else {
978 addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
979 DeclsInContainer);
980 }
981 }
982
983 // The common case.
984 if (DeclsInContainer.empty())
985 return VisitDeclContext(D);
986
987 // Get all the Decls in the DeclContext, and sort them with the
988 // additional ones we've collected. Then visit them.
989 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
990 I!=E; ++I) {
991 Decl *subDecl = *I;
992 if (!subDecl || subDecl->getLexicalDeclContext() != D ||
993 subDecl->getLocStart().isInvalid())
994 continue;
995 DeclsInContainer.push_back(subDecl);
996 }
997
998 // Now sort the Decls so that they appear in lexical order.
999 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
1000 ContainerDeclsSort(SM));
1001
1002 // Now visit the decls.
1003 for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
1004 E = DeclsInContainer.end(); I != E; ++I) {
1005 CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
Ted Kremenek943f9092013-02-21 01:29:01 +00001006 const Optional<bool> &V = shouldVisitCursor(Cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001007 if (!V.hasValue())
1008 continue;
1009 if (!V.getValue())
1010 return false;
1011 if (Visit(Cursor, true))
1012 return true;
1013 }
1014 return false;
1015}
1016
1017bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
1018 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
1019 TU)))
1020 return true;
1021
1022 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
1023 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
1024 E = ND->protocol_end(); I != E; ++I, ++PL)
1025 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1026 return true;
1027
1028 return VisitObjCContainerDecl(ND);
1029}
1030
1031bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1032 if (!PID->isThisDeclarationADefinition())
1033 return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
1034
1035 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
1036 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
1037 E = PID->protocol_end(); I != E; ++I, ++PL)
1038 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1039 return true;
1040
1041 return VisitObjCContainerDecl(PID);
1042}
1043
1044bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
1045 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
1046 return true;
1047
1048 // FIXME: This implements a workaround with @property declarations also being
1049 // installed in the DeclContext for the @interface. Eventually this code
1050 // should be removed.
1051 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
1052 if (!CDecl || !CDecl->IsClassExtension())
1053 return false;
1054
1055 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
1056 if (!ID)
1057 return false;
1058
1059 IdentifierInfo *PropertyId = PD->getIdentifier();
1060 ObjCPropertyDecl *prevDecl =
1061 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
1062
1063 if (!prevDecl)
1064 return false;
1065
1066 // Visit synthesized methods since they will be skipped when visiting
1067 // the @interface.
1068 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1069 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1070 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1071 return true;
1072
1073 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1074 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1075 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1076 return true;
1077
1078 return false;
1079}
1080
1081bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1082 if (!D->isThisDeclarationADefinition()) {
1083 // Forward declaration is treated like a reference.
1084 return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
1085 }
1086
1087 // Issue callbacks for super class.
1088 if (D->getSuperClass() &&
1089 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1090 D->getSuperClassLoc(),
1091 TU)))
1092 return true;
1093
1094 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1095 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1096 E = D->protocol_end(); I != E; ++I, ++PL)
1097 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1098 return true;
1099
1100 return VisitObjCContainerDecl(D);
1101}
1102
1103bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1104 return VisitObjCContainerDecl(D);
1105}
1106
1107bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1108 // 'ID' could be null when dealing with invalid code.
1109 if (ObjCInterfaceDecl *ID = D->getClassInterface())
1110 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1111 return true;
1112
1113 return VisitObjCImplDecl(D);
1114}
1115
1116bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1117#if 0
1118 // Issue callbacks for super class.
1119 // FIXME: No source location information!
1120 if (D->getSuperClass() &&
1121 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1122 D->getSuperClassLoc(),
1123 TU)))
1124 return true;
1125#endif
1126
1127 return VisitObjCImplDecl(D);
1128}
1129
1130bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1131 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1132 if (PD->isIvarNameSpecified())
1133 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1134
1135 return false;
1136}
1137
1138bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1139 return VisitDeclContext(D);
1140}
1141
1142bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1143 // Visit nested-name-specifier.
1144 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1145 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1146 return true;
1147
1148 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1149 D->getTargetNameLoc(), TU));
1150}
1151
1152bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1153 // Visit nested-name-specifier.
1154 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1155 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1156 return true;
1157 }
1158
1159 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1160 return true;
1161
1162 return VisitDeclarationNameInfo(D->getNameInfo());
1163}
1164
1165bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1166 // Visit nested-name-specifier.
1167 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1168 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1169 return true;
1170
1171 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1172 D->getIdentLocation(), TU));
1173}
1174
1175bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1176 // Visit nested-name-specifier.
1177 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1178 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1179 return true;
1180 }
1181
1182 return VisitDeclarationNameInfo(D->getNameInfo());
1183}
1184
1185bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1186 UnresolvedUsingTypenameDecl *D) {
1187 // Visit nested-name-specifier.
1188 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1189 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1190 return true;
1191
1192 return false;
1193}
1194
1195bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1196 switch (Name.getName().getNameKind()) {
1197 case clang::DeclarationName::Identifier:
1198 case clang::DeclarationName::CXXLiteralOperatorName:
1199 case clang::DeclarationName::CXXOperatorName:
1200 case clang::DeclarationName::CXXUsingDirective:
1201 return false;
1202
1203 case clang::DeclarationName::CXXConstructorName:
1204 case clang::DeclarationName::CXXDestructorName:
1205 case clang::DeclarationName::CXXConversionFunctionName:
1206 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1207 return Visit(TSInfo->getTypeLoc());
1208 return false;
1209
1210 case clang::DeclarationName::ObjCZeroArgSelector:
1211 case clang::DeclarationName::ObjCOneArgSelector:
1212 case clang::DeclarationName::ObjCMultiArgSelector:
1213 // FIXME: Per-identifier location info?
1214 return false;
1215 }
1216
1217 llvm_unreachable("Invalid DeclarationName::Kind!");
1218}
1219
1220bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1221 SourceRange Range) {
1222 // FIXME: This whole routine is a hack to work around the lack of proper
1223 // source information in nested-name-specifiers (PR5791). Since we do have
1224 // a beginning source location, we can visit the first component of the
1225 // nested-name-specifier, if it's a single-token component.
1226 if (!NNS)
1227 return false;
1228
1229 // Get the first component in the nested-name-specifier.
1230 while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1231 NNS = Prefix;
1232
1233 switch (NNS->getKind()) {
1234 case NestedNameSpecifier::Namespace:
1235 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1236 TU));
1237
1238 case NestedNameSpecifier::NamespaceAlias:
1239 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1240 Range.getBegin(), TU));
1241
1242 case NestedNameSpecifier::TypeSpec: {
1243 // If the type has a form where we know that the beginning of the source
1244 // range matches up with a reference cursor. Visit the appropriate reference
1245 // cursor.
1246 const Type *T = NNS->getAsType();
1247 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1248 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1249 if (const TagType *Tag = dyn_cast<TagType>(T))
1250 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1251 if (const TemplateSpecializationType *TST
1252 = dyn_cast<TemplateSpecializationType>(T))
1253 return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1254 break;
1255 }
1256
1257 case NestedNameSpecifier::TypeSpecWithTemplate:
1258 case NestedNameSpecifier::Global:
1259 case NestedNameSpecifier::Identifier:
1260 break;
1261 }
1262
1263 return false;
1264}
1265
1266bool
1267CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1268 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1269 for (; Qualifier; Qualifier = Qualifier.getPrefix())
1270 Qualifiers.push_back(Qualifier);
1271
1272 while (!Qualifiers.empty()) {
1273 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1274 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1275 switch (NNS->getKind()) {
1276 case NestedNameSpecifier::Namespace:
1277 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
1278 Q.getLocalBeginLoc(),
1279 TU)))
1280 return true;
1281
1282 break;
1283
1284 case NestedNameSpecifier::NamespaceAlias:
1285 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1286 Q.getLocalBeginLoc(),
1287 TU)))
1288 return true;
1289
1290 break;
1291
1292 case NestedNameSpecifier::TypeSpec:
1293 case NestedNameSpecifier::TypeSpecWithTemplate:
1294 if (Visit(Q.getTypeLoc()))
1295 return true;
1296
1297 break;
1298
1299 case NestedNameSpecifier::Global:
1300 case NestedNameSpecifier::Identifier:
1301 break;
1302 }
1303 }
1304
1305 return false;
1306}
1307
1308bool CursorVisitor::VisitTemplateParameters(
1309 const TemplateParameterList *Params) {
1310 if (!Params)
1311 return false;
1312
1313 for (TemplateParameterList::const_iterator P = Params->begin(),
1314 PEnd = Params->end();
1315 P != PEnd; ++P) {
1316 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
1317 return true;
1318 }
1319
1320 return false;
1321}
1322
1323bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1324 switch (Name.getKind()) {
1325 case TemplateName::Template:
1326 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1327
1328 case TemplateName::OverloadedTemplate:
1329 // Visit the overloaded template set.
1330 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1331 return true;
1332
1333 return false;
1334
1335 case TemplateName::DependentTemplate:
1336 // FIXME: Visit nested-name-specifier.
1337 return false;
1338
1339 case TemplateName::QualifiedTemplate:
1340 // FIXME: Visit nested-name-specifier.
1341 return Visit(MakeCursorTemplateRef(
1342 Name.getAsQualifiedTemplateName()->getDecl(),
1343 Loc, TU));
1344
1345 case TemplateName::SubstTemplateTemplateParm:
1346 return Visit(MakeCursorTemplateRef(
1347 Name.getAsSubstTemplateTemplateParm()->getParameter(),
1348 Loc, TU));
1349
1350 case TemplateName::SubstTemplateTemplateParmPack:
1351 return Visit(MakeCursorTemplateRef(
1352 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
1353 Loc, TU));
1354 }
1355
1356 llvm_unreachable("Invalid TemplateName::Kind!");
1357}
1358
1359bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1360 switch (TAL.getArgument().getKind()) {
1361 case TemplateArgument::Null:
1362 case TemplateArgument::Integral:
1363 case TemplateArgument::Pack:
1364 return false;
1365
1366 case TemplateArgument::Type:
1367 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1368 return Visit(TSInfo->getTypeLoc());
1369 return false;
1370
1371 case TemplateArgument::Declaration:
1372 if (Expr *E = TAL.getSourceDeclExpression())
1373 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1374 return false;
1375
1376 case TemplateArgument::NullPtr:
1377 if (Expr *E = TAL.getSourceNullPtrExpression())
1378 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1379 return false;
1380
1381 case TemplateArgument::Expression:
1382 if (Expr *E = TAL.getSourceExpression())
1383 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1384 return false;
1385
1386 case TemplateArgument::Template:
1387 case TemplateArgument::TemplateExpansion:
1388 if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1389 return true;
1390
1391 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
1392 TAL.getTemplateNameLoc());
1393 }
1394
1395 llvm_unreachable("Invalid TemplateArgument::Kind!");
1396}
1397
1398bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1399 return VisitDeclContext(D);
1400}
1401
1402bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1403 return Visit(TL.getUnqualifiedLoc());
1404}
1405
1406bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1407 ASTContext &Context = AU->getASTContext();
1408
1409 // Some builtin types (such as Objective-C's "id", "sel", and
1410 // "Class") have associated declarations. Create cursors for those.
1411 QualType VisitType;
1412 switch (TL.getTypePtr()->getKind()) {
1413
1414 case BuiltinType::Void:
1415 case BuiltinType::NullPtr:
1416 case BuiltinType::Dependent:
Guy Benyeib13621d2012-12-18 14:38:23 +00001417 case BuiltinType::OCLImage1d:
1418 case BuiltinType::OCLImage1dArray:
1419 case BuiltinType::OCLImage1dBuffer:
1420 case BuiltinType::OCLImage2d:
1421 case BuiltinType::OCLImage2dArray:
1422 case BuiltinType::OCLImage3d:
NAKAMURA Takumi775bb8a2013-02-07 12:47:42 +00001423 case BuiltinType::OCLSampler:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001424 case BuiltinType::OCLEvent:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001425#define BUILTIN_TYPE(Id, SingletonId)
1426#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1427#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1428#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1429#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1430#include "clang/AST/BuiltinTypes.def"
1431 break;
1432
1433 case BuiltinType::ObjCId:
1434 VisitType = Context.getObjCIdType();
1435 break;
1436
1437 case BuiltinType::ObjCClass:
1438 VisitType = Context.getObjCClassType();
1439 break;
1440
1441 case BuiltinType::ObjCSel:
1442 VisitType = Context.getObjCSelType();
1443 break;
1444 }
1445
1446 if (!VisitType.isNull()) {
1447 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1448 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
1449 TU));
1450 }
1451
1452 return false;
1453}
1454
1455bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1456 return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
1457}
1458
1459bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1460 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1461}
1462
1463bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1464 if (TL.isDefinition())
1465 return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
1466
1467 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1468}
1469
1470bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1471 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1472}
1473
1474bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1475 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1476 return true;
1477
1478 return false;
1479}
1480
1481bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1482 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1483 return true;
1484
1485 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1486 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1487 TU)))
1488 return true;
1489 }
1490
1491 return false;
1492}
1493
1494bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1495 return Visit(TL.getPointeeLoc());
1496}
1497
1498bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1499 return Visit(TL.getInnerLoc());
1500}
1501
1502bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1503 return Visit(TL.getPointeeLoc());
1504}
1505
1506bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1507 return Visit(TL.getPointeeLoc());
1508}
1509
1510bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1511 return Visit(TL.getPointeeLoc());
1512}
1513
1514bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1515 return Visit(TL.getPointeeLoc());
1516}
1517
1518bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1519 return Visit(TL.getPointeeLoc());
1520}
1521
1522bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1523 return Visit(TL.getModifiedLoc());
1524}
1525
1526bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1527 bool SkipResultType) {
1528 if (!SkipResultType && Visit(TL.getResultLoc()))
1529 return true;
1530
1531 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1532 if (Decl *D = TL.getArg(I))
1533 if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
1534 return true;
1535
1536 return false;
1537}
1538
1539bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1540 if (Visit(TL.getElementLoc()))
1541 return true;
1542
1543 if (Expr *Size = TL.getSizeExpr())
1544 return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
1545
1546 return false;
1547}
1548
Reid Kleckner12df2462013-06-24 17:51:48 +00001549bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
1550 return Visit(TL.getOriginalLoc());
1551}
1552
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001553bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1554 TemplateSpecializationTypeLoc TL) {
1555 // Visit the template name.
1556 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1557 TL.getTemplateNameLoc()))
1558 return true;
1559
1560 // Visit the template arguments.
1561 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1562 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1563 return true;
1564
1565 return false;
1566}
1567
1568bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1569 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1570}
1571
1572bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1573 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1574 return Visit(TSInfo->getTypeLoc());
1575
1576 return false;
1577}
1578
1579bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1580 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1581 return Visit(TSInfo->getTypeLoc());
1582
1583 return false;
1584}
1585
1586bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1587 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1588 return true;
1589
1590 return false;
1591}
1592
1593bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1594 DependentTemplateSpecializationTypeLoc TL) {
1595 // Visit the nested-name-specifier, if there is one.
1596 if (TL.getQualifierLoc() &&
1597 VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1598 return true;
1599
1600 // Visit the template arguments.
1601 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1602 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1603 return true;
1604
1605 return false;
1606}
1607
1608bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1609 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1610 return true;
1611
1612 return Visit(TL.getNamedTypeLoc());
1613}
1614
1615bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1616 return Visit(TL.getPatternLoc());
1617}
1618
1619bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1620 if (Expr *E = TL.getUnderlyingExpr())
1621 return Visit(MakeCXCursor(E, StmtParent, TU));
1622
1623 return false;
1624}
1625
1626bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1627 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1628}
1629
1630bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1631 return Visit(TL.getValueLoc());
1632}
1633
1634#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \
1635bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
1636 return Visit##PARENT##Loc(TL); \
1637}
1638
1639DEFAULT_TYPELOC_IMPL(Complex, Type)
1640DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
1641DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
1642DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
1643DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
1644DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
1645DEFAULT_TYPELOC_IMPL(Vector, Type)
1646DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
1647DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
1648DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
1649DEFAULT_TYPELOC_IMPL(Record, TagType)
1650DEFAULT_TYPELOC_IMPL(Enum, TagType)
1651DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
1652DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
1653DEFAULT_TYPELOC_IMPL(Auto, Type)
1654
1655bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1656 // Visit the nested-name-specifier, if present.
1657 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1658 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1659 return true;
1660
1661 if (D->isCompleteDefinition()) {
1662 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1663 E = D->bases_end(); I != E; ++I) {
1664 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
1665 return true;
1666 }
1667 }
1668
1669 return VisitTagDecl(D);
1670}
1671
1672bool CursorVisitor::VisitAttributes(Decl *D) {
1673 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
1674 i != e; ++i)
1675 if (Visit(MakeCXCursor(*i, D, TU)))
1676 return true;
1677
1678 return false;
1679}
1680
1681//===----------------------------------------------------------------------===//
1682// Data-recursive visitor methods.
1683//===----------------------------------------------------------------------===//
1684
1685namespace {
1686#define DEF_JOB(NAME, DATA, KIND)\
1687class NAME : public VisitorJob {\
1688public:\
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001689 NAME(const DATA *d, CXCursor parent) : \
1690 VisitorJob(parent, VisitorJob::KIND, d) {} \
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001691 static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001692 const DATA *get() const { return static_cast<const DATA*>(data[0]); }\
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001693};
1694
1695DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1696DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1697DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1698DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1699DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo,
1700 ExplicitTemplateArgsVisitKind)
1701DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1702DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1703DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
1704#undef DEF_JOB
1705
1706class DeclVisit : public VisitorJob {
1707public:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001708 DeclVisit(const Decl *D, CXCursor parent, bool isFirst) :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001709 VisitorJob(parent, VisitorJob::DeclVisitKind,
Dmitri Gribenkoa376f872013-02-03 13:19:54 +00001710 D, isFirst ? (void*) 1 : (void*) 0) {}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001711 static bool classof(const VisitorJob *VJ) {
1712 return VJ->getKind() == DeclVisitKind;
1713 }
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001714 const Decl *get() const { return static_cast<const Decl *>(data[0]); }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001715 bool isFirst() const { return data[1] ? true : false; }
1716};
1717class TypeLocVisit : public VisitorJob {
1718public:
1719 TypeLocVisit(TypeLoc tl, CXCursor parent) :
1720 VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1721 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1722
1723 static bool classof(const VisitorJob *VJ) {
1724 return VJ->getKind() == TypeLocVisitKind;
1725 }
1726
1727 TypeLoc get() const {
1728 QualType T = QualType::getFromOpaquePtr(data[0]);
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001729 return TypeLoc(T, const_cast<void *>(data[1]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001730 }
1731};
1732
1733class LabelRefVisit : public VisitorJob {
1734public:
1735 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
1736 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
1737 labelLoc.getPtrEncoding()) {}
1738
1739 static bool classof(const VisitorJob *VJ) {
1740 return VJ->getKind() == VisitorJob::LabelRefVisitKind;
1741 }
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001742 const LabelDecl *get() const {
1743 return static_cast<const LabelDecl *>(data[0]);
1744 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001745 SourceLocation getLoc() const {
1746 return SourceLocation::getFromPtrEncoding(data[1]); }
1747};
1748
1749class NestedNameSpecifierLocVisit : public VisitorJob {
1750public:
1751 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
1752 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
1753 Qualifier.getNestedNameSpecifier(),
1754 Qualifier.getOpaqueData()) { }
1755
1756 static bool classof(const VisitorJob *VJ) {
1757 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
1758 }
1759
1760 NestedNameSpecifierLoc get() const {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001761 return NestedNameSpecifierLoc(
1762 const_cast<NestedNameSpecifier *>(
1763 static_cast<const NestedNameSpecifier *>(data[0])),
1764 const_cast<void *>(data[1]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001765 }
1766};
1767
1768class DeclarationNameInfoVisit : public VisitorJob {
1769public:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001770 DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
Dmitri Gribenkoa376f872013-02-03 13:19:54 +00001771 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001772 static bool classof(const VisitorJob *VJ) {
1773 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
1774 }
1775 DeclarationNameInfo get() const {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001776 const Stmt *S = static_cast<const Stmt *>(data[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001777 switch (S->getStmtClass()) {
1778 default:
1779 llvm_unreachable("Unhandled Stmt");
1780 case clang::Stmt::MSDependentExistsStmtClass:
1781 return cast<MSDependentExistsStmt>(S)->getNameInfo();
1782 case Stmt::CXXDependentScopeMemberExprClass:
1783 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
1784 case Stmt::DependentScopeDeclRefExprClass:
1785 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
1786 }
1787 }
1788};
1789class MemberRefVisit : public VisitorJob {
1790public:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001791 MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001792 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
1793 L.getPtrEncoding()) {}
1794 static bool classof(const VisitorJob *VJ) {
1795 return VJ->getKind() == VisitorJob::MemberRefVisitKind;
1796 }
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001797 const FieldDecl *get() const {
1798 return static_cast<const FieldDecl *>(data[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001799 }
1800 SourceLocation getLoc() const {
1801 return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1802 }
1803};
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001804class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001805 friend class OMPClauseEnqueue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001806 VisitorWorkList &WL;
1807 CXCursor Parent;
1808public:
1809 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
1810 : WL(wl), Parent(parent) {}
1811
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001812 void VisitAddrLabelExpr(const AddrLabelExpr *E);
1813 void VisitBlockExpr(const BlockExpr *B);
1814 void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1815 void VisitCompoundStmt(const CompoundStmt *S);
1816 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ }
1817 void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
1818 void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
1819 void VisitCXXNewExpr(const CXXNewExpr *E);
1820 void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
1821 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
1822 void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
1823 void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
1824 void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1825 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
1826 void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
1827 void VisitCXXCatchStmt(const CXXCatchStmt *S);
1828 void VisitDeclRefExpr(const DeclRefExpr *D);
1829 void VisitDeclStmt(const DeclStmt *S);
1830 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
1831 void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
1832 void VisitExplicitCastExpr(const ExplicitCastExpr *E);
1833 void VisitForStmt(const ForStmt *FS);
1834 void VisitGotoStmt(const GotoStmt *GS);
1835 void VisitIfStmt(const IfStmt *If);
1836 void VisitInitListExpr(const InitListExpr *IE);
1837 void VisitMemberExpr(const MemberExpr *M);
1838 void VisitOffsetOfExpr(const OffsetOfExpr *E);
1839 void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1840 void VisitObjCMessageExpr(const ObjCMessageExpr *M);
1841 void VisitOverloadExpr(const OverloadExpr *E);
1842 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
1843 void VisitStmt(const Stmt *S);
1844 void VisitSwitchStmt(const SwitchStmt *S);
1845 void VisitWhileStmt(const WhileStmt *W);
1846 void VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E);
1847 void VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E);
1848 void VisitTypeTraitExpr(const TypeTraitExpr *E);
1849 void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
1850 void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
1851 void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
1852 void VisitVAArgExpr(const VAArgExpr *E);
1853 void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1854 void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
1855 void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
1856 void VisitLambdaExpr(const LambdaExpr *E);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001857 void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
1858 void VisitOMPParallelDirective(const OMPParallelDirective *D);
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001859
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001860private:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001861 void AddDeclarationNameInfo(const Stmt *S);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001862 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
1863 void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A);
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001864 void AddMemberRef(const FieldDecl *D, SourceLocation L);
1865 void AddStmt(const Stmt *S);
1866 void AddDecl(const Decl *D, bool isFirst = true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001867 void AddTypeLoc(TypeSourceInfo *TI);
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001868 void EnqueueChildren(const Stmt *S);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001869 void EnqueueChildren(const OMPClause *S);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001870};
1871} // end anonyous namespace
1872
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001873void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001874 // 'S' should always be non-null, since it comes from the
1875 // statement we are visiting.
1876 WL.push_back(DeclarationNameInfoVisit(S, Parent));
1877}
1878
1879void
1880EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1881 if (Qualifier)
1882 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
1883}
1884
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001885void EnqueueVisitor::AddStmt(const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001886 if (S)
1887 WL.push_back(StmtVisit(S, Parent));
1888}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001889void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001890 if (D)
1891 WL.push_back(DeclVisit(D, Parent, isFirst));
1892}
1893void EnqueueVisitor::
1894 AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) {
1895 if (A)
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001896 WL.push_back(ExplicitTemplateArgsVisit(A, Parent));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001897}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001898void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001899 if (D)
1900 WL.push_back(MemberRefVisit(D, L, Parent));
1901}
1902void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
1903 if (TI)
1904 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
1905 }
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001906void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001907 unsigned size = WL.size();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001908 for (Stmt::const_child_range Child = S->children(); Child; ++Child) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001909 AddStmt(*Child);
1910 }
1911 if (size == WL.size())
1912 return;
1913 // Now reverse the entries we just added. This will match the DFS
1914 // ordering performed by the worklist.
1915 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1916 std::reverse(I, E);
1917}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001918namespace {
1919class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
1920 EnqueueVisitor *Visitor;
1921public:
1922 OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { }
1923#define OPENMP_CLAUSE(Name, Class) \
1924 void Visit##Class(const Class *C);
1925#include "clang/Basic/OpenMPKinds.def"
1926};
1927
1928void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
1929#define PROCESS_OMP_CLAUSE_LIST(Class, Node) \
1930 for (OMPVarList<Class>::varlist_const_iterator I = Node->varlist_begin(), \
1931 E = Node->varlist_end(); \
1932 I != E; ++I) \
1933 Visitor->AddStmt(*I);
1934
1935void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
1936 PROCESS_OMP_CLAUSE_LIST(OMPPrivateClause, C)
1937}
1938#undef PROCESS_OMP_CLAUSE_LIST
1939}
1940void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
1941 unsigned size = WL.size();
1942 OMPClauseEnqueue Visitor(this);
1943 Visitor.Visit(S);
1944 if (size == WL.size())
1945 return;
1946 // Now reverse the entries we just added. This will match the DFS
1947 // ordering performed by the worklist.
1948 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1949 std::reverse(I, E);
1950}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001951void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001952 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
1953}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001954void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001955 AddDecl(B->getBlockDecl());
1956}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001957void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001958 EnqueueChildren(E);
1959 AddTypeLoc(E->getTypeSourceInfo());
1960}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001961void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
1962 for (CompoundStmt::const_reverse_body_iterator I = S->body_rbegin(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001963 E = S->body_rend(); I != E; ++I) {
1964 AddStmt(*I);
1965 }
1966}
1967void EnqueueVisitor::
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001968VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001969 AddStmt(S->getSubStmt());
1970 AddDeclarationNameInfo(S);
1971 if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
1972 AddNestedNameSpecifierLoc(QualifierLoc);
1973}
1974
1975void EnqueueVisitor::
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001976VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001977 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1978 AddDeclarationNameInfo(E);
1979 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1980 AddNestedNameSpecifierLoc(QualifierLoc);
1981 if (!E->isImplicitAccess())
1982 AddStmt(E->getBase());
1983}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001984void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001985 // Enqueue the initializer , if any.
1986 AddStmt(E->getInitializer());
1987 // Enqueue the array size, if any.
1988 AddStmt(E->getArraySize());
1989 // Enqueue the allocated type.
1990 AddTypeLoc(E->getAllocatedTypeSourceInfo());
1991 // Enqueue the placement arguments.
1992 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
1993 AddStmt(E->getPlacementArg(I-1));
1994}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00001995void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001996 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
1997 AddStmt(CE->getArg(I-1));
1998 AddStmt(CE->getCallee());
1999 AddStmt(CE->getArg(0));
2000}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002001void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
2002 const CXXPseudoDestructorExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002003 // Visit the name of the type being destroyed.
2004 AddTypeLoc(E->getDestroyedTypeInfo());
2005 // Visit the scope type that looks disturbingly like the nested-name-specifier
2006 // but isn't.
2007 AddTypeLoc(E->getScopeTypeInfo());
2008 // Visit the nested-name-specifier.
2009 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2010 AddNestedNameSpecifierLoc(QualifierLoc);
2011 // Visit base expression.
2012 AddStmt(E->getBase());
2013}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002014void EnqueueVisitor::VisitCXXScalarValueInitExpr(
2015 const CXXScalarValueInitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002016 AddTypeLoc(E->getTypeSourceInfo());
2017}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002018void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
2019 const CXXTemporaryObjectExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002020 EnqueueChildren(E);
2021 AddTypeLoc(E->getTypeSourceInfo());
2022}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002023void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002024 EnqueueChildren(E);
2025 if (E->isTypeOperand())
2026 AddTypeLoc(E->getTypeOperandSourceInfo());
2027}
2028
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002029void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
2030 const CXXUnresolvedConstructExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002031 EnqueueChildren(E);
2032 AddTypeLoc(E->getTypeSourceInfo());
2033}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002034void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002035 EnqueueChildren(E);
2036 if (E->isTypeOperand())
2037 AddTypeLoc(E->getTypeOperandSourceInfo());
2038}
2039
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002040void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002041 EnqueueChildren(S);
2042 AddDecl(S->getExceptionDecl());
2043}
2044
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002045void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002046 if (DR->hasExplicitTemplateArgs()) {
2047 AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
2048 }
2049 WL.push_back(DeclRefExprParts(DR, Parent));
2050}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002051void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
2052 const DependentScopeDeclRefExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002053 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2054 AddDeclarationNameInfo(E);
2055 AddNestedNameSpecifierLoc(E->getQualifierLoc());
2056}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002057void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002058 unsigned size = WL.size();
2059 bool isFirst = true;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002060 for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002061 D != DEnd; ++D) {
2062 AddDecl(*D, isFirst);
2063 isFirst = false;
2064 }
2065 if (size == WL.size())
2066 return;
2067 // Now reverse the entries we just added. This will match the DFS
2068 // ordering performed by the worklist.
2069 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2070 std::reverse(I, E);
2071}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002072void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002073 AddStmt(E->getInit());
2074 typedef DesignatedInitExpr::Designator Designator;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002075 for (DesignatedInitExpr::const_reverse_designators_iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002076 D = E->designators_rbegin(), DEnd = E->designators_rend();
2077 D != DEnd; ++D) {
2078 if (D->isFieldDesignator()) {
2079 if (FieldDecl *Field = D->getField())
2080 AddMemberRef(Field, D->getFieldLoc());
2081 continue;
2082 }
2083 if (D->isArrayDesignator()) {
2084 AddStmt(E->getArrayIndex(*D));
2085 continue;
2086 }
2087 assert(D->isArrayRangeDesignator() && "Unknown designator kind");
2088 AddStmt(E->getArrayRangeEnd(*D));
2089 AddStmt(E->getArrayRangeStart(*D));
2090 }
2091}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002092void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002093 EnqueueChildren(E);
2094 AddTypeLoc(E->getTypeInfoAsWritten());
2095}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002096void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002097 AddStmt(FS->getBody());
2098 AddStmt(FS->getInc());
2099 AddStmt(FS->getCond());
2100 AddDecl(FS->getConditionVariable());
2101 AddStmt(FS->getInit());
2102}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002103void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002104 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
2105}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002106void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002107 AddStmt(If->getElse());
2108 AddStmt(If->getThen());
2109 AddStmt(If->getCond());
2110 AddDecl(If->getConditionVariable());
2111}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002112void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002113 // We care about the syntactic form of the initializer list, only.
2114 if (InitListExpr *Syntactic = IE->getSyntacticForm())
2115 IE = Syntactic;
2116 EnqueueChildren(IE);
2117}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002118void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002119 WL.push_back(MemberExprParts(M, Parent));
2120
2121 // If the base of the member access expression is an implicit 'this', don't
2122 // visit it.
2123 // FIXME: If we ever want to show these implicit accesses, this will be
2124 // unfortunate. However, clang_getCursor() relies on this behavior.
2125 if (!M->isImplicitAccess())
2126 AddStmt(M->getBase());
2127}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002128void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002129 AddTypeLoc(E->getEncodedTypeSourceInfo());
2130}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002131void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002132 EnqueueChildren(M);
2133 AddTypeLoc(M->getClassReceiverTypeInfo());
2134}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002135void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002136 // Visit the components of the offsetof expression.
2137 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
2138 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
2139 const OffsetOfNode &Node = E->getComponent(I-1);
2140 switch (Node.getKind()) {
2141 case OffsetOfNode::Array:
2142 AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
2143 break;
2144 case OffsetOfNode::Field:
2145 AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
2146 break;
2147 case OffsetOfNode::Identifier:
2148 case OffsetOfNode::Base:
2149 continue;
2150 }
2151 }
2152 // Visit the type into which we're computing the offset.
2153 AddTypeLoc(E->getTypeSourceInfo());
2154}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002155void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002156 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2157 WL.push_back(OverloadExprParts(E, Parent));
2158}
2159void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002160 const UnaryExprOrTypeTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002161 EnqueueChildren(E);
2162 if (E->isArgumentType())
2163 AddTypeLoc(E->getArgumentTypeInfo());
2164}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002165void EnqueueVisitor::VisitStmt(const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002166 EnqueueChildren(S);
2167}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002168void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002169 AddStmt(S->getBody());
2170 AddStmt(S->getCond());
2171 AddDecl(S->getConditionVariable());
2172}
2173
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002174void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002175 AddStmt(W->getBody());
2176 AddStmt(W->getCond());
2177 AddDecl(W->getConditionVariable());
2178}
2179
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002180void EnqueueVisitor::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002181 AddTypeLoc(E->getQueriedTypeSourceInfo());
2182}
2183
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002184void EnqueueVisitor::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002185 AddTypeLoc(E->getRhsTypeSourceInfo());
2186 AddTypeLoc(E->getLhsTypeSourceInfo());
2187}
2188
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002189void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002190 for (unsigned I = E->getNumArgs(); I > 0; --I)
2191 AddTypeLoc(E->getArg(I-1));
2192}
2193
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002194void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002195 AddTypeLoc(E->getQueriedTypeSourceInfo());
2196}
2197
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002198void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002199 EnqueueChildren(E);
2200}
2201
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002202void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002203 VisitOverloadExpr(U);
2204 if (!U->isImplicitAccess())
2205 AddStmt(U->getBase());
2206}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002207void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002208 AddStmt(E->getSubExpr());
2209 AddTypeLoc(E->getWrittenTypeInfo());
2210}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002211void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002212 WL.push_back(SizeOfPackExprParts(E, Parent));
2213}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002214void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002215 // If the opaque value has a source expression, just transparently
2216 // visit that. This is useful for (e.g.) pseudo-object expressions.
2217 if (Expr *SourceExpr = E->getSourceExpr())
2218 return Visit(SourceExpr);
2219}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002220void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002221 AddStmt(E->getBody());
2222 WL.push_back(LambdaExprParts(E, Parent));
2223}
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002224void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002225 // Treat the expression like its syntactic form.
2226 Visit(E->getSyntacticForm());
2227}
2228
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00002229void EnqueueVisitor::VisitOMPExecutableDirective(
2230 const OMPExecutableDirective *D) {
2231 EnqueueChildren(D);
2232 for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
2233 E = D->clauses().end();
2234 I != E; ++I)
2235 EnqueueChildren(*I);
2236}
2237
2238void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
2239 VisitOMPExecutableDirective(D);
2240}
2241
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002242void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002243 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
2244}
2245
2246bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
2247 if (RegionOfInterest.isValid()) {
2248 SourceRange Range = getRawCursorExtent(C);
2249 if (Range.isInvalid() || CompareRegionOfInterest(Range))
2250 return false;
2251 }
2252 return true;
2253}
2254
2255bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
2256 while (!WL.empty()) {
2257 // Dequeue the worklist item.
2258 VisitorJob LI = WL.back();
2259 WL.pop_back();
2260
2261 // Set the Parent field, then back to its old value once we're done.
2262 SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
2263
2264 switch (LI.getKind()) {
2265 case VisitorJob::DeclVisitKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002266 const Decl *D = cast<DeclVisit>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002267 if (!D)
2268 continue;
2269
2270 // For now, perform default visitation for Decls.
2271 if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
2272 cast<DeclVisit>(&LI)->isFirst())))
2273 return true;
2274
2275 continue;
2276 }
2277 case VisitorJob::ExplicitTemplateArgsVisitKind: {
2278 const ASTTemplateArgumentListInfo *ArgList =
2279 cast<ExplicitTemplateArgsVisit>(&LI)->get();
2280 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
2281 *ArgEnd = Arg + ArgList->NumTemplateArgs;
2282 Arg != ArgEnd; ++Arg) {
2283 if (VisitTemplateArgumentLoc(*Arg))
2284 return true;
2285 }
2286 continue;
2287 }
2288 case VisitorJob::TypeLocVisitKind: {
2289 // Perform default visitation for TypeLocs.
2290 if (Visit(cast<TypeLocVisit>(&LI)->get()))
2291 return true;
2292 continue;
2293 }
2294 case VisitorJob::LabelRefVisitKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002295 const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002296 if (LabelStmt *stmt = LS->getStmt()) {
2297 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
2298 TU))) {
2299 return true;
2300 }
2301 }
2302 continue;
2303 }
2304
2305 case VisitorJob::NestedNameSpecifierLocVisitKind: {
2306 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
2307 if (VisitNestedNameSpecifierLoc(V->get()))
2308 return true;
2309 continue;
2310 }
2311
2312 case VisitorJob::DeclarationNameInfoVisitKind: {
2313 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
2314 ->get()))
2315 return true;
2316 continue;
2317 }
2318 case VisitorJob::MemberRefVisitKind: {
2319 MemberRefVisit *V = cast<MemberRefVisit>(&LI);
2320 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
2321 return true;
2322 continue;
2323 }
2324 case VisitorJob::StmtVisitKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002325 const Stmt *S = cast<StmtVisit>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002326 if (!S)
2327 continue;
2328
2329 // Update the current cursor.
2330 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
2331 if (!IsInRegionOfInterest(Cursor))
2332 continue;
2333 switch (Visitor(Cursor, Parent, ClientData)) {
2334 case CXChildVisit_Break: return true;
2335 case CXChildVisit_Continue: break;
2336 case CXChildVisit_Recurse:
2337 if (PostChildrenVisitor)
2338 WL.push_back(PostChildrenVisit(0, Cursor));
2339 EnqueueWorkList(WL, S);
2340 break;
2341 }
2342 continue;
2343 }
2344 case VisitorJob::MemberExprPartsKind: {
2345 // Handle the other pieces in the MemberExpr besides the base.
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002346 const MemberExpr *M = cast<MemberExprParts>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002347
2348 // Visit the nested-name-specifier
2349 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
2350 if (VisitNestedNameSpecifierLoc(QualifierLoc))
2351 return true;
2352
2353 // Visit the declaration name.
2354 if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
2355 return true;
2356
2357 // Visit the explicitly-specified template arguments, if any.
2358 if (M->hasExplicitTemplateArgs()) {
2359 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
2360 *ArgEnd = Arg + M->getNumTemplateArgs();
2361 Arg != ArgEnd; ++Arg) {
2362 if (VisitTemplateArgumentLoc(*Arg))
2363 return true;
2364 }
2365 }
2366 continue;
2367 }
2368 case VisitorJob::DeclRefExprPartsKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002369 const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002370 // Visit nested-name-specifier, if present.
2371 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
2372 if (VisitNestedNameSpecifierLoc(QualifierLoc))
2373 return true;
2374 // Visit declaration name.
2375 if (VisitDeclarationNameInfo(DR->getNameInfo()))
2376 return true;
2377 continue;
2378 }
2379 case VisitorJob::OverloadExprPartsKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002380 const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002381 // Visit the nested-name-specifier.
2382 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
2383 if (VisitNestedNameSpecifierLoc(QualifierLoc))
2384 return true;
2385 // Visit the declaration name.
2386 if (VisitDeclarationNameInfo(O->getNameInfo()))
2387 return true;
2388 // Visit the overloaded declaration reference.
2389 if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
2390 return true;
2391 continue;
2392 }
2393 case VisitorJob::SizeOfPackExprPartsKind: {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002394 const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002395 NamedDecl *Pack = E->getPack();
2396 if (isa<TemplateTypeParmDecl>(Pack)) {
2397 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
2398 E->getPackLoc(), TU)))
2399 return true;
2400
2401 continue;
2402 }
2403
2404 if (isa<TemplateTemplateParmDecl>(Pack)) {
2405 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
2406 E->getPackLoc(), TU)))
2407 return true;
2408
2409 continue;
2410 }
2411
2412 // Non-type template parameter packs and function parameter packs are
2413 // treated like DeclRefExpr cursors.
2414 continue;
2415 }
2416
2417 case VisitorJob::LambdaExprPartsKind: {
2418 // Visit captures.
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002419 const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002420 for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
2421 CEnd = E->explicit_capture_end();
2422 C != CEnd; ++C) {
Richard Smith0d8e9642013-05-16 06:20:58 +00002423 // FIXME: Lambda init-captures.
2424 if (!C->capturesVariable())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002425 continue;
Richard Smith0d8e9642013-05-16 06:20:58 +00002426
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002427 if (Visit(MakeCursorVariableRef(C->getCapturedVar(),
2428 C->getLocation(),
2429 TU)))
2430 return true;
2431 }
2432
2433 // Visit parameters and return type, if present.
2434 if (E->hasExplicitParameters() || E->hasExplicitResultType()) {
2435 TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2436 if (E->hasExplicitParameters() && E->hasExplicitResultType()) {
2437 // Visit the whole type.
2438 if (Visit(TL))
2439 return true;
David Blaikie39e6ab42013-02-18 22:06:02 +00002440 } else if (FunctionProtoTypeLoc Proto =
2441 TL.getAs<FunctionProtoTypeLoc>()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002442 if (E->hasExplicitParameters()) {
2443 // Visit parameters.
2444 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I)
2445 if (Visit(MakeCXCursor(Proto.getArg(I), TU)))
2446 return true;
2447 } else {
2448 // Visit result type.
2449 if (Visit(Proto.getResultLoc()))
2450 return true;
2451 }
2452 }
2453 }
2454 break;
2455 }
2456
2457 case VisitorJob::PostChildrenVisitKind:
2458 if (PostChildrenVisitor(Parent, ClientData))
2459 return true;
2460 break;
2461 }
2462 }
2463 return false;
2464}
2465
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00002466bool CursorVisitor::Visit(const Stmt *S) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002467 VisitorWorkList *WL = 0;
2468 if (!WorkListFreeList.empty()) {
2469 WL = WorkListFreeList.back();
2470 WL->clear();
2471 WorkListFreeList.pop_back();
2472 }
2473 else {
2474 WL = new VisitorWorkList();
2475 WorkListCache.push_back(WL);
2476 }
2477 EnqueueWorkList(*WL, S);
2478 bool result = RunVisitorWorkList(*WL);
2479 WorkListFreeList.push_back(WL);
2480 return result;
2481}
2482
2483namespace {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002484typedef SmallVector<SourceRange, 4> RefNamePieces;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002485RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
2486 const DeclarationNameInfo &NI,
2487 const SourceRange &QLoc,
2488 const ASTTemplateArgumentListInfo *TemplateArgs = 0){
2489 const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
2490 const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
2491 const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
2492
2493 const DeclarationName::NameKind Kind = NI.getName().getNameKind();
2494
2495 RefNamePieces Pieces;
2496
2497 if (WantQualifier && QLoc.isValid())
2498 Pieces.push_back(QLoc);
2499
2500 if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
2501 Pieces.push_back(NI.getLoc());
2502
2503 if (WantTemplateArgs && TemplateArgs)
2504 Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc,
2505 TemplateArgs->RAngleLoc));
2506
2507 if (Kind == DeclarationName::CXXOperatorName) {
2508 Pieces.push_back(SourceLocation::getFromRawEncoding(
2509 NI.getInfo().CXXOperatorName.BeginOpNameLoc));
2510 Pieces.push_back(SourceLocation::getFromRawEncoding(
2511 NI.getInfo().CXXOperatorName.EndOpNameLoc));
2512 }
2513
2514 if (WantSinglePiece) {
2515 SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
2516 Pieces.clear();
2517 Pieces.push_back(R);
2518 }
2519
2520 return Pieces;
2521}
2522}
2523
2524//===----------------------------------------------------------------------===//
2525// Misc. API hooks.
2526//===----------------------------------------------------------------------===//
2527
2528static llvm::sys::Mutex EnableMultithreadingMutex;
2529static bool EnabledMultithreading;
2530
Chad Rosier90836282013-03-27 18:28:23 +00002531static void fatal_error_handler(void *user_data, const std::string& reason,
2532 bool gen_crash_diag) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002533 // Write the result out to stderr avoiding errs() because raw_ostreams can
2534 // call report_fatal_error.
2535 fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str());
2536 ::abort();
2537}
2538
2539extern "C" {
2540CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
2541 int displayDiagnostics) {
2542 // Disable pretty stack trace functionality, which will otherwise be a very
2543 // poor citizen of the world and set up all sorts of signal handlers.
2544 llvm::DisablePrettyStackTrace = true;
2545
2546 // We use crash recovery to make some of our APIs more reliable, implicitly
2547 // enable it.
2548 llvm::CrashRecoveryContext::Enable();
2549
2550 // Enable support for multithreading in LLVM.
2551 {
2552 llvm::sys::ScopedLock L(EnableMultithreadingMutex);
2553 if (!EnabledMultithreading) {
2554 llvm::install_fatal_error_handler(fatal_error_handler, 0);
2555 llvm::llvm_start_multithreaded();
2556 EnabledMultithreading = true;
2557 }
2558 }
2559
2560 CIndexer *CIdxr = new CIndexer();
2561 if (excludeDeclarationsFromPCH)
2562 CIdxr->setOnlyLocalDecls();
2563 if (displayDiagnostics)
2564 CIdxr->setDisplayDiagnostics();
2565
2566 if (getenv("LIBCLANG_BGPRIO_INDEX"))
2567 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
2568 CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
2569 if (getenv("LIBCLANG_BGPRIO_EDIT"))
2570 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
2571 CXGlobalOpt_ThreadBackgroundPriorityForEditing);
2572
2573 return CIdxr;
2574}
2575
2576void clang_disposeIndex(CXIndex CIdx) {
2577 if (CIdx)
2578 delete static_cast<CIndexer *>(CIdx);
2579}
2580
2581void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
2582 if (CIdx)
2583 static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
2584}
2585
2586unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
2587 if (CIdx)
2588 return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
2589 return 0;
2590}
2591
2592void clang_toggleCrashRecovery(unsigned isEnabled) {
2593 if (isEnabled)
2594 llvm::CrashRecoveryContext::Enable();
2595 else
2596 llvm::CrashRecoveryContext::Disable();
2597}
2598
2599CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
2600 const char *ast_filename) {
Argyrios Kyrtzidis4c9f58f2013-05-24 22:24:07 +00002601 if (!CIdx || !ast_filename)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002602 return 0;
2603
Argyrios Kyrtzidis4c9f58f2013-05-24 22:24:07 +00002604 LOG_FUNC_SECTION {
2605 *Log << ast_filename;
2606 }
2607
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002608 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2609 FileSystemOptions FileSystemOpts;
2610
2611 IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
2612 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
2613 CXXIdx->getOnlyLocalDecls(),
2614 0, 0,
2615 /*CaptureDiagnostics=*/true,
2616 /*AllowPCHWithCompilerErrors=*/true,
2617 /*UserFilesAreVolatile=*/true);
2618 return MakeCXTranslationUnit(CXXIdx, TU);
2619}
2620
2621unsigned clang_defaultEditingTranslationUnitOptions() {
2622 return CXTranslationUnit_PrecompiledPreamble |
2623 CXTranslationUnit_CacheCompletionResults;
2624}
2625
2626CXTranslationUnit
2627clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
2628 const char *source_filename,
2629 int num_command_line_args,
2630 const char * const *command_line_args,
2631 unsigned num_unsaved_files,
2632 struct CXUnsavedFile *unsaved_files) {
2633 unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
2634 return clang_parseTranslationUnit(CIdx, source_filename,
2635 command_line_args, num_command_line_args,
2636 unsaved_files, num_unsaved_files,
2637 Options);
2638}
2639
2640struct ParseTranslationUnitInfo {
2641 CXIndex CIdx;
2642 const char *source_filename;
2643 const char *const *command_line_args;
2644 int num_command_line_args;
2645 struct CXUnsavedFile *unsaved_files;
2646 unsigned num_unsaved_files;
2647 unsigned options;
2648 CXTranslationUnit result;
2649};
2650static void clang_parseTranslationUnit_Impl(void *UserData) {
2651 ParseTranslationUnitInfo *PTUI =
2652 static_cast<ParseTranslationUnitInfo*>(UserData);
2653 CXIndex CIdx = PTUI->CIdx;
2654 const char *source_filename = PTUI->source_filename;
2655 const char * const *command_line_args = PTUI->command_line_args;
2656 int num_command_line_args = PTUI->num_command_line_args;
2657 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
2658 unsigned num_unsaved_files = PTUI->num_unsaved_files;
2659 unsigned options = PTUI->options;
2660 PTUI->result = 0;
2661
2662 if (!CIdx)
2663 return;
2664
2665 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2666
2667 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
2668 setThreadBackgroundPriority();
2669
2670 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
2671 // FIXME: Add a flag for modules.
2672 TranslationUnitKind TUKind
2673 = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
2674 bool CacheCodeCompetionResults
2675 = options & CXTranslationUnit_CacheCompletionResults;
2676 bool IncludeBriefCommentsInCodeCompletion
2677 = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
2678 bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies;
2679 bool ForSerialization = options & CXTranslationUnit_ForSerialization;
2680
2681 // Configure the diagnostics.
2682 IntrusiveRefCntPtr<DiagnosticsEngine>
Sean Silvad47afb92013-01-20 01:58:28 +00002683 Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002684
2685 // Recover resources if we crash before exiting this function.
2686 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
2687 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
2688 DiagCleanup(Diags.getPtr());
2689
2690 OwningPtr<std::vector<ASTUnit::RemappedFile> >
2691 RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
2692
2693 // Recover resources if we crash before exiting this function.
2694 llvm::CrashRecoveryContextCleanupRegistrar<
2695 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
2696
2697 for (unsigned I = 0; I != num_unsaved_files; ++I) {
2698 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2699 const llvm::MemoryBuffer *Buffer
2700 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
2701 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
2702 Buffer));
2703 }
2704
2705 OwningPtr<std::vector<const char *> >
2706 Args(new std::vector<const char*>());
2707
2708 // Recover resources if we crash before exiting this method.
2709 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
2710 ArgsCleanup(Args.get());
2711
2712 // Since the Clang C library is primarily used by batch tools dealing with
2713 // (often very broken) source code, where spell-checking can have a
2714 // significant negative impact on performance (particularly when
2715 // precompiled headers are involved), we disable it by default.
2716 // Only do this if we haven't found a spell-checking-related argument.
2717 bool FoundSpellCheckingArgument = false;
2718 for (int I = 0; I != num_command_line_args; ++I) {
2719 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
2720 strcmp(command_line_args[I], "-fspell-checking") == 0) {
2721 FoundSpellCheckingArgument = true;
2722 break;
2723 }
2724 }
2725 if (!FoundSpellCheckingArgument)
2726 Args->push_back("-fno-spell-checking");
2727
2728 Args->insert(Args->end(), command_line_args,
2729 command_line_args + num_command_line_args);
2730
2731 // The 'source_filename' argument is optional. If the caller does not
2732 // specify it then it is assumed that the source file is specified
2733 // in the actual argument list.
2734 // Put the source file after command_line_args otherwise if '-x' flag is
2735 // present it will be unused.
2736 if (source_filename)
2737 Args->push_back(source_filename);
2738
2739 // Do we need the detailed preprocessing record?
2740 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
2741 Args->push_back("-Xclang");
2742 Args->push_back("-detailed-preprocessing-record");
2743 }
2744
2745 unsigned NumErrors = Diags->getClient()->getNumErrors();
2746 OwningPtr<ASTUnit> ErrUnit;
2747 OwningPtr<ASTUnit> Unit(
2748 ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0
2749 /* vector::data() not portable */,
2750 Args->size() ? (&(*Args)[0] + Args->size()) :0,
2751 Diags,
2752 CXXIdx->getClangResourcesPath(),
2753 CXXIdx->getOnlyLocalDecls(),
2754 /*CaptureDiagnostics=*/true,
2755 RemappedFiles->size() ? &(*RemappedFiles)[0]:0,
2756 RemappedFiles->size(),
2757 /*RemappedFilesKeepOriginalName=*/true,
2758 PrecompilePreamble,
2759 TUKind,
2760 CacheCodeCompetionResults,
2761 IncludeBriefCommentsInCodeCompletion,
2762 /*AllowPCHWithCompilerErrors=*/true,
2763 SkipFunctionBodies,
2764 /*UserFilesAreVolatile=*/true,
2765 ForSerialization,
2766 &ErrUnit));
2767
2768 if (NumErrors != Diags->getClient()->getNumErrors()) {
2769 // Make sure to check that 'Unit' is non-NULL.
2770 if (CXXIdx->getDisplayDiagnostics())
2771 printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get());
2772 }
2773
2774 PTUI->result = MakeCXTranslationUnit(CXXIdx, Unit.take());
2775}
2776CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
2777 const char *source_filename,
2778 const char * const *command_line_args,
2779 int num_command_line_args,
2780 struct CXUnsavedFile *unsaved_files,
2781 unsigned num_unsaved_files,
2782 unsigned options) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00002783 LOG_FUNC_SECTION {
2784 *Log << source_filename << ": ";
2785 for (int i = 0; i != num_command_line_args; ++i)
2786 *Log << command_line_args[i] << " ";
2787 }
2788
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002789 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
2790 num_command_line_args, unsaved_files,
2791 num_unsaved_files, options, 0 };
2792 llvm::CrashRecoveryContext CRC;
2793
2794 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
2795 fprintf(stderr, "libclang: crash detected during parsing: {\n");
2796 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
2797 fprintf(stderr, " 'command_line_args' : [");
2798 for (int i = 0; i != num_command_line_args; ++i) {
2799 if (i)
2800 fprintf(stderr, ", ");
2801 fprintf(stderr, "'%s'", command_line_args[i]);
2802 }
2803 fprintf(stderr, "],\n");
2804 fprintf(stderr, " 'unsaved_files' : [");
2805 for (unsigned i = 0; i != num_unsaved_files; ++i) {
2806 if (i)
2807 fprintf(stderr, ", ");
2808 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
2809 unsaved_files[i].Length);
2810 }
2811 fprintf(stderr, "],\n");
2812 fprintf(stderr, " 'options' : %d,\n", options);
2813 fprintf(stderr, "}\n");
2814
2815 return 0;
2816 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
2817 PrintLibclangResourceUsage(PTUI.result);
2818 }
2819
2820 return PTUI.result;
2821}
2822
2823unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
2824 return CXSaveTranslationUnit_None;
2825}
2826
2827namespace {
2828
2829struct SaveTranslationUnitInfo {
2830 CXTranslationUnit TU;
2831 const char *FileName;
2832 unsigned options;
2833 CXSaveError result;
2834};
2835
2836}
2837
2838static void clang_saveTranslationUnit_Impl(void *UserData) {
2839 SaveTranslationUnitInfo *STUI =
2840 static_cast<SaveTranslationUnitInfo*>(UserData);
2841
Dmitri Gribenko8c718e72013-01-26 21:49:50 +00002842 CIndexer *CXXIdx = STUI->TU->CIdx;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002843 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
2844 setThreadBackgroundPriority();
2845
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002846 bool hadError = cxtu::getASTUnit(STUI->TU)->Save(STUI->FileName);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002847 STUI->result = hadError ? CXSaveError_Unknown : CXSaveError_None;
2848}
2849
2850int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
2851 unsigned options) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00002852 LOG_FUNC_SECTION {
2853 *Log << TU << ' ' << FileName;
2854 }
2855
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002856 if (!TU)
2857 return CXSaveError_InvalidTU;
2858
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002859 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002860 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2861 if (!CXXUnit->hasSema())
2862 return CXSaveError_InvalidTU;
2863
2864 SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None };
2865
2866 if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() ||
2867 getenv("LIBCLANG_NOTHREADS")) {
2868 clang_saveTranslationUnit_Impl(&STUI);
2869
2870 if (getenv("LIBCLANG_RESOURCE_USAGE"))
2871 PrintLibclangResourceUsage(TU);
2872
2873 return STUI.result;
2874 }
2875
2876 // We have an AST that has invalid nodes due to compiler errors.
2877 // Use a crash recovery thread for protection.
2878
2879 llvm::CrashRecoveryContext CRC;
2880
2881 if (!RunSafely(CRC, clang_saveTranslationUnit_Impl, &STUI)) {
2882 fprintf(stderr, "libclang: crash detected during AST saving: {\n");
2883 fprintf(stderr, " 'filename' : '%s'\n", FileName);
2884 fprintf(stderr, " 'options' : %d,\n", options);
2885 fprintf(stderr, "}\n");
2886
2887 return CXSaveError_Unknown;
2888
2889 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
2890 PrintLibclangResourceUsage(TU);
2891 }
2892
2893 return STUI.result;
2894}
2895
2896void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
2897 if (CTUnit) {
2898 // If the translation unit has been marked as unsafe to free, just discard
2899 // it.
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002900 if (cxtu::getASTUnit(CTUnit)->isUnsafeToFree())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002901 return;
2902
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002903 delete cxtu::getASTUnit(CTUnit);
Dmitri Gribenko9c48d162013-01-26 22:44:19 +00002904 delete CTUnit->StringPool;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002905 delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
2906 disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
Dmitri Gribenko337ee242013-01-26 21:39:50 +00002907 delete CTUnit->FormatContext;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002908 delete CTUnit;
2909 }
2910}
2911
2912unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
2913 return CXReparse_None;
2914}
2915
2916struct ReparseTranslationUnitInfo {
2917 CXTranslationUnit TU;
2918 unsigned num_unsaved_files;
2919 struct CXUnsavedFile *unsaved_files;
2920 unsigned options;
2921 int result;
2922};
2923
2924static void clang_reparseTranslationUnit_Impl(void *UserData) {
2925 ReparseTranslationUnitInfo *RTUI =
2926 static_cast<ReparseTranslationUnitInfo*>(UserData);
2927 CXTranslationUnit TU = RTUI->TU;
Argyrios Kyrtzidisd7bf4a42013-01-16 18:13:00 +00002928 if (!TU)
2929 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002930
2931 // Reset the associated diagnostics.
2932 delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
2933 TU->Diagnostics = 0;
2934
2935 unsigned num_unsaved_files = RTUI->num_unsaved_files;
2936 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
2937 unsigned options = RTUI->options;
2938 (void) options;
2939 RTUI->result = 1;
2940
Dmitri Gribenko8c718e72013-01-26 21:49:50 +00002941 CIndexer *CXXIdx = TU->CIdx;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002942 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
2943 setThreadBackgroundPriority();
2944
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002945 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002946 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2947
2948 OwningPtr<std::vector<ASTUnit::RemappedFile> >
2949 RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
2950
2951 // Recover resources if we crash before exiting this function.
2952 llvm::CrashRecoveryContextCleanupRegistrar<
2953 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
2954
2955 for (unsigned I = 0; I != num_unsaved_files; ++I) {
2956 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2957 const llvm::MemoryBuffer *Buffer
2958 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
2959 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
2960 Buffer));
2961 }
2962
2963 if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0,
2964 RemappedFiles->size()))
2965 RTUI->result = 0;
2966}
2967
2968int clang_reparseTranslationUnit(CXTranslationUnit TU,
2969 unsigned num_unsaved_files,
2970 struct CXUnsavedFile *unsaved_files,
2971 unsigned options) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00002972 LOG_FUNC_SECTION {
2973 *Log << TU;
2974 }
2975
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002976 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
2977 options, 0 };
2978
2979 if (getenv("LIBCLANG_NOTHREADS")) {
2980 clang_reparseTranslationUnit_Impl(&RTUI);
2981 return RTUI.result;
2982 }
2983
2984 llvm::CrashRecoveryContext CRC;
2985
2986 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
2987 fprintf(stderr, "libclang: crash detected during reparsing\n");
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00002988 cxtu::getASTUnit(TU)->setUnsafeToFree(true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002989 return 1;
2990 } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
2991 PrintLibclangResourceUsage(TU);
2992
2993 return RTUI.result;
2994}
2995
2996
2997CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
2998 if (!CTUnit)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00002999 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003000
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003001 ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003002 return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003003}
3004
3005CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Argyrios Kyrtzidis0b602832013-04-04 22:40:59 +00003006 if (!TU)
3007 return clang_getNullCursor();
3008
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003009 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003010 return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
3011}
3012
3013} // end: extern "C"
3014
3015//===----------------------------------------------------------------------===//
3016// CXFile Operations.
3017//===----------------------------------------------------------------------===//
3018
3019extern "C" {
3020CXString clang_getFileName(CXFile SFile) {
3021 if (!SFile)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +00003022 return cxstring::createNull();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003023
3024 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003025 return cxstring::createRef(FEnt->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003026}
3027
3028time_t clang_getFileTime(CXFile SFile) {
3029 if (!SFile)
3030 return 0;
3031
3032 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
3033 return FEnt->getModificationTime();
3034}
3035
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003036CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
3037 if (!TU)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003038 return 0;
3039
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003040 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003041
3042 FileManager &FMgr = CXXUnit->getFileManager();
3043 return const_cast<FileEntry *>(FMgr.getFile(file_name));
3044}
3045
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003046unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
3047 if (!TU || !file)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003048 return 0;
3049
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003050 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003051 FileEntry *FEnt = static_cast<FileEntry *>(file);
3052 return CXXUnit->getPreprocessor().getHeaderSearchInfo()
3053 .isFileMultipleIncludeGuarded(FEnt);
3054}
3055
Argyrios Kyrtzidisdb84e7a2013-01-26 04:52:52 +00003056int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
3057 if (!file || !outID)
3058 return 1;
3059
Argyrios Kyrtzidisdb84e7a2013-01-26 04:52:52 +00003060 FileEntry *FEnt = static_cast<FileEntry *>(file);
Rafael Espindola0fda0f72013-08-01 21:42:11 +00003061 const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
3062 outID->data[0] = ID.getDevice();
3063 outID->data[1] = ID.getFile();
Argyrios Kyrtzidisdb84e7a2013-01-26 04:52:52 +00003064 outID->data[2] = FEnt->getModificationTime();
3065 return 0;
Argyrios Kyrtzidisdb84e7a2013-01-26 04:52:52 +00003066}
3067
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003068} // end: extern "C"
3069
3070//===----------------------------------------------------------------------===//
3071// CXCursor Operations.
3072//===----------------------------------------------------------------------===//
3073
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003074static const Decl *getDeclFromExpr(const Stmt *E) {
3075 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003076 return getDeclFromExpr(CE->getSubExpr());
3077
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003078 if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003079 return RefExpr->getDecl();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003080 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003081 return ME->getMemberDecl();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003082 if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003083 return RE->getDecl();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003084 if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003085 if (PRE->isExplicitProperty())
3086 return PRE->getExplicitProperty();
3087 // It could be messaging both getter and setter as in:
3088 // ++myobj.myprop;
3089 // in which case prefer to associate the setter since it is less obvious
3090 // from inspecting the source that the setter is going to get called.
3091 if (PRE->isMessagingSetter())
3092 return PRE->getImplicitPropertySetter();
3093 return PRE->getImplicitPropertyGetter();
3094 }
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003095 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003096 return getDeclFromExpr(POE->getSyntacticForm());
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003097 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003098 if (Expr *Src = OVE->getSourceExpr())
3099 return getDeclFromExpr(Src);
3100
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003101 if (const CallExpr *CE = dyn_cast<CallExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003102 return getDeclFromExpr(CE->getCallee());
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003103 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003104 if (!CE->isElidable())
3105 return CE->getConstructor();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003106 if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003107 return OME->getMethodDecl();
3108
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003109 if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003110 return PE->getProtocol();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003111 if (const SubstNonTypeTemplateParmPackExpr *NTTP
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003112 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
3113 return NTTP->getParameterPack();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003114 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003115 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
3116 isa<ParmVarDecl>(SizeOfPack->getPack()))
3117 return SizeOfPack->getPack();
3118
3119 return 0;
3120}
3121
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003122static SourceLocation getLocationFromExpr(const Expr *E) {
3123 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003124 return getLocationFromExpr(CE->getSubExpr());
3125
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003126 if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003127 return /*FIXME:*/Msg->getLeftLoc();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003128 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003129 return DRE->getLocation();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003130 if (const MemberExpr *Member = dyn_cast<MemberExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003131 return Member->getMemberLoc();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003132 if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003133 return Ivar->getLocation();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003134 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003135 return SizeOfPack->getPackLoc();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003136 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003137 return PropRef->getLocation();
3138
3139 return E->getLocStart();
3140}
3141
3142extern "C" {
3143
3144unsigned clang_visitChildren(CXCursor parent,
3145 CXCursorVisitor visitor,
3146 CXClientData client_data) {
3147 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
3148 /*VisitPreprocessorLast=*/false);
3149 return CursorVis.VisitChildren(parent);
3150}
3151
3152#ifndef __has_feature
3153#define __has_feature(x) 0
3154#endif
3155#if __has_feature(blocks)
3156typedef enum CXChildVisitResult
3157 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
3158
3159static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
3160 CXClientData client_data) {
3161 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
3162 return block(cursor, parent);
3163}
3164#else
3165// If we are compiled with a compiler that doesn't have native blocks support,
3166// define and call the block manually, so the
3167typedef struct _CXChildVisitResult
3168{
3169 void *isa;
3170 int flags;
3171 int reserved;
3172 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
3173 CXCursor);
3174} *CXCursorVisitorBlock;
3175
3176static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
3177 CXClientData client_data) {
3178 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
3179 return block->invoke(block, cursor, parent);
3180}
3181#endif
3182
3183
3184unsigned clang_visitChildrenWithBlock(CXCursor parent,
3185 CXCursorVisitorBlock block) {
3186 return clang_visitChildren(parent, visitWithBlock, block);
3187}
3188
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003189static CXString getDeclSpelling(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003190 if (!D)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003191 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003192
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003193 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003194 if (!ND) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003195 if (const ObjCPropertyImplDecl *PropImpl =
3196 dyn_cast<ObjCPropertyImplDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003197 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003198 return cxstring::createDup(Property->getIdentifier()->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003199
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003200 if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003201 if (Module *Mod = ImportD->getImportedModule())
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003202 return cxstring::createDup(Mod->getFullModuleName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003203
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003204 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003205 }
3206
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003207 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003208 return cxstring::createDup(OMD->getSelector().getAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003209
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003210 if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003211 // No, this isn't the same as the code below. getIdentifier() is non-virtual
3212 // and returns different names. NamedDecl returns the class name and
3213 // ObjCCategoryImplDecl returns the category name.
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003214 return cxstring::createRef(CIMP->getIdentifier()->getNameStart());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003215
3216 if (isa<UsingDirectiveDecl>(D))
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003217 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003218
3219 SmallString<1024> S;
3220 llvm::raw_svector_ostream os(S);
3221 ND->printName(os);
3222
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003223 return cxstring::createDup(os.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003224}
3225
3226CXString clang_getCursorSpelling(CXCursor C) {
3227 if (clang_isTranslationUnit(C.kind))
Dmitri Gribenko46f92522013-01-11 19:28:44 +00003228 return clang_getTranslationUnitSpelling(getCursorTU(C));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003229
3230 if (clang_isReference(C.kind)) {
3231 switch (C.kind) {
3232 case CXCursor_ObjCSuperClassRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003233 const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003234 return cxstring::createRef(Super->getIdentifier()->getNameStart());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003235 }
3236 case CXCursor_ObjCClassRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003237 const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003238 return cxstring::createRef(Class->getIdentifier()->getNameStart());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003239 }
3240 case CXCursor_ObjCProtocolRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003241 const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003242 assert(OID && "getCursorSpelling(): Missing protocol decl");
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003243 return cxstring::createRef(OID->getIdentifier()->getNameStart());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003244 }
3245 case CXCursor_CXXBaseSpecifier: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003246 const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003247 return cxstring::createDup(B->getType().getAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003248 }
3249 case CXCursor_TypeRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003250 const TypeDecl *Type = getCursorTypeRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003251 assert(Type && "Missing type decl");
3252
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003253 return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type).
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003254 getAsString());
3255 }
3256 case CXCursor_TemplateRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003257 const TemplateDecl *Template = getCursorTemplateRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003258 assert(Template && "Missing template decl");
3259
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003260 return cxstring::createDup(Template->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003261 }
3262
3263 case CXCursor_NamespaceRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003264 const NamedDecl *NS = getCursorNamespaceRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003265 assert(NS && "Missing namespace decl");
3266
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003267 return cxstring::createDup(NS->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003268 }
3269
3270 case CXCursor_MemberRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003271 const FieldDecl *Field = getCursorMemberRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003272 assert(Field && "Missing member decl");
3273
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003274 return cxstring::createDup(Field->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003275 }
3276
3277 case CXCursor_LabelRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003278 const LabelStmt *Label = getCursorLabelRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003279 assert(Label && "Missing label");
3280
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003281 return cxstring::createRef(Label->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003282 }
3283
3284 case CXCursor_OverloadedDeclRef: {
3285 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003286 if (const Decl *D = Storage.dyn_cast<const Decl *>()) {
3287 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003288 return cxstring::createDup(ND->getNameAsString());
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003289 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003290 }
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003291 if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003292 return cxstring::createDup(E->getName().getAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003293 OverloadedTemplateStorage *Ovl
3294 = Storage.get<OverloadedTemplateStorage*>();
3295 if (Ovl->size() == 0)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003296 return cxstring::createEmpty();
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003297 return cxstring::createDup((*Ovl->begin())->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003298 }
3299
3300 case CXCursor_VariableRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00003301 const VarDecl *Var = getCursorVariableRef(C).first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003302 assert(Var && "Missing variable decl");
3303
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003304 return cxstring::createDup(Var->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003305 }
3306
3307 default:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003308 return cxstring::createRef("<not implemented>");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003309 }
3310 }
3311
3312 if (clang_isExpression(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003313 const Decl *D = getDeclFromExpr(getCursorExpr(C));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003314 if (D)
3315 return getDeclSpelling(D);
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003316 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003317 }
3318
3319 if (clang_isStatement(C.kind)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003320 const Stmt *S = getCursorStmt(C);
3321 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003322 return cxstring::createRef(Label->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003323
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003324 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003325 }
3326
3327 if (C.kind == CXCursor_MacroExpansion)
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003328 return cxstring::createRef(getCursorMacroExpansion(C).getName()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003329 ->getNameStart());
3330
3331 if (C.kind == CXCursor_MacroDefinition)
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003332 return cxstring::createRef(getCursorMacroDefinition(C)->getName()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003333 ->getNameStart());
3334
3335 if (C.kind == CXCursor_InclusionDirective)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003336 return cxstring::createDup(getCursorInclusionDirective(C)->getFileName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003337
3338 if (clang_isDeclaration(C.kind))
3339 return getDeclSpelling(getCursorDecl(C));
3340
3341 if (C.kind == CXCursor_AnnotateAttr) {
Dmitri Gribenko7d914382013-01-26 18:08:08 +00003342 const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003343 return cxstring::createDup(AA->getAnnotation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003344 }
3345
3346 if (C.kind == CXCursor_AsmLabelAttr) {
Dmitri Gribenko7d914382013-01-26 18:08:08 +00003347 const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003348 return cxstring::createDup(AA->getLabel());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003349 }
3350
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003351 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003352}
3353
3354CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C,
3355 unsigned pieceIndex,
3356 unsigned options) {
3357 if (clang_Cursor_isNull(C))
3358 return clang_getNullRange();
3359
3360 ASTContext &Ctx = getCursorContext(C);
3361
3362 if (clang_isStatement(C.kind)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003363 const Stmt *S = getCursorStmt(C);
3364 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003365 if (pieceIndex > 0)
3366 return clang_getNullRange();
3367 return cxloc::translateSourceRange(Ctx, Label->getIdentLoc());
3368 }
3369
3370 return clang_getNullRange();
3371 }
3372
3373 if (C.kind == CXCursor_ObjCMessageExpr) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00003374 if (const ObjCMessageExpr *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003375 ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) {
3376 if (pieceIndex >= ME->getNumSelectorLocs())
3377 return clang_getNullRange();
3378 return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex));
3379 }
3380 }
3381
3382 if (C.kind == CXCursor_ObjCInstanceMethodDecl ||
3383 C.kind == CXCursor_ObjCClassMethodDecl) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003384 if (const ObjCMethodDecl *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003385 MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) {
3386 if (pieceIndex >= MD->getNumSelectorLocs())
3387 return clang_getNullRange();
3388 return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex));
3389 }
3390 }
3391
3392 if (C.kind == CXCursor_ObjCCategoryDecl ||
3393 C.kind == CXCursor_ObjCCategoryImplDecl) {
3394 if (pieceIndex > 0)
3395 return clang_getNullRange();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003396 if (const ObjCCategoryDecl *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003397 CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C)))
3398 return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc());
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003399 if (const ObjCCategoryImplDecl *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003400 CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C)))
3401 return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc());
3402 }
3403
3404 if (C.kind == CXCursor_ModuleImportDecl) {
3405 if (pieceIndex > 0)
3406 return clang_getNullRange();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003407 if (const ImportDecl *ImportD =
3408 dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003409 ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs();
3410 if (!Locs.empty())
3411 return cxloc::translateSourceRange(Ctx,
3412 SourceRange(Locs.front(), Locs.back()));
3413 }
3414 return clang_getNullRange();
3415 }
3416
3417 // FIXME: A CXCursor_InclusionDirective should give the location of the
3418 // filename, but we don't keep track of this.
3419
3420 // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation
3421 // but we don't keep track of this.
3422
3423 // FIXME: A CXCursor_AsmLabelAttr should give the location of the label
3424 // but we don't keep track of this.
3425
3426 // Default handling, give the location of the cursor.
3427
3428 if (pieceIndex > 0)
3429 return clang_getNullRange();
3430
3431 CXSourceLocation CXLoc = clang_getCursorLocation(C);
3432 SourceLocation Loc = cxloc::translateSourceLocation(CXLoc);
3433 return cxloc::translateSourceRange(Ctx, Loc);
3434}
3435
3436CXString clang_getCursorDisplayName(CXCursor C) {
3437 if (!clang_isDeclaration(C.kind))
3438 return clang_getCursorSpelling(C);
3439
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003440 const Decl *D = getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003441 if (!D)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00003442 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003443
3444 PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003445 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003446 D = FunTmpl->getTemplatedDecl();
3447
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003448 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003449 SmallString<64> Str;
3450 llvm::raw_svector_ostream OS(Str);
3451 OS << *Function;
3452 if (Function->getPrimaryTemplate())
3453 OS << "<>";
3454 OS << "(";
3455 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
3456 if (I)
3457 OS << ", ";
3458 OS << Function->getParamDecl(I)->getType().getAsString(Policy);
3459 }
3460
3461 if (Function->isVariadic()) {
3462 if (Function->getNumParams())
3463 OS << ", ";
3464 OS << "...";
3465 }
3466 OS << ")";
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003467 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003468 }
3469
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003470 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003471 SmallString<64> Str;
3472 llvm::raw_svector_ostream OS(Str);
3473 OS << *ClassTemplate;
3474 OS << "<";
3475 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
3476 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
3477 if (I)
3478 OS << ", ";
3479
3480 NamedDecl *Param = Params->getParam(I);
3481 if (Param->getIdentifier()) {
3482 OS << Param->getIdentifier()->getName();
3483 continue;
3484 }
3485
3486 // There is no parameter name, which makes this tricky. Try to come up
3487 // with something useful that isn't too long.
3488 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3489 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
3490 else if (NonTypeTemplateParmDecl *NTTP
3491 = dyn_cast<NonTypeTemplateParmDecl>(Param))
3492 OS << NTTP->getType().getAsString(Policy);
3493 else
3494 OS << "template<...> class";
3495 }
3496
3497 OS << ">";
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003498 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003499 }
3500
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003501 if (const ClassTemplateSpecializationDecl *ClassSpec
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003502 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3503 // If the type was explicitly written, use that.
3504 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003505 return cxstring::createDup(TSInfo->getType().getAsString(Policy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003506
Benjamin Kramer5eada842013-02-22 15:46:01 +00003507 SmallString<128> Str;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003508 llvm::raw_svector_ostream OS(Str);
3509 OS << *ClassSpec;
Benjamin Kramer5eada842013-02-22 15:46:01 +00003510 TemplateSpecializationType::PrintTemplateArgumentList(OS,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003511 ClassSpec->getTemplateArgs().data(),
3512 ClassSpec->getTemplateArgs().size(),
3513 Policy);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00003514 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003515 }
3516
3517 return clang_getCursorSpelling(C);
3518}
3519
3520CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
3521 switch (Kind) {
3522 case CXCursor_FunctionDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003523 return cxstring::createRef("FunctionDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003524 case CXCursor_TypedefDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003525 return cxstring::createRef("TypedefDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003526 case CXCursor_EnumDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003527 return cxstring::createRef("EnumDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003528 case CXCursor_EnumConstantDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003529 return cxstring::createRef("EnumConstantDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003530 case CXCursor_StructDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003531 return cxstring::createRef("StructDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003532 case CXCursor_UnionDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003533 return cxstring::createRef("UnionDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003534 case CXCursor_ClassDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003535 return cxstring::createRef("ClassDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003536 case CXCursor_FieldDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003537 return cxstring::createRef("FieldDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003538 case CXCursor_VarDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003539 return cxstring::createRef("VarDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003540 case CXCursor_ParmDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003541 return cxstring::createRef("ParmDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003542 case CXCursor_ObjCInterfaceDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003543 return cxstring::createRef("ObjCInterfaceDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003544 case CXCursor_ObjCCategoryDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003545 return cxstring::createRef("ObjCCategoryDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003546 case CXCursor_ObjCProtocolDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003547 return cxstring::createRef("ObjCProtocolDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003548 case CXCursor_ObjCPropertyDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003549 return cxstring::createRef("ObjCPropertyDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003550 case CXCursor_ObjCIvarDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003551 return cxstring::createRef("ObjCIvarDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003552 case CXCursor_ObjCInstanceMethodDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003553 return cxstring::createRef("ObjCInstanceMethodDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003554 case CXCursor_ObjCClassMethodDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003555 return cxstring::createRef("ObjCClassMethodDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003556 case CXCursor_ObjCImplementationDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003557 return cxstring::createRef("ObjCImplementationDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003558 case CXCursor_ObjCCategoryImplDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003559 return cxstring::createRef("ObjCCategoryImplDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003560 case CXCursor_CXXMethod:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003561 return cxstring::createRef("CXXMethod");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003562 case CXCursor_UnexposedDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003563 return cxstring::createRef("UnexposedDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003564 case CXCursor_ObjCSuperClassRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003565 return cxstring::createRef("ObjCSuperClassRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003566 case CXCursor_ObjCProtocolRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003567 return cxstring::createRef("ObjCProtocolRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003568 case CXCursor_ObjCClassRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003569 return cxstring::createRef("ObjCClassRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003570 case CXCursor_TypeRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003571 return cxstring::createRef("TypeRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003572 case CXCursor_TemplateRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003573 return cxstring::createRef("TemplateRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003574 case CXCursor_NamespaceRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003575 return cxstring::createRef("NamespaceRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003576 case CXCursor_MemberRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003577 return cxstring::createRef("MemberRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003578 case CXCursor_LabelRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003579 return cxstring::createRef("LabelRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003580 case CXCursor_OverloadedDeclRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003581 return cxstring::createRef("OverloadedDeclRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003582 case CXCursor_VariableRef:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003583 return cxstring::createRef("VariableRef");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003584 case CXCursor_IntegerLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003585 return cxstring::createRef("IntegerLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003586 case CXCursor_FloatingLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003587 return cxstring::createRef("FloatingLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003588 case CXCursor_ImaginaryLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003589 return cxstring::createRef("ImaginaryLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003590 case CXCursor_StringLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003591 return cxstring::createRef("StringLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003592 case CXCursor_CharacterLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003593 return cxstring::createRef("CharacterLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003594 case CXCursor_ParenExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003595 return cxstring::createRef("ParenExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003596 case CXCursor_UnaryOperator:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003597 return cxstring::createRef("UnaryOperator");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003598 case CXCursor_ArraySubscriptExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003599 return cxstring::createRef("ArraySubscriptExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003600 case CXCursor_BinaryOperator:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003601 return cxstring::createRef("BinaryOperator");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003602 case CXCursor_CompoundAssignOperator:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003603 return cxstring::createRef("CompoundAssignOperator");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003604 case CXCursor_ConditionalOperator:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003605 return cxstring::createRef("ConditionalOperator");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003606 case CXCursor_CStyleCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003607 return cxstring::createRef("CStyleCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003608 case CXCursor_CompoundLiteralExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003609 return cxstring::createRef("CompoundLiteralExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003610 case CXCursor_InitListExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003611 return cxstring::createRef("InitListExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003612 case CXCursor_AddrLabelExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003613 return cxstring::createRef("AddrLabelExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003614 case CXCursor_StmtExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003615 return cxstring::createRef("StmtExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003616 case CXCursor_GenericSelectionExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003617 return cxstring::createRef("GenericSelectionExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003618 case CXCursor_GNUNullExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003619 return cxstring::createRef("GNUNullExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003620 case CXCursor_CXXStaticCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003621 return cxstring::createRef("CXXStaticCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003622 case CXCursor_CXXDynamicCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003623 return cxstring::createRef("CXXDynamicCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003624 case CXCursor_CXXReinterpretCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003625 return cxstring::createRef("CXXReinterpretCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003626 case CXCursor_CXXConstCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003627 return cxstring::createRef("CXXConstCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003628 case CXCursor_CXXFunctionalCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003629 return cxstring::createRef("CXXFunctionalCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003630 case CXCursor_CXXTypeidExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003631 return cxstring::createRef("CXXTypeidExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003632 case CXCursor_CXXBoolLiteralExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003633 return cxstring::createRef("CXXBoolLiteralExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003634 case CXCursor_CXXNullPtrLiteralExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003635 return cxstring::createRef("CXXNullPtrLiteralExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003636 case CXCursor_CXXThisExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003637 return cxstring::createRef("CXXThisExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003638 case CXCursor_CXXThrowExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003639 return cxstring::createRef("CXXThrowExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003640 case CXCursor_CXXNewExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003641 return cxstring::createRef("CXXNewExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003642 case CXCursor_CXXDeleteExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003643 return cxstring::createRef("CXXDeleteExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003644 case CXCursor_UnaryExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003645 return cxstring::createRef("UnaryExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003646 case CXCursor_ObjCStringLiteral:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003647 return cxstring::createRef("ObjCStringLiteral");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003648 case CXCursor_ObjCBoolLiteralExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003649 return cxstring::createRef("ObjCBoolLiteralExpr");
Argyrios Kyrtzidisedab0472013-04-23 17:57:17 +00003650 case CXCursor_ObjCSelfExpr:
3651 return cxstring::createRef("ObjCSelfExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003652 case CXCursor_ObjCEncodeExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003653 return cxstring::createRef("ObjCEncodeExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003654 case CXCursor_ObjCSelectorExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003655 return cxstring::createRef("ObjCSelectorExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003656 case CXCursor_ObjCProtocolExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003657 return cxstring::createRef("ObjCProtocolExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003658 case CXCursor_ObjCBridgedCastExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003659 return cxstring::createRef("ObjCBridgedCastExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003660 case CXCursor_BlockExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003661 return cxstring::createRef("BlockExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003662 case CXCursor_PackExpansionExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003663 return cxstring::createRef("PackExpansionExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003664 case CXCursor_SizeOfPackExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003665 return cxstring::createRef("SizeOfPackExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003666 case CXCursor_LambdaExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003667 return cxstring::createRef("LambdaExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003668 case CXCursor_UnexposedExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003669 return cxstring::createRef("UnexposedExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003670 case CXCursor_DeclRefExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003671 return cxstring::createRef("DeclRefExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003672 case CXCursor_MemberRefExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003673 return cxstring::createRef("MemberRefExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003674 case CXCursor_CallExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003675 return cxstring::createRef("CallExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003676 case CXCursor_ObjCMessageExpr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003677 return cxstring::createRef("ObjCMessageExpr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003678 case CXCursor_UnexposedStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003679 return cxstring::createRef("UnexposedStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003680 case CXCursor_DeclStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003681 return cxstring::createRef("DeclStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003682 case CXCursor_LabelStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003683 return cxstring::createRef("LabelStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003684 case CXCursor_CompoundStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003685 return cxstring::createRef("CompoundStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003686 case CXCursor_CaseStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003687 return cxstring::createRef("CaseStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003688 case CXCursor_DefaultStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003689 return cxstring::createRef("DefaultStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003690 case CXCursor_IfStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003691 return cxstring::createRef("IfStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003692 case CXCursor_SwitchStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003693 return cxstring::createRef("SwitchStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003694 case CXCursor_WhileStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003695 return cxstring::createRef("WhileStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003696 case CXCursor_DoStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003697 return cxstring::createRef("DoStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003698 case CXCursor_ForStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003699 return cxstring::createRef("ForStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003700 case CXCursor_GotoStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003701 return cxstring::createRef("GotoStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003702 case CXCursor_IndirectGotoStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003703 return cxstring::createRef("IndirectGotoStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003704 case CXCursor_ContinueStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003705 return cxstring::createRef("ContinueStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003706 case CXCursor_BreakStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003707 return cxstring::createRef("BreakStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003708 case CXCursor_ReturnStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003709 return cxstring::createRef("ReturnStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003710 case CXCursor_GCCAsmStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003711 return cxstring::createRef("GCCAsmStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003712 case CXCursor_MSAsmStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003713 return cxstring::createRef("MSAsmStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003714 case CXCursor_ObjCAtTryStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003715 return cxstring::createRef("ObjCAtTryStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003716 case CXCursor_ObjCAtCatchStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003717 return cxstring::createRef("ObjCAtCatchStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003718 case CXCursor_ObjCAtFinallyStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003719 return cxstring::createRef("ObjCAtFinallyStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003720 case CXCursor_ObjCAtThrowStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003721 return cxstring::createRef("ObjCAtThrowStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003722 case CXCursor_ObjCAtSynchronizedStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003723 return cxstring::createRef("ObjCAtSynchronizedStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003724 case CXCursor_ObjCAutoreleasePoolStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003725 return cxstring::createRef("ObjCAutoreleasePoolStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003726 case CXCursor_ObjCForCollectionStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003727 return cxstring::createRef("ObjCForCollectionStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003728 case CXCursor_CXXCatchStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003729 return cxstring::createRef("CXXCatchStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003730 case CXCursor_CXXTryStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003731 return cxstring::createRef("CXXTryStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003732 case CXCursor_CXXForRangeStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003733 return cxstring::createRef("CXXForRangeStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003734 case CXCursor_SEHTryStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003735 return cxstring::createRef("SEHTryStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003736 case CXCursor_SEHExceptStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003737 return cxstring::createRef("SEHExceptStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003738 case CXCursor_SEHFinallyStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003739 return cxstring::createRef("SEHFinallyStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003740 case CXCursor_NullStmt:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003741 return cxstring::createRef("NullStmt");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003742 case CXCursor_InvalidFile:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003743 return cxstring::createRef("InvalidFile");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003744 case CXCursor_InvalidCode:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003745 return cxstring::createRef("InvalidCode");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003746 case CXCursor_NoDeclFound:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003747 return cxstring::createRef("NoDeclFound");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003748 case CXCursor_NotImplemented:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003749 return cxstring::createRef("NotImplemented");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003750 case CXCursor_TranslationUnit:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003751 return cxstring::createRef("TranslationUnit");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003752 case CXCursor_UnexposedAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003753 return cxstring::createRef("UnexposedAttr");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003754 case CXCursor_IBActionAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003755 return cxstring::createRef("attribute(ibaction)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003756 case CXCursor_IBOutletAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003757 return cxstring::createRef("attribute(iboutlet)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003758 case CXCursor_IBOutletCollectionAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003759 return cxstring::createRef("attribute(iboutletcollection)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003760 case CXCursor_CXXFinalAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003761 return cxstring::createRef("attribute(final)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003762 case CXCursor_CXXOverrideAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003763 return cxstring::createRef("attribute(override)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003764 case CXCursor_AnnotateAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003765 return cxstring::createRef("attribute(annotate)");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003766 case CXCursor_AsmLabelAttr:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003767 return cxstring::createRef("asm label");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003768 case CXCursor_PreprocessingDirective:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003769 return cxstring::createRef("preprocessing directive");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003770 case CXCursor_MacroDefinition:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003771 return cxstring::createRef("macro definition");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003772 case CXCursor_MacroExpansion:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003773 return cxstring::createRef("macro expansion");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003774 case CXCursor_InclusionDirective:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003775 return cxstring::createRef("inclusion directive");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003776 case CXCursor_Namespace:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003777 return cxstring::createRef("Namespace");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003778 case CXCursor_LinkageSpec:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003779 return cxstring::createRef("LinkageSpec");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003780 case CXCursor_CXXBaseSpecifier:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003781 return cxstring::createRef("C++ base class specifier");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003782 case CXCursor_Constructor:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003783 return cxstring::createRef("CXXConstructor");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003784 case CXCursor_Destructor:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003785 return cxstring::createRef("CXXDestructor");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003786 case CXCursor_ConversionFunction:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003787 return cxstring::createRef("CXXConversion");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003788 case CXCursor_TemplateTypeParameter:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003789 return cxstring::createRef("TemplateTypeParameter");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003790 case CXCursor_NonTypeTemplateParameter:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003791 return cxstring::createRef("NonTypeTemplateParameter");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003792 case CXCursor_TemplateTemplateParameter:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003793 return cxstring::createRef("TemplateTemplateParameter");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003794 case CXCursor_FunctionTemplate:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003795 return cxstring::createRef("FunctionTemplate");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003796 case CXCursor_ClassTemplate:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003797 return cxstring::createRef("ClassTemplate");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003798 case CXCursor_ClassTemplatePartialSpecialization:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003799 return cxstring::createRef("ClassTemplatePartialSpecialization");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003800 case CXCursor_NamespaceAlias:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003801 return cxstring::createRef("NamespaceAlias");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003802 case CXCursor_UsingDirective:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003803 return cxstring::createRef("UsingDirective");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003804 case CXCursor_UsingDeclaration:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003805 return cxstring::createRef("UsingDeclaration");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003806 case CXCursor_TypeAliasDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003807 return cxstring::createRef("TypeAliasDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003808 case CXCursor_ObjCSynthesizeDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003809 return cxstring::createRef("ObjCSynthesizeDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003810 case CXCursor_ObjCDynamicDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003811 return cxstring::createRef("ObjCDynamicDecl");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003812 case CXCursor_CXXAccessSpecifier:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003813 return cxstring::createRef("CXXAccessSpecifier");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003814 case CXCursor_ModuleImportDecl:
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00003815 return cxstring::createRef("ModuleImport");
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00003816 case CXCursor_OMPParallelDirective:
3817 return cxstring::createRef("OMPParallelDirective");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003818 }
3819
3820 llvm_unreachable("Unhandled CXCursorKind");
3821}
3822
3823struct GetCursorData {
3824 SourceLocation TokenBeginLoc;
3825 bool PointsAtMacroArgExpansion;
3826 bool VisitedObjCPropertyImplDecl;
3827 SourceLocation VisitedDeclaratorDeclStartLoc;
3828 CXCursor &BestCursor;
3829
3830 GetCursorData(SourceManager &SM,
3831 SourceLocation tokenBegin, CXCursor &outputCursor)
3832 : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
3833 PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
3834 VisitedObjCPropertyImplDecl = false;
3835 }
3836};
3837
3838static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
3839 CXCursor parent,
3840 CXClientData client_data) {
3841 GetCursorData *Data = static_cast<GetCursorData *>(client_data);
3842 CXCursor *BestCursor = &Data->BestCursor;
3843
3844 // If we point inside a macro argument we should provide info of what the
3845 // token is so use the actual cursor, don't replace it with a macro expansion
3846 // cursor.
3847 if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
3848 return CXChildVisit_Recurse;
3849
3850 if (clang_isDeclaration(cursor.kind)) {
3851 // Avoid having the implicit methods override the property decls.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003852 if (const ObjCMethodDecl *MD
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003853 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
3854 if (MD->isImplicit())
3855 return CXChildVisit_Break;
3856
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003857 } else if (const ObjCInterfaceDecl *ID
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003858 = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) {
3859 // Check that when we have multiple @class references in the same line,
3860 // that later ones do not override the previous ones.
3861 // If we have:
3862 // @class Foo, Bar;
3863 // source ranges for both start at '@', so 'Bar' will end up overriding
3864 // 'Foo' even though the cursor location was at 'Foo'.
3865 if (BestCursor->kind == CXCursor_ObjCInterfaceDecl ||
3866 BestCursor->kind == CXCursor_ObjCClassRef)
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003867 if (const ObjCInterfaceDecl *PrevID
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003868 = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){
3869 if (PrevID != ID &&
3870 !PrevID->isThisDeclarationADefinition() &&
3871 !ID->isThisDeclarationADefinition())
3872 return CXChildVisit_Break;
3873 }
3874
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003875 } else if (const DeclaratorDecl *DD
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003876 = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) {
3877 SourceLocation StartLoc = DD->getSourceRange().getBegin();
3878 // Check that when we have multiple declarators in the same line,
3879 // that later ones do not override the previous ones.
3880 // If we have:
3881 // int Foo, Bar;
3882 // source ranges for both start at 'int', so 'Bar' will end up overriding
3883 // 'Foo' even though the cursor location was at 'Foo'.
3884 if (Data->VisitedDeclaratorDeclStartLoc == StartLoc)
3885 return CXChildVisit_Break;
3886 Data->VisitedDeclaratorDeclStartLoc = StartLoc;
3887
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003888 } else if (const ObjCPropertyImplDecl *PropImp
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003889 = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) {
3890 (void)PropImp;
3891 // Check that when we have multiple @synthesize in the same line,
3892 // that later ones do not override the previous ones.
3893 // If we have:
3894 // @synthesize Foo, Bar;
3895 // source ranges for both start at '@', so 'Bar' will end up overriding
3896 // 'Foo' even though the cursor location was at 'Foo'.
3897 if (Data->VisitedObjCPropertyImplDecl)
3898 return CXChildVisit_Break;
3899 Data->VisitedObjCPropertyImplDecl = true;
3900 }
3901 }
3902
3903 if (clang_isExpression(cursor.kind) &&
3904 clang_isDeclaration(BestCursor->kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00003905 if (const Decl *D = getCursorDecl(*BestCursor)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003906 // Avoid having the cursor of an expression replace the declaration cursor
3907 // when the expression source range overlaps the declaration range.
3908 // This can happen for C++ constructor expressions whose range generally
3909 // include the variable declaration, e.g.:
3910 // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor.
3911 if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
3912 D->getLocation() == Data->TokenBeginLoc)
3913 return CXChildVisit_Break;
3914 }
3915 }
3916
3917 // If our current best cursor is the construction of a temporary object,
3918 // don't replace that cursor with a type reference, because we want
3919 // clang_getCursor() to point at the constructor.
3920 if (clang_isExpression(BestCursor->kind) &&
3921 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
3922 cursor.kind == CXCursor_TypeRef) {
3923 // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
3924 // as having the actual point on the type reference.
3925 *BestCursor = getTypeRefedCallExprCursor(*BestCursor);
3926 return CXChildVisit_Recurse;
3927 }
3928
3929 *BestCursor = cursor;
3930 return CXChildVisit_Recurse;
3931}
3932
3933CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
3934 if (!TU)
3935 return clang_getNullCursor();
3936
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00003937 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003938 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3939
3940 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
3941 CXCursor Result = cxcursor::getCursor(TU, SLoc);
3942
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00003943 LOG_FUNC_SECTION {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003944 CXFile SearchFile;
3945 unsigned SearchLine, SearchColumn;
3946 CXFile ResultFile;
3947 unsigned ResultLine, ResultColumn;
3948 CXString SearchFileName, ResultFileName, KindSpelling, USR;
3949 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
3950 CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
3951
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00003952 clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0);
3953 clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003954 &ResultColumn, 0);
3955 SearchFileName = clang_getFileName(SearchFile);
3956 ResultFileName = clang_getFileName(ResultFile);
3957 KindSpelling = clang_getCursorKindSpelling(Result.kind);
3958 USR = clang_getCursorUSR(Result);
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00003959 *Log << llvm::format("(%s:%d:%d) = %s",
3960 clang_getCString(SearchFileName), SearchLine, SearchColumn,
3961 clang_getCString(KindSpelling))
3962 << llvm::format("(%s:%d:%d):%s%s",
3963 clang_getCString(ResultFileName), ResultLine, ResultColumn,
3964 clang_getCString(USR), IsDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003965 clang_disposeString(SearchFileName);
3966 clang_disposeString(ResultFileName);
3967 clang_disposeString(KindSpelling);
3968 clang_disposeString(USR);
3969
3970 CXCursor Definition = clang_getCursorDefinition(Result);
3971 if (!clang_equalCursors(Definition, clang_getNullCursor())) {
3972 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
3973 CXString DefinitionKindSpelling
3974 = clang_getCursorKindSpelling(Definition.kind);
3975 CXFile DefinitionFile;
3976 unsigned DefinitionLine, DefinitionColumn;
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00003977 clang_getFileLocation(DefinitionLoc, &DefinitionFile,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003978 &DefinitionLine, &DefinitionColumn, 0);
3979 CXString DefinitionFileName = clang_getFileName(DefinitionFile);
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00003980 *Log << llvm::format(" -> %s(%s:%d:%d)",
3981 clang_getCString(DefinitionKindSpelling),
3982 clang_getCString(DefinitionFileName),
3983 DefinitionLine, DefinitionColumn);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003984 clang_disposeString(DefinitionFileName);
3985 clang_disposeString(DefinitionKindSpelling);
3986 }
3987 }
3988
3989 return Result;
3990}
3991
3992CXCursor clang_getNullCursor(void) {
3993 return MakeCXCursorInvalid(CXCursor_InvalidFile);
3994}
3995
3996unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Argyrios Kyrtzidisd1d9df62013-01-08 18:23:28 +00003997 // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we
3998 // can't set consistently. For example, when visiting a DeclStmt we will set
3999 // it but we don't set it on the result of clang_getCursorDefinition for
4000 // a reference of the same declaration.
4001 // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works
4002 // when visiting a DeclStmt currently, the AST should be enhanced to be able
4003 // to provide that kind of info.
4004 if (clang_isDeclaration(X.kind))
4005 X.data[1] = 0;
4006 if (clang_isDeclaration(Y.kind))
4007 Y.data[1] = 0;
4008
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004009 return X == Y;
4010}
4011
4012unsigned clang_hashCursor(CXCursor C) {
4013 unsigned Index = 0;
4014 if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
4015 Index = 1;
4016
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004017 return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue(
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004018 std::make_pair(C.kind, C.data[Index]));
4019}
4020
4021unsigned clang_isInvalid(enum CXCursorKind K) {
4022 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
4023}
4024
4025unsigned clang_isDeclaration(enum CXCursorKind K) {
4026 return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) ||
4027 (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl);
4028}
4029
4030unsigned clang_isReference(enum CXCursorKind K) {
4031 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
4032}
4033
4034unsigned clang_isExpression(enum CXCursorKind K) {
4035 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
4036}
4037
4038unsigned clang_isStatement(enum CXCursorKind K) {
4039 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
4040}
4041
4042unsigned clang_isAttribute(enum CXCursorKind K) {
4043 return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
4044}
4045
4046unsigned clang_isTranslationUnit(enum CXCursorKind K) {
4047 return K == CXCursor_TranslationUnit;
4048}
4049
4050unsigned clang_isPreprocessing(enum CXCursorKind K) {
4051 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
4052}
4053
4054unsigned clang_isUnexposed(enum CXCursorKind K) {
4055 switch (K) {
4056 case CXCursor_UnexposedDecl:
4057 case CXCursor_UnexposedExpr:
4058 case CXCursor_UnexposedStmt:
4059 case CXCursor_UnexposedAttr:
4060 return true;
4061 default:
4062 return false;
4063 }
4064}
4065
4066CXCursorKind clang_getCursorKind(CXCursor C) {
4067 return C.kind;
4068}
4069
4070CXSourceLocation clang_getCursorLocation(CXCursor C) {
4071 if (clang_isReference(C.kind)) {
4072 switch (C.kind) {
4073 case CXCursor_ObjCSuperClassRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004074 std::pair<const ObjCInterfaceDecl *, SourceLocation> P
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004075 = getCursorObjCSuperClassRef(C);
4076 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4077 }
4078
4079 case CXCursor_ObjCProtocolRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004080 std::pair<const ObjCProtocolDecl *, SourceLocation> P
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004081 = getCursorObjCProtocolRef(C);
4082 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4083 }
4084
4085 case CXCursor_ObjCClassRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004086 std::pair<const ObjCInterfaceDecl *, SourceLocation> P
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004087 = getCursorObjCClassRef(C);
4088 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4089 }
4090
4091 case CXCursor_TypeRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004092 std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004093 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4094 }
4095
4096 case CXCursor_TemplateRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004097 std::pair<const TemplateDecl *, SourceLocation> P =
4098 getCursorTemplateRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004099 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4100 }
4101
4102 case CXCursor_NamespaceRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004103 std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004104 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4105 }
4106
4107 case CXCursor_MemberRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004108 std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004109 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4110 }
4111
4112 case CXCursor_VariableRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004113 std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004114 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4115 }
4116
4117 case CXCursor_CXXBaseSpecifier: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004118 const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004119 if (!BaseSpec)
4120 return clang_getNullLocation();
4121
4122 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
4123 return cxloc::translateSourceLocation(getCursorContext(C),
4124 TSInfo->getTypeLoc().getBeginLoc());
4125
4126 return cxloc::translateSourceLocation(getCursorContext(C),
4127 BaseSpec->getLocStart());
4128 }
4129
4130 case CXCursor_LabelRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004131 std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004132 return cxloc::translateSourceLocation(getCursorContext(C), P.second);
4133 }
4134
4135 case CXCursor_OverloadedDeclRef:
4136 return cxloc::translateSourceLocation(getCursorContext(C),
4137 getCursorOverloadedDeclRef(C).second);
4138
4139 default:
4140 // FIXME: Need a way to enumerate all non-reference cases.
4141 llvm_unreachable("Missed a reference kind");
4142 }
4143 }
4144
4145 if (clang_isExpression(C.kind))
4146 return cxloc::translateSourceLocation(getCursorContext(C),
4147 getLocationFromExpr(getCursorExpr(C)));
4148
4149 if (clang_isStatement(C.kind))
4150 return cxloc::translateSourceLocation(getCursorContext(C),
4151 getCursorStmt(C)->getLocStart());
4152
4153 if (C.kind == CXCursor_PreprocessingDirective) {
4154 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
4155 return cxloc::translateSourceLocation(getCursorContext(C), L);
4156 }
4157
4158 if (C.kind == CXCursor_MacroExpansion) {
4159 SourceLocation L
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00004160 = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004161 return cxloc::translateSourceLocation(getCursorContext(C), L);
4162 }
4163
4164 if (C.kind == CXCursor_MacroDefinition) {
4165 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
4166 return cxloc::translateSourceLocation(getCursorContext(C), L);
4167 }
4168
4169 if (C.kind == CXCursor_InclusionDirective) {
4170 SourceLocation L
4171 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
4172 return cxloc::translateSourceLocation(getCursorContext(C), L);
4173 }
4174
4175 if (!clang_isDeclaration(C.kind))
4176 return clang_getNullLocation();
4177
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004178 const Decl *D = getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004179 if (!D)
4180 return clang_getNullLocation();
4181
4182 SourceLocation Loc = D->getLocation();
4183 // FIXME: Multiple variables declared in a single declaration
4184 // currently lack the information needed to correctly determine their
4185 // ranges when accounting for the type-specifier. We use context
4186 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4187 // and if so, whether it is the first decl.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004188 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004189 if (!cxcursor::isFirstInDeclGroup(C))
4190 Loc = VD->getLocation();
4191 }
4192
4193 // For ObjC methods, give the start location of the method name.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004194 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004195 Loc = MD->getSelectorStartLoc();
4196
4197 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
4198}
4199
4200} // end extern "C"
4201
4202CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) {
4203 assert(TU);
4204
4205 // Guard against an invalid SourceLocation, or we may assert in one
4206 // of the following calls.
4207 if (SLoc.isInvalid())
4208 return clang_getNullCursor();
4209
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00004210 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004211
4212 // Translate the given source location to make it point at the beginning of
4213 // the token under the cursor.
4214 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
4215 CXXUnit->getASTContext().getLangOpts());
4216
4217 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
4218 if (SLoc.isValid()) {
4219 GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
4220 CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
4221 /*VisitPreprocessorLast=*/true,
4222 /*VisitIncludedEntities=*/false,
4223 SourceLocation(SLoc));
4224 CursorVis.visitFileRegion();
4225 }
4226
4227 return Result;
4228}
4229
4230static SourceRange getRawCursorExtent(CXCursor C) {
4231 if (clang_isReference(C.kind)) {
4232 switch (C.kind) {
4233 case CXCursor_ObjCSuperClassRef:
4234 return getCursorObjCSuperClassRef(C).second;
4235
4236 case CXCursor_ObjCProtocolRef:
4237 return getCursorObjCProtocolRef(C).second;
4238
4239 case CXCursor_ObjCClassRef:
4240 return getCursorObjCClassRef(C).second;
4241
4242 case CXCursor_TypeRef:
4243 return getCursorTypeRef(C).second;
4244
4245 case CXCursor_TemplateRef:
4246 return getCursorTemplateRef(C).second;
4247
4248 case CXCursor_NamespaceRef:
4249 return getCursorNamespaceRef(C).second;
4250
4251 case CXCursor_MemberRef:
4252 return getCursorMemberRef(C).second;
4253
4254 case CXCursor_CXXBaseSpecifier:
4255 return getCursorCXXBaseSpecifier(C)->getSourceRange();
4256
4257 case CXCursor_LabelRef:
4258 return getCursorLabelRef(C).second;
4259
4260 case CXCursor_OverloadedDeclRef:
4261 return getCursorOverloadedDeclRef(C).second;
4262
4263 case CXCursor_VariableRef:
4264 return getCursorVariableRef(C).second;
4265
4266 default:
4267 // FIXME: Need a way to enumerate all non-reference cases.
4268 llvm_unreachable("Missed a reference kind");
4269 }
4270 }
4271
4272 if (clang_isExpression(C.kind))
4273 return getCursorExpr(C)->getSourceRange();
4274
4275 if (clang_isStatement(C.kind))
4276 return getCursorStmt(C)->getSourceRange();
4277
4278 if (clang_isAttribute(C.kind))
4279 return getCursorAttr(C)->getRange();
4280
4281 if (C.kind == CXCursor_PreprocessingDirective)
4282 return cxcursor::getCursorPreprocessingDirective(C);
4283
4284 if (C.kind == CXCursor_MacroExpansion) {
4285 ASTUnit *TU = getCursorASTUnit(C);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00004286 SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004287 return TU->mapRangeFromPreamble(Range);
4288 }
4289
4290 if (C.kind == CXCursor_MacroDefinition) {
4291 ASTUnit *TU = getCursorASTUnit(C);
4292 SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
4293 return TU->mapRangeFromPreamble(Range);
4294 }
4295
4296 if (C.kind == CXCursor_InclusionDirective) {
4297 ASTUnit *TU = getCursorASTUnit(C);
4298 SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange();
4299 return TU->mapRangeFromPreamble(Range);
4300 }
4301
4302 if (C.kind == CXCursor_TranslationUnit) {
4303 ASTUnit *TU = getCursorASTUnit(C);
4304 FileID MainID = TU->getSourceManager().getMainFileID();
4305 SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID);
4306 SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID);
4307 return SourceRange(Start, End);
4308 }
4309
4310 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004311 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004312 if (!D)
4313 return SourceRange();
4314
4315 SourceRange R = D->getSourceRange();
4316 // FIXME: Multiple variables declared in a single declaration
4317 // currently lack the information needed to correctly determine their
4318 // ranges when accounting for the type-specifier. We use context
4319 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4320 // and if so, whether it is the first decl.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004321 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004322 if (!cxcursor::isFirstInDeclGroup(C))
4323 R.setBegin(VD->getLocation());
4324 }
4325 return R;
4326 }
4327 return SourceRange();
4328}
4329
4330/// \brief Retrieves the "raw" cursor extent, which is then extended to include
4331/// the decl-specifier-seq for declarations.
4332static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
4333 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004334 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004335 if (!D)
4336 return SourceRange();
4337
4338 SourceRange R = D->getSourceRange();
4339
4340 // Adjust the start of the location for declarations preceded by
4341 // declaration specifiers.
4342 SourceLocation StartLoc;
4343 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
4344 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
4345 StartLoc = TI->getTypeLoc().getLocStart();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004346 } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004347 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
4348 StartLoc = TI->getTypeLoc().getLocStart();
4349 }
4350
4351 if (StartLoc.isValid() && R.getBegin().isValid() &&
4352 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
4353 R.setBegin(StartLoc);
4354
4355 // FIXME: Multiple variables declared in a single declaration
4356 // currently lack the information needed to correctly determine their
4357 // ranges when accounting for the type-specifier. We use context
4358 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4359 // and if so, whether it is the first decl.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004360 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004361 if (!cxcursor::isFirstInDeclGroup(C))
4362 R.setBegin(VD->getLocation());
4363 }
4364
4365 return R;
4366 }
4367
4368 return getRawCursorExtent(C);
4369}
4370
4371extern "C" {
4372
4373CXSourceRange clang_getCursorExtent(CXCursor C) {
4374 SourceRange R = getRawCursorExtent(C);
4375 if (R.isInvalid())
4376 return clang_getNullRange();
4377
4378 return cxloc::translateSourceRange(getCursorContext(C), R);
4379}
4380
4381CXCursor clang_getCursorReferenced(CXCursor C) {
4382 if (clang_isInvalid(C.kind))
4383 return clang_getNullCursor();
4384
4385 CXTranslationUnit tu = getCursorTU(C);
4386 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004387 const Decl *D = getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004388 if (!D)
4389 return clang_getNullCursor();
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004390 if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004391 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004392 if (const ObjCPropertyImplDecl *PropImpl =
4393 dyn_cast<ObjCPropertyImplDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004394 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
4395 return MakeCXCursor(Property, tu);
4396
4397 return C;
4398 }
4399
4400 if (clang_isExpression(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004401 const Expr *E = getCursorExpr(C);
4402 const Decl *D = getDeclFromExpr(E);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004403 if (D) {
4404 CXCursor declCursor = MakeCXCursor(D, tu);
4405 declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C),
4406 declCursor);
4407 return declCursor;
4408 }
4409
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004410 if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004411 return MakeCursorOverloadedDeclRef(Ovl, tu);
4412
4413 return clang_getNullCursor();
4414 }
4415
4416 if (clang_isStatement(C.kind)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004417 const Stmt *S = getCursorStmt(C);
4418 if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004419 if (LabelDecl *label = Goto->getLabel())
4420 if (LabelStmt *labelS = label->getStmt())
4421 return MakeCXCursor(labelS, getCursorDecl(C), tu);
4422
4423 return clang_getNullCursor();
4424 }
4425
4426 if (C.kind == CXCursor_MacroExpansion) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004427 if (const MacroDefinition *Def = getCursorMacroExpansion(C).getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004428 return MakeMacroDefinitionCursor(Def, tu);
4429 }
4430
4431 if (!clang_isReference(C.kind))
4432 return clang_getNullCursor();
4433
4434 switch (C.kind) {
4435 case CXCursor_ObjCSuperClassRef:
4436 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
4437
4438 case CXCursor_ObjCProtocolRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004439 const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first;
4440 if (const ObjCProtocolDecl *Def = Prot->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004441 return MakeCXCursor(Def, tu);
4442
4443 return MakeCXCursor(Prot, tu);
4444 }
4445
4446 case CXCursor_ObjCClassRef: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004447 const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
4448 if (const ObjCInterfaceDecl *Def = Class->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004449 return MakeCXCursor(Def, tu);
4450
4451 return MakeCXCursor(Class, tu);
4452 }
4453
4454 case CXCursor_TypeRef:
4455 return MakeCXCursor(getCursorTypeRef(C).first, tu );
4456
4457 case CXCursor_TemplateRef:
4458 return MakeCXCursor(getCursorTemplateRef(C).first, tu );
4459
4460 case CXCursor_NamespaceRef:
4461 return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
4462
4463 case CXCursor_MemberRef:
4464 return MakeCXCursor(getCursorMemberRef(C).first, tu );
4465
4466 case CXCursor_CXXBaseSpecifier: {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00004467 const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004468 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
4469 tu ));
4470 }
4471
4472 case CXCursor_LabelRef:
4473 // FIXME: We end up faking the "parent" declaration here because we
4474 // don't want to make CXCursor larger.
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00004475 return MakeCXCursor(getCursorLabelRef(C).first,
4476 cxtu::getASTUnit(tu)->getASTContext()
4477 .getTranslationUnitDecl(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004478 tu);
4479
4480 case CXCursor_OverloadedDeclRef:
4481 return C;
4482
4483 case CXCursor_VariableRef:
4484 return MakeCXCursor(getCursorVariableRef(C).first, tu);
4485
4486 default:
4487 // We would prefer to enumerate all non-reference cursor kinds here.
4488 llvm_unreachable("Unhandled reference cursor kind");
4489 }
4490}
4491
4492CXCursor clang_getCursorDefinition(CXCursor C) {
4493 if (clang_isInvalid(C.kind))
4494 return clang_getNullCursor();
4495
4496 CXTranslationUnit TU = getCursorTU(C);
4497
4498 bool WasReference = false;
4499 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
4500 C = clang_getCursorReferenced(C);
4501 WasReference = true;
4502 }
4503
4504 if (C.kind == CXCursor_MacroExpansion)
4505 return clang_getCursorReferenced(C);
4506
4507 if (!clang_isDeclaration(C.kind))
4508 return clang_getNullCursor();
4509
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004510 const Decl *D = getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004511 if (!D)
4512 return clang_getNullCursor();
4513
4514 switch (D->getKind()) {
4515 // Declaration kinds that don't really separate the notions of
4516 // declaration and definition.
4517 case Decl::Namespace:
4518 case Decl::Typedef:
4519 case Decl::TypeAlias:
4520 case Decl::TypeAliasTemplate:
4521 case Decl::TemplateTypeParm:
4522 case Decl::EnumConstant:
4523 case Decl::Field:
John McCall76da55d2013-04-16 07:28:30 +00004524 case Decl::MSProperty:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004525 case Decl::IndirectField:
4526 case Decl::ObjCIvar:
4527 case Decl::ObjCAtDefsField:
4528 case Decl::ImplicitParam:
4529 case Decl::ParmVar:
4530 case Decl::NonTypeTemplateParm:
4531 case Decl::TemplateTemplateParm:
4532 case Decl::ObjCCategoryImpl:
4533 case Decl::ObjCImplementation:
4534 case Decl::AccessSpec:
4535 case Decl::LinkageSpec:
4536 case Decl::ObjCPropertyImpl:
4537 case Decl::FileScopeAsm:
4538 case Decl::StaticAssert:
4539 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00004540 case Decl::Captured:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004541 case Decl::Label: // FIXME: Is this right??
4542 case Decl::ClassScopeFunctionSpecialization:
4543 case Decl::Import:
Alexey Bataevc6400582013-03-22 06:34:35 +00004544 case Decl::OMPThreadPrivate:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004545 return C;
4546
4547 // Declaration kinds that don't make any sense here, but are
4548 // nonetheless harmless.
David Blaikief23546a2013-02-22 17:44:58 +00004549 case Decl::Empty:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004550 case Decl::TranslationUnit:
4551 break;
4552
4553 // Declaration kinds for which the definition is not resolvable.
4554 case Decl::UnresolvedUsingTypename:
4555 case Decl::UnresolvedUsingValue:
4556 break;
4557
4558 case Decl::UsingDirective:
4559 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
4560 TU);
4561
4562 case Decl::NamespaceAlias:
4563 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
4564
4565 case Decl::Enum:
4566 case Decl::Record:
4567 case Decl::CXXRecord:
4568 case Decl::ClassTemplateSpecialization:
4569 case Decl::ClassTemplatePartialSpecialization:
4570 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
4571 return MakeCXCursor(Def, TU);
4572 return clang_getNullCursor();
4573
4574 case Decl::Function:
4575 case Decl::CXXMethod:
4576 case Decl::CXXConstructor:
4577 case Decl::CXXDestructor:
4578 case Decl::CXXConversion: {
4579 const FunctionDecl *Def = 0;
4580 if (cast<FunctionDecl>(D)->getBody(Def))
Dmitri Gribenko05756dc2013-01-14 00:46:27 +00004581 return MakeCXCursor(Def, TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004582 return clang_getNullCursor();
4583 }
4584
4585 case Decl::Var: {
4586 // Ask the variable if it has a definition.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004587 if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004588 return MakeCXCursor(Def, TU);
4589 return clang_getNullCursor();
4590 }
4591
4592 case Decl::FunctionTemplate: {
4593 const FunctionDecl *Def = 0;
4594 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
4595 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
4596 return clang_getNullCursor();
4597 }
4598
4599 case Decl::ClassTemplate: {
4600 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
4601 ->getDefinition())
4602 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
4603 TU);
4604 return clang_getNullCursor();
4605 }
4606
4607 case Decl::Using:
4608 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
4609 D->getLocation(), TU);
4610
4611 case Decl::UsingShadow:
4612 return clang_getCursorDefinition(
4613 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
4614 TU));
4615
4616 case Decl::ObjCMethod: {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004617 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004618 if (Method->isThisDeclarationADefinition())
4619 return C;
4620
4621 // Dig out the method definition in the associated
4622 // @implementation, if we have it.
4623 // FIXME: The ASTs should make finding the definition easier.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004624 if (const ObjCInterfaceDecl *Class
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004625 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
4626 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
4627 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
4628 Method->isInstanceMethod()))
4629 if (Def->isThisDeclarationADefinition())
4630 return MakeCXCursor(Def, TU);
4631
4632 return clang_getNullCursor();
4633 }
4634
4635 case Decl::ObjCCategory:
4636 if (ObjCCategoryImplDecl *Impl
4637 = cast<ObjCCategoryDecl>(D)->getImplementation())
4638 return MakeCXCursor(Impl, TU);
4639 return clang_getNullCursor();
4640
4641 case Decl::ObjCProtocol:
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004642 if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004643 return MakeCXCursor(Def, TU);
4644 return clang_getNullCursor();
4645
4646 case Decl::ObjCInterface: {
4647 // There are two notions of a "definition" for an Objective-C
4648 // class: the interface and its implementation. When we resolved a
4649 // reference to an Objective-C class, produce the @interface as
4650 // the definition; when we were provided with the interface,
4651 // produce the @implementation as the definition.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004652 const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004653 if (WasReference) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004654 if (const ObjCInterfaceDecl *Def = IFace->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004655 return MakeCXCursor(Def, TU);
4656 } else if (ObjCImplementationDecl *Impl = IFace->getImplementation())
4657 return MakeCXCursor(Impl, TU);
4658 return clang_getNullCursor();
4659 }
4660
4661 case Decl::ObjCProperty:
4662 // FIXME: We don't really know where to find the
4663 // ObjCPropertyImplDecls that implement this property.
4664 return clang_getNullCursor();
4665
4666 case Decl::ObjCCompatibleAlias:
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004667 if (const ObjCInterfaceDecl *Class
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004668 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004669 if (const ObjCInterfaceDecl *Def = Class->getDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004670 return MakeCXCursor(Def, TU);
4671
4672 return clang_getNullCursor();
4673
4674 case Decl::Friend:
4675 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
4676 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
4677 return clang_getNullCursor();
4678
4679 case Decl::FriendTemplate:
4680 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
4681 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
4682 return clang_getNullCursor();
4683 }
4684
4685 return clang_getNullCursor();
4686}
4687
4688unsigned clang_isCursorDefinition(CXCursor C) {
4689 if (!clang_isDeclaration(C.kind))
4690 return 0;
4691
4692 return clang_getCursorDefinition(C) == C;
4693}
4694
4695CXCursor clang_getCanonicalCursor(CXCursor C) {
4696 if (!clang_isDeclaration(C.kind))
4697 return C;
4698
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004699 if (const Decl *D = getCursorDecl(C)) {
4700 if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004701 if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
4702 return MakeCXCursor(CatD, getCursorTU(C));
4703
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004704 if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4705 if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004706 return MakeCXCursor(IFD, getCursorTU(C));
4707
4708 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
4709 }
4710
4711 return C;
4712}
4713
4714int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) {
4715 return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first;
4716}
4717
4718unsigned clang_getNumOverloadedDecls(CXCursor C) {
4719 if (C.kind != CXCursor_OverloadedDeclRef)
4720 return 0;
4721
4722 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004723 if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004724 return E->getNumDecls();
4725
4726 if (OverloadedTemplateStorage *S
4727 = Storage.dyn_cast<OverloadedTemplateStorage*>())
4728 return S->size();
4729
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004730 const Decl *D = Storage.get<const Decl *>();
4731 if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004732 return Using->shadow_size();
4733
4734 return 0;
4735}
4736
4737CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
4738 if (cursor.kind != CXCursor_OverloadedDeclRef)
4739 return clang_getNullCursor();
4740
4741 if (index >= clang_getNumOverloadedDecls(cursor))
4742 return clang_getNullCursor();
4743
4744 CXTranslationUnit TU = getCursorTU(cursor);
4745 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004746 if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004747 return MakeCXCursor(E->decls_begin()[index], TU);
4748
4749 if (OverloadedTemplateStorage *S
4750 = Storage.dyn_cast<OverloadedTemplateStorage*>())
4751 return MakeCXCursor(S->begin()[index], TU);
4752
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004753 const Decl *D = Storage.get<const Decl *>();
4754 if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004755 // FIXME: This is, unfortunately, linear time.
4756 UsingDecl::shadow_iterator Pos = Using->shadow_begin();
4757 std::advance(Pos, index);
4758 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
4759 }
4760
4761 return clang_getNullCursor();
4762}
4763
4764void clang_getDefinitionSpellingAndExtent(CXCursor C,
4765 const char **startBuf,
4766 const char **endBuf,
4767 unsigned *startLine,
4768 unsigned *startColumn,
4769 unsigned *endLine,
4770 unsigned *endColumn) {
4771 assert(getCursorDecl(C) && "CXCursor has null decl");
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00004772 const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004773 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
4774
4775 SourceManager &SM = FD->getASTContext().getSourceManager();
4776 *startBuf = SM.getCharacterData(Body->getLBracLoc());
4777 *endBuf = SM.getCharacterData(Body->getRBracLoc());
4778 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
4779 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
4780 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
4781 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
4782}
4783
4784
4785CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
4786 unsigned PieceIndex) {
4787 RefNamePieces Pieces;
4788
4789 switch (C.kind) {
4790 case CXCursor_MemberRefExpr:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004791 if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004792 Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(),
4793 E->getQualifierLoc().getSourceRange());
4794 break;
4795
4796 case CXCursor_DeclRefExpr:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004797 if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004798 Pieces = buildPieces(NameFlags, false, E->getNameInfo(),
4799 E->getQualifierLoc().getSourceRange(),
4800 E->getOptionalExplicitTemplateArgs());
4801 break;
4802
4803 case CXCursor_CallExpr:
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004804 if (const CXXOperatorCallExpr *OCE =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004805 dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004806 const Expr *Callee = OCE->getCallee();
4807 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004808 Callee = ICE->getSubExpr();
4809
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00004810 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004811 Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(),
4812 DRE->getQualifierLoc().getSourceRange());
4813 }
4814 break;
4815
4816 default:
4817 break;
4818 }
4819
4820 if (Pieces.empty()) {
4821 if (PieceIndex == 0)
4822 return clang_getCursorExtent(C);
4823 } else if (PieceIndex < Pieces.size()) {
4824 SourceRange R = Pieces[PieceIndex];
4825 if (R.isValid())
4826 return cxloc::translateSourceRange(getCursorContext(C), R);
4827 }
4828
4829 return clang_getNullRange();
4830}
4831
4832void clang_enableStackTraces(void) {
4833 llvm::sys::PrintStackTraceOnErrorSignal();
4834}
4835
4836void clang_executeOnThread(void (*fn)(void*), void *user_data,
4837 unsigned stack_size) {
4838 llvm::llvm_execute_on_thread(fn, user_data, stack_size);
4839}
4840
4841} // end: extern "C"
4842
4843//===----------------------------------------------------------------------===//
4844// Token-based Operations.
4845//===----------------------------------------------------------------------===//
4846
4847/* CXToken layout:
4848 * int_data[0]: a CXTokenKind
4849 * int_data[1]: starting token location
4850 * int_data[2]: token length
4851 * int_data[3]: reserved
4852 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
4853 * otherwise unused.
4854 */
4855extern "C" {
4856
4857CXTokenKind clang_getTokenKind(CXToken CXTok) {
4858 return static_cast<CXTokenKind>(CXTok.int_data[0]);
4859}
4860
4861CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
4862 switch (clang_getTokenKind(CXTok)) {
4863 case CXToken_Identifier:
4864 case CXToken_Keyword:
4865 // We know we have an IdentifierInfo*, so use that.
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +00004866 return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004867 ->getNameStart());
4868
4869 case CXToken_Literal: {
4870 // We have stashed the starting pointer in the ptr_data field. Use it.
4871 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00004872 return cxstring::createDup(StringRef(Text, CXTok.int_data[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004873 }
4874
4875 case CXToken_Punctuation:
4876 case CXToken_Comment:
4877 break;
4878 }
4879
4880 // We have to find the starting buffer pointer the hard way, by
4881 // deconstructing the source location.
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00004882 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004883 if (!CXXUnit)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00004884 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004885
4886 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
4887 std::pair<FileID, unsigned> LocInfo
4888 = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
4889 bool Invalid = false;
4890 StringRef Buffer
4891 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
4892 if (Invalid)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00004893 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004894
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00004895 return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004896}
4897
4898CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00004899 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004900 if (!CXXUnit)
4901 return clang_getNullLocation();
4902
4903 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
4904 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4905}
4906
4907CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00004908 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004909 if (!CXXUnit)
4910 return clang_getNullRange();
4911
4912 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
4913 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4914}
4915
4916static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
4917 SmallVectorImpl<CXToken> &CXTokens) {
4918 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4919 std::pair<FileID, unsigned> BeginLocInfo
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00004920 = SourceMgr.getDecomposedSpellingLoc(Range.getBegin());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004921 std::pair<FileID, unsigned> EndLocInfo
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00004922 = SourceMgr.getDecomposedSpellingLoc(Range.getEnd());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004923
4924 // Cannot tokenize across files.
4925 if (BeginLocInfo.first != EndLocInfo.first)
4926 return;
4927
4928 // Create a lexer
4929 bool Invalid = false;
4930 StringRef Buffer
4931 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
4932 if (Invalid)
4933 return;
4934
4935 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4936 CXXUnit->getASTContext().getLangOpts(),
4937 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
4938 Lex.SetCommentRetentionState(true);
4939
4940 // Lex tokens until we hit the end of the range.
4941 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
4942 Token Tok;
4943 bool previousWasAt = false;
4944 do {
4945 // Lex the next token
4946 Lex.LexFromRawLexer(Tok);
4947 if (Tok.is(tok::eof))
4948 break;
4949
4950 // Initialize the CXToken.
4951 CXToken CXTok;
4952
4953 // - Common fields
4954 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
4955 CXTok.int_data[2] = Tok.getLength();
4956 CXTok.int_data[3] = 0;
4957
4958 // - Kind-specific fields
4959 if (Tok.isLiteral()) {
4960 CXTok.int_data[0] = CXToken_Literal;
Dmitri Gribenkoe4ea8792013-01-23 15:56:07 +00004961 CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004962 } else if (Tok.is(tok::raw_identifier)) {
4963 // Lookup the identifier to determine whether we have a keyword.
4964 IdentifierInfo *II
4965 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
4966
4967 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
4968 CXTok.int_data[0] = CXToken_Keyword;
4969 }
4970 else {
4971 CXTok.int_data[0] = Tok.is(tok::identifier)
4972 ? CXToken_Identifier
4973 : CXToken_Keyword;
4974 }
4975 CXTok.ptr_data = II;
4976 } else if (Tok.is(tok::comment)) {
4977 CXTok.int_data[0] = CXToken_Comment;
4978 CXTok.ptr_data = 0;
4979 } else {
4980 CXTok.int_data[0] = CXToken_Punctuation;
4981 CXTok.ptr_data = 0;
4982 }
4983 CXTokens.push_back(CXTok);
4984 previousWasAt = Tok.is(tok::at);
4985 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
4986}
4987
4988void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
4989 CXToken **Tokens, unsigned *NumTokens) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00004990 LOG_FUNC_SECTION {
4991 *Log << TU << ' ' << Range;
4992 }
4993
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004994 if (Tokens)
4995 *Tokens = 0;
4996 if (NumTokens)
4997 *NumTokens = 0;
4998
Argyrios Kyrtzidis0b602832013-04-04 22:40:59 +00004999 if (!TU)
5000 return;
5001
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005002 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005003 if (!CXXUnit || !Tokens || !NumTokens)
5004 return;
5005
5006 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
5007
5008 SourceRange R = cxloc::translateCXSourceRange(Range);
5009 if (R.isInvalid())
5010 return;
5011
5012 SmallVector<CXToken, 32> CXTokens;
5013 getTokens(CXXUnit, R, CXTokens);
5014
5015 if (CXTokens.empty())
5016 return;
5017
5018 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
5019 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
5020 *NumTokens = CXTokens.size();
5021}
5022
5023void clang_disposeTokens(CXTranslationUnit TU,
5024 CXToken *Tokens, unsigned NumTokens) {
5025 free(Tokens);
5026}
5027
5028} // end: extern "C"
5029
5030//===----------------------------------------------------------------------===//
5031// Token annotation APIs.
5032//===----------------------------------------------------------------------===//
5033
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005034static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
5035 CXCursor parent,
5036 CXClientData client_data);
5037static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
5038 CXClientData client_data);
5039
5040namespace {
5041class AnnotateTokensWorker {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005042 CXToken *Tokens;
5043 CXCursor *Cursors;
5044 unsigned NumTokens;
5045 unsigned TokIdx;
5046 unsigned PreprocessingTokIdx;
5047 CursorVisitor AnnotateVis;
5048 SourceManager &SrcMgr;
5049 bool HasContextSensitiveKeywords;
5050
5051 struct PostChildrenInfo {
5052 CXCursor Cursor;
5053 SourceRange CursorRange;
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005054 unsigned BeforeReachingCursorIdx;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005055 unsigned BeforeChildrenTokenIdx;
5056 };
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005057 SmallVector<PostChildrenInfo, 8> PostChildrenInfos;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005058
5059 bool MoreTokens() const { return TokIdx < NumTokens; }
5060 unsigned NextToken() const { return TokIdx; }
5061 void AdvanceToken() { ++TokIdx; }
5062 SourceLocation GetTokenLoc(unsigned tokI) {
5063 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
5064 }
5065 bool isFunctionMacroToken(unsigned tokI) const {
5066 return Tokens[tokI].int_data[3] != 0;
5067 }
5068 SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
5069 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]);
5070 }
5071
5072 void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005073 bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005074 SourceRange);
5075
5076public:
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005077 AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens,
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005078 CXTranslationUnit TU, SourceRange RegionOfInterest)
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005079 : Tokens(tokens), Cursors(cursors),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005080 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005081 AnnotateVis(TU,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005082 AnnotateTokensVisitor, this,
5083 /*VisitPreprocessorLast=*/true,
5084 /*VisitIncludedEntities=*/false,
5085 RegionOfInterest,
5086 /*VisitDeclsOnly=*/false,
5087 AnnotateTokensPostChildrenVisitor),
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005088 SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005089 HasContextSensitiveKeywords(false) { }
5090
5091 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
5092 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
5093 bool postVisitChildren(CXCursor cursor);
5094 void AnnotateTokens();
5095
5096 /// \brief Determine whether the annotator saw any cursors that have
5097 /// context-sensitive keywords.
5098 bool hasContextSensitiveKeywords() const {
5099 return HasContextSensitiveKeywords;
5100 }
5101
5102 ~AnnotateTokensWorker() {
5103 assert(PostChildrenInfos.empty());
5104 }
5105};
5106}
5107
5108void AnnotateTokensWorker::AnnotateTokens() {
5109 // Walk the AST within the region of interest, annotating tokens
5110 // along the way.
5111 AnnotateVis.visitFileRegion();
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005112}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005113
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005114static inline void updateCursorAnnotation(CXCursor &Cursor,
5115 const CXCursor &updateC) {
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005116 if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005117 return;
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005118 Cursor = updateC;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005119}
5120
5121/// \brief It annotates and advances tokens with a cursor until the comparison
5122//// between the cursor location and the source range is the same as
5123/// \arg compResult.
5124///
5125/// Pass RangeBefore to annotate tokens with a cursor until a range is reached.
5126/// Pass RangeOverlap to annotate tokens inside a range.
5127void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC,
5128 RangeComparisonResult compResult,
5129 SourceRange range) {
5130 while (MoreTokens()) {
5131 const unsigned I = NextToken();
5132 if (isFunctionMacroToken(I))
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005133 if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range))
5134 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005135
5136 SourceLocation TokLoc = GetTokenLoc(I);
5137 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005138 updateCursorAnnotation(Cursors[I], updateC);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005139 AdvanceToken();
5140 continue;
5141 }
5142 break;
5143 }
5144}
5145
5146/// \brief Special annotation handling for macro argument tokens.
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005147/// \returns true if it advanced beyond all macro tokens, false otherwise.
5148bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005149 CXCursor updateC,
5150 RangeComparisonResult compResult,
5151 SourceRange range) {
5152 assert(MoreTokens());
5153 assert(isFunctionMacroToken(NextToken()) &&
5154 "Should be called only for macro arg tokens");
5155
5156 // This works differently than annotateAndAdvanceTokens; because expanded
5157 // macro arguments can have arbitrary translation-unit source order, we do not
5158 // advance the token index one by one until a token fails the range test.
5159 // We only advance once past all of the macro arg tokens if all of them
5160 // pass the range test. If one of them fails we keep the token index pointing
5161 // at the start of the macro arg tokens so that the failing token will be
5162 // annotated by a subsequent annotation try.
5163
5164 bool atLeastOneCompFail = false;
5165
5166 unsigned I = NextToken();
5167 for (; I < NumTokens && isFunctionMacroToken(I); ++I) {
5168 SourceLocation TokLoc = getFunctionMacroTokenLoc(I);
5169 if (TokLoc.isFileID())
5170 continue; // not macro arg token, it's parens or comma.
5171 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
5172 if (clang_isInvalid(clang_getCursorKind(Cursors[I])))
5173 Cursors[I] = updateC;
5174 } else
5175 atLeastOneCompFail = true;
5176 }
5177
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005178 if (atLeastOneCompFail)
5179 return false;
5180
5181 TokIdx = I; // All of the tokens were handled, advance beyond all of them.
5182 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005183}
5184
5185enum CXChildVisitResult
5186AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005187 SourceRange cursorRange = getRawCursorExtent(cursor);
5188 if (cursorRange.isInvalid())
5189 return CXChildVisit_Recurse;
5190
5191 if (!HasContextSensitiveKeywords) {
5192 // Objective-C properties can have context-sensitive keywords.
5193 if (cursor.kind == CXCursor_ObjCPropertyDecl) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005194 if (const ObjCPropertyDecl *Property
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005195 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor)))
5196 HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0;
5197 }
5198 // Objective-C methods can have context-sensitive keywords.
5199 else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl ||
5200 cursor.kind == CXCursor_ObjCClassMethodDecl) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005201 if (const ObjCMethodDecl *Method
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005202 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
5203 if (Method->getObjCDeclQualifier())
5204 HasContextSensitiveKeywords = true;
5205 else {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005206 for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
5207 PEnd = Method->param_end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005208 P != PEnd; ++P) {
5209 if ((*P)->getObjCDeclQualifier()) {
5210 HasContextSensitiveKeywords = true;
5211 break;
5212 }
5213 }
5214 }
5215 }
5216 }
5217 // C++ methods can have context-sensitive keywords.
5218 else if (cursor.kind == CXCursor_CXXMethod) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005219 if (const CXXMethodDecl *Method
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005220 = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) {
5221 if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>())
5222 HasContextSensitiveKeywords = true;
5223 }
5224 }
5225 // C++ classes can have context-sensitive keywords.
5226 else if (cursor.kind == CXCursor_StructDecl ||
5227 cursor.kind == CXCursor_ClassDecl ||
5228 cursor.kind == CXCursor_ClassTemplate ||
5229 cursor.kind == CXCursor_ClassTemplatePartialSpecialization) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005230 if (const Decl *D = getCursorDecl(cursor))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005231 if (D->hasAttr<FinalAttr>())
5232 HasContextSensitiveKeywords = true;
5233 }
5234 }
Argyrios Kyrtzidis25cd4a22013-06-04 18:24:30 +00005235
5236 // Don't override a property annotation with its getter/setter method.
5237 if (cursor.kind == CXCursor_ObjCInstanceMethodDecl &&
5238 parent.kind == CXCursor_ObjCPropertyDecl)
5239 return CXChildVisit_Continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005240
5241 if (clang_isPreprocessing(cursor.kind)) {
5242 // Items in the preprocessing record are kept separate from items in
5243 // declarations, so we keep a separate token index.
5244 unsigned SavedTokIdx = TokIdx;
5245 TokIdx = PreprocessingTokIdx;
5246
5247 // Skip tokens up until we catch up to the beginning of the preprocessing
5248 // entry.
5249 while (MoreTokens()) {
5250 const unsigned I = NextToken();
5251 SourceLocation TokLoc = GetTokenLoc(I);
5252 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
5253 case RangeBefore:
5254 AdvanceToken();
5255 continue;
5256 case RangeAfter:
5257 case RangeOverlap:
5258 break;
5259 }
5260 break;
5261 }
5262
5263 // Look at all of the tokens within this range.
5264 while (MoreTokens()) {
5265 const unsigned I = NextToken();
5266 SourceLocation TokLoc = GetTokenLoc(I);
5267 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
5268 case RangeBefore:
5269 llvm_unreachable("Infeasible");
5270 case RangeAfter:
5271 break;
5272 case RangeOverlap:
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00005273 // For macro expansions, just note where the beginning of the macro
5274 // expansion occurs.
5275 if (cursor.kind == CXCursor_MacroExpansion) {
5276 if (TokLoc == cursorRange.getBegin())
5277 Cursors[I] = cursor;
5278 AdvanceToken();
5279 break;
5280 }
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005281 // We may have already annotated macro names inside macro definitions.
5282 if (Cursors[I].kind != CXCursor_MacroExpansion)
5283 Cursors[I] = cursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005284 AdvanceToken();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005285 continue;
5286 }
5287 break;
5288 }
5289
5290 // Save the preprocessing token index; restore the non-preprocessing
5291 // token index.
5292 PreprocessingTokIdx = TokIdx;
5293 TokIdx = SavedTokIdx;
5294 return CXChildVisit_Recurse;
5295 }
5296
5297 if (cursorRange.isInvalid())
5298 return CXChildVisit_Continue;
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005299
5300 unsigned BeforeReachingCursorIdx = NextToken();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005301 const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005302 const enum CXCursorKind K = clang_getCursorKind(parent);
5303 const CXCursor updateC =
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005304 (clang_isInvalid(K) || K == CXCursor_TranslationUnit ||
5305 // Attributes are annotated out-of-order, skip tokens until we reach it.
5306 clang_isAttribute(cursor.kind))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005307 ? clang_getNullCursor() : parent;
5308
5309 annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange);
5310
5311 // Avoid having the cursor of an expression "overwrite" the annotation of the
5312 // variable declaration that it belongs to.
5313 // This can happen for C++ constructor expressions whose range generally
5314 // include the variable declaration, e.g.:
5315 // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor.
5316 if (clang_isExpression(cursorK)) {
Dmitri Gribenkoff74f962013-01-26 15:29:08 +00005317 const Expr *E = getCursorExpr(cursor);
Dmitri Gribenko404628c2013-01-26 18:12:08 +00005318 if (const Decl *D = getCursorParentDecl(cursor)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005319 const unsigned I = NextToken();
5320 if (E->getLocStart().isValid() && D->getLocation().isValid() &&
5321 E->getLocStart() == D->getLocation() &&
5322 E->getLocStart() == GetTokenLoc(I)) {
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005323 updateCursorAnnotation(Cursors[I], updateC);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005324 AdvanceToken();
5325 }
5326 }
5327 }
5328
5329 // Before recursing into the children keep some state that we are going
5330 // to use in the AnnotateTokensWorker::postVisitChildren callback to do some
5331 // extra work after the child nodes are visited.
5332 // Note that we don't call VisitChildren here to avoid traversing statements
5333 // code-recursively which can blow the stack.
5334
5335 PostChildrenInfo Info;
5336 Info.Cursor = cursor;
5337 Info.CursorRange = cursorRange;
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005338 Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005339 Info.BeforeChildrenTokenIdx = NextToken();
5340 PostChildrenInfos.push_back(Info);
5341
5342 return CXChildVisit_Recurse;
5343}
5344
5345bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) {
5346 if (PostChildrenInfos.empty())
5347 return false;
5348 const PostChildrenInfo &Info = PostChildrenInfos.back();
5349 if (!clang_equalCursors(Info.Cursor, cursor))
5350 return false;
5351
5352 const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx;
5353 const unsigned AfterChildren = NextToken();
5354 SourceRange cursorRange = Info.CursorRange;
5355
5356 // Scan the tokens that are at the end of the cursor, but are not captured
5357 // but the child cursors.
5358 annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange);
5359
5360 // Scan the tokens that are at the beginning of the cursor, but are not
5361 // capture by the child cursors.
5362 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
5363 if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
5364 break;
5365
5366 Cursors[I] = cursor;
5367 }
5368
Argyrios Kyrtzidisa86b37e2013-02-08 01:12:25 +00005369 // Attributes are annotated out-of-order, rewind TokIdx to when we first
5370 // encountered the attribute cursor.
5371 if (clang_isAttribute(cursor.kind))
5372 TokIdx = Info.BeforeReachingCursorIdx;
5373
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005374 PostChildrenInfos.pop_back();
5375 return false;
5376}
5377
5378static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
5379 CXCursor parent,
5380 CXClientData client_data) {
5381 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
5382}
5383
5384static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
5385 CXClientData client_data) {
5386 return static_cast<AnnotateTokensWorker*>(client_data)->
5387 postVisitChildren(cursor);
5388}
5389
5390namespace {
5391
5392/// \brief Uses the macro expansions in the preprocessing record to find
5393/// and mark tokens that are macro arguments. This info is used by the
5394/// AnnotateTokensWorker.
5395class MarkMacroArgTokensVisitor {
5396 SourceManager &SM;
5397 CXToken *Tokens;
5398 unsigned NumTokens;
5399 unsigned CurIdx;
5400
5401public:
5402 MarkMacroArgTokensVisitor(SourceManager &SM,
5403 CXToken *tokens, unsigned numTokens)
5404 : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { }
5405
5406 CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {
5407 if (cursor.kind != CXCursor_MacroExpansion)
5408 return CXChildVisit_Continue;
5409
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00005410 SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005411 if (macroRange.getBegin() == macroRange.getEnd())
5412 return CXChildVisit_Continue; // it's not a function macro.
5413
5414 for (; CurIdx < NumTokens; ++CurIdx) {
5415 if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx),
5416 macroRange.getBegin()))
5417 break;
5418 }
5419
5420 if (CurIdx == NumTokens)
5421 return CXChildVisit_Break;
5422
5423 for (; CurIdx < NumTokens; ++CurIdx) {
5424 SourceLocation tokLoc = getTokenLoc(CurIdx);
5425 if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd()))
5426 break;
5427
5428 setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc));
5429 }
5430
5431 if (CurIdx == NumTokens)
5432 return CXChildVisit_Break;
5433
5434 return CXChildVisit_Continue;
5435 }
5436
5437private:
5438 SourceLocation getTokenLoc(unsigned tokI) {
5439 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
5440 }
5441
5442 void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
5443 // The third field is reserved and currently not used. Use it here
5444 // to mark macro arg expanded tokens with their expanded locations.
5445 Tokens[tokI].int_data[3] = loc.getRawEncoding();
5446 }
5447};
5448
5449} // end anonymous namespace
5450
5451static CXChildVisitResult
5452MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
5453 CXClientData client_data) {
5454 return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor,
5455 parent);
5456}
5457
5458namespace {
5459 struct clang_annotateTokens_Data {
5460 CXTranslationUnit TU;
5461 ASTUnit *CXXUnit;
5462 CXToken *Tokens;
5463 unsigned NumTokens;
5464 CXCursor *Cursors;
5465 };
5466}
5467
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005468/// \brief Used by \c annotatePreprocessorTokens.
5469/// \returns true if lexing was finished, false otherwise.
5470static bool lexNext(Lexer &Lex, Token &Tok,
5471 unsigned &NextIdx, unsigned NumTokens) {
5472 if (NextIdx >= NumTokens)
5473 return true;
5474
5475 ++NextIdx;
5476 Lex.LexFromRawLexer(Tok);
5477 if (Tok.is(tok::eof))
5478 return true;
5479
5480 return false;
5481}
5482
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005483static void annotatePreprocessorTokens(CXTranslationUnit TU,
5484 SourceRange RegionOfInterest,
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005485 CXCursor *Cursors,
5486 CXToken *Tokens,
5487 unsigned NumTokens) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005488 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005489
Argyrios Kyrtzidis3453bf72013-01-07 19:16:32 +00005490 Preprocessor &PP = CXXUnit->getPreprocessor();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005491 SourceManager &SourceMgr = CXXUnit->getSourceManager();
5492 std::pair<FileID, unsigned> BeginLocInfo
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00005493 = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005494 std::pair<FileID, unsigned> EndLocInfo
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00005495 = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005496
5497 if (BeginLocInfo.first != EndLocInfo.first)
5498 return;
5499
5500 StringRef Buffer;
5501 bool Invalid = false;
5502 Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
5503 if (Buffer.empty() || Invalid)
5504 return;
5505
5506 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
5507 CXXUnit->getASTContext().getLangOpts(),
5508 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
5509 Buffer.end());
5510 Lex.SetCommentRetentionState(true);
5511
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005512 unsigned NextIdx = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005513 // Lex tokens in raw mode until we hit the end of the range, to avoid
5514 // entering #includes or expanding macros.
5515 while (true) {
5516 Token Tok;
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005517 if (lexNext(Lex, Tok, NextIdx, NumTokens))
5518 break;
5519 unsigned TokIdx = NextIdx-1;
5520 assert(Tok.getLocation() ==
5521 SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005522
5523 reprocess:
5524 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005525 // We have found a preprocessing directive. Annotate the tokens
5526 // appropriately.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005527 //
5528 // FIXME: Some simple tests here could identify macro definitions and
5529 // #undefs, to provide specific cursor kinds for those.
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005530
5531 SourceLocation BeginLoc = Tok.getLocation();
Argyrios Kyrtzidis3453bf72013-01-07 19:16:32 +00005532 if (lexNext(Lex, Tok, NextIdx, NumTokens))
5533 break;
5534
5535 MacroInfo *MI = 0;
5536 if (Tok.is(tok::raw_identifier) &&
5537 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == "define") {
5538 if (lexNext(Lex, Tok, NextIdx, NumTokens))
5539 break;
5540
5541 if (Tok.is(tok::raw_identifier)) {
5542 StringRef Name(Tok.getRawIdentifierData(), Tok.getLength());
5543 IdentifierInfo &II = PP.getIdentifierTable().get(Name);
5544 SourceLocation MappedTokLoc =
5545 CXXUnit->mapLocationToPreamble(Tok.getLocation());
5546 MI = getMacroInfo(II, MappedTokLoc, TU);
5547 }
5548 }
5549
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005550 bool finished = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005551 do {
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005552 if (lexNext(Lex, Tok, NextIdx, NumTokens)) {
5553 finished = true;
5554 break;
5555 }
Argyrios Kyrtzidis3453bf72013-01-07 19:16:32 +00005556 // If we are in a macro definition, check if the token was ever a
5557 // macro name and annotate it if that's the case.
5558 if (MI) {
5559 SourceLocation SaveLoc = Tok.getLocation();
5560 Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc));
5561 MacroDefinition *MacroDef = checkForMacroInMacroDefinition(MI,Tok,TU);
5562 Tok.setLocation(SaveLoc);
5563 if (MacroDef)
5564 Cursors[NextIdx-1] = MakeMacroExpansionCursor(MacroDef,
5565 Tok.getLocation(), TU);
5566 }
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005567 } while (!Tok.isAtStartOfLine());
5568
5569 unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2;
5570 assert(TokIdx <= LastIdx);
5571 SourceLocation EndLoc =
5572 SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]);
5573 CXCursor Cursor =
5574 MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU);
5575
5576 for (; TokIdx <= LastIdx; ++TokIdx)
Argyrios Kyrtzidis3453bf72013-01-07 19:16:32 +00005577 updateCursorAnnotation(Cursors[TokIdx], Cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005578
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005579 if (finished)
5580 break;
5581 goto reprocess;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005582 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005583 }
5584}
5585
5586// This gets run a separate thread to avoid stack blowout.
5587static void clang_annotateTokensImpl(void *UserData) {
5588 CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU;
5589 ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit;
5590 CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens;
5591 const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens;
5592 CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors;
5593
Dmitri Gribenko8c718e72013-01-26 21:49:50 +00005594 CIndexer *CXXIdx = TU->CIdx;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005595 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
5596 setThreadBackgroundPriority();
5597
5598 // Determine the region of interest, which contains all of the tokens.
5599 SourceRange RegionOfInterest;
5600 RegionOfInterest.setBegin(
5601 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
5602 RegionOfInterest.setEnd(
5603 cxloc::translateSourceLocation(clang_getTokenLocation(TU,
5604 Tokens[NumTokens-1])));
5605
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005606 // Relex the tokens within the source range to look for preprocessing
5607 // directives.
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005608 annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens);
Argyrios Kyrtzidis82064212013-02-13 18:33:28 +00005609
5610 // If begin location points inside a macro argument, set it to the expansion
5611 // location so we can have the full context when annotating semantically.
5612 {
5613 SourceManager &SM = CXXUnit->getSourceManager();
5614 SourceLocation Loc =
5615 SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin());
5616 if (Loc.isMacroID())
5617 RegionOfInterest.setBegin(SM.getExpansionLoc(Loc));
5618 }
5619
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005620 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
5621 // Search and mark tokens that are macro argument expansions.
5622 MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(),
5623 Tokens, NumTokens);
5624 CursorVisitor MacroArgMarker(TU,
5625 MarkMacroArgTokensVisitorDelegate, &Visitor,
5626 /*VisitPreprocessorLast=*/true,
5627 /*VisitIncludedEntities=*/false,
5628 RegionOfInterest);
5629 MacroArgMarker.visitPreprocessedEntitiesInRegion();
5630 }
5631
5632 // Annotate all of the source locations in the region of interest that map to
5633 // a specific cursor.
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00005634 AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005635
5636 // FIXME: We use a ridiculous stack size here because the data-recursion
5637 // algorithm uses a large stack frame than the non-data recursive version,
5638 // and AnnotationTokensWorker currently transforms the data-recursion
5639 // algorithm back into a traditional recursion by explicitly calling
5640 // VisitChildren(). We will need to remove this explicit recursive call.
5641 W.AnnotateTokens();
5642
5643 // If we ran into any entities that involve context-sensitive keywords,
5644 // take another pass through the tokens to mark them as such.
5645 if (W.hasContextSensitiveKeywords()) {
5646 for (unsigned I = 0; I != NumTokens; ++I) {
5647 if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier)
5648 continue;
5649
5650 if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) {
5651 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005652 if (const ObjCPropertyDecl *Property
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005653 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) {
5654 if (Property->getPropertyAttributesAsWritten() != 0 &&
5655 llvm::StringSwitch<bool>(II->getName())
5656 .Case("readonly", true)
5657 .Case("assign", true)
5658 .Case("unsafe_unretained", true)
5659 .Case("readwrite", true)
5660 .Case("retain", true)
5661 .Case("copy", true)
5662 .Case("nonatomic", true)
5663 .Case("atomic", true)
5664 .Case("getter", true)
5665 .Case("setter", true)
5666 .Case("strong", true)
5667 .Case("weak", true)
5668 .Default(false))
5669 Tokens[I].int_data[0] = CXToken_Keyword;
5670 }
5671 continue;
5672 }
5673
5674 if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl ||
5675 Cursors[I].kind == CXCursor_ObjCClassMethodDecl) {
5676 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
5677 if (llvm::StringSwitch<bool>(II->getName())
5678 .Case("in", true)
5679 .Case("out", true)
5680 .Case("inout", true)
5681 .Case("oneway", true)
5682 .Case("bycopy", true)
5683 .Case("byref", true)
5684 .Default(false))
5685 Tokens[I].int_data[0] = CXToken_Keyword;
5686 continue;
5687 }
5688
5689 if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
5690 Cursors[I].kind == CXCursor_CXXOverrideAttr) {
5691 Tokens[I].int_data[0] = CXToken_Keyword;
5692 continue;
5693 }
5694 }
5695 }
5696}
5697
5698extern "C" {
5699
5700void clang_annotateTokens(CXTranslationUnit TU,
5701 CXToken *Tokens, unsigned NumTokens,
5702 CXCursor *Cursors) {
Argyrios Kyrtzidis0b602832013-04-04 22:40:59 +00005703 if (!TU || NumTokens == 0 || !Tokens || !Cursors) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00005704 LOG_FUNC_SECTION { *Log << "<null input>"; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005705 return;
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00005706 }
5707
5708 LOG_FUNC_SECTION {
5709 *Log << TU << ' ';
5710 CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]);
5711 CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]);
5712 *Log << clang_getRange(bloc, eloc);
5713 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005714
5715 // Any token we don't specifically annotate will have a NULL cursor.
5716 CXCursor C = clang_getNullCursor();
5717 for (unsigned I = 0; I != NumTokens; ++I)
5718 Cursors[I] = C;
5719
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00005720 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005721 if (!CXXUnit)
5722 return;
5723
5724 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
5725
5726 clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors };
5727 llvm::CrashRecoveryContext CRC;
5728 if (!RunSafely(CRC, clang_annotateTokensImpl, &data,
5729 GetSafetyThreadStackSize() * 2)) {
5730 fprintf(stderr, "libclang: crash detected while annotating tokens\n");
5731 }
5732}
5733
5734} // end: extern "C"
5735
5736//===----------------------------------------------------------------------===//
5737// Operations for querying linkage of a cursor.
5738//===----------------------------------------------------------------------===//
5739
5740extern "C" {
5741CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
5742 if (!clang_isDeclaration(cursor.kind))
5743 return CXLinkage_Invalid;
5744
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005745 const Decl *D = cxcursor::getCursorDecl(cursor);
5746 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
Rafael Espindola181e3ec2013-05-13 00:12:11 +00005747 switch (ND->getLinkageInternal()) {
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +00005748 case NoLinkage:
5749 case VisibleNoLinkage: return CXLinkage_NoLinkage;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005750 case InternalLinkage: return CXLinkage_Internal;
5751 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
5752 case ExternalLinkage: return CXLinkage_External;
5753 };
5754
5755 return CXLinkage_Invalid;
5756}
5757} // end: extern "C"
5758
5759//===----------------------------------------------------------------------===//
5760// Operations for querying language of a cursor.
5761//===----------------------------------------------------------------------===//
5762
5763static CXLanguageKind getDeclLanguage(const Decl *D) {
5764 if (!D)
5765 return CXLanguage_C;
5766
5767 switch (D->getKind()) {
5768 default:
5769 break;
5770 case Decl::ImplicitParam:
5771 case Decl::ObjCAtDefsField:
5772 case Decl::ObjCCategory:
5773 case Decl::ObjCCategoryImpl:
5774 case Decl::ObjCCompatibleAlias:
5775 case Decl::ObjCImplementation:
5776 case Decl::ObjCInterface:
5777 case Decl::ObjCIvar:
5778 case Decl::ObjCMethod:
5779 case Decl::ObjCProperty:
5780 case Decl::ObjCPropertyImpl:
5781 case Decl::ObjCProtocol:
5782 return CXLanguage_ObjC;
5783 case Decl::CXXConstructor:
5784 case Decl::CXXConversion:
5785 case Decl::CXXDestructor:
5786 case Decl::CXXMethod:
5787 case Decl::CXXRecord:
5788 case Decl::ClassTemplate:
5789 case Decl::ClassTemplatePartialSpecialization:
5790 case Decl::ClassTemplateSpecialization:
5791 case Decl::Friend:
5792 case Decl::FriendTemplate:
5793 case Decl::FunctionTemplate:
5794 case Decl::LinkageSpec:
5795 case Decl::Namespace:
5796 case Decl::NamespaceAlias:
5797 case Decl::NonTypeTemplateParm:
5798 case Decl::StaticAssert:
5799 case Decl::TemplateTemplateParm:
5800 case Decl::TemplateTypeParm:
5801 case Decl::UnresolvedUsingTypename:
5802 case Decl::UnresolvedUsingValue:
5803 case Decl::Using:
5804 case Decl::UsingDirective:
5805 case Decl::UsingShadow:
5806 return CXLanguage_CPlusPlus;
5807 }
5808
5809 return CXLanguage_C;
5810}
5811
5812extern "C" {
5813
5814enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
5815 if (clang_isDeclaration(cursor.kind))
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005816 if (const Decl *D = cxcursor::getCursorDecl(cursor)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005817 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())
5818 return CXAvailability_Available;
5819
5820 switch (D->getAvailability()) {
5821 case AR_Available:
5822 case AR_NotYetIntroduced:
5823 return CXAvailability_Available;
5824
5825 case AR_Deprecated:
5826 return CXAvailability_Deprecated;
5827
5828 case AR_Unavailable:
5829 return CXAvailability_NotAvailable;
5830 }
5831 }
5832
5833 return CXAvailability_Available;
5834}
5835
5836static CXVersion convertVersion(VersionTuple In) {
5837 CXVersion Out = { -1, -1, -1 };
5838 if (In.empty())
5839 return Out;
5840
5841 Out.Major = In.getMajor();
5842
NAKAMURA Takumi4a3012d2013-02-21 02:32:34 +00005843 Optional<unsigned> Minor = In.getMinor();
5844 if (Minor.hasValue())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005845 Out.Minor = *Minor;
5846 else
5847 return Out;
5848
NAKAMURA Takumi4a3012d2013-02-21 02:32:34 +00005849 Optional<unsigned> Subminor = In.getSubminor();
5850 if (Subminor.hasValue())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005851 Out.Subminor = *Subminor;
5852
5853 return Out;
5854}
5855
5856int clang_getCursorPlatformAvailability(CXCursor cursor,
5857 int *always_deprecated,
5858 CXString *deprecated_message,
5859 int *always_unavailable,
5860 CXString *unavailable_message,
5861 CXPlatformAvailability *availability,
5862 int availability_size) {
5863 if (always_deprecated)
5864 *always_deprecated = 0;
5865 if (deprecated_message)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00005866 *deprecated_message = cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005867 if (always_unavailable)
5868 *always_unavailable = 0;
5869 if (unavailable_message)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00005870 *unavailable_message = cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005871
5872 if (!clang_isDeclaration(cursor.kind))
5873 return 0;
5874
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005875 const Decl *D = cxcursor::getCursorDecl(cursor);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005876 if (!D)
5877 return 0;
5878
5879 int N = 0;
5880 for (Decl::attr_iterator A = D->attr_begin(), AEnd = D->attr_end(); A != AEnd;
5881 ++A) {
5882 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
5883 if (always_deprecated)
5884 *always_deprecated = 1;
5885 if (deprecated_message)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00005886 *deprecated_message = cxstring::createDup(Deprecated->getMessage());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005887 continue;
5888 }
5889
5890 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
5891 if (always_unavailable)
5892 *always_unavailable = 1;
5893 if (unavailable_message) {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00005894 *unavailable_message = cxstring::createDup(Unavailable->getMessage());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005895 }
5896 continue;
5897 }
5898
5899 if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(*A)) {
5900 if (N < availability_size) {
5901 availability[N].Platform
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00005902 = cxstring::createDup(Avail->getPlatform()->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005903 availability[N].Introduced = convertVersion(Avail->getIntroduced());
5904 availability[N].Deprecated = convertVersion(Avail->getDeprecated());
5905 availability[N].Obsoleted = convertVersion(Avail->getObsoleted());
5906 availability[N].Unavailable = Avail->getUnavailable();
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00005907 availability[N].Message = cxstring::createDup(Avail->getMessage());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005908 }
5909 ++N;
5910 }
5911 }
5912
5913 return N;
5914}
5915
5916void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) {
5917 clang_disposeString(availability->Platform);
5918 clang_disposeString(availability->Message);
5919}
5920
5921CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
5922 if (clang_isDeclaration(cursor.kind))
5923 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
5924
5925 return CXLanguage_Invalid;
5926}
5927
5928 /// \brief If the given cursor is the "templated" declaration
5929 /// descibing a class or function template, return the class or
5930 /// function template.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005931static const Decl *maybeGetTemplateCursor(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005932 if (!D)
5933 return 0;
5934
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005935 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005936 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
5937 return FunTmpl;
5938
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005939 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005940 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
5941 return ClassTmpl;
5942
5943 return D;
5944}
5945
5946CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
5947 if (clang_isDeclaration(cursor.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005948 if (const Decl *D = getCursorDecl(cursor)) {
5949 const DeclContext *DC = D->getDeclContext();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005950 if (!DC)
5951 return clang_getNullCursor();
5952
5953 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
5954 getCursorTU(cursor));
5955 }
5956 }
5957
5958 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005959 if (const Decl *D = getCursorDecl(cursor))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005960 return MakeCXCursor(D, getCursorTU(cursor));
5961 }
5962
5963 return clang_getNullCursor();
5964}
5965
5966CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
5967 if (clang_isDeclaration(cursor.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00005968 if (const Decl *D = getCursorDecl(cursor)) {
5969 const DeclContext *DC = D->getLexicalDeclContext();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005970 if (!DC)
5971 return clang_getNullCursor();
5972
5973 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
5974 getCursorTU(cursor));
5975 }
5976 }
5977
5978 // FIXME: Note that we can't easily compute the lexical context of a
5979 // statement or expression, so we return nothing.
5980 return clang_getNullCursor();
5981}
5982
5983CXFile clang_getIncludedFile(CXCursor cursor) {
5984 if (cursor.kind != CXCursor_InclusionDirective)
5985 return 0;
5986
Dmitri Gribenko67812b22013-01-11 21:01:49 +00005987 const InclusionDirective *ID = getCursorInclusionDirective(cursor);
Dmitri Gribenkoe4ea8792013-01-23 15:56:07 +00005988 return const_cast<FileEntry *>(ID->getFile());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005989}
5990
Argyrios Kyrtzidis9ee6a662013-04-18 22:15:49 +00005991unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
5992 if (C.kind != CXCursor_ObjCPropertyDecl)
5993 return CXObjCPropertyAttr_noattr;
5994
5995 unsigned Result = CXObjCPropertyAttr_noattr;
5996 const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C));
5997 ObjCPropertyDecl::PropertyAttributeKind Attr =
5998 PD->getPropertyAttributesAsWritten();
5999
6000#define SET_CXOBJCPROP_ATTR(A) \
6001 if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \
6002 Result |= CXObjCPropertyAttr_##A
6003 SET_CXOBJCPROP_ATTR(readonly);
6004 SET_CXOBJCPROP_ATTR(getter);
6005 SET_CXOBJCPROP_ATTR(assign);
6006 SET_CXOBJCPROP_ATTR(readwrite);
6007 SET_CXOBJCPROP_ATTR(retain);
6008 SET_CXOBJCPROP_ATTR(copy);
6009 SET_CXOBJCPROP_ATTR(nonatomic);
6010 SET_CXOBJCPROP_ATTR(setter);
6011 SET_CXOBJCPROP_ATTR(atomic);
6012 SET_CXOBJCPROP_ATTR(weak);
6013 SET_CXOBJCPROP_ATTR(strong);
6014 SET_CXOBJCPROP_ATTR(unsafe_unretained);
6015#undef SET_CXOBJCPROP_ATTR
6016
6017 return Result;
6018}
6019
Argyrios Kyrtzidis38dbad22013-04-18 23:29:12 +00006020unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) {
6021 if (!clang_isDeclaration(C.kind))
6022 return CXObjCDeclQualifier_None;
6023
6024 Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None;
6025 const Decl *D = getCursorDecl(C);
6026 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6027 QT = MD->getObjCDeclQualifier();
6028 else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
6029 QT = PD->getObjCDeclQualifier();
6030 if (QT == Decl::OBJC_TQ_None)
6031 return CXObjCDeclQualifier_None;
6032
6033 unsigned Result = CXObjCDeclQualifier_None;
6034 if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In;
6035 if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout;
6036 if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out;
6037 if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy;
6038 if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref;
6039 if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway;
6040
6041 return Result;
6042}
6043
Argyrios Kyrtzidis514afc72013-07-05 20:44:37 +00006044unsigned clang_Cursor_isObjCOptional(CXCursor C) {
6045 if (!clang_isDeclaration(C.kind))
6046 return 0;
6047
6048 const Decl *D = getCursorDecl(C);
6049 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
6050 return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional;
6051 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6052 return MD->getImplementationControl() == ObjCMethodDecl::Optional;
6053
6054 return 0;
6055}
6056
Argyrios Kyrtzidis80e1aca2013-04-18 23:53:05 +00006057unsigned clang_Cursor_isVariadic(CXCursor C) {
6058 if (!clang_isDeclaration(C.kind))
6059 return 0;
6060
6061 const Decl *D = getCursorDecl(C);
6062 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
6063 return FD->isVariadic();
6064 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6065 return MD->isVariadic();
6066
6067 return 0;
6068}
6069
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006070CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
6071 if (!clang_isDeclaration(C.kind))
6072 return clang_getNullRange();
6073
6074 const Decl *D = getCursorDecl(C);
6075 ASTContext &Context = getCursorContext(C);
6076 const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6077 if (!RC)
6078 return clang_getNullRange();
6079
6080 return cxloc::translateSourceRange(Context, RC->getSourceRange());
6081}
6082
6083CXString clang_Cursor_getRawCommentText(CXCursor C) {
6084 if (!clang_isDeclaration(C.kind))
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +00006085 return cxstring::createNull();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006086
6087 const Decl *D = getCursorDecl(C);
6088 ASTContext &Context = getCursorContext(C);
6089 const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6090 StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) :
6091 StringRef();
6092
6093 // Don't duplicate the string because RawText points directly into source
6094 // code.
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00006095 return cxstring::createRef(RawText);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006096}
6097
6098CXString clang_Cursor_getBriefCommentText(CXCursor C) {
6099 if (!clang_isDeclaration(C.kind))
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +00006100 return cxstring::createNull();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006101
6102 const Decl *D = getCursorDecl(C);
6103 const ASTContext &Context = getCursorContext(C);
6104 const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6105
6106 if (RC) {
6107 StringRef BriefText = RC->getBriefText(Context);
6108
6109 // Don't duplicate the string because RawComment ensures that this memory
6110 // will not go away.
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00006111 return cxstring::createRef(BriefText);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006112 }
6113
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +00006114 return cxstring::createNull();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006115}
6116
6117CXComment clang_Cursor_getParsedComment(CXCursor C) {
6118 if (!clang_isDeclaration(C.kind))
6119 return cxcomment::createCXComment(NULL, NULL);
6120
6121 const Decl *D = getCursorDecl(C);
6122 const ASTContext &Context = getCursorContext(C);
6123 const comments::FullComment *FC = Context.getCommentForDecl(D, /*PP=*/ NULL);
6124
6125 return cxcomment::createCXComment(FC, getCursorTU(C));
6126}
6127
6128CXModule clang_Cursor_getModule(CXCursor C) {
6129 if (C.kind == CXCursor_ModuleImportDecl) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00006130 if (const ImportDecl *ImportD =
6131 dyn_cast_or_null<ImportDecl>(getCursorDecl(C)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006132 return ImportD->getImportedModule();
6133 }
6134
6135 return 0;
6136}
6137
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00006138CXFile clang_Module_getASTFile(CXModule CXMod) {
6139 if (!CXMod)
6140 return 0;
6141 Module *Mod = static_cast<Module*>(CXMod);
6142 return const_cast<FileEntry *>(Mod->getASTFile());
6143}
6144
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006145CXModule clang_Module_getParent(CXModule CXMod) {
6146 if (!CXMod)
6147 return 0;
6148 Module *Mod = static_cast<Module*>(CXMod);
6149 return Mod->Parent;
6150}
6151
6152CXString clang_Module_getName(CXModule CXMod) {
6153 if (!CXMod)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00006154 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006155 Module *Mod = static_cast<Module*>(CXMod);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00006156 return cxstring::createDup(Mod->Name);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006157}
6158
6159CXString clang_Module_getFullName(CXModule CXMod) {
6160 if (!CXMod)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +00006161 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006162 Module *Mod = static_cast<Module*>(CXMod);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00006163 return cxstring::createDup(Mod->getFullModuleName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006164}
6165
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00006166unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
6167 CXModule CXMod) {
6168 if (!TU || !CXMod)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006169 return 0;
6170 Module *Mod = static_cast<Module*>(CXMod);
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00006171 FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
6172 ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
6173 return TopHeaders.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006174}
6175
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00006176CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
6177 CXModule CXMod, unsigned Index) {
6178 if (!TU || !CXMod)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006179 return 0;
6180 Module *Mod = static_cast<Module*>(CXMod);
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00006181 FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006182
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00006183 ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
6184 if (Index < TopHeaders.size())
6185 return const_cast<FileEntry *>(TopHeaders[Index]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006186
6187 return 0;
6188}
6189
6190} // end: extern "C"
6191
6192//===----------------------------------------------------------------------===//
6193// C++ AST instrospection.
6194//===----------------------------------------------------------------------===//
6195
6196extern "C" {
Dmitri Gribenkoc965f762013-05-17 18:38:35 +00006197unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
6198 if (!clang_isDeclaration(C.kind))
6199 return 0;
6200
6201 const CXXMethodDecl *Method = 0;
6202 const Decl *D = cxcursor::getCursorDecl(C);
6203 if (const FunctionTemplateDecl *FunTmpl =
6204 dyn_cast_or_null<FunctionTemplateDecl>(D))
6205 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
6206 else
6207 Method = dyn_cast_or_null<CXXMethodDecl>(D);
6208 return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0;
6209}
6210
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006211unsigned clang_CXXMethod_isStatic(CXCursor C) {
6212 if (!clang_isDeclaration(C.kind))
6213 return 0;
6214
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00006215 const CXXMethodDecl *Method = 0;
6216 const Decl *D = cxcursor::getCursorDecl(C);
6217 if (const FunctionTemplateDecl *FunTmpl =
6218 dyn_cast_or_null<FunctionTemplateDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006219 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
6220 else
6221 Method = dyn_cast_or_null<CXXMethodDecl>(D);
6222 return (Method && Method->isStatic()) ? 1 : 0;
6223}
6224
6225unsigned clang_CXXMethod_isVirtual(CXCursor C) {
6226 if (!clang_isDeclaration(C.kind))
6227 return 0;
6228
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00006229 const CXXMethodDecl *Method = 0;
6230 const Decl *D = cxcursor::getCursorDecl(C);
6231 if (const FunctionTemplateDecl *FunTmpl =
6232 dyn_cast_or_null<FunctionTemplateDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006233 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
6234 else
6235 Method = dyn_cast_or_null<CXXMethodDecl>(D);
6236 return (Method && Method->isVirtual()) ? 1 : 0;
6237}
6238} // end: extern "C"
6239
6240//===----------------------------------------------------------------------===//
6241// Attribute introspection.
6242//===----------------------------------------------------------------------===//
6243
6244extern "C" {
6245CXType clang_getIBOutletCollectionType(CXCursor C) {
6246 if (C.kind != CXCursor_IBOutletCollectionAttr)
6247 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
6248
Dmitri Gribenko7d914382013-01-26 18:08:08 +00006249 const IBOutletCollectionAttr *A =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006250 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
6251
6252 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
6253}
6254} // end: extern "C"
6255
6256//===----------------------------------------------------------------------===//
6257// Inspecting memory usage.
6258//===----------------------------------------------------------------------===//
6259
6260typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
6261
6262static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
6263 enum CXTUResourceUsageKind k,
6264 unsigned long amount) {
6265 CXTUResourceUsageEntry entry = { k, amount };
6266 entries.push_back(entry);
6267}
6268
6269extern "C" {
6270
6271const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
6272 const char *str = "";
6273 switch (kind) {
6274 case CXTUResourceUsage_AST:
6275 str = "ASTContext: expressions, declarations, and types";
6276 break;
6277 case CXTUResourceUsage_Identifiers:
6278 str = "ASTContext: identifiers";
6279 break;
6280 case CXTUResourceUsage_Selectors:
6281 str = "ASTContext: selectors";
6282 break;
6283 case CXTUResourceUsage_GlobalCompletionResults:
6284 str = "Code completion: cached global results";
6285 break;
6286 case CXTUResourceUsage_SourceManagerContentCache:
6287 str = "SourceManager: content cache allocator";
6288 break;
6289 case CXTUResourceUsage_AST_SideTables:
6290 str = "ASTContext: side tables";
6291 break;
6292 case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
6293 str = "SourceManager: malloc'ed memory buffers";
6294 break;
6295 case CXTUResourceUsage_SourceManager_Membuffer_MMap:
6296 str = "SourceManager: mmap'ed memory buffers";
6297 break;
6298 case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
6299 str = "ExternalASTSource: malloc'ed memory buffers";
6300 break;
6301 case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
6302 str = "ExternalASTSource: mmap'ed memory buffers";
6303 break;
6304 case CXTUResourceUsage_Preprocessor:
6305 str = "Preprocessor: malloc'ed memory";
6306 break;
6307 case CXTUResourceUsage_PreprocessingRecord:
6308 str = "Preprocessor: PreprocessingRecord";
6309 break;
6310 case CXTUResourceUsage_SourceManager_DataStructures:
6311 str = "SourceManager: data structures and tables";
6312 break;
6313 case CXTUResourceUsage_Preprocessor_HeaderSearch:
6314 str = "Preprocessor: header search tables";
6315 break;
6316 }
6317 return str;
6318}
6319
6320CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
6321 if (!TU) {
6322 CXTUResourceUsage usage = { (void*) 0, 0, 0 };
6323 return usage;
6324 }
6325
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00006326 ASTUnit *astUnit = cxtu::getASTUnit(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006327 OwningPtr<MemUsageEntries> entries(new MemUsageEntries());
6328 ASTContext &astContext = astUnit->getASTContext();
6329
6330 // How much memory is used by AST nodes and types?
6331 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST,
6332 (unsigned long) astContext.getASTAllocatedMemory());
6333
6334 // How much memory is used by identifiers?
6335 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers,
6336 (unsigned long) astContext.Idents.getAllocator().getTotalMemory());
6337
6338 // How much memory is used for selectors?
6339 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors,
6340 (unsigned long) astContext.Selectors.getTotalMemory());
6341
6342 // How much memory is used by ASTContext's side tables?
6343 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables,
6344 (unsigned long) astContext.getSideTableAllocatedMemory());
6345
6346 // How much memory is used for caching global code completion results?
6347 unsigned long completionBytes = 0;
6348 if (GlobalCodeCompletionAllocator *completionAllocator =
6349 astUnit->getCachedCompletionAllocator().getPtr()) {
6350 completionBytes = completionAllocator->getTotalMemory();
6351 }
6352 createCXTUResourceUsageEntry(*entries,
6353 CXTUResourceUsage_GlobalCompletionResults,
6354 completionBytes);
6355
6356 // How much memory is being used by SourceManager's content cache?
6357 createCXTUResourceUsageEntry(*entries,
6358 CXTUResourceUsage_SourceManagerContentCache,
6359 (unsigned long) astContext.getSourceManager().getContentCacheSize());
6360
6361 // How much memory is being used by the MemoryBuffer's in SourceManager?
6362 const SourceManager::MemoryBufferSizes &srcBufs =
6363 astUnit->getSourceManager().getMemoryBufferSizes();
6364
6365 createCXTUResourceUsageEntry(*entries,
6366 CXTUResourceUsage_SourceManager_Membuffer_Malloc,
6367 (unsigned long) srcBufs.malloc_bytes);
6368 createCXTUResourceUsageEntry(*entries,
6369 CXTUResourceUsage_SourceManager_Membuffer_MMap,
6370 (unsigned long) srcBufs.mmap_bytes);
6371 createCXTUResourceUsageEntry(*entries,
6372 CXTUResourceUsage_SourceManager_DataStructures,
6373 (unsigned long) astContext.getSourceManager()
6374 .getDataStructureSizes());
6375
6376 // How much memory is being used by the ExternalASTSource?
6377 if (ExternalASTSource *esrc = astContext.getExternalSource()) {
6378 const ExternalASTSource::MemoryBufferSizes &sizes =
6379 esrc->getMemoryBufferSizes();
6380
6381 createCXTUResourceUsageEntry(*entries,
6382 CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc,
6383 (unsigned long) sizes.malloc_bytes);
6384 createCXTUResourceUsageEntry(*entries,
6385 CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
6386 (unsigned long) sizes.mmap_bytes);
6387 }
6388
6389 // How much memory is being used by the Preprocessor?
6390 Preprocessor &pp = astUnit->getPreprocessor();
6391 createCXTUResourceUsageEntry(*entries,
6392 CXTUResourceUsage_Preprocessor,
6393 pp.getTotalMemory());
6394
6395 if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
6396 createCXTUResourceUsageEntry(*entries,
6397 CXTUResourceUsage_PreprocessingRecord,
6398 pRec->getTotalMemory());
6399 }
6400
6401 createCXTUResourceUsageEntry(*entries,
6402 CXTUResourceUsage_Preprocessor_HeaderSearch,
6403 pp.getHeaderSearchInfo().getTotalMemory());
6404
6405 CXTUResourceUsage usage = { (void*) entries.get(),
6406 (unsigned) entries->size(),
6407 entries->size() ? &(*entries)[0] : 0 };
6408 entries.take();
6409 return usage;
6410}
6411
6412void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
6413 if (usage.data)
6414 delete (MemUsageEntries*) usage.data;
6415}
6416
6417} // end extern "C"
6418
6419void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {
6420 CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU);
6421 for (unsigned I = 0; I != Usage.numEntries; ++I)
6422 fprintf(stderr, " %s: %lu\n",
6423 clang_getTUResourceUsageName(Usage.entries[I].kind),
6424 Usage.entries[I].amount);
6425
6426 clang_disposeCXTUResourceUsage(Usage);
6427}
6428
6429//===----------------------------------------------------------------------===//
6430// Misc. utility functions.
6431//===----------------------------------------------------------------------===//
6432
6433/// Default to using an 8 MB stack size on "safety" threads.
6434static unsigned SafetyStackThreadSize = 8 << 20;
6435
6436namespace clang {
6437
6438bool RunSafely(llvm::CrashRecoveryContext &CRC,
6439 void (*Fn)(void*), void *UserData,
6440 unsigned Size) {
6441 if (!Size)
6442 Size = GetSafetyThreadStackSize();
6443 if (Size)
6444 return CRC.RunSafelyOnThread(Fn, UserData, Size);
6445 return CRC.RunSafely(Fn, UserData);
6446}
6447
6448unsigned GetSafetyThreadStackSize() {
6449 return SafetyStackThreadSize;
6450}
6451
6452void SetSafetyThreadStackSize(unsigned Value) {
6453 SafetyStackThreadSize = Value;
6454}
6455
6456}
6457
6458void clang::setThreadBackgroundPriority() {
6459 if (getenv("LIBCLANG_BGPRIO_DISABLE"))
6460 return;
6461
6462 // FIXME: Move to llvm/Support and make it cross-platform.
6463#ifdef __APPLE__
6464 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
6465#endif
6466}
6467
6468void cxindex::printDiagsToStderr(ASTUnit *Unit) {
6469 if (!Unit)
6470 return;
6471
6472 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
6473 DEnd = Unit->stored_diag_end();
6474 D != DEnd; ++D) {
6475 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts());
6476 CXString Msg = clang_formatDiagnostic(&Diag,
6477 clang_defaultDiagnosticDisplayOptions());
6478 fprintf(stderr, "%s\n", clang_getCString(Msg));
6479 clang_disposeString(Msg);
6480 }
6481#ifdef LLVM_ON_WIN32
6482 // On Windows, force a flush, since there may be multiple copies of
6483 // stderr and stdout in the file system, all with different buffers
6484 // but writing to the same device.
6485 fflush(stderr);
6486#endif
6487}
6488
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006489MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II,
6490 SourceLocation MacroDefLoc,
6491 CXTranslationUnit TU){
6492 if (MacroDefLoc.isInvalid() || !TU)
6493 return 0;
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006494 if (!II.hadMacroDefinition())
6495 return 0;
6496
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00006497 ASTUnit *Unit = cxtu::getASTUnit(TU);
Argyrios Kyrtzidisc059f892013-01-07 19:16:30 +00006498 Preprocessor &PP = Unit->getPreprocessor();
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00006499 MacroDirective *MD = PP.getMacroDirectiveHistory(&II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00006500 if (MD) {
6501 for (MacroDirective::DefInfo
6502 Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) {
6503 if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc())
6504 return Def.getMacroInfo();
6505 }
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006506 }
6507
6508 return 0;
6509}
6510
Dmitri Gribenko67812b22013-01-11 21:01:49 +00006511const MacroInfo *cxindex::getMacroInfo(const MacroDefinition *MacroDef,
6512 CXTranslationUnit TU) {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006513 if (!MacroDef || !TU)
6514 return 0;
6515 const IdentifierInfo *II = MacroDef->getName();
6516 if (!II)
6517 return 0;
6518
6519 return getMacroInfo(*II, MacroDef->getLocation(), TU);
6520}
6521
6522MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
6523 const Token &Tok,
6524 CXTranslationUnit TU) {
6525 if (!MI || !TU)
6526 return 0;
6527 if (Tok.isNot(tok::raw_identifier))
6528 return 0;
6529
6530 if (MI->getNumTokens() == 0)
6531 return 0;
6532 SourceRange DefRange(MI->getReplacementToken(0).getLocation(),
6533 MI->getDefinitionEndLoc());
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00006534 ASTUnit *Unit = cxtu::getASTUnit(TU);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006535
6536 // Check that the token is inside the definition and not its argument list.
6537 SourceManager &SM = Unit->getSourceManager();
6538 if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin()))
6539 return 0;
6540 if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation()))
6541 return 0;
6542
6543 Preprocessor &PP = Unit->getPreprocessor();
6544 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
6545 if (!PPRec)
6546 return 0;
6547
6548 StringRef Name(Tok.getRawIdentifierData(), Tok.getLength());
6549 IdentifierInfo &II = PP.getIdentifierTable().get(Name);
6550 if (!II.hadMacroDefinition())
6551 return 0;
6552
6553 // Check that the identifier is not one of the macro arguments.
6554 if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end())
6555 return 0;
6556
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00006557 MacroDirective *InnerMD = PP.getMacroDirectiveHistory(&II);
6558 if (!InnerMD)
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006559 return 0;
6560
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00006561 return PPRec->findMacroDefinition(InnerMD->getMacroInfo());
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006562}
6563
6564MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
6565 SourceLocation Loc,
6566 CXTranslationUnit TU) {
6567 if (Loc.isInvalid() || !MI || !TU)
6568 return 0;
6569
6570 if (MI->getNumTokens() == 0)
6571 return 0;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00006572 ASTUnit *Unit = cxtu::getASTUnit(TU);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +00006573 Preprocessor &PP = Unit->getPreprocessor();
6574 if (!PP.getPreprocessingRecord())
6575 return 0;
6576 Loc = Unit->getSourceManager().getSpellingLoc(Loc);
6577 Token Tok;
6578 if (PP.getRawToken(Loc, Tok))
6579 return 0;
6580
6581 return checkForMacroInMacroDefinition(MI, Tok, TU);
6582}
6583
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006584extern "C" {
6585
6586CXString clang_getClangVersion() {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00006587 return cxstring::createDup(getClangFullVersion());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006588}
6589
6590} // end: extern "C"
6591
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00006592Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) {
6593 if (TU) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00006594 if (ASTUnit *Unit = cxtu::getASTUnit(TU)) {
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00006595 LogOS << '<' << Unit->getMainFileName() << '>';
Argyrios Kyrtzidis44f65a52013-03-05 20:21:14 +00006596 if (Unit->isMainFileAST())
6597 LogOS << " (" << Unit->getASTFileName() << ')';
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00006598 return *this;
6599 }
6600 }
6601
6602 LogOS << "<NULL TU>";
6603 return *this;
6604}
6605
Argyrios Kyrtzidisb70e7a82013-03-08 02:32:26 +00006606Logger &cxindex::Logger::operator<<(const FileEntry *FE) {
6607 *this << FE->getName();
6608 return *this;
6609}
6610
6611Logger &cxindex::Logger::operator<<(CXCursor cursor) {
6612 CXString cursorName = clang_getCursorDisplayName(cursor);
6613 *this << cursorName << "@" << clang_getCursorLocation(cursor);
6614 clang_disposeString(cursorName);
6615 return *this;
6616}
6617
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00006618Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) {
6619 CXFile File;
6620 unsigned Line, Column;
6621 clang_getFileLocation(Loc, &File, &Line, &Column, 0);
6622 CXString FileName = clang_getFileName(File);
6623 *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column);
6624 clang_disposeString(FileName);
6625 return *this;
6626}
6627
6628Logger &cxindex::Logger::operator<<(CXSourceRange range) {
6629 CXSourceLocation BLoc = clang_getRangeStart(range);
6630 CXSourceLocation ELoc = clang_getRangeEnd(range);
6631
6632 CXFile BFile;
6633 unsigned BLine, BColumn;
6634 clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, 0);
6635
6636 CXFile EFile;
6637 unsigned ELine, EColumn;
6638 clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, 0);
6639
6640 CXString BFileName = clang_getFileName(BFile);
6641 if (BFile == EFile) {
6642 *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName),
6643 BLine, BColumn, ELine, EColumn);
6644 } else {
6645 CXString EFileName = clang_getFileName(EFile);
6646 *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName),
6647 BLine, BColumn)
6648 << llvm::format("%s:%d:%d]", clang_getCString(EFileName),
6649 ELine, EColumn);
6650 clang_disposeString(EFileName);
6651 }
6652 clang_disposeString(BFileName);
6653 return *this;
6654}
6655
6656Logger &cxindex::Logger::operator<<(CXString Str) {
6657 *this << clang_getCString(Str);
6658 return *this;
6659}
6660
6661Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) {
6662 LogOS << Fmt;
6663 return *this;
6664}
6665
6666cxindex::Logger::~Logger() {
6667 LogOS.flush();
6668
6669 llvm::sys::ScopedLock L(EnableMultithreadingMutex);
6670
6671 static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();
6672
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006673 raw_ostream &OS = llvm::errs();
Argyrios Kyrtzidisc6f5c6a2013-01-10 18:54:52 +00006674 OS << "[libclang:" << Name << ':';
6675
6676 // FIXME: Portability.
6677#if HAVE_PTHREAD_H && __APPLE__
6678 mach_port_t tid = pthread_mach_thread_np(pthread_self());
6679 OS << tid << ':';
6680#endif
6681
6682 llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime();
6683 OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime());
6684 OS << Msg.str() << '\n';
6685
6686 if (Trace) {
6687 llvm::sys::PrintStackTrace(stderr);
6688 OS << "--------------------------------------------------\n";
6689 }
6690}