blob: 8a7dd4d7ea29d648657a68622bdf84e9f65b49a5 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +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.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenekab188932010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekd2fa5662009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "CIndexer.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremenek0a90d322010-11-17 23:24:11 +000017#include "CXTranslationUnit.h"
Ted Kremeneked122732010-11-16 01:56:27 +000018#include "CXString.h"
Ted Kremenek95f33552010-08-26 01:42:22 +000019#include "CXType.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000020#include "CXSourceLocation.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000021#include "CIndexDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000022
Ted Kremenek04bb7162010-01-22 22:44:15 +000023#include "clang/Basic/Version.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000024
Steve Naroff50398192009-08-28 15:28:48 +000025#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000026#include "clang/AST/StmtVisitor.h"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000027#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000028#include "clang/Basic/Diagnostic.h"
29#include "clang/Frontend/ASTUnit.h"
30#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000031#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000032#include "clang/Lex/Lexer.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000033#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000034#include "clang/Lex/Preprocessor.h"
Douglas Gregora67e03f2010-09-09 21:42:20 +000035#include "llvm/ADT/STLExtras.h"
Ted Kremenekd8c370c2010-11-02 23:10:24 +000036#include "llvm/ADT/Optional.h"
37#include "clang/Analysis/Support/SaveAndRestore.h"
Daniel Dunbarc7df4f32010-08-18 18:43:14 +000038#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar48615ff2010-10-08 19:30:33 +000039#include "llvm/Support/PrettyStackTrace.h"
Douglas Gregor02465752009-10-16 21:24:31 +000040#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor358559d2010-10-02 22:49:11 +000041#include "llvm/Support/raw_ostream.h"
Douglas Gregor7a07fcb2010-08-09 21:00:09 +000042#include "llvm/Support/Timer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000043#include "llvm/Support/Mutex.h"
44#include "llvm/Support/Program.h"
45#include "llvm/Support/Signals.h"
46#include "llvm/Support/Threading.h"
Ted Kremenek37f1ea02010-11-15 23:11:54 +000047#include "llvm/Support/Compiler.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000048
Steve Naroff50398192009-08-28 15:28:48 +000049using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000050using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000051using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000052
Ted Kremeneka60ed472010-11-16 08:15:36 +000053static CXTranslationUnit MakeCXTranslationUnit(ASTUnit *TU) {
54 if (!TU)
55 return 0;
56 CXTranslationUnit D = new CXTranslationUnitImpl();
57 D->TUData = TU;
58 D->StringPool = createCXStringPool();
59 return D;
60}
61
Douglas Gregor33e9abd2010-01-22 19:49:59 +000062/// \brief The result of comparing two source ranges.
63enum RangeComparisonResult {
64 /// \brief Either the ranges overlap or one of the ranges is invalid.
65 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000066
Douglas Gregor33e9abd2010-01-22 19:49:59 +000067 /// \brief The first range ends before the second range starts.
68 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000069
Douglas Gregor33e9abd2010-01-22 19:49:59 +000070 /// \brief The first range starts after the second range ends.
71 RangeAfter
72};
73
Ted Kremenekf0e23e82010-02-17 00:41:40 +000074/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +000075/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +000076static RangeComparisonResult RangeCompare(SourceManager &SM,
77 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +000078 SourceRange R2) {
79 assert(R1.isValid() && "First range is invalid?");
80 assert(R2.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000081 if (R1.getEnd() != R2.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000082 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000083 return RangeBefore;
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000084 if (R2.getEnd() != R1.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000085 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000086 return RangeAfter;
87 return RangeOverlap;
88}
89
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000090/// \brief Determine if a source location falls within, before, or after a
91/// a given source range.
92static RangeComparisonResult LocationCompare(SourceManager &SM,
93 SourceLocation L, SourceRange R) {
94 assert(R.isValid() && "First range is invalid?");
95 assert(L.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000096 if (L == R.getBegin() || L == R.getEnd())
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000097 return RangeOverlap;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000098 if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
99 return RangeBefore;
100 if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
101 return RangeAfter;
102 return RangeOverlap;
103}
104
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000105/// \brief Translate a Clang source range into a CIndex source range.
106///
107/// Clang internally represents ranges where the end location points to the
108/// start of the token at the end. However, for external clients it is more
109/// useful to have a CXSourceRange be a proper half-open interval. This routine
110/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000111CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000112 const LangOptions &LangOpts,
Chris Lattner0a76aae2010-06-18 22:45:06 +0000113 const CharSourceRange &R) {
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000114 // We want the last character in this location, so we will adjust the
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000115 // location accordingly.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000116 SourceLocation EndLoc = R.getEnd();
Douglas Gregora9b06d42010-11-09 06:24:54 +0000117 if (EndLoc.isValid() && EndLoc.isMacroID())
118 EndLoc = SM.getSpellingLoc(EndLoc);
Chris Lattner0a76aae2010-06-18 22:45:06 +0000119 if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000120 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000121 EndLoc = EndLoc.getFileLocWithOffset(Length);
122 }
123
124 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
125 R.getBegin().getRawEncoding(),
126 EndLoc.getRawEncoding() };
127 return Result;
128}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000129
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000130//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000131// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000132//===----------------------------------------------------------------------===//
133
Steve Naroff89922f82009-08-31 00:59:03 +0000134namespace {
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000135
136class VisitorJob {
137public:
Ted Kremenekcdb4caf2010-11-12 21:34:12 +0000138 enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind,
Ted Kremeneke4979cc2010-11-13 00:58:18 +0000139 TypeLocVisitKind, OverloadExprPartsKind,
Ted Kremenek60608ec2010-11-17 00:50:47 +0000140 DeclRefExprPartsKind, LabelRefVisitKind,
Ted Kremenekf64d8032010-11-18 00:02:32 +0000141 ExplicitTemplateArgsVisitKind,
142 NestedNameSpecifierVisitKind,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000143 NestedNameSpecifierLocVisitKind,
Ted Kremenekcdba6592010-11-18 00:42:18 +0000144 DeclarationNameInfoVisitKind,
Douglas Gregor94d96292011-01-19 20:34:17 +0000145 MemberRefVisitKind, SizeOfPackExprPartsKind };
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000146protected:
Ted Kremenekf64d8032010-11-18 00:02:32 +0000147 void *data[3];
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000148 CXCursor parent;
149 Kind K;
Ted Kremenekf64d8032010-11-18 00:02:32 +0000150 VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0)
151 : parent(C), K(k) {
152 data[0] = d1;
153 data[1] = d2;
154 data[2] = d3;
155 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000156public:
157 Kind getKind() const { return K; }
158 const CXCursor &getParent() const { return parent; }
159 static bool classof(VisitorJob *VJ) { return true; }
160};
161
162typedef llvm::SmallVector<VisitorJob, 10> VisitorWorkList;
163
Douglas Gregorb1373d02010-01-20 20:59:29 +0000164// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000165class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Ted Kremenekcdba6592010-11-18 00:42:18 +0000166 public TypeLocVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000167{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000168 /// \brief The translation unit we are traversing.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000169 CXTranslationUnit TU;
170 ASTUnit *AU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000171
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000172 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000173 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000174
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000175 /// \brief The declaration that serves at the parent of any statement or
176 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000177 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000178
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000179 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000180 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000181
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000182 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000183 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000184
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
186 // to the visitor. Declarations with a PCH level greater than this value will
187 // be suppressed.
188 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000189
190 /// \brief When valid, a source range to which the cursor should restrict
191 /// its search.
192 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000193
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000194 // FIXME: Eventually remove. This part of a hack to support proper
195 // iteration over all Decls contained lexically within an ObjC container.
196 DeclContext::decl_iterator *DI_current;
197 DeclContext::decl_iterator DE_current;
198
Ted Kremenekd1ded662010-11-15 23:31:32 +0000199 // Cache of pre-allocated worklists for data-recursion walk of Stmts.
200 llvm::SmallVector<VisitorWorkList*, 5> WorkListFreeList;
201 llvm::SmallVector<VisitorWorkList*, 5> WorkListCache;
202
Douglas Gregorb1373d02010-01-20 20:59:29 +0000203 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000204 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000205
206 /// \brief Determine whether this particular source range comes before, comes
207 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000208 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000209 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000210 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
211
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000212 class SetParentRAII {
213 CXCursor &Parent;
214 Decl *&StmtParent;
215 CXCursor OldParent;
216
217 public:
218 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
219 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
220 {
221 Parent = NewParent;
222 if (clang_isDeclaration(Parent.kind))
223 StmtParent = getCursorDecl(Parent);
224 }
225
226 ~SetParentRAII() {
227 Parent = OldParent;
228 if (clang_isDeclaration(Parent.kind))
229 StmtParent = getCursorDecl(Parent);
230 }
231 };
232
Steve Naroff89922f82009-08-31 00:59:03 +0000233public:
Ted Kremeneka60ed472010-11-16 08:15:36 +0000234 CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor,
235 CXClientData ClientData,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000236 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000237 SourceRange RegionOfInterest = SourceRange())
Ted Kremeneka60ed472010-11-16 08:15:36 +0000238 : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)),
239 Visitor(Visitor), ClientData(ClientData),
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000240 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest),
241 DI_current(0)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000242 {
243 Parent.kind = CXCursor_NoDeclFound;
244 Parent.data[0] = 0;
245 Parent.data[1] = 0;
246 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000247 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000248 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000249
Ted Kremenekd1ded662010-11-15 23:31:32 +0000250 ~CursorVisitor() {
251 // Free the pre-allocated worklists for data-recursion.
252 for (llvm::SmallVectorImpl<VisitorWorkList*>::iterator
253 I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) {
254 delete *I;
255 }
256 }
257
Ted Kremeneka60ed472010-11-16 08:15:36 +0000258 ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); }
259 CXTranslationUnit getTU() const { return TU; }
Ted Kremenekab979612010-11-11 08:05:23 +0000260
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000261 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000262
263 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
264 getPreprocessedEntities();
265
Douglas Gregorb1373d02010-01-20 20:59:29 +0000266 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000267
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000268 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000269 bool VisitAttributes(Decl *D);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000270 bool VisitBlockDecl(BlockDecl *B);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000271 bool VisitCXXRecordDecl(CXXRecordDecl *D);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000272 llvm::Optional<bool> shouldVisitCursor(CXCursor C);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000273 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000274 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
275 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000276 bool VisitTagDecl(TagDecl *D);
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000277 bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D);
Douglas Gregor74dbe642010-08-31 19:31:58 +0000278 bool VisitClassTemplatePartialSpecializationDecl(
279 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000280 bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000281 bool VisitEnumConstantDecl(EnumConstantDecl *D);
282 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
283 bool VisitFunctionDecl(FunctionDecl *ND);
284 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000285 bool VisitVarDecl(VarDecl *);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000286 bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000287 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +0000288 bool VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000289 bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000290 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
291 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
292 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
293 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
Ted Kremenek23173d72010-05-18 21:09:07 +0000294 bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD);
Ted Kremenek79758f62010-02-18 22:36:18 +0000295 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
296 bool VisitObjCImplDecl(ObjCImplDecl *D);
297 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
298 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000299 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
300 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
301 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora4ffd852010-11-17 01:03:52 +0000302 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD);
Ted Kremeneka0536d82010-05-07 01:04:29 +0000303 bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
Ted Kremenek8f06e0e2010-05-06 23:38:21 +0000304 bool VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +0000305 bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000306 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor7e242562010-09-01 19:52:22 +0000307 bool VisitUsingDecl(UsingDecl *D);
308 bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
309 bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000310
Douglas Gregor01829d32010-08-31 14:41:23 +0000311 // Name visitor
312 bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000313 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range);
Douglas Gregordc355712011-02-25 00:36:19 +0000314 bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
Douglas Gregor01829d32010-08-31 14:41:23 +0000315
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000316 // Template visitors
317 bool VisitTemplateParameters(const TemplateParameterList *Params);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000318 bool VisitTemplateName(TemplateName Name, SourceLocation Loc);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000319 bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL);
320
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000321 // Type visitors
Douglas Gregor01829d32010-08-31 14:41:23 +0000322 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000323 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000324 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000325 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
326 bool VisitTagTypeLoc(TagTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000327 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000328 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
John McCallc12c5bb2010-05-15 11:32:37 +0000329 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000330 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000331 bool VisitParenTypeLoc(ParenTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000332 bool VisitPointerTypeLoc(PointerTypeLoc TL);
333 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
334 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
335 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
336 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
Douglas Gregor01829d32010-08-31 14:41:23 +0000337 bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000338 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000339 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000340 // FIXME: Implement visitors here when the unimplemented TypeLocs get
341 // implemented
342 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000343 bool VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000344 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000345 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000346 bool VisitDependentTemplateSpecializationTypeLoc(
347 DependentTemplateSpecializationTypeLoc TL);
Douglas Gregor9e876872011-03-01 18:12:44 +0000348 bool VisitElaboratedTypeLoc(ElaboratedTypeLoc TL);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000349
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000350 // Data-recursive visitor functions.
351 bool IsInRegionOfInterest(CXCursor C);
352 bool RunVisitorWorkList(VisitorWorkList &WL);
353 void EnqueueWorkList(VisitorWorkList &WL, Stmt *S);
Ted Kremenekcdba6592010-11-18 00:42:18 +0000354 LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S);
Steve Naroff89922f82009-08-31 00:59:03 +0000355};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000356
Ted Kremenekab188932010-01-05 19:32:54 +0000357} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000358
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000359static SourceRange getRawCursorExtent(CXCursor C);
Douglas Gregor66537982010-11-17 17:14:07 +0000360static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
361
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000362
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000363RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000364 return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000365}
366
Douglas Gregorb1373d02010-01-20 20:59:29 +0000367/// \brief Visit the given cursor and, if requested by the visitor,
368/// its children.
369///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000370/// \param Cursor the cursor to visit.
371///
372/// \param CheckRegionOfInterest if true, then the caller already checked that
373/// this cursor is within the region of interest.
374///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000375/// \returns true if the visitation should be aborted, false if it
376/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000377bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000378 if (clang_isInvalid(Cursor.kind))
379 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000380
Douglas Gregorb1373d02010-01-20 20:59:29 +0000381 if (clang_isDeclaration(Cursor.kind)) {
382 Decl *D = getCursorDecl(Cursor);
383 assert(D && "Invalid declaration cursor");
384 if (D->getPCHLevel() > MaxPCHLevel)
385 return false;
386
387 if (D->isImplicit())
388 return false;
389 }
390
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000391 // If we have a range of interest, and this cursor doesn't intersect with it,
392 // we're done.
393 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000394 SourceRange Range = getRawCursorExtent(Cursor);
Daniel Dunbarf408f322010-02-14 08:32:05 +0000395 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000396 return false;
397 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000398
Douglas Gregorb1373d02010-01-20 20:59:29 +0000399 switch (Visitor(Cursor, Parent, ClientData)) {
400 case CXChildVisit_Break:
401 return true;
402
403 case CXChildVisit_Continue:
404 return false;
405
406 case CXChildVisit_Recurse:
407 return VisitChildren(Cursor);
408 }
409
Douglas Gregorfd643772010-01-25 16:45:46 +0000410 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000411}
412
Douglas Gregor788f5a12010-03-20 00:41:21 +0000413std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
414CursorVisitor::getPreprocessedEntities() {
415 PreprocessingRecord &PPRec
Ted Kremeneka60ed472010-11-16 08:15:36 +0000416 = *AU->getPreprocessor().getPreprocessingRecord();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000417
418 bool OnlyLocalDecls
Douglas Gregor32038bb2010-12-21 19:07:48 +0000419 = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
420
421 if (OnlyLocalDecls && RegionOfInterest.isValid()) {
422 // If we would only look at local declarations but we have a region of
423 // interest, check whether that region of interest is in the main file.
424 // If not, we should traverse all declarations.
425 // FIXME: My kingdom for a proper binary search approach to finding
426 // cursors!
427 std::pair<FileID, unsigned> Location
428 = AU->getSourceManager().getDecomposedInstantiationLoc(
429 RegionOfInterest.getBegin());
430 if (Location.first != AU->getSourceManager().getMainFileID())
431 OnlyLocalDecls = false;
432 }
Douglas Gregor788f5a12010-03-20 00:41:21 +0000433
Douglas Gregor89d99802010-11-30 06:16:57 +0000434 PreprocessingRecord::iterator StartEntity, EndEntity;
435 if (OnlyLocalDecls) {
436 StartEntity = AU->pp_entity_begin();
437 EndEntity = AU->pp_entity_end();
438 } else {
439 StartEntity = PPRec.begin();
440 EndEntity = PPRec.end();
441 }
442
Douglas Gregor788f5a12010-03-20 00:41:21 +0000443 // There is no region of interest; we have to walk everything.
444 if (RegionOfInterest.isInvalid())
Douglas Gregor89d99802010-11-30 06:16:57 +0000445 return std::make_pair(StartEntity, EndEntity);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000446
447 // Find the file in which the region of interest lands.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000448 SourceManager &SM = AU->getSourceManager();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000449 std::pair<FileID, unsigned> Begin
450 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
451 std::pair<FileID, unsigned> End
452 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
453
454 // The region of interest spans files; we have to walk everything.
455 if (Begin.first != End.first)
Douglas Gregor89d99802010-11-30 06:16:57 +0000456 return std::make_pair(StartEntity, EndEntity);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000457
458 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
Ted Kremeneka60ed472010-11-16 08:15:36 +0000459 = AU->getPreprocessedEntitiesByFile();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000460 if (ByFileMap.empty()) {
461 // Build the mapping from files to sets of preprocessed entities.
Douglas Gregor89d99802010-11-30 06:16:57 +0000462 for (PreprocessingRecord::iterator E = StartEntity; E != EndEntity; ++E) {
Douglas Gregor788f5a12010-03-20 00:41:21 +0000463 std::pair<FileID, unsigned> P
464 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
Douglas Gregor89d99802010-11-30 06:16:57 +0000465
Douglas Gregor788f5a12010-03-20 00:41:21 +0000466 ByFileMap[P.first].push_back(*E);
467 }
468 }
469
470 return std::make_pair(ByFileMap[Begin.first].begin(),
471 ByFileMap[Begin.first].end());
472}
473
Douglas Gregorb1373d02010-01-20 20:59:29 +0000474/// \brief Visit the children of the given cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000475///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000476/// \returns true if the visitation should be aborted, false if it
477/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000478bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000479 if (clang_isReference(Cursor.kind)) {
480 // By definition, references have no children.
481 return false;
482 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000483
484 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000485 // done.
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000486 SetParentRAII SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000487
Douglas Gregorb1373d02010-01-20 20:59:29 +0000488 if (clang_isDeclaration(Cursor.kind)) {
489 Decl *D = getCursorDecl(Cursor);
490 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000491 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000492 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000493
Douglas Gregora59e3902010-01-21 23:27:09 +0000494 if (clang_isStatement(Cursor.kind))
495 return Visit(getCursorStmt(Cursor));
496 if (clang_isExpression(Cursor.kind))
497 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000498
Douglas Gregorb1373d02010-01-20 20:59:29 +0000499 if (clang_isTranslationUnit(Cursor.kind)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000500 CXTranslationUnit tu = getCursorTU(Cursor);
501 ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000502 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
503 RegionOfInterest.isInvalid()) {
Douglas Gregoreb8837b2010-08-03 19:06:41 +0000504 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
505 TLEnd = CXXUnit->top_level_end();
506 TL != TLEnd; ++TL) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000507 if (Visit(MakeCXCursor(*TL, tu), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000508 return true;
509 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000510 } else if (VisitDeclContext(
511 CXXUnit->getASTContext().getTranslationUnitDecl()))
512 return true;
Bob Wilson3178cb62010-03-19 03:57:57 +0000513
Douglas Gregor0396f462010-03-19 05:22:59 +0000514 // Walk the preprocessing record.
Daniel Dunbar8de30ff2010-03-20 01:11:56 +0000515 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000516 // FIXME: Once we have the ability to deserialize a preprocessing record,
517 // do so.
Douglas Gregor788f5a12010-03-20 00:41:21 +0000518 PreprocessingRecord::iterator E, EEnd;
519 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000520 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000521 if (Visit(MakeMacroInstantiationCursor(MI, tu)))
Douglas Gregor0396f462010-03-19 05:22:59 +0000522 return true;
Douglas Gregor788f5a12010-03-20 00:41:21 +0000523
Douglas Gregor0396f462010-03-19 05:22:59 +0000524 continue;
525 }
526
527 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000528 if (Visit(MakeMacroDefinitionCursor(MD, tu)))
Douglas Gregor0396f462010-03-19 05:22:59 +0000529 return true;
530
531 continue;
532 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000533
534 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000535 if (Visit(MakeInclusionDirectiveCursor(ID, tu)))
Douglas Gregorecdcb882010-10-20 22:00:55 +0000536 return true;
537
538 continue;
539 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000540 }
541 }
Douglas Gregor7b691f332010-01-20 21:13:59 +0000542 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000543 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000544
Douglas Gregorb1373d02010-01-20 20:59:29 +0000545 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000546 return false;
547}
548
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000549bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
John McCallfc929202010-06-04 22:33:30 +0000550 if (Visit(B->getSignatureAsWritten()->getTypeLoc()))
551 return true;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000552
Ted Kremenek664cffd2010-07-22 11:30:19 +0000553 if (Stmt *Body = B->getBody())
554 return Visit(MakeCXCursor(Body, StmtParent, TU));
555
556 return false;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000557}
558
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000559llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
560 if (RegionOfInterest.isValid()) {
Douglas Gregor66537982010-11-17 17:14:07 +0000561 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000562 if (Range.isInvalid())
563 return llvm::Optional<bool>();
Douglas Gregor66537982010-11-17 17:14:07 +0000564
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000565 switch (CompareRegionOfInterest(Range)) {
566 case RangeBefore:
567 // This declaration comes before the region of interest; skip it.
568 return llvm::Optional<bool>();
569
570 case RangeAfter:
571 // This declaration comes after the region of interest; we're done.
572 return false;
573
574 case RangeOverlap:
575 // This declaration overlaps the region of interest; visit it.
576 break;
577 }
578 }
579 return true;
580}
581
582bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
583 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
584
585 // FIXME: Eventually remove. This part of a hack to support proper
586 // iteration over all Decls contained lexically within an ObjC container.
587 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
588 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
589
590 for ( ; I != E; ++I) {
Ted Kremenek23173d72010-05-18 21:09:07 +0000591 Decl *D = *I;
592 if (D->getLexicalDeclContext() != DC)
593 continue;
Ted Kremenek23173d72010-05-18 21:09:07 +0000594 CXCursor Cursor = MakeCXCursor(D, TU);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000595 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
596 if (!V.hasValue())
597 continue;
598 if (!V.getValue())
599 return false;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000600 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000601 return true;
602 }
Douglas Gregorb1373d02010-01-20 20:59:29 +0000603 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000604}
605
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000606bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
607 llvm_unreachable("Translation units are visited directly by Visit()");
608 return false;
609}
610
611bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
612 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
613 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000614
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000615 return false;
616}
617
618bool CursorVisitor::VisitTagDecl(TagDecl *D) {
619 return VisitDeclContext(D);
620}
621
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000622bool CursorVisitor::VisitClassTemplateSpecializationDecl(
623 ClassTemplateSpecializationDecl *D) {
624 bool ShouldVisitBody = false;
625 switch (D->getSpecializationKind()) {
626 case TSK_Undeclared:
627 case TSK_ImplicitInstantiation:
628 // Nothing to visit
629 return false;
630
631 case TSK_ExplicitInstantiationDeclaration:
632 case TSK_ExplicitInstantiationDefinition:
633 break;
634
635 case TSK_ExplicitSpecialization:
636 ShouldVisitBody = true;
637 break;
638 }
639
640 // Visit the template arguments used in the specialization.
641 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
642 TypeLoc TL = SpecType->getTypeLoc();
643 if (TemplateSpecializationTypeLoc *TSTLoc
644 = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) {
645 for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I)
646 if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I)))
647 return true;
648 }
649 }
650
651 if (ShouldVisitBody && VisitCXXRecordDecl(D))
652 return true;
653
654 return false;
655}
656
Douglas Gregor74dbe642010-08-31 19:31:58 +0000657bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
658 ClassTemplatePartialSpecializationDecl *D) {
659 // FIXME: Visit the "outer" template parameter lists on the TagDecl
660 // before visiting these template parameters.
661 if (VisitTemplateParameters(D->getTemplateParameters()))
662 return true;
663
664 // Visit the partial specialization arguments.
665 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
666 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
667 if (VisitTemplateArgumentLoc(TemplateArgs[I]))
668 return true;
669
670 return VisitCXXRecordDecl(D);
671}
672
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000673bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregor84b51d72010-09-01 20:16:53 +0000674 // Visit the default argument.
675 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
676 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
677 if (Visit(DefArg->getTypeLoc()))
678 return true;
679
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000680 return false;
681}
682
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000683bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
684 if (Expr *Init = D->getInitExpr())
685 return Visit(MakeCXCursor(Init, StmtParent, TU));
686 return false;
687}
688
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000689bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
690 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
691 if (Visit(TSInfo->getTypeLoc()))
692 return true;
693
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000694 // Visit the nested-name-specifier, if present.
695 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
696 if (VisitNestedNameSpecifierLoc(QualifierLoc))
697 return true;
698
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000699 return false;
700}
701
Douglas Gregora67e03f2010-09-09 21:42:20 +0000702/// \brief Compare two base or member initializers based on their source order.
Sean Huntcbb67482011-01-08 20:30:50 +0000703static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
704 CXXCtorInitializer const * const *X
705 = static_cast<CXXCtorInitializer const * const *>(Xp);
706 CXXCtorInitializer const * const *Y
707 = static_cast<CXXCtorInitializer const * const *>(Yp);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000708
709 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
710 return -1;
711 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
712 return 1;
713 else
714 return 0;
715}
716
Douglas Gregorb1373d02010-01-20 20:59:29 +0000717bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregor01829d32010-08-31 14:41:23 +0000718 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
719 // Visit the function declaration's syntactic components in the order
720 // written. This requires a bit of work.
Abramo Bagnara723df242010-12-14 22:11:44 +0000721 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Douglas Gregor01829d32010-08-31 14:41:23 +0000722 FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
723
724 // If we have a function declared directly (without the use of a typedef),
725 // visit just the return type. Otherwise, just visit the function's type
726 // now.
727 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) ||
728 (!FTL && Visit(TL)))
729 return true;
730
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000731 // Visit the nested-name-specifier, if present.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000732 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
733 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000734 return true;
Douglas Gregor01829d32010-08-31 14:41:23 +0000735
736 // Visit the declaration name.
737 if (VisitDeclarationNameInfo(ND->getNameInfo()))
738 return true;
739
740 // FIXME: Visit explicitly-specified template arguments!
741
742 // Visit the function parameters, if we have a function type.
743 if (FTL && VisitFunctionTypeLoc(*FTL, true))
744 return true;
745
746 // FIXME: Attributes?
747 }
748
Douglas Gregora67e03f2010-09-09 21:42:20 +0000749 if (ND->isThisDeclarationADefinition()) {
750 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
751 // Find the initializers that were written in the source.
Sean Huntcbb67482011-01-08 20:30:50 +0000752 llvm::SmallVector<CXXCtorInitializer *, 4> WrittenInits;
Douglas Gregora67e03f2010-09-09 21:42:20 +0000753 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
754 IEnd = Constructor->init_end();
755 I != IEnd; ++I) {
756 if (!(*I)->isWritten())
757 continue;
758
759 WrittenInits.push_back(*I);
760 }
761
762 // Sort the initializers in source order
763 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
Sean Huntcbb67482011-01-08 20:30:50 +0000764 &CompareCXXCtorInitializers);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000765
766 // Visit the initializers in source order
767 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
Sean Huntcbb67482011-01-08 20:30:50 +0000768 CXXCtorInitializer *Init = WrittenInits[I];
Francois Pichet00eb3f92010-12-04 09:14:42 +0000769 if (Init->isAnyMemberInitializer()) {
770 if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
Douglas Gregora67e03f2010-09-09 21:42:20 +0000771 Init->getMemberLocation(), TU)))
772 return true;
773 } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) {
774 if (Visit(BaseInfo->getTypeLoc()))
775 return true;
776 }
777
778 // Visit the initializer value.
779 if (Expr *Initializer = Init->getInit())
780 if (Visit(MakeCXCursor(Initializer, ND, TU)))
781 return true;
782 }
783 }
784
785 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
786 return true;
787 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000788
Douglas Gregorb1373d02010-01-20 20:59:29 +0000789 return false;
790}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000791
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000792bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
793 if (VisitDeclaratorDecl(D))
794 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000795
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000796 if (Expr *BitWidth = D->getBitWidth())
797 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000798
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000799 return false;
800}
801
802bool CursorVisitor::VisitVarDecl(VarDecl *D) {
803 if (VisitDeclaratorDecl(D))
804 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000805
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000806 if (Expr *Init = D->getInit())
807 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000808
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000809 return false;
810}
811
Douglas Gregor84b51d72010-09-01 20:16:53 +0000812bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
813 if (VisitDeclaratorDecl(D))
814 return true;
815
816 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
817 if (Expr *DefArg = D->getDefaultArgument())
818 return Visit(MakeCXCursor(DefArg, StmtParent, TU));
819
820 return false;
821}
822
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000823bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
824 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
825 // before visiting these template parameters.
826 if (VisitTemplateParameters(D->getTemplateParameters()))
827 return true;
828
829 return VisitFunctionDecl(D->getTemplatedDecl());
830}
831
Douglas Gregor39d6f072010-08-31 19:02:00 +0000832bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
833 // FIXME: Visit the "outer" template parameter lists on the TagDecl
834 // before visiting these template parameters.
835 if (VisitTemplateParameters(D->getTemplateParameters()))
836 return true;
837
838 return VisitCXXRecordDecl(D->getTemplatedDecl());
839}
840
Douglas Gregor84b51d72010-09-01 20:16:53 +0000841bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
842 if (VisitTemplateParameters(D->getTemplateParameters()))
843 return true;
844
845 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
846 VisitTemplateArgumentLoc(D->getDefaultArgument()))
847 return true;
848
849 return false;
850}
851
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000852bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000853 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
854 if (Visit(TSInfo->getTypeLoc()))
855 return true;
856
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000857 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000858 PEnd = ND->param_end();
859 P != PEnd; ++P) {
860 if (Visit(MakeCXCursor(*P, TU)))
861 return true;
862 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000863
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000864 if (ND->isThisDeclarationADefinition() &&
865 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
866 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000867
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000868 return false;
869}
870
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000871namespace {
872 struct ContainerDeclsSort {
873 SourceManager &SM;
874 ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
875 bool operator()(Decl *A, Decl *B) {
876 SourceLocation L_A = A->getLocStart();
877 SourceLocation L_B = B->getLocStart();
878 assert(L_A.isValid() && L_B.isValid());
879 return SM.isBeforeInTranslationUnit(L_A, L_B);
880 }
881 };
882}
883
Douglas Gregora59e3902010-01-21 23:27:09 +0000884bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000885 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
886 // an @implementation can lexically contain Decls that are not properly
887 // nested in the AST. When we identify such cases, we need to retrofit
888 // this nesting here.
889 if (!DI_current)
890 return VisitDeclContext(D);
891
892 // Scan the Decls that immediately come after the container
893 // in the current DeclContext. If any fall within the
894 // container's lexical region, stash them into a vector
895 // for later processing.
896 llvm::SmallVector<Decl *, 24> DeclsInContainer;
897 SourceLocation EndLoc = D->getSourceRange().getEnd();
Ted Kremeneka60ed472010-11-16 08:15:36 +0000898 SourceManager &SM = AU->getSourceManager();
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000899 if (EndLoc.isValid()) {
900 DeclContext::decl_iterator next = *DI_current;
901 while (++next != DE_current) {
902 Decl *D_next = *next;
903 if (!D_next)
904 break;
905 SourceLocation L = D_next->getLocStart();
906 if (!L.isValid())
907 break;
908 if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
909 *DI_current = next;
910 DeclsInContainer.push_back(D_next);
911 continue;
912 }
913 break;
914 }
915 }
916
917 // The common case.
918 if (DeclsInContainer.empty())
919 return VisitDeclContext(D);
920
921 // Get all the Decls in the DeclContext, and sort them with the
922 // additional ones we've collected. Then visit them.
923 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
924 I!=E; ++I) {
925 Decl *subDecl = *I;
Ted Kremenek0582c892010-11-02 23:17:51 +0000926 if (!subDecl || subDecl->getLexicalDeclContext() != D ||
927 subDecl->getLocStart().isInvalid())
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000928 continue;
929 DeclsInContainer.push_back(subDecl);
930 }
931
932 // Now sort the Decls so that they appear in lexical order.
933 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
934 ContainerDeclsSort(SM));
935
936 // Now visit the decls.
937 for (llvm::SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
938 E = DeclsInContainer.end(); I != E; ++I) {
939 CXCursor Cursor = MakeCXCursor(*I, TU);
940 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
941 if (!V.hasValue())
942 continue;
943 if (!V.getValue())
944 return false;
945 if (Visit(Cursor, true))
946 return true;
947 }
948 return false;
Douglas Gregora59e3902010-01-21 23:27:09 +0000949}
950
Douglas Gregorb1373d02010-01-20 20:59:29 +0000951bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000952 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
953 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000954 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000955
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000956 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
957 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
958 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000959 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000960 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000961
Douglas Gregora59e3902010-01-21 23:27:09 +0000962 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000963}
964
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000965bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
966 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
967 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
968 E = PID->protocol_end(); I != E; ++I, ++PL)
969 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
970 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000971
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000972 return VisitObjCContainerDecl(PID);
973}
974
Ted Kremenek23173d72010-05-18 21:09:07 +0000975bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
Douglas Gregor83cb9422010-09-09 17:09:21 +0000976 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
John McCallfc929202010-06-04 22:33:30 +0000977 return true;
978
Ted Kremenek23173d72010-05-18 21:09:07 +0000979 // FIXME: This implements a workaround with @property declarations also being
980 // installed in the DeclContext for the @interface. Eventually this code
981 // should be removed.
982 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
983 if (!CDecl || !CDecl->IsClassExtension())
984 return false;
985
986 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
987 if (!ID)
988 return false;
989
990 IdentifierInfo *PropertyId = PD->getIdentifier();
991 ObjCPropertyDecl *prevDecl =
992 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
993
994 if (!prevDecl)
995 return false;
996
997 // Visit synthesized methods since they will be skipped when visiting
998 // the @interface.
999 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +00001000 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +00001001 if (Visit(MakeCXCursor(MD, TU)))
1002 return true;
1003
1004 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +00001005 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +00001006 if (Visit(MakeCXCursor(MD, TU)))
1007 return true;
1008
1009 return false;
1010}
1011
Douglas Gregorb1373d02010-01-20 20:59:29 +00001012bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001013 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +00001014 if (D->getSuperClass() &&
1015 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001016 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001017 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001018 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001019
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001020 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1021 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1022 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001023 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001024 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001025
Douglas Gregora59e3902010-01-21 23:27:09 +00001026 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001027}
1028
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001029bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1030 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001031}
1032
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001033bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001034 // 'ID' could be null when dealing with invalid code.
1035 if (ObjCInterfaceDecl *ID = D->getClassInterface())
1036 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1037 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001038
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001039 return VisitObjCImplDecl(D);
1040}
1041
1042bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1043#if 0
1044 // Issue callbacks for super class.
1045 // FIXME: No source location information!
1046 if (D->getSuperClass() &&
1047 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001048 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001049 TU)))
1050 return true;
1051#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001052
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001053 return VisitObjCImplDecl(D);
1054}
1055
1056bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
1057 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1058 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
1059 E = D->protocol_end();
1060 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001061 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001062 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001063
1064 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001065}
1066
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001067bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
1068 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
1069 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
1070 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001071
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001072 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001073}
1074
Douglas Gregora4ffd852010-11-17 01:03:52 +00001075bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1076 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1077 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1078
1079 return false;
1080}
1081
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00001082bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1083 return VisitDeclContext(D);
1084}
1085
Douglas Gregor69319002010-08-31 23:48:11 +00001086bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001087 // Visit nested-name-specifier.
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001088 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1089 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001090 return true;
Douglas Gregor69319002010-08-31 23:48:11 +00001091
1092 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1093 D->getTargetNameLoc(), TU));
1094}
1095
Douglas Gregor7e242562010-09-01 19:52:22 +00001096bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001097 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001098 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1099 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001100 return true;
Douglas Gregordc355712011-02-25 00:36:19 +00001101 }
Douglas Gregor7e242562010-09-01 19:52:22 +00001102
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001103 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1104 return true;
1105
Douglas Gregor7e242562010-09-01 19:52:22 +00001106 return VisitDeclarationNameInfo(D->getNameInfo());
1107}
1108
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001109bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001110 // Visit nested-name-specifier.
Douglas Gregordb992412011-02-25 16:33:46 +00001111 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1112 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001113 return true;
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001114
1115 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1116 D->getIdentLocation(), TU));
1117}
1118
Douglas Gregor7e242562010-09-01 19:52:22 +00001119bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001120 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001121 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1122 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001123 return true;
Douglas Gregordc355712011-02-25 00:36:19 +00001124 }
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001125
Douglas Gregor7e242562010-09-01 19:52:22 +00001126 return VisitDeclarationNameInfo(D->getNameInfo());
1127}
1128
1129bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1130 UnresolvedUsingTypenameDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001131 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001132 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1133 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001134 return true;
1135
Douglas Gregor7e242562010-09-01 19:52:22 +00001136 return false;
1137}
1138
Douglas Gregor01829d32010-08-31 14:41:23 +00001139bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1140 switch (Name.getName().getNameKind()) {
1141 case clang::DeclarationName::Identifier:
1142 case clang::DeclarationName::CXXLiteralOperatorName:
1143 case clang::DeclarationName::CXXOperatorName:
1144 case clang::DeclarationName::CXXUsingDirective:
1145 return false;
1146
1147 case clang::DeclarationName::CXXConstructorName:
1148 case clang::DeclarationName::CXXDestructorName:
1149 case clang::DeclarationName::CXXConversionFunctionName:
1150 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1151 return Visit(TSInfo->getTypeLoc());
1152 return false;
1153
1154 case clang::DeclarationName::ObjCZeroArgSelector:
1155 case clang::DeclarationName::ObjCOneArgSelector:
1156 case clang::DeclarationName::ObjCMultiArgSelector:
1157 // FIXME: Per-identifier location info?
1158 return false;
1159 }
1160
1161 return false;
1162}
1163
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001164bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1165 SourceRange Range) {
1166 // FIXME: This whole routine is a hack to work around the lack of proper
1167 // source information in nested-name-specifiers (PR5791). Since we do have
1168 // a beginning source location, we can visit the first component of the
1169 // nested-name-specifier, if it's a single-token component.
1170 if (!NNS)
1171 return false;
1172
1173 // Get the first component in the nested-name-specifier.
1174 while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1175 NNS = Prefix;
1176
1177 switch (NNS->getKind()) {
1178 case NestedNameSpecifier::Namespace:
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001179 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1180 TU));
1181
Douglas Gregor14aba762011-02-24 02:36:08 +00001182 case NestedNameSpecifier::NamespaceAlias:
1183 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1184 Range.getBegin(), TU));
1185
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001186 case NestedNameSpecifier::TypeSpec: {
1187 // If the type has a form where we know that the beginning of the source
1188 // range matches up with a reference cursor. Visit the appropriate reference
1189 // cursor.
John McCallf4c73712011-01-19 06:33:43 +00001190 const Type *T = NNS->getAsType();
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001191 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1192 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1193 if (const TagType *Tag = dyn_cast<TagType>(T))
1194 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1195 if (const TemplateSpecializationType *TST
1196 = dyn_cast<TemplateSpecializationType>(T))
1197 return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1198 break;
1199 }
1200
1201 case NestedNameSpecifier::TypeSpecWithTemplate:
1202 case NestedNameSpecifier::Global:
1203 case NestedNameSpecifier::Identifier:
1204 break;
1205 }
1206
1207 return false;
1208}
1209
Douglas Gregordc355712011-02-25 00:36:19 +00001210bool
1211CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1212 llvm::SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1213 for (; Qualifier; Qualifier = Qualifier.getPrefix())
1214 Qualifiers.push_back(Qualifier);
1215
1216 while (!Qualifiers.empty()) {
1217 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1218 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1219 switch (NNS->getKind()) {
1220 case NestedNameSpecifier::Namespace:
1221 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001222 Q.getLocalBeginLoc(),
Douglas Gregordc355712011-02-25 00:36:19 +00001223 TU)))
1224 return true;
1225
1226 break;
1227
1228 case NestedNameSpecifier::NamespaceAlias:
1229 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001230 Q.getLocalBeginLoc(),
Douglas Gregordc355712011-02-25 00:36:19 +00001231 TU)))
1232 return true;
1233
1234 break;
1235
1236 case NestedNameSpecifier::TypeSpec:
1237 case NestedNameSpecifier::TypeSpecWithTemplate:
1238 if (Visit(Q.getTypeLoc()))
1239 return true;
1240
1241 break;
1242
1243 case NestedNameSpecifier::Global:
1244 case NestedNameSpecifier::Identifier:
1245 break;
1246 }
1247 }
1248
1249 return false;
1250}
1251
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001252bool CursorVisitor::VisitTemplateParameters(
1253 const TemplateParameterList *Params) {
1254 if (!Params)
1255 return false;
1256
1257 for (TemplateParameterList::const_iterator P = Params->begin(),
1258 PEnd = Params->end();
1259 P != PEnd; ++P) {
1260 if (Visit(MakeCXCursor(*P, TU)))
1261 return true;
1262 }
1263
1264 return false;
1265}
1266
Douglas Gregor0b36e612010-08-31 20:37:03 +00001267bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1268 switch (Name.getKind()) {
1269 case TemplateName::Template:
1270 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1271
1272 case TemplateName::OverloadedTemplate:
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001273 // Visit the overloaded template set.
1274 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1275 return true;
1276
Douglas Gregor0b36e612010-08-31 20:37:03 +00001277 return false;
1278
1279 case TemplateName::DependentTemplate:
1280 // FIXME: Visit nested-name-specifier.
1281 return false;
1282
1283 case TemplateName::QualifiedTemplate:
1284 // FIXME: Visit nested-name-specifier.
1285 return Visit(MakeCursorTemplateRef(
1286 Name.getAsQualifiedTemplateName()->getDecl(),
1287 Loc, TU));
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001288
1289 case TemplateName::SubstTemplateTemplateParmPack:
1290 return Visit(MakeCursorTemplateRef(
1291 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
1292 Loc, TU));
Douglas Gregor0b36e612010-08-31 20:37:03 +00001293 }
1294
1295 return false;
1296}
1297
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001298bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1299 switch (TAL.getArgument().getKind()) {
1300 case TemplateArgument::Null:
1301 case TemplateArgument::Integral:
Douglas Gregor87dd6972010-12-20 16:52:59 +00001302 case TemplateArgument::Pack:
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001303 return false;
1304
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001305 case TemplateArgument::Type:
1306 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1307 return Visit(TSInfo->getTypeLoc());
1308 return false;
1309
1310 case TemplateArgument::Declaration:
1311 if (Expr *E = TAL.getSourceDeclExpression())
1312 return Visit(MakeCXCursor(E, StmtParent, TU));
1313 return false;
1314
1315 case TemplateArgument::Expression:
1316 if (Expr *E = TAL.getSourceExpression())
1317 return Visit(MakeCXCursor(E, StmtParent, TU));
1318 return false;
1319
1320 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001321 case TemplateArgument::TemplateExpansion:
1322 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
Douglas Gregor0b36e612010-08-31 20:37:03 +00001323 TAL.getTemplateNameLoc());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001324 }
1325
1326 return false;
1327}
1328
Ted Kremeneka0536d82010-05-07 01:04:29 +00001329bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1330 return VisitDeclContext(D);
1331}
1332
Douglas Gregor01829d32010-08-31 14:41:23 +00001333bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1334 return Visit(TL.getUnqualifiedLoc());
1335}
1336
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001337bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00001338 ASTContext &Context = AU->getASTContext();
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001339
1340 // Some builtin types (such as Objective-C's "id", "sel", and
1341 // "Class") have associated declarations. Create cursors for those.
1342 QualType VisitType;
1343 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001344 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001345 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001346 case BuiltinType::Char_U:
1347 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001348 case BuiltinType::Char16:
1349 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001350 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001351 case BuiltinType::UInt:
1352 case BuiltinType::ULong:
1353 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001354 case BuiltinType::UInt128:
1355 case BuiltinType::Char_S:
1356 case BuiltinType::SChar:
Chris Lattner3f59c972010-12-25 23:25:43 +00001357 case BuiltinType::WChar_U:
1358 case BuiltinType::WChar_S:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001359 case BuiltinType::Short:
1360 case BuiltinType::Int:
1361 case BuiltinType::Long:
1362 case BuiltinType::LongLong:
1363 case BuiltinType::Int128:
1364 case BuiltinType::Float:
1365 case BuiltinType::Double:
1366 case BuiltinType::LongDouble:
1367 case BuiltinType::NullPtr:
1368 case BuiltinType::Overload:
1369 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001370 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001371
Ted Kremenekc4174cc2010-02-18 18:52:18 +00001372 case BuiltinType::ObjCId:
1373 VisitType = Context.getObjCIdType();
1374 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001375
1376 case BuiltinType::ObjCClass:
1377 VisitType = Context.getObjCClassType();
1378 break;
1379
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001380 case BuiltinType::ObjCSel:
1381 VisitType = Context.getObjCSelType();
1382 break;
1383 }
1384
1385 if (!VisitType.isNull()) {
1386 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001387 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001388 TU));
1389 }
1390
1391 return false;
1392}
1393
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001394bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1395 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
1396}
1397
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001398bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1399 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1400}
1401
1402bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1403 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1404}
1405
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001406bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001407 // FIXME: We can't visit the template type parameter, because there's
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001408 // no context information with which we can match up the depth/index in the
1409 // type to the appropriate
1410 return false;
1411}
1412
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001413bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1414 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1415 return true;
1416
John McCallc12c5bb2010-05-15 11:32:37 +00001417 return false;
1418}
1419
1420bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1421 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1422 return true;
1423
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001424 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1425 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1426 TU)))
1427 return true;
1428 }
1429
1430 return false;
1431}
1432
1433bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCallc12c5bb2010-05-15 11:32:37 +00001434 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001435}
1436
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001437bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1438 return Visit(TL.getInnerLoc());
1439}
1440
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001441bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1442 return Visit(TL.getPointeeLoc());
1443}
1444
1445bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1446 return Visit(TL.getPointeeLoc());
1447}
1448
1449bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1450 return Visit(TL.getPointeeLoc());
1451}
1452
1453bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001454 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001455}
1456
1457bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001458 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001459}
1460
Douglas Gregor01829d32010-08-31 14:41:23 +00001461bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1462 bool SkipResultType) {
1463 if (!SkipResultType && Visit(TL.getResultLoc()))
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001464 return true;
1465
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001466 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
Ted Kremenek5dbacb42010-04-07 00:27:13 +00001467 if (Decl *D = TL.getArg(I))
1468 if (Visit(MakeCXCursor(D, TU)))
1469 return true;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001470
1471 return false;
1472}
1473
1474bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1475 if (Visit(TL.getElementLoc()))
1476 return true;
1477
1478 if (Expr *Size = TL.getSizeExpr())
1479 return Visit(MakeCXCursor(Size, StmtParent, TU));
1480
1481 return false;
1482}
1483
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001484bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1485 TemplateSpecializationTypeLoc TL) {
Douglas Gregor0b36e612010-08-31 20:37:03 +00001486 // Visit the template name.
1487 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1488 TL.getTemplateNameLoc()))
1489 return true;
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001490
1491 // Visit the template arguments.
1492 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1493 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1494 return true;
1495
1496 return false;
1497}
1498
Douglas Gregor2332c112010-01-21 20:48:56 +00001499bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1500 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1501}
1502
1503bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1504 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1505 return Visit(TSInfo->getTypeLoc());
1506
1507 return false;
1508}
1509
Douglas Gregor2494dd02011-03-01 01:34:45 +00001510bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1511 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1512 return true;
1513
1514 return false;
1515}
1516
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001517bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1518 DependentTemplateSpecializationTypeLoc TL) {
1519 // Visit the nested-name-specifier, if there is one.
1520 if (TL.getQualifierLoc() &&
1521 VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1522 return true;
1523
1524 // Visit the template arguments.
1525 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1526 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1527 return true;
1528
1529 return false;
1530}
1531
Douglas Gregor9e876872011-03-01 18:12:44 +00001532bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1533 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1534 return true;
1535
1536 return Visit(TL.getNamedTypeLoc());
1537}
1538
Douglas Gregor7536dd52010-12-20 02:24:11 +00001539bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1540 return Visit(TL.getPatternLoc());
1541}
1542
Ted Kremenek3064ef92010-08-27 21:34:58 +00001543bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001544 // Visit the nested-name-specifier, if present.
1545 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1546 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1547 return true;
1548
Ted Kremenek3064ef92010-08-27 21:34:58 +00001549 if (D->isDefinition()) {
1550 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1551 E = D->bases_end(); I != E; ++I) {
1552 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
1553 return true;
1554 }
1555 }
1556
1557 return VisitTagDecl(D);
1558}
1559
Ted Kremenek09dfa372010-02-18 05:46:33 +00001560bool CursorVisitor::VisitAttributes(Decl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00001561 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
1562 i != e; ++i)
1563 if (Visit(MakeCXCursor(*i, D, TU)))
Ted Kremenek09dfa372010-02-18 05:46:33 +00001564 return true;
1565
1566 return false;
1567}
1568
Ted Kremenekc0e1d922010-11-11 08:05:18 +00001569//===----------------------------------------------------------------------===//
1570// Data-recursive visitor methods.
1571//===----------------------------------------------------------------------===//
1572
Ted Kremenek28a71942010-11-13 00:36:47 +00001573namespace {
Ted Kremenek035dc412010-11-13 00:36:50 +00001574#define DEF_JOB(NAME, DATA, KIND)\
1575class NAME : public VisitorJob {\
1576public:\
1577 NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
1578 static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
Ted Kremenekf64d8032010-11-18 00:02:32 +00001579 DATA *get() const { return static_cast<DATA*>(data[0]); }\
Ted Kremenek035dc412010-11-13 00:36:50 +00001580};
1581
1582DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1583DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001584DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
Ted Kremenek035dc412010-11-13 00:36:50 +00001585DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
Ted Kremenek60608ec2010-11-17 00:50:47 +00001586DEF_JOB(ExplicitTemplateArgsVisit, ExplicitTemplateArgumentList,
1587 ExplicitTemplateArgsVisitKind)
Douglas Gregor94d96292011-01-19 20:34:17 +00001588DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
Ted Kremenek035dc412010-11-13 00:36:50 +00001589#undef DEF_JOB
1590
1591class DeclVisit : public VisitorJob {
1592public:
1593 DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
1594 VisitorJob(parent, VisitorJob::DeclVisitKind,
1595 d, isFirst ? (void*) 1 : (void*) 0) {}
1596 static bool classof(const VisitorJob *VJ) {
Ted Kremenek82f3c502010-11-15 22:23:26 +00001597 return VJ->getKind() == DeclVisitKind;
Ted Kremenek035dc412010-11-13 00:36:50 +00001598 }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001599 Decl *get() const { return static_cast<Decl*>(data[0]); }
1600 bool isFirst() const { return data[1] ? true : false; }
Ted Kremenek035dc412010-11-13 00:36:50 +00001601};
Ted Kremenek035dc412010-11-13 00:36:50 +00001602class TypeLocVisit : public VisitorJob {
1603public:
1604 TypeLocVisit(TypeLoc tl, CXCursor parent) :
1605 VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1606 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1607
1608 static bool classof(const VisitorJob *VJ) {
1609 return VJ->getKind() == TypeLocVisitKind;
1610 }
1611
Ted Kremenek82f3c502010-11-15 22:23:26 +00001612 TypeLoc get() const {
Ted Kremenekf64d8032010-11-18 00:02:32 +00001613 QualType T = QualType::getFromOpaquePtr(data[0]);
1614 return TypeLoc(T, data[1]);
Ted Kremenek035dc412010-11-13 00:36:50 +00001615 }
1616};
1617
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001618class LabelRefVisit : public VisitorJob {
1619public:
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001620 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
1621 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001622 labelLoc.getPtrEncoding()) {}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001623
1624 static bool classof(const VisitorJob *VJ) {
1625 return VJ->getKind() == VisitorJob::LabelRefVisitKind;
1626 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001627 LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001628 SourceLocation getLoc() const {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001629 return SourceLocation::getFromPtrEncoding(data[1]); }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001630};
1631class NestedNameSpecifierVisit : public VisitorJob {
1632public:
1633 NestedNameSpecifierVisit(NestedNameSpecifier *NS, SourceRange R,
1634 CXCursor parent)
1635 : VisitorJob(parent, VisitorJob::NestedNameSpecifierVisitKind,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001636 NS, R.getBegin().getPtrEncoding(),
1637 R.getEnd().getPtrEncoding()) {}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001638 static bool classof(const VisitorJob *VJ) {
1639 return VJ->getKind() == VisitorJob::NestedNameSpecifierVisitKind;
1640 }
1641 NestedNameSpecifier *get() const {
1642 return static_cast<NestedNameSpecifier*>(data[0]);
1643 }
1644 SourceRange getSourceRange() const {
1645 SourceLocation A =
1646 SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1647 SourceLocation B =
1648 SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[2]);
1649 return SourceRange(A, B);
1650 }
1651};
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001652
1653class NestedNameSpecifierLocVisit : public VisitorJob {
1654public:
1655 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
1656 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
1657 Qualifier.getNestedNameSpecifier(),
1658 Qualifier.getOpaqueData()) { }
1659
1660 static bool classof(const VisitorJob *VJ) {
1661 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
1662 }
1663
1664 NestedNameSpecifierLoc get() const {
1665 return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]),
1666 data[1]);
1667 }
1668};
1669
Ted Kremenekf64d8032010-11-18 00:02:32 +00001670class DeclarationNameInfoVisit : public VisitorJob {
1671public:
1672 DeclarationNameInfoVisit(Stmt *S, CXCursor parent)
1673 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
1674 static bool classof(const VisitorJob *VJ) {
1675 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
1676 }
1677 DeclarationNameInfo get() const {
1678 Stmt *S = static_cast<Stmt*>(data[0]);
1679 switch (S->getStmtClass()) {
1680 default:
1681 llvm_unreachable("Unhandled Stmt");
1682 case Stmt::CXXDependentScopeMemberExprClass:
1683 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
1684 case Stmt::DependentScopeDeclRefExprClass:
1685 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
1686 }
1687 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001688};
Ted Kremenekcdba6592010-11-18 00:42:18 +00001689class MemberRefVisit : public VisitorJob {
1690public:
1691 MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent)
1692 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001693 L.getPtrEncoding()) {}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001694 static bool classof(const VisitorJob *VJ) {
1695 return VJ->getKind() == VisitorJob::MemberRefVisitKind;
1696 }
1697 FieldDecl *get() const {
1698 return static_cast<FieldDecl*>(data[0]);
1699 }
1700 SourceLocation getLoc() const {
1701 return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1702 }
1703};
Ted Kremenek28a71942010-11-13 00:36:47 +00001704class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
1705 VisitorWorkList &WL;
1706 CXCursor Parent;
1707public:
1708 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
1709 : WL(wl), Parent(parent) {}
1710
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001711 void VisitAddrLabelExpr(AddrLabelExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001712 void VisitBlockExpr(BlockExpr *B);
Ted Kremenek28a71942010-11-13 00:36:47 +00001713 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Ted Kremenek083c7e22010-11-13 05:38:03 +00001714 void VisitCompoundStmt(CompoundStmt *S);
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001715 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001716 void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001717 void VisitCXXNewExpr(CXXNewExpr *E);
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001718 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001719 void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001720 void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001721 void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
Ted Kremenekb8dd1ca2010-11-17 00:50:41 +00001722 void VisitCXXTypeidExpr(CXXTypeidExpr *E);
Ted Kremenek55b933a2010-11-17 00:50:36 +00001723 void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
Ted Kremenek1e7e8772010-11-17 00:50:52 +00001724 void VisitCXXUuidofExpr(CXXUuidofExpr *E);
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001725 void VisitDeclRefExpr(DeclRefExpr *D);
Ted Kremenek035dc412010-11-13 00:36:50 +00001726 void VisitDeclStmt(DeclStmt *S);
Ted Kremenekf64d8032010-11-18 00:02:32 +00001727 void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001728 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001729 void VisitExplicitCastExpr(ExplicitCastExpr *E);
1730 void VisitForStmt(ForStmt *FS);
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001731 void VisitGotoStmt(GotoStmt *GS);
Ted Kremenek28a71942010-11-13 00:36:47 +00001732 void VisitIfStmt(IfStmt *If);
1733 void VisitInitListExpr(InitListExpr *IE);
1734 void VisitMemberExpr(MemberExpr *M);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001735 void VisitOffsetOfExpr(OffsetOfExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001736 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001737 void VisitObjCMessageExpr(ObjCMessageExpr *M);
1738 void VisitOverloadExpr(OverloadExpr *E);
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001739 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001740 void VisitStmt(Stmt *S);
1741 void VisitSwitchStmt(SwitchStmt *S);
1742 void VisitWhileStmt(WhileStmt *W);
Ted Kremenek2939b6f2010-11-17 00:50:50 +00001743 void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
Francois Pichet6ad6f282010-12-07 00:08:36 +00001744 void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001745 void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U);
Ted Kremenek9d3bf792010-11-17 00:50:43 +00001746 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor94d96292011-01-19 20:34:17 +00001747 void VisitSizeOfPackExpr(SizeOfPackExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001748
Ted Kremenek28a71942010-11-13 00:36:47 +00001749private:
Ted Kremenekf64d8032010-11-18 00:02:32 +00001750 void AddDeclarationNameInfo(Stmt *S);
1751 void AddNestedNameSpecifier(NestedNameSpecifier *NS, SourceRange R);
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001752 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
Ted Kremenek60608ec2010-11-17 00:50:47 +00001753 void AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001754 void AddMemberRef(FieldDecl *D, SourceLocation L);
Ted Kremenek28a71942010-11-13 00:36:47 +00001755 void AddStmt(Stmt *S);
Ted Kremenek035dc412010-11-13 00:36:50 +00001756 void AddDecl(Decl *D, bool isFirst = true);
Ted Kremenek28a71942010-11-13 00:36:47 +00001757 void AddTypeLoc(TypeSourceInfo *TI);
1758 void EnqueueChildren(Stmt *S);
1759};
1760} // end anonyous namespace
1761
Ted Kremenekf64d8032010-11-18 00:02:32 +00001762void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) {
1763 // 'S' should always be non-null, since it comes from the
1764 // statement we are visiting.
1765 WL.push_back(DeclarationNameInfoVisit(S, Parent));
1766}
1767void EnqueueVisitor::AddNestedNameSpecifier(NestedNameSpecifier *N,
1768 SourceRange R) {
1769 if (N)
1770 WL.push_back(NestedNameSpecifierVisit(N, R, Parent));
1771}
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001772
1773void
1774EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1775 if (Qualifier)
1776 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
1777}
1778
Ted Kremenek28a71942010-11-13 00:36:47 +00001779void EnqueueVisitor::AddStmt(Stmt *S) {
1780 if (S)
1781 WL.push_back(StmtVisit(S, Parent));
1782}
Ted Kremenek035dc412010-11-13 00:36:50 +00001783void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
Ted Kremenek28a71942010-11-13 00:36:47 +00001784 if (D)
Ted Kremenek035dc412010-11-13 00:36:50 +00001785 WL.push_back(DeclVisit(D, Parent, isFirst));
Ted Kremenek28a71942010-11-13 00:36:47 +00001786}
Ted Kremenek60608ec2010-11-17 00:50:47 +00001787void EnqueueVisitor::
1788 AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A) {
1789 if (A)
1790 WL.push_back(ExplicitTemplateArgsVisit(
1791 const_cast<ExplicitTemplateArgumentList*>(A), Parent));
1792}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001793void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) {
1794 if (D)
1795 WL.push_back(MemberRefVisit(D, L, Parent));
1796}
Ted Kremenek28a71942010-11-13 00:36:47 +00001797void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
1798 if (TI)
1799 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
1800 }
1801void EnqueueVisitor::EnqueueChildren(Stmt *S) {
Ted Kremeneka6b70432010-11-12 21:34:09 +00001802 unsigned size = WL.size();
John McCall7502c1d2011-02-13 04:07:26 +00001803 for (Stmt::child_range Child = S->children(); Child; ++Child) {
Ted Kremenek28a71942010-11-13 00:36:47 +00001804 AddStmt(*Child);
Ted Kremeneka6b70432010-11-12 21:34:09 +00001805 }
1806 if (size == WL.size())
1807 return;
1808 // Now reverse the entries we just added. This will match the DFS
1809 // ordering performed by the worklist.
1810 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1811 std::reverse(I, E);
1812}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001813void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
1814 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
1815}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001816void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) {
1817 AddDecl(B->getBlockDecl());
1818}
Ted Kremenek28a71942010-11-13 00:36:47 +00001819void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1820 EnqueueChildren(E);
1821 AddTypeLoc(E->getTypeSourceInfo());
1822}
Ted Kremenek083c7e22010-11-13 05:38:03 +00001823void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) {
1824 for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(),
1825 E = S->body_rend(); I != E; ++I) {
1826 AddStmt(*I);
1827 }
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001828}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001829void EnqueueVisitor::
1830VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
1831 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1832 AddDeclarationNameInfo(E);
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001833 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1834 AddNestedNameSpecifierLoc(QualifierLoc);
Ted Kremenekf64d8032010-11-18 00:02:32 +00001835 if (!E->isImplicitAccess())
1836 AddStmt(E->getBase());
1837}
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001838void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
1839 // Enqueue the initializer or constructor arguments.
1840 for (unsigned I = E->getNumConstructorArgs(); I > 0; --I)
1841 AddStmt(E->getConstructorArg(I-1));
1842 // Enqueue the array size, if any.
1843 AddStmt(E->getArraySize());
1844 // Enqueue the allocated type.
1845 AddTypeLoc(E->getAllocatedTypeSourceInfo());
1846 // Enqueue the placement arguments.
1847 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
1848 AddStmt(E->getPlacementArg(I-1));
1849}
Ted Kremenek28a71942010-11-13 00:36:47 +00001850void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) {
Ted Kremenek8b8d8c92010-11-13 05:55:56 +00001851 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
1852 AddStmt(CE->getArg(I-1));
Ted Kremenek28a71942010-11-13 00:36:47 +00001853 AddStmt(CE->getCallee());
1854 AddStmt(CE->getArg(0));
1855}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001856void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1857 // Visit the name of the type being destroyed.
1858 AddTypeLoc(E->getDestroyedTypeInfo());
1859 // Visit the scope type that looks disturbingly like the nested-name-specifier
1860 // but isn't.
1861 AddTypeLoc(E->getScopeTypeInfo());
1862 // Visit the nested-name-specifier.
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001863 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1864 AddNestedNameSpecifierLoc(QualifierLoc);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001865 // Visit base expression.
1866 AddStmt(E->getBase());
1867}
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001868void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1869 AddTypeLoc(E->getTypeSourceInfo());
1870}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001871void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1872 EnqueueChildren(E);
1873 AddTypeLoc(E->getTypeSourceInfo());
1874}
Ted Kremenekb8dd1ca2010-11-17 00:50:41 +00001875void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1876 EnqueueChildren(E);
1877 if (E->isTypeOperand())
1878 AddTypeLoc(E->getTypeOperandSourceInfo());
1879}
Ted Kremenek55b933a2010-11-17 00:50:36 +00001880
1881void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr
1882 *E) {
1883 EnqueueChildren(E);
1884 AddTypeLoc(E->getTypeSourceInfo());
1885}
Ted Kremenek1e7e8772010-11-17 00:50:52 +00001886void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1887 EnqueueChildren(E);
1888 if (E->isTypeOperand())
1889 AddTypeLoc(E->getTypeOperandSourceInfo());
1890}
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001891void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek60608ec2010-11-17 00:50:47 +00001892 if (DR->hasExplicitTemplateArgs()) {
1893 AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
1894 }
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001895 WL.push_back(DeclRefExprParts(DR, Parent));
1896}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001897void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1898 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1899 AddDeclarationNameInfo(E);
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00001900 AddNestedNameSpecifierLoc(E->getQualifierLoc());
Ted Kremenekf64d8032010-11-18 00:02:32 +00001901}
Ted Kremenek035dc412010-11-13 00:36:50 +00001902void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
1903 unsigned size = WL.size();
1904 bool isFirst = true;
1905 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
1906 D != DEnd; ++D) {
1907 AddDecl(*D, isFirst);
1908 isFirst = false;
1909 }
1910 if (size == WL.size())
1911 return;
1912 // Now reverse the entries we just added. This will match the DFS
1913 // ordering performed by the worklist.
1914 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1915 std::reverse(I, E);
1916}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001917void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1918 AddStmt(E->getInit());
1919 typedef DesignatedInitExpr::Designator Designator;
1920 for (DesignatedInitExpr::reverse_designators_iterator
1921 D = E->designators_rbegin(), DEnd = E->designators_rend();
1922 D != DEnd; ++D) {
1923 if (D->isFieldDesignator()) {
1924 if (FieldDecl *Field = D->getField())
1925 AddMemberRef(Field, D->getFieldLoc());
1926 continue;
1927 }
1928 if (D->isArrayDesignator()) {
1929 AddStmt(E->getArrayIndex(*D));
1930 continue;
1931 }
1932 assert(D->isArrayRangeDesignator() && "Unknown designator kind");
1933 AddStmt(E->getArrayRangeEnd(*D));
1934 AddStmt(E->getArrayRangeStart(*D));
1935 }
1936}
Ted Kremenek28a71942010-11-13 00:36:47 +00001937void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1938 EnqueueChildren(E);
1939 AddTypeLoc(E->getTypeInfoAsWritten());
1940}
1941void EnqueueVisitor::VisitForStmt(ForStmt *FS) {
1942 AddStmt(FS->getBody());
1943 AddStmt(FS->getInc());
1944 AddStmt(FS->getCond());
1945 AddDecl(FS->getConditionVariable());
1946 AddStmt(FS->getInit());
1947}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001948void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) {
1949 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
1950}
Ted Kremenek28a71942010-11-13 00:36:47 +00001951void EnqueueVisitor::VisitIfStmt(IfStmt *If) {
1952 AddStmt(If->getElse());
1953 AddStmt(If->getThen());
1954 AddStmt(If->getCond());
1955 AddDecl(If->getConditionVariable());
1956}
1957void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) {
1958 // We care about the syntactic form of the initializer list, only.
1959 if (InitListExpr *Syntactic = IE->getSyntacticForm())
1960 IE = Syntactic;
1961 EnqueueChildren(IE);
1962}
1963void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) {
Douglas Gregor89629a72010-11-17 17:15:08 +00001964 WL.push_back(MemberExprParts(M, Parent));
1965
1966 // If the base of the member access expression is an implicit 'this', don't
1967 // visit it.
1968 // FIXME: If we ever want to show these implicit accesses, this will be
1969 // unfortunate. However, clang_getCursor() relies on this behavior.
1970 if (CXXThisExpr *This
1971 = llvm::dyn_cast<CXXThisExpr>(M->getBase()->IgnoreParenImpCasts()))
1972 if (This->isImplicit())
1973 return;
1974
Ted Kremenek28a71942010-11-13 00:36:47 +00001975 AddStmt(M->getBase());
1976}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001977void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1978 AddTypeLoc(E->getEncodedTypeSourceInfo());
1979}
Ted Kremenek28a71942010-11-13 00:36:47 +00001980void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) {
1981 EnqueueChildren(M);
1982 AddTypeLoc(M->getClassReceiverTypeInfo());
1983}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001984void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
1985 // Visit the components of the offsetof expression.
1986 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
1987 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
1988 const OffsetOfNode &Node = E->getComponent(I-1);
1989 switch (Node.getKind()) {
1990 case OffsetOfNode::Array:
1991 AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
1992 break;
1993 case OffsetOfNode::Field:
1994 AddMemberRef(Node.getField(), Node.getRange().getEnd());
1995 break;
1996 case OffsetOfNode::Identifier:
1997 case OffsetOfNode::Base:
1998 continue;
1999 }
2000 }
2001 // Visit the type into which we're computing the offset.
2002 AddTypeLoc(E->getTypeSourceInfo());
2003}
Ted Kremenek28a71942010-11-13 00:36:47 +00002004void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) {
Ted Kremenek60608ec2010-11-17 00:50:47 +00002005 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
Ted Kremenek60458782010-11-12 21:34:16 +00002006 WL.push_back(OverloadExprParts(E, Parent));
2007}
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00002008void EnqueueVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2009 EnqueueChildren(E);
2010 if (E->isArgumentType())
2011 AddTypeLoc(E->getArgumentTypeInfo());
2012}
Ted Kremenek28a71942010-11-13 00:36:47 +00002013void EnqueueVisitor::VisitStmt(Stmt *S) {
2014 EnqueueChildren(S);
2015}
2016void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) {
2017 AddStmt(S->getBody());
2018 AddStmt(S->getCond());
2019 AddDecl(S->getConditionVariable());
2020}
Ted Kremenekfafa75a2010-11-17 00:50:39 +00002021
Ted Kremenek28a71942010-11-13 00:36:47 +00002022void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) {
2023 AddStmt(W->getBody());
2024 AddStmt(W->getCond());
2025 AddDecl(W->getConditionVariable());
2026}
Ted Kremenek2939b6f2010-11-17 00:50:50 +00002027void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
2028 AddTypeLoc(E->getQueriedTypeSourceInfo());
2029}
Francois Pichet6ad6f282010-12-07 00:08:36 +00002030
2031void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
Francois Pichet6ad6f282010-12-07 00:08:36 +00002032 AddTypeLoc(E->getRhsTypeSourceInfo());
Francois Pichet0a03a3f2010-12-08 09:11:05 +00002033 AddTypeLoc(E->getLhsTypeSourceInfo());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002034}
2035
Ted Kremenek28a71942010-11-13 00:36:47 +00002036void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) {
2037 VisitOverloadExpr(U);
2038 if (!U->isImplicitAccess())
2039 AddStmt(U->getBase());
2040}
Ted Kremenek9d3bf792010-11-17 00:50:43 +00002041void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) {
2042 AddStmt(E->getSubExpr());
2043 AddTypeLoc(E->getWrittenTypeInfo());
2044}
Douglas Gregor94d96292011-01-19 20:34:17 +00002045void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2046 WL.push_back(SizeOfPackExprParts(E, Parent));
2047}
Ted Kremenek60458782010-11-12 21:34:16 +00002048
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002049void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) {
Ted Kremenek28a71942010-11-13 00:36:47 +00002050 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S);
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002051}
2052
2053bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
2054 if (RegionOfInterest.isValid()) {
2055 SourceRange Range = getRawCursorExtent(C);
2056 if (Range.isInvalid() || CompareRegionOfInterest(Range))
2057 return false;
2058 }
2059 return true;
2060}
2061
2062bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
2063 while (!WL.empty()) {
2064 // Dequeue the worklist item.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002065 VisitorJob LI = WL.back();
2066 WL.pop_back();
2067
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002068 // Set the Parent field, then back to its old value once we're done.
2069 SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
2070
2071 switch (LI.getKind()) {
Ted Kremenekf1107452010-11-12 18:26:56 +00002072 case VisitorJob::DeclVisitKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002073 Decl *D = cast<DeclVisit>(&LI)->get();
Ted Kremenekf1107452010-11-12 18:26:56 +00002074 if (!D)
2075 continue;
2076
2077 // For now, perform default visitation for Decls.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002078 if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst())))
Ted Kremenekf1107452010-11-12 18:26:56 +00002079 return true;
2080
2081 continue;
2082 }
Ted Kremenek60608ec2010-11-17 00:50:47 +00002083 case VisitorJob::ExplicitTemplateArgsVisitKind: {
2084 const ExplicitTemplateArgumentList *ArgList =
2085 cast<ExplicitTemplateArgsVisit>(&LI)->get();
2086 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
2087 *ArgEnd = Arg + ArgList->NumTemplateArgs;
2088 Arg != ArgEnd; ++Arg) {
2089 if (VisitTemplateArgumentLoc(*Arg))
2090 return true;
2091 }
2092 continue;
2093 }
Ted Kremenekcdb4caf2010-11-12 21:34:12 +00002094 case VisitorJob::TypeLocVisitKind: {
2095 // Perform default visitation for TypeLocs.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002096 if (Visit(cast<TypeLocVisit>(&LI)->get()))
Ted Kremenekcdb4caf2010-11-12 21:34:12 +00002097 return true;
2098 continue;
2099 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00002100 case VisitorJob::LabelRefVisitKind: {
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002101 LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
Ted Kremeneke7455012011-02-23 04:54:51 +00002102 if (LabelStmt *stmt = LS->getStmt()) {
2103 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
2104 TU))) {
2105 return true;
2106 }
2107 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00002108 continue;
2109 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00002110
Ted Kremenekf64d8032010-11-18 00:02:32 +00002111 case VisitorJob::NestedNameSpecifierVisitKind: {
2112 NestedNameSpecifierVisit *V = cast<NestedNameSpecifierVisit>(&LI);
2113 if (VisitNestedNameSpecifier(V->get(), V->getSourceRange()))
2114 return true;
2115 continue;
2116 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00002117
2118 case VisitorJob::NestedNameSpecifierLocVisitKind: {
2119 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
2120 if (VisitNestedNameSpecifierLoc(V->get()))
2121 return true;
2122 continue;
2123 }
2124
Ted Kremenekf64d8032010-11-18 00:02:32 +00002125 case VisitorJob::DeclarationNameInfoVisitKind: {
2126 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
2127 ->get()))
2128 return true;
2129 continue;
2130 }
Ted Kremenekcdba6592010-11-18 00:42:18 +00002131 case VisitorJob::MemberRefVisitKind: {
2132 MemberRefVisit *V = cast<MemberRefVisit>(&LI);
2133 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
2134 return true;
2135 continue;
2136 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002137 case VisitorJob::StmtVisitKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002138 Stmt *S = cast<StmtVisit>(&LI)->get();
Ted Kremenek8c269ac2010-11-11 23:11:43 +00002139 if (!S)
2140 continue;
2141
Ted Kremenekf1107452010-11-12 18:26:56 +00002142 // Update the current cursor.
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002143 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU);
Ted Kremenekcdba6592010-11-18 00:42:18 +00002144 if (!IsInRegionOfInterest(Cursor))
2145 continue;
2146 switch (Visitor(Cursor, Parent, ClientData)) {
2147 case CXChildVisit_Break: return true;
2148 case CXChildVisit_Continue: break;
2149 case CXChildVisit_Recurse:
2150 EnqueueWorkList(WL, S);
Ted Kremenek82f3c502010-11-15 22:23:26 +00002151 break;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002152 }
Ted Kremenek82f3c502010-11-15 22:23:26 +00002153 continue;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002154 }
2155 case VisitorJob::MemberExprPartsKind: {
2156 // Handle the other pieces in the MemberExpr besides the base.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002157 MemberExpr *M = cast<MemberExprParts>(&LI)->get();
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002158
2159 // Visit the nested-name-specifier
Douglas Gregor40d96a62011-02-28 21:54:11 +00002160 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
2161 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002162 return true;
2163
2164 // Visit the declaration name.
2165 if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
2166 return true;
2167
2168 // Visit the explicitly-specified template arguments, if any.
2169 if (M->hasExplicitTemplateArgs()) {
2170 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
2171 *ArgEnd = Arg + M->getNumTemplateArgs();
2172 Arg != ArgEnd; ++Arg) {
2173 if (VisitTemplateArgumentLoc(*Arg))
2174 return true;
2175 }
2176 }
2177 continue;
2178 }
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002179 case VisitorJob::DeclRefExprPartsKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002180 DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002181 // Visit nested-name-specifier, if present.
Douglas Gregor40d96a62011-02-28 21:54:11 +00002182 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
2183 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002184 return true;
2185 // Visit declaration name.
2186 if (VisitDeclarationNameInfo(DR->getNameInfo()))
2187 return true;
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002188 continue;
2189 }
Ted Kremenek60458782010-11-12 21:34:16 +00002190 case VisitorJob::OverloadExprPartsKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002191 OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
Ted Kremenek60458782010-11-12 21:34:16 +00002192 // Visit the nested-name-specifier.
Douglas Gregor4c9be892011-02-28 20:01:57 +00002193 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
2194 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremenek60458782010-11-12 21:34:16 +00002195 return true;
2196 // Visit the declaration name.
2197 if (VisitDeclarationNameInfo(O->getNameInfo()))
2198 return true;
2199 // Visit the overloaded declaration reference.
2200 if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
2201 return true;
Ted Kremenek60458782010-11-12 21:34:16 +00002202 continue;
2203 }
Douglas Gregor94d96292011-01-19 20:34:17 +00002204 case VisitorJob::SizeOfPackExprPartsKind: {
2205 SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
2206 NamedDecl *Pack = E->getPack();
2207 if (isa<TemplateTypeParmDecl>(Pack)) {
2208 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
2209 E->getPackLoc(), TU)))
2210 return true;
2211
2212 continue;
2213 }
2214
2215 if (isa<TemplateTemplateParmDecl>(Pack)) {
2216 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
2217 E->getPackLoc(), TU)))
2218 return true;
2219
2220 continue;
2221 }
2222
2223 // Non-type template parameter packs and function parameter packs are
2224 // treated like DeclRefExpr cursors.
2225 continue;
2226 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002227 }
2228 }
2229 return false;
2230}
2231
Ted Kremenekcdba6592010-11-18 00:42:18 +00002232bool CursorVisitor::Visit(Stmt *S) {
Ted Kremenekd1ded662010-11-15 23:31:32 +00002233 VisitorWorkList *WL = 0;
2234 if (!WorkListFreeList.empty()) {
2235 WL = WorkListFreeList.back();
2236 WL->clear();
2237 WorkListFreeList.pop_back();
2238 }
2239 else {
2240 WL = new VisitorWorkList();
2241 WorkListCache.push_back(WL);
2242 }
2243 EnqueueWorkList(*WL, S);
2244 bool result = RunVisitorWorkList(*WL);
2245 WorkListFreeList.push_back(WL);
2246 return result;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002247}
2248
2249//===----------------------------------------------------------------------===//
2250// Misc. API hooks.
2251//===----------------------------------------------------------------------===//
2252
Douglas Gregor8c8d5412010-09-24 21:18:36 +00002253static llvm::sys::Mutex EnableMultithreadingMutex;
2254static bool EnabledMultithreading;
2255
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00002256extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002257CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
2258 int displayDiagnostics) {
Daniel Dunbar48615ff2010-10-08 19:30:33 +00002259 // Disable pretty stack trace functionality, which will otherwise be a very
2260 // poor citizen of the world and set up all sorts of signal handlers.
2261 llvm::DisablePrettyStackTrace = true;
2262
Daniel Dunbarc7df4f32010-08-18 18:43:14 +00002263 // We use crash recovery to make some of our APIs more reliable, implicitly
2264 // enable it.
2265 llvm::CrashRecoveryContext::Enable();
2266
Douglas Gregor8c8d5412010-09-24 21:18:36 +00002267 // Enable support for multithreading in LLVM.
2268 {
2269 llvm::sys::ScopedLock L(EnableMultithreadingMutex);
2270 if (!EnabledMultithreading) {
2271 llvm::llvm_start_multithreaded();
2272 EnabledMultithreading = true;
2273 }
2274 }
2275
Douglas Gregora030b7c2010-01-22 20:35:53 +00002276 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002277 if (excludeDeclarationsFromPCH)
2278 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002279 if (displayDiagnostics)
2280 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002281 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +00002282}
2283
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002284void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002285 if (CIdx)
2286 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00002287}
2288
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002289CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +00002290 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002291 if (!CIdx)
2292 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002293
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00002294 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002295 FileSystemOptions FileSystemOpts;
2296 FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002297
Douglas Gregor28019772010-04-05 23:52:57 +00002298 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002299 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
Douglas Gregora88084b2010-02-18 18:08:43 +00002300 CXXIdx->getOnlyLocalDecls(),
2301 0, 0, true);
Ted Kremeneka60ed472010-11-16 08:15:36 +00002302 return MakeCXTranslationUnit(TU);
Steve Naroff600866c2009-08-27 19:51:58 +00002303}
2304
Douglas Gregorb1c031b2010-08-09 22:28:58 +00002305unsigned clang_defaultEditingTranslationUnitOptions() {
Douglas Gregor2a2c50b2010-09-27 05:49:58 +00002306 return CXTranslationUnit_PrecompiledPreamble |
Douglas Gregor99ba2022010-10-27 17:24:53 +00002307 CXTranslationUnit_CacheCompletionResults |
2308 CXTranslationUnit_CXXPrecompiledPreamble;
Douglas Gregorb1c031b2010-08-09 22:28:58 +00002309}
2310
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002311CXTranslationUnit
2312clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
2313 const char *source_filename,
2314 int num_command_line_args,
Douglas Gregor2ef69442010-09-01 16:43:19 +00002315 const char * const *command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +00002316 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00002317 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor5a430212010-07-21 18:52:53 +00002318 return clang_parseTranslationUnit(CIdx, source_filename,
2319 command_line_args, num_command_line_args,
2320 unsaved_files, num_unsaved_files,
2321 CXTranslationUnit_DetailedPreprocessingRecord);
2322}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002323
2324struct ParseTranslationUnitInfo {
2325 CXIndex CIdx;
2326 const char *source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002327 const char *const *command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002328 int num_command_line_args;
2329 struct CXUnsavedFile *unsaved_files;
2330 unsigned num_unsaved_files;
2331 unsigned options;
2332 CXTranslationUnit result;
2333};
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002334static void clang_parseTranslationUnit_Impl(void *UserData) {
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002335 ParseTranslationUnitInfo *PTUI =
2336 static_cast<ParseTranslationUnitInfo*>(UserData);
2337 CXIndex CIdx = PTUI->CIdx;
2338 const char *source_filename = PTUI->source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002339 const char * const *command_line_args = PTUI->command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002340 int num_command_line_args = PTUI->num_command_line_args;
2341 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
2342 unsigned num_unsaved_files = PTUI->num_unsaved_files;
2343 unsigned options = PTUI->options;
2344 PTUI->result = 0;
Douglas Gregor5a430212010-07-21 18:52:53 +00002345
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002346 if (!CIdx)
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002347 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002348
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002349 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2350
Douglas Gregor44c181a2010-07-23 00:33:23 +00002351 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
Douglas Gregordf95a132010-08-09 20:45:32 +00002352 bool CompleteTranslationUnit
2353 = ((options & CXTranslationUnit_Incomplete) == 0);
Douglas Gregor87c08a52010-08-13 22:48:40 +00002354 bool CacheCodeCompetionResults
2355 = options & CXTranslationUnit_CacheCompletionResults;
Douglas Gregor99ba2022010-10-27 17:24:53 +00002356 bool CXXPrecompilePreamble
2357 = options & CXTranslationUnit_CXXPrecompiledPreamble;
2358 bool CXXChainedPCH
2359 = options & CXTranslationUnit_CXXChainedPCH;
Douglas Gregor87c08a52010-08-13 22:48:40 +00002360
Douglas Gregor5352ac02010-01-28 00:27:43 +00002361 // Configure the diagnostics.
2362 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +00002363 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Douglas Gregor0b53cf82011-01-19 01:02:47 +00002364 Diags = CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
2365 command_line_args);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002366
Douglas Gregor4db64a42010-01-23 00:14:00 +00002367 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2368 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattnera0a270c2010-04-05 22:42:27 +00002369 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002370 const llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00002371 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +00002372 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2373 Buffer));
2374 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002375
Douglas Gregorb10daed2010-10-11 16:52:23 +00002376 llvm::SmallVector<const char *, 16> Args;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002377
Ted Kremenek139ba862009-10-22 00:03:57 +00002378 // The 'source_filename' argument is optional. If the caller does not
2379 // specify it then it is assumed that the source file is specified
2380 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002381 if (source_filename)
Douglas Gregorb10daed2010-10-11 16:52:23 +00002382 Args.push_back(source_filename);
Douglas Gregor52ddc5d2010-07-09 18:39:07 +00002383
2384 // Since the Clang C library is primarily used by batch tools dealing with
2385 // (often very broken) source code, where spell-checking can have a
2386 // significant negative impact on performance (particularly when
2387 // precompiled headers are involved), we disable it by default.
Douglas Gregorb10daed2010-10-11 16:52:23 +00002388 // Only do this if we haven't found a spell-checking-related argument.
2389 bool FoundSpellCheckingArgument = false;
2390 for (int I = 0; I != num_command_line_args; ++I) {
2391 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
2392 strcmp(command_line_args[I], "-fspell-checking") == 0) {
2393 FoundSpellCheckingArgument = true;
2394 break;
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002395 }
Douglas Gregorb10daed2010-10-11 16:52:23 +00002396 }
2397 if (!FoundSpellCheckingArgument)
2398 Args.push_back("-fno-spell-checking");
2399
2400 Args.insert(Args.end(), command_line_args,
2401 command_line_args + num_command_line_args);
Douglas Gregord93256e2010-01-28 06:00:51 +00002402
Douglas Gregor44c181a2010-07-23 00:33:23 +00002403 // Do we need the detailed preprocessing record?
2404 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
Douglas Gregorb10daed2010-10-11 16:52:23 +00002405 Args.push_back("-Xclang");
2406 Args.push_back("-detailed-preprocessing-record");
Douglas Gregor44c181a2010-07-23 00:33:23 +00002407 }
2408
Argyrios Kyrtzidis026f6912010-11-18 21:47:04 +00002409 unsigned NumErrors = Diags->getClient()->getNumErrors();
Douglas Gregorb10daed2010-10-11 16:52:23 +00002410 llvm::OwningPtr<ASTUnit> Unit(
2411 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
2412 Diags,
2413 CXXIdx->getClangResourcesPath(),
2414 CXXIdx->getOnlyLocalDecls(),
Douglas Gregore47be3e2010-11-11 00:39:14 +00002415 /*CaptureDiagnostics=*/true,
Douglas Gregorb10daed2010-10-11 16:52:23 +00002416 RemappedFiles.data(),
2417 RemappedFiles.size(),
Douglas Gregorb10daed2010-10-11 16:52:23 +00002418 PrecompilePreamble,
2419 CompleteTranslationUnit,
Douglas Gregor99ba2022010-10-27 17:24:53 +00002420 CacheCodeCompetionResults,
2421 CXXPrecompilePreamble,
2422 CXXChainedPCH));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002423
Argyrios Kyrtzidis026f6912010-11-18 21:47:04 +00002424 if (NumErrors != Diags->getClient()->getNumErrors()) {
Douglas Gregorb10daed2010-10-11 16:52:23 +00002425 // Make sure to check that 'Unit' is non-NULL.
2426 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
2427 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
2428 DEnd = Unit->stored_diag_end();
2429 D != DEnd; ++D) {
2430 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
2431 CXString Msg = clang_formatDiagnostic(&Diag,
2432 clang_defaultDiagnosticDisplayOptions());
2433 fprintf(stderr, "%s\n", clang_getCString(Msg));
2434 clang_disposeString(Msg);
2435 }
Douglas Gregor274f1902010-02-22 23:17:23 +00002436#ifdef LLVM_ON_WIN32
Douglas Gregorb10daed2010-10-11 16:52:23 +00002437 // On Windows, force a flush, since there may be multiple copies of
2438 // stderr and stdout in the file system, all with different buffers
2439 // but writing to the same device.
2440 fflush(stderr);
2441#endif
2442 }
Douglas Gregora88084b2010-02-18 18:08:43 +00002443 }
Douglas Gregord93256e2010-01-28 06:00:51 +00002444
Ted Kremeneka60ed472010-11-16 08:15:36 +00002445 PTUI->result = MakeCXTranslationUnit(Unit.take());
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002446}
2447CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
2448 const char *source_filename,
Douglas Gregor2ef69442010-09-01 16:43:19 +00002449 const char * const *command_line_args,
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002450 int num_command_line_args,
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002451 struct CXUnsavedFile *unsaved_files,
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002452 unsigned num_unsaved_files,
2453 unsigned options) {
2454 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002455 num_command_line_args, unsaved_files,
2456 num_unsaved_files, options, 0 };
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002457 llvm::CrashRecoveryContext CRC;
2458
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00002459 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
Daniel Dunbar60a45432010-08-23 22:35:34 +00002460 fprintf(stderr, "libclang: crash detected during parsing: {\n");
2461 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
2462 fprintf(stderr, " 'command_line_args' : [");
2463 for (int i = 0; i != num_command_line_args; ++i) {
2464 if (i)
2465 fprintf(stderr, ", ");
2466 fprintf(stderr, "'%s'", command_line_args[i]);
2467 }
2468 fprintf(stderr, "],\n");
2469 fprintf(stderr, " 'unsaved_files' : [");
2470 for (unsigned i = 0; i != num_unsaved_files; ++i) {
2471 if (i)
2472 fprintf(stderr, ", ");
2473 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
2474 unsaved_files[i].Length);
2475 }
2476 fprintf(stderr, "],\n");
2477 fprintf(stderr, " 'options' : %d,\n", options);
2478 fprintf(stderr, "}\n");
2479
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002480 return 0;
2481 }
2482
2483 return PTUI.result;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00002484}
2485
Douglas Gregor19998442010-08-13 15:35:05 +00002486unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
2487 return CXSaveTranslationUnit_None;
2488}
2489
2490int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
2491 unsigned options) {
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002492 if (!TU)
2493 return 1;
2494
Ted Kremeneka60ed472010-11-16 08:15:36 +00002495 return static_cast<ASTUnit *>(TU->TUData)->Save(FileName);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002496}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002497
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002498void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002499 if (CTUnit) {
2500 // If the translation unit has been marked as unsafe to free, just discard
2501 // it.
Ted Kremeneka60ed472010-11-16 08:15:36 +00002502 if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree())
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002503 return;
2504
Ted Kremeneka60ed472010-11-16 08:15:36 +00002505 delete static_cast<ASTUnit *>(CTUnit->TUData);
2506 disposeCXStringPool(CTUnit->StringPool);
2507 delete CTUnit;
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002508 }
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00002509}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002510
Douglas Gregore1e13bf2010-08-11 15:58:42 +00002511unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
2512 return CXReparse_None;
2513}
2514
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002515struct ReparseTranslationUnitInfo {
2516 CXTranslationUnit TU;
2517 unsigned num_unsaved_files;
2518 struct CXUnsavedFile *unsaved_files;
2519 unsigned options;
2520 int result;
2521};
Douglas Gregor593b0c12010-09-23 18:47:53 +00002522
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002523static void clang_reparseTranslationUnit_Impl(void *UserData) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002524 ReparseTranslationUnitInfo *RTUI =
2525 static_cast<ReparseTranslationUnitInfo*>(UserData);
2526 CXTranslationUnit TU = RTUI->TU;
2527 unsigned num_unsaved_files = RTUI->num_unsaved_files;
2528 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
2529 unsigned options = RTUI->options;
2530 (void) options;
2531 RTUI->result = 1;
2532
Douglas Gregorabc563f2010-07-19 21:46:24 +00002533 if (!TU)
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002534 return;
Douglas Gregor593b0c12010-09-23 18:47:53 +00002535
Ted Kremeneka60ed472010-11-16 08:15:36 +00002536 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor593b0c12010-09-23 18:47:53 +00002537 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002538
2539 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2540 for (unsigned I = 0; I != num_unsaved_files; ++I) {
2541 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2542 const llvm::MemoryBuffer *Buffer
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00002543 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002544 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2545 Buffer));
2546 }
2547
Douglas Gregor593b0c12010-09-23 18:47:53 +00002548 if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size()))
2549 RTUI->result = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00002550}
Douglas Gregor593b0c12010-09-23 18:47:53 +00002551
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002552int clang_reparseTranslationUnit(CXTranslationUnit TU,
2553 unsigned num_unsaved_files,
2554 struct CXUnsavedFile *unsaved_files,
2555 unsigned options) {
2556 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
2557 options, 0 };
2558 llvm::CrashRecoveryContext CRC;
2559
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00002560 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002561 fprintf(stderr, "libclang: crash detected during reparsing\n");
Ted Kremeneka60ed472010-11-16 08:15:36 +00002562 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002563 return 1;
2564 }
2565
Ted Kremenek1dfb26a2010-10-29 01:06:50 +00002566
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002567 return RTUI.result;
2568}
2569
Douglas Gregordf95a132010-08-09 20:45:32 +00002570
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002571CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002572 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002573 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002574
Ted Kremeneka60ed472010-11-16 08:15:36 +00002575 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002576 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00002577}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00002578
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002579CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002580 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002581 return Result;
2582}
2583
Ted Kremenekfb480492010-01-13 21:46:36 +00002584} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00002585
Ted Kremenekfb480492010-01-13 21:46:36 +00002586//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00002587// CXSourceLocation and CXSourceRange Operations.
2588//===----------------------------------------------------------------------===//
2589
Douglas Gregorb9790342010-01-22 21:44:22 +00002590extern "C" {
2591CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00002592 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00002593 return Result;
2594}
2595
2596unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00002597 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
2598 loc1.ptr_data[1] == loc2.ptr_data[1] &&
2599 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00002600}
2601
2602CXSourceLocation clang_getLocation(CXTranslationUnit tu,
2603 CXFile file,
2604 unsigned line,
2605 unsigned column) {
Douglas Gregor42748ec2010-04-30 19:45:53 +00002606 if (!tu || !file)
Douglas Gregorb9790342010-01-22 21:44:22 +00002607 return clang_getNullLocation();
Douglas Gregor42748ec2010-04-30 19:45:53 +00002608
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002609 bool Logging = ::getenv("LIBCLANG_LOGGING");
Ted Kremeneka60ed472010-11-16 08:15:36 +00002610 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002611 const FileEntry *File = static_cast<const FileEntry *>(file);
Douglas Gregorb9790342010-01-22 21:44:22 +00002612 SourceLocation SLoc
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002613 = CXXUnit->getSourceManager().getLocation(File, line, column);
2614 if (SLoc.isInvalid()) {
2615 if (Logging)
2616 llvm::errs() << "clang_getLocation(\"" << File->getName()
2617 << "\", " << line << ", " << column << ") = invalid\n";
2618 return clang_getNullLocation();
2619 }
2620
2621 if (Logging)
2622 llvm::errs() << "clang_getLocation(\"" << File->getName()
2623 << "\", " << line << ", " << column << ") = "
2624 << SLoc.getRawEncoding() << "\n";
David Chisnall83889a72010-10-15 17:07:39 +00002625
2626 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
2627}
2628
2629CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu,
2630 CXFile file,
2631 unsigned offset) {
2632 if (!tu || !file)
2633 return clang_getNullLocation();
2634
Ted Kremeneka60ed472010-11-16 08:15:36 +00002635 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
David Chisnall83889a72010-10-15 17:07:39 +00002636 SourceLocation Start
2637 = CXXUnit->getSourceManager().getLocation(
2638 static_cast<const FileEntry *>(file),
2639 1, 1);
2640 if (Start.isInvalid()) return clang_getNullLocation();
2641
2642 SourceLocation SLoc = Start.getFileLocWithOffset(offset);
2643
2644 if (SLoc.isInvalid()) return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002645
Ted Kremenek1a9a0bc2010-06-28 23:54:17 +00002646 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00002647}
2648
Douglas Gregor5352ac02010-01-28 00:27:43 +00002649CXSourceRange clang_getNullRange() {
2650 CXSourceRange Result = { { 0, 0 }, 0, 0 };
2651 return Result;
2652}
Daniel Dunbard52864b2010-02-14 10:02:57 +00002653
Douglas Gregor5352ac02010-01-28 00:27:43 +00002654CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
2655 if (begin.ptr_data[0] != end.ptr_data[0] ||
2656 begin.ptr_data[1] != end.ptr_data[1])
2657 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002658
2659 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002660 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00002661 return Result;
2662}
2663
Douglas Gregor46766dc2010-01-26 19:19:08 +00002664void clang_getInstantiationLocation(CXSourceLocation location,
2665 CXFile *file,
2666 unsigned *line,
2667 unsigned *column,
2668 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00002669 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
2670
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002671 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00002672 if (file)
2673 *file = 0;
2674 if (line)
2675 *line = 0;
2676 if (column)
2677 *column = 0;
2678 if (offset)
2679 *offset = 0;
2680 return;
2681 }
2682
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002683 const SourceManager &SM =
2684 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002685 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002686
2687 if (file)
2688 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
2689 if (line)
2690 *line = SM.getInstantiationLineNumber(InstLoc);
2691 if (column)
2692 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00002693 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00002694 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00002695}
2696
Douglas Gregora9b06d42010-11-09 06:24:54 +00002697void clang_getSpellingLocation(CXSourceLocation location,
2698 CXFile *file,
2699 unsigned *line,
2700 unsigned *column,
2701 unsigned *offset) {
2702 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
2703
2704 if (!location.ptr_data[0] || Loc.isInvalid()) {
2705 if (file)
2706 *file = 0;
2707 if (line)
2708 *line = 0;
2709 if (column)
2710 *column = 0;
2711 if (offset)
2712 *offset = 0;
2713 return;
2714 }
2715
2716 const SourceManager &SM =
2717 *static_cast<const SourceManager*>(location.ptr_data[0]);
2718 SourceLocation SpellLoc = Loc;
2719 if (SpellLoc.isMacroID()) {
2720 SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);
2721 if (SimpleSpellingLoc.isFileID() &&
2722 SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))
2723 SpellLoc = SimpleSpellingLoc;
2724 else
2725 SpellLoc = SM.getInstantiationLoc(SpellLoc);
2726 }
2727
2728 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
2729 FileID FID = LocInfo.first;
2730 unsigned FileOffset = LocInfo.second;
2731
2732 if (file)
2733 *file = (void *)SM.getFileEntryForID(FID);
2734 if (line)
2735 *line = SM.getLineNumber(FID, FileOffset);
2736 if (column)
2737 *column = SM.getColumnNumber(FID, FileOffset);
2738 if (offset)
2739 *offset = FileOffset;
2740}
2741
Douglas Gregor1db19de2010-01-19 21:36:55 +00002742CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002743 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002744 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002745 return Result;
2746}
2747
2748CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002749 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002750 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002751 return Result;
2752}
2753
Douglas Gregorb9790342010-01-22 21:44:22 +00002754} // end: extern "C"
2755
Douglas Gregor1db19de2010-01-19 21:36:55 +00002756//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002757// CXFile Operations.
2758//===----------------------------------------------------------------------===//
2759
2760extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00002761CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002762 if (!SFile)
Ted Kremeneka60ed472010-11-16 08:15:36 +00002763 return createCXString((const char*)NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002764
Steve Naroff88145032009-10-27 14:35:18 +00002765 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00002766 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00002767}
2768
2769time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002770 if (!SFile)
2771 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002772
Steve Naroff88145032009-10-27 14:35:18 +00002773 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
2774 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00002775}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002776
Douglas Gregorb9790342010-01-22 21:44:22 +00002777CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
2778 if (!tu)
2779 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002780
Ted Kremeneka60ed472010-11-16 08:15:36 +00002781 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002782
Douglas Gregorb9790342010-01-22 21:44:22 +00002783 FileManager &FMgr = CXXUnit->getFileManager();
Chris Lattner39b49bc2010-11-23 08:35:12 +00002784 return const_cast<FileEntry *>(FMgr.getFile(file_name));
Douglas Gregorb9790342010-01-22 21:44:22 +00002785}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002786
Ted Kremenekfb480492010-01-13 21:46:36 +00002787} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00002788
Ted Kremenekfb480492010-01-13 21:46:36 +00002789//===----------------------------------------------------------------------===//
2790// CXCursor Operations.
2791//===----------------------------------------------------------------------===//
2792
Ted Kremenekfb480492010-01-13 21:46:36 +00002793static Decl *getDeclFromExpr(Stmt *E) {
Douglas Gregordb1314e2010-10-01 21:11:22 +00002794 if (CastExpr *CE = dyn_cast<CastExpr>(E))
2795 return getDeclFromExpr(CE->getSubExpr());
2796
Ted Kremenekfb480492010-01-13 21:46:36 +00002797 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
2798 return RefExpr->getDecl();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002799 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2800 return RefExpr->getDecl();
Ted Kremenekfb480492010-01-13 21:46:36 +00002801 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
2802 return ME->getMemberDecl();
2803 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
2804 return RE->getDecl();
Douglas Gregordb1314e2010-10-01 21:11:22 +00002805 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
John McCall12f78a62010-12-02 01:19:52 +00002806 return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0;
Douglas Gregordb1314e2010-10-01 21:11:22 +00002807
Ted Kremenekfb480492010-01-13 21:46:36 +00002808 if (CallExpr *CE = dyn_cast<CallExpr>(E))
2809 return getDeclFromExpr(CE->getCallee());
Douglas Gregor93798e22010-11-05 21:11:19 +00002810 if (CXXConstructExpr *CE = llvm::dyn_cast<CXXConstructExpr>(E))
2811 if (!CE->isElidable())
2812 return CE->getConstructor();
Ted Kremenekfb480492010-01-13 21:46:36 +00002813 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
2814 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002815
Douglas Gregordb1314e2010-10-01 21:11:22 +00002816 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
2817 return PE->getProtocol();
Douglas Gregorc7793c72011-01-15 01:15:58 +00002818 if (SubstNonTypeTemplateParmPackExpr *NTTP
2819 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
2820 return NTTP->getParameterPack();
Douglas Gregor94d96292011-01-19 20:34:17 +00002821 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2822 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
2823 isa<ParmVarDecl>(SizeOfPack->getPack()))
2824 return SizeOfPack->getPack();
Douglas Gregordb1314e2010-10-01 21:11:22 +00002825
Ted Kremenekfb480492010-01-13 21:46:36 +00002826 return 0;
2827}
2828
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002829static SourceLocation getLocationFromExpr(Expr *E) {
2830 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
2831 return /*FIXME:*/Msg->getLeftLoc();
2832 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
2833 return DRE->getLocation();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002834 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2835 return RefExpr->getLocation();
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002836 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
2837 return Member->getMemberLoc();
2838 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
2839 return Ivar->getLocation();
Douglas Gregor94d96292011-01-19 20:34:17 +00002840 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2841 return SizeOfPack->getPackLoc();
2842
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002843 return E->getLocStart();
2844}
2845
Ted Kremenekfb480492010-01-13 21:46:36 +00002846extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002847
2848unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00002849 CXCursorVisitor visitor,
2850 CXClientData client_data) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00002851 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
2852 getCursorASTUnit(parent)->getMaxPCHLevel());
Douglas Gregorb1373d02010-01-20 20:59:29 +00002853 return CursorVis.VisitChildren(parent);
2854}
2855
David Chisnall3387c652010-11-03 14:12:26 +00002856#ifndef __has_feature
2857#define __has_feature(x) 0
2858#endif
2859#if __has_feature(blocks)
2860typedef enum CXChildVisitResult
2861 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
2862
2863static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2864 CXClientData client_data) {
2865 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2866 return block(cursor, parent);
2867}
2868#else
2869// If we are compiled with a compiler that doesn't have native blocks support,
2870// define and call the block manually, so the
2871typedef struct _CXChildVisitResult
2872{
2873 void *isa;
2874 int flags;
2875 int reserved;
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002876 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
2877 CXCursor);
David Chisnall3387c652010-11-03 14:12:26 +00002878} *CXCursorVisitorBlock;
2879
2880static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2881 CXClientData client_data) {
2882 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2883 return block->invoke(block, cursor, parent);
2884}
2885#endif
2886
2887
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002888unsigned clang_visitChildrenWithBlock(CXCursor parent,
2889 CXCursorVisitorBlock block) {
David Chisnall3387c652010-11-03 14:12:26 +00002890 return clang_visitChildren(parent, visitWithBlock, block);
2891}
2892
Douglas Gregor78205d42010-01-20 21:45:58 +00002893static CXString getDeclSpelling(Decl *D) {
2894 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
Douglas Gregore3c60a72010-11-17 00:13:31 +00002895 if (!ND) {
2896 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
2897 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
2898 return createCXString(Property->getIdentifier()->getName());
2899
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002900 return createCXString("");
Douglas Gregore3c60a72010-11-17 00:13:31 +00002901 }
2902
Douglas Gregor78205d42010-01-20 21:45:58 +00002903 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002904 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002905
Douglas Gregor78205d42010-01-20 21:45:58 +00002906 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
2907 // No, this isn't the same as the code below. getIdentifier() is non-virtual
2908 // and returns different names. NamedDecl returns the class name and
2909 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002910 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002911
Douglas Gregor0a35bce2010-09-01 03:07:18 +00002912 if (isa<UsingDirectiveDecl>(D))
2913 return createCXString("");
2914
Ted Kremenek50aa6ac2010-05-19 21:51:10 +00002915 llvm::SmallString<1024> S;
2916 llvm::raw_svector_ostream os(S);
2917 ND->printName(os);
2918
2919 return createCXString(os.str());
Douglas Gregor78205d42010-01-20 21:45:58 +00002920}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002921
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002922CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002923 if (clang_isTranslationUnit(C.kind))
Ted Kremeneka60ed472010-11-16 08:15:36 +00002924 return clang_getTranslationUnitSpelling(
2925 static_cast<CXTranslationUnit>(C.data[2]));
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002926
Steve Narofff334b4e2009-09-02 18:26:48 +00002927 if (clang_isReference(C.kind)) {
2928 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00002929 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00002930 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002931 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002932 }
2933 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00002934 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002935 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002936 }
2937 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00002938 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00002939 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002940 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002941 }
Ted Kremenek3064ef92010-08-27 21:34:58 +00002942 case CXCursor_CXXBaseSpecifier: {
2943 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
2944 return createCXString(B->getType().getAsString());
2945 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002946 case CXCursor_TypeRef: {
2947 TypeDecl *Type = getCursorTypeRef(C).first;
2948 assert(Type && "Missing type decl");
2949
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002950 return createCXString(getCursorContext(C).getTypeDeclType(Type).
2951 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002952 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00002953 case CXCursor_TemplateRef: {
2954 TemplateDecl *Template = getCursorTemplateRef(C).first;
Douglas Gregor69319002010-08-31 23:48:11 +00002955 assert(Template && "Missing template decl");
Douglas Gregor0b36e612010-08-31 20:37:03 +00002956
2957 return createCXString(Template->getNameAsString());
2958 }
Douglas Gregor69319002010-08-31 23:48:11 +00002959
2960 case CXCursor_NamespaceRef: {
2961 NamedDecl *NS = getCursorNamespaceRef(C).first;
2962 assert(NS && "Missing namespace decl");
2963
2964 return createCXString(NS->getNameAsString());
2965 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002966
Douglas Gregora67e03f2010-09-09 21:42:20 +00002967 case CXCursor_MemberRef: {
2968 FieldDecl *Field = getCursorMemberRef(C).first;
2969 assert(Field && "Missing member decl");
2970
2971 return createCXString(Field->getNameAsString());
2972 }
2973
Douglas Gregor36897b02010-09-10 00:22:18 +00002974 case CXCursor_LabelRef: {
2975 LabelStmt *Label = getCursorLabelRef(C).first;
2976 assert(Label && "Missing label");
2977
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002978 return createCXString(Label->getName());
Douglas Gregor36897b02010-09-10 00:22:18 +00002979 }
2980
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00002981 case CXCursor_OverloadedDeclRef: {
2982 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
2983 if (Decl *D = Storage.dyn_cast<Decl *>()) {
2984 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
2985 return createCXString(ND->getNameAsString());
2986 return createCXString("");
2987 }
2988 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
2989 return createCXString(E->getName().getAsString());
2990 OverloadedTemplateStorage *Ovl
2991 = Storage.get<OverloadedTemplateStorage*>();
2992 if (Ovl->size() == 0)
2993 return createCXString("");
2994 return createCXString((*Ovl->begin())->getNameAsString());
2995 }
2996
Daniel Dunbaracca7252009-11-30 20:42:49 +00002997 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002998 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00002999 }
3000 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003001
3002 if (clang_isExpression(C.kind)) {
3003 Decl *D = getDeclFromExpr(getCursorExpr(C));
3004 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00003005 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003006 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00003007 }
3008
Douglas Gregor36897b02010-09-10 00:22:18 +00003009 if (clang_isStatement(C.kind)) {
3010 Stmt *S = getCursorStmt(C);
3011 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003012 return createCXString(Label->getName());
Douglas Gregor36897b02010-09-10 00:22:18 +00003013
3014 return createCXString("");
3015 }
3016
Douglas Gregor4ae8f292010-03-18 17:52:52 +00003017 if (C.kind == CXCursor_MacroInstantiation)
3018 return createCXString(getCursorMacroInstantiation(C)->getName()
3019 ->getNameStart());
3020
Douglas Gregor572feb22010-03-18 18:04:21 +00003021 if (C.kind == CXCursor_MacroDefinition)
3022 return createCXString(getCursorMacroDefinition(C)->getName()
3023 ->getNameStart());
3024
Douglas Gregorecdcb882010-10-20 22:00:55 +00003025 if (C.kind == CXCursor_InclusionDirective)
3026 return createCXString(getCursorInclusionDirective(C)->getFileName());
3027
Douglas Gregor60cbfac2010-01-25 16:56:17 +00003028 if (clang_isDeclaration(C.kind))
3029 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00003030
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003031 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00003032}
3033
Douglas Gregor358559d2010-10-02 22:49:11 +00003034CXString clang_getCursorDisplayName(CXCursor C) {
3035 if (!clang_isDeclaration(C.kind))
3036 return clang_getCursorSpelling(C);
3037
3038 Decl *D = getCursorDecl(C);
3039 if (!D)
3040 return createCXString("");
3041
3042 PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy;
3043 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
3044 D = FunTmpl->getTemplatedDecl();
3045
3046 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
3047 llvm::SmallString<64> Str;
3048 llvm::raw_svector_ostream OS(Str);
3049 OS << Function->getNameAsString();
3050 if (Function->getPrimaryTemplate())
3051 OS << "<>";
3052 OS << "(";
3053 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
3054 if (I)
3055 OS << ", ";
3056 OS << Function->getParamDecl(I)->getType().getAsString(Policy);
3057 }
3058
3059 if (Function->isVariadic()) {
3060 if (Function->getNumParams())
3061 OS << ", ";
3062 OS << "...";
3063 }
3064 OS << ")";
3065 return createCXString(OS.str());
3066 }
3067
3068 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
3069 llvm::SmallString<64> Str;
3070 llvm::raw_svector_ostream OS(Str);
3071 OS << ClassTemplate->getNameAsString();
3072 OS << "<";
3073 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
3074 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
3075 if (I)
3076 OS << ", ";
3077
3078 NamedDecl *Param = Params->getParam(I);
3079 if (Param->getIdentifier()) {
3080 OS << Param->getIdentifier()->getName();
3081 continue;
3082 }
3083
3084 // There is no parameter name, which makes this tricky. Try to come up
3085 // with something useful that isn't too long.
3086 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3087 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
3088 else if (NonTypeTemplateParmDecl *NTTP
3089 = dyn_cast<NonTypeTemplateParmDecl>(Param))
3090 OS << NTTP->getType().getAsString(Policy);
3091 else
3092 OS << "template<...> class";
3093 }
3094
3095 OS << ">";
3096 return createCXString(OS.str());
3097 }
3098
3099 if (ClassTemplateSpecializationDecl *ClassSpec
3100 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3101 // If the type was explicitly written, use that.
3102 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
3103 return createCXString(TSInfo->getType().getAsString(Policy));
3104
3105 llvm::SmallString<64> Str;
3106 llvm::raw_svector_ostream OS(Str);
3107 OS << ClassSpec->getNameAsString();
3108 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00003109 ClassSpec->getTemplateArgs().data(),
3110 ClassSpec->getTemplateArgs().size(),
Douglas Gregor358559d2010-10-02 22:49:11 +00003111 Policy);
3112 return createCXString(OS.str());
3113 }
3114
3115 return clang_getCursorSpelling(C);
3116}
3117
Ted Kremeneke68fff62010-02-17 00:41:32 +00003118CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00003119 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003120 case CXCursor_FunctionDecl:
3121 return createCXString("FunctionDecl");
3122 case CXCursor_TypedefDecl:
3123 return createCXString("TypedefDecl");
3124 case CXCursor_EnumDecl:
3125 return createCXString("EnumDecl");
3126 case CXCursor_EnumConstantDecl:
3127 return createCXString("EnumConstantDecl");
3128 case CXCursor_StructDecl:
3129 return createCXString("StructDecl");
3130 case CXCursor_UnionDecl:
3131 return createCXString("UnionDecl");
3132 case CXCursor_ClassDecl:
3133 return createCXString("ClassDecl");
3134 case CXCursor_FieldDecl:
3135 return createCXString("FieldDecl");
3136 case CXCursor_VarDecl:
3137 return createCXString("VarDecl");
3138 case CXCursor_ParmDecl:
3139 return createCXString("ParmDecl");
3140 case CXCursor_ObjCInterfaceDecl:
3141 return createCXString("ObjCInterfaceDecl");
3142 case CXCursor_ObjCCategoryDecl:
3143 return createCXString("ObjCCategoryDecl");
3144 case CXCursor_ObjCProtocolDecl:
3145 return createCXString("ObjCProtocolDecl");
3146 case CXCursor_ObjCPropertyDecl:
3147 return createCXString("ObjCPropertyDecl");
3148 case CXCursor_ObjCIvarDecl:
3149 return createCXString("ObjCIvarDecl");
3150 case CXCursor_ObjCInstanceMethodDecl:
3151 return createCXString("ObjCInstanceMethodDecl");
3152 case CXCursor_ObjCClassMethodDecl:
3153 return createCXString("ObjCClassMethodDecl");
3154 case CXCursor_ObjCImplementationDecl:
3155 return createCXString("ObjCImplementationDecl");
3156 case CXCursor_ObjCCategoryImplDecl:
3157 return createCXString("ObjCCategoryImplDecl");
Ted Kremenek8bd5a692010-04-13 23:39:06 +00003158 case CXCursor_CXXMethod:
3159 return createCXString("CXXMethod");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003160 case CXCursor_UnexposedDecl:
3161 return createCXString("UnexposedDecl");
3162 case CXCursor_ObjCSuperClassRef:
3163 return createCXString("ObjCSuperClassRef");
3164 case CXCursor_ObjCProtocolRef:
3165 return createCXString("ObjCProtocolRef");
3166 case CXCursor_ObjCClassRef:
3167 return createCXString("ObjCClassRef");
3168 case CXCursor_TypeRef:
3169 return createCXString("TypeRef");
Douglas Gregor0b36e612010-08-31 20:37:03 +00003170 case CXCursor_TemplateRef:
3171 return createCXString("TemplateRef");
Douglas Gregor69319002010-08-31 23:48:11 +00003172 case CXCursor_NamespaceRef:
3173 return createCXString("NamespaceRef");
Douglas Gregora67e03f2010-09-09 21:42:20 +00003174 case CXCursor_MemberRef:
3175 return createCXString("MemberRef");
Douglas Gregor36897b02010-09-10 00:22:18 +00003176 case CXCursor_LabelRef:
3177 return createCXString("LabelRef");
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003178 case CXCursor_OverloadedDeclRef:
3179 return createCXString("OverloadedDeclRef");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003180 case CXCursor_UnexposedExpr:
3181 return createCXString("UnexposedExpr");
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00003182 case CXCursor_BlockExpr:
3183 return createCXString("BlockExpr");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003184 case CXCursor_DeclRefExpr:
3185 return createCXString("DeclRefExpr");
3186 case CXCursor_MemberRefExpr:
3187 return createCXString("MemberRefExpr");
3188 case CXCursor_CallExpr:
3189 return createCXString("CallExpr");
3190 case CXCursor_ObjCMessageExpr:
3191 return createCXString("ObjCMessageExpr");
3192 case CXCursor_UnexposedStmt:
3193 return createCXString("UnexposedStmt");
Douglas Gregor36897b02010-09-10 00:22:18 +00003194 case CXCursor_LabelStmt:
3195 return createCXString("LabelStmt");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003196 case CXCursor_InvalidFile:
3197 return createCXString("InvalidFile");
Ted Kremenek292db642010-03-19 20:39:05 +00003198 case CXCursor_InvalidCode:
3199 return createCXString("InvalidCode");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003200 case CXCursor_NoDeclFound:
3201 return createCXString("NoDeclFound");
3202 case CXCursor_NotImplemented:
3203 return createCXString("NotImplemented");
3204 case CXCursor_TranslationUnit:
3205 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00003206 case CXCursor_UnexposedAttr:
3207 return createCXString("UnexposedAttr");
3208 case CXCursor_IBActionAttr:
3209 return createCXString("attribute(ibaction)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003210 case CXCursor_IBOutletAttr:
3211 return createCXString("attribute(iboutlet)");
Ted Kremenek857e9182010-05-19 17:38:06 +00003212 case CXCursor_IBOutletCollectionAttr:
3213 return createCXString("attribute(iboutletcollection)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003214 case CXCursor_PreprocessingDirective:
3215 return createCXString("preprocessing directive");
Douglas Gregor572feb22010-03-18 18:04:21 +00003216 case CXCursor_MacroDefinition:
3217 return createCXString("macro definition");
Douglas Gregor48072312010-03-18 15:23:44 +00003218 case CXCursor_MacroInstantiation:
3219 return createCXString("macro instantiation");
Douglas Gregorecdcb882010-10-20 22:00:55 +00003220 case CXCursor_InclusionDirective:
3221 return createCXString("inclusion directive");
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00003222 case CXCursor_Namespace:
3223 return createCXString("Namespace");
Ted Kremeneka0536d82010-05-07 01:04:29 +00003224 case CXCursor_LinkageSpec:
3225 return createCXString("LinkageSpec");
Ted Kremenek3064ef92010-08-27 21:34:58 +00003226 case CXCursor_CXXBaseSpecifier:
3227 return createCXString("C++ base class specifier");
Douglas Gregor01829d32010-08-31 14:41:23 +00003228 case CXCursor_Constructor:
3229 return createCXString("CXXConstructor");
3230 case CXCursor_Destructor:
3231 return createCXString("CXXDestructor");
3232 case CXCursor_ConversionFunction:
3233 return createCXString("CXXConversion");
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00003234 case CXCursor_TemplateTypeParameter:
3235 return createCXString("TemplateTypeParameter");
3236 case CXCursor_NonTypeTemplateParameter:
3237 return createCXString("NonTypeTemplateParameter");
3238 case CXCursor_TemplateTemplateParameter:
3239 return createCXString("TemplateTemplateParameter");
3240 case CXCursor_FunctionTemplate:
3241 return createCXString("FunctionTemplate");
Douglas Gregor39d6f072010-08-31 19:02:00 +00003242 case CXCursor_ClassTemplate:
3243 return createCXString("ClassTemplate");
Douglas Gregor74dbe642010-08-31 19:31:58 +00003244 case CXCursor_ClassTemplatePartialSpecialization:
3245 return createCXString("ClassTemplatePartialSpecialization");
Douglas Gregor69319002010-08-31 23:48:11 +00003246 case CXCursor_NamespaceAlias:
3247 return createCXString("NamespaceAlias");
Douglas Gregor0a35bce2010-09-01 03:07:18 +00003248 case CXCursor_UsingDirective:
3249 return createCXString("UsingDirective");
Douglas Gregor7e242562010-09-01 19:52:22 +00003250 case CXCursor_UsingDeclaration:
3251 return createCXString("UsingDeclaration");
Steve Naroff89922f82009-08-31 00:59:03 +00003252 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00003253
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00003254 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneka60ed472010-11-16 08:15:36 +00003255 return createCXString((const char*) 0);
Steve Naroff600866c2009-08-27 19:51:58 +00003256}
Steve Naroff89922f82009-08-31 00:59:03 +00003257
Ted Kremeneke68fff62010-02-17 00:41:32 +00003258enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
3259 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003260 CXClientData client_data) {
3261 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
Douglas Gregor93798e22010-11-05 21:11:19 +00003262
3263 // If our current best cursor is the construction of a temporary object,
3264 // don't replace that cursor with a type reference, because we want
3265 // clang_getCursor() to point at the constructor.
3266 if (clang_isExpression(BestCursor->kind) &&
3267 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
3268 cursor.kind == CXCursor_TypeRef)
3269 return CXChildVisit_Recurse;
3270
Douglas Gregor85fe1562010-12-10 07:23:11 +00003271 // Don't override a preprocessing cursor with another preprocessing
3272 // cursor; we want the outermost preprocessing cursor.
3273 if (clang_isPreprocessing(cursor.kind) &&
3274 clang_isPreprocessing(BestCursor->kind))
3275 return CXChildVisit_Recurse;
3276
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003277 *BestCursor = cursor;
3278 return CXChildVisit_Recurse;
3279}
Ted Kremeneke68fff62010-02-17 00:41:32 +00003280
Douglas Gregorb9790342010-01-22 21:44:22 +00003281CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
3282 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00003283 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00003284
Ted Kremeneka60ed472010-11-16 08:15:36 +00003285 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorbdf60622010-03-05 21:16:25 +00003286 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3287
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003288 // Translate the given source location to make it point at the beginning of
3289 // the token under the cursor.
Ted Kremeneka297de22010-01-25 22:34:44 +00003290 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Ted Kremeneka629ea42010-07-29 00:52:07 +00003291
3292 // Guard against an invalid SourceLocation, or we may assert in one
3293 // of the following calls.
3294 if (SLoc.isInvalid())
3295 return clang_getNullCursor();
3296
Douglas Gregor40749ee2010-11-03 00:35:38 +00003297 bool Logging = getenv("LIBCLANG_LOGGING");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003298 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
3299 CXXUnit->getASTContext().getLangOptions());
3300
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003301 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
3302 if (SLoc.isValid()) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003303 // FIXME: Would be great to have a "hint" cursor, then walk from that
3304 // hint cursor upward until we find a cursor whose source range encloses
3305 // the region of interest, rather than starting from the translation unit.
Ted Kremeneka60ed472010-11-16 08:15:36 +00003306 CXCursor Parent = clang_getTranslationUnitCursor(TU);
3307 CursorVisitor CursorVis(TU, GetCursorVisitor, &Result,
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003308 Decl::MaxPCHLevel, SourceLocation(SLoc));
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003309 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00003310 }
Douglas Gregor40749ee2010-11-03 00:35:38 +00003311
3312 if (Logging) {
3313 CXFile SearchFile;
3314 unsigned SearchLine, SearchColumn;
3315 CXFile ResultFile;
3316 unsigned ResultLine, ResultColumn;
Douglas Gregor66537982010-11-17 17:14:07 +00003317 CXString SearchFileName, ResultFileName, KindSpelling, USR;
3318 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
Douglas Gregor40749ee2010-11-03 00:35:38 +00003319 CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
3320
3321 clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
3322 0);
3323 clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine,
3324 &ResultColumn, 0);
3325 SearchFileName = clang_getFileName(SearchFile);
3326 ResultFileName = clang_getFileName(ResultFile);
3327 KindSpelling = clang_getCursorKindSpelling(Result.kind);
Douglas Gregor66537982010-11-17 17:14:07 +00003328 USR = clang_getCursorUSR(Result);
3329 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n",
Douglas Gregor40749ee2010-11-03 00:35:38 +00003330 clang_getCString(SearchFileName), SearchLine, SearchColumn,
3331 clang_getCString(KindSpelling),
Douglas Gregor66537982010-11-17 17:14:07 +00003332 clang_getCString(ResultFileName), ResultLine, ResultColumn,
3333 clang_getCString(USR), IsDef);
Douglas Gregor40749ee2010-11-03 00:35:38 +00003334 clang_disposeString(SearchFileName);
3335 clang_disposeString(ResultFileName);
3336 clang_disposeString(KindSpelling);
Douglas Gregor66537982010-11-17 17:14:07 +00003337 clang_disposeString(USR);
Douglas Gregor0aefbd82010-12-10 01:45:00 +00003338
3339 CXCursor Definition = clang_getCursorDefinition(Result);
3340 if (!clang_equalCursors(Definition, clang_getNullCursor())) {
3341 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
3342 CXString DefinitionKindSpelling
3343 = clang_getCursorKindSpelling(Definition.kind);
3344 CXFile DefinitionFile;
3345 unsigned DefinitionLine, DefinitionColumn;
3346 clang_getInstantiationLocation(DefinitionLoc, &DefinitionFile,
3347 &DefinitionLine, &DefinitionColumn, 0);
3348 CXString DefinitionFileName = clang_getFileName(DefinitionFile);
3349 fprintf(stderr, " -> %s(%s:%d:%d)\n",
3350 clang_getCString(DefinitionKindSpelling),
3351 clang_getCString(DefinitionFileName),
3352 DefinitionLine, DefinitionColumn);
3353 clang_disposeString(DefinitionFileName);
3354 clang_disposeString(DefinitionKindSpelling);
3355 }
Douglas Gregor40749ee2010-11-03 00:35:38 +00003356 }
3357
Ted Kremeneke68fff62010-02-17 00:41:32 +00003358 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00003359}
3360
Ted Kremenek73885552009-11-17 19:28:59 +00003361CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00003362 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00003363}
3364
3365unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00003366 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00003367}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00003368
Douglas Gregor9ce55842010-11-20 00:09:34 +00003369unsigned clang_hashCursor(CXCursor C) {
3370 unsigned Index = 0;
3371 if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
3372 Index = 1;
3373
3374 return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue(
3375 std::make_pair(C.kind, C.data[Index]));
3376}
3377
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003378unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00003379 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
3380}
3381
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003382unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00003383 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
3384}
Steve Naroff2d4d6292009-08-31 14:26:51 +00003385
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003386unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00003387 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
3388}
3389
Douglas Gregor97b98722010-01-19 23:20:36 +00003390unsigned clang_isExpression(enum CXCursorKind K) {
3391 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
3392}
3393
3394unsigned clang_isStatement(enum CXCursorKind K) {
3395 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
3396}
3397
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00003398unsigned clang_isTranslationUnit(enum CXCursorKind K) {
3399 return K == CXCursor_TranslationUnit;
3400}
3401
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003402unsigned clang_isPreprocessing(enum CXCursorKind K) {
3403 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
3404}
3405
Ted Kremenekad6eff62010-03-08 21:17:29 +00003406unsigned clang_isUnexposed(enum CXCursorKind K) {
3407 switch (K) {
3408 case CXCursor_UnexposedDecl:
3409 case CXCursor_UnexposedExpr:
3410 case CXCursor_UnexposedStmt:
3411 case CXCursor_UnexposedAttr:
3412 return true;
3413 default:
3414 return false;
3415 }
3416}
3417
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003418CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00003419 return C.kind;
3420}
3421
Douglas Gregor98258af2010-01-18 22:46:11 +00003422CXSourceLocation clang_getCursorLocation(CXCursor C) {
3423 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003424 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003425 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003426 std::pair<ObjCInterfaceDecl *, SourceLocation> P
3427 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003428 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003429 }
3430
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003431 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003432 std::pair<ObjCProtocolDecl *, SourceLocation> P
3433 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003434 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003435 }
3436
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003437 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003438 std::pair<ObjCInterfaceDecl *, SourceLocation> P
3439 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003440 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003441 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003442
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003443 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003444 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003445 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003446 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00003447
3448 case CXCursor_TemplateRef: {
3449 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C);
3450 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3451 }
3452
Douglas Gregor69319002010-08-31 23:48:11 +00003453 case CXCursor_NamespaceRef: {
3454 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
3455 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3456 }
3457
Douglas Gregora67e03f2010-09-09 21:42:20 +00003458 case CXCursor_MemberRef: {
3459 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
3460 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3461 }
3462
Ted Kremenek3064ef92010-08-27 21:34:58 +00003463 case CXCursor_CXXBaseSpecifier: {
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003464 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
3465 if (!BaseSpec)
3466 return clang_getNullLocation();
3467
3468 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
3469 return cxloc::translateSourceLocation(getCursorContext(C),
3470 TSInfo->getTypeLoc().getBeginLoc());
3471
3472 return cxloc::translateSourceLocation(getCursorContext(C),
3473 BaseSpec->getSourceRange().getBegin());
Ted Kremenek3064ef92010-08-27 21:34:58 +00003474 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003475
Douglas Gregor36897b02010-09-10 00:22:18 +00003476 case CXCursor_LabelRef: {
3477 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
3478 return cxloc::translateSourceLocation(getCursorContext(C), P.second);
3479 }
3480
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003481 case CXCursor_OverloadedDeclRef:
3482 return cxloc::translateSourceLocation(getCursorContext(C),
3483 getCursorOverloadedDeclRef(C).second);
3484
Douglas Gregorf46034a2010-01-18 23:41:10 +00003485 default:
3486 // FIXME: Need a way to enumerate all non-reference cases.
3487 llvm_unreachable("Missed a reference kind");
3488 }
Douglas Gregor98258af2010-01-18 22:46:11 +00003489 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003490
3491 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003492 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00003493 getLocationFromExpr(getCursorExpr(C)));
3494
Douglas Gregor36897b02010-09-10 00:22:18 +00003495 if (clang_isStatement(C.kind))
3496 return cxloc::translateSourceLocation(getCursorContext(C),
3497 getCursorStmt(C)->getLocStart());
3498
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003499 if (C.kind == CXCursor_PreprocessingDirective) {
3500 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
3501 return cxloc::translateSourceLocation(getCursorContext(C), L);
3502 }
Douglas Gregor48072312010-03-18 15:23:44 +00003503
3504 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00003505 SourceLocation L
3506 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
Douglas Gregor48072312010-03-18 15:23:44 +00003507 return cxloc::translateSourceLocation(getCursorContext(C), L);
3508 }
Douglas Gregor572feb22010-03-18 18:04:21 +00003509
3510 if (C.kind == CXCursor_MacroDefinition) {
3511 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
3512 return cxloc::translateSourceLocation(getCursorContext(C), L);
3513 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00003514
3515 if (C.kind == CXCursor_InclusionDirective) {
3516 SourceLocation L
3517 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
3518 return cxloc::translateSourceLocation(getCursorContext(C), L);
3519 }
3520
Ted Kremenek9a700d22010-05-12 06:16:13 +00003521 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
Douglas Gregor5352ac02010-01-28 00:27:43 +00003522 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00003523
Douglas Gregorf46034a2010-01-18 23:41:10 +00003524 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003525 SourceLocation Loc = D->getLocation();
3526 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
3527 Loc = Class->getClassLoc();
Ted Kremenek007a7c92010-11-01 23:26:51 +00003528 // FIXME: Multiple variables declared in a single declaration
3529 // currently lack the information needed to correctly determine their
3530 // ranges when accounting for the type-specifier. We use context
3531 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3532 // and if so, whether it is the first decl.
3533 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3534 if (!cxcursor::isFirstInDeclGroup(C))
3535 Loc = VD->getLocation();
3536 }
3537
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00003538 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00003539}
Douglas Gregora7bde202010-01-19 00:34:46 +00003540
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003541} // end extern "C"
3542
3543static SourceRange getRawCursorExtent(CXCursor C) {
Douglas Gregora7bde202010-01-19 00:34:46 +00003544 if (clang_isReference(C.kind)) {
3545 switch (C.kind) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003546 case CXCursor_ObjCSuperClassRef:
3547 return getCursorObjCSuperClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003548
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003549 case CXCursor_ObjCProtocolRef:
3550 return getCursorObjCProtocolRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003551
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003552 case CXCursor_ObjCClassRef:
3553 return getCursorObjCClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003554
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003555 case CXCursor_TypeRef:
3556 return getCursorTypeRef(C).second;
Douglas Gregor0b36e612010-08-31 20:37:03 +00003557
3558 case CXCursor_TemplateRef:
3559 return getCursorTemplateRef(C).second;
3560
Douglas Gregor69319002010-08-31 23:48:11 +00003561 case CXCursor_NamespaceRef:
3562 return getCursorNamespaceRef(C).second;
Douglas Gregora67e03f2010-09-09 21:42:20 +00003563
3564 case CXCursor_MemberRef:
3565 return getCursorMemberRef(C).second;
3566
Ted Kremenek3064ef92010-08-27 21:34:58 +00003567 case CXCursor_CXXBaseSpecifier:
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003568 return getCursorCXXBaseSpecifier(C)->getSourceRange();
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003569
Douglas Gregor36897b02010-09-10 00:22:18 +00003570 case CXCursor_LabelRef:
3571 return getCursorLabelRef(C).second;
3572
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003573 case CXCursor_OverloadedDeclRef:
3574 return getCursorOverloadedDeclRef(C).second;
3575
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003576 default:
3577 // FIXME: Need a way to enumerate all non-reference cases.
3578 llvm_unreachable("Missed a reference kind");
Douglas Gregora7bde202010-01-19 00:34:46 +00003579 }
3580 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003581
3582 if (clang_isExpression(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003583 return getCursorExpr(C)->getSourceRange();
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003584
3585 if (clang_isStatement(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003586 return getCursorStmt(C)->getSourceRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003587
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003588 if (C.kind == CXCursor_PreprocessingDirective)
3589 return cxcursor::getCursorPreprocessingDirective(C);
Douglas Gregor48072312010-03-18 15:23:44 +00003590
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003591 if (C.kind == CXCursor_MacroInstantiation)
3592 return cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
Douglas Gregor572feb22010-03-18 18:04:21 +00003593
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003594 if (C.kind == CXCursor_MacroDefinition)
3595 return cxcursor::getCursorMacroDefinition(C)->getSourceRange();
Douglas Gregorecdcb882010-10-20 22:00:55 +00003596
3597 if (C.kind == CXCursor_InclusionDirective)
3598 return cxcursor::getCursorInclusionDirective(C)->getSourceRange();
3599
Ted Kremenek007a7c92010-11-01 23:26:51 +00003600 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3601 Decl *D = cxcursor::getCursorDecl(C);
3602 SourceRange R = D->getSourceRange();
3603 // FIXME: Multiple variables declared in a single declaration
3604 // currently lack the information needed to correctly determine their
3605 // ranges when accounting for the type-specifier. We use context
3606 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3607 // and if so, whether it is the first decl.
3608 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3609 if (!cxcursor::isFirstInDeclGroup(C))
3610 R.setBegin(VD->getLocation());
3611 }
3612 return R;
3613 }
Douglas Gregor66537982010-11-17 17:14:07 +00003614 return SourceRange();
3615}
3616
3617/// \brief Retrieves the "raw" cursor extent, which is then extended to include
3618/// the decl-specifier-seq for declarations.
3619static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
3620 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3621 Decl *D = cxcursor::getCursorDecl(C);
3622 SourceRange R = D->getSourceRange();
Douglas Gregor66537982010-11-17 17:14:07 +00003623
Douglas Gregor2494dd02011-03-01 01:34:45 +00003624 // Adjust the start of the location for declarations preceded by
3625 // declaration specifiers.
3626 SourceLocation StartLoc;
3627 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
3628 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
3629 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3630 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
3631 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
3632 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3633 }
3634
3635 if (StartLoc.isValid() && R.getBegin().isValid() &&
3636 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
3637 R.setBegin(StartLoc);
3638
3639 // FIXME: Multiple variables declared in a single declaration
3640 // currently lack the information needed to correctly determine their
3641 // ranges when accounting for the type-specifier. We use context
3642 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3643 // and if so, whether it is the first decl.
3644 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3645 if (!cxcursor::isFirstInDeclGroup(C))
3646 R.setBegin(VD->getLocation());
Douglas Gregor66537982010-11-17 17:14:07 +00003647 }
3648
3649 return R;
3650 }
3651
3652 return getRawCursorExtent(C);
3653}
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003654
3655extern "C" {
3656
3657CXSourceRange clang_getCursorExtent(CXCursor C) {
3658 SourceRange R = getRawCursorExtent(C);
3659 if (R.isInvalid())
Douglas Gregor5352ac02010-01-28 00:27:43 +00003660 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003661
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003662 return cxloc::translateSourceRange(getCursorContext(C), R);
Douglas Gregora7bde202010-01-19 00:34:46 +00003663}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003664
3665CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003666 if (clang_isInvalid(C.kind))
3667 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003668
Ted Kremeneka60ed472010-11-16 08:15:36 +00003669 CXTranslationUnit tu = getCursorTU(C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003670 if (clang_isDeclaration(C.kind)) {
3671 Decl *D = getCursorDecl(C);
3672 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003673 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003674 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003675 return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003676 if (ObjCForwardProtocolDecl *Protocols
3677 = dyn_cast<ObjCForwardProtocolDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003678 return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu);
Douglas Gregore3c60a72010-11-17 00:13:31 +00003679 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
3680 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
3681 return MakeCXCursor(Property, tu);
3682
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003683 return C;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003684 }
3685
Douglas Gregor97b98722010-01-19 23:20:36 +00003686 if (clang_isExpression(C.kind)) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003687 Expr *E = getCursorExpr(C);
3688 Decl *D = getDeclFromExpr(E);
Douglas Gregor97b98722010-01-19 23:20:36 +00003689 if (D)
Ted Kremeneka60ed472010-11-16 08:15:36 +00003690 return MakeCXCursor(D, tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003691
3692 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003693 return MakeCursorOverloadedDeclRef(Ovl, tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003694
Douglas Gregor97b98722010-01-19 23:20:36 +00003695 return clang_getNullCursor();
3696 }
3697
Douglas Gregor36897b02010-09-10 00:22:18 +00003698 if (clang_isStatement(C.kind)) {
3699 Stmt *S = getCursorStmt(C);
3700 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003701 return MakeCXCursor(Goto->getLabel()->getStmt(), getCursorDecl(C), tu);
Douglas Gregor36897b02010-09-10 00:22:18 +00003702
3703 return clang_getNullCursor();
3704 }
3705
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003706 if (C.kind == CXCursor_MacroInstantiation) {
3707 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003708 return MakeMacroDefinitionCursor(Def, tu);
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003709 }
3710
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003711 if (!clang_isReference(C.kind))
3712 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003713
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003714 switch (C.kind) {
3715 case CXCursor_ObjCSuperClassRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003716 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003717
3718 case CXCursor_ObjCProtocolRef: {
Ted Kremeneka60ed472010-11-16 08:15:36 +00003719 return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003720
3721 case CXCursor_ObjCClassRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003722 return MakeCXCursor(getCursorObjCClassRef(C).first, tu );
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003723
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003724 case CXCursor_TypeRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003725 return MakeCXCursor(getCursorTypeRef(C).first, tu );
Douglas Gregor0b36e612010-08-31 20:37:03 +00003726
3727 case CXCursor_TemplateRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003728 return MakeCXCursor(getCursorTemplateRef(C).first, tu );
Douglas Gregor0b36e612010-08-31 20:37:03 +00003729
Douglas Gregor69319002010-08-31 23:48:11 +00003730 case CXCursor_NamespaceRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003731 return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
Douglas Gregor69319002010-08-31 23:48:11 +00003732
Douglas Gregora67e03f2010-09-09 21:42:20 +00003733 case CXCursor_MemberRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003734 return MakeCXCursor(getCursorMemberRef(C).first, tu );
Douglas Gregora67e03f2010-09-09 21:42:20 +00003735
Ted Kremenek3064ef92010-08-27 21:34:58 +00003736 case CXCursor_CXXBaseSpecifier: {
3737 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
3738 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003739 tu ));
Ted Kremenek3064ef92010-08-27 21:34:58 +00003740 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003741
Douglas Gregor36897b02010-09-10 00:22:18 +00003742 case CXCursor_LabelRef:
3743 // FIXME: We end up faking the "parent" declaration here because we
3744 // don't want to make CXCursor larger.
3745 return MakeCXCursor(getCursorLabelRef(C).first,
Ted Kremeneka60ed472010-11-16 08:15:36 +00003746 static_cast<ASTUnit*>(tu->TUData)->getASTContext()
3747 .getTranslationUnitDecl(),
3748 tu);
Douglas Gregor36897b02010-09-10 00:22:18 +00003749
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003750 case CXCursor_OverloadedDeclRef:
3751 return C;
3752
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003753 default:
3754 // We would prefer to enumerate all non-reference cursor kinds here.
3755 llvm_unreachable("Unhandled reference cursor kind");
3756 break;
3757 }
3758 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003759
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003760 return clang_getNullCursor();
3761}
3762
Douglas Gregorb6998662010-01-19 19:34:47 +00003763CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003764 if (clang_isInvalid(C.kind))
3765 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003766
Ted Kremeneka60ed472010-11-16 08:15:36 +00003767 CXTranslationUnit TU = getCursorTU(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003768
Douglas Gregorb6998662010-01-19 19:34:47 +00003769 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00003770 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00003771 C = clang_getCursorReferenced(C);
3772 WasReference = true;
3773 }
3774
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003775 if (C.kind == CXCursor_MacroInstantiation)
3776 return clang_getCursorReferenced(C);
3777
Douglas Gregorb6998662010-01-19 19:34:47 +00003778 if (!clang_isDeclaration(C.kind))
3779 return clang_getNullCursor();
3780
3781 Decl *D = getCursorDecl(C);
3782 if (!D)
3783 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003784
Douglas Gregorb6998662010-01-19 19:34:47 +00003785 switch (D->getKind()) {
3786 // Declaration kinds that don't really separate the notions of
3787 // declaration and definition.
3788 case Decl::Namespace:
3789 case Decl::Typedef:
3790 case Decl::TemplateTypeParm:
3791 case Decl::EnumConstant:
3792 case Decl::Field:
Benjamin Kramerd9811462010-11-21 14:11:41 +00003793 case Decl::IndirectField:
Douglas Gregorb6998662010-01-19 19:34:47 +00003794 case Decl::ObjCIvar:
3795 case Decl::ObjCAtDefsField:
3796 case Decl::ImplicitParam:
3797 case Decl::ParmVar:
3798 case Decl::NonTypeTemplateParm:
3799 case Decl::TemplateTemplateParm:
3800 case Decl::ObjCCategoryImpl:
3801 case Decl::ObjCImplementation:
Abramo Bagnara6206d532010-06-05 05:09:32 +00003802 case Decl::AccessSpec:
Douglas Gregorb6998662010-01-19 19:34:47 +00003803 case Decl::LinkageSpec:
3804 case Decl::ObjCPropertyImpl:
3805 case Decl::FileScopeAsm:
3806 case Decl::StaticAssert:
3807 case Decl::Block:
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003808 case Decl::Label: // FIXME: Is this right??
Douglas Gregorb6998662010-01-19 19:34:47 +00003809 return C;
3810
3811 // Declaration kinds that don't make any sense here, but are
3812 // nonetheless harmless.
3813 case Decl::TranslationUnit:
Douglas Gregorb6998662010-01-19 19:34:47 +00003814 break;
3815
3816 // Declaration kinds for which the definition is not resolvable.
3817 case Decl::UnresolvedUsingTypename:
3818 case Decl::UnresolvedUsingValue:
3819 break;
3820
3821 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003822 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003823 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003824
3825 case Decl::NamespaceAlias:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003826 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003827
3828 case Decl::Enum:
3829 case Decl::Record:
3830 case Decl::CXXRecord:
3831 case Decl::ClassTemplateSpecialization:
3832 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00003833 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003834 return MakeCXCursor(Def, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003835 return clang_getNullCursor();
3836
3837 case Decl::Function:
3838 case Decl::CXXMethod:
3839 case Decl::CXXConstructor:
3840 case Decl::CXXDestructor:
3841 case Decl::CXXConversion: {
3842 const FunctionDecl *Def = 0;
3843 if (cast<FunctionDecl>(D)->getBody(Def))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003844 return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003845 return clang_getNullCursor();
3846 }
3847
3848 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00003849 // Ask the variable if it has a definition.
3850 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003851 return MakeCXCursor(Def, TU);
Sebastian Redl31310a22010-02-01 20:16:42 +00003852 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00003853 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003854
Douglas Gregorb6998662010-01-19 19:34:47 +00003855 case Decl::FunctionTemplate: {
3856 const FunctionDecl *Def = 0;
3857 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003858 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003859 return clang_getNullCursor();
3860 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003861
Douglas Gregorb6998662010-01-19 19:34:47 +00003862 case Decl::ClassTemplate: {
3863 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00003864 ->getDefinition())
Douglas Gregor0b36e612010-08-31 20:37:03 +00003865 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003866 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003867 return clang_getNullCursor();
3868 }
3869
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003870 case Decl::Using:
3871 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003872 D->getLocation(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003873
3874 case Decl::UsingShadow:
3875 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003876 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003877 TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003878
3879 case Decl::ObjCMethod: {
3880 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
3881 if (Method->isThisDeclarationADefinition())
3882 return C;
3883
3884 // Dig out the method definition in the associated
3885 // @implementation, if we have it.
3886 // FIXME: The ASTs should make finding the definition easier.
3887 if (ObjCInterfaceDecl *Class
3888 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
3889 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
3890 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
3891 Method->isInstanceMethod()))
3892 if (Def->isThisDeclarationADefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003893 return MakeCXCursor(Def, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003894
3895 return clang_getNullCursor();
3896 }
3897
3898 case Decl::ObjCCategory:
3899 if (ObjCCategoryImplDecl *Impl
3900 = cast<ObjCCategoryDecl>(D)->getImplementation())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003901 return MakeCXCursor(Impl, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003902 return clang_getNullCursor();
3903
3904 case Decl::ObjCProtocol:
3905 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
3906 return C;
3907 return clang_getNullCursor();
3908
3909 case Decl::ObjCInterface:
3910 // There are two notions of a "definition" for an Objective-C
3911 // class: the interface and its implementation. When we resolved a
3912 // reference to an Objective-C class, produce the @interface as
3913 // the definition; when we were provided with the interface,
3914 // produce the @implementation as the definition.
3915 if (WasReference) {
3916 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
3917 return C;
3918 } else if (ObjCImplementationDecl *Impl
3919 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003920 return MakeCXCursor(Impl, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003921 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003922
Douglas Gregorb6998662010-01-19 19:34:47 +00003923 case Decl::ObjCProperty:
3924 // FIXME: We don't really know where to find the
3925 // ObjCPropertyImplDecls that implement this property.
3926 return clang_getNullCursor();
3927
3928 case Decl::ObjCCompatibleAlias:
3929 if (ObjCInterfaceDecl *Class
3930 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
3931 if (!Class->isForwardDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003932 return MakeCXCursor(Class, TU);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003933
Douglas Gregorb6998662010-01-19 19:34:47 +00003934 return clang_getNullCursor();
3935
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003936 case Decl::ObjCForwardProtocol:
3937 return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003938 D->getLocation(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003939
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003940 case Decl::ObjCClass:
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00003941 return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003942 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003943
3944 case Decl::Friend:
3945 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003946 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003947 return clang_getNullCursor();
3948
3949 case Decl::FriendTemplate:
3950 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003951 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003952 return clang_getNullCursor();
3953 }
3954
3955 return clang_getNullCursor();
3956}
3957
3958unsigned clang_isCursorDefinition(CXCursor C) {
3959 if (!clang_isDeclaration(C.kind))
3960 return 0;
3961
3962 return clang_getCursorDefinition(C) == C;
3963}
3964
Douglas Gregor1a9d0502010-11-19 23:44:15 +00003965CXCursor clang_getCanonicalCursor(CXCursor C) {
3966 if (!clang_isDeclaration(C.kind))
3967 return C;
3968
3969 if (Decl *D = getCursorDecl(C))
3970 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
3971
3972 return C;
3973}
3974
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003975unsigned clang_getNumOverloadedDecls(CXCursor C) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00003976 if (C.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003977 return 0;
3978
3979 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
3980 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
3981 return E->getNumDecls();
3982
3983 if (OverloadedTemplateStorage *S
3984 = Storage.dyn_cast<OverloadedTemplateStorage*>())
3985 return S->size();
3986
3987 Decl *D = Storage.get<Decl*>();
3988 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00003989 return Using->shadow_size();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003990 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
3991 return Classes->size();
3992 if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D))
3993 return Protocols->protocol_size();
3994
3995 return 0;
3996}
3997
3998CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00003999 if (cursor.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004000 return clang_getNullCursor();
4001
4002 if (index >= clang_getNumOverloadedDecls(cursor))
4003 return clang_getNullCursor();
4004
Ted Kremeneka60ed472010-11-16 08:15:36 +00004005 CXTranslationUnit TU = getCursorTU(cursor);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004006 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
4007 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
Ted Kremeneka60ed472010-11-16 08:15:36 +00004008 return MakeCXCursor(E->decls_begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004009
4010 if (OverloadedTemplateStorage *S
4011 = Storage.dyn_cast<OverloadedTemplateStorage*>())
Ted Kremeneka60ed472010-11-16 08:15:36 +00004012 return MakeCXCursor(S->begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004013
4014 Decl *D = Storage.get<Decl*>();
4015 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
4016 // FIXME: This is, unfortunately, linear time.
4017 UsingDecl::shadow_iterator Pos = Using->shadow_begin();
4018 std::advance(Pos, index);
Ted Kremeneka60ed472010-11-16 08:15:36 +00004019 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004020 }
4021
4022 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004023 return MakeCXCursor(Classes->begin()[index].getInterface(), TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004024
4025 if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004026 return MakeCXCursor(Protocols->protocol_begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004027
4028 return clang_getNullCursor();
4029}
4030
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00004031void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00004032 const char **startBuf,
4033 const char **endBuf,
4034 unsigned *startLine,
4035 unsigned *startColumn,
4036 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00004037 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00004038 assert(getCursorDecl(C) && "CXCursor has null decl");
4039 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00004040 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
4041 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004042
Steve Naroff4ade6d62009-09-23 17:52:52 +00004043 SourceManager &SM = FD->getASTContext().getSourceManager();
4044 *startBuf = SM.getCharacterData(Body->getLBracLoc());
4045 *endBuf = SM.getCharacterData(Body->getRBracLoc());
4046 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
4047 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
4048 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
4049 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
4050}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004051
Douglas Gregor0a812cf2010-02-18 23:07:20 +00004052void clang_enableStackTraces(void) {
4053 llvm::sys::PrintStackTraceOnErrorSignal();
4054}
4055
Daniel Dunbar995aaf92010-11-04 01:26:29 +00004056void clang_executeOnThread(void (*fn)(void*), void *user_data,
4057 unsigned stack_size) {
4058 llvm::llvm_execute_on_thread(fn, user_data, stack_size);
4059}
4060
Ted Kremenekfb480492010-01-13 21:46:36 +00004061} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00004062
Ted Kremenekfb480492010-01-13 21:46:36 +00004063//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004064// Token-based Operations.
4065//===----------------------------------------------------------------------===//
4066
4067/* CXToken layout:
4068 * int_data[0]: a CXTokenKind
4069 * int_data[1]: starting token location
4070 * int_data[2]: token length
4071 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004072 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004073 * otherwise unused.
4074 */
4075extern "C" {
4076
4077CXTokenKind clang_getTokenKind(CXToken CXTok) {
4078 return static_cast<CXTokenKind>(CXTok.int_data[0]);
4079}
4080
4081CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
4082 switch (clang_getTokenKind(CXTok)) {
4083 case CXToken_Identifier:
4084 case CXToken_Keyword:
4085 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004086 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
4087 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004088
4089 case CXToken_Literal: {
4090 // We have stashed the starting pointer in the ptr_data field. Use it.
4091 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004092 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004093 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004094
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004095 case CXToken_Punctuation:
4096 case CXToken_Comment:
4097 break;
4098 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004099
4100 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004101 // deconstructing the source location.
Ted Kremeneka60ed472010-11-16 08:15:36 +00004102 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004103 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004104 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004105
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004106 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
4107 std::pair<FileID, unsigned> LocInfo
4108 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +00004109 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004110 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004111 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
4112 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00004113 return createCXString("");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004114
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004115 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004116}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004117
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004118CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004119 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004120 if (!CXXUnit)
4121 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004122
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004123 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
4124 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4125}
4126
4127CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004128 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor5352ac02010-01-28 00:27:43 +00004129 if (!CXXUnit)
4130 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004131
4132 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004133 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4134}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004135
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004136void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
4137 CXToken **Tokens, unsigned *NumTokens) {
4138 if (Tokens)
4139 *Tokens = 0;
4140 if (NumTokens)
4141 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004142
Ted Kremeneka60ed472010-11-16 08:15:36 +00004143 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004144 if (!CXXUnit || !Tokens || !NumTokens)
4145 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004146
Douglas Gregorbdf60622010-03-05 21:16:25 +00004147 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4148
Daniel Dunbar85b988f2010-02-14 08:31:57 +00004149 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004150 if (R.isInvalid())
4151 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004152
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004153 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4154 std::pair<FileID, unsigned> BeginLocInfo
4155 = SourceMgr.getDecomposedLoc(R.getBegin());
4156 std::pair<FileID, unsigned> EndLocInfo
4157 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004158
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004159 // Cannot tokenize across files.
4160 if (BeginLocInfo.first != EndLocInfo.first)
4161 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004162
4163 // Create a lexer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004164 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004165 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004166 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
Douglas Gregor47a3fcd2010-03-16 20:26:15 +00004167 if (Invalid)
4168 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +00004169
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004170 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4171 CXXUnit->getASTContext().getLangOptions(),
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004172 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004173 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004174
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004175 // Lex tokens until we hit the end of the range.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004176 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004177 llvm::SmallVector<CXToken, 32> CXTokens;
4178 Token Tok;
David Chisnall096428b2010-10-13 21:44:48 +00004179 bool previousWasAt = false;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004180 do {
4181 // Lex the next token
4182 Lex.LexFromRawLexer(Tok);
4183 if (Tok.is(tok::eof))
4184 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004185
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004186 // Initialize the CXToken.
4187 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004188
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004189 // - Common fields
4190 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
4191 CXTok.int_data[2] = Tok.getLength();
4192 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004193
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004194 // - Kind-specific fields
4195 if (Tok.isLiteral()) {
4196 CXTok.int_data[0] = CXToken_Literal;
4197 CXTok.ptr_data = (void *)Tok.getLiteralData();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004198 } else if (Tok.is(tok::raw_identifier)) {
Douglas Gregoraea67db2010-03-15 22:54:52 +00004199 // Lookup the identifier to determine whether we have a keyword.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004200 IdentifierInfo *II
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004201 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004202
David Chisnall096428b2010-10-13 21:44:48 +00004203 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004204 CXTok.int_data[0] = CXToken_Keyword;
4205 }
4206 else {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004207 CXTok.int_data[0] = Tok.is(tok::identifier)
4208 ? CXToken_Identifier
4209 : CXToken_Keyword;
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004210 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004211 CXTok.ptr_data = II;
4212 } else if (Tok.is(tok::comment)) {
4213 CXTok.int_data[0] = CXToken_Comment;
4214 CXTok.ptr_data = 0;
4215 } else {
4216 CXTok.int_data[0] = CXToken_Punctuation;
4217 CXTok.ptr_data = 0;
4218 }
4219 CXTokens.push_back(CXTok);
David Chisnall096428b2010-10-13 21:44:48 +00004220 previousWasAt = Tok.is(tok::at);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004221 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004222
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004223 if (CXTokens.empty())
4224 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004225
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004226 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
4227 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
4228 *NumTokens = CXTokens.size();
4229}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004230
Ted Kremenek6db61092010-05-05 00:55:15 +00004231void clang_disposeTokens(CXTranslationUnit TU,
4232 CXToken *Tokens, unsigned NumTokens) {
4233 free(Tokens);
4234}
4235
4236} // end: extern "C"
4237
4238//===----------------------------------------------------------------------===//
4239// Token annotation APIs.
4240//===----------------------------------------------------------------------===//
4241
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004242typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004243static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4244 CXCursor parent,
4245 CXClientData client_data);
Ted Kremenek6db61092010-05-05 00:55:15 +00004246namespace {
4247class AnnotateTokensWorker {
4248 AnnotateTokensData &Annotated;
Ted Kremenek11949cb2010-05-05 00:55:17 +00004249 CXToken *Tokens;
4250 CXCursor *Cursors;
4251 unsigned NumTokens;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004252 unsigned TokIdx;
Douglas Gregor4419b672010-10-21 06:10:04 +00004253 unsigned PreprocessingTokIdx;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004254 CursorVisitor AnnotateVis;
4255 SourceManager &SrcMgr;
4256
4257 bool MoreTokens() const { return TokIdx < NumTokens; }
4258 unsigned NextToken() const { return TokIdx; }
4259 void AdvanceToken() { ++TokIdx; }
4260 SourceLocation GetTokenLoc(unsigned tokI) {
4261 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
4262 }
4263
Ted Kremenek6db61092010-05-05 00:55:15 +00004264public:
Ted Kremenek11949cb2010-05-05 00:55:17 +00004265 AnnotateTokensWorker(AnnotateTokensData &annotated,
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004266 CXToken *tokens, CXCursor *cursors, unsigned numTokens,
Ted Kremeneka60ed472010-11-16 08:15:36 +00004267 CXTranslationUnit tu, SourceRange RegionOfInterest)
Ted Kremenek11949cb2010-05-05 00:55:17 +00004268 : Annotated(annotated), Tokens(tokens), Cursors(cursors),
Douglas Gregor4419b672010-10-21 06:10:04 +00004269 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004270 AnnotateVis(tu,
4271 AnnotateTokensVisitor, this,
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004272 Decl::MaxPCHLevel, RegionOfInterest),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004273 SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()) {}
Ted Kremenek11949cb2010-05-05 00:55:17 +00004274
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004275 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
Ted Kremenek6db61092010-05-05 00:55:15 +00004276 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004277 void AnnotateTokens(CXCursor parent);
Ted Kremenekab979612010-11-11 08:05:23 +00004278 void AnnotateTokens() {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004279 AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU()));
Ted Kremenekab979612010-11-11 08:05:23 +00004280 }
Ted Kremenek6db61092010-05-05 00:55:15 +00004281};
4282}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004283
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004284void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) {
4285 // Walk the AST within the region of interest, annotating tokens
4286 // along the way.
4287 VisitChildren(parent);
Ted Kremenek11949cb2010-05-05 00:55:17 +00004288
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004289 for (unsigned I = 0 ; I < TokIdx ; ++I) {
4290 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
Douglas Gregor4419b672010-10-21 06:10:04 +00004291 if (Pos != Annotated.end() &&
4292 (clang_isInvalid(Cursors[I].kind) ||
4293 Pos->second.kind != CXCursor_PreprocessingDirective))
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004294 Cursors[I] = Pos->second;
4295 }
4296
4297 // Finish up annotating any tokens left.
4298 if (!MoreTokens())
4299 return;
4300
4301 const CXCursor &C = clang_getNullCursor();
4302 for (unsigned I = TokIdx ; I < NumTokens ; ++I) {
4303 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
4304 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second;
Ted Kremenek11949cb2010-05-05 00:55:17 +00004305 }
4306}
4307
Ted Kremenek6db61092010-05-05 00:55:15 +00004308enum CXChildVisitResult
Douglas Gregor4419b672010-10-21 06:10:04 +00004309AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004310 CXSourceLocation Loc = clang_getCursorLocation(cursor);
Douglas Gregor4419b672010-10-21 06:10:04 +00004311 SourceRange cursorRange = getRawCursorExtent(cursor);
Douglas Gregor81d3c042010-11-01 20:13:04 +00004312 if (cursorRange.isInvalid())
4313 return CXChildVisit_Recurse;
4314
Douglas Gregor4419b672010-10-21 06:10:04 +00004315 if (clang_isPreprocessing(cursor.kind)) {
4316 // For macro instantiations, just note where the beginning of the macro
4317 // instantiation occurs.
4318 if (cursor.kind == CXCursor_MacroInstantiation) {
4319 Annotated[Loc.int_data] = cursor;
4320 return CXChildVisit_Recurse;
4321 }
4322
Douglas Gregor4419b672010-10-21 06:10:04 +00004323 // Items in the preprocessing record are kept separate from items in
4324 // declarations, so we keep a separate token index.
4325 unsigned SavedTokIdx = TokIdx;
4326 TokIdx = PreprocessingTokIdx;
4327
4328 // Skip tokens up until we catch up to the beginning of the preprocessing
4329 // entry.
4330 while (MoreTokens()) {
4331 const unsigned I = NextToken();
4332 SourceLocation TokLoc = GetTokenLoc(I);
4333 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4334 case RangeBefore:
4335 AdvanceToken();
4336 continue;
4337 case RangeAfter:
4338 case RangeOverlap:
4339 break;
4340 }
4341 break;
4342 }
4343
4344 // Look at all of the tokens within this range.
4345 while (MoreTokens()) {
4346 const unsigned I = NextToken();
4347 SourceLocation TokLoc = GetTokenLoc(I);
4348 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4349 case RangeBefore:
4350 assert(0 && "Infeasible");
4351 case RangeAfter:
4352 break;
4353 case RangeOverlap:
4354 Cursors[I] = cursor;
4355 AdvanceToken();
4356 continue;
4357 }
4358 break;
4359 }
4360
4361 // Save the preprocessing token index; restore the non-preprocessing
4362 // token index.
4363 PreprocessingTokIdx = TokIdx;
4364 TokIdx = SavedTokIdx;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004365 return CXChildVisit_Recurse;
4366 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004367
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004368 if (cursorRange.isInvalid())
4369 return CXChildVisit_Continue;
Ted Kremeneka333c662010-05-12 05:29:33 +00004370
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004371 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data);
4372
Ted Kremeneka333c662010-05-12 05:29:33 +00004373 // Adjust the annotated range based specific declarations.
4374 const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
4375 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) {
Ted Kremenek23173d72010-05-18 21:09:07 +00004376 Decl *D = cxcursor::getCursorDecl(cursor);
4377 // Don't visit synthesized ObjC methods, since they have no syntatic
4378 // representation in the source.
4379 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
4380 if (MD->isSynthesized())
4381 return CXChildVisit_Continue;
4382 }
Douglas Gregor2494dd02011-03-01 01:34:45 +00004383
4384 SourceLocation StartLoc;
Ted Kremenek23173d72010-05-18 21:09:07 +00004385 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
Douglas Gregor2494dd02011-03-01 01:34:45 +00004386 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
4387 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
4388 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
4389 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
4390 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
Ted Kremeneka333c662010-05-12 05:29:33 +00004391 }
Douglas Gregor2494dd02011-03-01 01:34:45 +00004392
4393 if (StartLoc.isValid() && L.isValid() &&
4394 SrcMgr.isBeforeInTranslationUnit(StartLoc, L))
4395 cursorRange.setBegin(StartLoc);
Ted Kremeneka333c662010-05-12 05:29:33 +00004396 }
Douglas Gregor81d3c042010-11-01 20:13:04 +00004397
Ted Kremenek3f404602010-08-14 01:14:06 +00004398 // If the location of the cursor occurs within a macro instantiation, record
4399 // the spelling location of the cursor in our annotation map. We can then
4400 // paper over the token labelings during a post-processing step to try and
4401 // get cursor mappings for tokens that are the *arguments* of a macro
4402 // instantiation.
4403 if (L.isMacroID()) {
4404 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding();
4405 // Only invalidate the old annotation if it isn't part of a preprocessing
4406 // directive. Here we assume that the default construction of CXCursor
4407 // results in CXCursor.kind being an initialized value (i.e., 0). If
4408 // this isn't the case, we can fix by doing lookup + insertion.
Douglas Gregor4419b672010-10-21 06:10:04 +00004409
Ted Kremenek3f404602010-08-14 01:14:06 +00004410 CXCursor &oldC = Annotated[rawEncoding];
4411 if (!clang_isPreprocessing(oldC.kind))
4412 oldC = cursor;
4413 }
4414
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004415 const enum CXCursorKind K = clang_getCursorKind(parent);
4416 const CXCursor updateC =
Ted Kremenekd8b0a842010-08-25 22:16:02 +00004417 (clang_isInvalid(K) || K == CXCursor_TranslationUnit)
4418 ? clang_getNullCursor() : parent;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004419
4420 while (MoreTokens()) {
4421 const unsigned I = NextToken();
4422 SourceLocation TokLoc = GetTokenLoc(I);
4423 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4424 case RangeBefore:
4425 Cursors[I] = updateC;
4426 AdvanceToken();
4427 continue;
4428 case RangeAfter:
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004429 case RangeOverlap:
4430 break;
4431 }
4432 break;
4433 }
4434
4435 // Visit children to get their cursor information.
4436 const unsigned BeforeChildren = NextToken();
4437 VisitChildren(cursor);
4438 const unsigned AfterChildren = NextToken();
4439
4440 // Adjust 'Last' to the last token within the extent of the cursor.
4441 while (MoreTokens()) {
4442 const unsigned I = NextToken();
4443 SourceLocation TokLoc = GetTokenLoc(I);
4444 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4445 case RangeBefore:
4446 assert(0 && "Infeasible");
4447 case RangeAfter:
4448 break;
4449 case RangeOverlap:
4450 Cursors[I] = updateC;
4451 AdvanceToken();
4452 continue;
4453 }
4454 break;
4455 }
4456 const unsigned Last = NextToken();
Ted Kremenek6db61092010-05-05 00:55:15 +00004457
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004458 // Scan the tokens that are at the beginning of the cursor, but are not
4459 // capture by the child cursors.
4460
4461 // For AST elements within macros, rely on a post-annotate pass to
4462 // to correctly annotate the tokens with cursors. Otherwise we can
4463 // get confusing results of having tokens that map to cursors that really
4464 // are expanded by an instantiation.
4465 if (L.isMacroID())
4466 cursor = clang_getNullCursor();
4467
4468 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
4469 if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
4470 break;
Douglas Gregor4419b672010-10-21 06:10:04 +00004471
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004472 Cursors[I] = cursor;
4473 }
4474 // Scan the tokens that are at the end of the cursor, but are not captured
4475 // but the child cursors.
4476 for (unsigned I = AfterChildren; I != Last; ++I)
4477 Cursors[I] = cursor;
4478
4479 TokIdx = Last;
4480 return CXChildVisit_Continue;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004481}
4482
Ted Kremenek6db61092010-05-05 00:55:15 +00004483static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4484 CXCursor parent,
4485 CXClientData client_data) {
4486 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
4487}
4488
Ted Kremenekab979612010-11-11 08:05:23 +00004489// This gets run a separate thread to avoid stack blowout.
4490static void runAnnotateTokensWorker(void *UserData) {
4491 ((AnnotateTokensWorker*)UserData)->AnnotateTokens();
4492}
4493
Ted Kremenek6db61092010-05-05 00:55:15 +00004494extern "C" {
4495
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004496void clang_annotateTokens(CXTranslationUnit TU,
4497 CXToken *Tokens, unsigned NumTokens,
4498 CXCursor *Cursors) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004499
4500 if (NumTokens == 0 || !Tokens || !Cursors)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004501 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004502
Douglas Gregor4419b672010-10-21 06:10:04 +00004503 // Any token we don't specifically annotate will have a NULL cursor.
4504 CXCursor C = clang_getNullCursor();
4505 for (unsigned I = 0; I != NumTokens; ++I)
4506 Cursors[I] = C;
4507
Ted Kremeneka60ed472010-11-16 08:15:36 +00004508 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor4419b672010-10-21 06:10:04 +00004509 if (!CXXUnit)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004510 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004511
Douglas Gregorbdf60622010-03-05 21:16:25 +00004512 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004513
Douglas Gregor0396f462010-03-19 05:22:59 +00004514 // Determine the region of interest, which contains all of the tokens.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004515 SourceRange RegionOfInterest;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004516 RegionOfInterest.setBegin(cxloc::translateSourceLocation(
4517 clang_getTokenLocation(TU, Tokens[0])));
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00004518 RegionOfInterest.setEnd(cxloc::translateSourceLocation(
4519 clang_getTokenLocation(TU,
4520 Tokens[NumTokens - 1])));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004521
Douglas Gregor0396f462010-03-19 05:22:59 +00004522 // A mapping from the source locations found when re-lexing or traversing the
4523 // region of interest to the corresponding cursors.
4524 AnnotateTokensData Annotated;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004525
4526 // Relex the tokens within the source range to look for preprocessing
Douglas Gregor0396f462010-03-19 05:22:59 +00004527 // directives.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004528 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4529 std::pair<FileID, unsigned> BeginLocInfo
4530 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
4531 std::pair<FileID, unsigned> EndLocInfo
4532 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004533
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004534 llvm::StringRef Buffer;
Douglas Gregor0396f462010-03-19 05:22:59 +00004535 bool Invalid = false;
4536 if (BeginLocInfo.first == EndLocInfo.first &&
4537 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
4538 !Invalid) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004539 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4540 CXXUnit->getASTContext().getLangOptions(),
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004541 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004542 Buffer.end());
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004543 Lex.SetCommentRetentionState(true);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004544
4545 // Lex tokens in raw mode until we hit the end of the range, to avoid
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004546 // entering #includes or expanding macros.
Douglas Gregor48072312010-03-18 15:23:44 +00004547 while (true) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004548 Token Tok;
4549 Lex.LexFromRawLexer(Tok);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004550
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004551 reprocess:
4552 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
4553 // We have found a preprocessing directive. Gobble it up so that we
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00004554 // don't see it while preprocessing these tokens later, but keep track
4555 // of all of the token locations inside this preprocessing directive so
4556 // that we can annotate them appropriately.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004557 //
4558 // FIXME: Some simple tests here could identify macro definitions and
4559 // #undefs, to provide specific cursor kinds for those.
4560 std::vector<SourceLocation> Locations;
4561 do {
4562 Locations.push_back(Tok.getLocation());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004563 Lex.LexFromRawLexer(Tok);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004564 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004565
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004566 using namespace cxcursor;
4567 CXCursor Cursor
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004568 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
4569 Locations.back()),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004570 TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004571 for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
4572 Annotated[Locations[I].getRawEncoding()] = Cursor;
4573 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004574
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004575 if (Tok.isAtStartOfLine())
4576 goto reprocess;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004577
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004578 continue;
4579 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004580
Douglas Gregor48072312010-03-18 15:23:44 +00004581 if (Tok.is(tok::eof))
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004582 break;
4583 }
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004584 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004585
Douglas Gregor0396f462010-03-19 05:22:59 +00004586 // Annotate all of the source locations in the region of interest that map to
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004587 // a specific cursor.
4588 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
Ted Kremeneka60ed472010-11-16 08:15:36 +00004589 TU, RegionOfInterest);
Ted Kremenekab979612010-11-11 08:05:23 +00004590
4591 // Run the worker within a CrashRecoveryContext.
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004592 // FIXME: We use a ridiculous stack size here because the data-recursion
4593 // algorithm uses a large stack frame than the non-data recursive version,
4594 // and AnnotationTokensWorker currently transforms the data-recursion
4595 // algorithm back into a traditional recursion by explicitly calling
4596 // VisitChildren(). We will need to remove this explicit recursive call.
Ted Kremenekab979612010-11-11 08:05:23 +00004597 llvm::CrashRecoveryContext CRC;
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004598 if (!RunSafely(CRC, runAnnotateTokensWorker, &W,
4599 GetSafetyThreadStackSize() * 2)) {
Ted Kremenekab979612010-11-11 08:05:23 +00004600 fprintf(stderr, "libclang: crash detected while annotating tokens\n");
4601 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004602}
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004603} // end: extern "C"
4604
4605//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00004606// Operations for querying linkage of a cursor.
4607//===----------------------------------------------------------------------===//
4608
4609extern "C" {
4610CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
Douglas Gregor0396f462010-03-19 05:22:59 +00004611 if (!clang_isDeclaration(cursor.kind))
4612 return CXLinkage_Invalid;
4613
Ted Kremenek16b42592010-03-03 06:36:57 +00004614 Decl *D = cxcursor::getCursorDecl(cursor);
4615 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
4616 switch (ND->getLinkage()) {
4617 case NoLinkage: return CXLinkage_NoLinkage;
4618 case InternalLinkage: return CXLinkage_Internal;
4619 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
4620 case ExternalLinkage: return CXLinkage_External;
4621 };
4622
4623 return CXLinkage_Invalid;
4624}
4625} // end: extern "C"
4626
4627//===----------------------------------------------------------------------===//
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004628// Operations for querying language of a cursor.
4629//===----------------------------------------------------------------------===//
4630
4631static CXLanguageKind getDeclLanguage(const Decl *D) {
4632 switch (D->getKind()) {
4633 default:
4634 break;
4635 case Decl::ImplicitParam:
4636 case Decl::ObjCAtDefsField:
4637 case Decl::ObjCCategory:
4638 case Decl::ObjCCategoryImpl:
4639 case Decl::ObjCClass:
4640 case Decl::ObjCCompatibleAlias:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004641 case Decl::ObjCForwardProtocol:
4642 case Decl::ObjCImplementation:
4643 case Decl::ObjCInterface:
4644 case Decl::ObjCIvar:
4645 case Decl::ObjCMethod:
4646 case Decl::ObjCProperty:
4647 case Decl::ObjCPropertyImpl:
4648 case Decl::ObjCProtocol:
4649 return CXLanguage_ObjC;
4650 case Decl::CXXConstructor:
4651 case Decl::CXXConversion:
4652 case Decl::CXXDestructor:
4653 case Decl::CXXMethod:
4654 case Decl::CXXRecord:
4655 case Decl::ClassTemplate:
4656 case Decl::ClassTemplatePartialSpecialization:
4657 case Decl::ClassTemplateSpecialization:
4658 case Decl::Friend:
4659 case Decl::FriendTemplate:
4660 case Decl::FunctionTemplate:
4661 case Decl::LinkageSpec:
4662 case Decl::Namespace:
4663 case Decl::NamespaceAlias:
4664 case Decl::NonTypeTemplateParm:
4665 case Decl::StaticAssert:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004666 case Decl::TemplateTemplateParm:
4667 case Decl::TemplateTypeParm:
4668 case Decl::UnresolvedUsingTypename:
4669 case Decl::UnresolvedUsingValue:
4670 case Decl::Using:
4671 case Decl::UsingDirective:
4672 case Decl::UsingShadow:
4673 return CXLanguage_CPlusPlus;
4674 }
4675
4676 return CXLanguage_C;
4677}
4678
4679extern "C" {
Douglas Gregor58ddb602010-08-23 23:00:57 +00004680
4681enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
4682 if (clang_isDeclaration(cursor.kind))
4683 if (Decl *D = cxcursor::getCursorDecl(cursor)) {
4684 if (D->hasAttr<UnavailableAttr>() ||
4685 (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()))
4686 return CXAvailability_Available;
4687
4688 if (D->hasAttr<DeprecatedAttr>())
4689 return CXAvailability_Deprecated;
4690 }
4691
4692 return CXAvailability_Available;
4693}
4694
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004695CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
4696 if (clang_isDeclaration(cursor.kind))
4697 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
4698
4699 return CXLanguage_Invalid;
4700}
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004701
4702 /// \brief If the given cursor is the "templated" declaration
4703 /// descibing a class or function template, return the class or
4704 /// function template.
4705static Decl *maybeGetTemplateCursor(Decl *D) {
4706 if (!D)
4707 return 0;
4708
4709 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4710 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
4711 return FunTmpl;
4712
4713 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
4714 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
4715 return ClassTmpl;
4716
4717 return D;
4718}
4719
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004720CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
4721 if (clang_isDeclaration(cursor.kind)) {
4722 if (Decl *D = getCursorDecl(cursor)) {
4723 DeclContext *DC = D->getDeclContext();
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004724 if (!DC)
4725 return clang_getNullCursor();
4726
4727 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
4728 getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004729 }
4730 }
4731
4732 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
4733 if (Decl *D = getCursorDecl(cursor))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004734 return MakeCXCursor(D, getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004735 }
4736
4737 return clang_getNullCursor();
4738}
4739
4740CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
4741 if (clang_isDeclaration(cursor.kind)) {
4742 if (Decl *D = getCursorDecl(cursor)) {
4743 DeclContext *DC = D->getLexicalDeclContext();
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004744 if (!DC)
4745 return clang_getNullCursor();
4746
4747 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
4748 getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004749 }
4750 }
4751
4752 // FIXME: Note that we can't easily compute the lexical context of a
4753 // statement or expression, so we return nothing.
4754 return clang_getNullCursor();
4755}
4756
Douglas Gregor9f592342010-10-01 20:25:15 +00004757static void CollectOverriddenMethods(DeclContext *Ctx,
4758 ObjCMethodDecl *Method,
4759 llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) {
4760 if (!Ctx)
4761 return;
4762
4763 // If we have a class or category implementation, jump straight to the
4764 // interface.
4765 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
4766 return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods);
4767
4768 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
4769 if (!Container)
4770 return;
4771
4772 // Check whether we have a matching method at this level.
4773 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
4774 Method->isInstanceMethod()))
4775 if (Method != Overridden) {
4776 // We found an override at this level; there is no need to look
4777 // into other protocols or categories.
4778 Methods.push_back(Overridden);
4779 return;
4780 }
4781
4782 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4783 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
4784 PEnd = Protocol->protocol_end();
4785 P != PEnd; ++P)
4786 CollectOverriddenMethods(*P, Method, Methods);
4787 }
4788
4789 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
4790 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
4791 PEnd = Category->protocol_end();
4792 P != PEnd; ++P)
4793 CollectOverriddenMethods(*P, Method, Methods);
4794 }
4795
4796 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
4797 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
4798 PEnd = Interface->protocol_end();
4799 P != PEnd; ++P)
4800 CollectOverriddenMethods(*P, Method, Methods);
4801
4802 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
4803 Category; Category = Category->getNextClassCategory())
4804 CollectOverriddenMethods(Category, Method, Methods);
4805
4806 // We only look into the superclass if we haven't found anything yet.
4807 if (Methods.empty())
4808 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
4809 return CollectOverriddenMethods(Super, Method, Methods);
4810 }
4811}
4812
4813void clang_getOverriddenCursors(CXCursor cursor,
4814 CXCursor **overridden,
4815 unsigned *num_overridden) {
4816 if (overridden)
4817 *overridden = 0;
4818 if (num_overridden)
4819 *num_overridden = 0;
4820 if (!overridden || !num_overridden)
4821 return;
4822
4823 if (!clang_isDeclaration(cursor.kind))
4824 return;
4825
4826 Decl *D = getCursorDecl(cursor);
4827 if (!D)
4828 return;
4829
4830 // Handle C++ member functions.
Ted Kremeneka60ed472010-11-16 08:15:36 +00004831 CXTranslationUnit TU = getCursorTU(cursor);
Douglas Gregor9f592342010-10-01 20:25:15 +00004832 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
4833 *num_overridden = CXXMethod->size_overridden_methods();
4834 if (!*num_overridden)
4835 return;
4836
4837 *overridden = new CXCursor [*num_overridden];
4838 unsigned I = 0;
4839 for (CXXMethodDecl::method_iterator
4840 M = CXXMethod->begin_overridden_methods(),
4841 MEnd = CXXMethod->end_overridden_methods();
4842 M != MEnd; (void)++M, ++I)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004843 (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU);
Douglas Gregor9f592342010-10-01 20:25:15 +00004844 return;
4845 }
4846
4847 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
4848 if (!Method)
4849 return;
4850
4851 // Handle Objective-C methods.
4852 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
4853 CollectOverriddenMethods(Method->getDeclContext(), Method, Methods);
4854
4855 if (Methods.empty())
4856 return;
4857
4858 *num_overridden = Methods.size();
4859 *overridden = new CXCursor [Methods.size()];
4860 for (unsigned I = 0, N = Methods.size(); I != N; ++I)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004861 (*overridden)[I] = MakeCXCursor(Methods[I], TU);
Douglas Gregor9f592342010-10-01 20:25:15 +00004862}
4863
4864void clang_disposeOverriddenCursors(CXCursor *overridden) {
4865 delete [] overridden;
4866}
4867
Douglas Gregorecdcb882010-10-20 22:00:55 +00004868CXFile clang_getIncludedFile(CXCursor cursor) {
4869 if (cursor.kind != CXCursor_InclusionDirective)
4870 return 0;
4871
4872 InclusionDirective *ID = getCursorInclusionDirective(cursor);
4873 return (void *)ID->getFile();
4874}
4875
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004876} // end: extern "C"
4877
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004878
4879//===----------------------------------------------------------------------===//
4880// C++ AST instrospection.
4881//===----------------------------------------------------------------------===//
4882
4883extern "C" {
4884unsigned clang_CXXMethod_isStatic(CXCursor C) {
4885 if (!clang_isDeclaration(C.kind))
4886 return 0;
Douglas Gregor49f6f542010-08-31 22:12:17 +00004887
4888 CXXMethodDecl *Method = 0;
4889 Decl *D = cxcursor::getCursorDecl(C);
4890 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
4891 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
4892 else
4893 Method = dyn_cast_or_null<CXXMethodDecl>(D);
4894 return (Method && Method->isStatic()) ? 1 : 0;
Ted Kremenek40b492a2010-05-17 20:12:45 +00004895}
Ted Kremenekb12903e2010-05-18 22:32:15 +00004896
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004897} // end: extern "C"
4898
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004899//===----------------------------------------------------------------------===//
Ted Kremenek95f33552010-08-26 01:42:22 +00004900// Attribute introspection.
4901//===----------------------------------------------------------------------===//
4902
4903extern "C" {
4904CXType clang_getIBOutletCollectionType(CXCursor C) {
4905 if (C.kind != CXCursor_IBOutletCollectionAttr)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004906 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
Ted Kremenek95f33552010-08-26 01:42:22 +00004907
4908 IBOutletCollectionAttr *A =
4909 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
4910
Ted Kremeneka60ed472010-11-16 08:15:36 +00004911 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
Ted Kremenek95f33552010-08-26 01:42:22 +00004912}
4913} // end: extern "C"
4914
4915//===----------------------------------------------------------------------===//
Ted Kremenek04bb7162010-01-22 22:44:15 +00004916// Misc. utility functions.
4917//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004918
Daniel Dunbarabdce7a2010-11-05 17:21:46 +00004919/// Default to using an 8 MB stack size on "safety" threads.
4920static unsigned SafetyStackThreadSize = 8 << 20;
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00004921
4922namespace clang {
4923
4924bool RunSafely(llvm::CrashRecoveryContext &CRC,
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004925 void (*Fn)(void*), void *UserData,
4926 unsigned Size) {
4927 if (!Size)
4928 Size = GetSafetyThreadStackSize();
4929 if (Size)
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00004930 return CRC.RunSafelyOnThread(Fn, UserData, Size);
4931 return CRC.RunSafely(Fn, UserData);
4932}
4933
4934unsigned GetSafetyThreadStackSize() {
4935 return SafetyStackThreadSize;
4936}
4937
4938void SetSafetyThreadStackSize(unsigned Value) {
4939 SafetyStackThreadSize = Value;
4940}
4941
4942}
4943
Ted Kremenek04bb7162010-01-22 22:44:15 +00004944extern "C" {
4945
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00004946CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004947 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00004948}
4949
4950} // end: extern "C"