blob: ac57a6941c34a8534a9e6af432a294158d71b0b1 [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 Kremenek95f33552010-08-26 01:42:22 +000017#include "CXType.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000018#include "CXSourceLocation.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000019#include "CIndexDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000020
Ted Kremenek04bb7162010-01-22 22:44:15 +000021#include "clang/Basic/Version.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000022
Steve Naroff50398192009-08-28 15:28:48 +000023#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000024#include "clang/AST/StmtVisitor.h"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000025#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000026#include "clang/Basic/Diagnostic.h"
27#include "clang/Frontend/ASTUnit.h"
28#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000029#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000030#include "clang/Lex/Lexer.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000032#include "clang/Lex/Preprocessor.h"
Douglas Gregora67e03f2010-09-09 21:42:20 +000033#include "llvm/ADT/STLExtras.h"
Ted Kremenekd8c370c2010-11-02 23:10:24 +000034#include "llvm/ADT/Optional.h"
35#include "clang/Analysis/Support/SaveAndRestore.h"
Daniel Dunbarc7df4f32010-08-18 18:43:14 +000036#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar48615ff2010-10-08 19:30:33 +000037#include "llvm/Support/PrettyStackTrace.h"
Douglas Gregor02465752009-10-16 21:24:31 +000038#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor358559d2010-10-02 22:49:11 +000039#include "llvm/Support/raw_ostream.h"
Douglas Gregor7a07fcb2010-08-09 21:00:09 +000040#include "llvm/Support/Timer.h"
Douglas Gregor8c8d5412010-09-24 21:18:36 +000041#include "llvm/System/Mutex.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000042#include "llvm/System/Program.h"
Douglas Gregor0a812cf2010-02-18 23:07:20 +000043#include "llvm/System/Signals.h"
Douglas Gregor8c8d5412010-09-24 21:18:36 +000044#include "llvm/System/Threading.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000045
Benjamin Kramerc2a98162010-03-13 21:22:49 +000046// Needed to define L_TMPNAM on some systems.
47#include <cstdio>
48
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
Douglas Gregor33e9abd2010-01-22 19:49:59 +000053/// \brief The result of comparing two source ranges.
54enum RangeComparisonResult {
55 /// \brief Either the ranges overlap or one of the ranges is invalid.
56 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000057
Douglas Gregor33e9abd2010-01-22 19:49:59 +000058 /// \brief The first range ends before the second range starts.
59 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000060
Douglas Gregor33e9abd2010-01-22 19:49:59 +000061 /// \brief The first range starts after the second range ends.
62 RangeAfter
63};
64
Ted Kremenekf0e23e82010-02-17 00:41:40 +000065/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +000066/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +000067static RangeComparisonResult RangeCompare(SourceManager &SM,
68 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +000069 SourceRange R2) {
70 assert(R1.isValid() && "First range is invalid?");
71 assert(R2.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000072 if (R1.getEnd() != R2.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000073 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000074 return RangeBefore;
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000075 if (R2.getEnd() != R1.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000076 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000077 return RangeAfter;
78 return RangeOverlap;
79}
80
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000081/// \brief Determine if a source location falls within, before, or after a
82/// a given source range.
83static RangeComparisonResult LocationCompare(SourceManager &SM,
84 SourceLocation L, SourceRange R) {
85 assert(R.isValid() && "First range is invalid?");
86 assert(L.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000087 if (L == R.getBegin() || L == R.getEnd())
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000088 return RangeOverlap;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000089 if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
90 return RangeBefore;
91 if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
92 return RangeAfter;
93 return RangeOverlap;
94}
95
Daniel Dunbar76dd3c22010-02-14 01:47:29 +000096/// \brief Translate a Clang source range into a CIndex source range.
97///
98/// Clang internally represents ranges where the end location points to the
99/// start of the token at the end. However, for external clients it is more
100/// useful to have a CXSourceRange be a proper half-open interval. This routine
101/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000102CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000103 const LangOptions &LangOpts,
Chris Lattner0a76aae2010-06-18 22:45:06 +0000104 const CharSourceRange &R) {
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000105 // We want the last character in this location, so we will adjust the
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000106 // location accordingly.
107 // FIXME: How do do this with a macro instantiation location?
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000108 SourceLocation EndLoc = R.getEnd();
Chris Lattner0a76aae2010-06-18 22:45:06 +0000109 if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000110 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000111 EndLoc = EndLoc.getFileLocWithOffset(Length);
112 }
113
114 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
115 R.getBegin().getRawEncoding(),
116 EndLoc.getRawEncoding() };
117 return Result;
118}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000119
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000120//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000121// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000122//===----------------------------------------------------------------------===//
123
Steve Naroff89922f82009-08-31 00:59:03 +0000124namespace {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000125
Douglas Gregorb1373d02010-01-20 20:59:29 +0000126// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000127class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregora59e3902010-01-21 23:27:09 +0000128 public TypeLocVisitor<CursorVisitor, bool>,
129 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000130{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000131 /// \brief The translation unit we are traversing.
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000132 ASTUnit *TU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000133
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000134 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000135 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000136
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000137 /// \brief The declaration that serves at the parent of any statement or
138 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000139 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000140
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000141 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000142 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000143
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000144 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000145 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000146
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000147 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
148 // to the visitor. Declarations with a PCH level greater than this value will
149 // be suppressed.
150 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000151
152 /// \brief When valid, a source range to which the cursor should restrict
153 /// its search.
154 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000155
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000156 // FIXME: Eventually remove. This part of a hack to support proper
157 // iteration over all Decls contained lexically within an ObjC container.
158 DeclContext::decl_iterator *DI_current;
159 DeclContext::decl_iterator DE_current;
160
Douglas Gregorb1373d02010-01-20 20:59:29 +0000161 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000162 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregora59e3902010-01-21 23:27:09 +0000163 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000164
165 /// \brief Determine whether this particular source range comes before, comes
166 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000167 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000168 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000169 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
170
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000171 class SetParentRAII {
172 CXCursor &Parent;
173 Decl *&StmtParent;
174 CXCursor OldParent;
175
176 public:
177 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
178 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
179 {
180 Parent = NewParent;
181 if (clang_isDeclaration(Parent.kind))
182 StmtParent = getCursorDecl(Parent);
183 }
184
185 ~SetParentRAII() {
186 Parent = OldParent;
187 if (clang_isDeclaration(Parent.kind))
188 StmtParent = getCursorDecl(Parent);
189 }
190 };
191
Steve Naroff89922f82009-08-31 00:59:03 +0000192public:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000193 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
194 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000195 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000196 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000197 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest),
198 DI_current(0)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000199 {
200 Parent.kind = CXCursor_NoDeclFound;
201 Parent.data[0] = 0;
202 Parent.data[1] = 0;
203 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000204 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000205 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000206
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000207 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000208
209 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
210 getPreprocessedEntities();
211
Douglas Gregorb1373d02010-01-20 20:59:29 +0000212 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000213
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000214 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000215 bool VisitAttributes(Decl *D);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000216 bool VisitBlockDecl(BlockDecl *B);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000217 bool VisitCXXRecordDecl(CXXRecordDecl *D);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000218 llvm::Optional<bool> shouldVisitCursor(CXCursor C);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000219 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000220 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
221 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000222 bool VisitTagDecl(TagDecl *D);
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000223 bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D);
Douglas Gregor74dbe642010-08-31 19:31:58 +0000224 bool VisitClassTemplatePartialSpecializationDecl(
225 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000226 bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000227 bool VisitEnumConstantDecl(EnumConstantDecl *D);
228 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
229 bool VisitFunctionDecl(FunctionDecl *ND);
230 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000231 bool VisitVarDecl(VarDecl *);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000232 bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000233 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +0000234 bool VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000235 bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000236 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
237 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
238 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
239 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
Ted Kremenek23173d72010-05-18 21:09:07 +0000240 bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD);
Ted Kremenek79758f62010-02-18 22:36:18 +0000241 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
242 bool VisitObjCImplDecl(ObjCImplDecl *D);
243 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
244 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000245 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
246 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
247 bool VisitObjCClassDecl(ObjCClassDecl *D);
Ted Kremeneka0536d82010-05-07 01:04:29 +0000248 bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
Ted Kremenek8f06e0e2010-05-06 23:38:21 +0000249 bool VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +0000250 bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000251 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor7e242562010-09-01 19:52:22 +0000252 bool VisitUsingDecl(UsingDecl *D);
253 bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
254 bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000255
Douglas Gregor01829d32010-08-31 14:41:23 +0000256 // Name visitor
257 bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000258 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range);
Douglas Gregor01829d32010-08-31 14:41:23 +0000259
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000260 // Template visitors
261 bool VisitTemplateParameters(const TemplateParameterList *Params);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000262 bool VisitTemplateName(TemplateName Name, SourceLocation Loc);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000263 bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL);
264
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000265 // Type visitors
Douglas Gregor01829d32010-08-31 14:41:23 +0000266 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000267 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000268 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000269 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
270 bool VisitTagTypeLoc(TagTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000271 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000272 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
John McCallc12c5bb2010-05-15 11:32:37 +0000273 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000274 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
275 bool VisitPointerTypeLoc(PointerTypeLoc TL);
276 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
277 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
278 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
279 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
Douglas Gregor01829d32010-08-31 14:41:23 +0000280 bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000281 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000282 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000283 // FIXME: Implement visitors here when the unimplemented TypeLocs get
284 // implemented
285 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
286 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000287
Douglas Gregora59e3902010-01-21 23:27:09 +0000288 // Statement visitors
289 bool VisitStmt(Stmt *S);
290 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregor36897b02010-09-10 00:22:18 +0000291 bool VisitGotoStmt(GotoStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000292 bool VisitIfStmt(IfStmt *S);
293 bool VisitSwitchStmt(SwitchStmt *S);
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000294 bool VisitCaseStmt(CaseStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000295 bool VisitWhileStmt(WhileStmt *S);
296 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000297
Douglas Gregor336fd812010-01-23 00:40:08 +0000298 // Expression visitors
Douglas Gregor8947a752010-09-02 20:35:02 +0000299 bool VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor6cd24e22010-07-29 00:26:18 +0000300 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000301 bool VisitBlockExpr(BlockExpr *B);
Douglas Gregor336fd812010-01-23 00:40:08 +0000302 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000303 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
Douglas Gregorc2350e52010-03-08 16:40:19 +0000304 bool VisitObjCMessageExpr(ObjCMessageExpr *E);
Douglas Gregor81d34662010-04-20 15:39:42 +0000305 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000306 bool VisitOffsetOfExpr(OffsetOfExpr *E);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000307 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorfbb4c982010-09-02 21:07:44 +0000308 bool VisitMemberExpr(MemberExpr *E);
Douglas Gregor36897b02010-09-10 00:22:18 +0000309 bool VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor648220e2010-08-10 15:02:34 +0000310 bool VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
311 bool VisitVAArgExpr(VAArgExpr *E);
Douglas Gregorfa2e26f2010-09-09 23:28:23 +0000312 bool VisitInitListExpr(InitListExpr *E);
313 bool VisitDesignatedInitExpr(DesignatedInitExpr *E);
Douglas Gregor94802292010-09-02 21:20:16 +0000314 bool VisitCXXTypeidExpr(CXXTypeidExpr *E);
Douglas Gregor3d37c0a2010-09-09 16:14:44 +0000315 bool VisitCXXUuidofExpr(CXXUuidofExpr *E);
Douglas Gregorda135b12010-09-02 21:38:13 +0000316 bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { return false; }
Douglas Gregorab6677e2010-09-08 00:15:04 +0000317 bool VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
318 bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000319 bool VisitCXXNewExpr(CXXNewExpr *E);
Douglas Gregor6f7198f2010-09-02 22:09:03 +0000320 bool VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Douglas Gregor3d37c0a2010-09-09 16:14:44 +0000321 bool VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
Douglas Gregor1f7b5902010-09-02 22:29:21 +0000322 bool VisitOverloadExpr(OverloadExpr *E);
Douglas Gregorbfebed22010-09-03 17:24:10 +0000323 bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000324 bool VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
Douglas Gregor25d63622010-09-03 17:35:34 +0000325 bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Douglas Gregoraaa80b22010-09-03 18:01:25 +0000326 bool VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000327};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000328
Ted Kremenekab188932010-01-05 19:32:54 +0000329} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000330
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000331static SourceRange getRawCursorExtent(CXCursor C);
332
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000333RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000334 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
335}
336
Douglas Gregorb1373d02010-01-20 20:59:29 +0000337/// \brief Visit the given cursor and, if requested by the visitor,
338/// its children.
339///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000340/// \param Cursor the cursor to visit.
341///
342/// \param CheckRegionOfInterest if true, then the caller already checked that
343/// this cursor is within the region of interest.
344///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000345/// \returns true if the visitation should be aborted, false if it
346/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000347bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000348 if (clang_isInvalid(Cursor.kind))
349 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000350
Douglas Gregorb1373d02010-01-20 20:59:29 +0000351 if (clang_isDeclaration(Cursor.kind)) {
352 Decl *D = getCursorDecl(Cursor);
353 assert(D && "Invalid declaration cursor");
354 if (D->getPCHLevel() > MaxPCHLevel)
355 return false;
356
357 if (D->isImplicit())
358 return false;
359 }
360
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000361 // If we have a range of interest, and this cursor doesn't intersect with it,
362 // we're done.
363 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000364 SourceRange Range = getRawCursorExtent(Cursor);
Daniel Dunbarf408f322010-02-14 08:32:05 +0000365 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000366 return false;
367 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000368
Douglas Gregorb1373d02010-01-20 20:59:29 +0000369 switch (Visitor(Cursor, Parent, ClientData)) {
370 case CXChildVisit_Break:
371 return true;
372
373 case CXChildVisit_Continue:
374 return false;
375
376 case CXChildVisit_Recurse:
377 return VisitChildren(Cursor);
378 }
379
Douglas Gregorfd643772010-01-25 16:45:46 +0000380 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000381}
382
Douglas Gregor788f5a12010-03-20 00:41:21 +0000383std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
384CursorVisitor::getPreprocessedEntities() {
385 PreprocessingRecord &PPRec
386 = *TU->getPreprocessor().getPreprocessingRecord();
387
388 bool OnlyLocalDecls
389 = !TU->isMainFileAST() && TU->getOnlyLocalDecls();
390
391 // There is no region of interest; we have to walk everything.
392 if (RegionOfInterest.isInvalid())
393 return std::make_pair(PPRec.begin(OnlyLocalDecls),
394 PPRec.end(OnlyLocalDecls));
395
396 // Find the file in which the region of interest lands.
397 SourceManager &SM = TU->getSourceManager();
398 std::pair<FileID, unsigned> Begin
399 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
400 std::pair<FileID, unsigned> End
401 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
402
403 // The region of interest spans files; we have to walk everything.
404 if (Begin.first != End.first)
405 return std::make_pair(PPRec.begin(OnlyLocalDecls),
406 PPRec.end(OnlyLocalDecls));
407
408 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
409 = TU->getPreprocessedEntitiesByFile();
410 if (ByFileMap.empty()) {
411 // Build the mapping from files to sets of preprocessed entities.
412 for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls),
413 EEnd = PPRec.end(OnlyLocalDecls);
414 E != EEnd; ++E) {
415 std::pair<FileID, unsigned> P
416 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
417 ByFileMap[P.first].push_back(*E);
418 }
419 }
420
421 return std::make_pair(ByFileMap[Begin.first].begin(),
422 ByFileMap[Begin.first].end());
423}
424
Douglas Gregorb1373d02010-01-20 20:59:29 +0000425/// \brief Visit the children of the given cursor.
426///
427/// \returns true if the visitation should be aborted, false if it
428/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000429bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000430 if (clang_isReference(Cursor.kind)) {
431 // By definition, references have no children.
432 return false;
433 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000434
435 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000436 // done.
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000437 SetParentRAII SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000438
Douglas Gregorb1373d02010-01-20 20:59:29 +0000439 if (clang_isDeclaration(Cursor.kind)) {
440 Decl *D = getCursorDecl(Cursor);
441 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000442 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000443 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000444
Douglas Gregora59e3902010-01-21 23:27:09 +0000445 if (clang_isStatement(Cursor.kind))
446 return Visit(getCursorStmt(Cursor));
447 if (clang_isExpression(Cursor.kind))
448 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000449
Douglas Gregorb1373d02010-01-20 20:59:29 +0000450 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000451 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000452 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
453 RegionOfInterest.isInvalid()) {
Douglas Gregoreb8837b2010-08-03 19:06:41 +0000454 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
455 TLEnd = CXXUnit->top_level_end();
456 TL != TLEnd; ++TL) {
457 if (Visit(MakeCXCursor(*TL, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000458 return true;
459 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000460 } else if (VisitDeclContext(
461 CXXUnit->getASTContext().getTranslationUnitDecl()))
462 return true;
Bob Wilson3178cb62010-03-19 03:57:57 +0000463
Douglas Gregor0396f462010-03-19 05:22:59 +0000464 // Walk the preprocessing record.
Daniel Dunbar8de30ff2010-03-20 01:11:56 +0000465 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000466 // FIXME: Once we have the ability to deserialize a preprocessing record,
467 // do so.
Douglas Gregor788f5a12010-03-20 00:41:21 +0000468 PreprocessingRecord::iterator E, EEnd;
469 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000470 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
471 if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit)))
472 return true;
Douglas Gregor788f5a12010-03-20 00:41:21 +0000473
Douglas Gregor0396f462010-03-19 05:22:59 +0000474 continue;
475 }
476
477 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
478 if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit)))
479 return true;
480
481 continue;
482 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000483
484 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
485 if (Visit(MakeInclusionDirectiveCursor(ID, CXXUnit)))
486 return true;
487
488 continue;
489 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000490 }
491 }
Douglas Gregor7b691f332010-01-20 21:13:59 +0000492 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000493 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000494
Douglas Gregorb1373d02010-01-20 20:59:29 +0000495 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000496 return false;
497}
498
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000499bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
John McCallfc929202010-06-04 22:33:30 +0000500 if (Visit(B->getSignatureAsWritten()->getTypeLoc()))
501 return true;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000502
Ted Kremenek664cffd2010-07-22 11:30:19 +0000503 if (Stmt *Body = B->getBody())
504 return Visit(MakeCXCursor(Body, StmtParent, TU));
505
506 return false;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000507}
508
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000509llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
510 if (RegionOfInterest.isValid()) {
511 SourceRange Range = getRawCursorExtent(Cursor);
512 if (Range.isInvalid())
513 return llvm::Optional<bool>();
Ted Kremenek09dfa372010-02-18 05:46:33 +0000514
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000515 switch (CompareRegionOfInterest(Range)) {
516 case RangeBefore:
517 // This declaration comes before the region of interest; skip it.
518 return llvm::Optional<bool>();
519
520 case RangeAfter:
521 // This declaration comes after the region of interest; we're done.
522 return false;
523
524 case RangeOverlap:
525 // This declaration overlaps the region of interest; visit it.
526 break;
527 }
528 }
529 return true;
530}
531
532bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
533 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
534
535 // FIXME: Eventually remove. This part of a hack to support proper
536 // iteration over all Decls contained lexically within an ObjC container.
537 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
538 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
539
540 for ( ; I != E; ++I) {
Ted Kremenek23173d72010-05-18 21:09:07 +0000541 Decl *D = *I;
542 if (D->getLexicalDeclContext() != DC)
543 continue;
Ted Kremenek23173d72010-05-18 21:09:07 +0000544 CXCursor Cursor = MakeCXCursor(D, TU);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000545 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
546 if (!V.hasValue())
547 continue;
548 if (!V.getValue())
549 return false;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000550 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000551 return true;
552 }
Douglas Gregorb1373d02010-01-20 20:59:29 +0000553 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000554}
555
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000556bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
557 llvm_unreachable("Translation units are visited directly by Visit()");
558 return false;
559}
560
561bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
562 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
563 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000564
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000565 return false;
566}
567
568bool CursorVisitor::VisitTagDecl(TagDecl *D) {
569 return VisitDeclContext(D);
570}
571
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000572bool CursorVisitor::VisitClassTemplateSpecializationDecl(
573 ClassTemplateSpecializationDecl *D) {
574 bool ShouldVisitBody = false;
575 switch (D->getSpecializationKind()) {
576 case TSK_Undeclared:
577 case TSK_ImplicitInstantiation:
578 // Nothing to visit
579 return false;
580
581 case TSK_ExplicitInstantiationDeclaration:
582 case TSK_ExplicitInstantiationDefinition:
583 break;
584
585 case TSK_ExplicitSpecialization:
586 ShouldVisitBody = true;
587 break;
588 }
589
590 // Visit the template arguments used in the specialization.
591 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
592 TypeLoc TL = SpecType->getTypeLoc();
593 if (TemplateSpecializationTypeLoc *TSTLoc
594 = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) {
595 for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I)
596 if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I)))
597 return true;
598 }
599 }
600
601 if (ShouldVisitBody && VisitCXXRecordDecl(D))
602 return true;
603
604 return false;
605}
606
Douglas Gregor74dbe642010-08-31 19:31:58 +0000607bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
608 ClassTemplatePartialSpecializationDecl *D) {
609 // FIXME: Visit the "outer" template parameter lists on the TagDecl
610 // before visiting these template parameters.
611 if (VisitTemplateParameters(D->getTemplateParameters()))
612 return true;
613
614 // Visit the partial specialization arguments.
615 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
616 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
617 if (VisitTemplateArgumentLoc(TemplateArgs[I]))
618 return true;
619
620 return VisitCXXRecordDecl(D);
621}
622
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000623bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregor84b51d72010-09-01 20:16:53 +0000624 // Visit the default argument.
625 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
626 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
627 if (Visit(DefArg->getTypeLoc()))
628 return true;
629
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000630 return false;
631}
632
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000633bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
634 if (Expr *Init = D->getInitExpr())
635 return Visit(MakeCXCursor(Init, StmtParent, TU));
636 return false;
637}
638
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000639bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
640 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
641 if (Visit(TSInfo->getTypeLoc()))
642 return true;
643
644 return false;
645}
646
Douglas Gregora67e03f2010-09-09 21:42:20 +0000647/// \brief Compare two base or member initializers based on their source order.
648static int CompareCXXBaseOrMemberInitializers(const void* Xp, const void *Yp) {
649 CXXBaseOrMemberInitializer const * const *X
650 = static_cast<CXXBaseOrMemberInitializer const * const *>(Xp);
651 CXXBaseOrMemberInitializer const * const *Y
652 = static_cast<CXXBaseOrMemberInitializer const * const *>(Yp);
653
654 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
655 return -1;
656 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
657 return 1;
658 else
659 return 0;
660}
661
Douglas Gregorb1373d02010-01-20 20:59:29 +0000662bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregor01829d32010-08-31 14:41:23 +0000663 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
664 // Visit the function declaration's syntactic components in the order
665 // written. This requires a bit of work.
666 TypeLoc TL = TSInfo->getTypeLoc();
667 FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
668
669 // If we have a function declared directly (without the use of a typedef),
670 // visit just the return type. Otherwise, just visit the function's type
671 // now.
672 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) ||
673 (!FTL && Visit(TL)))
674 return true;
675
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000676 // Visit the nested-name-specifier, if present.
677 if (NestedNameSpecifier *Qualifier = ND->getQualifier())
678 if (VisitNestedNameSpecifier(Qualifier, ND->getQualifierRange()))
679 return true;
Douglas Gregor01829d32010-08-31 14:41:23 +0000680
681 // Visit the declaration name.
682 if (VisitDeclarationNameInfo(ND->getNameInfo()))
683 return true;
684
685 // FIXME: Visit explicitly-specified template arguments!
686
687 // Visit the function parameters, if we have a function type.
688 if (FTL && VisitFunctionTypeLoc(*FTL, true))
689 return true;
690
691 // FIXME: Attributes?
692 }
693
Douglas Gregora67e03f2010-09-09 21:42:20 +0000694 if (ND->isThisDeclarationADefinition()) {
695 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
696 // Find the initializers that were written in the source.
697 llvm::SmallVector<CXXBaseOrMemberInitializer *, 4> WrittenInits;
698 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
699 IEnd = Constructor->init_end();
700 I != IEnd; ++I) {
701 if (!(*I)->isWritten())
702 continue;
703
704 WrittenInits.push_back(*I);
705 }
706
707 // Sort the initializers in source order
708 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
709 &CompareCXXBaseOrMemberInitializers);
710
711 // Visit the initializers in source order
712 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
713 CXXBaseOrMemberInitializer *Init = WrittenInits[I];
714 if (Init->isMemberInitializer()) {
715 if (Visit(MakeCursorMemberRef(Init->getMember(),
716 Init->getMemberLocation(), TU)))
717 return true;
718 } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) {
719 if (Visit(BaseInfo->getTypeLoc()))
720 return true;
721 }
722
723 // Visit the initializer value.
724 if (Expr *Initializer = Init->getInit())
725 if (Visit(MakeCXCursor(Initializer, ND, TU)))
726 return true;
727 }
728 }
729
730 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
731 return true;
732 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000733
Douglas Gregorb1373d02010-01-20 20:59:29 +0000734 return false;
735}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000736
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000737bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
738 if (VisitDeclaratorDecl(D))
739 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000740
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000741 if (Expr *BitWidth = D->getBitWidth())
742 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000743
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000744 return false;
745}
746
747bool CursorVisitor::VisitVarDecl(VarDecl *D) {
748 if (VisitDeclaratorDecl(D))
749 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000750
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000751 if (Expr *Init = D->getInit())
752 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000753
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000754 return false;
755}
756
Douglas Gregor84b51d72010-09-01 20:16:53 +0000757bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
758 if (VisitDeclaratorDecl(D))
759 return true;
760
761 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
762 if (Expr *DefArg = D->getDefaultArgument())
763 return Visit(MakeCXCursor(DefArg, StmtParent, TU));
764
765 return false;
766}
767
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000768bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
769 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
770 // before visiting these template parameters.
771 if (VisitTemplateParameters(D->getTemplateParameters()))
772 return true;
773
774 return VisitFunctionDecl(D->getTemplatedDecl());
775}
776
Douglas Gregor39d6f072010-08-31 19:02:00 +0000777bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
778 // FIXME: Visit the "outer" template parameter lists on the TagDecl
779 // before visiting these template parameters.
780 if (VisitTemplateParameters(D->getTemplateParameters()))
781 return true;
782
783 return VisitCXXRecordDecl(D->getTemplatedDecl());
784}
785
Douglas Gregor84b51d72010-09-01 20:16:53 +0000786bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
787 if (VisitTemplateParameters(D->getTemplateParameters()))
788 return true;
789
790 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
791 VisitTemplateArgumentLoc(D->getDefaultArgument()))
792 return true;
793
794 return false;
795}
796
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000797bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000798 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
799 if (Visit(TSInfo->getTypeLoc()))
800 return true;
801
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000802 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000803 PEnd = ND->param_end();
804 P != PEnd; ++P) {
805 if (Visit(MakeCXCursor(*P, TU)))
806 return true;
807 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000808
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000809 if (ND->isThisDeclarationADefinition() &&
810 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
811 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000812
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000813 return false;
814}
815
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000816namespace {
817 struct ContainerDeclsSort {
818 SourceManager &SM;
819 ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
820 bool operator()(Decl *A, Decl *B) {
821 SourceLocation L_A = A->getLocStart();
822 SourceLocation L_B = B->getLocStart();
823 assert(L_A.isValid() && L_B.isValid());
824 return SM.isBeforeInTranslationUnit(L_A, L_B);
825 }
826 };
827}
828
Douglas Gregora59e3902010-01-21 23:27:09 +0000829bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000830 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
831 // an @implementation can lexically contain Decls that are not properly
832 // nested in the AST. When we identify such cases, we need to retrofit
833 // this nesting here.
834 if (!DI_current)
835 return VisitDeclContext(D);
836
837 // Scan the Decls that immediately come after the container
838 // in the current DeclContext. If any fall within the
839 // container's lexical region, stash them into a vector
840 // for later processing.
841 llvm::SmallVector<Decl *, 24> DeclsInContainer;
842 SourceLocation EndLoc = D->getSourceRange().getEnd();
843 SourceManager &SM = TU->getSourceManager();
844 if (EndLoc.isValid()) {
845 DeclContext::decl_iterator next = *DI_current;
846 while (++next != DE_current) {
847 Decl *D_next = *next;
848 if (!D_next)
849 break;
850 SourceLocation L = D_next->getLocStart();
851 if (!L.isValid())
852 break;
853 if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
854 *DI_current = next;
855 DeclsInContainer.push_back(D_next);
856 continue;
857 }
858 break;
859 }
860 }
861
862 // The common case.
863 if (DeclsInContainer.empty())
864 return VisitDeclContext(D);
865
866 // Get all the Decls in the DeclContext, and sort them with the
867 // additional ones we've collected. Then visit them.
868 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
869 I!=E; ++I) {
870 Decl *subDecl = *I;
Ted Kremenek0582c892010-11-02 23:17:51 +0000871 if (!subDecl || subDecl->getLexicalDeclContext() != D ||
872 subDecl->getLocStart().isInvalid())
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000873 continue;
874 DeclsInContainer.push_back(subDecl);
875 }
876
877 // Now sort the Decls so that they appear in lexical order.
878 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
879 ContainerDeclsSort(SM));
880
881 // Now visit the decls.
882 for (llvm::SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
883 E = DeclsInContainer.end(); I != E; ++I) {
884 CXCursor Cursor = MakeCXCursor(*I, TU);
885 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
886 if (!V.hasValue())
887 continue;
888 if (!V.getValue())
889 return false;
890 if (Visit(Cursor, true))
891 return true;
892 }
893 return false;
Douglas Gregora59e3902010-01-21 23:27:09 +0000894}
895
Douglas Gregorb1373d02010-01-20 20:59:29 +0000896bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000897 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
898 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000899 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000900
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000901 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
902 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
903 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000904 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000905 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000906
Douglas Gregora59e3902010-01-21 23:27:09 +0000907 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000908}
909
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000910bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
911 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
912 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
913 E = PID->protocol_end(); I != E; ++I, ++PL)
914 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
915 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000916
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000917 return VisitObjCContainerDecl(PID);
918}
919
Ted Kremenek23173d72010-05-18 21:09:07 +0000920bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
Douglas Gregor83cb9422010-09-09 17:09:21 +0000921 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
John McCallfc929202010-06-04 22:33:30 +0000922 return true;
923
Ted Kremenek23173d72010-05-18 21:09:07 +0000924 // FIXME: This implements a workaround with @property declarations also being
925 // installed in the DeclContext for the @interface. Eventually this code
926 // should be removed.
927 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
928 if (!CDecl || !CDecl->IsClassExtension())
929 return false;
930
931 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
932 if (!ID)
933 return false;
934
935 IdentifierInfo *PropertyId = PD->getIdentifier();
936 ObjCPropertyDecl *prevDecl =
937 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
938
939 if (!prevDecl)
940 return false;
941
942 // Visit synthesized methods since they will be skipped when visiting
943 // the @interface.
944 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +0000945 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +0000946 if (Visit(MakeCXCursor(MD, TU)))
947 return true;
948
949 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +0000950 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +0000951 if (Visit(MakeCXCursor(MD, TU)))
952 return true;
953
954 return false;
955}
956
Douglas Gregorb1373d02010-01-20 20:59:29 +0000957bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000958 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000959 if (D->getSuperClass() &&
960 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000961 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000962 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000963 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000964
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000965 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
966 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
967 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000968 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000969 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000970
Douglas Gregora59e3902010-01-21 23:27:09 +0000971 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000972}
973
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000974bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
975 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000976}
977
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000978bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000979 // 'ID' could be null when dealing with invalid code.
980 if (ObjCInterfaceDecl *ID = D->getClassInterface())
981 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
982 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000983
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000984 return VisitObjCImplDecl(D);
985}
986
987bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
988#if 0
989 // Issue callbacks for super class.
990 // FIXME: No source location information!
991 if (D->getSuperClass() &&
992 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000993 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000994 TU)))
995 return true;
996#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000997
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000998 return VisitObjCImplDecl(D);
999}
1000
1001bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
1002 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1003 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
1004 E = D->protocol_end();
1005 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001006 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001007 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001008
1009 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001010}
1011
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001012bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
1013 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
1014 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
1015 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001016
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001017 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001018}
1019
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00001020bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1021 return VisitDeclContext(D);
1022}
1023
Douglas Gregor69319002010-08-31 23:48:11 +00001024bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001025 // Visit nested-name-specifier.
1026 if (NestedNameSpecifier *Qualifier = D->getQualifier())
1027 if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange()))
1028 return true;
Douglas Gregor69319002010-08-31 23:48:11 +00001029
1030 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1031 D->getTargetNameLoc(), TU));
1032}
1033
Douglas Gregor7e242562010-09-01 19:52:22 +00001034bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001035 // Visit nested-name-specifier.
1036 if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameDecl())
1037 if (VisitNestedNameSpecifier(Qualifier, D->getNestedNameRange()))
1038 return true;
Douglas Gregor7e242562010-09-01 19:52:22 +00001039
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001040 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1041 return true;
1042
Douglas Gregor7e242562010-09-01 19:52:22 +00001043 return VisitDeclarationNameInfo(D->getNameInfo());
1044}
1045
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001046bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001047 // Visit nested-name-specifier.
1048 if (NestedNameSpecifier *Qualifier = D->getQualifier())
1049 if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange()))
1050 return true;
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001051
1052 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1053 D->getIdentLocation(), TU));
1054}
1055
Douglas Gregor7e242562010-09-01 19:52:22 +00001056bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001057 // Visit nested-name-specifier.
1058 if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier())
1059 if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange()))
1060 return true;
1061
Douglas Gregor7e242562010-09-01 19:52:22 +00001062 return VisitDeclarationNameInfo(D->getNameInfo());
1063}
1064
1065bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1066 UnresolvedUsingTypenameDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001067 // Visit nested-name-specifier.
1068 if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier())
1069 if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange()))
1070 return true;
1071
Douglas Gregor7e242562010-09-01 19:52:22 +00001072 return false;
1073}
1074
Douglas Gregor01829d32010-08-31 14:41:23 +00001075bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1076 switch (Name.getName().getNameKind()) {
1077 case clang::DeclarationName::Identifier:
1078 case clang::DeclarationName::CXXLiteralOperatorName:
1079 case clang::DeclarationName::CXXOperatorName:
1080 case clang::DeclarationName::CXXUsingDirective:
1081 return false;
1082
1083 case clang::DeclarationName::CXXConstructorName:
1084 case clang::DeclarationName::CXXDestructorName:
1085 case clang::DeclarationName::CXXConversionFunctionName:
1086 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1087 return Visit(TSInfo->getTypeLoc());
1088 return false;
1089
1090 case clang::DeclarationName::ObjCZeroArgSelector:
1091 case clang::DeclarationName::ObjCOneArgSelector:
1092 case clang::DeclarationName::ObjCMultiArgSelector:
1093 // FIXME: Per-identifier location info?
1094 return false;
1095 }
1096
1097 return false;
1098}
1099
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001100bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1101 SourceRange Range) {
1102 // FIXME: This whole routine is a hack to work around the lack of proper
1103 // source information in nested-name-specifiers (PR5791). Since we do have
1104 // a beginning source location, we can visit the first component of the
1105 // nested-name-specifier, if it's a single-token component.
1106 if (!NNS)
1107 return false;
1108
1109 // Get the first component in the nested-name-specifier.
1110 while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1111 NNS = Prefix;
1112
1113 switch (NNS->getKind()) {
1114 case NestedNameSpecifier::Namespace:
1115 // FIXME: The token at this source location might actually have been a
1116 // namespace alias, but we don't model that. Lame!
1117 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1118 TU));
1119
1120 case NestedNameSpecifier::TypeSpec: {
1121 // If the type has a form where we know that the beginning of the source
1122 // range matches up with a reference cursor. Visit the appropriate reference
1123 // cursor.
1124 Type *T = NNS->getAsType();
1125 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1126 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1127 if (const TagType *Tag = dyn_cast<TagType>(T))
1128 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1129 if (const TemplateSpecializationType *TST
1130 = dyn_cast<TemplateSpecializationType>(T))
1131 return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1132 break;
1133 }
1134
1135 case NestedNameSpecifier::TypeSpecWithTemplate:
1136 case NestedNameSpecifier::Global:
1137 case NestedNameSpecifier::Identifier:
1138 break;
1139 }
1140
1141 return false;
1142}
1143
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001144bool CursorVisitor::VisitTemplateParameters(
1145 const TemplateParameterList *Params) {
1146 if (!Params)
1147 return false;
1148
1149 for (TemplateParameterList::const_iterator P = Params->begin(),
1150 PEnd = Params->end();
1151 P != PEnd; ++P) {
1152 if (Visit(MakeCXCursor(*P, TU)))
1153 return true;
1154 }
1155
1156 return false;
1157}
1158
Douglas Gregor0b36e612010-08-31 20:37:03 +00001159bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1160 switch (Name.getKind()) {
1161 case TemplateName::Template:
1162 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1163
1164 case TemplateName::OverloadedTemplate:
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001165 // Visit the overloaded template set.
1166 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1167 return true;
1168
Douglas Gregor0b36e612010-08-31 20:37:03 +00001169 return false;
1170
1171 case TemplateName::DependentTemplate:
1172 // FIXME: Visit nested-name-specifier.
1173 return false;
1174
1175 case TemplateName::QualifiedTemplate:
1176 // FIXME: Visit nested-name-specifier.
1177 return Visit(MakeCursorTemplateRef(
1178 Name.getAsQualifiedTemplateName()->getDecl(),
1179 Loc, TU));
1180 }
1181
1182 return false;
1183}
1184
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001185bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1186 switch (TAL.getArgument().getKind()) {
1187 case TemplateArgument::Null:
1188 case TemplateArgument::Integral:
1189 return false;
1190
1191 case TemplateArgument::Pack:
1192 // FIXME: Implement when variadic templates come along.
1193 return false;
1194
1195 case TemplateArgument::Type:
1196 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1197 return Visit(TSInfo->getTypeLoc());
1198 return false;
1199
1200 case TemplateArgument::Declaration:
1201 if (Expr *E = TAL.getSourceDeclExpression())
1202 return Visit(MakeCXCursor(E, StmtParent, TU));
1203 return false;
1204
1205 case TemplateArgument::Expression:
1206 if (Expr *E = TAL.getSourceExpression())
1207 return Visit(MakeCXCursor(E, StmtParent, TU));
1208 return false;
1209
1210 case TemplateArgument::Template:
Douglas Gregor0b36e612010-08-31 20:37:03 +00001211 return VisitTemplateName(TAL.getArgument().getAsTemplate(),
1212 TAL.getTemplateNameLoc());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001213 }
1214
1215 return false;
1216}
1217
Ted Kremeneka0536d82010-05-07 01:04:29 +00001218bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1219 return VisitDeclContext(D);
1220}
1221
Douglas Gregor01829d32010-08-31 14:41:23 +00001222bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1223 return Visit(TL.getUnqualifiedLoc());
1224}
1225
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001226bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1227 ASTContext &Context = TU->getASTContext();
1228
1229 // Some builtin types (such as Objective-C's "id", "sel", and
1230 // "Class") have associated declarations. Create cursors for those.
1231 QualType VisitType;
1232 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001233 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001234 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001235 case BuiltinType::Char_U:
1236 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001237 case BuiltinType::Char16:
1238 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001239 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001240 case BuiltinType::UInt:
1241 case BuiltinType::ULong:
1242 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001243 case BuiltinType::UInt128:
1244 case BuiltinType::Char_S:
1245 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001246 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001247 case BuiltinType::Short:
1248 case BuiltinType::Int:
1249 case BuiltinType::Long:
1250 case BuiltinType::LongLong:
1251 case BuiltinType::Int128:
1252 case BuiltinType::Float:
1253 case BuiltinType::Double:
1254 case BuiltinType::LongDouble:
1255 case BuiltinType::NullPtr:
1256 case BuiltinType::Overload:
1257 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001258 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001259
1260 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001261 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001262
Ted Kremenekc4174cc2010-02-18 18:52:18 +00001263 case BuiltinType::ObjCId:
1264 VisitType = Context.getObjCIdType();
1265 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001266
1267 case BuiltinType::ObjCClass:
1268 VisitType = Context.getObjCClassType();
1269 break;
1270
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001271 case BuiltinType::ObjCSel:
1272 VisitType = Context.getObjCSelType();
1273 break;
1274 }
1275
1276 if (!VisitType.isNull()) {
1277 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001278 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001279 TU));
1280 }
1281
1282 return false;
1283}
1284
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001285bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1286 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
1287}
1288
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001289bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1290 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1291}
1292
1293bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1294 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1295}
1296
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001297bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001298 // FIXME: We can't visit the template type parameter, because there's
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001299 // no context information with which we can match up the depth/index in the
1300 // type to the appropriate
1301 return false;
1302}
1303
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001304bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1305 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1306 return true;
1307
John McCallc12c5bb2010-05-15 11:32:37 +00001308 return false;
1309}
1310
1311bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1312 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1313 return true;
1314
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001315 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1316 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1317 TU)))
1318 return true;
1319 }
1320
1321 return false;
1322}
1323
1324bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCallc12c5bb2010-05-15 11:32:37 +00001325 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001326}
1327
1328bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1329 return Visit(TL.getPointeeLoc());
1330}
1331
1332bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1333 return Visit(TL.getPointeeLoc());
1334}
1335
1336bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1337 return Visit(TL.getPointeeLoc());
1338}
1339
1340bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001341 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001342}
1343
1344bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001345 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001346}
1347
Douglas Gregor01829d32010-08-31 14:41:23 +00001348bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1349 bool SkipResultType) {
1350 if (!SkipResultType && Visit(TL.getResultLoc()))
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001351 return true;
1352
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001353 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
Ted Kremenek5dbacb42010-04-07 00:27:13 +00001354 if (Decl *D = TL.getArg(I))
1355 if (Visit(MakeCXCursor(D, TU)))
1356 return true;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001357
1358 return false;
1359}
1360
1361bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1362 if (Visit(TL.getElementLoc()))
1363 return true;
1364
1365 if (Expr *Size = TL.getSizeExpr())
1366 return Visit(MakeCXCursor(Size, StmtParent, TU));
1367
1368 return false;
1369}
1370
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001371bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1372 TemplateSpecializationTypeLoc TL) {
Douglas Gregor0b36e612010-08-31 20:37:03 +00001373 // Visit the template name.
1374 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1375 TL.getTemplateNameLoc()))
1376 return true;
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001377
1378 // Visit the template arguments.
1379 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1380 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1381 return true;
1382
1383 return false;
1384}
1385
Douglas Gregor2332c112010-01-21 20:48:56 +00001386bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1387 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1388}
1389
1390bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1391 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1392 return Visit(TSInfo->getTypeLoc());
1393
1394 return false;
1395}
1396
Douglas Gregora59e3902010-01-21 23:27:09 +00001397bool CursorVisitor::VisitStmt(Stmt *S) {
1398 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
1399 Child != ChildEnd; ++Child) {
Ted Kremenek0f91f6a2010-05-13 00:25:00 +00001400 if (Stmt *C = *Child)
1401 if (Visit(MakeCXCursor(C, StmtParent, TU)))
1402 return true;
Douglas Gregora59e3902010-01-21 23:27:09 +00001403 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001404
Douglas Gregora59e3902010-01-21 23:27:09 +00001405 return false;
1406}
1407
Ted Kremenek0f91f6a2010-05-13 00:25:00 +00001408bool CursorVisitor::VisitCaseStmt(CaseStmt *S) {
1409 // Specially handle CaseStmts because they can be nested, e.g.:
1410 //
1411 // case 1:
1412 // case 2:
1413 //
1414 // In this case the second CaseStmt is the child of the first. Walking
1415 // these recursively can blow out the stack.
1416 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU);
1417 while (true) {
1418 // Set the Parent field to Cursor, then back to its old value once we're
1419 // done.
1420 SetParentRAII SetParent(Parent, StmtParent, Cursor);
1421
1422 if (Stmt *LHS = S->getLHS())
1423 if (Visit(MakeCXCursor(LHS, StmtParent, TU)))
1424 return true;
1425 if (Stmt *RHS = S->getRHS())
1426 if (Visit(MakeCXCursor(RHS, StmtParent, TU)))
1427 return true;
1428 if (Stmt *SubStmt = S->getSubStmt()) {
1429 if (!isa<CaseStmt>(SubStmt))
1430 return Visit(MakeCXCursor(SubStmt, StmtParent, TU));
1431
1432 // Specially handle 'CaseStmt' so that we don't blow out the stack.
1433 CaseStmt *CS = cast<CaseStmt>(SubStmt);
1434 Cursor = MakeCXCursor(CS, StmtParent, TU);
1435 if (RegionOfInterest.isValid()) {
1436 SourceRange Range = CS->getSourceRange();
1437 if (Range.isInvalid() || CompareRegionOfInterest(Range))
1438 return false;
1439 }
1440
1441 switch (Visitor(Cursor, Parent, ClientData)) {
1442 case CXChildVisit_Break: return true;
1443 case CXChildVisit_Continue: return false;
1444 case CXChildVisit_Recurse:
1445 // Perform tail-recursion manually.
1446 S = CS;
1447 continue;
1448 }
1449 }
1450 return false;
1451 }
1452}
1453
Douglas Gregora59e3902010-01-21 23:27:09 +00001454bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
Ted Kremenek007a7c92010-11-01 23:26:51 +00001455 bool isFirst = true;
Douglas Gregora59e3902010-01-21 23:27:09 +00001456 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
1457 D != DEnd; ++D) {
Ted Kremenek007a7c92010-11-01 23:26:51 +00001458 if (*D && Visit(MakeCXCursor(*D, TU, isFirst)))
Douglas Gregora59e3902010-01-21 23:27:09 +00001459 return true;
Ted Kremenek007a7c92010-11-01 23:26:51 +00001460 isFirst = false;
Douglas Gregora59e3902010-01-21 23:27:09 +00001461 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001462
Douglas Gregora59e3902010-01-21 23:27:09 +00001463 return false;
1464}
1465
Douglas Gregor36897b02010-09-10 00:22:18 +00001466bool CursorVisitor::VisitGotoStmt(GotoStmt *S) {
1467 return Visit(MakeCursorLabelRef(S->getLabel(), S->getLabelLoc(), TU));
1468}
1469
Douglas Gregorf5bab412010-01-22 01:00:11 +00001470bool CursorVisitor::VisitIfStmt(IfStmt *S) {
1471 if (VarDecl *Var = S->getConditionVariable()) {
1472 if (Visit(MakeCXCursor(Var, TU)))
1473 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001474 }
1475
Douglas Gregor263b47b2010-01-25 16:12:32 +00001476 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
1477 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +00001478 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
1479 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +00001480 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
1481 return true;
1482
1483 return false;
1484}
1485
1486bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
1487 if (VarDecl *Var = S->getConditionVariable()) {
1488 if (Visit(MakeCXCursor(Var, TU)))
1489 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001490 }
1491
Douglas Gregor263b47b2010-01-25 16:12:32 +00001492 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
1493 return true;
1494 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
1495 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001496
Douglas Gregor263b47b2010-01-25 16:12:32 +00001497 return false;
1498}
1499
1500bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
1501 if (VarDecl *Var = S->getConditionVariable()) {
1502 if (Visit(MakeCXCursor(Var, TU)))
1503 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001504 }
1505
Douglas Gregor263b47b2010-01-25 16:12:32 +00001506 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
1507 return true;
1508 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +00001509 return true;
1510
Douglas Gregor263b47b2010-01-25 16:12:32 +00001511 return false;
1512}
1513
1514bool CursorVisitor::VisitForStmt(ForStmt *S) {
1515 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
1516 return true;
1517 if (VarDecl *Var = S->getConditionVariable()) {
1518 if (Visit(MakeCXCursor(Var, TU)))
1519 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001520 }
1521
Douglas Gregor263b47b2010-01-25 16:12:32 +00001522 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
1523 return true;
1524 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
1525 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +00001526 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
1527 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001528
Douglas Gregorf5bab412010-01-22 01:00:11 +00001529 return false;
1530}
1531
Douglas Gregor8947a752010-09-02 20:35:02 +00001532bool CursorVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
1533 // Visit nested-name-specifier, if present.
1534 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1535 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1536 return true;
1537
1538 // Visit declaration name.
1539 if (VisitDeclarationNameInfo(E->getNameInfo()))
1540 return true;
1541
1542 // Visit explicitly-specified template arguments.
1543 if (E->hasExplicitTemplateArgs()) {
1544 ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
1545 for (TemplateArgumentLoc *Arg = Args.getTemplateArgs(),
1546 *ArgEnd = Arg + Args.NumTemplateArgs;
1547 Arg != ArgEnd; ++Arg)
1548 if (VisitTemplateArgumentLoc(*Arg))
1549 return true;
1550 }
1551
1552 return false;
1553}
1554
Douglas Gregor6cd24e22010-07-29 00:26:18 +00001555bool CursorVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1556 if (Visit(MakeCXCursor(E->getArg(0), StmtParent, TU)))
1557 return true;
1558
1559 if (Visit(MakeCXCursor(E->getCallee(), StmtParent, TU)))
1560 return true;
1561
1562 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
1563 if (Visit(MakeCXCursor(E->getArg(I), StmtParent, TU)))
1564 return true;
1565
1566 return false;
1567}
1568
Ted Kremenek3064ef92010-08-27 21:34:58 +00001569bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1570 if (D->isDefinition()) {
1571 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1572 E = D->bases_end(); I != E; ++I) {
1573 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
1574 return true;
1575 }
1576 }
1577
1578 return VisitTagDecl(D);
1579}
1580
1581
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00001582bool CursorVisitor::VisitBlockExpr(BlockExpr *B) {
1583 return Visit(B->getBlockDecl());
1584}
1585
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001586bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
Douglas Gregor8ccef2d2010-09-09 23:10:46 +00001587 // Visit the type into which we're computing an offset.
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001588 if (Visit(E->getTypeSourceInfo()->getTypeLoc()))
1589 return true;
Douglas Gregor8ccef2d2010-09-09 23:10:46 +00001590
1591 // Visit the components of the offsetof expression.
1592 for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
1593 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
1594 const OffsetOfNode &Node = E->getComponent(I);
1595 switch (Node.getKind()) {
1596 case OffsetOfNode::Array:
1597 if (Visit(MakeCXCursor(E->getIndexExpr(Node.getArrayExprIndex()),
1598 StmtParent, TU)))
1599 return true;
1600 break;
1601
1602 case OffsetOfNode::Field:
1603 if (Visit(MakeCursorMemberRef(Node.getField(), Node.getRange().getEnd(),
1604 TU)))
1605 return true;
1606 break;
1607
1608 case OffsetOfNode::Identifier:
1609 case OffsetOfNode::Base:
1610 continue;
1611 }
1612 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001613
Douglas Gregor8ccef2d2010-09-09 23:10:46 +00001614 return false;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001615}
1616
Douglas Gregor336fd812010-01-23 00:40:08 +00001617bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
1618 if (E->isArgumentType()) {
1619 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
1620 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001621
Douglas Gregor336fd812010-01-23 00:40:08 +00001622 return false;
1623 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001624
Douglas Gregor336fd812010-01-23 00:40:08 +00001625 return VisitExpr(E);
1626}
1627
Douglas Gregorfbb4c982010-09-02 21:07:44 +00001628bool CursorVisitor::VisitMemberExpr(MemberExpr *E) {
1629 // Visit the base expression.
1630 if (Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
1631 return true;
1632
1633 // Visit the nested-name-specifier
1634 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1635 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1636 return true;
1637
1638 // Visit the declaration name.
1639 if (VisitDeclarationNameInfo(E->getMemberNameInfo()))
1640 return true;
1641
1642 // Visit the explicitly-specified template arguments, if any.
1643 if (E->hasExplicitTemplateArgs()) {
1644 for (const TemplateArgumentLoc *Arg = E->getTemplateArgs(),
1645 *ArgEnd = Arg + E->getNumTemplateArgs();
1646 Arg != ArgEnd;
1647 ++Arg) {
1648 if (VisitTemplateArgumentLoc(*Arg))
1649 return true;
1650 }
1651 }
1652
1653 return false;
1654}
1655
Douglas Gregor336fd812010-01-23 00:40:08 +00001656bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1657 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
1658 if (Visit(TSInfo->getTypeLoc()))
1659 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001660
Douglas Gregor336fd812010-01-23 00:40:08 +00001661 return VisitCastExpr(E);
1662}
1663
1664bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1665 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
1666 if (Visit(TSInfo->getTypeLoc()))
1667 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001668
Douglas Gregor336fd812010-01-23 00:40:08 +00001669 return VisitExpr(E);
1670}
1671
Douglas Gregor36897b02010-09-10 00:22:18 +00001672bool CursorVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
1673 return Visit(MakeCursorLabelRef(E->getLabel(), E->getLabelLoc(), TU));
1674}
1675
Douglas Gregor648220e2010-08-10 15:02:34 +00001676bool CursorVisitor::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1677 return Visit(E->getArgTInfo1()->getTypeLoc()) ||
1678 Visit(E->getArgTInfo2()->getTypeLoc());
1679}
1680
1681bool CursorVisitor::VisitVAArgExpr(VAArgExpr *E) {
1682 if (Visit(E->getWrittenTypeInfo()->getTypeLoc()))
1683 return true;
1684
1685 return Visit(MakeCXCursor(E->getSubExpr(), StmtParent, TU));
1686}
1687
Douglas Gregorfa2e26f2010-09-09 23:28:23 +00001688bool CursorVisitor::VisitInitListExpr(InitListExpr *E) {
1689 // We care about the syntactic form of the initializer list, only.
Douglas Gregor692577c2010-09-17 20:26:51 +00001690 if (InitListExpr *Syntactic = E->getSyntacticForm())
1691 return VisitExpr(Syntactic);
1692
1693 return VisitExpr(E);
Douglas Gregorfa2e26f2010-09-09 23:28:23 +00001694}
1695
1696bool CursorVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1697 // Visit the designators.
1698 typedef DesignatedInitExpr::Designator Designator;
1699 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1700 DEnd = E->designators_end();
1701 D != DEnd; ++D) {
1702 if (D->isFieldDesignator()) {
1703 if (FieldDecl *Field = D->getField())
1704 if (Visit(MakeCursorMemberRef(Field, D->getFieldLoc(), TU)))
1705 return true;
1706
1707 continue;
1708 }
1709
1710 if (D->isArrayDesignator()) {
1711 if (Visit(MakeCXCursor(E->getArrayIndex(*D), StmtParent, TU)))
1712 return true;
1713
1714 continue;
1715 }
1716
1717 assert(D->isArrayRangeDesignator() && "Unknown designator kind");
1718 if (Visit(MakeCXCursor(E->getArrayRangeStart(*D), StmtParent, TU)) ||
1719 Visit(MakeCXCursor(E->getArrayRangeEnd(*D), StmtParent, TU)))
1720 return true;
1721 }
1722
1723 // Visit the initializer value itself.
1724 return Visit(MakeCXCursor(E->getInit(), StmtParent, TU));
1725}
1726
Douglas Gregor94802292010-09-02 21:20:16 +00001727bool CursorVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1728 if (E->isTypeOperand()) {
1729 if (TypeSourceInfo *TSInfo = E->getTypeOperandSourceInfo())
1730 return Visit(TSInfo->getTypeLoc());
1731
1732 return false;
1733 }
1734
1735 return VisitExpr(E);
1736}
1737
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001738bool CursorVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1739 if (E->isTypeOperand()) {
1740 if (TypeSourceInfo *TSInfo = E->getTypeOperandSourceInfo())
1741 return Visit(TSInfo->getTypeLoc());
1742
1743 return false;
1744 }
1745
1746 return VisitExpr(E);
1747}
1748
Douglas Gregorab6677e2010-09-08 00:15:04 +00001749bool CursorVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1750 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
Douglas Gregor40749ee2010-11-03 00:35:38 +00001751 if (Visit(TSInfo->getTypeLoc()))
1752 return true;
Douglas Gregorab6677e2010-09-08 00:15:04 +00001753
1754 return VisitExpr(E);
1755}
1756
1757bool CursorVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1758 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
1759 return Visit(TSInfo->getTypeLoc());
1760
1761 return false;
1762}
1763
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001764bool CursorVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
1765 // Visit placement arguments.
1766 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1767 if (Visit(MakeCXCursor(E->getPlacementArg(I), StmtParent, TU)))
1768 return true;
1769
1770 // Visit the allocated type.
1771 if (TypeSourceInfo *TSInfo = E->getAllocatedTypeSourceInfo())
1772 if (Visit(TSInfo->getTypeLoc()))
1773 return true;
1774
1775 // Visit the array size, if any.
1776 if (E->isArray() && Visit(MakeCXCursor(E->getArraySize(), StmtParent, TU)))
1777 return true;
1778
1779 // Visit the initializer or constructor arguments.
1780 for (unsigned I = 0, N = E->getNumConstructorArgs(); I != N; ++I)
1781 if (Visit(MakeCXCursor(E->getConstructorArg(I), StmtParent, TU)))
1782 return true;
1783
1784 return false;
1785}
1786
Douglas Gregor6f7198f2010-09-02 22:09:03 +00001787bool CursorVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1788 // Visit base expression.
1789 if (Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
1790 return true;
1791
1792 // Visit the nested-name-specifier.
1793 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1794 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1795 return true;
1796
1797 // Visit the scope type that looks disturbingly like the nested-name-specifier
1798 // but isn't.
1799 if (TypeSourceInfo *TSInfo = E->getScopeTypeInfo())
1800 if (Visit(TSInfo->getTypeLoc()))
1801 return true;
1802
1803 // Visit the name of the type being destroyed.
1804 if (TypeSourceInfo *TSInfo = E->getDestroyedTypeInfo())
1805 if (Visit(TSInfo->getTypeLoc()))
1806 return true;
1807
1808 return false;
1809}
1810
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00001811bool CursorVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1812 return Visit(E->getQueriedTypeSourceInfo()->getTypeLoc());
1813}
1814
Douglas Gregor1f7b5902010-09-02 22:29:21 +00001815bool CursorVisitor::VisitOverloadExpr(OverloadExpr *E) {
Douglas Gregor8ab670e2010-09-02 22:19:24 +00001816 // Visit the nested-name-specifier.
1817 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1818 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1819 return true;
1820
1821 // Visit the declaration name.
1822 if (VisitDeclarationNameInfo(E->getNameInfo()))
1823 return true;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001824
1825 // Visit the overloaded declaration reference.
1826 if (Visit(MakeCursorOverloadedDeclRef(E, TU)))
1827 return true;
1828
Douglas Gregor1f7b5902010-09-02 22:29:21 +00001829 // Visit the explicitly-specified template arguments.
1830 if (const ExplicitTemplateArgumentList *ArgList
1831 = E->getOptionalExplicitTemplateArgs()) {
1832 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
1833 *ArgEnd = Arg + ArgList->NumTemplateArgs;
1834 Arg != ArgEnd; ++Arg) {
1835 if (VisitTemplateArgumentLoc(*Arg))
1836 return true;
1837 }
1838 }
1839
Douglas Gregor8ab670e2010-09-02 22:19:24 +00001840 return false;
1841}
1842
Douglas Gregorbfebed22010-09-03 17:24:10 +00001843bool CursorVisitor::VisitDependentScopeDeclRefExpr(
1844 DependentScopeDeclRefExpr *E) {
1845 // Visit the nested-name-specifier.
1846 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1847 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1848 return true;
1849
1850 // Visit the declaration name.
1851 if (VisitDeclarationNameInfo(E->getNameInfo()))
1852 return true;
1853
1854 // Visit the explicitly-specified template arguments.
1855 if (const ExplicitTemplateArgumentList *ArgList
1856 = E->getOptionalExplicitTemplateArgs()) {
1857 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
1858 *ArgEnd = Arg + ArgList->NumTemplateArgs;
1859 Arg != ArgEnd; ++Arg) {
1860 if (VisitTemplateArgumentLoc(*Arg))
1861 return true;
1862 }
1863 }
1864
1865 return false;
1866}
1867
Douglas Gregorab6677e2010-09-08 00:15:04 +00001868bool CursorVisitor::VisitCXXUnresolvedConstructExpr(
1869 CXXUnresolvedConstructExpr *E) {
1870 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
1871 if (Visit(TSInfo->getTypeLoc()))
1872 return true;
1873
1874 return VisitExpr(E);
1875}
1876
Douglas Gregor25d63622010-09-03 17:35:34 +00001877bool CursorVisitor::VisitCXXDependentScopeMemberExpr(
1878 CXXDependentScopeMemberExpr *E) {
1879 // Visit the base expression, if there is one.
1880 if (!E->isImplicitAccess() &&
1881 Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
1882 return true;
1883
1884 // Visit the nested-name-specifier.
1885 if (NestedNameSpecifier *Qualifier = E->getQualifier())
1886 if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
1887 return true;
1888
1889 // Visit the declaration name.
1890 if (VisitDeclarationNameInfo(E->getMemberNameInfo()))
1891 return true;
1892
1893 // Visit the explicitly-specified template arguments.
1894 if (const ExplicitTemplateArgumentList *ArgList
1895 = E->getOptionalExplicitTemplateArgs()) {
1896 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
1897 *ArgEnd = Arg + ArgList->NumTemplateArgs;
1898 Arg != ArgEnd; ++Arg) {
1899 if (VisitTemplateArgumentLoc(*Arg))
1900 return true;
1901 }
1902 }
1903
1904 return false;
1905}
1906
Douglas Gregoraaa80b22010-09-03 18:01:25 +00001907bool CursorVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1908 // Visit the base expression, if there is one.
1909 if (!E->isImplicitAccess() &&
1910 Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
1911 return true;
1912
1913 return VisitOverloadExpr(E);
1914}
Douglas Gregor25d63622010-09-03 17:35:34 +00001915
Douglas Gregorc2350e52010-03-08 16:40:19 +00001916bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Douglas Gregor04badcf2010-04-21 00:45:42 +00001917 if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo())
1918 if (Visit(TSInfo->getTypeLoc()))
1919 return true;
Douglas Gregorc2350e52010-03-08 16:40:19 +00001920
1921 return VisitExpr(E);
1922}
1923
Douglas Gregor81d34662010-04-20 15:39:42 +00001924bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1925 return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc());
1926}
1927
1928
Ted Kremenek09dfa372010-02-18 05:46:33 +00001929bool CursorVisitor::VisitAttributes(Decl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00001930 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
1931 i != e; ++i)
1932 if (Visit(MakeCXCursor(*i, D, TU)))
Ted Kremenek09dfa372010-02-18 05:46:33 +00001933 return true;
1934
1935 return false;
1936}
1937
Douglas Gregor8c8d5412010-09-24 21:18:36 +00001938static llvm::sys::Mutex EnableMultithreadingMutex;
1939static bool EnabledMultithreading;
1940
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001941extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001942CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
1943 int displayDiagnostics) {
Daniel Dunbar48615ff2010-10-08 19:30:33 +00001944 // Disable pretty stack trace functionality, which will otherwise be a very
1945 // poor citizen of the world and set up all sorts of signal handlers.
1946 llvm::DisablePrettyStackTrace = true;
1947
Daniel Dunbarc7df4f32010-08-18 18:43:14 +00001948 // We use crash recovery to make some of our APIs more reliable, implicitly
1949 // enable it.
1950 llvm::CrashRecoveryContext::Enable();
1951
Douglas Gregor8c8d5412010-09-24 21:18:36 +00001952 // Enable support for multithreading in LLVM.
1953 {
1954 llvm::sys::ScopedLock L(EnableMultithreadingMutex);
1955 if (!EnabledMultithreading) {
1956 llvm::llvm_start_multithreaded();
1957 EnabledMultithreading = true;
1958 }
1959 }
1960
Douglas Gregora030b7c2010-01-22 20:35:53 +00001961 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001962 if (excludeDeclarationsFromPCH)
1963 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001964 if (displayDiagnostics)
1965 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001966 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +00001967}
1968
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001969void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001970 if (CIdx)
1971 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001972}
1973
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001974CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +00001975 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001976 if (!CIdx)
1977 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001978
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00001979 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001980
Douglas Gregor28019772010-04-05 23:52:57 +00001981 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001982 return ASTUnit::LoadFromASTFile(ast_filename, Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +00001983 CXXIdx->getOnlyLocalDecls(),
1984 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +00001985}
1986
Douglas Gregorb1c031b2010-08-09 22:28:58 +00001987unsigned clang_defaultEditingTranslationUnitOptions() {
Douglas Gregor2a2c50b2010-09-27 05:49:58 +00001988 return CXTranslationUnit_PrecompiledPreamble |
Douglas Gregor99ba2022010-10-27 17:24:53 +00001989 CXTranslationUnit_CacheCompletionResults |
1990 CXTranslationUnit_CXXPrecompiledPreamble;
Douglas Gregorb1c031b2010-08-09 22:28:58 +00001991}
1992
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001993CXTranslationUnit
1994clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
1995 const char *source_filename,
1996 int num_command_line_args,
Douglas Gregor2ef69442010-09-01 16:43:19 +00001997 const char * const *command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001998 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001999 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor5a430212010-07-21 18:52:53 +00002000 return clang_parseTranslationUnit(CIdx, source_filename,
2001 command_line_args, num_command_line_args,
2002 unsaved_files, num_unsaved_files,
2003 CXTranslationUnit_DetailedPreprocessingRecord);
2004}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002005
2006struct ParseTranslationUnitInfo {
2007 CXIndex CIdx;
2008 const char *source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002009 const char *const *command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002010 int num_command_line_args;
2011 struct CXUnsavedFile *unsaved_files;
2012 unsigned num_unsaved_files;
2013 unsigned options;
2014 CXTranslationUnit result;
2015};
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002016static void clang_parseTranslationUnit_Impl(void *UserData) {
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002017 ParseTranslationUnitInfo *PTUI =
2018 static_cast<ParseTranslationUnitInfo*>(UserData);
2019 CXIndex CIdx = PTUI->CIdx;
2020 const char *source_filename = PTUI->source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002021 const char * const *command_line_args = PTUI->command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002022 int num_command_line_args = PTUI->num_command_line_args;
2023 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
2024 unsigned num_unsaved_files = PTUI->num_unsaved_files;
2025 unsigned options = PTUI->options;
2026 PTUI->result = 0;
Douglas Gregor5a430212010-07-21 18:52:53 +00002027
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002028 if (!CIdx)
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002029 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002030
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002031 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2032
Douglas Gregor44c181a2010-07-23 00:33:23 +00002033 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
Douglas Gregordf95a132010-08-09 20:45:32 +00002034 bool CompleteTranslationUnit
2035 = ((options & CXTranslationUnit_Incomplete) == 0);
Douglas Gregor87c08a52010-08-13 22:48:40 +00002036 bool CacheCodeCompetionResults
2037 = options & CXTranslationUnit_CacheCompletionResults;
Douglas Gregor99ba2022010-10-27 17:24:53 +00002038 bool CXXPrecompilePreamble
2039 = options & CXTranslationUnit_CXXPrecompiledPreamble;
2040 bool CXXChainedPCH
2041 = options & CXTranslationUnit_CXXChainedPCH;
Douglas Gregor87c08a52010-08-13 22:48:40 +00002042
Douglas Gregor5352ac02010-01-28 00:27:43 +00002043 // Configure the diagnostics.
2044 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +00002045 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
2046 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002047
Douglas Gregor4db64a42010-01-23 00:14:00 +00002048 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2049 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattnera0a270c2010-04-05 22:42:27 +00002050 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002051 const llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00002052 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +00002053 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2054 Buffer));
2055 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002056
Douglas Gregorb10daed2010-10-11 16:52:23 +00002057 llvm::SmallVector<const char *, 16> Args;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002058
Ted Kremenek139ba862009-10-22 00:03:57 +00002059 // The 'source_filename' argument is optional. If the caller does not
2060 // specify it then it is assumed that the source file is specified
2061 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002062 if (source_filename)
Douglas Gregorb10daed2010-10-11 16:52:23 +00002063 Args.push_back(source_filename);
Douglas Gregor52ddc5d2010-07-09 18:39:07 +00002064
2065 // Since the Clang C library is primarily used by batch tools dealing with
2066 // (often very broken) source code, where spell-checking can have a
2067 // significant negative impact on performance (particularly when
2068 // precompiled headers are involved), we disable it by default.
Douglas Gregorb10daed2010-10-11 16:52:23 +00002069 // Only do this if we haven't found a spell-checking-related argument.
2070 bool FoundSpellCheckingArgument = false;
2071 for (int I = 0; I != num_command_line_args; ++I) {
2072 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
2073 strcmp(command_line_args[I], "-fspell-checking") == 0) {
2074 FoundSpellCheckingArgument = true;
2075 break;
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002076 }
Douglas Gregorb10daed2010-10-11 16:52:23 +00002077 }
2078 if (!FoundSpellCheckingArgument)
2079 Args.push_back("-fno-spell-checking");
2080
2081 Args.insert(Args.end(), command_line_args,
2082 command_line_args + num_command_line_args);
Douglas Gregord93256e2010-01-28 06:00:51 +00002083
Douglas Gregor44c181a2010-07-23 00:33:23 +00002084 // Do we need the detailed preprocessing record?
2085 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
Douglas Gregorb10daed2010-10-11 16:52:23 +00002086 Args.push_back("-Xclang");
2087 Args.push_back("-detailed-preprocessing-record");
Douglas Gregor44c181a2010-07-23 00:33:23 +00002088 }
2089
Douglas Gregorb10daed2010-10-11 16:52:23 +00002090 unsigned NumErrors = Diags->getNumErrors();
Douglas Gregorb10daed2010-10-11 16:52:23 +00002091 llvm::OwningPtr<ASTUnit> Unit(
2092 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
2093 Diags,
2094 CXXIdx->getClangResourcesPath(),
2095 CXXIdx->getOnlyLocalDecls(),
2096 RemappedFiles.data(),
2097 RemappedFiles.size(),
2098 /*CaptureDiagnostics=*/true,
2099 PrecompilePreamble,
2100 CompleteTranslationUnit,
Douglas Gregor99ba2022010-10-27 17:24:53 +00002101 CacheCodeCompetionResults,
2102 CXXPrecompilePreamble,
2103 CXXChainedPCH));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002104
Douglas Gregorb10daed2010-10-11 16:52:23 +00002105 if (NumErrors != Diags->getNumErrors()) {
2106 // Make sure to check that 'Unit' is non-NULL.
2107 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
2108 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
2109 DEnd = Unit->stored_diag_end();
2110 D != DEnd; ++D) {
2111 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
2112 CXString Msg = clang_formatDiagnostic(&Diag,
2113 clang_defaultDiagnosticDisplayOptions());
2114 fprintf(stderr, "%s\n", clang_getCString(Msg));
2115 clang_disposeString(Msg);
2116 }
Douglas Gregor274f1902010-02-22 23:17:23 +00002117#ifdef LLVM_ON_WIN32
Douglas Gregorb10daed2010-10-11 16:52:23 +00002118 // On Windows, force a flush, since there may be multiple copies of
2119 // stderr and stdout in the file system, all with different buffers
2120 // but writing to the same device.
2121 fflush(stderr);
2122#endif
2123 }
Douglas Gregora88084b2010-02-18 18:08:43 +00002124 }
Douglas Gregord93256e2010-01-28 06:00:51 +00002125
Douglas Gregorb10daed2010-10-11 16:52:23 +00002126 PTUI->result = Unit.take();
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002127}
2128CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
2129 const char *source_filename,
Douglas Gregor2ef69442010-09-01 16:43:19 +00002130 const char * const *command_line_args,
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002131 int num_command_line_args,
2132 struct CXUnsavedFile *unsaved_files,
2133 unsigned num_unsaved_files,
2134 unsigned options) {
2135 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
2136 num_command_line_args, unsaved_files, num_unsaved_files,
2137 options, 0 };
2138 llvm::CrashRecoveryContext CRC;
2139
2140 if (!CRC.RunSafely(clang_parseTranslationUnit_Impl, &PTUI)) {
Daniel Dunbar60a45432010-08-23 22:35:34 +00002141 fprintf(stderr, "libclang: crash detected during parsing: {\n");
2142 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
2143 fprintf(stderr, " 'command_line_args' : [");
2144 for (int i = 0; i != num_command_line_args; ++i) {
2145 if (i)
2146 fprintf(stderr, ", ");
2147 fprintf(stderr, "'%s'", command_line_args[i]);
2148 }
2149 fprintf(stderr, "],\n");
2150 fprintf(stderr, " 'unsaved_files' : [");
2151 for (unsigned i = 0; i != num_unsaved_files; ++i) {
2152 if (i)
2153 fprintf(stderr, ", ");
2154 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
2155 unsaved_files[i].Length);
2156 }
2157 fprintf(stderr, "],\n");
2158 fprintf(stderr, " 'options' : %d,\n", options);
2159 fprintf(stderr, "}\n");
2160
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002161 return 0;
2162 }
2163
2164 return PTUI.result;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00002165}
2166
Douglas Gregor19998442010-08-13 15:35:05 +00002167unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
2168 return CXSaveTranslationUnit_None;
2169}
2170
2171int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
2172 unsigned options) {
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002173 if (!TU)
2174 return 1;
2175
2176 return static_cast<ASTUnit *>(TU)->Save(FileName);
2177}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002178
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002179void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002180 if (CTUnit) {
2181 // If the translation unit has been marked as unsafe to free, just discard
2182 // it.
2183 if (static_cast<ASTUnit *>(CTUnit)->isUnsafeToFree())
2184 return;
2185
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002186 delete static_cast<ASTUnit *>(CTUnit);
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002187 }
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00002188}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002189
Douglas Gregore1e13bf2010-08-11 15:58:42 +00002190unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
2191 return CXReparse_None;
2192}
2193
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002194struct ReparseTranslationUnitInfo {
2195 CXTranslationUnit TU;
2196 unsigned num_unsaved_files;
2197 struct CXUnsavedFile *unsaved_files;
2198 unsigned options;
2199 int result;
2200};
Douglas Gregor593b0c12010-09-23 18:47:53 +00002201
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002202static void clang_reparseTranslationUnit_Impl(void *UserData) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002203 ReparseTranslationUnitInfo *RTUI =
2204 static_cast<ReparseTranslationUnitInfo*>(UserData);
2205 CXTranslationUnit TU = RTUI->TU;
2206 unsigned num_unsaved_files = RTUI->num_unsaved_files;
2207 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
2208 unsigned options = RTUI->options;
2209 (void) options;
2210 RTUI->result = 1;
2211
Douglas Gregorabc563f2010-07-19 21:46:24 +00002212 if (!TU)
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002213 return;
Douglas Gregor593b0c12010-09-23 18:47:53 +00002214
2215 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2216 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002217
2218 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2219 for (unsigned I = 0; I != num_unsaved_files; ++I) {
2220 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2221 const llvm::MemoryBuffer *Buffer
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00002222 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002223 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2224 Buffer));
2225 }
2226
Douglas Gregor593b0c12010-09-23 18:47:53 +00002227 if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size()))
2228 RTUI->result = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00002229}
Douglas Gregor593b0c12010-09-23 18:47:53 +00002230
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002231int clang_reparseTranslationUnit(CXTranslationUnit TU,
2232 unsigned num_unsaved_files,
2233 struct CXUnsavedFile *unsaved_files,
2234 unsigned options) {
2235 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
2236 options, 0 };
2237 llvm::CrashRecoveryContext CRC;
2238
2239 if (!CRC.RunSafely(clang_reparseTranslationUnit_Impl, &RTUI)) {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002240 fprintf(stderr, "libclang: crash detected during reparsing\n");
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002241 static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
2242 return 1;
2243 }
2244
Ted Kremenek1dfb26a2010-10-29 01:06:50 +00002245
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002246 return RTUI.result;
2247}
2248
Douglas Gregordf95a132010-08-09 20:45:32 +00002249
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002250CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002251 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002252 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002253
Steve Naroff77accc12009-09-03 18:19:54 +00002254 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002255 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00002256}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00002257
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002258CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002259 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002260 return Result;
2261}
2262
Ted Kremenekfb480492010-01-13 21:46:36 +00002263} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00002264
Ted Kremenekfb480492010-01-13 21:46:36 +00002265//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00002266// CXSourceLocation and CXSourceRange Operations.
2267//===----------------------------------------------------------------------===//
2268
Douglas Gregorb9790342010-01-22 21:44:22 +00002269extern "C" {
2270CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00002271 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00002272 return Result;
2273}
2274
2275unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00002276 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
2277 loc1.ptr_data[1] == loc2.ptr_data[1] &&
2278 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00002279}
2280
2281CXSourceLocation clang_getLocation(CXTranslationUnit tu,
2282 CXFile file,
2283 unsigned line,
2284 unsigned column) {
Douglas Gregor42748ec2010-04-30 19:45:53 +00002285 if (!tu || !file)
Douglas Gregorb9790342010-01-22 21:44:22 +00002286 return clang_getNullLocation();
Douglas Gregor42748ec2010-04-30 19:45:53 +00002287
Douglas Gregorb9790342010-01-22 21:44:22 +00002288 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
2289 SourceLocation SLoc
2290 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002291 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00002292 line, column);
David Chisnall83889a72010-10-15 17:07:39 +00002293 if (SLoc.isInvalid()) return clang_getNullLocation();
2294
2295 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
2296}
2297
2298CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu,
2299 CXFile file,
2300 unsigned offset) {
2301 if (!tu || !file)
2302 return clang_getNullLocation();
2303
2304 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
2305 SourceLocation Start
2306 = CXXUnit->getSourceManager().getLocation(
2307 static_cast<const FileEntry *>(file),
2308 1, 1);
2309 if (Start.isInvalid()) return clang_getNullLocation();
2310
2311 SourceLocation SLoc = Start.getFileLocWithOffset(offset);
2312
2313 if (SLoc.isInvalid()) return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002314
Ted Kremenek1a9a0bc2010-06-28 23:54:17 +00002315 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00002316}
2317
Douglas Gregor5352ac02010-01-28 00:27:43 +00002318CXSourceRange clang_getNullRange() {
2319 CXSourceRange Result = { { 0, 0 }, 0, 0 };
2320 return Result;
2321}
Daniel Dunbard52864b2010-02-14 10:02:57 +00002322
Douglas Gregor5352ac02010-01-28 00:27:43 +00002323CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
2324 if (begin.ptr_data[0] != end.ptr_data[0] ||
2325 begin.ptr_data[1] != end.ptr_data[1])
2326 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002327
2328 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002329 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00002330 return Result;
2331}
2332
Douglas Gregor46766dc2010-01-26 19:19:08 +00002333void clang_getInstantiationLocation(CXSourceLocation location,
2334 CXFile *file,
2335 unsigned *line,
2336 unsigned *column,
2337 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00002338 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
2339
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002340 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00002341 if (file)
2342 *file = 0;
2343 if (line)
2344 *line = 0;
2345 if (column)
2346 *column = 0;
2347 if (offset)
2348 *offset = 0;
2349 return;
2350 }
2351
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002352 const SourceManager &SM =
2353 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002354 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002355
2356 if (file)
2357 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
2358 if (line)
2359 *line = SM.getInstantiationLineNumber(InstLoc);
2360 if (column)
2361 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00002362 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00002363 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00002364}
2365
Douglas Gregor1db19de2010-01-19 21:36:55 +00002366CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002367 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002368 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002369 return Result;
2370}
2371
2372CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002373 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002374 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002375 return Result;
2376}
2377
Douglas Gregorb9790342010-01-22 21:44:22 +00002378} // end: extern "C"
2379
Douglas Gregor1db19de2010-01-19 21:36:55 +00002380//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002381// CXFile Operations.
2382//===----------------------------------------------------------------------===//
2383
2384extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00002385CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002386 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00002387 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002388
Steve Naroff88145032009-10-27 14:35:18 +00002389 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00002390 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00002391}
2392
2393time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002394 if (!SFile)
2395 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002396
Steve Naroff88145032009-10-27 14:35:18 +00002397 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
2398 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00002399}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002400
Douglas Gregorb9790342010-01-22 21:44:22 +00002401CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
2402 if (!tu)
2403 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002404
Douglas Gregorb9790342010-01-22 21:44:22 +00002405 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002406
Douglas Gregorb9790342010-01-22 21:44:22 +00002407 FileManager &FMgr = CXXUnit->getFileManager();
2408 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
2409 return const_cast<FileEntry *>(File);
2410}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002411
Ted Kremenekfb480492010-01-13 21:46:36 +00002412} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00002413
Ted Kremenekfb480492010-01-13 21:46:36 +00002414//===----------------------------------------------------------------------===//
2415// CXCursor Operations.
2416//===----------------------------------------------------------------------===//
2417
Ted Kremenekfb480492010-01-13 21:46:36 +00002418static Decl *getDeclFromExpr(Stmt *E) {
Douglas Gregordb1314e2010-10-01 21:11:22 +00002419 if (CastExpr *CE = dyn_cast<CastExpr>(E))
2420 return getDeclFromExpr(CE->getSubExpr());
2421
Ted Kremenekfb480492010-01-13 21:46:36 +00002422 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
2423 return RefExpr->getDecl();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002424 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2425 return RefExpr->getDecl();
Ted Kremenekfb480492010-01-13 21:46:36 +00002426 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
2427 return ME->getMemberDecl();
2428 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
2429 return RE->getDecl();
Douglas Gregordb1314e2010-10-01 21:11:22 +00002430 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
2431 return PRE->getProperty();
2432
Ted Kremenekfb480492010-01-13 21:46:36 +00002433 if (CallExpr *CE = dyn_cast<CallExpr>(E))
2434 return getDeclFromExpr(CE->getCallee());
Ted Kremenekfb480492010-01-13 21:46:36 +00002435 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
2436 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002437
Douglas Gregordb1314e2010-10-01 21:11:22 +00002438 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
2439 return PE->getProtocol();
2440
Ted Kremenekfb480492010-01-13 21:46:36 +00002441 return 0;
2442}
2443
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002444static SourceLocation getLocationFromExpr(Expr *E) {
2445 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
2446 return /*FIXME:*/Msg->getLeftLoc();
2447 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
2448 return DRE->getLocation();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002449 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2450 return RefExpr->getLocation();
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002451 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
2452 return Member->getMemberLoc();
2453 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
2454 return Ivar->getLocation();
2455 return E->getLocStart();
2456}
2457
Ted Kremenekfb480492010-01-13 21:46:36 +00002458extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002459
2460unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00002461 CXCursorVisitor visitor,
2462 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002463 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00002464
Douglas Gregoreb8837b2010-08-03 19:06:41 +00002465 CursorVisitor CursorVis(CXXUnit, visitor, client_data,
2466 CXXUnit->getMaxPCHLevel());
Douglas Gregorb1373d02010-01-20 20:59:29 +00002467 return CursorVis.VisitChildren(parent);
2468}
2469
David Chisnall3387c652010-11-03 14:12:26 +00002470#ifndef __has_feature
2471#define __has_feature(x) 0
2472#endif
2473#if __has_feature(blocks)
2474typedef enum CXChildVisitResult
2475 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
2476
2477static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2478 CXClientData client_data) {
2479 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2480 return block(cursor, parent);
2481}
2482#else
2483// If we are compiled with a compiler that doesn't have native blocks support,
2484// define and call the block manually, so the
2485typedef struct _CXChildVisitResult
2486{
2487 void *isa;
2488 int flags;
2489 int reserved;
2490 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, CXCursor);
2491} *CXCursorVisitorBlock;
2492
2493static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2494 CXClientData client_data) {
2495 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2496 return block->invoke(block, cursor, parent);
2497}
2498#endif
2499
2500
2501unsigned clang_visitChildrenWithBlock(CXCursor parent, CXCursorVisitorBlock block) {
2502 return clang_visitChildren(parent, visitWithBlock, block);
2503}
2504
Douglas Gregor78205d42010-01-20 21:45:58 +00002505static CXString getDeclSpelling(Decl *D) {
2506 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
2507 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002508 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002509
Douglas Gregor78205d42010-01-20 21:45:58 +00002510 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002511 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002512
Douglas Gregor78205d42010-01-20 21:45:58 +00002513 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
2514 // No, this isn't the same as the code below. getIdentifier() is non-virtual
2515 // and returns different names. NamedDecl returns the class name and
2516 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002517 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002518
Douglas Gregor0a35bce2010-09-01 03:07:18 +00002519 if (isa<UsingDirectiveDecl>(D))
2520 return createCXString("");
2521
Ted Kremenek50aa6ac2010-05-19 21:51:10 +00002522 llvm::SmallString<1024> S;
2523 llvm::raw_svector_ostream os(S);
2524 ND->printName(os);
2525
2526 return createCXString(os.str());
Douglas Gregor78205d42010-01-20 21:45:58 +00002527}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002528
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002529CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002530 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002531 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002532
Steve Narofff334b4e2009-09-02 18:26:48 +00002533 if (clang_isReference(C.kind)) {
2534 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00002535 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00002536 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002537 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002538 }
2539 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00002540 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002541 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002542 }
2543 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00002544 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00002545 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002546 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002547 }
Ted Kremenek3064ef92010-08-27 21:34:58 +00002548 case CXCursor_CXXBaseSpecifier: {
2549 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
2550 return createCXString(B->getType().getAsString());
2551 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002552 case CXCursor_TypeRef: {
2553 TypeDecl *Type = getCursorTypeRef(C).first;
2554 assert(Type && "Missing type decl");
2555
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002556 return createCXString(getCursorContext(C).getTypeDeclType(Type).
2557 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002558 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00002559 case CXCursor_TemplateRef: {
2560 TemplateDecl *Template = getCursorTemplateRef(C).first;
Douglas Gregor69319002010-08-31 23:48:11 +00002561 assert(Template && "Missing template decl");
Douglas Gregor0b36e612010-08-31 20:37:03 +00002562
2563 return createCXString(Template->getNameAsString());
2564 }
Douglas Gregor69319002010-08-31 23:48:11 +00002565
2566 case CXCursor_NamespaceRef: {
2567 NamedDecl *NS = getCursorNamespaceRef(C).first;
2568 assert(NS && "Missing namespace decl");
2569
2570 return createCXString(NS->getNameAsString());
2571 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002572
Douglas Gregora67e03f2010-09-09 21:42:20 +00002573 case CXCursor_MemberRef: {
2574 FieldDecl *Field = getCursorMemberRef(C).first;
2575 assert(Field && "Missing member decl");
2576
2577 return createCXString(Field->getNameAsString());
2578 }
2579
Douglas Gregor36897b02010-09-10 00:22:18 +00002580 case CXCursor_LabelRef: {
2581 LabelStmt *Label = getCursorLabelRef(C).first;
2582 assert(Label && "Missing label");
2583
2584 return createCXString(Label->getID()->getName());
2585 }
2586
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00002587 case CXCursor_OverloadedDeclRef: {
2588 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
2589 if (Decl *D = Storage.dyn_cast<Decl *>()) {
2590 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
2591 return createCXString(ND->getNameAsString());
2592 return createCXString("");
2593 }
2594 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
2595 return createCXString(E->getName().getAsString());
2596 OverloadedTemplateStorage *Ovl
2597 = Storage.get<OverloadedTemplateStorage*>();
2598 if (Ovl->size() == 0)
2599 return createCXString("");
2600 return createCXString((*Ovl->begin())->getNameAsString());
2601 }
2602
Daniel Dunbaracca7252009-11-30 20:42:49 +00002603 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002604 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00002605 }
2606 }
Douglas Gregor97b98722010-01-19 23:20:36 +00002607
2608 if (clang_isExpression(C.kind)) {
2609 Decl *D = getDeclFromExpr(getCursorExpr(C));
2610 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00002611 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002612 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00002613 }
2614
Douglas Gregor36897b02010-09-10 00:22:18 +00002615 if (clang_isStatement(C.kind)) {
2616 Stmt *S = getCursorStmt(C);
2617 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
2618 return createCXString(Label->getID()->getName());
2619
2620 return createCXString("");
2621 }
2622
Douglas Gregor4ae8f292010-03-18 17:52:52 +00002623 if (C.kind == CXCursor_MacroInstantiation)
2624 return createCXString(getCursorMacroInstantiation(C)->getName()
2625 ->getNameStart());
2626
Douglas Gregor572feb22010-03-18 18:04:21 +00002627 if (C.kind == CXCursor_MacroDefinition)
2628 return createCXString(getCursorMacroDefinition(C)->getName()
2629 ->getNameStart());
2630
Douglas Gregorecdcb882010-10-20 22:00:55 +00002631 if (C.kind == CXCursor_InclusionDirective)
2632 return createCXString(getCursorInclusionDirective(C)->getFileName());
2633
Douglas Gregor60cbfac2010-01-25 16:56:17 +00002634 if (clang_isDeclaration(C.kind))
2635 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00002636
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002637 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00002638}
2639
Douglas Gregor358559d2010-10-02 22:49:11 +00002640CXString clang_getCursorDisplayName(CXCursor C) {
2641 if (!clang_isDeclaration(C.kind))
2642 return clang_getCursorSpelling(C);
2643
2644 Decl *D = getCursorDecl(C);
2645 if (!D)
2646 return createCXString("");
2647
2648 PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy;
2649 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
2650 D = FunTmpl->getTemplatedDecl();
2651
2652 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
2653 llvm::SmallString<64> Str;
2654 llvm::raw_svector_ostream OS(Str);
2655 OS << Function->getNameAsString();
2656 if (Function->getPrimaryTemplate())
2657 OS << "<>";
2658 OS << "(";
2659 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
2660 if (I)
2661 OS << ", ";
2662 OS << Function->getParamDecl(I)->getType().getAsString(Policy);
2663 }
2664
2665 if (Function->isVariadic()) {
2666 if (Function->getNumParams())
2667 OS << ", ";
2668 OS << "...";
2669 }
2670 OS << ")";
2671 return createCXString(OS.str());
2672 }
2673
2674 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
2675 llvm::SmallString<64> Str;
2676 llvm::raw_svector_ostream OS(Str);
2677 OS << ClassTemplate->getNameAsString();
2678 OS << "<";
2679 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
2680 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
2681 if (I)
2682 OS << ", ";
2683
2684 NamedDecl *Param = Params->getParam(I);
2685 if (Param->getIdentifier()) {
2686 OS << Param->getIdentifier()->getName();
2687 continue;
2688 }
2689
2690 // There is no parameter name, which makes this tricky. Try to come up
2691 // with something useful that isn't too long.
2692 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2693 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
2694 else if (NonTypeTemplateParmDecl *NTTP
2695 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2696 OS << NTTP->getType().getAsString(Policy);
2697 else
2698 OS << "template<...> class";
2699 }
2700
2701 OS << ">";
2702 return createCXString(OS.str());
2703 }
2704
2705 if (ClassTemplateSpecializationDecl *ClassSpec
2706 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
2707 // If the type was explicitly written, use that.
2708 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
2709 return createCXString(TSInfo->getType().getAsString(Policy));
2710
2711 llvm::SmallString<64> Str;
2712 llvm::raw_svector_ostream OS(Str);
2713 OS << ClassSpec->getNameAsString();
2714 OS << TemplateSpecializationType::PrintTemplateArgumentList(
2715 ClassSpec->getTemplateArgs().getFlatArgumentList(),
2716 ClassSpec->getTemplateArgs().flat_size(),
2717 Policy);
2718 return createCXString(OS.str());
2719 }
2720
2721 return clang_getCursorSpelling(C);
2722}
2723
Ted Kremeneke68fff62010-02-17 00:41:32 +00002724CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00002725 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002726 case CXCursor_FunctionDecl:
2727 return createCXString("FunctionDecl");
2728 case CXCursor_TypedefDecl:
2729 return createCXString("TypedefDecl");
2730 case CXCursor_EnumDecl:
2731 return createCXString("EnumDecl");
2732 case CXCursor_EnumConstantDecl:
2733 return createCXString("EnumConstantDecl");
2734 case CXCursor_StructDecl:
2735 return createCXString("StructDecl");
2736 case CXCursor_UnionDecl:
2737 return createCXString("UnionDecl");
2738 case CXCursor_ClassDecl:
2739 return createCXString("ClassDecl");
2740 case CXCursor_FieldDecl:
2741 return createCXString("FieldDecl");
2742 case CXCursor_VarDecl:
2743 return createCXString("VarDecl");
2744 case CXCursor_ParmDecl:
2745 return createCXString("ParmDecl");
2746 case CXCursor_ObjCInterfaceDecl:
2747 return createCXString("ObjCInterfaceDecl");
2748 case CXCursor_ObjCCategoryDecl:
2749 return createCXString("ObjCCategoryDecl");
2750 case CXCursor_ObjCProtocolDecl:
2751 return createCXString("ObjCProtocolDecl");
2752 case CXCursor_ObjCPropertyDecl:
2753 return createCXString("ObjCPropertyDecl");
2754 case CXCursor_ObjCIvarDecl:
2755 return createCXString("ObjCIvarDecl");
2756 case CXCursor_ObjCInstanceMethodDecl:
2757 return createCXString("ObjCInstanceMethodDecl");
2758 case CXCursor_ObjCClassMethodDecl:
2759 return createCXString("ObjCClassMethodDecl");
2760 case CXCursor_ObjCImplementationDecl:
2761 return createCXString("ObjCImplementationDecl");
2762 case CXCursor_ObjCCategoryImplDecl:
2763 return createCXString("ObjCCategoryImplDecl");
Ted Kremenek8bd5a692010-04-13 23:39:06 +00002764 case CXCursor_CXXMethod:
2765 return createCXString("CXXMethod");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002766 case CXCursor_UnexposedDecl:
2767 return createCXString("UnexposedDecl");
2768 case CXCursor_ObjCSuperClassRef:
2769 return createCXString("ObjCSuperClassRef");
2770 case CXCursor_ObjCProtocolRef:
2771 return createCXString("ObjCProtocolRef");
2772 case CXCursor_ObjCClassRef:
2773 return createCXString("ObjCClassRef");
2774 case CXCursor_TypeRef:
2775 return createCXString("TypeRef");
Douglas Gregor0b36e612010-08-31 20:37:03 +00002776 case CXCursor_TemplateRef:
2777 return createCXString("TemplateRef");
Douglas Gregor69319002010-08-31 23:48:11 +00002778 case CXCursor_NamespaceRef:
2779 return createCXString("NamespaceRef");
Douglas Gregora67e03f2010-09-09 21:42:20 +00002780 case CXCursor_MemberRef:
2781 return createCXString("MemberRef");
Douglas Gregor36897b02010-09-10 00:22:18 +00002782 case CXCursor_LabelRef:
2783 return createCXString("LabelRef");
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00002784 case CXCursor_OverloadedDeclRef:
2785 return createCXString("OverloadedDeclRef");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002786 case CXCursor_UnexposedExpr:
2787 return createCXString("UnexposedExpr");
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00002788 case CXCursor_BlockExpr:
2789 return createCXString("BlockExpr");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002790 case CXCursor_DeclRefExpr:
2791 return createCXString("DeclRefExpr");
2792 case CXCursor_MemberRefExpr:
2793 return createCXString("MemberRefExpr");
2794 case CXCursor_CallExpr:
2795 return createCXString("CallExpr");
2796 case CXCursor_ObjCMessageExpr:
2797 return createCXString("ObjCMessageExpr");
2798 case CXCursor_UnexposedStmt:
2799 return createCXString("UnexposedStmt");
Douglas Gregor36897b02010-09-10 00:22:18 +00002800 case CXCursor_LabelStmt:
2801 return createCXString("LabelStmt");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002802 case CXCursor_InvalidFile:
2803 return createCXString("InvalidFile");
Ted Kremenek292db642010-03-19 20:39:05 +00002804 case CXCursor_InvalidCode:
2805 return createCXString("InvalidCode");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002806 case CXCursor_NoDeclFound:
2807 return createCXString("NoDeclFound");
2808 case CXCursor_NotImplemented:
2809 return createCXString("NotImplemented");
2810 case CXCursor_TranslationUnit:
2811 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00002812 case CXCursor_UnexposedAttr:
2813 return createCXString("UnexposedAttr");
2814 case CXCursor_IBActionAttr:
2815 return createCXString("attribute(ibaction)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002816 case CXCursor_IBOutletAttr:
2817 return createCXString("attribute(iboutlet)");
Ted Kremenek857e9182010-05-19 17:38:06 +00002818 case CXCursor_IBOutletCollectionAttr:
2819 return createCXString("attribute(iboutletcollection)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002820 case CXCursor_PreprocessingDirective:
2821 return createCXString("preprocessing directive");
Douglas Gregor572feb22010-03-18 18:04:21 +00002822 case CXCursor_MacroDefinition:
2823 return createCXString("macro definition");
Douglas Gregor48072312010-03-18 15:23:44 +00002824 case CXCursor_MacroInstantiation:
2825 return createCXString("macro instantiation");
Douglas Gregorecdcb882010-10-20 22:00:55 +00002826 case CXCursor_InclusionDirective:
2827 return createCXString("inclusion directive");
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00002828 case CXCursor_Namespace:
2829 return createCXString("Namespace");
Ted Kremeneka0536d82010-05-07 01:04:29 +00002830 case CXCursor_LinkageSpec:
2831 return createCXString("LinkageSpec");
Ted Kremenek3064ef92010-08-27 21:34:58 +00002832 case CXCursor_CXXBaseSpecifier:
2833 return createCXString("C++ base class specifier");
Douglas Gregor01829d32010-08-31 14:41:23 +00002834 case CXCursor_Constructor:
2835 return createCXString("CXXConstructor");
2836 case CXCursor_Destructor:
2837 return createCXString("CXXDestructor");
2838 case CXCursor_ConversionFunction:
2839 return createCXString("CXXConversion");
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00002840 case CXCursor_TemplateTypeParameter:
2841 return createCXString("TemplateTypeParameter");
2842 case CXCursor_NonTypeTemplateParameter:
2843 return createCXString("NonTypeTemplateParameter");
2844 case CXCursor_TemplateTemplateParameter:
2845 return createCXString("TemplateTemplateParameter");
2846 case CXCursor_FunctionTemplate:
2847 return createCXString("FunctionTemplate");
Douglas Gregor39d6f072010-08-31 19:02:00 +00002848 case CXCursor_ClassTemplate:
2849 return createCXString("ClassTemplate");
Douglas Gregor74dbe642010-08-31 19:31:58 +00002850 case CXCursor_ClassTemplatePartialSpecialization:
2851 return createCXString("ClassTemplatePartialSpecialization");
Douglas Gregor69319002010-08-31 23:48:11 +00002852 case CXCursor_NamespaceAlias:
2853 return createCXString("NamespaceAlias");
Douglas Gregor0a35bce2010-09-01 03:07:18 +00002854 case CXCursor_UsingDirective:
2855 return createCXString("UsingDirective");
Douglas Gregor7e242562010-09-01 19:52:22 +00002856 case CXCursor_UsingDeclaration:
2857 return createCXString("UsingDeclaration");
Steve Naroff89922f82009-08-31 00:59:03 +00002858 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002859
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00002860 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002861 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00002862}
Steve Naroff89922f82009-08-31 00:59:03 +00002863
Ted Kremeneke68fff62010-02-17 00:41:32 +00002864enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
2865 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00002866 CXClientData client_data) {
2867 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
2868 *BestCursor = cursor;
2869 return CXChildVisit_Recurse;
2870}
Ted Kremeneke68fff62010-02-17 00:41:32 +00002871
Douglas Gregorb9790342010-01-22 21:44:22 +00002872CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
2873 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00002874 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00002875
Douglas Gregorb9790342010-01-22 21:44:22 +00002876 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregorbdf60622010-03-05 21:16:25 +00002877 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2878
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00002879 // Translate the given source location to make it point at the beginning of
2880 // the token under the cursor.
Ted Kremeneka297de22010-01-25 22:34:44 +00002881 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Ted Kremeneka629ea42010-07-29 00:52:07 +00002882
2883 // Guard against an invalid SourceLocation, or we may assert in one
2884 // of the following calls.
2885 if (SLoc.isInvalid())
2886 return clang_getNullCursor();
2887
Douglas Gregor40749ee2010-11-03 00:35:38 +00002888 bool Logging = getenv("LIBCLANG_LOGGING");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00002889 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
2890 CXXUnit->getASTContext().getLangOptions());
2891
Douglas Gregor33e9abd2010-01-22 19:49:59 +00002892 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
2893 if (SLoc.isValid()) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +00002894 // FIXME: Would be great to have a "hint" cursor, then walk from that
2895 // hint cursor upward until we find a cursor whose source range encloses
2896 // the region of interest, rather than starting from the translation unit.
2897 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00002898 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00002899 Decl::MaxPCHLevel, SourceLocation(SLoc));
Douglas Gregor33e9abd2010-01-22 19:49:59 +00002900 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00002901 }
Douglas Gregor40749ee2010-11-03 00:35:38 +00002902
2903 if (Logging) {
2904 CXFile SearchFile;
2905 unsigned SearchLine, SearchColumn;
2906 CXFile ResultFile;
2907 unsigned ResultLine, ResultColumn;
2908 CXString SearchFileName, ResultFileName, KindSpelling;
2909 CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
2910
2911 clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
2912 0);
2913 clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine,
2914 &ResultColumn, 0);
2915 SearchFileName = clang_getFileName(SearchFile);
2916 ResultFileName = clang_getFileName(ResultFile);
2917 KindSpelling = clang_getCursorKindSpelling(Result.kind);
2918 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d)\n",
2919 clang_getCString(SearchFileName), SearchLine, SearchColumn,
2920 clang_getCString(KindSpelling),
2921 clang_getCString(ResultFileName), ResultLine, ResultColumn);
2922 clang_disposeString(SearchFileName);
2923 clang_disposeString(ResultFileName);
2924 clang_disposeString(KindSpelling);
2925 }
2926
Ted Kremeneke68fff62010-02-17 00:41:32 +00002927 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00002928}
2929
Ted Kremenek73885552009-11-17 19:28:59 +00002930CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00002931 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00002932}
2933
2934unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00002935 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00002936}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002937
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002938unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00002939 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
2940}
2941
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002942unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00002943 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
2944}
Steve Naroff2d4d6292009-08-31 14:26:51 +00002945
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002946unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00002947 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
2948}
2949
Douglas Gregor97b98722010-01-19 23:20:36 +00002950unsigned clang_isExpression(enum CXCursorKind K) {
2951 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
2952}
2953
2954unsigned clang_isStatement(enum CXCursorKind K) {
2955 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
2956}
2957
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002958unsigned clang_isTranslationUnit(enum CXCursorKind K) {
2959 return K == CXCursor_TranslationUnit;
2960}
2961
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002962unsigned clang_isPreprocessing(enum CXCursorKind K) {
2963 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
2964}
2965
Ted Kremenekad6eff62010-03-08 21:17:29 +00002966unsigned clang_isUnexposed(enum CXCursorKind K) {
2967 switch (K) {
2968 case CXCursor_UnexposedDecl:
2969 case CXCursor_UnexposedExpr:
2970 case CXCursor_UnexposedStmt:
2971 case CXCursor_UnexposedAttr:
2972 return true;
2973 default:
2974 return false;
2975 }
2976}
2977
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002978CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00002979 return C.kind;
2980}
2981
Douglas Gregor98258af2010-01-18 22:46:11 +00002982CXSourceLocation clang_getCursorLocation(CXCursor C) {
2983 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00002984 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002985 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00002986 std::pair<ObjCInterfaceDecl *, SourceLocation> P
2987 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00002988 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00002989 }
2990
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002991 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00002992 std::pair<ObjCProtocolDecl *, SourceLocation> P
2993 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00002994 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00002995 }
2996
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002997 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00002998 std::pair<ObjCInterfaceDecl *, SourceLocation> P
2999 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003000 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003001 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003002
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003003 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003004 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003005 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003006 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00003007
3008 case CXCursor_TemplateRef: {
3009 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C);
3010 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3011 }
3012
Douglas Gregor69319002010-08-31 23:48:11 +00003013 case CXCursor_NamespaceRef: {
3014 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
3015 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3016 }
3017
Douglas Gregora67e03f2010-09-09 21:42:20 +00003018 case CXCursor_MemberRef: {
3019 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
3020 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3021 }
3022
Ted Kremenek3064ef92010-08-27 21:34:58 +00003023 case CXCursor_CXXBaseSpecifier: {
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003024 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
3025 if (!BaseSpec)
3026 return clang_getNullLocation();
3027
3028 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
3029 return cxloc::translateSourceLocation(getCursorContext(C),
3030 TSInfo->getTypeLoc().getBeginLoc());
3031
3032 return cxloc::translateSourceLocation(getCursorContext(C),
3033 BaseSpec->getSourceRange().getBegin());
Ted Kremenek3064ef92010-08-27 21:34:58 +00003034 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003035
Douglas Gregor36897b02010-09-10 00:22:18 +00003036 case CXCursor_LabelRef: {
3037 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
3038 return cxloc::translateSourceLocation(getCursorContext(C), P.second);
3039 }
3040
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003041 case CXCursor_OverloadedDeclRef:
3042 return cxloc::translateSourceLocation(getCursorContext(C),
3043 getCursorOverloadedDeclRef(C).second);
3044
Douglas Gregorf46034a2010-01-18 23:41:10 +00003045 default:
3046 // FIXME: Need a way to enumerate all non-reference cases.
3047 llvm_unreachable("Missed a reference kind");
3048 }
Douglas Gregor98258af2010-01-18 22:46:11 +00003049 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003050
3051 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003052 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00003053 getLocationFromExpr(getCursorExpr(C)));
3054
Douglas Gregor36897b02010-09-10 00:22:18 +00003055 if (clang_isStatement(C.kind))
3056 return cxloc::translateSourceLocation(getCursorContext(C),
3057 getCursorStmt(C)->getLocStart());
3058
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003059 if (C.kind == CXCursor_PreprocessingDirective) {
3060 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
3061 return cxloc::translateSourceLocation(getCursorContext(C), L);
3062 }
Douglas Gregor48072312010-03-18 15:23:44 +00003063
3064 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00003065 SourceLocation L
3066 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
Douglas Gregor48072312010-03-18 15:23:44 +00003067 return cxloc::translateSourceLocation(getCursorContext(C), L);
3068 }
Douglas Gregor572feb22010-03-18 18:04:21 +00003069
3070 if (C.kind == CXCursor_MacroDefinition) {
3071 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
3072 return cxloc::translateSourceLocation(getCursorContext(C), L);
3073 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00003074
3075 if (C.kind == CXCursor_InclusionDirective) {
3076 SourceLocation L
3077 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
3078 return cxloc::translateSourceLocation(getCursorContext(C), L);
3079 }
3080
Ted Kremenek9a700d22010-05-12 06:16:13 +00003081 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
Douglas Gregor5352ac02010-01-28 00:27:43 +00003082 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00003083
Douglas Gregorf46034a2010-01-18 23:41:10 +00003084 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003085 SourceLocation Loc = D->getLocation();
3086 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
3087 Loc = Class->getClassLoc();
Ted Kremenek007a7c92010-11-01 23:26:51 +00003088 // FIXME: Multiple variables declared in a single declaration
3089 // currently lack the information needed to correctly determine their
3090 // ranges when accounting for the type-specifier. We use context
3091 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3092 // and if so, whether it is the first decl.
3093 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3094 if (!cxcursor::isFirstInDeclGroup(C))
3095 Loc = VD->getLocation();
3096 }
3097
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00003098 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00003099}
Douglas Gregora7bde202010-01-19 00:34:46 +00003100
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003101} // end extern "C"
3102
3103static SourceRange getRawCursorExtent(CXCursor C) {
Douglas Gregora7bde202010-01-19 00:34:46 +00003104 if (clang_isReference(C.kind)) {
3105 switch (C.kind) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003106 case CXCursor_ObjCSuperClassRef:
3107 return getCursorObjCSuperClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003108
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003109 case CXCursor_ObjCProtocolRef:
3110 return getCursorObjCProtocolRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003111
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003112 case CXCursor_ObjCClassRef:
3113 return getCursorObjCClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003114
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003115 case CXCursor_TypeRef:
3116 return getCursorTypeRef(C).second;
Douglas Gregor0b36e612010-08-31 20:37:03 +00003117
3118 case CXCursor_TemplateRef:
3119 return getCursorTemplateRef(C).second;
3120
Douglas Gregor69319002010-08-31 23:48:11 +00003121 case CXCursor_NamespaceRef:
3122 return getCursorNamespaceRef(C).second;
Douglas Gregora67e03f2010-09-09 21:42:20 +00003123
3124 case CXCursor_MemberRef:
3125 return getCursorMemberRef(C).second;
3126
Ted Kremenek3064ef92010-08-27 21:34:58 +00003127 case CXCursor_CXXBaseSpecifier:
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003128 return getCursorCXXBaseSpecifier(C)->getSourceRange();
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003129
Douglas Gregor36897b02010-09-10 00:22:18 +00003130 case CXCursor_LabelRef:
3131 return getCursorLabelRef(C).second;
3132
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003133 case CXCursor_OverloadedDeclRef:
3134 return getCursorOverloadedDeclRef(C).second;
3135
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003136 default:
3137 // FIXME: Need a way to enumerate all non-reference cases.
3138 llvm_unreachable("Missed a reference kind");
Douglas Gregora7bde202010-01-19 00:34:46 +00003139 }
3140 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003141
3142 if (clang_isExpression(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003143 return getCursorExpr(C)->getSourceRange();
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003144
3145 if (clang_isStatement(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003146 return getCursorStmt(C)->getSourceRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003147
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003148 if (C.kind == CXCursor_PreprocessingDirective)
3149 return cxcursor::getCursorPreprocessingDirective(C);
Douglas Gregor48072312010-03-18 15:23:44 +00003150
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003151 if (C.kind == CXCursor_MacroInstantiation)
3152 return cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
Douglas Gregor572feb22010-03-18 18:04:21 +00003153
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003154 if (C.kind == CXCursor_MacroDefinition)
3155 return cxcursor::getCursorMacroDefinition(C)->getSourceRange();
Douglas Gregorecdcb882010-10-20 22:00:55 +00003156
3157 if (C.kind == CXCursor_InclusionDirective)
3158 return cxcursor::getCursorInclusionDirective(C)->getSourceRange();
3159
Ted Kremenek007a7c92010-11-01 23:26:51 +00003160 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3161 Decl *D = cxcursor::getCursorDecl(C);
3162 SourceRange R = D->getSourceRange();
3163 // FIXME: Multiple variables declared in a single declaration
3164 // currently lack the information needed to correctly determine their
3165 // ranges when accounting for the type-specifier. We use context
3166 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3167 // and if so, whether it is the first decl.
3168 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3169 if (!cxcursor::isFirstInDeclGroup(C))
3170 R.setBegin(VD->getLocation());
3171 }
3172 return R;
3173 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00003174 return SourceRange();}
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003175
3176extern "C" {
3177
3178CXSourceRange clang_getCursorExtent(CXCursor C) {
3179 SourceRange R = getRawCursorExtent(C);
3180 if (R.isInvalid())
Douglas Gregor5352ac02010-01-28 00:27:43 +00003181 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003182
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003183 return cxloc::translateSourceRange(getCursorContext(C), R);
Douglas Gregora7bde202010-01-19 00:34:46 +00003184}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003185
3186CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003187 if (clang_isInvalid(C.kind))
3188 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003189
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003190 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003191 if (clang_isDeclaration(C.kind)) {
3192 Decl *D = getCursorDecl(C);
3193 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
3194 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), CXXUnit);
3195 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
3196 return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), CXXUnit);
3197 if (ObjCForwardProtocolDecl *Protocols
3198 = dyn_cast<ObjCForwardProtocolDecl>(D))
3199 return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), CXXUnit);
3200
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003201 return C;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003202 }
3203
Douglas Gregor97b98722010-01-19 23:20:36 +00003204 if (clang_isExpression(C.kind)) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003205 Expr *E = getCursorExpr(C);
3206 Decl *D = getDeclFromExpr(E);
Douglas Gregor97b98722010-01-19 23:20:36 +00003207 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003208 return MakeCXCursor(D, CXXUnit);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003209
3210 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
3211 return MakeCursorOverloadedDeclRef(Ovl, CXXUnit);
3212
Douglas Gregor97b98722010-01-19 23:20:36 +00003213 return clang_getNullCursor();
3214 }
3215
Douglas Gregor36897b02010-09-10 00:22:18 +00003216 if (clang_isStatement(C.kind)) {
3217 Stmt *S = getCursorStmt(C);
3218 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
3219 return MakeCXCursor(Goto->getLabel(), getCursorDecl(C),
3220 getCursorASTUnit(C));
3221
3222 return clang_getNullCursor();
3223 }
3224
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003225 if (C.kind == CXCursor_MacroInstantiation) {
3226 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
3227 return MakeMacroDefinitionCursor(Def, CXXUnit);
3228 }
3229
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003230 if (!clang_isReference(C.kind))
3231 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003232
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003233 switch (C.kind) {
3234 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003235 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003236
3237 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003238 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003239
3240 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003241 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003242
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003243 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003244 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Douglas Gregor0b36e612010-08-31 20:37:03 +00003245
3246 case CXCursor_TemplateRef:
3247 return MakeCXCursor(getCursorTemplateRef(C).first, CXXUnit);
3248
Douglas Gregor69319002010-08-31 23:48:11 +00003249 case CXCursor_NamespaceRef:
3250 return MakeCXCursor(getCursorNamespaceRef(C).first, CXXUnit);
3251
Douglas Gregora67e03f2010-09-09 21:42:20 +00003252 case CXCursor_MemberRef:
3253 return MakeCXCursor(getCursorMemberRef(C).first, CXXUnit);
3254
Ted Kremenek3064ef92010-08-27 21:34:58 +00003255 case CXCursor_CXXBaseSpecifier: {
3256 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
3257 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
3258 CXXUnit));
3259 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003260
Douglas Gregor36897b02010-09-10 00:22:18 +00003261 case CXCursor_LabelRef:
3262 // FIXME: We end up faking the "parent" declaration here because we
3263 // don't want to make CXCursor larger.
3264 return MakeCXCursor(getCursorLabelRef(C).first,
3265 CXXUnit->getASTContext().getTranslationUnitDecl(),
3266 CXXUnit);
3267
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003268 case CXCursor_OverloadedDeclRef:
3269 return C;
3270
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003271 default:
3272 // We would prefer to enumerate all non-reference cursor kinds here.
3273 llvm_unreachable("Unhandled reference cursor kind");
3274 break;
3275 }
3276 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003277
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003278 return clang_getNullCursor();
3279}
3280
Douglas Gregorb6998662010-01-19 19:34:47 +00003281CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003282 if (clang_isInvalid(C.kind))
3283 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003284
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003285 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003286
Douglas Gregorb6998662010-01-19 19:34:47 +00003287 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00003288 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00003289 C = clang_getCursorReferenced(C);
3290 WasReference = true;
3291 }
3292
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003293 if (C.kind == CXCursor_MacroInstantiation)
3294 return clang_getCursorReferenced(C);
3295
Douglas Gregorb6998662010-01-19 19:34:47 +00003296 if (!clang_isDeclaration(C.kind))
3297 return clang_getNullCursor();
3298
3299 Decl *D = getCursorDecl(C);
3300 if (!D)
3301 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003302
Douglas Gregorb6998662010-01-19 19:34:47 +00003303 switch (D->getKind()) {
3304 // Declaration kinds that don't really separate the notions of
3305 // declaration and definition.
3306 case Decl::Namespace:
3307 case Decl::Typedef:
3308 case Decl::TemplateTypeParm:
3309 case Decl::EnumConstant:
3310 case Decl::Field:
3311 case Decl::ObjCIvar:
3312 case Decl::ObjCAtDefsField:
3313 case Decl::ImplicitParam:
3314 case Decl::ParmVar:
3315 case Decl::NonTypeTemplateParm:
3316 case Decl::TemplateTemplateParm:
3317 case Decl::ObjCCategoryImpl:
3318 case Decl::ObjCImplementation:
Abramo Bagnara6206d532010-06-05 05:09:32 +00003319 case Decl::AccessSpec:
Douglas Gregorb6998662010-01-19 19:34:47 +00003320 case Decl::LinkageSpec:
3321 case Decl::ObjCPropertyImpl:
3322 case Decl::FileScopeAsm:
3323 case Decl::StaticAssert:
3324 case Decl::Block:
3325 return C;
3326
3327 // Declaration kinds that don't make any sense here, but are
3328 // nonetheless harmless.
3329 case Decl::TranslationUnit:
Douglas Gregorb6998662010-01-19 19:34:47 +00003330 break;
3331
3332 // Declaration kinds for which the definition is not resolvable.
3333 case Decl::UnresolvedUsingTypename:
3334 case Decl::UnresolvedUsingValue:
3335 break;
3336
3337 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003338 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
3339 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003340
3341 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003342 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003343
3344 case Decl::Enum:
3345 case Decl::Record:
3346 case Decl::CXXRecord:
3347 case Decl::ClassTemplateSpecialization:
3348 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00003349 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003350 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003351 return clang_getNullCursor();
3352
3353 case Decl::Function:
3354 case Decl::CXXMethod:
3355 case Decl::CXXConstructor:
3356 case Decl::CXXDestructor:
3357 case Decl::CXXConversion: {
3358 const FunctionDecl *Def = 0;
3359 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003360 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003361 return clang_getNullCursor();
3362 }
3363
3364 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00003365 // Ask the variable if it has a definition.
3366 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
3367 return MakeCXCursor(Def, CXXUnit);
3368 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00003369 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003370
Douglas Gregorb6998662010-01-19 19:34:47 +00003371 case Decl::FunctionTemplate: {
3372 const FunctionDecl *Def = 0;
3373 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003374 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003375 return clang_getNullCursor();
3376 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003377
Douglas Gregorb6998662010-01-19 19:34:47 +00003378 case Decl::ClassTemplate: {
3379 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00003380 ->getDefinition())
Douglas Gregor0b36e612010-08-31 20:37:03 +00003381 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003382 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003383 return clang_getNullCursor();
3384 }
3385
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003386 case Decl::Using:
3387 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
3388 D->getLocation(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003389
3390 case Decl::UsingShadow:
3391 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003392 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003393 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00003394
3395 case Decl::ObjCMethod: {
3396 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
3397 if (Method->isThisDeclarationADefinition())
3398 return C;
3399
3400 // Dig out the method definition in the associated
3401 // @implementation, if we have it.
3402 // FIXME: The ASTs should make finding the definition easier.
3403 if (ObjCInterfaceDecl *Class
3404 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
3405 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
3406 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
3407 Method->isInstanceMethod()))
3408 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003409 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003410
3411 return clang_getNullCursor();
3412 }
3413
3414 case Decl::ObjCCategory:
3415 if (ObjCCategoryImplDecl *Impl
3416 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003417 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003418 return clang_getNullCursor();
3419
3420 case Decl::ObjCProtocol:
3421 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
3422 return C;
3423 return clang_getNullCursor();
3424
3425 case Decl::ObjCInterface:
3426 // There are two notions of a "definition" for an Objective-C
3427 // class: the interface and its implementation. When we resolved a
3428 // reference to an Objective-C class, produce the @interface as
3429 // the definition; when we were provided with the interface,
3430 // produce the @implementation as the definition.
3431 if (WasReference) {
3432 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
3433 return C;
3434 } else if (ObjCImplementationDecl *Impl
3435 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003436 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003437 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003438
Douglas Gregorb6998662010-01-19 19:34:47 +00003439 case Decl::ObjCProperty:
3440 // FIXME: We don't really know where to find the
3441 // ObjCPropertyImplDecls that implement this property.
3442 return clang_getNullCursor();
3443
3444 case Decl::ObjCCompatibleAlias:
3445 if (ObjCInterfaceDecl *Class
3446 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
3447 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003448 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003449
Douglas Gregorb6998662010-01-19 19:34:47 +00003450 return clang_getNullCursor();
3451
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003452 case Decl::ObjCForwardProtocol:
3453 return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D),
3454 D->getLocation(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003455
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003456 case Decl::ObjCClass:
3457 return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(),
3458 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00003459
3460 case Decl::Friend:
3461 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003462 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00003463 return clang_getNullCursor();
3464
3465 case Decl::FriendTemplate:
3466 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003467 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00003468 return clang_getNullCursor();
3469 }
3470
3471 return clang_getNullCursor();
3472}
3473
3474unsigned clang_isCursorDefinition(CXCursor C) {
3475 if (!clang_isDeclaration(C.kind))
3476 return 0;
3477
3478 return clang_getCursorDefinition(C) == C;
3479}
3480
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003481unsigned clang_getNumOverloadedDecls(CXCursor C) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00003482 if (C.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003483 return 0;
3484
3485 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
3486 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
3487 return E->getNumDecls();
3488
3489 if (OverloadedTemplateStorage *S
3490 = Storage.dyn_cast<OverloadedTemplateStorage*>())
3491 return S->size();
3492
3493 Decl *D = Storage.get<Decl*>();
3494 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
3495 return Using->getNumShadowDecls();
3496 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
3497 return Classes->size();
3498 if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D))
3499 return Protocols->protocol_size();
3500
3501 return 0;
3502}
3503
3504CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00003505 if (cursor.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003506 return clang_getNullCursor();
3507
3508 if (index >= clang_getNumOverloadedDecls(cursor))
3509 return clang_getNullCursor();
3510
3511 ASTUnit *Unit = getCursorASTUnit(cursor);
3512 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
3513 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
3514 return MakeCXCursor(E->decls_begin()[index], Unit);
3515
3516 if (OverloadedTemplateStorage *S
3517 = Storage.dyn_cast<OverloadedTemplateStorage*>())
3518 return MakeCXCursor(S->begin()[index], Unit);
3519
3520 Decl *D = Storage.get<Decl*>();
3521 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
3522 // FIXME: This is, unfortunately, linear time.
3523 UsingDecl::shadow_iterator Pos = Using->shadow_begin();
3524 std::advance(Pos, index);
3525 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), Unit);
3526 }
3527
3528 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
3529 return MakeCXCursor(Classes->begin()[index].getInterface(), Unit);
3530
3531 if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D))
3532 return MakeCXCursor(Protocols->protocol_begin()[index], Unit);
3533
3534 return clang_getNullCursor();
3535}
3536
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00003537void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00003538 const char **startBuf,
3539 const char **endBuf,
3540 unsigned *startLine,
3541 unsigned *startColumn,
3542 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003543 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00003544 assert(getCursorDecl(C) && "CXCursor has null decl");
3545 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00003546 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
3547 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003548
Steve Naroff4ade6d62009-09-23 17:52:52 +00003549 SourceManager &SM = FD->getASTContext().getSourceManager();
3550 *startBuf = SM.getCharacterData(Body->getLBracLoc());
3551 *endBuf = SM.getCharacterData(Body->getRBracLoc());
3552 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
3553 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
3554 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
3555 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
3556}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003557
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003558void clang_enableStackTraces(void) {
3559 llvm::sys::PrintStackTraceOnErrorSignal();
3560}
3561
Ted Kremenekfb480492010-01-13 21:46:36 +00003562} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00003563
Ted Kremenekfb480492010-01-13 21:46:36 +00003564//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003565// Token-based Operations.
3566//===----------------------------------------------------------------------===//
3567
3568/* CXToken layout:
3569 * int_data[0]: a CXTokenKind
3570 * int_data[1]: starting token location
3571 * int_data[2]: token length
3572 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003573 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003574 * otherwise unused.
3575 */
3576extern "C" {
3577
3578CXTokenKind clang_getTokenKind(CXToken CXTok) {
3579 return static_cast<CXTokenKind>(CXTok.int_data[0]);
3580}
3581
3582CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
3583 switch (clang_getTokenKind(CXTok)) {
3584 case CXToken_Identifier:
3585 case CXToken_Keyword:
3586 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003587 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
3588 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003589
3590 case CXToken_Literal: {
3591 // We have stashed the starting pointer in the ptr_data field. Use it.
3592 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003593 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003594 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003595
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003596 case CXToken_Punctuation:
3597 case CXToken_Comment:
3598 break;
3599 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003600
3601 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003602 // deconstructing the source location.
3603 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
3604 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003605 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003606
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003607 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
3608 std::pair<FileID, unsigned> LocInfo
3609 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +00003610 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003611 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00003612 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
3613 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00003614 return createCXString("");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003615
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003616 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003617}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003618
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003619CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
3620 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
3621 if (!CXXUnit)
3622 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003623
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003624 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
3625 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
3626}
3627
3628CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
3629 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00003630 if (!CXXUnit)
3631 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003632
3633 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003634 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
3635}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003636
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003637void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
3638 CXToken **Tokens, unsigned *NumTokens) {
3639 if (Tokens)
3640 *Tokens = 0;
3641 if (NumTokens)
3642 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003643
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003644 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
3645 if (!CXXUnit || !Tokens || !NumTokens)
3646 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003647
Douglas Gregorbdf60622010-03-05 21:16:25 +00003648 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3649
Daniel Dunbar85b988f2010-02-14 08:31:57 +00003650 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003651 if (R.isInvalid())
3652 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003653
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003654 SourceManager &SourceMgr = CXXUnit->getSourceManager();
3655 std::pair<FileID, unsigned> BeginLocInfo
3656 = SourceMgr.getDecomposedLoc(R.getBegin());
3657 std::pair<FileID, unsigned> EndLocInfo
3658 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003659
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003660 // Cannot tokenize across files.
3661 if (BeginLocInfo.first != EndLocInfo.first)
3662 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003663
3664 // Create a lexer
Douglas Gregorf715ca12010-03-16 00:06:06 +00003665 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003666 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00003667 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
Douglas Gregor47a3fcd2010-03-16 20:26:15 +00003668 if (Invalid)
3669 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +00003670
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003671 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
3672 CXXUnit->getASTContext().getLangOptions(),
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003673 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003674 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003675
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003676 // Lex tokens until we hit the end of the range.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003677 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003678 llvm::SmallVector<CXToken, 32> CXTokens;
3679 Token Tok;
David Chisnall096428b2010-10-13 21:44:48 +00003680 bool previousWasAt = false;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003681 do {
3682 // Lex the next token
3683 Lex.LexFromRawLexer(Tok);
3684 if (Tok.is(tok::eof))
3685 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003686
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003687 // Initialize the CXToken.
3688 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003689
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003690 // - Common fields
3691 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
3692 CXTok.int_data[2] = Tok.getLength();
3693 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003694
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003695 // - Kind-specific fields
3696 if (Tok.isLiteral()) {
3697 CXTok.int_data[0] = CXToken_Literal;
3698 CXTok.ptr_data = (void *)Tok.getLiteralData();
3699 } else if (Tok.is(tok::identifier)) {
Douglas Gregoraea67db2010-03-15 22:54:52 +00003700 // Lookup the identifier to determine whether we have a keyword.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003701 std::pair<FileID, unsigned> LocInfo
3702 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Douglas Gregorf715ca12010-03-16 00:06:06 +00003703 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003704 llvm::StringRef Buf
Douglas Gregorf715ca12010-03-16 00:06:06 +00003705 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
3706 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00003707 return;
3708
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00003709 const char *StartPos = Buf.data() + LocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003710 IdentifierInfo *II
3711 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00003712
David Chisnall096428b2010-10-13 21:44:48 +00003713 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00003714 CXTok.int_data[0] = CXToken_Keyword;
3715 }
3716 else {
3717 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
3718 CXToken_Identifier
3719 : CXToken_Keyword;
3720 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003721 CXTok.ptr_data = II;
3722 } else if (Tok.is(tok::comment)) {
3723 CXTok.int_data[0] = CXToken_Comment;
3724 CXTok.ptr_data = 0;
3725 } else {
3726 CXTok.int_data[0] = CXToken_Punctuation;
3727 CXTok.ptr_data = 0;
3728 }
3729 CXTokens.push_back(CXTok);
David Chisnall096428b2010-10-13 21:44:48 +00003730 previousWasAt = Tok.is(tok::at);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003731 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003732
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003733 if (CXTokens.empty())
3734 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003735
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003736 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
3737 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
3738 *NumTokens = CXTokens.size();
3739}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003740
Ted Kremenek6db61092010-05-05 00:55:15 +00003741void clang_disposeTokens(CXTranslationUnit TU,
3742 CXToken *Tokens, unsigned NumTokens) {
3743 free(Tokens);
3744}
3745
3746} // end: extern "C"
3747
3748//===----------------------------------------------------------------------===//
3749// Token annotation APIs.
3750//===----------------------------------------------------------------------===//
3751
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003752typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003753static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
3754 CXCursor parent,
3755 CXClientData client_data);
Ted Kremenek6db61092010-05-05 00:55:15 +00003756namespace {
3757class AnnotateTokensWorker {
3758 AnnotateTokensData &Annotated;
Ted Kremenek11949cb2010-05-05 00:55:17 +00003759 CXToken *Tokens;
3760 CXCursor *Cursors;
3761 unsigned NumTokens;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003762 unsigned TokIdx;
Douglas Gregor4419b672010-10-21 06:10:04 +00003763 unsigned PreprocessingTokIdx;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003764 CursorVisitor AnnotateVis;
3765 SourceManager &SrcMgr;
3766
3767 bool MoreTokens() const { return TokIdx < NumTokens; }
3768 unsigned NextToken() const { return TokIdx; }
3769 void AdvanceToken() { ++TokIdx; }
3770 SourceLocation GetTokenLoc(unsigned tokI) {
3771 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
3772 }
3773
Ted Kremenek6db61092010-05-05 00:55:15 +00003774public:
Ted Kremenek11949cb2010-05-05 00:55:17 +00003775 AnnotateTokensWorker(AnnotateTokensData &annotated,
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003776 CXToken *tokens, CXCursor *cursors, unsigned numTokens,
3777 ASTUnit *CXXUnit, SourceRange RegionOfInterest)
Ted Kremenek11949cb2010-05-05 00:55:17 +00003778 : Annotated(annotated), Tokens(tokens), Cursors(cursors),
Douglas Gregor4419b672010-10-21 06:10:04 +00003779 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003780 AnnotateVis(CXXUnit, AnnotateTokensVisitor, this,
3781 Decl::MaxPCHLevel, RegionOfInterest),
3782 SrcMgr(CXXUnit->getSourceManager()) {}
Ted Kremenek11949cb2010-05-05 00:55:17 +00003783
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003784 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
Ted Kremenek6db61092010-05-05 00:55:15 +00003785 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003786 void AnnotateTokens(CXCursor parent);
Ted Kremenek6db61092010-05-05 00:55:15 +00003787};
3788}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003789
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003790void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) {
3791 // Walk the AST within the region of interest, annotating tokens
3792 // along the way.
3793 VisitChildren(parent);
Ted Kremenek11949cb2010-05-05 00:55:17 +00003794
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003795 for (unsigned I = 0 ; I < TokIdx ; ++I) {
3796 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
Douglas Gregor4419b672010-10-21 06:10:04 +00003797 if (Pos != Annotated.end() &&
3798 (clang_isInvalid(Cursors[I].kind) ||
3799 Pos->second.kind != CXCursor_PreprocessingDirective))
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003800 Cursors[I] = Pos->second;
3801 }
3802
3803 // Finish up annotating any tokens left.
3804 if (!MoreTokens())
3805 return;
3806
3807 const CXCursor &C = clang_getNullCursor();
3808 for (unsigned I = TokIdx ; I < NumTokens ; ++I) {
3809 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
3810 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second;
Ted Kremenek11949cb2010-05-05 00:55:17 +00003811 }
3812}
3813
Ted Kremenek6db61092010-05-05 00:55:15 +00003814enum CXChildVisitResult
Douglas Gregor4419b672010-10-21 06:10:04 +00003815AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003816 CXSourceLocation Loc = clang_getCursorLocation(cursor);
Douglas Gregor4419b672010-10-21 06:10:04 +00003817 SourceRange cursorRange = getRawCursorExtent(cursor);
Douglas Gregor81d3c042010-11-01 20:13:04 +00003818 if (cursorRange.isInvalid())
3819 return CXChildVisit_Recurse;
3820
Douglas Gregor4419b672010-10-21 06:10:04 +00003821 if (clang_isPreprocessing(cursor.kind)) {
3822 // For macro instantiations, just note where the beginning of the macro
3823 // instantiation occurs.
3824 if (cursor.kind == CXCursor_MacroInstantiation) {
3825 Annotated[Loc.int_data] = cursor;
3826 return CXChildVisit_Recurse;
3827 }
3828
Douglas Gregor4419b672010-10-21 06:10:04 +00003829 // Items in the preprocessing record are kept separate from items in
3830 // declarations, so we keep a separate token index.
3831 unsigned SavedTokIdx = TokIdx;
3832 TokIdx = PreprocessingTokIdx;
3833
3834 // Skip tokens up until we catch up to the beginning of the preprocessing
3835 // entry.
3836 while (MoreTokens()) {
3837 const unsigned I = NextToken();
3838 SourceLocation TokLoc = GetTokenLoc(I);
3839 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
3840 case RangeBefore:
3841 AdvanceToken();
3842 continue;
3843 case RangeAfter:
3844 case RangeOverlap:
3845 break;
3846 }
3847 break;
3848 }
3849
3850 // Look at all of the tokens within this range.
3851 while (MoreTokens()) {
3852 const unsigned I = NextToken();
3853 SourceLocation TokLoc = GetTokenLoc(I);
3854 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
3855 case RangeBefore:
3856 assert(0 && "Infeasible");
3857 case RangeAfter:
3858 break;
3859 case RangeOverlap:
3860 Cursors[I] = cursor;
3861 AdvanceToken();
3862 continue;
3863 }
3864 break;
3865 }
3866
3867 // Save the preprocessing token index; restore the non-preprocessing
3868 // token index.
3869 PreprocessingTokIdx = TokIdx;
3870 TokIdx = SavedTokIdx;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003871 return CXChildVisit_Recurse;
3872 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003873
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003874 if (cursorRange.isInvalid())
3875 return CXChildVisit_Continue;
Ted Kremeneka333c662010-05-12 05:29:33 +00003876
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003877 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data);
3878
Ted Kremeneka333c662010-05-12 05:29:33 +00003879 // Adjust the annotated range based specific declarations.
3880 const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
3881 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) {
Ted Kremenek23173d72010-05-18 21:09:07 +00003882 Decl *D = cxcursor::getCursorDecl(cursor);
3883 // Don't visit synthesized ObjC methods, since they have no syntatic
3884 // representation in the source.
3885 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
3886 if (MD->isSynthesized())
3887 return CXChildVisit_Continue;
3888 }
3889 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
Ted Kremeneka333c662010-05-12 05:29:33 +00003890 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) {
3891 TypeLoc TL = TI->getTypeLoc();
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003892 SourceLocation TLoc = TL.getSourceRange().getBegin();
Douglas Gregor81d3c042010-11-01 20:13:04 +00003893 if (TLoc.isValid() && L.isValid() &&
Ted Kremenek6bfd5332010-05-13 15:38:38 +00003894 SrcMgr.isBeforeInTranslationUnit(TLoc, L))
Ted Kremeneka333c662010-05-12 05:29:33 +00003895 cursorRange.setBegin(TLoc);
Ted Kremeneka333c662010-05-12 05:29:33 +00003896 }
3897 }
3898 }
Douglas Gregor81d3c042010-11-01 20:13:04 +00003899
Ted Kremenek3f404602010-08-14 01:14:06 +00003900 // If the location of the cursor occurs within a macro instantiation, record
3901 // the spelling location of the cursor in our annotation map. We can then
3902 // paper over the token labelings during a post-processing step to try and
3903 // get cursor mappings for tokens that are the *arguments* of a macro
3904 // instantiation.
3905 if (L.isMacroID()) {
3906 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding();
3907 // Only invalidate the old annotation if it isn't part of a preprocessing
3908 // directive. Here we assume that the default construction of CXCursor
3909 // results in CXCursor.kind being an initialized value (i.e., 0). If
3910 // this isn't the case, we can fix by doing lookup + insertion.
Douglas Gregor4419b672010-10-21 06:10:04 +00003911
Ted Kremenek3f404602010-08-14 01:14:06 +00003912 CXCursor &oldC = Annotated[rawEncoding];
3913 if (!clang_isPreprocessing(oldC.kind))
3914 oldC = cursor;
3915 }
3916
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003917 const enum CXCursorKind K = clang_getCursorKind(parent);
3918 const CXCursor updateC =
Ted Kremenekd8b0a842010-08-25 22:16:02 +00003919 (clang_isInvalid(K) || K == CXCursor_TranslationUnit)
3920 ? clang_getNullCursor() : parent;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003921
3922 while (MoreTokens()) {
3923 const unsigned I = NextToken();
3924 SourceLocation TokLoc = GetTokenLoc(I);
3925 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
3926 case RangeBefore:
3927 Cursors[I] = updateC;
3928 AdvanceToken();
3929 continue;
3930 case RangeAfter:
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003931 case RangeOverlap:
3932 break;
3933 }
3934 break;
3935 }
3936
3937 // Visit children to get their cursor information.
3938 const unsigned BeforeChildren = NextToken();
3939 VisitChildren(cursor);
3940 const unsigned AfterChildren = NextToken();
3941
3942 // Adjust 'Last' to the last token within the extent of the cursor.
3943 while (MoreTokens()) {
3944 const unsigned I = NextToken();
3945 SourceLocation TokLoc = GetTokenLoc(I);
3946 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
3947 case RangeBefore:
3948 assert(0 && "Infeasible");
3949 case RangeAfter:
3950 break;
3951 case RangeOverlap:
3952 Cursors[I] = updateC;
3953 AdvanceToken();
3954 continue;
3955 }
3956 break;
3957 }
3958 const unsigned Last = NextToken();
Ted Kremenek6db61092010-05-05 00:55:15 +00003959
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003960 // Scan the tokens that are at the beginning of the cursor, but are not
3961 // capture by the child cursors.
3962
3963 // For AST elements within macros, rely on a post-annotate pass to
3964 // to correctly annotate the tokens with cursors. Otherwise we can
3965 // get confusing results of having tokens that map to cursors that really
3966 // are expanded by an instantiation.
3967 if (L.isMacroID())
3968 cursor = clang_getNullCursor();
3969
3970 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
3971 if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
3972 break;
Douglas Gregor4419b672010-10-21 06:10:04 +00003973
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003974 Cursors[I] = cursor;
3975 }
3976 // Scan the tokens that are at the end of the cursor, but are not captured
3977 // but the child cursors.
3978 for (unsigned I = AfterChildren; I != Last; ++I)
3979 Cursors[I] = cursor;
3980
3981 TokIdx = Last;
3982 return CXChildVisit_Continue;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003983}
3984
Ted Kremenek6db61092010-05-05 00:55:15 +00003985static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
3986 CXCursor parent,
3987 CXClientData client_data) {
3988 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
3989}
3990
3991extern "C" {
3992
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003993void clang_annotateTokens(CXTranslationUnit TU,
3994 CXToken *Tokens, unsigned NumTokens,
3995 CXCursor *Cursors) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003996
3997 if (NumTokens == 0 || !Tokens || !Cursors)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003998 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00003999
Douglas Gregor4419b672010-10-21 06:10:04 +00004000 // Any token we don't specifically annotate will have a NULL cursor.
4001 CXCursor C = clang_getNullCursor();
4002 for (unsigned I = 0; I != NumTokens; ++I)
4003 Cursors[I] = C;
4004
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004005 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor4419b672010-10-21 06:10:04 +00004006 if (!CXXUnit)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004007 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004008
Douglas Gregorbdf60622010-03-05 21:16:25 +00004009 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004010
Douglas Gregor0396f462010-03-19 05:22:59 +00004011 // Determine the region of interest, which contains all of the tokens.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004012 SourceRange RegionOfInterest;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004013 RegionOfInterest.setBegin(cxloc::translateSourceLocation(
4014 clang_getTokenLocation(TU, Tokens[0])));
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00004015 RegionOfInterest.setEnd(cxloc::translateSourceLocation(
4016 clang_getTokenLocation(TU,
4017 Tokens[NumTokens - 1])));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004018
Douglas Gregor0396f462010-03-19 05:22:59 +00004019 // A mapping from the source locations found when re-lexing or traversing the
4020 // region of interest to the corresponding cursors.
4021 AnnotateTokensData Annotated;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004022
4023 // Relex the tokens within the source range to look for preprocessing
Douglas Gregor0396f462010-03-19 05:22:59 +00004024 // directives.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004025 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4026 std::pair<FileID, unsigned> BeginLocInfo
4027 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
4028 std::pair<FileID, unsigned> EndLocInfo
4029 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004030
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004031 llvm::StringRef Buffer;
Douglas Gregor0396f462010-03-19 05:22:59 +00004032 bool Invalid = false;
4033 if (BeginLocInfo.first == EndLocInfo.first &&
4034 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
4035 !Invalid) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004036 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4037 CXXUnit->getASTContext().getLangOptions(),
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004038 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004039 Buffer.end());
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004040 Lex.SetCommentRetentionState(true);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004041
4042 // Lex tokens in raw mode until we hit the end of the range, to avoid
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004043 // entering #includes or expanding macros.
Douglas Gregor48072312010-03-18 15:23:44 +00004044 while (true) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004045 Token Tok;
4046 Lex.LexFromRawLexer(Tok);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004047
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004048 reprocess:
4049 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
4050 // We have found a preprocessing directive. Gobble it up so that we
4051 // don't see it while preprocessing these tokens later, but keep track of
4052 // all of the token locations inside this preprocessing directive so that
4053 // we can annotate them appropriately.
4054 //
4055 // FIXME: Some simple tests here could identify macro definitions and
4056 // #undefs, to provide specific cursor kinds for those.
4057 std::vector<SourceLocation> Locations;
4058 do {
4059 Locations.push_back(Tok.getLocation());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004060 Lex.LexFromRawLexer(Tok);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004061 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004062
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004063 using namespace cxcursor;
4064 CXCursor Cursor
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004065 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
4066 Locations.back()),
Ted Kremenek6db61092010-05-05 00:55:15 +00004067 CXXUnit);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004068 for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
4069 Annotated[Locations[I].getRawEncoding()] = Cursor;
4070 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004071
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004072 if (Tok.isAtStartOfLine())
4073 goto reprocess;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004074
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004075 continue;
4076 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004077
Douglas Gregor48072312010-03-18 15:23:44 +00004078 if (Tok.is(tok::eof))
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004079 break;
4080 }
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004081 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004082
Douglas Gregor0396f462010-03-19 05:22:59 +00004083 // Annotate all of the source locations in the region of interest that map to
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004084 // a specific cursor.
4085 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
4086 CXXUnit, RegionOfInterest);
4087 W.AnnotateTokens(clang_getTranslationUnitCursor(CXXUnit));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004088}
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004089} // end: extern "C"
4090
4091//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00004092// Operations for querying linkage of a cursor.
4093//===----------------------------------------------------------------------===//
4094
4095extern "C" {
4096CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
Douglas Gregor0396f462010-03-19 05:22:59 +00004097 if (!clang_isDeclaration(cursor.kind))
4098 return CXLinkage_Invalid;
4099
Ted Kremenek16b42592010-03-03 06:36:57 +00004100 Decl *D = cxcursor::getCursorDecl(cursor);
4101 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
4102 switch (ND->getLinkage()) {
4103 case NoLinkage: return CXLinkage_NoLinkage;
4104 case InternalLinkage: return CXLinkage_Internal;
4105 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
4106 case ExternalLinkage: return CXLinkage_External;
4107 };
4108
4109 return CXLinkage_Invalid;
4110}
4111} // end: extern "C"
4112
4113//===----------------------------------------------------------------------===//
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004114// Operations for querying language of a cursor.
4115//===----------------------------------------------------------------------===//
4116
4117static CXLanguageKind getDeclLanguage(const Decl *D) {
4118 switch (D->getKind()) {
4119 default:
4120 break;
4121 case Decl::ImplicitParam:
4122 case Decl::ObjCAtDefsField:
4123 case Decl::ObjCCategory:
4124 case Decl::ObjCCategoryImpl:
4125 case Decl::ObjCClass:
4126 case Decl::ObjCCompatibleAlias:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004127 case Decl::ObjCForwardProtocol:
4128 case Decl::ObjCImplementation:
4129 case Decl::ObjCInterface:
4130 case Decl::ObjCIvar:
4131 case Decl::ObjCMethod:
4132 case Decl::ObjCProperty:
4133 case Decl::ObjCPropertyImpl:
4134 case Decl::ObjCProtocol:
4135 return CXLanguage_ObjC;
4136 case Decl::CXXConstructor:
4137 case Decl::CXXConversion:
4138 case Decl::CXXDestructor:
4139 case Decl::CXXMethod:
4140 case Decl::CXXRecord:
4141 case Decl::ClassTemplate:
4142 case Decl::ClassTemplatePartialSpecialization:
4143 case Decl::ClassTemplateSpecialization:
4144 case Decl::Friend:
4145 case Decl::FriendTemplate:
4146 case Decl::FunctionTemplate:
4147 case Decl::LinkageSpec:
4148 case Decl::Namespace:
4149 case Decl::NamespaceAlias:
4150 case Decl::NonTypeTemplateParm:
4151 case Decl::StaticAssert:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004152 case Decl::TemplateTemplateParm:
4153 case Decl::TemplateTypeParm:
4154 case Decl::UnresolvedUsingTypename:
4155 case Decl::UnresolvedUsingValue:
4156 case Decl::Using:
4157 case Decl::UsingDirective:
4158 case Decl::UsingShadow:
4159 return CXLanguage_CPlusPlus;
4160 }
4161
4162 return CXLanguage_C;
4163}
4164
4165extern "C" {
Douglas Gregor58ddb602010-08-23 23:00:57 +00004166
4167enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
4168 if (clang_isDeclaration(cursor.kind))
4169 if (Decl *D = cxcursor::getCursorDecl(cursor)) {
4170 if (D->hasAttr<UnavailableAttr>() ||
4171 (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()))
4172 return CXAvailability_Available;
4173
4174 if (D->hasAttr<DeprecatedAttr>())
4175 return CXAvailability_Deprecated;
4176 }
4177
4178 return CXAvailability_Available;
4179}
4180
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004181CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
4182 if (clang_isDeclaration(cursor.kind))
4183 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
4184
4185 return CXLanguage_Invalid;
4186}
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004187
4188CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
4189 if (clang_isDeclaration(cursor.kind)) {
4190 if (Decl *D = getCursorDecl(cursor)) {
4191 DeclContext *DC = D->getDeclContext();
4192 return MakeCXCursor(cast<Decl>(DC), getCursorASTUnit(cursor));
4193 }
4194 }
4195
4196 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
4197 if (Decl *D = getCursorDecl(cursor))
4198 return MakeCXCursor(D, getCursorASTUnit(cursor));
4199 }
4200
4201 return clang_getNullCursor();
4202}
4203
4204CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
4205 if (clang_isDeclaration(cursor.kind)) {
4206 if (Decl *D = getCursorDecl(cursor)) {
4207 DeclContext *DC = D->getLexicalDeclContext();
4208 return MakeCXCursor(cast<Decl>(DC), getCursorASTUnit(cursor));
4209 }
4210 }
4211
4212 // FIXME: Note that we can't easily compute the lexical context of a
4213 // statement or expression, so we return nothing.
4214 return clang_getNullCursor();
4215}
4216
Douglas Gregor9f592342010-10-01 20:25:15 +00004217static void CollectOverriddenMethods(DeclContext *Ctx,
4218 ObjCMethodDecl *Method,
4219 llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) {
4220 if (!Ctx)
4221 return;
4222
4223 // If we have a class or category implementation, jump straight to the
4224 // interface.
4225 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
4226 return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods);
4227
4228 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
4229 if (!Container)
4230 return;
4231
4232 // Check whether we have a matching method at this level.
4233 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
4234 Method->isInstanceMethod()))
4235 if (Method != Overridden) {
4236 // We found an override at this level; there is no need to look
4237 // into other protocols or categories.
4238 Methods.push_back(Overridden);
4239 return;
4240 }
4241
4242 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4243 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
4244 PEnd = Protocol->protocol_end();
4245 P != PEnd; ++P)
4246 CollectOverriddenMethods(*P, Method, Methods);
4247 }
4248
4249 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
4250 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
4251 PEnd = Category->protocol_end();
4252 P != PEnd; ++P)
4253 CollectOverriddenMethods(*P, Method, Methods);
4254 }
4255
4256 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
4257 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
4258 PEnd = Interface->protocol_end();
4259 P != PEnd; ++P)
4260 CollectOverriddenMethods(*P, Method, Methods);
4261
4262 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
4263 Category; Category = Category->getNextClassCategory())
4264 CollectOverriddenMethods(Category, Method, Methods);
4265
4266 // We only look into the superclass if we haven't found anything yet.
4267 if (Methods.empty())
4268 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
4269 return CollectOverriddenMethods(Super, Method, Methods);
4270 }
4271}
4272
4273void clang_getOverriddenCursors(CXCursor cursor,
4274 CXCursor **overridden,
4275 unsigned *num_overridden) {
4276 if (overridden)
4277 *overridden = 0;
4278 if (num_overridden)
4279 *num_overridden = 0;
4280 if (!overridden || !num_overridden)
4281 return;
4282
4283 if (!clang_isDeclaration(cursor.kind))
4284 return;
4285
4286 Decl *D = getCursorDecl(cursor);
4287 if (!D)
4288 return;
4289
4290 // Handle C++ member functions.
4291 ASTUnit *CXXUnit = getCursorASTUnit(cursor);
4292 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
4293 *num_overridden = CXXMethod->size_overridden_methods();
4294 if (!*num_overridden)
4295 return;
4296
4297 *overridden = new CXCursor [*num_overridden];
4298 unsigned I = 0;
4299 for (CXXMethodDecl::method_iterator
4300 M = CXXMethod->begin_overridden_methods(),
4301 MEnd = CXXMethod->end_overridden_methods();
4302 M != MEnd; (void)++M, ++I)
4303 (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), CXXUnit);
4304 return;
4305 }
4306
4307 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
4308 if (!Method)
4309 return;
4310
4311 // Handle Objective-C methods.
4312 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
4313 CollectOverriddenMethods(Method->getDeclContext(), Method, Methods);
4314
4315 if (Methods.empty())
4316 return;
4317
4318 *num_overridden = Methods.size();
4319 *overridden = new CXCursor [Methods.size()];
4320 for (unsigned I = 0, N = Methods.size(); I != N; ++I)
4321 (*overridden)[I] = MakeCXCursor(Methods[I], CXXUnit);
4322}
4323
4324void clang_disposeOverriddenCursors(CXCursor *overridden) {
4325 delete [] overridden;
4326}
4327
Douglas Gregorecdcb882010-10-20 22:00:55 +00004328CXFile clang_getIncludedFile(CXCursor cursor) {
4329 if (cursor.kind != CXCursor_InclusionDirective)
4330 return 0;
4331
4332 InclusionDirective *ID = getCursorInclusionDirective(cursor);
4333 return (void *)ID->getFile();
4334}
4335
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004336} // end: extern "C"
4337
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004338
4339//===----------------------------------------------------------------------===//
4340// C++ AST instrospection.
4341//===----------------------------------------------------------------------===//
4342
4343extern "C" {
4344unsigned clang_CXXMethod_isStatic(CXCursor C) {
4345 if (!clang_isDeclaration(C.kind))
4346 return 0;
Douglas Gregor49f6f542010-08-31 22:12:17 +00004347
4348 CXXMethodDecl *Method = 0;
4349 Decl *D = cxcursor::getCursorDecl(C);
4350 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
4351 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
4352 else
4353 Method = dyn_cast_or_null<CXXMethodDecl>(D);
4354 return (Method && Method->isStatic()) ? 1 : 0;
Ted Kremenek40b492a2010-05-17 20:12:45 +00004355}
Ted Kremenekb12903e2010-05-18 22:32:15 +00004356
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004357} // end: extern "C"
4358
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004359//===----------------------------------------------------------------------===//
Ted Kremenek95f33552010-08-26 01:42:22 +00004360// Attribute introspection.
4361//===----------------------------------------------------------------------===//
4362
4363extern "C" {
4364CXType clang_getIBOutletCollectionType(CXCursor C) {
4365 if (C.kind != CXCursor_IBOutletCollectionAttr)
4366 return cxtype::MakeCXType(QualType(), cxcursor::getCursorASTUnit(C));
4367
4368 IBOutletCollectionAttr *A =
4369 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
4370
4371 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorASTUnit(C));
4372}
4373} // end: extern "C"
4374
4375//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00004376// CXString Operations.
4377//===----------------------------------------------------------------------===//
4378
4379extern "C" {
4380const char *clang_getCString(CXString string) {
4381 return string.Spelling;
4382}
4383
4384void clang_disposeString(CXString string) {
4385 if (string.MustFreeString && string.Spelling)
4386 free((void*)string.Spelling);
4387}
Ted Kremenek04bb7162010-01-22 22:44:15 +00004388
Ted Kremenekfb480492010-01-13 21:46:36 +00004389} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00004390
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004391namespace clang { namespace cxstring {
4392CXString createCXString(const char *String, bool DupString){
4393 CXString Str;
4394 if (DupString) {
4395 Str.Spelling = strdup(String);
4396 Str.MustFreeString = 1;
4397 } else {
4398 Str.Spelling = String;
4399 Str.MustFreeString = 0;
4400 }
4401 return Str;
4402}
4403
4404CXString createCXString(llvm::StringRef String, bool DupString) {
4405 CXString Result;
4406 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
4407 char *Spelling = (char *)malloc(String.size() + 1);
4408 memmove(Spelling, String.data(), String.size());
4409 Spelling[String.size()] = 0;
4410 Result.Spelling = Spelling;
4411 Result.MustFreeString = 1;
4412 } else {
4413 Result.Spelling = String.data();
4414 Result.MustFreeString = 0;
4415 }
4416 return Result;
4417}
4418}}
4419
Ted Kremenek04bb7162010-01-22 22:44:15 +00004420//===----------------------------------------------------------------------===//
4421// Misc. utility functions.
4422//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004423
Ted Kremenek04bb7162010-01-22 22:44:15 +00004424extern "C" {
4425
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00004426CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004427 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00004428}
4429
4430} // end: extern "C"