| Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | //===- 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 Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 7 | // | 
| Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
| Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 10 | // This file implements the main API hooks in the Clang-C Source Indexing | 
|  | 11 | // library. | 
| Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 15 | #include "CIndexer.h" | 
| Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 16 | #include "CXCursor.h" | 
| Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 17 | #include "CXType.h" | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 18 | #include "CXSourceLocation.h" | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 19 | #include "CIndexDiagnostic.h" | 
| Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 20 |  | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Version.h" | 
| Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 22 |  | 
| Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" | 
| Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtVisitor.h" | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLocVisitor.h" | 
| Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Diagnostic.h" | 
|  | 27 | #include "clang/Frontend/ASTUnit.h" | 
|  | 28 | #include "clang/Frontend/CompilerInstance.h" | 
| Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/FrontendDiagnostic.h" | 
| Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Lexer.h" | 
| Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 31 | #include "clang/Lex/PreprocessingRecord.h" | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Optional.h" | 
|  | 35 | #include "clang/Analysis/Support/SaveAndRestore.h" | 
| Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CrashRecoveryContext.h" | 
| Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PrettyStackTrace.h" | 
| Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" | 
| Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" | 
| Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Timer.h" | 
| Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 41 | #include "llvm/System/Mutex.h" | 
| Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 42 | #include "llvm/System/Program.h" | 
| Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 43 | #include "llvm/System/Signals.h" | 
| Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 44 | #include "llvm/System/Threading.h" | 
| Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 45 |  | 
| Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 46 | // Needed to define L_TMPNAM on some systems. | 
|  | 47 | #include <cstdio> | 
|  | 48 |  | 
| Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 49 | using namespace clang; | 
| Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 50 | using namespace clang::cxcursor; | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 51 | using namespace clang::cxstring; | 
| Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 52 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 53 | /// \brief The result of comparing two source ranges. | 
|  | 54 | enum RangeComparisonResult { | 
|  | 55 | /// \brief Either the ranges overlap or one of the ranges is invalid. | 
|  | 56 | RangeOverlap, | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 57 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 58 | /// \brief The first range ends before the second range starts. | 
|  | 59 | RangeBefore, | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 60 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 61 | /// \brief The first range starts after the second range ends. | 
|  | 62 | RangeAfter | 
|  | 63 | }; | 
|  | 64 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 65 | /// \brief Compare two source ranges to determine their relative position in | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 66 | /// the translation unit. | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 67 | static RangeComparisonResult RangeCompare(SourceManager &SM, | 
|  | 68 | SourceRange R1, | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 69 | SourceRange R2) { | 
|  | 70 | assert(R1.isValid() && "First range is invalid?"); | 
|  | 71 | assert(R2.isValid() && "Second range is invalid?"); | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 72 | if (R1.getEnd() != R2.getBegin() && | 
| Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 73 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 74 | return RangeBefore; | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 75 | if (R2.getEnd() != R1.getBegin() && | 
| Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 76 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 77 | return RangeAfter; | 
|  | 78 | return RangeOverlap; | 
|  | 79 | } | 
|  | 80 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 81 | /// \brief Determine if a source location falls within, before, or after a | 
|  | 82 | ///   a given source range. | 
|  | 83 | static 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 Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 87 | if (L == R.getBegin() || L == R.getEnd()) | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 88 | return RangeOverlap; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 89 | 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 Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 96 | /// \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 Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 102 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, | 
| Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 103 | const LangOptions &LangOpts, | 
| Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 104 | const CharSourceRange &R) { | 
| Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 105 | // We want the last character in this location, so we will adjust the | 
| Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 106 | // location accordingly. | 
| Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 107 | SourceLocation EndLoc = R.getEnd(); | 
| Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 108 | if (EndLoc.isValid() && EndLoc.isMacroID()) | 
|  | 109 | EndLoc = SM.getSpellingLoc(EndLoc); | 
| Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 110 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { | 
| Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 111 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); | 
| Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 112 | EndLoc = EndLoc.getFileLocWithOffset(Length); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, | 
|  | 116 | R.getBegin().getRawEncoding(), | 
|  | 117 | EndLoc.getRawEncoding() }; | 
|  | 118 | return Result; | 
|  | 119 | } | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 120 |  | 
| Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 122 | // Cursor visitor. | 
| Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// | 
|  | 124 |  | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 125 | namespace { | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 126 |  | 
|  | 127 | class VisitorJob { | 
|  | 128 | public: | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 129 | enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind, | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 130 | TypeLocVisitKind, OverloadExprPartsKind }; | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 131 | protected: | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 132 | void *dataA; | 
|  | 133 | void *dataB; | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 134 | CXCursor parent; | 
|  | 135 | Kind K; | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 136 | VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0) | 
|  | 137 | : dataA(d1), dataB(d2), parent(C), K(k) {} | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 138 | public: | 
|  | 139 | Kind getKind() const { return K; } | 
|  | 140 | const CXCursor &getParent() const { return parent; } | 
|  | 141 | static bool classof(VisitorJob *VJ) { return true; } | 
|  | 142 | }; | 
|  | 143 |  | 
|  | 144 | typedef llvm::SmallVector<VisitorJob, 10> VisitorWorkList; | 
|  | 145 |  | 
|  | 146 | #define DEF_JOB(NAME, DATA, KIND)\ | 
|  | 147 | class NAME : public VisitorJob {\ | 
|  | 148 | public:\ | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 149 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 150 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 151 | DATA *get() const { return static_cast<DATA*>(dataA); }\ | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 152 | }; | 
|  | 153 |  | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 154 | DEF_JOB(DeclVisit, Decl, DeclVisitKind) | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 155 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) | 
|  | 156 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 157 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 158 | #undef DEF_JOB | 
|  | 159 |  | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 160 | class TypeLocVisit : public VisitorJob { | 
|  | 161 | public: | 
|  | 162 | TypeLocVisit(TypeLoc tl, CXCursor parent) : | 
|  | 163 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, | 
|  | 164 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} | 
|  | 165 |  | 
|  | 166 | static bool classof(const VisitorJob *VJ) { | 
|  | 167 | return VJ->getKind() == TypeLocVisitKind; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | TypeLoc get() { | 
|  | 171 | QualType T = QualType::getFromOpaquePtr(dataA); | 
|  | 172 | return TypeLoc(T, dataB); | 
|  | 173 | } | 
|  | 174 | }; | 
|  | 175 |  | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 176 | static inline void WLAddStmt(VisitorWorkList &WL, CXCursor Parent, Stmt *S) { | 
|  | 177 | if (S) | 
|  | 178 | WL.push_back(StmtVisit(S, Parent)); | 
|  | 179 | } | 
|  | 180 | static inline void WLAddDecl(VisitorWorkList &WL, CXCursor Parent, Decl *D) { | 
|  | 181 | if (D) | 
|  | 182 | WL.push_back(DeclVisit(D, Parent)); | 
|  | 183 | } | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 184 | static inline void WLAddTypeLoc(VisitorWorkList &WL, CXCursor Parent, | 
|  | 185 | TypeSourceInfo *TI) { | 
|  | 186 | if (TI) | 
|  | 187 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); | 
|  | 188 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 189 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 190 | // Cursor visitor. | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 191 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 192 | public TypeLocVisitor<CursorVisitor, bool>, | 
|  | 193 | public StmtVisitor<CursorVisitor, bool> | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 194 | { | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 195 | /// \brief The translation unit we are traversing. | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 196 | ASTUnit *TU; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 197 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 198 | /// \brief The parent cursor whose children we are traversing. | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 199 | CXCursor Parent; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 200 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 201 | /// \brief The declaration that serves at the parent of any statement or | 
|  | 202 | /// expression nodes. | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 203 | Decl *StmtParent; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 204 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 205 | /// \brief The visitor function. | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 206 | CXCursorVisitor Visitor; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 207 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 208 | /// \brief The opaque client data, to be passed along to the visitor. | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 209 | CXClientData ClientData; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 210 |  | 
| Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 211 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on | 
|  | 212 | // to the visitor. Declarations with a PCH level greater than this value will | 
|  | 213 | // be suppressed. | 
|  | 214 | unsigned MaxPCHLevel; | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 215 |  | 
|  | 216 | /// \brief When valid, a source range to which the cursor should restrict | 
|  | 217 | /// its search. | 
|  | 218 | SourceRange RegionOfInterest; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 219 |  | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 220 | // FIXME: Eventually remove.  This part of a hack to support proper | 
|  | 221 | // iteration over all Decls contained lexically within an ObjC container. | 
|  | 222 | DeclContext::decl_iterator *DI_current; | 
|  | 223 | DeclContext::decl_iterator DE_current; | 
|  | 224 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 225 | using DeclVisitor<CursorVisitor, bool>::Visit; | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 226 | using TypeLocVisitor<CursorVisitor, bool>::Visit; | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 227 | using StmtVisitor<CursorVisitor, bool>::Visit; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 228 |  | 
|  | 229 | /// \brief Determine whether this particular source range comes before, comes | 
|  | 230 | /// after, or overlaps the region of interest. | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 231 | /// | 
| Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 232 | /// \param R a half-open source range retrieved from the abstract syntax tree. | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 233 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); | 
|  | 234 |  | 
| Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 235 | class SetParentRAII { | 
|  | 236 | CXCursor &Parent; | 
|  | 237 | Decl *&StmtParent; | 
|  | 238 | CXCursor OldParent; | 
|  | 239 |  | 
|  | 240 | public: | 
|  | 241 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) | 
|  | 242 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) | 
|  | 243 | { | 
|  | 244 | Parent = NewParent; | 
|  | 245 | if (clang_isDeclaration(Parent.kind)) | 
|  | 246 | StmtParent = getCursorDecl(Parent); | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | ~SetParentRAII() { | 
|  | 250 | Parent = OldParent; | 
|  | 251 | if (clang_isDeclaration(Parent.kind)) | 
|  | 252 | StmtParent = getCursorDecl(Parent); | 
|  | 253 | } | 
|  | 254 | }; | 
|  | 255 |  | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 256 | public: | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 257 | CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData, | 
|  | 258 | unsigned MaxPCHLevel, | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 259 | SourceRange RegionOfInterest = SourceRange()) | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 260 | : TU(TU), Visitor(Visitor), ClientData(ClientData), | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 261 | MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest), | 
|  | 262 | DI_current(0) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 263 | { | 
|  | 264 | Parent.kind = CXCursor_NoDeclFound; | 
|  | 265 | Parent.data[0] = 0; | 
|  | 266 | Parent.data[1] = 0; | 
|  | 267 | Parent.data[2] = 0; | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 268 | StmtParent = 0; | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 269 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 270 |  | 
| Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 271 | ASTUnit *getASTUnit() const { return TU; } | 
|  | 272 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 273 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); | 
| Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 274 |  | 
|  | 275 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> | 
|  | 276 | getPreprocessedEntities(); | 
|  | 277 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 278 | bool VisitChildren(CXCursor Parent); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 279 |  | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 280 | // Declaration visitors | 
| Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 281 | bool VisitAttributes(Decl *D); | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 282 | bool VisitBlockDecl(BlockDecl *B); | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 283 | bool VisitCXXRecordDecl(CXXRecordDecl *D); | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 284 | llvm::Optional<bool> shouldVisitCursor(CXCursor C); | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 285 | bool VisitDeclContext(DeclContext *DC); | 
| Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 286 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); | 
|  | 287 | bool VisitTypedefDecl(TypedefDecl *D); | 
| Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 288 | bool VisitTagDecl(TagDecl *D); | 
| Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 289 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); | 
| Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 290 | bool VisitClassTemplatePartialSpecializationDecl( | 
|  | 291 | ClassTemplatePartialSpecializationDecl *D); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 292 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); | 
| Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 293 | bool VisitEnumConstantDecl(EnumConstantDecl *D); | 
|  | 294 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); | 
|  | 295 | bool VisitFunctionDecl(FunctionDecl *ND); | 
|  | 296 | bool VisitFieldDecl(FieldDecl *D); | 
| Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 297 | bool VisitVarDecl(VarDecl *); | 
| Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 298 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 299 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); | 
| Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 300 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); | 
| Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 301 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); | 
| Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 302 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); | 
|  | 303 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); | 
|  | 304 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); | 
|  | 305 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 306 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); | 
| Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 307 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); | 
|  | 308 | bool VisitObjCImplDecl(ObjCImplDecl *D); | 
|  | 309 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); | 
|  | 310 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); | 
| Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 311 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. | 
|  | 312 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); | 
|  | 313 | bool VisitObjCClassDecl(ObjCClassDecl *D); | 
| Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 314 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); | 
| Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 315 | bool VisitNamespaceDecl(NamespaceDecl *D); | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 316 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 317 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 318 | bool VisitUsingDecl(UsingDecl *D); | 
|  | 319 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); | 
|  | 320 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 321 |  | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 322 | // Name visitor | 
|  | 323 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 324 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 325 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 326 | // Template visitors | 
|  | 327 | bool VisitTemplateParameters(const TemplateParameterList *Params); | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 328 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 329 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); | 
|  | 330 |  | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 331 | // Type visitors | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 332 | bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 333 | bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL); | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 334 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 335 | bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); | 
|  | 336 | bool VisitTagTypeLoc(TagTypeLoc TL); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 337 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 338 | bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); | 
| John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 339 | bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 340 | bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); | 
|  | 341 | bool VisitPointerTypeLoc(PointerTypeLoc TL); | 
|  | 342 | bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL); | 
|  | 343 | bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL); | 
|  | 344 | bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL); | 
|  | 345 | bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL); | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 346 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 347 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 348 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL); | 
| Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 349 | // FIXME: Implement visitors here when the unimplemented TypeLocs get | 
|  | 350 | // implemented | 
|  | 351 | bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL); | 
|  | 352 | bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 353 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 354 | // Statement visitors | 
|  | 355 | bool VisitStmt(Stmt *S); | 
|  | 356 | bool VisitDeclStmt(DeclStmt *S); | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 357 | bool VisitGotoStmt(GotoStmt *S); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 358 |  | 
| Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 359 | // Expression visitors | 
| Douglas Gregor | 8947a75 | 2010-09-02 20:35:02 +0000 | [diff] [blame] | 360 | bool VisitDeclRefExpr(DeclRefExpr *E); | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 361 | bool VisitBlockExpr(BlockExpr *B); | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 362 | bool VisitExplicitCastExpr(ExplicitCastExpr *E); | 
| Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 363 | bool VisitObjCMessageExpr(ObjCMessageExpr *E); | 
| Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 364 | bool VisitObjCEncodeExpr(ObjCEncodeExpr *E); | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 365 | bool VisitOffsetOfExpr(OffsetOfExpr *E); | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 366 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 367 | bool VisitAddrLabelExpr(AddrLabelExpr *E); | 
| Douglas Gregor | 648220e | 2010-08-10 15:02:34 +0000 | [diff] [blame] | 368 | bool VisitTypesCompatibleExpr(TypesCompatibleExpr *E); | 
|  | 369 | bool VisitVAArgExpr(VAArgExpr *E); | 
| Douglas Gregor | fa2e26f | 2010-09-09 23:28:23 +0000 | [diff] [blame] | 370 | bool VisitDesignatedInitExpr(DesignatedInitExpr *E); | 
| Douglas Gregor | 9480229 | 2010-09-02 21:20:16 +0000 | [diff] [blame] | 371 | bool VisitCXXTypeidExpr(CXXTypeidExpr *E); | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 372 | bool VisitCXXUuidofExpr(CXXUuidofExpr *E); | 
| Douglas Gregor | da135b1 | 2010-09-02 21:38:13 +0000 | [diff] [blame] | 373 | bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { return false; } | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 374 | bool VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); | 
|  | 375 | bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 376 | bool VisitCXXNewExpr(CXXNewExpr *E); | 
| Douglas Gregor | 6f7198f | 2010-09-02 22:09:03 +0000 | [diff] [blame] | 377 | bool VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 378 | bool VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); | 
| Douglas Gregor | bfebed2 | 2010-09-03 17:24:10 +0000 | [diff] [blame] | 379 | bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 380 | bool VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); | 
| Douglas Gregor | 25d6362 | 2010-09-03 17:35:34 +0000 | [diff] [blame] | 381 | bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 382 |  | 
|  | 383 | #define DATA_RECURSIVE_VISIT(NAME)\ | 
|  | 384 | bool Visit##NAME(NAME *S) { return VisitDataRecursive(S); } | 
|  | 385 | DATA_RECURSIVE_VISIT(BinaryOperator) | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 386 | DATA_RECURSIVE_VISIT(CompoundLiteralExpr) | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 387 | DATA_RECURSIVE_VISIT(CXXMemberCallExpr) | 
| Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 388 | DATA_RECURSIVE_VISIT(CXXOperatorCallExpr) | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 389 | DATA_RECURSIVE_VISIT(DoStmt) | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 390 | DATA_RECURSIVE_VISIT(IfStmt) | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 391 | DATA_RECURSIVE_VISIT(InitListExpr) | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 392 | DATA_RECURSIVE_VISIT(ForStmt) | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 393 | DATA_RECURSIVE_VISIT(MemberExpr) | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 394 | DATA_RECURSIVE_VISIT(OverloadExpr) | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 395 | DATA_RECURSIVE_VISIT(SwitchStmt) | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 396 | DATA_RECURSIVE_VISIT(WhileStmt) | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 397 | DATA_RECURSIVE_VISIT(UnresolvedMemberExpr) | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 398 |  | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 399 | // Data-recursive visitor functions. | 
|  | 400 | bool IsInRegionOfInterest(CXCursor C); | 
|  | 401 | bool RunVisitorWorkList(VisitorWorkList &WL); | 
|  | 402 | void EnqueueWorkList(VisitorWorkList &WL, Stmt *S); | 
|  | 403 | bool VisitDataRecursive(Stmt *S); | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 404 | }; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 405 |  | 
| Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 406 | } // end anonymous namespace | 
| Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 407 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 408 | static SourceRange getRawCursorExtent(CXCursor C); | 
|  | 409 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 410 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 411 | return RangeCompare(TU->getSourceManager(), R, RegionOfInterest); | 
|  | 412 | } | 
|  | 413 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 414 | /// \brief Visit the given cursor and, if requested by the visitor, | 
|  | 415 | /// its children. | 
|  | 416 | /// | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 417 | /// \param Cursor the cursor to visit. | 
|  | 418 | /// | 
|  | 419 | /// \param CheckRegionOfInterest if true, then the caller already checked that | 
|  | 420 | /// this cursor is within the region of interest. | 
|  | 421 | /// | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 422 | /// \returns true if the visitation should be aborted, false if it | 
|  | 423 | /// should continue. | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 424 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 425 | if (clang_isInvalid(Cursor.kind)) | 
|  | 426 | return false; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 427 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 428 | if (clang_isDeclaration(Cursor.kind)) { | 
|  | 429 | Decl *D = getCursorDecl(Cursor); | 
|  | 430 | assert(D && "Invalid declaration cursor"); | 
|  | 431 | if (D->getPCHLevel() > MaxPCHLevel) | 
|  | 432 | return false; | 
|  | 433 |  | 
|  | 434 | if (D->isImplicit()) | 
|  | 435 | return false; | 
|  | 436 | } | 
|  | 437 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 438 | // If we have a range of interest, and this cursor doesn't intersect with it, | 
|  | 439 | // we're done. | 
|  | 440 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 441 | SourceRange Range = getRawCursorExtent(Cursor); | 
| Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 442 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 443 | return false; | 
|  | 444 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 445 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 446 | switch (Visitor(Cursor, Parent, ClientData)) { | 
|  | 447 | case CXChildVisit_Break: | 
|  | 448 | return true; | 
|  | 449 |  | 
|  | 450 | case CXChildVisit_Continue: | 
|  | 451 | return false; | 
|  | 452 |  | 
|  | 453 | case CXChildVisit_Recurse: | 
|  | 454 | return VisitChildren(Cursor); | 
|  | 455 | } | 
|  | 456 |  | 
| Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 457 | return false; | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
| Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 460 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> | 
|  | 461 | CursorVisitor::getPreprocessedEntities() { | 
|  | 462 | PreprocessingRecord &PPRec | 
|  | 463 | = *TU->getPreprocessor().getPreprocessingRecord(); | 
|  | 464 |  | 
|  | 465 | bool OnlyLocalDecls | 
|  | 466 | = !TU->isMainFileAST() && TU->getOnlyLocalDecls(); | 
|  | 467 |  | 
|  | 468 | // There is no region of interest; we have to walk everything. | 
|  | 469 | if (RegionOfInterest.isInvalid()) | 
|  | 470 | return std::make_pair(PPRec.begin(OnlyLocalDecls), | 
|  | 471 | PPRec.end(OnlyLocalDecls)); | 
|  | 472 |  | 
|  | 473 | // Find the file in which the region of interest lands. | 
|  | 474 | SourceManager &SM = TU->getSourceManager(); | 
|  | 475 | std::pair<FileID, unsigned> Begin | 
|  | 476 | = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin()); | 
|  | 477 | std::pair<FileID, unsigned> End | 
|  | 478 | = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd()); | 
|  | 479 |  | 
|  | 480 | // The region of interest spans files; we have to walk everything. | 
|  | 481 | if (Begin.first != End.first) | 
|  | 482 | return std::make_pair(PPRec.begin(OnlyLocalDecls), | 
|  | 483 | PPRec.end(OnlyLocalDecls)); | 
|  | 484 |  | 
|  | 485 | ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap | 
|  | 486 | = TU->getPreprocessedEntitiesByFile(); | 
|  | 487 | if (ByFileMap.empty()) { | 
|  | 488 | // Build the mapping from files to sets of preprocessed entities. | 
|  | 489 | for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls), | 
|  | 490 | EEnd = PPRec.end(OnlyLocalDecls); | 
|  | 491 | E != EEnd; ++E) { | 
|  | 492 | std::pair<FileID, unsigned> P | 
|  | 493 | = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin()); | 
|  | 494 | ByFileMap[P.first].push_back(*E); | 
|  | 495 | } | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | return std::make_pair(ByFileMap[Begin.first].begin(), | 
|  | 499 | ByFileMap[Begin.first].end()); | 
|  | 500 | } | 
|  | 501 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 502 | /// \brief Visit the children of the given cursor. | 
|  | 503 | /// | 
|  | 504 | /// \returns true if the visitation should be aborted, false if it | 
|  | 505 | /// should continue. | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 506 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 507 | if (clang_isReference(Cursor.kind)) { | 
|  | 508 | // By definition, references have no children. | 
|  | 509 | return false; | 
|  | 510 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 511 |  | 
|  | 512 | // Set the Parent field to Cursor, then back to its old value once we're | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 513 | // done. | 
| Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 514 | SetParentRAII SetParent(Parent, StmtParent, Cursor); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 515 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 516 | if (clang_isDeclaration(Cursor.kind)) { | 
|  | 517 | Decl *D = getCursorDecl(Cursor); | 
|  | 518 | assert(D && "Invalid declaration cursor"); | 
| Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 519 | return VisitAttributes(D) || Visit(D); | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 520 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 521 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 522 | if (clang_isStatement(Cursor.kind)) | 
|  | 523 | return Visit(getCursorStmt(Cursor)); | 
|  | 524 | if (clang_isExpression(Cursor.kind)) | 
|  | 525 | return Visit(getCursorExpr(Cursor)); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 526 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 527 | if (clang_isTranslationUnit(Cursor.kind)) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 528 | ASTUnit *CXXUnit = getCursorASTUnit(Cursor); | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 529 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && | 
|  | 530 | RegionOfInterest.isInvalid()) { | 
| Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 531 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), | 
|  | 532 | TLEnd = CXXUnit->top_level_end(); | 
|  | 533 | TL != TLEnd; ++TL) { | 
|  | 534 | if (Visit(MakeCXCursor(*TL, CXXUnit), true)) | 
| Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 535 | return true; | 
|  | 536 | } | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 537 | } else if (VisitDeclContext( | 
|  | 538 | CXXUnit->getASTContext().getTranslationUnitDecl())) | 
|  | 539 | return true; | 
| Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 540 |  | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 541 | // Walk the preprocessing record. | 
| Daniel Dunbar | 8de30ff | 2010-03-20 01:11:56 +0000 | [diff] [blame] | 542 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 543 | // FIXME: Once we have the ability to deserialize a preprocessing record, | 
|  | 544 | // do so. | 
| Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 545 | PreprocessingRecord::iterator E, EEnd; | 
|  | 546 | for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) { | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 547 | if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { | 
|  | 548 | if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit))) | 
|  | 549 | return true; | 
| Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 550 |  | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 551 | continue; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { | 
|  | 555 | if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit))) | 
|  | 556 | return true; | 
|  | 557 |  | 
|  | 558 | continue; | 
|  | 559 | } | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 560 |  | 
|  | 561 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { | 
|  | 562 | if (Visit(MakeInclusionDirectiveCursor(ID, CXXUnit))) | 
|  | 563 | return true; | 
|  | 564 |  | 
|  | 565 | continue; | 
|  | 566 | } | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 567 | } | 
|  | 568 | } | 
| Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 569 | return false; | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 570 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 571 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 572 | // Nothing to visit at the moment. | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 573 | return false; | 
|  | 574 | } | 
|  | 575 |  | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 576 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { | 
| John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 577 | if (Visit(B->getSignatureAsWritten()->getTypeLoc())) | 
|  | 578 | return true; | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 579 |  | 
| Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 580 | if (Stmt *Body = B->getBody()) | 
|  | 581 | return Visit(MakeCXCursor(Body, StmtParent, TU)); | 
|  | 582 |  | 
|  | 583 | return false; | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 584 | } | 
|  | 585 |  | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 586 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { | 
|  | 587 | if (RegionOfInterest.isValid()) { | 
|  | 588 | SourceRange Range = getRawCursorExtent(Cursor); | 
|  | 589 | if (Range.isInvalid()) | 
|  | 590 | return llvm::Optional<bool>(); | 
| Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 591 |  | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 592 | switch (CompareRegionOfInterest(Range)) { | 
|  | 593 | case RangeBefore: | 
|  | 594 | // This declaration comes before the region of interest; skip it. | 
|  | 595 | return llvm::Optional<bool>(); | 
|  | 596 |  | 
|  | 597 | case RangeAfter: | 
|  | 598 | // This declaration comes after the region of interest; we're done. | 
|  | 599 | return false; | 
|  | 600 |  | 
|  | 601 | case RangeOverlap: | 
|  | 602 | // This declaration overlaps the region of interest; visit it. | 
|  | 603 | break; | 
|  | 604 | } | 
|  | 605 | } | 
|  | 606 | return true; | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { | 
|  | 610 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); | 
|  | 611 |  | 
|  | 612 | // FIXME: Eventually remove.  This part of a hack to support proper | 
|  | 613 | // iteration over all Decls contained lexically within an ObjC container. | 
|  | 614 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); | 
|  | 615 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); | 
|  | 616 |  | 
|  | 617 | for ( ; I != E; ++I) { | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 618 | Decl *D = *I; | 
|  | 619 | if (D->getLexicalDeclContext() != DC) | 
|  | 620 | continue; | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 621 | CXCursor Cursor = MakeCXCursor(D, TU); | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 622 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); | 
|  | 623 | if (!V.hasValue()) | 
|  | 624 | continue; | 
|  | 625 | if (!V.getValue()) | 
|  | 626 | return false; | 
| Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 627 | if (Visit(Cursor, true)) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 628 | return true; | 
|  | 629 | } | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 630 | return false; | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 631 | } | 
|  | 632 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 633 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | 
|  | 634 | llvm_unreachable("Translation units are visited directly by Visit()"); | 
|  | 635 | return false; | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { | 
|  | 639 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) | 
|  | 640 | return Visit(TSInfo->getTypeLoc()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 641 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 642 | return false; | 
|  | 643 | } | 
|  | 644 |  | 
|  | 645 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { | 
|  | 646 | return VisitDeclContext(D); | 
|  | 647 | } | 
|  | 648 |  | 
| Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 649 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( | 
|  | 650 | ClassTemplateSpecializationDecl *D) { | 
|  | 651 | bool ShouldVisitBody = false; | 
|  | 652 | switch (D->getSpecializationKind()) { | 
|  | 653 | case TSK_Undeclared: | 
|  | 654 | case TSK_ImplicitInstantiation: | 
|  | 655 | // Nothing to visit | 
|  | 656 | return false; | 
|  | 657 |  | 
|  | 658 | case TSK_ExplicitInstantiationDeclaration: | 
|  | 659 | case TSK_ExplicitInstantiationDefinition: | 
|  | 660 | break; | 
|  | 661 |  | 
|  | 662 | case TSK_ExplicitSpecialization: | 
|  | 663 | ShouldVisitBody = true; | 
|  | 664 | break; | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | // Visit the template arguments used in the specialization. | 
|  | 668 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { | 
|  | 669 | TypeLoc TL = SpecType->getTypeLoc(); | 
|  | 670 | if (TemplateSpecializationTypeLoc *TSTLoc | 
|  | 671 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { | 
|  | 672 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) | 
|  | 673 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) | 
|  | 674 | return true; | 
|  | 675 | } | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) | 
|  | 679 | return true; | 
|  | 680 |  | 
|  | 681 | return false; | 
|  | 682 | } | 
|  | 683 |  | 
| Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 684 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( | 
|  | 685 | ClassTemplatePartialSpecializationDecl *D) { | 
|  | 686 | // FIXME: Visit the "outer" template parameter lists on the TagDecl | 
|  | 687 | // before visiting these template parameters. | 
|  | 688 | if (VisitTemplateParameters(D->getTemplateParameters())) | 
|  | 689 | return true; | 
|  | 690 |  | 
|  | 691 | // Visit the partial specialization arguments. | 
|  | 692 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); | 
|  | 693 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) | 
|  | 694 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) | 
|  | 695 | return true; | 
|  | 696 |  | 
|  | 697 | return VisitCXXRecordDecl(D); | 
|  | 698 | } | 
|  | 699 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 700 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { | 
| Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 701 | // Visit the default argument. | 
|  | 702 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) | 
|  | 703 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) | 
|  | 704 | if (Visit(DefArg->getTypeLoc())) | 
|  | 705 | return true; | 
|  | 706 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 707 | return false; | 
|  | 708 | } | 
|  | 709 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 710 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { | 
|  | 711 | if (Expr *Init = D->getInitExpr()) | 
|  | 712 | return Visit(MakeCXCursor(Init, StmtParent, TU)); | 
|  | 713 | return false; | 
|  | 714 | } | 
|  | 715 |  | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 716 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { | 
|  | 717 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) | 
|  | 718 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 719 | return true; | 
|  | 720 |  | 
|  | 721 | return false; | 
|  | 722 | } | 
|  | 723 |  | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 724 | /// \brief Compare two base or member initializers based on their source order. | 
|  | 725 | static int CompareCXXBaseOrMemberInitializers(const void* Xp, const void *Yp) { | 
|  | 726 | CXXBaseOrMemberInitializer const * const *X | 
|  | 727 | = static_cast<CXXBaseOrMemberInitializer const * const *>(Xp); | 
|  | 728 | CXXBaseOrMemberInitializer const * const *Y | 
|  | 729 | = static_cast<CXXBaseOrMemberInitializer const * const *>(Yp); | 
|  | 730 |  | 
|  | 731 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) | 
|  | 732 | return -1; | 
|  | 733 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) | 
|  | 734 | return 1; | 
|  | 735 | else | 
|  | 736 | return 0; | 
|  | 737 | } | 
|  | 738 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 739 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 740 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { | 
|  | 741 | // Visit the function declaration's syntactic components in the order | 
|  | 742 | // written. This requires a bit of work. | 
|  | 743 | TypeLoc TL = TSInfo->getTypeLoc(); | 
|  | 744 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); | 
|  | 745 |  | 
|  | 746 | // If we have a function declared directly (without the use of a typedef), | 
|  | 747 | // visit just the return type. Otherwise, just visit the function's type | 
|  | 748 | // now. | 
|  | 749 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || | 
|  | 750 | (!FTL && Visit(TL))) | 
|  | 751 | return true; | 
|  | 752 |  | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 753 | // Visit the nested-name-specifier, if present. | 
|  | 754 | if (NestedNameSpecifier *Qualifier = ND->getQualifier()) | 
|  | 755 | if (VisitNestedNameSpecifier(Qualifier, ND->getQualifierRange())) | 
|  | 756 | return true; | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 757 |  | 
|  | 758 | // Visit the declaration name. | 
|  | 759 | if (VisitDeclarationNameInfo(ND->getNameInfo())) | 
|  | 760 | return true; | 
|  | 761 |  | 
|  | 762 | // FIXME: Visit explicitly-specified template arguments! | 
|  | 763 |  | 
|  | 764 | // Visit the function parameters, if we have a function type. | 
|  | 765 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) | 
|  | 766 | return true; | 
|  | 767 |  | 
|  | 768 | // FIXME: Attributes? | 
|  | 769 | } | 
|  | 770 |  | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 771 | if (ND->isThisDeclarationADefinition()) { | 
|  | 772 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { | 
|  | 773 | // Find the initializers that were written in the source. | 
|  | 774 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 4> WrittenInits; | 
|  | 775 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), | 
|  | 776 | IEnd = Constructor->init_end(); | 
|  | 777 | I != IEnd; ++I) { | 
|  | 778 | if (!(*I)->isWritten()) | 
|  | 779 | continue; | 
|  | 780 |  | 
|  | 781 | WrittenInits.push_back(*I); | 
|  | 782 | } | 
|  | 783 |  | 
|  | 784 | // Sort the initializers in source order | 
|  | 785 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), | 
|  | 786 | &CompareCXXBaseOrMemberInitializers); | 
|  | 787 |  | 
|  | 788 | // Visit the initializers in source order | 
|  | 789 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { | 
|  | 790 | CXXBaseOrMemberInitializer *Init = WrittenInits[I]; | 
|  | 791 | if (Init->isMemberInitializer()) { | 
|  | 792 | if (Visit(MakeCursorMemberRef(Init->getMember(), | 
|  | 793 | Init->getMemberLocation(), TU))) | 
|  | 794 | return true; | 
|  | 795 | } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) { | 
|  | 796 | if (Visit(BaseInfo->getTypeLoc())) | 
|  | 797 | return true; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | // Visit the initializer value. | 
|  | 801 | if (Expr *Initializer = Init->getInit()) | 
|  | 802 | if (Visit(MakeCXCursor(Initializer, ND, TU))) | 
|  | 803 | return true; | 
|  | 804 | } | 
|  | 805 | } | 
|  | 806 |  | 
|  | 807 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) | 
|  | 808 | return true; | 
|  | 809 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 810 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 811 | return false; | 
|  | 812 | } | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 813 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 814 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { | 
|  | 815 | if (VisitDeclaratorDecl(D)) | 
|  | 816 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 817 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 818 | if (Expr *BitWidth = D->getBitWidth()) | 
|  | 819 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 820 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 821 | return false; | 
|  | 822 | } | 
|  | 823 |  | 
|  | 824 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { | 
|  | 825 | if (VisitDeclaratorDecl(D)) | 
|  | 826 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 827 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 828 | if (Expr *Init = D->getInit()) | 
|  | 829 | return Visit(MakeCXCursor(Init, StmtParent, TU)); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 830 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 831 | return false; | 
|  | 832 | } | 
|  | 833 |  | 
| Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 834 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { | 
|  | 835 | if (VisitDeclaratorDecl(D)) | 
|  | 836 | return true; | 
|  | 837 |  | 
|  | 838 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) | 
|  | 839 | if (Expr *DefArg = D->getDefaultArgument()) | 
|  | 840 | return Visit(MakeCXCursor(DefArg, StmtParent, TU)); | 
|  | 841 |  | 
|  | 842 | return false; | 
|  | 843 | } | 
|  | 844 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 845 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | 
|  | 846 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl | 
|  | 847 | // before visiting these template parameters. | 
|  | 848 | if (VisitTemplateParameters(D->getTemplateParameters())) | 
|  | 849 | return true; | 
|  | 850 |  | 
|  | 851 | return VisitFunctionDecl(D->getTemplatedDecl()); | 
|  | 852 | } | 
|  | 853 |  | 
| Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 854 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { | 
|  | 855 | // FIXME: Visit the "outer" template parameter lists on the TagDecl | 
|  | 856 | // before visiting these template parameters. | 
|  | 857 | if (VisitTemplateParameters(D->getTemplateParameters())) | 
|  | 858 | return true; | 
|  | 859 |  | 
|  | 860 | return VisitCXXRecordDecl(D->getTemplatedDecl()); | 
|  | 861 | } | 
|  | 862 |  | 
| Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 863 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { | 
|  | 864 | if (VisitTemplateParameters(D->getTemplateParameters())) | 
|  | 865 | return true; | 
|  | 866 |  | 
|  | 867 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && | 
|  | 868 | VisitTemplateArgumentLoc(D->getDefaultArgument())) | 
|  | 869 | return true; | 
|  | 870 |  | 
|  | 871 | return false; | 
|  | 872 | } | 
|  | 873 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 874 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { | 
| Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 875 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) | 
|  | 876 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 877 | return true; | 
|  | 878 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 879 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 880 | PEnd = ND->param_end(); | 
|  | 881 | P != PEnd; ++P) { | 
|  | 882 | if (Visit(MakeCXCursor(*P, TU))) | 
|  | 883 | return true; | 
|  | 884 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 885 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 886 | if (ND->isThisDeclarationADefinition() && | 
|  | 887 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) | 
|  | 888 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 889 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 890 | return false; | 
|  | 891 | } | 
|  | 892 |  | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 893 | namespace { | 
|  | 894 | struct ContainerDeclsSort { | 
|  | 895 | SourceManager &SM; | 
|  | 896 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} | 
|  | 897 | bool operator()(Decl *A, Decl *B) { | 
|  | 898 | SourceLocation L_A = A->getLocStart(); | 
|  | 899 | SourceLocation L_B = B->getLocStart(); | 
|  | 900 | assert(L_A.isValid() && L_B.isValid()); | 
|  | 901 | return SM.isBeforeInTranslationUnit(L_A, L_B); | 
|  | 902 | } | 
|  | 903 | }; | 
|  | 904 | } | 
|  | 905 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 906 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 907 | // FIXME: Eventually convert back to just 'VisitDeclContext()'.  Essentially | 
|  | 908 | // an @implementation can lexically contain Decls that are not properly | 
|  | 909 | // nested in the AST.  When we identify such cases, we need to retrofit | 
|  | 910 | // this nesting here. | 
|  | 911 | if (!DI_current) | 
|  | 912 | return VisitDeclContext(D); | 
|  | 913 |  | 
|  | 914 | // Scan the Decls that immediately come after the container | 
|  | 915 | // in the current DeclContext.  If any fall within the | 
|  | 916 | // container's lexical region, stash them into a vector | 
|  | 917 | // for later processing. | 
|  | 918 | llvm::SmallVector<Decl *, 24> DeclsInContainer; | 
|  | 919 | SourceLocation EndLoc = D->getSourceRange().getEnd(); | 
|  | 920 | SourceManager &SM = TU->getSourceManager(); | 
|  | 921 | if (EndLoc.isValid()) { | 
|  | 922 | DeclContext::decl_iterator next = *DI_current; | 
|  | 923 | while (++next != DE_current) { | 
|  | 924 | Decl *D_next = *next; | 
|  | 925 | if (!D_next) | 
|  | 926 | break; | 
|  | 927 | SourceLocation L = D_next->getLocStart(); | 
|  | 928 | if (!L.isValid()) | 
|  | 929 | break; | 
|  | 930 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { | 
|  | 931 | *DI_current = next; | 
|  | 932 | DeclsInContainer.push_back(D_next); | 
|  | 933 | continue; | 
|  | 934 | } | 
|  | 935 | break; | 
|  | 936 | } | 
|  | 937 | } | 
|  | 938 |  | 
|  | 939 | // The common case. | 
|  | 940 | if (DeclsInContainer.empty()) | 
|  | 941 | return VisitDeclContext(D); | 
|  | 942 |  | 
|  | 943 | // Get all the Decls in the DeclContext, and sort them with the | 
|  | 944 | // additional ones we've collected.  Then visit them. | 
|  | 945 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); | 
|  | 946 | I!=E; ++I) { | 
|  | 947 | Decl *subDecl = *I; | 
| Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 948 | if (!subDecl || subDecl->getLexicalDeclContext() != D || | 
|  | 949 | subDecl->getLocStart().isInvalid()) | 
| Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 950 | continue; | 
|  | 951 | DeclsInContainer.push_back(subDecl); | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | // Now sort the Decls so that they appear in lexical order. | 
|  | 955 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), | 
|  | 956 | ContainerDeclsSort(SM)); | 
|  | 957 |  | 
|  | 958 | // Now visit the decls. | 
|  | 959 | for (llvm::SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), | 
|  | 960 | E = DeclsInContainer.end(); I != E; ++I) { | 
|  | 961 | CXCursor Cursor = MakeCXCursor(*I, TU); | 
|  | 962 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); | 
|  | 963 | if (!V.hasValue()) | 
|  | 964 | continue; | 
|  | 965 | if (!V.getValue()) | 
|  | 966 | return false; | 
|  | 967 | if (Visit(Cursor, true)) | 
|  | 968 | return true; | 
|  | 969 | } | 
|  | 970 | return false; | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 971 | } | 
|  | 972 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 973 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 974 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), | 
|  | 975 | TU))) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 976 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 977 |  | 
| Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 978 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); | 
|  | 979 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), | 
|  | 980 | E = ND->protocol_end(); I != E; ++I, ++PL) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 981 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 982 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 983 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 984 | return VisitObjCContainerDecl(ND); | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 985 | } | 
|  | 986 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 987 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { | 
|  | 988 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); | 
|  | 989 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), | 
|  | 990 | E = PID->protocol_end(); I != E; ++I, ++PL) | 
|  | 991 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) | 
|  | 992 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 993 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 994 | return VisitObjCContainerDecl(PID); | 
|  | 995 | } | 
|  | 996 |  | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 997 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { | 
| Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 998 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) | 
| John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 999 | return true; | 
|  | 1000 |  | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1001 | // FIXME: This implements a workaround with @property declarations also being | 
|  | 1002 | // installed in the DeclContext for the @interface.  Eventually this code | 
|  | 1003 | // should be removed. | 
|  | 1004 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); | 
|  | 1005 | if (!CDecl || !CDecl->IsClassExtension()) | 
|  | 1006 | return false; | 
|  | 1007 |  | 
|  | 1008 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); | 
|  | 1009 | if (!ID) | 
|  | 1010 | return false; | 
|  | 1011 |  | 
|  | 1012 | IdentifierInfo *PropertyId = PD->getIdentifier(); | 
|  | 1013 | ObjCPropertyDecl *prevDecl = | 
|  | 1014 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); | 
|  | 1015 |  | 
|  | 1016 | if (!prevDecl) | 
|  | 1017 | return false; | 
|  | 1018 |  | 
|  | 1019 | // Visit synthesized methods since they will be skipped when visiting | 
|  | 1020 | // the @interface. | 
|  | 1021 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) | 
| Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1022 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1023 | if (Visit(MakeCXCursor(MD, TU))) | 
|  | 1024 | return true; | 
|  | 1025 |  | 
|  | 1026 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) | 
| Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1027 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1028 | if (Visit(MakeCXCursor(MD, TU))) | 
|  | 1029 | return true; | 
|  | 1030 |  | 
|  | 1031 | return false; | 
|  | 1032 | } | 
|  | 1033 |  | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1034 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1035 | // Issue callbacks for super class. | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1036 | if (D->getSuperClass() && | 
|  | 1037 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1038 | D->getSuperClassLoc(), | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1039 | TU))) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1040 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1041 |  | 
| Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1042 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); | 
|  | 1043 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), | 
|  | 1044 | E = D->protocol_end(); I != E; ++I, ++PL) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1045 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1046 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1047 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1048 | return VisitObjCContainerDecl(D); | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1051 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { | 
|  | 1052 | return VisitObjCContainerDecl(D); | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1053 | } | 
|  | 1054 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1055 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { | 
| Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1056 | // 'ID' could be null when dealing with invalid code. | 
|  | 1057 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) | 
|  | 1058 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) | 
|  | 1059 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1060 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1061 | return VisitObjCImplDecl(D); | 
|  | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { | 
|  | 1065 | #if 0 | 
|  | 1066 | // Issue callbacks for super class. | 
|  | 1067 | // FIXME: No source location information! | 
|  | 1068 | if (D->getSuperClass() && | 
|  | 1069 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1070 | D->getSuperClassLoc(), | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1071 | TU))) | 
|  | 1072 | return true; | 
|  | 1073 | #endif | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1074 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1075 | return VisitObjCImplDecl(D); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { | 
|  | 1079 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); | 
|  | 1080 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), | 
|  | 1081 | E = D->protocol_end(); | 
|  | 1082 | I != E; ++I, ++PL) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1083 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1084 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1085 |  | 
|  | 1086 | return false; | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1087 | } | 
|  | 1088 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1089 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { | 
|  | 1090 | for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C) | 
|  | 1091 | if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU))) | 
|  | 1092 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1093 |  | 
| Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1094 | return false; | 
| Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
| Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1097 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { | 
|  | 1098 | return VisitDeclContext(D); | 
|  | 1099 | } | 
|  | 1100 |  | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1101 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1102 | // Visit nested-name-specifier. | 
|  | 1103 | if (NestedNameSpecifier *Qualifier = D->getQualifier()) | 
|  | 1104 | if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange())) | 
|  | 1105 | return true; | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1106 |  | 
|  | 1107 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), | 
|  | 1108 | D->getTargetNameLoc(), TU)); | 
|  | 1109 | } | 
|  | 1110 |  | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1111 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1112 | // Visit nested-name-specifier. | 
|  | 1113 | if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameDecl()) | 
|  | 1114 | if (VisitNestedNameSpecifier(Qualifier, D->getNestedNameRange())) | 
|  | 1115 | return true; | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1116 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1117 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) | 
|  | 1118 | return true; | 
|  | 1119 |  | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1120 | return VisitDeclarationNameInfo(D->getNameInfo()); | 
|  | 1121 | } | 
|  | 1122 |  | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1123 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1124 | // Visit nested-name-specifier. | 
|  | 1125 | if (NestedNameSpecifier *Qualifier = D->getQualifier()) | 
|  | 1126 | if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange())) | 
|  | 1127 | return true; | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1128 |  | 
|  | 1129 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), | 
|  | 1130 | D->getIdentLocation(), TU)); | 
|  | 1131 | } | 
|  | 1132 |  | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1133 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1134 | // Visit nested-name-specifier. | 
|  | 1135 | if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier()) | 
|  | 1136 | if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange())) | 
|  | 1137 | return true; | 
|  | 1138 |  | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1139 | return VisitDeclarationNameInfo(D->getNameInfo()); | 
|  | 1140 | } | 
|  | 1141 |  | 
|  | 1142 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( | 
|  | 1143 | UnresolvedUsingTypenameDecl *D) { | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1144 | // Visit nested-name-specifier. | 
|  | 1145 | if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier()) | 
|  | 1146 | if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange())) | 
|  | 1147 | return true; | 
|  | 1148 |  | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1149 | return false; | 
|  | 1150 | } | 
|  | 1151 |  | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1152 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { | 
|  | 1153 | switch (Name.getName().getNameKind()) { | 
|  | 1154 | case clang::DeclarationName::Identifier: | 
|  | 1155 | case clang::DeclarationName::CXXLiteralOperatorName: | 
|  | 1156 | case clang::DeclarationName::CXXOperatorName: | 
|  | 1157 | case clang::DeclarationName::CXXUsingDirective: | 
|  | 1158 | return false; | 
|  | 1159 |  | 
|  | 1160 | case clang::DeclarationName::CXXConstructorName: | 
|  | 1161 | case clang::DeclarationName::CXXDestructorName: | 
|  | 1162 | case clang::DeclarationName::CXXConversionFunctionName: | 
|  | 1163 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) | 
|  | 1164 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1165 | return false; | 
|  | 1166 |  | 
|  | 1167 | case clang::DeclarationName::ObjCZeroArgSelector: | 
|  | 1168 | case clang::DeclarationName::ObjCOneArgSelector: | 
|  | 1169 | case clang::DeclarationName::ObjCMultiArgSelector: | 
|  | 1170 | // FIXME: Per-identifier location info? | 
|  | 1171 | return false; | 
|  | 1172 | } | 
|  | 1173 |  | 
|  | 1174 | return false; | 
|  | 1175 | } | 
|  | 1176 |  | 
| Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1177 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, | 
|  | 1178 | SourceRange Range) { | 
|  | 1179 | // FIXME: This whole routine is a hack to work around the lack of proper | 
|  | 1180 | // source information in nested-name-specifiers (PR5791). Since we do have | 
|  | 1181 | // a beginning source location, we can visit the first component of the | 
|  | 1182 | // nested-name-specifier, if it's a single-token component. | 
|  | 1183 | if (!NNS) | 
|  | 1184 | return false; | 
|  | 1185 |  | 
|  | 1186 | // Get the first component in the nested-name-specifier. | 
|  | 1187 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) | 
|  | 1188 | NNS = Prefix; | 
|  | 1189 |  | 
|  | 1190 | switch (NNS->getKind()) { | 
|  | 1191 | case NestedNameSpecifier::Namespace: | 
|  | 1192 | // FIXME: The token at this source location might actually have been a | 
|  | 1193 | // namespace alias, but we don't model that. Lame! | 
|  | 1194 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), | 
|  | 1195 | TU)); | 
|  | 1196 |  | 
|  | 1197 | case NestedNameSpecifier::TypeSpec: { | 
|  | 1198 | // If the type has a form where we know that the beginning of the source | 
|  | 1199 | // range matches up with a reference cursor. Visit the appropriate reference | 
|  | 1200 | // cursor. | 
|  | 1201 | Type *T = NNS->getAsType(); | 
|  | 1202 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) | 
|  | 1203 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); | 
|  | 1204 | if (const TagType *Tag = dyn_cast<TagType>(T)) | 
|  | 1205 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); | 
|  | 1206 | if (const TemplateSpecializationType *TST | 
|  | 1207 | = dyn_cast<TemplateSpecializationType>(T)) | 
|  | 1208 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); | 
|  | 1209 | break; | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | case NestedNameSpecifier::TypeSpecWithTemplate: | 
|  | 1213 | case NestedNameSpecifier::Global: | 
|  | 1214 | case NestedNameSpecifier::Identifier: | 
|  | 1215 | break; | 
|  | 1216 | } | 
|  | 1217 |  | 
|  | 1218 | return false; | 
|  | 1219 | } | 
|  | 1220 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1221 | bool CursorVisitor::VisitTemplateParameters( | 
|  | 1222 | const TemplateParameterList *Params) { | 
|  | 1223 | if (!Params) | 
|  | 1224 | return false; | 
|  | 1225 |  | 
|  | 1226 | for (TemplateParameterList::const_iterator P = Params->begin(), | 
|  | 1227 | PEnd = Params->end(); | 
|  | 1228 | P != PEnd; ++P) { | 
|  | 1229 | if (Visit(MakeCXCursor(*P, TU))) | 
|  | 1230 | return true; | 
|  | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | return false; | 
|  | 1234 | } | 
|  | 1235 |  | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1236 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { | 
|  | 1237 | switch (Name.getKind()) { | 
|  | 1238 | case TemplateName::Template: | 
|  | 1239 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); | 
|  | 1240 |  | 
|  | 1241 | case TemplateName::OverloadedTemplate: | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1242 | // Visit the overloaded template set. | 
|  | 1243 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) | 
|  | 1244 | return true; | 
|  | 1245 |  | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1246 | return false; | 
|  | 1247 |  | 
|  | 1248 | case TemplateName::DependentTemplate: | 
|  | 1249 | // FIXME: Visit nested-name-specifier. | 
|  | 1250 | return false; | 
|  | 1251 |  | 
|  | 1252 | case TemplateName::QualifiedTemplate: | 
|  | 1253 | // FIXME: Visit nested-name-specifier. | 
|  | 1254 | return Visit(MakeCursorTemplateRef( | 
|  | 1255 | Name.getAsQualifiedTemplateName()->getDecl(), | 
|  | 1256 | Loc, TU)); | 
|  | 1257 | } | 
|  | 1258 |  | 
|  | 1259 | return false; | 
|  | 1260 | } | 
|  | 1261 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1262 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { | 
|  | 1263 | switch (TAL.getArgument().getKind()) { | 
|  | 1264 | case TemplateArgument::Null: | 
|  | 1265 | case TemplateArgument::Integral: | 
|  | 1266 | return false; | 
|  | 1267 |  | 
|  | 1268 | case TemplateArgument::Pack: | 
|  | 1269 | // FIXME: Implement when variadic templates come along. | 
|  | 1270 | return false; | 
|  | 1271 |  | 
|  | 1272 | case TemplateArgument::Type: | 
|  | 1273 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) | 
|  | 1274 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1275 | return false; | 
|  | 1276 |  | 
|  | 1277 | case TemplateArgument::Declaration: | 
|  | 1278 | if (Expr *E = TAL.getSourceDeclExpression()) | 
|  | 1279 | return Visit(MakeCXCursor(E, StmtParent, TU)); | 
|  | 1280 | return false; | 
|  | 1281 |  | 
|  | 1282 | case TemplateArgument::Expression: | 
|  | 1283 | if (Expr *E = TAL.getSourceExpression()) | 
|  | 1284 | return Visit(MakeCXCursor(E, StmtParent, TU)); | 
|  | 1285 | return false; | 
|  | 1286 |  | 
|  | 1287 | case TemplateArgument::Template: | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1288 | return VisitTemplateName(TAL.getArgument().getAsTemplate(), | 
|  | 1289 | TAL.getTemplateNameLoc()); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1290 | } | 
|  | 1291 |  | 
|  | 1292 | return false; | 
|  | 1293 | } | 
|  | 1294 |  | 
| Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1295 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { | 
|  | 1296 | return VisitDeclContext(D); | 
|  | 1297 | } | 
|  | 1298 |  | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1299 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { | 
|  | 1300 | return Visit(TL.getUnqualifiedLoc()); | 
|  | 1301 | } | 
|  | 1302 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1303 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { | 
|  | 1304 | ASTContext &Context = TU->getASTContext(); | 
|  | 1305 |  | 
|  | 1306 | // Some builtin types (such as Objective-C's "id", "sel", and | 
|  | 1307 | // "Class") have associated declarations. Create cursors for those. | 
|  | 1308 | QualType VisitType; | 
|  | 1309 | switch (TL.getType()->getAs<BuiltinType>()->getKind()) { | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1310 | case BuiltinType::Void: | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1311 | case BuiltinType::Bool: | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1312 | case BuiltinType::Char_U: | 
|  | 1313 | case BuiltinType::UChar: | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1314 | case BuiltinType::Char16: | 
|  | 1315 | case BuiltinType::Char32: | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1316 | case BuiltinType::UShort: | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1317 | case BuiltinType::UInt: | 
|  | 1318 | case BuiltinType::ULong: | 
|  | 1319 | case BuiltinType::ULongLong: | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1320 | case BuiltinType::UInt128: | 
|  | 1321 | case BuiltinType::Char_S: | 
|  | 1322 | case BuiltinType::SChar: | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1323 | case BuiltinType::WChar: | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1324 | case BuiltinType::Short: | 
|  | 1325 | case BuiltinType::Int: | 
|  | 1326 | case BuiltinType::Long: | 
|  | 1327 | case BuiltinType::LongLong: | 
|  | 1328 | case BuiltinType::Int128: | 
|  | 1329 | case BuiltinType::Float: | 
|  | 1330 | case BuiltinType::Double: | 
|  | 1331 | case BuiltinType::LongDouble: | 
|  | 1332 | case BuiltinType::NullPtr: | 
|  | 1333 | case BuiltinType::Overload: | 
|  | 1334 | case BuiltinType::Dependent: | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1335 | break; | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1336 |  | 
|  | 1337 | case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor? | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1338 | break; | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1339 |  | 
| Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1340 | case BuiltinType::ObjCId: | 
|  | 1341 | VisitType = Context.getObjCIdType(); | 
|  | 1342 | break; | 
| Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1343 |  | 
|  | 1344 | case BuiltinType::ObjCClass: | 
|  | 1345 | VisitType = Context.getObjCClassType(); | 
|  | 1346 | break; | 
|  | 1347 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1348 | case BuiltinType::ObjCSel: | 
|  | 1349 | VisitType = Context.getObjCSelType(); | 
|  | 1350 | break; | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | if (!VisitType.isNull()) { | 
|  | 1354 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1355 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1356 | TU)); | 
|  | 1357 | } | 
|  | 1358 |  | 
|  | 1359 | return false; | 
|  | 1360 | } | 
|  | 1361 |  | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1362 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { | 
|  | 1363 | return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU)); | 
|  | 1364 | } | 
|  | 1365 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1366 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { | 
|  | 1367 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); | 
|  | 1368 | } | 
|  | 1369 |  | 
|  | 1370 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { | 
|  | 1371 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); | 
|  | 1372 | } | 
|  | 1373 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1374 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1375 | // FIXME: We can't visit the template type parameter, because there's | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1376 | // no context information with which we can match up the depth/index in the | 
|  | 1377 | // type to the appropriate | 
|  | 1378 | return false; | 
|  | 1379 | } | 
|  | 1380 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1381 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { | 
|  | 1382 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) | 
|  | 1383 | return true; | 
|  | 1384 |  | 
| John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1385 | return false; | 
|  | 1386 | } | 
|  | 1387 |  | 
|  | 1388 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { | 
|  | 1389 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) | 
|  | 1390 | return true; | 
|  | 1391 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1392 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { | 
|  | 1393 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), | 
|  | 1394 | TU))) | 
|  | 1395 | return true; | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | return false; | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { | 
| John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1402 | return Visit(TL.getPointeeLoc()); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1403 | } | 
|  | 1404 |  | 
|  | 1405 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { | 
|  | 1406 | return Visit(TL.getPointeeLoc()); | 
|  | 1407 | } | 
|  | 1408 |  | 
|  | 1409 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { | 
|  | 1410 | return Visit(TL.getPointeeLoc()); | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { | 
|  | 1414 | return Visit(TL.getPointeeLoc()); | 
|  | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1418 | return Visit(TL.getPointeeLoc()); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1419 | } | 
|  | 1420 |  | 
|  | 1421 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1422 | return Visit(TL.getPointeeLoc()); | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1423 | } | 
|  | 1424 |  | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1425 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, | 
|  | 1426 | bool SkipResultType) { | 
|  | 1427 | if (!SkipResultType && Visit(TL.getResultLoc())) | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1428 | return true; | 
|  | 1429 |  | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1430 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) | 
| Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1431 | if (Decl *D = TL.getArg(I)) | 
|  | 1432 | if (Visit(MakeCXCursor(D, TU))) | 
|  | 1433 | return true; | 
| Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1434 |  | 
|  | 1435 | return false; | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { | 
|  | 1439 | if (Visit(TL.getElementLoc())) | 
|  | 1440 | return true; | 
|  | 1441 |  | 
|  | 1442 | if (Expr *Size = TL.getSizeExpr()) | 
|  | 1443 | return Visit(MakeCXCursor(Size, StmtParent, TU)); | 
|  | 1444 |  | 
|  | 1445 | return false; | 
|  | 1446 | } | 
|  | 1447 |  | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1448 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( | 
|  | 1449 | TemplateSpecializationTypeLoc TL) { | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1450 | // Visit the template name. | 
|  | 1451 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), | 
|  | 1452 | TL.getTemplateNameLoc())) | 
|  | 1453 | return true; | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1454 |  | 
|  | 1455 | // Visit the template arguments. | 
|  | 1456 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) | 
|  | 1457 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) | 
|  | 1458 | return true; | 
|  | 1459 |  | 
|  | 1460 | return false; | 
|  | 1461 | } | 
|  | 1462 |  | 
| Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1463 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { | 
|  | 1464 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); | 
|  | 1465 | } | 
|  | 1466 |  | 
|  | 1467 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { | 
|  | 1468 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) | 
|  | 1469 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1470 |  | 
|  | 1471 | return false; | 
|  | 1472 | } | 
|  | 1473 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1474 | bool CursorVisitor::VisitStmt(Stmt *S) { | 
|  | 1475 | for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end(); | 
|  | 1476 | Child != ChildEnd; ++Child) { | 
| Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 1477 | if (Stmt *C = *Child) | 
|  | 1478 | if (Visit(MakeCXCursor(C, StmtParent, TU))) | 
|  | 1479 | return true; | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1480 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1481 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1482 | return false; | 
|  | 1483 | } | 
|  | 1484 |  | 
|  | 1485 | bool CursorVisitor::VisitDeclStmt(DeclStmt *S) { | 
| Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 1486 | bool isFirst = true; | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1487 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); | 
|  | 1488 | D != DEnd; ++D) { | 
| Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 1489 | if (*D && Visit(MakeCXCursor(*D, TU, isFirst))) | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1490 | return true; | 
| Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 1491 | isFirst = false; | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1492 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1493 |  | 
| Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1494 | return false; | 
|  | 1495 | } | 
|  | 1496 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 1497 | bool CursorVisitor::VisitGotoStmt(GotoStmt *S) { | 
|  | 1498 | return Visit(MakeCursorLabelRef(S->getLabel(), S->getLabelLoc(), TU)); | 
|  | 1499 | } | 
|  | 1500 |  | 
| Douglas Gregor | 8947a75 | 2010-09-02 20:35:02 +0000 | [diff] [blame] | 1501 | bool CursorVisitor::VisitDeclRefExpr(DeclRefExpr *E) { | 
|  | 1502 | // Visit nested-name-specifier, if present. | 
|  | 1503 | if (NestedNameSpecifier *Qualifier = E->getQualifier()) | 
|  | 1504 | if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange())) | 
|  | 1505 | return true; | 
|  | 1506 |  | 
|  | 1507 | // Visit declaration name. | 
|  | 1508 | if (VisitDeclarationNameInfo(E->getNameInfo())) | 
|  | 1509 | return true; | 
|  | 1510 |  | 
|  | 1511 | // Visit explicitly-specified template arguments. | 
|  | 1512 | if (E->hasExplicitTemplateArgs()) { | 
|  | 1513 | ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs(); | 
|  | 1514 | for (TemplateArgumentLoc *Arg = Args.getTemplateArgs(), | 
|  | 1515 | *ArgEnd = Arg + Args.NumTemplateArgs; | 
|  | 1516 | Arg != ArgEnd; ++Arg) | 
|  | 1517 | if (VisitTemplateArgumentLoc(*Arg)) | 
|  | 1518 | return true; | 
|  | 1519 | } | 
|  | 1520 |  | 
|  | 1521 | return false; | 
|  | 1522 | } | 
|  | 1523 |  | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1524 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { | 
|  | 1525 | if (D->isDefinition()) { | 
|  | 1526 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), | 
|  | 1527 | E = D->bases_end(); I != E; ++I) { | 
|  | 1528 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) | 
|  | 1529 | return true; | 
|  | 1530 | } | 
|  | 1531 | } | 
|  | 1532 |  | 
|  | 1533 | return VisitTagDecl(D); | 
|  | 1534 | } | 
|  | 1535 |  | 
|  | 1536 |  | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 1537 | bool CursorVisitor::VisitBlockExpr(BlockExpr *B) { | 
|  | 1538 | return Visit(B->getBlockDecl()); | 
|  | 1539 | } | 
|  | 1540 |  | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1541 | bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { | 
| Douglas Gregor | 8ccef2d | 2010-09-09 23:10:46 +0000 | [diff] [blame] | 1542 | // Visit the type into which we're computing an offset. | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1543 | if (Visit(E->getTypeSourceInfo()->getTypeLoc())) | 
|  | 1544 | return true; | 
| Douglas Gregor | 8ccef2d | 2010-09-09 23:10:46 +0000 | [diff] [blame] | 1545 |  | 
|  | 1546 | // Visit the components of the offsetof expression. | 
|  | 1547 | for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) { | 
|  | 1548 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; | 
|  | 1549 | const OffsetOfNode &Node = E->getComponent(I); | 
|  | 1550 | switch (Node.getKind()) { | 
|  | 1551 | case OffsetOfNode::Array: | 
|  | 1552 | if (Visit(MakeCXCursor(E->getIndexExpr(Node.getArrayExprIndex()), | 
|  | 1553 | StmtParent, TU))) | 
|  | 1554 | return true; | 
|  | 1555 | break; | 
|  | 1556 |  | 
|  | 1557 | case OffsetOfNode::Field: | 
|  | 1558 | if (Visit(MakeCursorMemberRef(Node.getField(), Node.getRange().getEnd(), | 
|  | 1559 | TU))) | 
|  | 1560 | return true; | 
|  | 1561 | break; | 
|  | 1562 |  | 
|  | 1563 | case OffsetOfNode::Identifier: | 
|  | 1564 | case OffsetOfNode::Base: | 
|  | 1565 | continue; | 
|  | 1566 | } | 
|  | 1567 | } | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1568 |  | 
| Douglas Gregor | 8ccef2d | 2010-09-09 23:10:46 +0000 | [diff] [blame] | 1569 | return false; | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1570 | } | 
|  | 1571 |  | 
| Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1572 | bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { | 
|  | 1573 | if (E->isArgumentType()) { | 
|  | 1574 | if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo()) | 
|  | 1575 | return Visit(TSInfo->getTypeLoc()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1576 |  | 
| Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1577 | return false; | 
|  | 1578 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1579 |  | 
| Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1580 | return VisitExpr(E); | 
|  | 1581 | } | 
|  | 1582 |  | 
|  | 1583 | bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { | 
|  | 1584 | if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten()) | 
|  | 1585 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1586 | return true; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1587 |  | 
| Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1588 | return VisitCastExpr(E); | 
|  | 1589 | } | 
|  | 1590 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 1591 | bool CursorVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { | 
|  | 1592 | return Visit(MakeCursorLabelRef(E->getLabel(), E->getLabelLoc(), TU)); | 
|  | 1593 | } | 
|  | 1594 |  | 
| Douglas Gregor | 648220e | 2010-08-10 15:02:34 +0000 | [diff] [blame] | 1595 | bool CursorVisitor::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { | 
|  | 1596 | return Visit(E->getArgTInfo1()->getTypeLoc()) || | 
|  | 1597 | Visit(E->getArgTInfo2()->getTypeLoc()); | 
|  | 1598 | } | 
|  | 1599 |  | 
|  | 1600 | bool CursorVisitor::VisitVAArgExpr(VAArgExpr *E) { | 
|  | 1601 | if (Visit(E->getWrittenTypeInfo()->getTypeLoc())) | 
|  | 1602 | return true; | 
|  | 1603 |  | 
|  | 1604 | return Visit(MakeCXCursor(E->getSubExpr(), StmtParent, TU)); | 
|  | 1605 | } | 
|  | 1606 |  | 
| Douglas Gregor | fa2e26f | 2010-09-09 23:28:23 +0000 | [diff] [blame] | 1607 | bool CursorVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { | 
|  | 1608 | // Visit the designators. | 
|  | 1609 | typedef DesignatedInitExpr::Designator Designator; | 
|  | 1610 | for (DesignatedInitExpr::designators_iterator D = E->designators_begin(), | 
|  | 1611 | DEnd = E->designators_end(); | 
|  | 1612 | D != DEnd; ++D) { | 
|  | 1613 | if (D->isFieldDesignator()) { | 
|  | 1614 | if (FieldDecl *Field = D->getField()) | 
|  | 1615 | if (Visit(MakeCursorMemberRef(Field, D->getFieldLoc(), TU))) | 
|  | 1616 | return true; | 
|  | 1617 |  | 
|  | 1618 | continue; | 
|  | 1619 | } | 
|  | 1620 |  | 
|  | 1621 | if (D->isArrayDesignator()) { | 
|  | 1622 | if (Visit(MakeCXCursor(E->getArrayIndex(*D), StmtParent, TU))) | 
|  | 1623 | return true; | 
|  | 1624 |  | 
|  | 1625 | continue; | 
|  | 1626 | } | 
|  | 1627 |  | 
|  | 1628 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); | 
|  | 1629 | if (Visit(MakeCXCursor(E->getArrayRangeStart(*D), StmtParent, TU)) || | 
|  | 1630 | Visit(MakeCXCursor(E->getArrayRangeEnd(*D), StmtParent, TU))) | 
|  | 1631 | return true; | 
|  | 1632 | } | 
|  | 1633 |  | 
|  | 1634 | // Visit the initializer value itself. | 
|  | 1635 | return Visit(MakeCXCursor(E->getInit(), StmtParent, TU)); | 
|  | 1636 | } | 
|  | 1637 |  | 
| Douglas Gregor | 9480229 | 2010-09-02 21:20:16 +0000 | [diff] [blame] | 1638 | bool CursorVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { | 
|  | 1639 | if (E->isTypeOperand()) { | 
|  | 1640 | if (TypeSourceInfo *TSInfo = E->getTypeOperandSourceInfo()) | 
|  | 1641 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1642 |  | 
|  | 1643 | return false; | 
|  | 1644 | } | 
|  | 1645 |  | 
|  | 1646 | return VisitExpr(E); | 
|  | 1647 | } | 
|  | 1648 |  | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 1649 | bool CursorVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { | 
|  | 1650 | if (E->isTypeOperand()) { | 
|  | 1651 | if (TypeSourceInfo *TSInfo = E->getTypeOperandSourceInfo()) | 
|  | 1652 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1653 |  | 
|  | 1654 | return false; | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | return VisitExpr(E); | 
|  | 1658 | } | 
|  | 1659 |  | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1660 | bool CursorVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { | 
|  | 1661 | if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo()) | 
| Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 1662 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1663 | return true; | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1664 |  | 
|  | 1665 | return VisitExpr(E); | 
|  | 1666 | } | 
|  | 1667 |  | 
|  | 1668 | bool CursorVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { | 
|  | 1669 | if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo()) | 
|  | 1670 | return Visit(TSInfo->getTypeLoc()); | 
|  | 1671 |  | 
|  | 1672 | return false; | 
|  | 1673 | } | 
|  | 1674 |  | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1675 | bool CursorVisitor::VisitCXXNewExpr(CXXNewExpr *E) { | 
|  | 1676 | // Visit placement arguments. | 
|  | 1677 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) | 
|  | 1678 | if (Visit(MakeCXCursor(E->getPlacementArg(I), StmtParent, TU))) | 
|  | 1679 | return true; | 
|  | 1680 |  | 
|  | 1681 | // Visit the allocated type. | 
|  | 1682 | if (TypeSourceInfo *TSInfo = E->getAllocatedTypeSourceInfo()) | 
|  | 1683 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1684 | return true; | 
|  | 1685 |  | 
|  | 1686 | // Visit the array size, if any. | 
|  | 1687 | if (E->isArray() && Visit(MakeCXCursor(E->getArraySize(), StmtParent, TU))) | 
|  | 1688 | return true; | 
|  | 1689 |  | 
|  | 1690 | // Visit the initializer or constructor arguments. | 
|  | 1691 | for (unsigned I = 0, N = E->getNumConstructorArgs(); I != N; ++I) | 
|  | 1692 | if (Visit(MakeCXCursor(E->getConstructorArg(I), StmtParent, TU))) | 
|  | 1693 | return true; | 
|  | 1694 |  | 
|  | 1695 | return false; | 
|  | 1696 | } | 
|  | 1697 |  | 
| Douglas Gregor | 6f7198f | 2010-09-02 22:09:03 +0000 | [diff] [blame] | 1698 | bool CursorVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { | 
|  | 1699 | // Visit base expression. | 
|  | 1700 | if (Visit(MakeCXCursor(E->getBase(), StmtParent, TU))) | 
|  | 1701 | return true; | 
|  | 1702 |  | 
|  | 1703 | // Visit the nested-name-specifier. | 
|  | 1704 | if (NestedNameSpecifier *Qualifier = E->getQualifier()) | 
|  | 1705 | if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange())) | 
|  | 1706 | return true; | 
|  | 1707 |  | 
|  | 1708 | // Visit the scope type that looks disturbingly like the nested-name-specifier | 
|  | 1709 | // but isn't. | 
|  | 1710 | if (TypeSourceInfo *TSInfo = E->getScopeTypeInfo()) | 
|  | 1711 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1712 | return true; | 
|  | 1713 |  | 
|  | 1714 | // Visit the name of the type being destroyed. | 
|  | 1715 | if (TypeSourceInfo *TSInfo = E->getDestroyedTypeInfo()) | 
|  | 1716 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1717 | return true; | 
|  | 1718 |  | 
|  | 1719 | return false; | 
|  | 1720 | } | 
|  | 1721 |  | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 1722 | bool CursorVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { | 
|  | 1723 | return Visit(E->getQueriedTypeSourceInfo()->getTypeLoc()); | 
|  | 1724 | } | 
|  | 1725 |  | 
| Douglas Gregor | bfebed2 | 2010-09-03 17:24:10 +0000 | [diff] [blame] | 1726 | bool CursorVisitor::VisitDependentScopeDeclRefExpr( | 
|  | 1727 | DependentScopeDeclRefExpr *E) { | 
|  | 1728 | // Visit the nested-name-specifier. | 
|  | 1729 | if (NestedNameSpecifier *Qualifier = E->getQualifier()) | 
|  | 1730 | if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange())) | 
|  | 1731 | return true; | 
|  | 1732 |  | 
|  | 1733 | // Visit the declaration name. | 
|  | 1734 | if (VisitDeclarationNameInfo(E->getNameInfo())) | 
|  | 1735 | return true; | 
|  | 1736 |  | 
|  | 1737 | // Visit the explicitly-specified template arguments. | 
|  | 1738 | if (const ExplicitTemplateArgumentList *ArgList | 
|  | 1739 | = E->getOptionalExplicitTemplateArgs()) { | 
|  | 1740 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), | 
|  | 1741 | *ArgEnd = Arg + ArgList->NumTemplateArgs; | 
|  | 1742 | Arg != ArgEnd; ++Arg) { | 
|  | 1743 | if (VisitTemplateArgumentLoc(*Arg)) | 
|  | 1744 | return true; | 
|  | 1745 | } | 
|  | 1746 | } | 
|  | 1747 |  | 
|  | 1748 | return false; | 
|  | 1749 | } | 
|  | 1750 |  | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1751 | bool CursorVisitor::VisitCXXUnresolvedConstructExpr( | 
|  | 1752 | CXXUnresolvedConstructExpr *E) { | 
|  | 1753 | if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo()) | 
|  | 1754 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1755 | return true; | 
|  | 1756 |  | 
|  | 1757 | return VisitExpr(E); | 
|  | 1758 | } | 
|  | 1759 |  | 
| Douglas Gregor | 25d6362 | 2010-09-03 17:35:34 +0000 | [diff] [blame] | 1760 | bool CursorVisitor::VisitCXXDependentScopeMemberExpr( | 
|  | 1761 | CXXDependentScopeMemberExpr *E) { | 
|  | 1762 | // Visit the base expression, if there is one. | 
|  | 1763 | if (!E->isImplicitAccess() && | 
|  | 1764 | Visit(MakeCXCursor(E->getBase(), StmtParent, TU))) | 
|  | 1765 | return true; | 
|  | 1766 |  | 
|  | 1767 | // Visit the nested-name-specifier. | 
|  | 1768 | if (NestedNameSpecifier *Qualifier = E->getQualifier()) | 
|  | 1769 | if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange())) | 
|  | 1770 | return true; | 
|  | 1771 |  | 
|  | 1772 | // Visit the declaration name. | 
|  | 1773 | if (VisitDeclarationNameInfo(E->getMemberNameInfo())) | 
|  | 1774 | return true; | 
|  | 1775 |  | 
|  | 1776 | // Visit the explicitly-specified template arguments. | 
|  | 1777 | if (const ExplicitTemplateArgumentList *ArgList | 
|  | 1778 | = E->getOptionalExplicitTemplateArgs()) { | 
|  | 1779 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), | 
|  | 1780 | *ArgEnd = Arg + ArgList->NumTemplateArgs; | 
|  | 1781 | Arg != ArgEnd; ++Arg) { | 
|  | 1782 | if (VisitTemplateArgumentLoc(*Arg)) | 
|  | 1783 | return true; | 
|  | 1784 | } | 
|  | 1785 | } | 
|  | 1786 |  | 
|  | 1787 | return false; | 
|  | 1788 | } | 
|  | 1789 |  | 
| Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 1790 | bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) { | 
| Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1791 | if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo()) | 
|  | 1792 | if (Visit(TSInfo->getTypeLoc())) | 
|  | 1793 | return true; | 
| Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 1794 |  | 
|  | 1795 | return VisitExpr(E); | 
|  | 1796 | } | 
|  | 1797 |  | 
| Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1798 | bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { | 
|  | 1799 | return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc()); | 
|  | 1800 | } | 
|  | 1801 |  | 
|  | 1802 |  | 
| Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1803 | bool CursorVisitor::VisitAttributes(Decl *D) { | 
| Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1804 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); | 
|  | 1805 | i != e; ++i) | 
|  | 1806 | if (Visit(MakeCXCursor(*i, D, TU))) | 
| Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1807 | return true; | 
|  | 1808 |  | 
|  | 1809 | return false; | 
|  | 1810 | } | 
|  | 1811 |  | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1812 | //===----------------------------------------------------------------------===// | 
|  | 1813 | // Data-recursive visitor methods. | 
|  | 1814 | //===----------------------------------------------------------------------===// | 
|  | 1815 |  | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1816 | static void EnqueueChildren(VisitorWorkList &WL, CXCursor Parent, Stmt *S) { | 
|  | 1817 | unsigned size = WL.size(); | 
|  | 1818 | for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end(); | 
|  | 1819 | Child != ChildEnd; ++Child) { | 
|  | 1820 | WLAddStmt(WL, Parent, *Child); | 
|  | 1821 | } | 
|  | 1822 | if (size == WL.size()) | 
|  | 1823 | return; | 
|  | 1824 | // Now reverse the entries we just added.  This will match the DFS | 
|  | 1825 | // ordering performed by the worklist. | 
|  | 1826 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); | 
|  | 1827 | std::reverse(I, E); | 
|  | 1828 | } | 
|  | 1829 |  | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 1830 | static void EnqueueOverloadExpr(VisitorWorkList &WL, CXCursor Parent, | 
|  | 1831 | OverloadExpr *E) { | 
|  | 1832 | WL.push_back(OverloadExprParts(E, Parent)); | 
|  | 1833 | } | 
|  | 1834 |  | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1835 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { | 
|  | 1836 | CXCursor C = MakeCXCursor(S, StmtParent, TU); | 
|  | 1837 | switch (S->getStmtClass()) { | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1838 | default: | 
|  | 1839 | EnqueueChildren(WL, C, S); | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1840 | break; | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 1841 | case Stmt::CompoundLiteralExprClass: { | 
|  | 1842 | CompoundLiteralExpr *CL = cast<CompoundLiteralExpr>(S); | 
|  | 1843 | EnqueueChildren(WL, C, CL); | 
|  | 1844 | WLAddTypeLoc(WL, C, CL->getTypeSourceInfo()); | 
|  | 1845 | break; | 
|  | 1846 | } | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1847 | case Stmt::CXXOperatorCallExprClass: { | 
|  | 1848 | CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(S); | 
|  | 1849 | // Note that we enqueue things in reverse order so that | 
|  | 1850 | // they are visited correctly by the DFS. | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1851 | for (unsigned I = 1, N = CE->getNumArgs(); I != N; ++I) | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1852 | WLAddStmt(WL, C, CE->getArg(N-I)); | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1853 |  | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1854 | WLAddStmt(WL, C, CE->getCallee()); | 
|  | 1855 | WLAddStmt(WL, C, CE->getArg(0)); | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1856 | break; | 
|  | 1857 | } | 
|  | 1858 | case Stmt::BinaryOperatorClass: { | 
|  | 1859 | BinaryOperator *B = cast<BinaryOperator>(S); | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1860 | WLAddStmt(WL, C, B->getRHS()); | 
|  | 1861 | WLAddStmt(WL, C, B->getLHS()); | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1862 | break; | 
|  | 1863 | } | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 1864 | case Stmt::ForStmtClass: { | 
|  | 1865 | ForStmt *FS = cast<ForStmt>(S); | 
|  | 1866 | WLAddStmt(WL, C, FS->getBody()); | 
|  | 1867 | WLAddStmt(WL, C, FS->getInc()); | 
|  | 1868 | WLAddStmt(WL, C, FS->getCond()); | 
|  | 1869 | WLAddDecl(WL, C, FS->getConditionVariable()); | 
|  | 1870 | WLAddStmt(WL, C, FS->getInit()); | 
|  | 1871 | break; | 
|  | 1872 | } | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 1873 | case Stmt::IfStmtClass: { | 
|  | 1874 | IfStmt *If = cast<IfStmt>(S); | 
|  | 1875 | WLAddStmt(WL, C, If->getElse()); | 
|  | 1876 | WLAddStmt(WL, C, If->getThen()); | 
|  | 1877 | WLAddStmt(WL, C, If->getCond()); | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1878 | WLAddDecl(WL, C, If->getConditionVariable()); | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 1879 | break; | 
|  | 1880 | } | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1881 | case Stmt::InitListExprClass: { | 
|  | 1882 | InitListExpr *IE = cast<InitListExpr>(S); | 
|  | 1883 | // We care about the syntactic form of the initializer list, only. | 
|  | 1884 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) | 
|  | 1885 | IE = Syntactic; | 
|  | 1886 | EnqueueChildren(WL, C, IE); | 
|  | 1887 | break; | 
|  | 1888 | } | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1889 | case Stmt::MemberExprClass: { | 
|  | 1890 | MemberExpr *M = cast<MemberExpr>(S); | 
|  | 1891 | WL.push_back(MemberExprParts(M, C)); | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1892 | WLAddStmt(WL, C, M->getBase()); | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1893 | break; | 
|  | 1894 | } | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1895 | case Stmt::ParenExprClass: { | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1896 | WLAddStmt(WL, C, cast<ParenExpr>(S)->getSubExpr()); | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1897 | break; | 
|  | 1898 | } | 
|  | 1899 | case Stmt::SwitchStmtClass: { | 
|  | 1900 | SwitchStmt *SS = cast<SwitchStmt>(S); | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1901 | WLAddStmt(WL, C, SS->getBody()); | 
|  | 1902 | WLAddStmt(WL, C, SS->getCond()); | 
|  | 1903 | WLAddDecl(WL, C, SS->getConditionVariable()); | 
| Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 1904 | break; | 
|  | 1905 | } | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 1906 | case Stmt::WhileStmtClass: { | 
|  | 1907 | WhileStmt *W = cast<WhileStmt>(S); | 
|  | 1908 | WLAddStmt(WL, C, W->getBody()); | 
|  | 1909 | WLAddStmt(WL, C, W->getCond()); | 
|  | 1910 | WLAddDecl(WL, C, W->getConditionVariable()); | 
|  | 1911 | break; | 
|  | 1912 | } | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 1913 | case Stmt::UnresolvedLookupExprClass: | 
|  | 1914 | EnqueueOverloadExpr(WL, C, cast<OverloadExpr>(S)); | 
|  | 1915 | break; | 
|  | 1916 | case Stmt::UnresolvedMemberExprClass: { | 
|  | 1917 | UnresolvedMemberExpr *U = cast<UnresolvedMemberExpr>(S); | 
|  | 1918 | EnqueueOverloadExpr(WL, C, U); | 
|  | 1919 | if (!U->isImplicitAccess()) | 
|  | 1920 | WLAddStmt(WL, C, U->getBase()); | 
|  | 1921 | break; | 
|  | 1922 | } | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1923 | } | 
|  | 1924 | } | 
|  | 1925 |  | 
|  | 1926 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { | 
|  | 1927 | if (RegionOfInterest.isValid()) { | 
|  | 1928 | SourceRange Range = getRawCursorExtent(C); | 
|  | 1929 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) | 
|  | 1930 | return false; | 
|  | 1931 | } | 
|  | 1932 | return true; | 
|  | 1933 | } | 
|  | 1934 |  | 
|  | 1935 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { | 
|  | 1936 | while (!WL.empty()) { | 
|  | 1937 | // Dequeue the worklist item. | 
|  | 1938 | VisitorJob LI = WL.back(); WL.pop_back(); | 
|  | 1939 |  | 
|  | 1940 | // Set the Parent field, then back to its old value once we're done. | 
|  | 1941 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); | 
|  | 1942 |  | 
|  | 1943 | switch (LI.getKind()) { | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1944 | case VisitorJob::DeclVisitKind: { | 
|  | 1945 | Decl *D = cast<DeclVisit>(LI).get(); | 
|  | 1946 | if (!D) | 
|  | 1947 | continue; | 
|  | 1948 |  | 
|  | 1949 | // For now, perform default visitation for Decls. | 
|  | 1950 | if (Visit(MakeCXCursor(D, TU))) | 
|  | 1951 | return true; | 
|  | 1952 |  | 
|  | 1953 | continue; | 
|  | 1954 | } | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 1955 | case VisitorJob::TypeLocVisitKind: { | 
|  | 1956 | // Perform default visitation for TypeLocs. | 
|  | 1957 | if (Visit(cast<TypeLocVisit>(LI).get())) | 
|  | 1958 | return true; | 
|  | 1959 | continue; | 
|  | 1960 | } | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1961 | case VisitorJob::StmtVisitKind: { | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1962 | Stmt *S = cast<StmtVisit>(LI).get(); | 
| Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 1963 | if (!S) | 
|  | 1964 | continue; | 
|  | 1965 |  | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1966 | // Update the current cursor. | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1967 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU); | 
|  | 1968 |  | 
|  | 1969 | switch (S->getStmtClass()) { | 
|  | 1970 | default: { | 
|  | 1971 | // Perform default visitation for other cases. | 
|  | 1972 | if (Visit(Cursor)) | 
|  | 1973 | return true; | 
|  | 1974 | continue; | 
|  | 1975 | } | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1976 | case Stmt::BinaryOperatorClass: | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1977 | case Stmt::CallExprClass: | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1978 | case Stmt::CaseStmtClass: | 
| Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 1979 | case Stmt::CompoundLiteralExprClass: | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1980 | case Stmt::CompoundStmtClass: | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1981 | case Stmt::CXXMemberCallExprClass: | 
| Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 1982 | case Stmt::CXXOperatorCallExprClass: | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1983 | case Stmt::DefaultStmtClass: | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 1984 | case Stmt::DoStmtClass: | 
|  | 1985 | case Stmt::ForStmtClass: | 
| Ted Kremenek | c70ebba | 2010-11-12 18:26:58 +0000 | [diff] [blame] | 1986 | case Stmt::IfStmtClass: | 
| Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1987 | case Stmt::InitListExprClass: | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1988 | case Stmt::MemberExprClass: | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1989 | case Stmt::ParenExprClass: | 
|  | 1990 | case Stmt::SwitchStmtClass: | 
| Ted Kremenek | ae3c220 | 2010-11-12 18:27:01 +0000 | [diff] [blame] | 1991 | case Stmt::UnaryOperatorClass: | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 1992 | case Stmt::UnresolvedLookupExprClass: | 
|  | 1993 | case Stmt::UnresolvedMemberExprClass: | 
| Ted Kremenek | bb67713 | 2010-11-12 18:27:04 +0000 | [diff] [blame] | 1994 | case Stmt::WhileStmtClass: | 
| Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 1995 | { | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1996 | if (!IsInRegionOfInterest(Cursor)) | 
|  | 1997 | continue; | 
|  | 1998 | switch (Visitor(Cursor, Parent, ClientData)) { | 
|  | 1999 | case CXChildVisit_Break: | 
|  | 2000 | return true; | 
|  | 2001 | case CXChildVisit_Continue: | 
|  | 2002 | break; | 
|  | 2003 | case CXChildVisit_Recurse: | 
|  | 2004 | EnqueueWorkList(WL, S); | 
|  | 2005 | break; | 
|  | 2006 | } | 
|  | 2007 | } | 
|  | 2008 | } | 
|  | 2009 | continue; | 
|  | 2010 | } | 
|  | 2011 | case VisitorJob::MemberExprPartsKind: { | 
|  | 2012 | // Handle the other pieces in the MemberExpr besides the base. | 
|  | 2013 | MemberExpr *M = cast<MemberExprParts>(LI).get(); | 
|  | 2014 |  | 
|  | 2015 | // Visit the nested-name-specifier | 
|  | 2016 | if (NestedNameSpecifier *Qualifier = M->getQualifier()) | 
|  | 2017 | if (VisitNestedNameSpecifier(Qualifier, M->getQualifierRange())) | 
|  | 2018 | return true; | 
|  | 2019 |  | 
|  | 2020 | // Visit the declaration name. | 
|  | 2021 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) | 
|  | 2022 | return true; | 
|  | 2023 |  | 
|  | 2024 | // Visit the explicitly-specified template arguments, if any. | 
|  | 2025 | if (M->hasExplicitTemplateArgs()) { | 
|  | 2026 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), | 
|  | 2027 | *ArgEnd = Arg + M->getNumTemplateArgs(); | 
|  | 2028 | Arg != ArgEnd; ++Arg) { | 
|  | 2029 | if (VisitTemplateArgumentLoc(*Arg)) | 
|  | 2030 | return true; | 
|  | 2031 | } | 
|  | 2032 | } | 
|  | 2033 | continue; | 
|  | 2034 | } | 
| Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame^] | 2035 | case VisitorJob::OverloadExprPartsKind: { | 
|  | 2036 | OverloadExpr *O = cast<OverloadExprParts>(LI).get(); | 
|  | 2037 | // Visit the nested-name-specifier. | 
|  | 2038 | if (NestedNameSpecifier *Qualifier = O->getQualifier()) | 
|  | 2039 | if (VisitNestedNameSpecifier(Qualifier, O->getQualifierRange())) | 
|  | 2040 | return true; | 
|  | 2041 | // Visit the declaration name. | 
|  | 2042 | if (VisitDeclarationNameInfo(O->getNameInfo())) | 
|  | 2043 | return true; | 
|  | 2044 | // Visit the overloaded declaration reference. | 
|  | 2045 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) | 
|  | 2046 | return true; | 
|  | 2047 | // Visit the explicitly-specified template arguments. | 
|  | 2048 | if (const ExplicitTemplateArgumentList *ArgList | 
|  | 2049 | = O->getOptionalExplicitTemplateArgs()) { | 
|  | 2050 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), | 
|  | 2051 | *ArgEnd = Arg + ArgList->NumTemplateArgs; | 
|  | 2052 | Arg != ArgEnd; ++Arg) { | 
|  | 2053 | if (VisitTemplateArgumentLoc(*Arg)) | 
|  | 2054 | return true; | 
|  | 2055 | } | 
|  | 2056 | } | 
|  | 2057 | continue; | 
|  | 2058 | } | 
| Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2059 | } | 
|  | 2060 | } | 
|  | 2061 | return false; | 
|  | 2062 | } | 
|  | 2063 |  | 
|  | 2064 | bool CursorVisitor::VisitDataRecursive(Stmt *S) { | 
|  | 2065 | VisitorWorkList WL; | 
|  | 2066 | EnqueueWorkList(WL, S); | 
|  | 2067 | return RunVisitorWorkList(WL); | 
|  | 2068 | } | 
|  | 2069 |  | 
|  | 2070 | //===----------------------------------------------------------------------===// | 
|  | 2071 | // Misc. API hooks. | 
|  | 2072 | //===----------------------------------------------------------------------===// | 
|  | 2073 |  | 
| Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2074 | static llvm::sys::Mutex EnableMultithreadingMutex; | 
|  | 2075 | static bool EnabledMultithreading; | 
|  | 2076 |  | 
| Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2077 | extern "C" { | 
| Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2078 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, | 
|  | 2079 | int displayDiagnostics) { | 
| Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2080 | // Disable pretty stack trace functionality, which will otherwise be a very | 
|  | 2081 | // poor citizen of the world and set up all sorts of signal handlers. | 
|  | 2082 | llvm::DisablePrettyStackTrace = true; | 
|  | 2083 |  | 
| Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2084 | // We use crash recovery to make some of our APIs more reliable, implicitly | 
|  | 2085 | // enable it. | 
|  | 2086 | llvm::CrashRecoveryContext::Enable(); | 
|  | 2087 |  | 
| Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2088 | // Enable support for multithreading in LLVM. | 
|  | 2089 | { | 
|  | 2090 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); | 
|  | 2091 | if (!EnabledMultithreading) { | 
|  | 2092 | llvm::llvm_start_multithreaded(); | 
|  | 2093 | EnabledMultithreading = true; | 
|  | 2094 | } | 
|  | 2095 | } | 
|  | 2096 |  | 
| Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2097 | CIndexer *CIdxr = new CIndexer(); | 
| Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2098 | if (excludeDeclarationsFromPCH) | 
|  | 2099 | CIdxr->setOnlyLocalDecls(); | 
| Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2100 | if (displayDiagnostics) | 
|  | 2101 | CIdxr->setDisplayDiagnostics(); | 
| Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2102 | return CIdxr; | 
| Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2103 | } | 
|  | 2104 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2105 | void clang_disposeIndex(CXIndex CIdx) { | 
| Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2106 | if (CIdx) | 
|  | 2107 | delete static_cast<CIndexer *>(CIdx); | 
| Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2108 | } | 
|  | 2109 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2110 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, | 
| Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2111 | const char *ast_filename) { | 
| Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2112 | if (!CIdx) | 
|  | 2113 | return 0; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2114 |  | 
| Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2115 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); | 
| Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2116 | FileSystemOptions FileSystemOpts; | 
|  | 2117 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2118 |  | 
| Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 2119 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; | 
| Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2120 | return ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, | 
| Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2121 | CXXIdx->getOnlyLocalDecls(), | 
|  | 2122 | 0, 0, true); | 
| Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2123 | } | 
|  | 2124 |  | 
| Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2125 | unsigned clang_defaultEditingTranslationUnitOptions() { | 
| Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2126 | return CXTranslationUnit_PrecompiledPreamble | | 
| Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2127 | CXTranslationUnit_CacheCompletionResults | | 
|  | 2128 | CXTranslationUnit_CXXPrecompiledPreamble; | 
| Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2129 | } | 
|  | 2130 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2131 | CXTranslationUnit | 
|  | 2132 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, | 
|  | 2133 | const char *source_filename, | 
|  | 2134 | int num_command_line_args, | 
| Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2135 | const char * const *command_line_args, | 
| Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2136 | unsigned num_unsaved_files, | 
| Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2137 | struct CXUnsavedFile *unsaved_files) { | 
| Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2138 | return clang_parseTranslationUnit(CIdx, source_filename, | 
|  | 2139 | command_line_args, num_command_line_args, | 
|  | 2140 | unsaved_files, num_unsaved_files, | 
|  | 2141 | CXTranslationUnit_DetailedPreprocessingRecord); | 
|  | 2142 | } | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2143 |  | 
|  | 2144 | struct ParseTranslationUnitInfo { | 
|  | 2145 | CXIndex CIdx; | 
|  | 2146 | const char *source_filename; | 
| Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2147 | const char *const *command_line_args; | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2148 | int num_command_line_args; | 
|  | 2149 | struct CXUnsavedFile *unsaved_files; | 
|  | 2150 | unsigned num_unsaved_files; | 
|  | 2151 | unsigned options; | 
|  | 2152 | CXTranslationUnit result; | 
|  | 2153 | }; | 
| Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2154 | static void clang_parseTranslationUnit_Impl(void *UserData) { | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2155 | ParseTranslationUnitInfo *PTUI = | 
|  | 2156 | static_cast<ParseTranslationUnitInfo*>(UserData); | 
|  | 2157 | CXIndex CIdx = PTUI->CIdx; | 
|  | 2158 | const char *source_filename = PTUI->source_filename; | 
| Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2159 | const char * const *command_line_args = PTUI->command_line_args; | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2160 | int num_command_line_args = PTUI->num_command_line_args; | 
|  | 2161 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; | 
|  | 2162 | unsigned num_unsaved_files = PTUI->num_unsaved_files; | 
|  | 2163 | unsigned options = PTUI->options; | 
|  | 2164 | PTUI->result = 0; | 
| Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2165 |  | 
| Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2166 | if (!CIdx) | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2167 | return; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2168 |  | 
| Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2169 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); | 
|  | 2170 |  | 
| Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2171 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; | 
| Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2172 | bool CompleteTranslationUnit | 
|  | 2173 | = ((options & CXTranslationUnit_Incomplete) == 0); | 
| Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2174 | bool CacheCodeCompetionResults | 
|  | 2175 | = options & CXTranslationUnit_CacheCompletionResults; | 
| Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2176 | bool CXXPrecompilePreamble | 
|  | 2177 | = options & CXTranslationUnit_CXXPrecompiledPreamble; | 
|  | 2178 | bool CXXChainedPCH | 
|  | 2179 | = options & CXTranslationUnit_CXXChainedPCH; | 
| Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2180 |  | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2181 | // Configure the diagnostics. | 
|  | 2182 | DiagnosticOptions DiagOpts; | 
| Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 2183 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; | 
|  | 2184 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2185 |  | 
| Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2186 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; | 
|  | 2187 | for (unsigned I = 0; I != num_unsaved_files; ++I) { | 
| Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2188 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2189 | const llvm::MemoryBuffer *Buffer | 
| Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2190 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); | 
| Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2191 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, | 
|  | 2192 | Buffer)); | 
|  | 2193 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2194 |  | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2195 | llvm::SmallVector<const char *, 16> Args; | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2196 |  | 
| Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 2197 | // The 'source_filename' argument is optional.  If the caller does not | 
|  | 2198 | // specify it then it is assumed that the source file is specified | 
|  | 2199 | // in the actual argument list. | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2200 | if (source_filename) | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2201 | Args.push_back(source_filename); | 
| Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2202 |  | 
|  | 2203 | // Since the Clang C library is primarily used by batch tools dealing with | 
|  | 2204 | // (often very broken) source code, where spell-checking can have a | 
|  | 2205 | // significant negative impact on performance (particularly when | 
|  | 2206 | // precompiled headers are involved), we disable it by default. | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2207 | // Only do this if we haven't found a spell-checking-related argument. | 
|  | 2208 | bool FoundSpellCheckingArgument = false; | 
|  | 2209 | for (int I = 0; I != num_command_line_args; ++I) { | 
|  | 2210 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || | 
|  | 2211 | strcmp(command_line_args[I], "-fspell-checking") == 0) { | 
|  | 2212 | FoundSpellCheckingArgument = true; | 
|  | 2213 | break; | 
| Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2214 | } | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2215 | } | 
|  | 2216 | if (!FoundSpellCheckingArgument) | 
|  | 2217 | Args.push_back("-fno-spell-checking"); | 
|  | 2218 |  | 
|  | 2219 | Args.insert(Args.end(), command_line_args, | 
|  | 2220 | command_line_args + num_command_line_args); | 
| Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2221 |  | 
| Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2222 | // Do we need the detailed preprocessing record? | 
|  | 2223 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2224 | Args.push_back("-Xclang"); | 
|  | 2225 | Args.push_back("-detailed-preprocessing-record"); | 
| Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2226 | } | 
|  | 2227 |  | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2228 | unsigned NumErrors = Diags->getNumErrors(); | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2229 | llvm::OwningPtr<ASTUnit> Unit( | 
|  | 2230 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), | 
|  | 2231 | Diags, | 
|  | 2232 | CXXIdx->getClangResourcesPath(), | 
|  | 2233 | CXXIdx->getOnlyLocalDecls(), | 
| Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2234 | /*CaptureDiagnostics=*/true, | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2235 | RemappedFiles.data(), | 
|  | 2236 | RemappedFiles.size(), | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2237 | PrecompilePreamble, | 
|  | 2238 | CompleteTranslationUnit, | 
| Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2239 | CacheCodeCompetionResults, | 
|  | 2240 | CXXPrecompilePreamble, | 
|  | 2241 | CXXChainedPCH)); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2242 |  | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2243 | if (NumErrors != Diags->getNumErrors()) { | 
|  | 2244 | // Make sure to check that 'Unit' is non-NULL. | 
|  | 2245 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { | 
|  | 2246 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), | 
|  | 2247 | DEnd = Unit->stored_diag_end(); | 
|  | 2248 | D != DEnd; ++D) { | 
|  | 2249 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); | 
|  | 2250 | CXString Msg = clang_formatDiagnostic(&Diag, | 
|  | 2251 | clang_defaultDiagnosticDisplayOptions()); | 
|  | 2252 | fprintf(stderr, "%s\n", clang_getCString(Msg)); | 
|  | 2253 | clang_disposeString(Msg); | 
|  | 2254 | } | 
| Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2255 | #ifdef LLVM_ON_WIN32 | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2256 | // On Windows, force a flush, since there may be multiple copies of | 
|  | 2257 | // stderr and stdout in the file system, all with different buffers | 
|  | 2258 | // but writing to the same device. | 
|  | 2259 | fflush(stderr); | 
|  | 2260 | #endif | 
|  | 2261 | } | 
| Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2262 | } | 
| Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2263 |  | 
| Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2264 | PTUI->result = Unit.take(); | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2265 | } | 
|  | 2266 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, | 
|  | 2267 | const char *source_filename, | 
| Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2268 | const char * const *command_line_args, | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2269 | int num_command_line_args, | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2270 | struct CXUnsavedFile *unsaved_files, | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2271 | unsigned num_unsaved_files, | 
|  | 2272 | unsigned options) { | 
|  | 2273 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2274 | num_command_line_args, unsaved_files, | 
|  | 2275 | num_unsaved_files, options, 0 }; | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2276 | llvm::CrashRecoveryContext CRC; | 
|  | 2277 |  | 
| Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2278 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { | 
| Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2279 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); | 
|  | 2280 | fprintf(stderr, "  'source_filename' : '%s'\n", source_filename); | 
|  | 2281 | fprintf(stderr, "  'command_line_args' : ["); | 
|  | 2282 | for (int i = 0; i != num_command_line_args; ++i) { | 
|  | 2283 | if (i) | 
|  | 2284 | fprintf(stderr, ", "); | 
|  | 2285 | fprintf(stderr, "'%s'", command_line_args[i]); | 
|  | 2286 | } | 
|  | 2287 | fprintf(stderr, "],\n"); | 
|  | 2288 | fprintf(stderr, "  'unsaved_files' : ["); | 
|  | 2289 | for (unsigned i = 0; i != num_unsaved_files; ++i) { | 
|  | 2290 | if (i) | 
|  | 2291 | fprintf(stderr, ", "); | 
|  | 2292 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, | 
|  | 2293 | unsaved_files[i].Length); | 
|  | 2294 | } | 
|  | 2295 | fprintf(stderr, "],\n"); | 
|  | 2296 | fprintf(stderr, "  'options' : %d,\n", options); | 
|  | 2297 | fprintf(stderr, "}\n"); | 
|  | 2298 |  | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2299 | return 0; | 
|  | 2300 | } | 
|  | 2301 |  | 
|  | 2302 | return PTUI.result; | 
| Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2303 | } | 
|  | 2304 |  | 
| Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2305 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { | 
|  | 2306 | return CXSaveTranslationUnit_None; | 
|  | 2307 | } | 
|  | 2308 |  | 
|  | 2309 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, | 
|  | 2310 | unsigned options) { | 
| Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2311 | if (!TU) | 
|  | 2312 | return 1; | 
|  | 2313 |  | 
|  | 2314 | return static_cast<ASTUnit *>(TU)->Save(FileName); | 
|  | 2315 | } | 
| Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2316 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2317 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2318 | if (CTUnit) { | 
|  | 2319 | // If the translation unit has been marked as unsafe to free, just discard | 
|  | 2320 | // it. | 
|  | 2321 | if (static_cast<ASTUnit *>(CTUnit)->isUnsafeToFree()) | 
|  | 2322 | return; | 
|  | 2323 |  | 
| Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2324 | delete static_cast<ASTUnit *>(CTUnit); | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2325 | } | 
| Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2326 | } | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2327 |  | 
| Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2328 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { | 
|  | 2329 | return CXReparse_None; | 
|  | 2330 | } | 
|  | 2331 |  | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2332 | struct ReparseTranslationUnitInfo { | 
|  | 2333 | CXTranslationUnit TU; | 
|  | 2334 | unsigned num_unsaved_files; | 
|  | 2335 | struct CXUnsavedFile *unsaved_files; | 
|  | 2336 | unsigned options; | 
|  | 2337 | int result; | 
|  | 2338 | }; | 
| Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2339 |  | 
| Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2340 | static void clang_reparseTranslationUnit_Impl(void *UserData) { | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2341 | ReparseTranslationUnitInfo *RTUI = | 
|  | 2342 | static_cast<ReparseTranslationUnitInfo*>(UserData); | 
|  | 2343 | CXTranslationUnit TU = RTUI->TU; | 
|  | 2344 | unsigned num_unsaved_files = RTUI->num_unsaved_files; | 
|  | 2345 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; | 
|  | 2346 | unsigned options = RTUI->options; | 
|  | 2347 | (void) options; | 
|  | 2348 | RTUI->result = 1; | 
|  | 2349 |  | 
| Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2350 | if (!TU) | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2351 | return; | 
| Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2352 |  | 
|  | 2353 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
|  | 2354 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); | 
| Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2355 |  | 
|  | 2356 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; | 
|  | 2357 | for (unsigned I = 0; I != num_unsaved_files; ++I) { | 
|  | 2358 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); | 
|  | 2359 | const llvm::MemoryBuffer *Buffer | 
| Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2360 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); | 
| Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2361 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, | 
|  | 2362 | Buffer)); | 
|  | 2363 | } | 
|  | 2364 |  | 
| Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2365 | if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size())) | 
|  | 2366 | RTUI->result = 0; | 
| Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2367 | } | 
| Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2368 |  | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2369 | int clang_reparseTranslationUnit(CXTranslationUnit TU, | 
|  | 2370 | unsigned num_unsaved_files, | 
|  | 2371 | struct CXUnsavedFile *unsaved_files, | 
|  | 2372 | unsigned options) { | 
|  | 2373 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, | 
|  | 2374 | options, 0 }; | 
|  | 2375 | llvm::CrashRecoveryContext CRC; | 
|  | 2376 |  | 
| Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2377 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { | 
| Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2378 | fprintf(stderr, "libclang: crash detected during reparsing\n"); | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2379 | static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); | 
|  | 2380 | return 1; | 
|  | 2381 | } | 
|  | 2382 |  | 
| Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2383 |  | 
| Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2384 | return RTUI.result; | 
|  | 2385 | } | 
|  | 2386 |  | 
| Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2387 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2388 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { | 
| Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2389 | if (!CTUnit) | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2390 | return createCXString(""); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2391 |  | 
| Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 2392 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2393 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); | 
| Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2394 | } | 
| Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2395 |  | 
| Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2396 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2397 | CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; | 
| Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2398 | return Result; | 
|  | 2399 | } | 
|  | 2400 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2401 | } // end: extern "C" | 
| Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2402 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2403 | //===----------------------------------------------------------------------===// | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2404 | // CXSourceLocation and CXSourceRange Operations. | 
|  | 2405 | //===----------------------------------------------------------------------===// | 
|  | 2406 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2407 | extern "C" { | 
|  | 2408 | CXSourceLocation clang_getNullLocation() { | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2409 | CXSourceLocation Result = { { 0, 0 }, 0 }; | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2410 | return Result; | 
|  | 2411 | } | 
|  | 2412 |  | 
|  | 2413 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { | 
| Daniel Dunbar | 90a6b9e | 2010-01-30 23:58:27 +0000 | [diff] [blame] | 2414 | return (loc1.ptr_data[0] == loc2.ptr_data[0] && | 
|  | 2415 | loc1.ptr_data[1] == loc2.ptr_data[1] && | 
|  | 2416 | loc1.int_data == loc2.int_data); | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2417 | } | 
|  | 2418 |  | 
|  | 2419 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, | 
|  | 2420 | CXFile file, | 
|  | 2421 | unsigned line, | 
|  | 2422 | unsigned column) { | 
| Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2423 | if (!tu || !file) | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2424 | return clang_getNullLocation(); | 
| Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2425 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2426 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); | 
|  | 2427 | SourceLocation SLoc | 
|  | 2428 | = CXXUnit->getSourceManager().getLocation( | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2429 | static_cast<const FileEntry *>(file), | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2430 | line, column); | 
| David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2431 | if (SLoc.isInvalid()) return clang_getNullLocation(); | 
|  | 2432 |  | 
|  | 2433 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); | 
|  | 2434 | } | 
|  | 2435 |  | 
|  | 2436 | CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, | 
|  | 2437 | CXFile file, | 
|  | 2438 | unsigned offset) { | 
|  | 2439 | if (!tu || !file) | 
|  | 2440 | return clang_getNullLocation(); | 
|  | 2441 |  | 
|  | 2442 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); | 
|  | 2443 | SourceLocation Start | 
|  | 2444 | = CXXUnit->getSourceManager().getLocation( | 
|  | 2445 | static_cast<const FileEntry *>(file), | 
|  | 2446 | 1, 1); | 
|  | 2447 | if (Start.isInvalid()) return clang_getNullLocation(); | 
|  | 2448 |  | 
|  | 2449 | SourceLocation SLoc = Start.getFileLocWithOffset(offset); | 
|  | 2450 |  | 
|  | 2451 | if (SLoc.isInvalid()) return clang_getNullLocation(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2452 |  | 
| Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 2453 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2454 | } | 
|  | 2455 |  | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2456 | CXSourceRange clang_getNullRange() { | 
|  | 2457 | CXSourceRange Result = { { 0, 0 }, 0, 0 }; | 
|  | 2458 | return Result; | 
|  | 2459 | } | 
| Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 2460 |  | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2461 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { | 
|  | 2462 | if (begin.ptr_data[0] != end.ptr_data[0] || | 
|  | 2463 | begin.ptr_data[1] != end.ptr_data[1]) | 
|  | 2464 | return clang_getNullRange(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2465 |  | 
|  | 2466 | CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2467 | begin.int_data, end.int_data }; | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2468 | return Result; | 
|  | 2469 | } | 
|  | 2470 |  | 
| Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2471 | void clang_getInstantiationLocation(CXSourceLocation location, | 
|  | 2472 | CXFile *file, | 
|  | 2473 | unsigned *line, | 
|  | 2474 | unsigned *column, | 
|  | 2475 | unsigned *offset) { | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2476 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); | 
|  | 2477 |  | 
| Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2478 | if (!location.ptr_data[0] || Loc.isInvalid()) { | 
| Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2479 | if (file) | 
|  | 2480 | *file = 0; | 
|  | 2481 | if (line) | 
|  | 2482 | *line = 0; | 
|  | 2483 | if (column) | 
|  | 2484 | *column = 0; | 
|  | 2485 | if (offset) | 
|  | 2486 | *offset = 0; | 
|  | 2487 | return; | 
|  | 2488 | } | 
|  | 2489 |  | 
| Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2490 | const SourceManager &SM = | 
|  | 2491 | *static_cast<const SourceManager*>(location.ptr_data[0]); | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2492 | SourceLocation InstLoc = SM.getInstantiationLoc(Loc); | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2493 |  | 
|  | 2494 | if (file) | 
|  | 2495 | *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); | 
|  | 2496 | if (line) | 
|  | 2497 | *line = SM.getInstantiationLineNumber(InstLoc); | 
|  | 2498 | if (column) | 
|  | 2499 | *column = SM.getInstantiationColumnNumber(InstLoc); | 
| Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2500 | if (offset) | 
| Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2501 | *offset = SM.getDecomposedLoc(InstLoc).second; | 
| Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2502 | } | 
|  | 2503 |  | 
| Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2504 | void clang_getSpellingLocation(CXSourceLocation location, | 
|  | 2505 | CXFile *file, | 
|  | 2506 | unsigned *line, | 
|  | 2507 | unsigned *column, | 
|  | 2508 | unsigned *offset) { | 
|  | 2509 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); | 
|  | 2510 |  | 
|  | 2511 | if (!location.ptr_data[0] || Loc.isInvalid()) { | 
|  | 2512 | if (file) | 
|  | 2513 | *file = 0; | 
|  | 2514 | if (line) | 
|  | 2515 | *line = 0; | 
|  | 2516 | if (column) | 
|  | 2517 | *column = 0; | 
|  | 2518 | if (offset) | 
|  | 2519 | *offset = 0; | 
|  | 2520 | return; | 
|  | 2521 | } | 
|  | 2522 |  | 
|  | 2523 | const SourceManager &SM = | 
|  | 2524 | *static_cast<const SourceManager*>(location.ptr_data[0]); | 
|  | 2525 | SourceLocation SpellLoc = Loc; | 
|  | 2526 | if (SpellLoc.isMacroID()) { | 
|  | 2527 | SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); | 
|  | 2528 | if (SimpleSpellingLoc.isFileID() && | 
|  | 2529 | SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) | 
|  | 2530 | SpellLoc = SimpleSpellingLoc; | 
|  | 2531 | else | 
|  | 2532 | SpellLoc = SM.getInstantiationLoc(SpellLoc); | 
|  | 2533 | } | 
|  | 2534 |  | 
|  | 2535 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); | 
|  | 2536 | FileID FID = LocInfo.first; | 
|  | 2537 | unsigned FileOffset = LocInfo.second; | 
|  | 2538 |  | 
|  | 2539 | if (file) | 
|  | 2540 | *file = (void *)SM.getFileEntryForID(FID); | 
|  | 2541 | if (line) | 
|  | 2542 | *line = SM.getLineNumber(FID, FileOffset); | 
|  | 2543 | if (column) | 
|  | 2544 | *column = SM.getColumnNumber(FID, FileOffset); | 
|  | 2545 | if (offset) | 
|  | 2546 | *offset = FileOffset; | 
|  | 2547 | } | 
|  | 2548 |  | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2549 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2550 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2551 | range.begin_int_data }; | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2552 | return Result; | 
|  | 2553 | } | 
|  | 2554 |  | 
|  | 2555 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { | 
| Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2556 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2557 | range.end_int_data }; | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2558 | return Result; | 
|  | 2559 | } | 
|  | 2560 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2561 | } // end: extern "C" | 
|  | 2562 |  | 
| Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2563 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2564 | // CXFile Operations. | 
|  | 2565 | //===----------------------------------------------------------------------===// | 
|  | 2566 |  | 
|  | 2567 | extern "C" { | 
| Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2568 | CXString clang_getFileName(CXFile SFile) { | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2569 | if (!SFile) | 
| Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2570 | return createCXString(NULL); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2571 |  | 
| Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2572 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); | 
| Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2573 | return createCXString(FEnt->getName()); | 
| Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2574 | } | 
|  | 2575 |  | 
|  | 2576 | time_t clang_getFileTime(CXFile SFile) { | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2577 | if (!SFile) | 
|  | 2578 | return 0; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2579 |  | 
| Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2580 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); | 
|  | 2581 | return FEnt->getModificationTime(); | 
| Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2582 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2583 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2584 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { | 
|  | 2585 | if (!tu) | 
|  | 2586 | return 0; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2587 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2588 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2589 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2590 | FileManager &FMgr = CXXUnit->getFileManager(); | 
| Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2591 | const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name), | 
|  | 2592 | CXXUnit->getFileSystemOpts()); | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2593 | return const_cast<FileEntry *>(File); | 
|  | 2594 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2595 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2596 | } // end: extern "C" | 
| Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2597 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2598 | //===----------------------------------------------------------------------===// | 
|  | 2599 | // CXCursor Operations. | 
|  | 2600 | //===----------------------------------------------------------------------===// | 
|  | 2601 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2602 | static Decl *getDeclFromExpr(Stmt *E) { | 
| Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2603 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) | 
|  | 2604 | return getDeclFromExpr(CE->getSubExpr()); | 
|  | 2605 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2606 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) | 
|  | 2607 | return RefExpr->getDecl(); | 
| Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2608 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) | 
|  | 2609 | return RefExpr->getDecl(); | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2610 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) | 
|  | 2611 | return ME->getMemberDecl(); | 
|  | 2612 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) | 
|  | 2613 | return RE->getDecl(); | 
| Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2614 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) | 
|  | 2615 | return PRE->getProperty(); | 
|  | 2616 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2617 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) | 
|  | 2618 | return getDeclFromExpr(CE->getCallee()); | 
| Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2619 | if (CXXConstructExpr *CE = llvm::dyn_cast<CXXConstructExpr>(E)) | 
|  | 2620 | if (!CE->isElidable()) | 
|  | 2621 | return CE->getConstructor(); | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2622 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) | 
|  | 2623 | return OME->getMethodDecl(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2624 |  | 
| Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2625 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) | 
|  | 2626 | return PE->getProtocol(); | 
|  | 2627 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2628 | return 0; | 
|  | 2629 | } | 
|  | 2630 |  | 
| Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2631 | static SourceLocation getLocationFromExpr(Expr *E) { | 
|  | 2632 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) | 
|  | 2633 | return /*FIXME:*/Msg->getLeftLoc(); | 
|  | 2634 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) | 
|  | 2635 | return DRE->getLocation(); | 
| Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2636 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) | 
|  | 2637 | return RefExpr->getLocation(); | 
| Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2638 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) | 
|  | 2639 | return Member->getMemberLoc(); | 
|  | 2640 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) | 
|  | 2641 | return Ivar->getLocation(); | 
|  | 2642 | return E->getLocStart(); | 
|  | 2643 | } | 
|  | 2644 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2645 | extern "C" { | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2646 |  | 
|  | 2647 | unsigned clang_visitChildren(CXCursor parent, | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2648 | CXCursorVisitor visitor, | 
|  | 2649 | CXClientData client_data) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2650 | ASTUnit *CXXUnit = getCursorASTUnit(parent); | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2651 |  | 
| Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 2652 | CursorVisitor CursorVis(CXXUnit, visitor, client_data, | 
|  | 2653 | CXXUnit->getMaxPCHLevel()); | 
| Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2654 | return CursorVis.VisitChildren(parent); | 
|  | 2655 | } | 
|  | 2656 |  | 
| David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2657 | #ifndef __has_feature | 
|  | 2658 | #define __has_feature(x) 0 | 
|  | 2659 | #endif | 
|  | 2660 | #if __has_feature(blocks) | 
|  | 2661 | typedef enum CXChildVisitResult | 
|  | 2662 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); | 
|  | 2663 |  | 
|  | 2664 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, | 
|  | 2665 | CXClientData client_data) { | 
|  | 2666 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; | 
|  | 2667 | return block(cursor, parent); | 
|  | 2668 | } | 
|  | 2669 | #else | 
|  | 2670 | // If we are compiled with a compiler that doesn't have native blocks support, | 
|  | 2671 | // define and call the block manually, so the | 
|  | 2672 | typedef struct _CXChildVisitResult | 
|  | 2673 | { | 
|  | 2674 | void *isa; | 
|  | 2675 | int flags; | 
|  | 2676 | int reserved; | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2677 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, | 
|  | 2678 | CXCursor); | 
| David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2679 | } *CXCursorVisitorBlock; | 
|  | 2680 |  | 
|  | 2681 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, | 
|  | 2682 | CXClientData client_data) { | 
|  | 2683 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; | 
|  | 2684 | return block->invoke(block, cursor, parent); | 
|  | 2685 | } | 
|  | 2686 | #endif | 
|  | 2687 |  | 
|  | 2688 |  | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2689 | unsigned clang_visitChildrenWithBlock(CXCursor parent, | 
|  | 2690 | CXCursorVisitorBlock block) { | 
| David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2691 | return clang_visitChildren(parent, visitWithBlock, block); | 
|  | 2692 | } | 
|  | 2693 |  | 
| Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2694 | static CXString getDeclSpelling(Decl *D) { | 
|  | 2695 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); | 
|  | 2696 | if (!ND) | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2697 | return createCXString(""); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2698 |  | 
| Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2699 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2700 | return createCXString(OMD->getSelector().getAsString()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2701 |  | 
| Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2702 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) | 
|  | 2703 | // No, this isn't the same as the code below. getIdentifier() is non-virtual | 
|  | 2704 | // and returns different names. NamedDecl returns the class name and | 
|  | 2705 | // ObjCCategoryImplDecl returns the category name. | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2706 | return createCXString(CIMP->getIdentifier()->getNameStart()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2707 |  | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2708 | if (isa<UsingDirectiveDecl>(D)) | 
|  | 2709 | return createCXString(""); | 
|  | 2710 |  | 
| Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 2711 | llvm::SmallString<1024> S; | 
|  | 2712 | llvm::raw_svector_ostream os(S); | 
|  | 2713 | ND->printName(os); | 
|  | 2714 |  | 
|  | 2715 | return createCXString(os.str()); | 
| Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2716 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2717 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2718 | CXString clang_getCursorSpelling(CXCursor C) { | 
| Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2719 | if (clang_isTranslationUnit(C.kind)) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2720 | return clang_getTranslationUnitSpelling(C.data[2]); | 
| Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2721 |  | 
| Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2722 | if (clang_isReference(C.kind)) { | 
|  | 2723 | switch (C.kind) { | 
| Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2724 | case CXCursor_ObjCSuperClassRef: { | 
| Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 2725 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2726 | return createCXString(Super->getIdentifier()->getNameStart()); | 
| Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2727 | } | 
|  | 2728 | case CXCursor_ObjCClassRef: { | 
| Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 2729 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2730 | return createCXString(Class->getIdentifier()->getNameStart()); | 
| Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2731 | } | 
|  | 2732 | case CXCursor_ObjCProtocolRef: { | 
| Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 2733 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2734 | assert(OID && "getCursorSpelling(): Missing protocol decl"); | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2735 | return createCXString(OID->getIdentifier()->getNameStart()); | 
| Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2736 | } | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2737 | case CXCursor_CXXBaseSpecifier: { | 
|  | 2738 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); | 
|  | 2739 | return createCXString(B->getType().getAsString()); | 
|  | 2740 | } | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2741 | case CXCursor_TypeRef: { | 
|  | 2742 | TypeDecl *Type = getCursorTypeRef(C).first; | 
|  | 2743 | assert(Type && "Missing type decl"); | 
|  | 2744 |  | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2745 | return createCXString(getCursorContext(C).getTypeDeclType(Type). | 
|  | 2746 | getAsString()); | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2747 | } | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2748 | case CXCursor_TemplateRef: { | 
|  | 2749 | TemplateDecl *Template = getCursorTemplateRef(C).first; | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2750 | assert(Template && "Missing template decl"); | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2751 |  | 
|  | 2752 | return createCXString(Template->getNameAsString()); | 
|  | 2753 | } | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2754 |  | 
|  | 2755 | case CXCursor_NamespaceRef: { | 
|  | 2756 | NamedDecl *NS = getCursorNamespaceRef(C).first; | 
|  | 2757 | assert(NS && "Missing namespace decl"); | 
|  | 2758 |  | 
|  | 2759 | return createCXString(NS->getNameAsString()); | 
|  | 2760 | } | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2761 |  | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 2762 | case CXCursor_MemberRef: { | 
|  | 2763 | FieldDecl *Field = getCursorMemberRef(C).first; | 
|  | 2764 | assert(Field && "Missing member decl"); | 
|  | 2765 |  | 
|  | 2766 | return createCXString(Field->getNameAsString()); | 
|  | 2767 | } | 
|  | 2768 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2769 | case CXCursor_LabelRef: { | 
|  | 2770 | LabelStmt *Label = getCursorLabelRef(C).first; | 
|  | 2771 | assert(Label && "Missing label"); | 
|  | 2772 |  | 
|  | 2773 | return createCXString(Label->getID()->getName()); | 
|  | 2774 | } | 
|  | 2775 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 2776 | case CXCursor_OverloadedDeclRef: { | 
|  | 2777 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; | 
|  | 2778 | if (Decl *D = Storage.dyn_cast<Decl *>()) { | 
|  | 2779 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) | 
|  | 2780 | return createCXString(ND->getNameAsString()); | 
|  | 2781 | return createCXString(""); | 
|  | 2782 | } | 
|  | 2783 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) | 
|  | 2784 | return createCXString(E->getName().getAsString()); | 
|  | 2785 | OverloadedTemplateStorage *Ovl | 
|  | 2786 | = Storage.get<OverloadedTemplateStorage*>(); | 
|  | 2787 | if (Ovl->size() == 0) | 
|  | 2788 | return createCXString(""); | 
|  | 2789 | return createCXString((*Ovl->begin())->getNameAsString()); | 
|  | 2790 | } | 
|  | 2791 |  | 
| Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2792 | default: | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2793 | return createCXString("<not implemented>"); | 
| Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2794 | } | 
|  | 2795 | } | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2796 |  | 
|  | 2797 | if (clang_isExpression(C.kind)) { | 
|  | 2798 | Decl *D = getDeclFromExpr(getCursorExpr(C)); | 
|  | 2799 | if (D) | 
| Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2800 | return getDeclSpelling(D); | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2801 | return createCXString(""); | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2802 | } | 
|  | 2803 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2804 | if (clang_isStatement(C.kind)) { | 
|  | 2805 | Stmt *S = getCursorStmt(C); | 
|  | 2806 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) | 
|  | 2807 | return createCXString(Label->getID()->getName()); | 
|  | 2808 |  | 
|  | 2809 | return createCXString(""); | 
|  | 2810 | } | 
|  | 2811 |  | 
| Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 2812 | if (C.kind == CXCursor_MacroInstantiation) | 
|  | 2813 | return createCXString(getCursorMacroInstantiation(C)->getName() | 
|  | 2814 | ->getNameStart()); | 
|  | 2815 |  | 
| Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2816 | if (C.kind == CXCursor_MacroDefinition) | 
|  | 2817 | return createCXString(getCursorMacroDefinition(C)->getName() | 
|  | 2818 | ->getNameStart()); | 
|  | 2819 |  | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 2820 | if (C.kind == CXCursor_InclusionDirective) | 
|  | 2821 | return createCXString(getCursorInclusionDirective(C)->getFileName()); | 
|  | 2822 |  | 
| Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 2823 | if (clang_isDeclaration(C.kind)) | 
|  | 2824 | return getDeclSpelling(getCursorDecl(C)); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2825 |  | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2826 | return createCXString(""); | 
| Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2827 | } | 
|  | 2828 |  | 
| Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 2829 | CXString clang_getCursorDisplayName(CXCursor C) { | 
|  | 2830 | if (!clang_isDeclaration(C.kind)) | 
|  | 2831 | return clang_getCursorSpelling(C); | 
|  | 2832 |  | 
|  | 2833 | Decl *D = getCursorDecl(C); | 
|  | 2834 | if (!D) | 
|  | 2835 | return createCXString(""); | 
|  | 2836 |  | 
|  | 2837 | PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy; | 
|  | 2838 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) | 
|  | 2839 | D = FunTmpl->getTemplatedDecl(); | 
|  | 2840 |  | 
|  | 2841 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { | 
|  | 2842 | llvm::SmallString<64> Str; | 
|  | 2843 | llvm::raw_svector_ostream OS(Str); | 
|  | 2844 | OS << Function->getNameAsString(); | 
|  | 2845 | if (Function->getPrimaryTemplate()) | 
|  | 2846 | OS << "<>"; | 
|  | 2847 | OS << "("; | 
|  | 2848 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { | 
|  | 2849 | if (I) | 
|  | 2850 | OS << ", "; | 
|  | 2851 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); | 
|  | 2852 | } | 
|  | 2853 |  | 
|  | 2854 | if (Function->isVariadic()) { | 
|  | 2855 | if (Function->getNumParams()) | 
|  | 2856 | OS << ", "; | 
|  | 2857 | OS << "..."; | 
|  | 2858 | } | 
|  | 2859 | OS << ")"; | 
|  | 2860 | return createCXString(OS.str()); | 
|  | 2861 | } | 
|  | 2862 |  | 
|  | 2863 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { | 
|  | 2864 | llvm::SmallString<64> Str; | 
|  | 2865 | llvm::raw_svector_ostream OS(Str); | 
|  | 2866 | OS << ClassTemplate->getNameAsString(); | 
|  | 2867 | OS << "<"; | 
|  | 2868 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); | 
|  | 2869 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | 
|  | 2870 | if (I) | 
|  | 2871 | OS << ", "; | 
|  | 2872 |  | 
|  | 2873 | NamedDecl *Param = Params->getParam(I); | 
|  | 2874 | if (Param->getIdentifier()) { | 
|  | 2875 | OS << Param->getIdentifier()->getName(); | 
|  | 2876 | continue; | 
|  | 2877 | } | 
|  | 2878 |  | 
|  | 2879 | // There is no parameter name, which makes this tricky. Try to come up | 
|  | 2880 | // with something useful that isn't too long. | 
|  | 2881 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) | 
|  | 2882 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); | 
|  | 2883 | else if (NonTypeTemplateParmDecl *NTTP | 
|  | 2884 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) | 
|  | 2885 | OS << NTTP->getType().getAsString(Policy); | 
|  | 2886 | else | 
|  | 2887 | OS << "template<...> class"; | 
|  | 2888 | } | 
|  | 2889 |  | 
|  | 2890 | OS << ">"; | 
|  | 2891 | return createCXString(OS.str()); | 
|  | 2892 | } | 
|  | 2893 |  | 
|  | 2894 | if (ClassTemplateSpecializationDecl *ClassSpec | 
|  | 2895 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { | 
|  | 2896 | // If the type was explicitly written, use that. | 
|  | 2897 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) | 
|  | 2898 | return createCXString(TSInfo->getType().getAsString(Policy)); | 
|  | 2899 |  | 
|  | 2900 | llvm::SmallString<64> Str; | 
|  | 2901 | llvm::raw_svector_ostream OS(Str); | 
|  | 2902 | OS << ClassSpec->getNameAsString(); | 
|  | 2903 | OS << TemplateSpecializationType::PrintTemplateArgumentList( | 
| Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2904 | ClassSpec->getTemplateArgs().data(), | 
|  | 2905 | ClassSpec->getTemplateArgs().size(), | 
| Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 2906 | Policy); | 
|  | 2907 | return createCXString(OS.str()); | 
|  | 2908 | } | 
|  | 2909 |  | 
|  | 2910 | return clang_getCursorSpelling(C); | 
|  | 2911 | } | 
|  | 2912 |  | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2913 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 2914 | switch (Kind) { | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2915 | case CXCursor_FunctionDecl: | 
|  | 2916 | return createCXString("FunctionDecl"); | 
|  | 2917 | case CXCursor_TypedefDecl: | 
|  | 2918 | return createCXString("TypedefDecl"); | 
|  | 2919 | case CXCursor_EnumDecl: | 
|  | 2920 | return createCXString("EnumDecl"); | 
|  | 2921 | case CXCursor_EnumConstantDecl: | 
|  | 2922 | return createCXString("EnumConstantDecl"); | 
|  | 2923 | case CXCursor_StructDecl: | 
|  | 2924 | return createCXString("StructDecl"); | 
|  | 2925 | case CXCursor_UnionDecl: | 
|  | 2926 | return createCXString("UnionDecl"); | 
|  | 2927 | case CXCursor_ClassDecl: | 
|  | 2928 | return createCXString("ClassDecl"); | 
|  | 2929 | case CXCursor_FieldDecl: | 
|  | 2930 | return createCXString("FieldDecl"); | 
|  | 2931 | case CXCursor_VarDecl: | 
|  | 2932 | return createCXString("VarDecl"); | 
|  | 2933 | case CXCursor_ParmDecl: | 
|  | 2934 | return createCXString("ParmDecl"); | 
|  | 2935 | case CXCursor_ObjCInterfaceDecl: | 
|  | 2936 | return createCXString("ObjCInterfaceDecl"); | 
|  | 2937 | case CXCursor_ObjCCategoryDecl: | 
|  | 2938 | return createCXString("ObjCCategoryDecl"); | 
|  | 2939 | case CXCursor_ObjCProtocolDecl: | 
|  | 2940 | return createCXString("ObjCProtocolDecl"); | 
|  | 2941 | case CXCursor_ObjCPropertyDecl: | 
|  | 2942 | return createCXString("ObjCPropertyDecl"); | 
|  | 2943 | case CXCursor_ObjCIvarDecl: | 
|  | 2944 | return createCXString("ObjCIvarDecl"); | 
|  | 2945 | case CXCursor_ObjCInstanceMethodDecl: | 
|  | 2946 | return createCXString("ObjCInstanceMethodDecl"); | 
|  | 2947 | case CXCursor_ObjCClassMethodDecl: | 
|  | 2948 | return createCXString("ObjCClassMethodDecl"); | 
|  | 2949 | case CXCursor_ObjCImplementationDecl: | 
|  | 2950 | return createCXString("ObjCImplementationDecl"); | 
|  | 2951 | case CXCursor_ObjCCategoryImplDecl: | 
|  | 2952 | return createCXString("ObjCCategoryImplDecl"); | 
| Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 2953 | case CXCursor_CXXMethod: | 
|  | 2954 | return createCXString("CXXMethod"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2955 | case CXCursor_UnexposedDecl: | 
|  | 2956 | return createCXString("UnexposedDecl"); | 
|  | 2957 | case CXCursor_ObjCSuperClassRef: | 
|  | 2958 | return createCXString("ObjCSuperClassRef"); | 
|  | 2959 | case CXCursor_ObjCProtocolRef: | 
|  | 2960 | return createCXString("ObjCProtocolRef"); | 
|  | 2961 | case CXCursor_ObjCClassRef: | 
|  | 2962 | return createCXString("ObjCClassRef"); | 
|  | 2963 | case CXCursor_TypeRef: | 
|  | 2964 | return createCXString("TypeRef"); | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2965 | case CXCursor_TemplateRef: | 
|  | 2966 | return createCXString("TemplateRef"); | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2967 | case CXCursor_NamespaceRef: | 
|  | 2968 | return createCXString("NamespaceRef"); | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 2969 | case CXCursor_MemberRef: | 
|  | 2970 | return createCXString("MemberRef"); | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2971 | case CXCursor_LabelRef: | 
|  | 2972 | return createCXString("LabelRef"); | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 2973 | case CXCursor_OverloadedDeclRef: | 
|  | 2974 | return createCXString("OverloadedDeclRef"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2975 | case CXCursor_UnexposedExpr: | 
|  | 2976 | return createCXString("UnexposedExpr"); | 
| Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 2977 | case CXCursor_BlockExpr: | 
|  | 2978 | return createCXString("BlockExpr"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2979 | case CXCursor_DeclRefExpr: | 
|  | 2980 | return createCXString("DeclRefExpr"); | 
|  | 2981 | case CXCursor_MemberRefExpr: | 
|  | 2982 | return createCXString("MemberRefExpr"); | 
|  | 2983 | case CXCursor_CallExpr: | 
|  | 2984 | return createCXString("CallExpr"); | 
|  | 2985 | case CXCursor_ObjCMessageExpr: | 
|  | 2986 | return createCXString("ObjCMessageExpr"); | 
|  | 2987 | case CXCursor_UnexposedStmt: | 
|  | 2988 | return createCXString("UnexposedStmt"); | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2989 | case CXCursor_LabelStmt: | 
|  | 2990 | return createCXString("LabelStmt"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2991 | case CXCursor_InvalidFile: | 
|  | 2992 | return createCXString("InvalidFile"); | 
| Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 2993 | case CXCursor_InvalidCode: | 
|  | 2994 | return createCXString("InvalidCode"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2995 | case CXCursor_NoDeclFound: | 
|  | 2996 | return createCXString("NoDeclFound"); | 
|  | 2997 | case CXCursor_NotImplemented: | 
|  | 2998 | return createCXString("NotImplemented"); | 
|  | 2999 | case CXCursor_TranslationUnit: | 
|  | 3000 | return createCXString("TranslationUnit"); | 
| Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3001 | case CXCursor_UnexposedAttr: | 
|  | 3002 | return createCXString("UnexposedAttr"); | 
|  | 3003 | case CXCursor_IBActionAttr: | 
|  | 3004 | return createCXString("attribute(ibaction)"); | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3005 | case CXCursor_IBOutletAttr: | 
|  | 3006 | return createCXString("attribute(iboutlet)"); | 
| Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3007 | case CXCursor_IBOutletCollectionAttr: | 
|  | 3008 | return createCXString("attribute(iboutletcollection)"); | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3009 | case CXCursor_PreprocessingDirective: | 
|  | 3010 | return createCXString("preprocessing directive"); | 
| Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3011 | case CXCursor_MacroDefinition: | 
|  | 3012 | return createCXString("macro definition"); | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3013 | case CXCursor_MacroInstantiation: | 
|  | 3014 | return createCXString("macro instantiation"); | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3015 | case CXCursor_InclusionDirective: | 
|  | 3016 | return createCXString("inclusion directive"); | 
| Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3017 | case CXCursor_Namespace: | 
|  | 3018 | return createCXString("Namespace"); | 
| Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3019 | case CXCursor_LinkageSpec: | 
|  | 3020 | return createCXString("LinkageSpec"); | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3021 | case CXCursor_CXXBaseSpecifier: | 
|  | 3022 | return createCXString("C++ base class specifier"); | 
| Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3023 | case CXCursor_Constructor: | 
|  | 3024 | return createCXString("CXXConstructor"); | 
|  | 3025 | case CXCursor_Destructor: | 
|  | 3026 | return createCXString("CXXDestructor"); | 
|  | 3027 | case CXCursor_ConversionFunction: | 
|  | 3028 | return createCXString("CXXConversion"); | 
| Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3029 | case CXCursor_TemplateTypeParameter: | 
|  | 3030 | return createCXString("TemplateTypeParameter"); | 
|  | 3031 | case CXCursor_NonTypeTemplateParameter: | 
|  | 3032 | return createCXString("NonTypeTemplateParameter"); | 
|  | 3033 | case CXCursor_TemplateTemplateParameter: | 
|  | 3034 | return createCXString("TemplateTemplateParameter"); | 
|  | 3035 | case CXCursor_FunctionTemplate: | 
|  | 3036 | return createCXString("FunctionTemplate"); | 
| Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3037 | case CXCursor_ClassTemplate: | 
|  | 3038 | return createCXString("ClassTemplate"); | 
| Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3039 | case CXCursor_ClassTemplatePartialSpecialization: | 
|  | 3040 | return createCXString("ClassTemplatePartialSpecialization"); | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3041 | case CXCursor_NamespaceAlias: | 
|  | 3042 | return createCXString("NamespaceAlias"); | 
| Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3043 | case CXCursor_UsingDirective: | 
|  | 3044 | return createCXString("UsingDirective"); | 
| Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3045 | case CXCursor_UsingDeclaration: | 
|  | 3046 | return createCXString("UsingDeclaration"); | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3047 | } | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3048 |  | 
| Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3049 | llvm_unreachable("Unhandled CXCursorKind"); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3050 | return createCXString(NULL); | 
| Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3051 | } | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3052 |  | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3053 | enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, | 
|  | 3054 | CXCursor parent, | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3055 | CXClientData client_data) { | 
|  | 3056 | CXCursor *BestCursor = static_cast<CXCursor *>(client_data); | 
| Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3057 |  | 
|  | 3058 | // If our current best cursor is the construction of a temporary object, | 
|  | 3059 | // don't replace that cursor with a type reference, because we want | 
|  | 3060 | // clang_getCursor() to point at the constructor. | 
|  | 3061 | if (clang_isExpression(BestCursor->kind) && | 
|  | 3062 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && | 
|  | 3063 | cursor.kind == CXCursor_TypeRef) | 
|  | 3064 | return CXChildVisit_Recurse; | 
|  | 3065 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3066 | *BestCursor = cursor; | 
|  | 3067 | return CXChildVisit_Recurse; | 
|  | 3068 | } | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3069 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3070 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { | 
|  | 3071 | if (!TU) | 
| Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3072 | return clang_getNullCursor(); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3073 |  | 
| Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3074 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
| Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3075 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); | 
|  | 3076 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3077 | // Translate the given source location to make it point at the beginning of | 
|  | 3078 | // the token under the cursor. | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3079 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); | 
| Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3080 |  | 
|  | 3081 | // Guard against an invalid SourceLocation, or we may assert in one | 
|  | 3082 | // of the following calls. | 
|  | 3083 | if (SLoc.isInvalid()) | 
|  | 3084 | return clang_getNullCursor(); | 
|  | 3085 |  | 
| Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3086 | bool Logging = getenv("LIBCLANG_LOGGING"); | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3087 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), | 
|  | 3088 | CXXUnit->getASTContext().getLangOptions()); | 
|  | 3089 |  | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3090 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); | 
|  | 3091 | if (SLoc.isValid()) { | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3092 | // FIXME: Would be great to have a "hint" cursor, then walk from that | 
|  | 3093 | // hint cursor upward until we find a cursor whose source range encloses | 
|  | 3094 | // the region of interest, rather than starting from the translation unit. | 
|  | 3095 | CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit); | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3096 | CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3097 | Decl::MaxPCHLevel, SourceLocation(SLoc)); | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3098 | CursorVis.VisitChildren(Parent); | 
| Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3099 | } | 
| Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3100 |  | 
|  | 3101 | if (Logging) { | 
|  | 3102 | CXFile SearchFile; | 
|  | 3103 | unsigned SearchLine, SearchColumn; | 
|  | 3104 | CXFile ResultFile; | 
|  | 3105 | unsigned ResultLine, ResultColumn; | 
|  | 3106 | CXString SearchFileName, ResultFileName, KindSpelling; | 
|  | 3107 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); | 
|  | 3108 |  | 
|  | 3109 | clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, | 
|  | 3110 | 0); | 
|  | 3111 | clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine, | 
|  | 3112 | &ResultColumn, 0); | 
|  | 3113 | SearchFileName = clang_getFileName(SearchFile); | 
|  | 3114 | ResultFileName = clang_getFileName(ResultFile); | 
|  | 3115 | KindSpelling = clang_getCursorKindSpelling(Result.kind); | 
|  | 3116 | fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d)\n", | 
|  | 3117 | clang_getCString(SearchFileName), SearchLine, SearchColumn, | 
|  | 3118 | clang_getCString(KindSpelling), | 
|  | 3119 | clang_getCString(ResultFileName), ResultLine, ResultColumn); | 
|  | 3120 | clang_disposeString(SearchFileName); | 
|  | 3121 | clang_disposeString(ResultFileName); | 
|  | 3122 | clang_disposeString(KindSpelling); | 
|  | 3123 | } | 
|  | 3124 |  | 
| Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3125 | return Result; | 
| Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3126 | } | 
|  | 3127 |  | 
| Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3128 | CXCursor clang_getNullCursor(void) { | 
| Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3129 | return MakeCXCursorInvalid(CXCursor_InvalidFile); | 
| Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3130 | } | 
|  | 3131 |  | 
|  | 3132 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { | 
| Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3133 | return X == Y; | 
| Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3134 | } | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3135 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3136 | unsigned clang_isInvalid(enum CXCursorKind K) { | 
| Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3137 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; | 
|  | 3138 | } | 
|  | 3139 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3140 | unsigned clang_isDeclaration(enum CXCursorKind K) { | 
| Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3141 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; | 
|  | 3142 | } | 
| Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3143 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3144 | unsigned clang_isReference(enum CXCursorKind K) { | 
| Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3145 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; | 
|  | 3146 | } | 
|  | 3147 |  | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3148 | unsigned clang_isExpression(enum CXCursorKind K) { | 
|  | 3149 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; | 
|  | 3150 | } | 
|  | 3151 |  | 
|  | 3152 | unsigned clang_isStatement(enum CXCursorKind K) { | 
|  | 3153 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; | 
|  | 3154 | } | 
|  | 3155 |  | 
| Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3156 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { | 
|  | 3157 | return K == CXCursor_TranslationUnit; | 
|  | 3158 | } | 
|  | 3159 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3160 | unsigned clang_isPreprocessing(enum CXCursorKind K) { | 
|  | 3161 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; | 
|  | 3162 | } | 
|  | 3163 |  | 
| Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3164 | unsigned clang_isUnexposed(enum CXCursorKind K) { | 
|  | 3165 | switch (K) { | 
|  | 3166 | case CXCursor_UnexposedDecl: | 
|  | 3167 | case CXCursor_UnexposedExpr: | 
|  | 3168 | case CXCursor_UnexposedStmt: | 
|  | 3169 | case CXCursor_UnexposedAttr: | 
|  | 3170 | return true; | 
|  | 3171 | default: | 
|  | 3172 | return false; | 
|  | 3173 | } | 
|  | 3174 | } | 
|  | 3175 |  | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3176 | CXCursorKind clang_getCursorKind(CXCursor C) { | 
| Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3177 | return C.kind; | 
|  | 3178 | } | 
|  | 3179 |  | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3180 | CXSourceLocation clang_getCursorLocation(CXCursor C) { | 
|  | 3181 | if (clang_isReference(C.kind)) { | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3182 | switch (C.kind) { | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3183 | case CXCursor_ObjCSuperClassRef: { | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3184 | std::pair<ObjCInterfaceDecl *, SourceLocation> P | 
|  | 3185 | = getCursorObjCSuperClassRef(C); | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3186 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3187 | } | 
|  | 3188 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3189 | case CXCursor_ObjCProtocolRef: { | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3190 | std::pair<ObjCProtocolDecl *, SourceLocation> P | 
|  | 3191 | = getCursorObjCProtocolRef(C); | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3192 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3193 | } | 
|  | 3194 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3195 | case CXCursor_ObjCClassRef: { | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3196 | std::pair<ObjCInterfaceDecl *, SourceLocation> P | 
|  | 3197 | = getCursorObjCClassRef(C); | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3198 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3199 | } | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3200 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3201 | case CXCursor_TypeRef: { | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3202 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); | 
| Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3203 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3204 | } | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3205 |  | 
|  | 3206 | case CXCursor_TemplateRef: { | 
|  | 3207 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); | 
|  | 3208 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
|  | 3209 | } | 
|  | 3210 |  | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3211 | case CXCursor_NamespaceRef: { | 
|  | 3212 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); | 
|  | 3213 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
|  | 3214 | } | 
|  | 3215 |  | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3216 | case CXCursor_MemberRef: { | 
|  | 3217 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); | 
|  | 3218 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); | 
|  | 3219 | } | 
|  | 3220 |  | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3221 | case CXCursor_CXXBaseSpecifier: { | 
| Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3222 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); | 
|  | 3223 | if (!BaseSpec) | 
|  | 3224 | return clang_getNullLocation(); | 
|  | 3225 |  | 
|  | 3226 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) | 
|  | 3227 | return cxloc::translateSourceLocation(getCursorContext(C), | 
|  | 3228 | TSInfo->getTypeLoc().getBeginLoc()); | 
|  | 3229 |  | 
|  | 3230 | return cxloc::translateSourceLocation(getCursorContext(C), | 
|  | 3231 | BaseSpec->getSourceRange().getBegin()); | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3232 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3233 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3234 | case CXCursor_LabelRef: { | 
|  | 3235 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); | 
|  | 3236 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); | 
|  | 3237 | } | 
|  | 3238 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3239 | case CXCursor_OverloadedDeclRef: | 
|  | 3240 | return cxloc::translateSourceLocation(getCursorContext(C), | 
|  | 3241 | getCursorOverloadedDeclRef(C).second); | 
|  | 3242 |  | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3243 | default: | 
|  | 3244 | // FIXME: Need a way to enumerate all non-reference cases. | 
|  | 3245 | llvm_unreachable("Missed a reference kind"); | 
|  | 3246 | } | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3247 | } | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3248 |  | 
|  | 3249 | if (clang_isExpression(C.kind)) | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3250 | return cxloc::translateSourceLocation(getCursorContext(C), | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3251 | getLocationFromExpr(getCursorExpr(C))); | 
|  | 3252 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3253 | if (clang_isStatement(C.kind)) | 
|  | 3254 | return cxloc::translateSourceLocation(getCursorContext(C), | 
|  | 3255 | getCursorStmt(C)->getLocStart()); | 
|  | 3256 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3257 | if (C.kind == CXCursor_PreprocessingDirective) { | 
|  | 3258 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); | 
|  | 3259 | return cxloc::translateSourceLocation(getCursorContext(C), L); | 
|  | 3260 | } | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3261 |  | 
|  | 3262 | if (C.kind == CXCursor_MacroInstantiation) { | 
| Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3263 | SourceLocation L | 
|  | 3264 | = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin(); | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3265 | return cxloc::translateSourceLocation(getCursorContext(C), L); | 
|  | 3266 | } | 
| Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3267 |  | 
|  | 3268 | if (C.kind == CXCursor_MacroDefinition) { | 
|  | 3269 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); | 
|  | 3270 | return cxloc::translateSourceLocation(getCursorContext(C), L); | 
|  | 3271 | } | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3272 |  | 
|  | 3273 | if (C.kind == CXCursor_InclusionDirective) { | 
|  | 3274 | SourceLocation L | 
|  | 3275 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); | 
|  | 3276 | return cxloc::translateSourceLocation(getCursorContext(C), L); | 
|  | 3277 | } | 
|  | 3278 |  | 
| Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3279 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3280 | return clang_getNullLocation(); | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3281 |  | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3282 | Decl *D = getCursorDecl(C); | 
| Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3283 | SourceLocation Loc = D->getLocation(); | 
|  | 3284 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) | 
|  | 3285 | Loc = Class->getClassLoc(); | 
| Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3286 | // FIXME: Multiple variables declared in a single declaration | 
|  | 3287 | // currently lack the information needed to correctly determine their | 
|  | 3288 | // ranges when accounting for the type-specifier.  We use context | 
|  | 3289 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, | 
|  | 3290 | // and if so, whether it is the first decl. | 
|  | 3291 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { | 
|  | 3292 | if (!cxcursor::isFirstInDeclGroup(C)) | 
|  | 3293 | Loc = VD->getLocation(); | 
|  | 3294 | } | 
|  | 3295 |  | 
| Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3296 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); | 
| Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3297 | } | 
| Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3298 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3299 | } // end extern "C" | 
|  | 3300 |  | 
|  | 3301 | static SourceRange getRawCursorExtent(CXCursor C) { | 
| Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3302 | if (clang_isReference(C.kind)) { | 
|  | 3303 | switch (C.kind) { | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3304 | case CXCursor_ObjCSuperClassRef: | 
|  | 3305 | return  getCursorObjCSuperClassRef(C).second; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3306 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3307 | case CXCursor_ObjCProtocolRef: | 
|  | 3308 | return getCursorObjCProtocolRef(C).second; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3309 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3310 | case CXCursor_ObjCClassRef: | 
|  | 3311 | return getCursorObjCClassRef(C).second; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3312 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3313 | case CXCursor_TypeRef: | 
|  | 3314 | return getCursorTypeRef(C).second; | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3315 |  | 
|  | 3316 | case CXCursor_TemplateRef: | 
|  | 3317 | return getCursorTemplateRef(C).second; | 
|  | 3318 |  | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3319 | case CXCursor_NamespaceRef: | 
|  | 3320 | return getCursorNamespaceRef(C).second; | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3321 |  | 
|  | 3322 | case CXCursor_MemberRef: | 
|  | 3323 | return getCursorMemberRef(C).second; | 
|  | 3324 |  | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3325 | case CXCursor_CXXBaseSpecifier: | 
| Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3326 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3327 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3328 | case CXCursor_LabelRef: | 
|  | 3329 | return getCursorLabelRef(C).second; | 
|  | 3330 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3331 | case CXCursor_OverloadedDeclRef: | 
|  | 3332 | return getCursorOverloadedDeclRef(C).second; | 
|  | 3333 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3334 | default: | 
|  | 3335 | // FIXME: Need a way to enumerate all non-reference cases. | 
|  | 3336 | llvm_unreachable("Missed a reference kind"); | 
| Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3337 | } | 
|  | 3338 | } | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3339 |  | 
|  | 3340 | if (clang_isExpression(C.kind)) | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3341 | return getCursorExpr(C)->getSourceRange(); | 
| Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3342 |  | 
|  | 3343 | if (clang_isStatement(C.kind)) | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3344 | return getCursorStmt(C)->getSourceRange(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3345 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3346 | if (C.kind == CXCursor_PreprocessingDirective) | 
|  | 3347 | return cxcursor::getCursorPreprocessingDirective(C); | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3348 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3349 | if (C.kind == CXCursor_MacroInstantiation) | 
|  | 3350 | return cxcursor::getCursorMacroInstantiation(C)->getSourceRange(); | 
| Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3351 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3352 | if (C.kind == CXCursor_MacroDefinition) | 
|  | 3353 | return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3354 |  | 
|  | 3355 | if (C.kind == CXCursor_InclusionDirective) | 
|  | 3356 | return cxcursor::getCursorInclusionDirective(C)->getSourceRange(); | 
|  | 3357 |  | 
| Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3358 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { | 
|  | 3359 | Decl *D = cxcursor::getCursorDecl(C); | 
|  | 3360 | SourceRange R = D->getSourceRange(); | 
|  | 3361 | // FIXME: Multiple variables declared in a single declaration | 
|  | 3362 | // currently lack the information needed to correctly determine their | 
|  | 3363 | // ranges when accounting for the type-specifier.  We use context | 
|  | 3364 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, | 
|  | 3365 | // and if so, whether it is the first decl. | 
|  | 3366 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { | 
|  | 3367 | if (!cxcursor::isFirstInDeclGroup(C)) | 
|  | 3368 | R.setBegin(VD->getLocation()); | 
|  | 3369 | } | 
|  | 3370 | return R; | 
|  | 3371 | } | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3372 | return SourceRange();} | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3373 |  | 
|  | 3374 | extern "C" { | 
|  | 3375 |  | 
|  | 3376 | CXSourceRange clang_getCursorExtent(CXCursor C) { | 
|  | 3377 | SourceRange R = getRawCursorExtent(C); | 
|  | 3378 | if (R.isInvalid()) | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3379 | return clang_getNullRange(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3380 |  | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3381 | return cxloc::translateSourceRange(getCursorContext(C), R); | 
| Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3382 | } | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3383 |  | 
|  | 3384 | CXCursor clang_getCursorReferenced(CXCursor C) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3385 | if (clang_isInvalid(C.kind)) | 
|  | 3386 | return clang_getNullCursor(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3387 |  | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3388 | ASTUnit *CXXUnit = getCursorASTUnit(C); | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3389 | if (clang_isDeclaration(C.kind)) { | 
|  | 3390 | Decl *D = getCursorDecl(C); | 
|  | 3391 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) | 
|  | 3392 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), CXXUnit); | 
|  | 3393 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) | 
|  | 3394 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), CXXUnit); | 
|  | 3395 | if (ObjCForwardProtocolDecl *Protocols | 
|  | 3396 | = dyn_cast<ObjCForwardProtocolDecl>(D)) | 
|  | 3397 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), CXXUnit); | 
|  | 3398 |  | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3399 | return C; | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3400 | } | 
|  | 3401 |  | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3402 | if (clang_isExpression(C.kind)) { | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3403 | Expr *E = getCursorExpr(C); | 
|  | 3404 | Decl *D = getDeclFromExpr(E); | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3405 | if (D) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3406 | return MakeCXCursor(D, CXXUnit); | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3407 |  | 
|  | 3408 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) | 
|  | 3409 | return MakeCursorOverloadedDeclRef(Ovl, CXXUnit); | 
|  | 3410 |  | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3411 | return clang_getNullCursor(); | 
|  | 3412 | } | 
|  | 3413 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3414 | if (clang_isStatement(C.kind)) { | 
|  | 3415 | Stmt *S = getCursorStmt(C); | 
|  | 3416 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) | 
|  | 3417 | return MakeCXCursor(Goto->getLabel(), getCursorDecl(C), | 
|  | 3418 | getCursorASTUnit(C)); | 
|  | 3419 |  | 
|  | 3420 | return clang_getNullCursor(); | 
|  | 3421 | } | 
|  | 3422 |  | 
| Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3423 | if (C.kind == CXCursor_MacroInstantiation) { | 
|  | 3424 | if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition()) | 
|  | 3425 | return MakeMacroDefinitionCursor(Def, CXXUnit); | 
|  | 3426 | } | 
|  | 3427 |  | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3428 | if (!clang_isReference(C.kind)) | 
|  | 3429 | return clang_getNullCursor(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3430 |  | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3431 | switch (C.kind) { | 
|  | 3432 | case CXCursor_ObjCSuperClassRef: | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3433 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3434 |  | 
|  | 3435 | case CXCursor_ObjCProtocolRef: { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3436 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3437 |  | 
|  | 3438 | case CXCursor_ObjCClassRef: | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3439 | return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit); | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3440 |  | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3441 | case CXCursor_TypeRef: | 
| Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3442 | return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit); | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3443 |  | 
|  | 3444 | case CXCursor_TemplateRef: | 
|  | 3445 | return MakeCXCursor(getCursorTemplateRef(C).first, CXXUnit); | 
|  | 3446 |  | 
| Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3447 | case CXCursor_NamespaceRef: | 
|  | 3448 | return MakeCXCursor(getCursorNamespaceRef(C).first, CXXUnit); | 
|  | 3449 |  | 
| Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3450 | case CXCursor_MemberRef: | 
|  | 3451 | return MakeCXCursor(getCursorMemberRef(C).first, CXXUnit); | 
|  | 3452 |  | 
| Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3453 | case CXCursor_CXXBaseSpecifier: { | 
|  | 3454 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); | 
|  | 3455 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), | 
|  | 3456 | CXXUnit)); | 
|  | 3457 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3458 |  | 
| Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3459 | case CXCursor_LabelRef: | 
|  | 3460 | // FIXME: We end up faking the "parent" declaration here because we | 
|  | 3461 | // don't want to make CXCursor larger. | 
|  | 3462 | return MakeCXCursor(getCursorLabelRef(C).first, | 
|  | 3463 | CXXUnit->getASTContext().getTranslationUnitDecl(), | 
|  | 3464 | CXXUnit); | 
|  | 3465 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3466 | case CXCursor_OverloadedDeclRef: | 
|  | 3467 | return C; | 
|  | 3468 |  | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3469 | default: | 
|  | 3470 | // We would prefer to enumerate all non-reference cursor kinds here. | 
|  | 3471 | llvm_unreachable("Unhandled reference cursor kind"); | 
|  | 3472 | break; | 
|  | 3473 | } | 
|  | 3474 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3475 |  | 
| Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3476 | return clang_getNullCursor(); | 
|  | 3477 | } | 
|  | 3478 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3479 | CXCursor clang_getCursorDefinition(CXCursor C) { | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3480 | if (clang_isInvalid(C.kind)) | 
|  | 3481 | return clang_getNullCursor(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3482 |  | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3483 | ASTUnit *CXXUnit = getCursorASTUnit(C); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3484 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3485 | bool WasReference = false; | 
| Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3486 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3487 | C = clang_getCursorReferenced(C); | 
|  | 3488 | WasReference = true; | 
|  | 3489 | } | 
|  | 3490 |  | 
| Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3491 | if (C.kind == CXCursor_MacroInstantiation) | 
|  | 3492 | return clang_getCursorReferenced(C); | 
|  | 3493 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3494 | if (!clang_isDeclaration(C.kind)) | 
|  | 3495 | return clang_getNullCursor(); | 
|  | 3496 |  | 
|  | 3497 | Decl *D = getCursorDecl(C); | 
|  | 3498 | if (!D) | 
|  | 3499 | return clang_getNullCursor(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3500 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3501 | switch (D->getKind()) { | 
|  | 3502 | // Declaration kinds that don't really separate the notions of | 
|  | 3503 | // declaration and definition. | 
|  | 3504 | case Decl::Namespace: | 
|  | 3505 | case Decl::Typedef: | 
|  | 3506 | case Decl::TemplateTypeParm: | 
|  | 3507 | case Decl::EnumConstant: | 
|  | 3508 | case Decl::Field: | 
|  | 3509 | case Decl::ObjCIvar: | 
|  | 3510 | case Decl::ObjCAtDefsField: | 
|  | 3511 | case Decl::ImplicitParam: | 
|  | 3512 | case Decl::ParmVar: | 
|  | 3513 | case Decl::NonTypeTemplateParm: | 
|  | 3514 | case Decl::TemplateTemplateParm: | 
|  | 3515 | case Decl::ObjCCategoryImpl: | 
|  | 3516 | case Decl::ObjCImplementation: | 
| Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3517 | case Decl::AccessSpec: | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3518 | case Decl::LinkageSpec: | 
|  | 3519 | case Decl::ObjCPropertyImpl: | 
|  | 3520 | case Decl::FileScopeAsm: | 
|  | 3521 | case Decl::StaticAssert: | 
|  | 3522 | case Decl::Block: | 
|  | 3523 | return C; | 
|  | 3524 |  | 
|  | 3525 | // Declaration kinds that don't make any sense here, but are | 
|  | 3526 | // nonetheless harmless. | 
|  | 3527 | case Decl::TranslationUnit: | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3528 | break; | 
|  | 3529 |  | 
|  | 3530 | // Declaration kinds for which the definition is not resolvable. | 
|  | 3531 | case Decl::UnresolvedUsingTypename: | 
|  | 3532 | case Decl::UnresolvedUsingValue: | 
|  | 3533 | break; | 
|  | 3534 |  | 
|  | 3535 | case Decl::UsingDirective: | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3536 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), | 
|  | 3537 | CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3538 |  | 
|  | 3539 | case Decl::NamespaceAlias: | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3540 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3541 |  | 
|  | 3542 | case Decl::Enum: | 
|  | 3543 | case Decl::Record: | 
|  | 3544 | case Decl::CXXRecord: | 
|  | 3545 | case Decl::ClassTemplateSpecialization: | 
|  | 3546 | case Decl::ClassTemplatePartialSpecialization: | 
| Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3547 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3548 | return MakeCXCursor(Def, CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3549 | return clang_getNullCursor(); | 
|  | 3550 |  | 
|  | 3551 | case Decl::Function: | 
|  | 3552 | case Decl::CXXMethod: | 
|  | 3553 | case Decl::CXXConstructor: | 
|  | 3554 | case Decl::CXXDestructor: | 
|  | 3555 | case Decl::CXXConversion: { | 
|  | 3556 | const FunctionDecl *Def = 0; | 
|  | 3557 | if (cast<FunctionDecl>(D)->getBody(Def)) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3558 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3559 | return clang_getNullCursor(); | 
|  | 3560 | } | 
|  | 3561 |  | 
|  | 3562 | case Decl::Var: { | 
| Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 3563 | // Ask the variable if it has a definition. | 
|  | 3564 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) | 
|  | 3565 | return MakeCXCursor(Def, CXXUnit); | 
|  | 3566 | return clang_getNullCursor(); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3567 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3568 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3569 | case Decl::FunctionTemplate: { | 
|  | 3570 | const FunctionDecl *Def = 0; | 
|  | 3571 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3572 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3573 | return clang_getNullCursor(); | 
|  | 3574 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3575 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3576 | case Decl::ClassTemplate: { | 
|  | 3577 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() | 
| Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3578 | ->getDefinition()) | 
| Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3579 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3580 | CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3581 | return clang_getNullCursor(); | 
|  | 3582 | } | 
|  | 3583 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3584 | case Decl::Using: | 
|  | 3585 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), | 
|  | 3586 | D->getLocation(), CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3587 |  | 
|  | 3588 | case Decl::UsingShadow: | 
|  | 3589 | return clang_getCursorDefinition( | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3590 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3591 | CXXUnit)); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3592 |  | 
|  | 3593 | case Decl::ObjCMethod: { | 
|  | 3594 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); | 
|  | 3595 | if (Method->isThisDeclarationADefinition()) | 
|  | 3596 | return C; | 
|  | 3597 |  | 
|  | 3598 | // Dig out the method definition in the associated | 
|  | 3599 | // @implementation, if we have it. | 
|  | 3600 | // FIXME: The ASTs should make finding the definition easier. | 
|  | 3601 | if (ObjCInterfaceDecl *Class | 
|  | 3602 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) | 
|  | 3603 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) | 
|  | 3604 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), | 
|  | 3605 | Method->isInstanceMethod())) | 
|  | 3606 | if (Def->isThisDeclarationADefinition()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3607 | return MakeCXCursor(Def, CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3608 |  | 
|  | 3609 | return clang_getNullCursor(); | 
|  | 3610 | } | 
|  | 3611 |  | 
|  | 3612 | case Decl::ObjCCategory: | 
|  | 3613 | if (ObjCCategoryImplDecl *Impl | 
|  | 3614 | = cast<ObjCCategoryDecl>(D)->getImplementation()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3615 | return MakeCXCursor(Impl, CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3616 | return clang_getNullCursor(); | 
|  | 3617 |  | 
|  | 3618 | case Decl::ObjCProtocol: | 
|  | 3619 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) | 
|  | 3620 | return C; | 
|  | 3621 | return clang_getNullCursor(); | 
|  | 3622 |  | 
|  | 3623 | case Decl::ObjCInterface: | 
|  | 3624 | // There are two notions of a "definition" for an Objective-C | 
|  | 3625 | // class: the interface and its implementation. When we resolved a | 
|  | 3626 | // reference to an Objective-C class, produce the @interface as | 
|  | 3627 | // the definition; when we were provided with the interface, | 
|  | 3628 | // produce the @implementation as the definition. | 
|  | 3629 | if (WasReference) { | 
|  | 3630 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) | 
|  | 3631 | return C; | 
|  | 3632 | } else if (ObjCImplementationDecl *Impl | 
|  | 3633 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3634 | return MakeCXCursor(Impl, CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3635 | return clang_getNullCursor(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3636 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3637 | case Decl::ObjCProperty: | 
|  | 3638 | // FIXME: We don't really know where to find the | 
|  | 3639 | // ObjCPropertyImplDecls that implement this property. | 
|  | 3640 | return clang_getNullCursor(); | 
|  | 3641 |  | 
|  | 3642 | case Decl::ObjCCompatibleAlias: | 
|  | 3643 | if (ObjCInterfaceDecl *Class | 
|  | 3644 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) | 
|  | 3645 | if (!Class->isForwardDecl()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3646 | return MakeCXCursor(Class, CXXUnit); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3647 |  | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3648 | return clang_getNullCursor(); | 
|  | 3649 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3650 | case Decl::ObjCForwardProtocol: | 
|  | 3651 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), | 
|  | 3652 | D->getLocation(), CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3653 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3654 | case Decl::ObjCClass: | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3655 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3656 | CXXUnit); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3657 |  | 
|  | 3658 | case Decl::Friend: | 
|  | 3659 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3660 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3661 | return clang_getNullCursor(); | 
|  | 3662 |  | 
|  | 3663 | case Decl::FriendTemplate: | 
|  | 3664 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) | 
| Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3665 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); | 
| Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3666 | return clang_getNullCursor(); | 
|  | 3667 | } | 
|  | 3668 |  | 
|  | 3669 | return clang_getNullCursor(); | 
|  | 3670 | } | 
|  | 3671 |  | 
|  | 3672 | unsigned clang_isCursorDefinition(CXCursor C) { | 
|  | 3673 | if (!clang_isDeclaration(C.kind)) | 
|  | 3674 | return 0; | 
|  | 3675 |  | 
|  | 3676 | return clang_getCursorDefinition(C) == C; | 
|  | 3677 | } | 
|  | 3678 |  | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3679 | unsigned clang_getNumOverloadedDecls(CXCursor C) { | 
| Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 3680 | if (C.kind != CXCursor_OverloadedDeclRef) | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3681 | return 0; | 
|  | 3682 |  | 
|  | 3683 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; | 
|  | 3684 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) | 
|  | 3685 | return E->getNumDecls(); | 
|  | 3686 |  | 
|  | 3687 | if (OverloadedTemplateStorage *S | 
|  | 3688 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) | 
|  | 3689 | return S->size(); | 
|  | 3690 |  | 
|  | 3691 | Decl *D = Storage.get<Decl*>(); | 
|  | 3692 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) | 
| Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 3693 | return Using->shadow_size(); | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3694 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) | 
|  | 3695 | return Classes->size(); | 
|  | 3696 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) | 
|  | 3697 | return Protocols->protocol_size(); | 
|  | 3698 |  | 
|  | 3699 | return 0; | 
|  | 3700 | } | 
|  | 3701 |  | 
|  | 3702 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { | 
| Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 3703 | if (cursor.kind != CXCursor_OverloadedDeclRef) | 
| Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3704 | return clang_getNullCursor(); | 
|  | 3705 |  | 
|  | 3706 | if (index >= clang_getNumOverloadedDecls(cursor)) | 
|  | 3707 | return clang_getNullCursor(); | 
|  | 3708 |  | 
|  | 3709 | ASTUnit *Unit = getCursorASTUnit(cursor); | 
|  | 3710 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; | 
|  | 3711 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) | 
|  | 3712 | return MakeCXCursor(E->decls_begin()[index], Unit); | 
|  | 3713 |  | 
|  | 3714 | if (OverloadedTemplateStorage *S | 
|  | 3715 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) | 
|  | 3716 | return MakeCXCursor(S->begin()[index], Unit); | 
|  | 3717 |  | 
|  | 3718 | Decl *D = Storage.get<Decl*>(); | 
|  | 3719 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { | 
|  | 3720 | // FIXME: This is, unfortunately, linear time. | 
|  | 3721 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); | 
|  | 3722 | std::advance(Pos, index); | 
|  | 3723 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), Unit); | 
|  | 3724 | } | 
|  | 3725 |  | 
|  | 3726 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) | 
|  | 3727 | return MakeCXCursor(Classes->begin()[index].getInterface(), Unit); | 
|  | 3728 |  | 
|  | 3729 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) | 
|  | 3730 | return MakeCXCursor(Protocols->protocol_begin()[index], Unit); | 
|  | 3731 |  | 
|  | 3732 | return clang_getNullCursor(); | 
|  | 3733 | } | 
|  | 3734 |  | 
| Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3735 | void clang_getDefinitionSpellingAndExtent(CXCursor C, | 
| Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 3736 | const char **startBuf, | 
|  | 3737 | const char **endBuf, | 
|  | 3738 | unsigned *startLine, | 
|  | 3739 | unsigned *startColumn, | 
|  | 3740 | unsigned *endLine, | 
| Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3741 | unsigned *endColumn) { | 
| Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3742 | assert(getCursorDecl(C) && "CXCursor has null decl"); | 
|  | 3743 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); | 
| Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 3744 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); | 
|  | 3745 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3746 |  | 
| Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 3747 | SourceManager &SM = FD->getASTContext().getSourceManager(); | 
|  | 3748 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); | 
|  | 3749 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); | 
|  | 3750 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); | 
|  | 3751 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); | 
|  | 3752 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); | 
|  | 3753 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); | 
|  | 3754 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3755 |  | 
| Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 3756 | void clang_enableStackTraces(void) { | 
|  | 3757 | llvm::sys::PrintStackTraceOnErrorSignal(); | 
|  | 3758 | } | 
|  | 3759 |  | 
| Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 3760 | void clang_executeOnThread(void (*fn)(void*), void *user_data, | 
|  | 3761 | unsigned stack_size) { | 
|  | 3762 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); | 
|  | 3763 | } | 
|  | 3764 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3765 | } // end: extern "C" | 
| Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 3766 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3767 | //===----------------------------------------------------------------------===// | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3768 | // Token-based Operations. | 
|  | 3769 | //===----------------------------------------------------------------------===// | 
|  | 3770 |  | 
|  | 3771 | /* CXToken layout: | 
|  | 3772 | *   int_data[0]: a CXTokenKind | 
|  | 3773 | *   int_data[1]: starting token location | 
|  | 3774 | *   int_data[2]: token length | 
|  | 3775 | *   int_data[3]: reserved | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3776 | *   ptr_data: for identifiers and keywords, an IdentifierInfo*. | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3777 | *   otherwise unused. | 
|  | 3778 | */ | 
|  | 3779 | extern "C" { | 
|  | 3780 |  | 
|  | 3781 | CXTokenKind clang_getTokenKind(CXToken CXTok) { | 
|  | 3782 | return static_cast<CXTokenKind>(CXTok.int_data[0]); | 
|  | 3783 | } | 
|  | 3784 |  | 
|  | 3785 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { | 
|  | 3786 | switch (clang_getTokenKind(CXTok)) { | 
|  | 3787 | case CXToken_Identifier: | 
|  | 3788 | case CXToken_Keyword: | 
|  | 3789 | // We know we have an IdentifierInfo*, so use that. | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3790 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) | 
|  | 3791 | ->getNameStart()); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3792 |  | 
|  | 3793 | case CXToken_Literal: { | 
|  | 3794 | // We have stashed the starting pointer in the ptr_data field. Use it. | 
|  | 3795 | const char *Text = static_cast<const char *>(CXTok.ptr_data); | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3796 | return createCXString(llvm::StringRef(Text, CXTok.int_data[2])); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3797 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3798 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3799 | case CXToken_Punctuation: | 
|  | 3800 | case CXToken_Comment: | 
|  | 3801 | break; | 
|  | 3802 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3803 |  | 
|  | 3804 | // We have to find the starting buffer pointer the hard way, by | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3805 | // deconstructing the source location. | 
|  | 3806 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
|  | 3807 | if (!CXXUnit) | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3808 | return createCXString(""); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3809 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3810 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); | 
|  | 3811 | std::pair<FileID, unsigned> LocInfo | 
|  | 3812 | = CXXUnit->getSourceManager().getDecomposedLoc(Loc); | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3813 | bool Invalid = false; | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3814 | llvm::StringRef Buffer | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3815 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); | 
|  | 3816 | if (Invalid) | 
| Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3817 | return createCXString(""); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3818 |  | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3819 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3820 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3821 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3822 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { | 
|  | 3823 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
|  | 3824 | if (!CXXUnit) | 
|  | 3825 | return clang_getNullLocation(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3826 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3827 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), | 
|  | 3828 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); | 
|  | 3829 | } | 
|  | 3830 |  | 
|  | 3831 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { | 
|  | 3832 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
| Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3833 | if (!CXXUnit) | 
|  | 3834 | return clang_getNullRange(); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3835 |  | 
|  | 3836 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3837 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); | 
|  | 3838 | } | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3839 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3840 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, | 
|  | 3841 | CXToken **Tokens, unsigned *NumTokens) { | 
|  | 3842 | if (Tokens) | 
|  | 3843 | *Tokens = 0; | 
|  | 3844 | if (NumTokens) | 
|  | 3845 | *NumTokens = 0; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3846 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3847 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
|  | 3848 | if (!CXXUnit || !Tokens || !NumTokens) | 
|  | 3849 | return; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3850 |  | 
| Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3851 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); | 
|  | 3852 |  | 
| Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 3853 | SourceRange R = cxloc::translateCXSourceRange(Range); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3854 | if (R.isInvalid()) | 
|  | 3855 | return; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3856 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3857 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); | 
|  | 3858 | std::pair<FileID, unsigned> BeginLocInfo | 
|  | 3859 | = SourceMgr.getDecomposedLoc(R.getBegin()); | 
|  | 3860 | std::pair<FileID, unsigned> EndLocInfo | 
|  | 3861 | = SourceMgr.getDecomposedLoc(R.getEnd()); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3862 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3863 | // Cannot tokenize across files. | 
|  | 3864 | if (BeginLocInfo.first != EndLocInfo.first) | 
|  | 3865 | return; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3866 |  | 
|  | 3867 | // Create a lexer | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3868 | bool Invalid = false; | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3869 | llvm::StringRef Buffer | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3870 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); | 
| Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 3871 | if (Invalid) | 
|  | 3872 | return; | 
| Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3873 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3874 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), | 
|  | 3875 | CXXUnit->getASTContext().getLangOptions(), | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3876 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3877 | Lex.SetCommentRetentionState(true); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3878 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3879 | // Lex tokens until we hit the end of the range. | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3880 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3881 | llvm::SmallVector<CXToken, 32> CXTokens; | 
|  | 3882 | Token Tok; | 
| David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 3883 | bool previousWasAt = false; | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3884 | do { | 
|  | 3885 | // Lex the next token | 
|  | 3886 | Lex.LexFromRawLexer(Tok); | 
|  | 3887 | if (Tok.is(tok::eof)) | 
|  | 3888 | break; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3889 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3890 | // Initialize the CXToken. | 
|  | 3891 | CXToken CXTok; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3892 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3893 | //   - Common fields | 
|  | 3894 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); | 
|  | 3895 | CXTok.int_data[2] = Tok.getLength(); | 
|  | 3896 | CXTok.int_data[3] = 0; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3897 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3898 | //   - Kind-specific fields | 
|  | 3899 | if (Tok.isLiteral()) { | 
|  | 3900 | CXTok.int_data[0] = CXToken_Literal; | 
|  | 3901 | CXTok.ptr_data = (void *)Tok.getLiteralData(); | 
|  | 3902 | } else if (Tok.is(tok::identifier)) { | 
| Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3903 | // Lookup the identifier to determine whether we have a keyword. | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3904 | std::pair<FileID, unsigned> LocInfo | 
|  | 3905 | = SourceMgr.getDecomposedLoc(Tok.getLocation()); | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3906 | bool Invalid = false; | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3907 | llvm::StringRef Buf | 
| Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3908 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); | 
|  | 3909 | if (Invalid) | 
| Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3910 | return; | 
|  | 3911 |  | 
| Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3912 | const char *StartPos = Buf.data() + LocInfo.second; | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3913 | IdentifierInfo *II | 
|  | 3914 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos); | 
| Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 3915 |  | 
| David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 3916 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { | 
| Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 3917 | CXTok.int_data[0] = CXToken_Keyword; | 
|  | 3918 | } | 
|  | 3919 | else { | 
|  | 3920 | CXTok.int_data[0] = II->getTokenID() == tok::identifier? | 
|  | 3921 | CXToken_Identifier | 
|  | 3922 | : CXToken_Keyword; | 
|  | 3923 | } | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3924 | CXTok.ptr_data = II; | 
|  | 3925 | } else if (Tok.is(tok::comment)) { | 
|  | 3926 | CXTok.int_data[0] = CXToken_Comment; | 
|  | 3927 | CXTok.ptr_data = 0; | 
|  | 3928 | } else { | 
|  | 3929 | CXTok.int_data[0] = CXToken_Punctuation; | 
|  | 3930 | CXTok.ptr_data = 0; | 
|  | 3931 | } | 
|  | 3932 | CXTokens.push_back(CXTok); | 
| David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 3933 | previousWasAt = Tok.is(tok::at); | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3934 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3935 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3936 | if (CXTokens.empty()) | 
|  | 3937 | return; | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3938 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3939 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); | 
|  | 3940 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); | 
|  | 3941 | *NumTokens = CXTokens.size(); | 
|  | 3942 | } | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3943 |  | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3944 | void clang_disposeTokens(CXTranslationUnit TU, | 
|  | 3945 | CXToken *Tokens, unsigned NumTokens) { | 
|  | 3946 | free(Tokens); | 
|  | 3947 | } | 
|  | 3948 |  | 
|  | 3949 | } // end: extern "C" | 
|  | 3950 |  | 
|  | 3951 | //===----------------------------------------------------------------------===// | 
|  | 3952 | // Token annotation APIs. | 
|  | 3953 | //===----------------------------------------------------------------------===// | 
|  | 3954 |  | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3955 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3956 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, | 
|  | 3957 | CXCursor parent, | 
|  | 3958 | CXClientData client_data); | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3959 | namespace { | 
|  | 3960 | class AnnotateTokensWorker { | 
|  | 3961 | AnnotateTokensData &Annotated; | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3962 | CXToken *Tokens; | 
|  | 3963 | CXCursor *Cursors; | 
|  | 3964 | unsigned NumTokens; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3965 | unsigned TokIdx; | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 3966 | unsigned PreprocessingTokIdx; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3967 | CursorVisitor AnnotateVis; | 
|  | 3968 | SourceManager &SrcMgr; | 
|  | 3969 |  | 
|  | 3970 | bool MoreTokens() const { return TokIdx < NumTokens; } | 
|  | 3971 | unsigned NextToken() const { return TokIdx; } | 
|  | 3972 | void AdvanceToken() { ++TokIdx; } | 
|  | 3973 | SourceLocation GetTokenLoc(unsigned tokI) { | 
|  | 3974 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); | 
|  | 3975 | } | 
|  | 3976 |  | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3977 | public: | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3978 | AnnotateTokensWorker(AnnotateTokensData &annotated, | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3979 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, | 
|  | 3980 | ASTUnit *CXXUnit, SourceRange RegionOfInterest) | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3981 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 3982 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3983 | AnnotateVis(CXXUnit, AnnotateTokensVisitor, this, | 
|  | 3984 | Decl::MaxPCHLevel, RegionOfInterest), | 
|  | 3985 | SrcMgr(CXXUnit->getSourceManager()) {} | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3986 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3987 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3988 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3989 | void AnnotateTokens(CXCursor parent); | 
| Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 3990 | void AnnotateTokens() { | 
|  | 3991 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getASTUnit())); | 
|  | 3992 | } | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3993 | }; | 
|  | 3994 | } | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3995 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3996 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { | 
|  | 3997 | // Walk the AST within the region of interest, annotating tokens | 
|  | 3998 | // along the way. | 
|  | 3999 | VisitChildren(parent); | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4000 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4001 | for (unsigned I = 0 ; I < TokIdx ; ++I) { | 
|  | 4002 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4003 | if (Pos != Annotated.end() && | 
|  | 4004 | (clang_isInvalid(Cursors[I].kind) || | 
|  | 4005 | Pos->second.kind != CXCursor_PreprocessingDirective)) | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4006 | Cursors[I] = Pos->second; | 
|  | 4007 | } | 
|  | 4008 |  | 
|  | 4009 | // Finish up annotating any tokens left. | 
|  | 4010 | if (!MoreTokens()) | 
|  | 4011 | return; | 
|  | 4012 |  | 
|  | 4013 | const CXCursor &C = clang_getNullCursor(); | 
|  | 4014 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { | 
|  | 4015 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); | 
|  | 4016 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; | 
| Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4017 | } | 
|  | 4018 | } | 
|  | 4019 |  | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4020 | enum CXChildVisitResult | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4021 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4022 | CXSourceLocation Loc = clang_getCursorLocation(cursor); | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4023 | SourceRange cursorRange = getRawCursorExtent(cursor); | 
| Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4024 | if (cursorRange.isInvalid()) | 
|  | 4025 | return CXChildVisit_Recurse; | 
|  | 4026 |  | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4027 | if (clang_isPreprocessing(cursor.kind)) { | 
|  | 4028 | // For macro instantiations, just note where the beginning of the macro | 
|  | 4029 | // instantiation occurs. | 
|  | 4030 | if (cursor.kind == CXCursor_MacroInstantiation) { | 
|  | 4031 | Annotated[Loc.int_data] = cursor; | 
|  | 4032 | return CXChildVisit_Recurse; | 
|  | 4033 | } | 
|  | 4034 |  | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4035 | // Items in the preprocessing record are kept separate from items in | 
|  | 4036 | // declarations, so we keep a separate token index. | 
|  | 4037 | unsigned SavedTokIdx = TokIdx; | 
|  | 4038 | TokIdx = PreprocessingTokIdx; | 
|  | 4039 |  | 
|  | 4040 | // Skip tokens up until we catch up to the beginning of the preprocessing | 
|  | 4041 | // entry. | 
|  | 4042 | while (MoreTokens()) { | 
|  | 4043 | const unsigned I = NextToken(); | 
|  | 4044 | SourceLocation TokLoc = GetTokenLoc(I); | 
|  | 4045 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { | 
|  | 4046 | case RangeBefore: | 
|  | 4047 | AdvanceToken(); | 
|  | 4048 | continue; | 
|  | 4049 | case RangeAfter: | 
|  | 4050 | case RangeOverlap: | 
|  | 4051 | break; | 
|  | 4052 | } | 
|  | 4053 | break; | 
|  | 4054 | } | 
|  | 4055 |  | 
|  | 4056 | // Look at all of the tokens within this range. | 
|  | 4057 | while (MoreTokens()) { | 
|  | 4058 | const unsigned I = NextToken(); | 
|  | 4059 | SourceLocation TokLoc = GetTokenLoc(I); | 
|  | 4060 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { | 
|  | 4061 | case RangeBefore: | 
|  | 4062 | assert(0 && "Infeasible"); | 
|  | 4063 | case RangeAfter: | 
|  | 4064 | break; | 
|  | 4065 | case RangeOverlap: | 
|  | 4066 | Cursors[I] = cursor; | 
|  | 4067 | AdvanceToken(); | 
|  | 4068 | continue; | 
|  | 4069 | } | 
|  | 4070 | break; | 
|  | 4071 | } | 
|  | 4072 |  | 
|  | 4073 | // Save the preprocessing token index; restore the non-preprocessing | 
|  | 4074 | // token index. | 
|  | 4075 | PreprocessingTokIdx = TokIdx; | 
|  | 4076 | TokIdx = SavedTokIdx; | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4077 | return CXChildVisit_Recurse; | 
|  | 4078 | } | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4079 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4080 | if (cursorRange.isInvalid()) | 
|  | 4081 | return CXChildVisit_Continue; | 
| Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4082 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4083 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); | 
|  | 4084 |  | 
| Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4085 | // Adjust the annotated range based specific declarations. | 
|  | 4086 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); | 
|  | 4087 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4088 | Decl *D = cxcursor::getCursorDecl(cursor); | 
|  | 4089 | // Don't visit synthesized ObjC methods, since they have no syntatic | 
|  | 4090 | // representation in the source. | 
|  | 4091 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { | 
|  | 4092 | if (MD->isSynthesized()) | 
|  | 4093 | return CXChildVisit_Continue; | 
|  | 4094 | } | 
|  | 4095 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { | 
| Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4096 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) { | 
|  | 4097 | TypeLoc TL = TI->getTypeLoc(); | 
| Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4098 | SourceLocation TLoc = TL.getSourceRange().getBegin(); | 
| Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4099 | if (TLoc.isValid() && L.isValid() && | 
| Ted Kremenek | 6bfd533 | 2010-05-13 15:38:38 +0000 | [diff] [blame] | 4100 | SrcMgr.isBeforeInTranslationUnit(TLoc, L)) | 
| Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4101 | cursorRange.setBegin(TLoc); | 
| Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4102 | } | 
|  | 4103 | } | 
|  | 4104 | } | 
| Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4105 |  | 
| Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4106 | // If the location of the cursor occurs within a macro instantiation, record | 
|  | 4107 | // the spelling location of the cursor in our annotation map.  We can then | 
|  | 4108 | // paper over the token labelings during a post-processing step to try and | 
|  | 4109 | // get cursor mappings for tokens that are the *arguments* of a macro | 
|  | 4110 | // instantiation. | 
|  | 4111 | if (L.isMacroID()) { | 
|  | 4112 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); | 
|  | 4113 | // Only invalidate the old annotation if it isn't part of a preprocessing | 
|  | 4114 | // directive.  Here we assume that the default construction of CXCursor | 
|  | 4115 | // results in CXCursor.kind being an initialized value (i.e., 0).  If | 
|  | 4116 | // this isn't the case, we can fix by doing lookup + insertion. | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4117 |  | 
| Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4118 | CXCursor &oldC = Annotated[rawEncoding]; | 
|  | 4119 | if (!clang_isPreprocessing(oldC.kind)) | 
|  | 4120 | oldC = cursor; | 
|  | 4121 | } | 
|  | 4122 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4123 | const enum CXCursorKind K = clang_getCursorKind(parent); | 
|  | 4124 | const CXCursor updateC = | 
| Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4125 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) | 
|  | 4126 | ? clang_getNullCursor() : parent; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4127 |  | 
|  | 4128 | while (MoreTokens()) { | 
|  | 4129 | const unsigned I = NextToken(); | 
|  | 4130 | SourceLocation TokLoc = GetTokenLoc(I); | 
|  | 4131 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { | 
|  | 4132 | case RangeBefore: | 
|  | 4133 | Cursors[I] = updateC; | 
|  | 4134 | AdvanceToken(); | 
|  | 4135 | continue; | 
|  | 4136 | case RangeAfter: | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4137 | case RangeOverlap: | 
|  | 4138 | break; | 
|  | 4139 | } | 
|  | 4140 | break; | 
|  | 4141 | } | 
|  | 4142 |  | 
|  | 4143 | // Visit children to get their cursor information. | 
|  | 4144 | const unsigned BeforeChildren = NextToken(); | 
|  | 4145 | VisitChildren(cursor); | 
|  | 4146 | const unsigned AfterChildren = NextToken(); | 
|  | 4147 |  | 
|  | 4148 | // Adjust 'Last' to the last token within the extent of the cursor. | 
|  | 4149 | while (MoreTokens()) { | 
|  | 4150 | const unsigned I = NextToken(); | 
|  | 4151 | SourceLocation TokLoc = GetTokenLoc(I); | 
|  | 4152 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { | 
|  | 4153 | case RangeBefore: | 
|  | 4154 | assert(0 && "Infeasible"); | 
|  | 4155 | case RangeAfter: | 
|  | 4156 | break; | 
|  | 4157 | case RangeOverlap: | 
|  | 4158 | Cursors[I] = updateC; | 
|  | 4159 | AdvanceToken(); | 
|  | 4160 | continue; | 
|  | 4161 | } | 
|  | 4162 | break; | 
|  | 4163 | } | 
|  | 4164 | const unsigned Last = NextToken(); | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4165 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4166 | // Scan the tokens that are at the beginning of the cursor, but are not | 
|  | 4167 | // capture by the child cursors. | 
|  | 4168 |  | 
|  | 4169 | // For AST elements within macros, rely on a post-annotate pass to | 
|  | 4170 | // to correctly annotate the tokens with cursors.  Otherwise we can | 
|  | 4171 | // get confusing results of having tokens that map to cursors that really | 
|  | 4172 | // are expanded by an instantiation. | 
|  | 4173 | if (L.isMacroID()) | 
|  | 4174 | cursor = clang_getNullCursor(); | 
|  | 4175 |  | 
|  | 4176 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { | 
|  | 4177 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) | 
|  | 4178 | break; | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4179 |  | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4180 | Cursors[I] = cursor; | 
|  | 4181 | } | 
|  | 4182 | // Scan the tokens that are at the end of the cursor, but are not captured | 
|  | 4183 | // but the child cursors. | 
|  | 4184 | for (unsigned I = AfterChildren; I != Last; ++I) | 
|  | 4185 | Cursors[I] = cursor; | 
|  | 4186 |  | 
|  | 4187 | TokIdx = Last; | 
|  | 4188 | return CXChildVisit_Continue; | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4189 | } | 
|  | 4190 |  | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4191 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, | 
|  | 4192 | CXCursor parent, | 
|  | 4193 | CXClientData client_data) { | 
|  | 4194 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); | 
|  | 4195 | } | 
|  | 4196 |  | 
| Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4197 | // This gets run a separate thread to avoid stack blowout. | 
|  | 4198 | static void runAnnotateTokensWorker(void *UserData) { | 
|  | 4199 | ((AnnotateTokensWorker*)UserData)->AnnotateTokens(); | 
|  | 4200 | } | 
|  | 4201 |  | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4202 | extern "C" { | 
|  | 4203 |  | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4204 | void clang_annotateTokens(CXTranslationUnit TU, | 
|  | 4205 | CXToken *Tokens, unsigned NumTokens, | 
|  | 4206 | CXCursor *Cursors) { | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4207 |  | 
|  | 4208 | if (NumTokens == 0 || !Tokens || !Cursors) | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4209 | return; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4210 |  | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4211 | // Any token we don't specifically annotate will have a NULL cursor. | 
|  | 4212 | CXCursor C = clang_getNullCursor(); | 
|  | 4213 | for (unsigned I = 0; I != NumTokens; ++I) | 
|  | 4214 | Cursors[I] = C; | 
|  | 4215 |  | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4216 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); | 
| Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4217 | if (!CXXUnit) | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4218 | return; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4219 |  | 
| Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 4220 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4221 |  | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4222 | // Determine the region of interest, which contains all of the tokens. | 
| Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4223 | SourceRange RegionOfInterest; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4224 | RegionOfInterest.setBegin(cxloc::translateSourceLocation( | 
|  | 4225 | clang_getTokenLocation(TU, Tokens[0]))); | 
| Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4226 | RegionOfInterest.setEnd(cxloc::translateSourceLocation( | 
|  | 4227 | clang_getTokenLocation(TU, | 
|  | 4228 | Tokens[NumTokens - 1]))); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4229 |  | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4230 | // A mapping from the source locations found when re-lexing or traversing the | 
|  | 4231 | // region of interest to the corresponding cursors. | 
|  | 4232 | AnnotateTokensData Annotated; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4233 |  | 
|  | 4234 | // Relex the tokens within the source range to look for preprocessing | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4235 | // directives. | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4236 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); | 
|  | 4237 | std::pair<FileID, unsigned> BeginLocInfo | 
|  | 4238 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); | 
|  | 4239 | std::pair<FileID, unsigned> EndLocInfo | 
|  | 4240 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4241 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4242 | llvm::StringRef Buffer; | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4243 | bool Invalid = false; | 
|  | 4244 | if (BeginLocInfo.first == EndLocInfo.first && | 
|  | 4245 | ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && | 
|  | 4246 | !Invalid) { | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4247 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), | 
|  | 4248 | CXXUnit->getASTContext().getLangOptions(), | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4249 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, | 
| Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 4250 | Buffer.end()); | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4251 | Lex.SetCommentRetentionState(true); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4252 |  | 
|  | 4253 | // Lex tokens in raw mode until we hit the end of the range, to avoid | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4254 | // entering #includes or expanding macros. | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 4255 | while (true) { | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4256 | Token Tok; | 
|  | 4257 | Lex.LexFromRawLexer(Tok); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4258 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4259 | reprocess: | 
|  | 4260 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { | 
|  | 4261 | // We have found a preprocessing directive. Gobble it up so that we | 
| Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4262 | // don't see it while preprocessing these tokens later, but keep track | 
|  | 4263 | // of all of the token locations inside this preprocessing directive so | 
|  | 4264 | // that we can annotate them appropriately. | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4265 | // | 
|  | 4266 | // FIXME: Some simple tests here could identify macro definitions and | 
|  | 4267 | // #undefs, to provide specific cursor kinds for those. | 
|  | 4268 | std::vector<SourceLocation> Locations; | 
|  | 4269 | do { | 
|  | 4270 | Locations.push_back(Tok.getLocation()); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4271 | Lex.LexFromRawLexer(Tok); | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4272 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4273 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4274 | using namespace cxcursor; | 
|  | 4275 | CXCursor Cursor | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4276 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), | 
|  | 4277 | Locations.back()), | 
| Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4278 | CXXUnit); | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4279 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { | 
|  | 4280 | Annotated[Locations[I].getRawEncoding()] = Cursor; | 
|  | 4281 | } | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4282 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4283 | if (Tok.isAtStartOfLine()) | 
|  | 4284 | goto reprocess; | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4285 |  | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4286 | continue; | 
|  | 4287 | } | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4288 |  | 
| Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 4289 | if (Tok.is(tok::eof)) | 
| Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 4290 | break; | 
|  | 4291 | } | 
| Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 4292 | } | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4293 |  | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4294 | // Annotate all of the source locations in the region of interest that map to | 
| Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4295 | // a specific cursor. | 
|  | 4296 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, | 
|  | 4297 | CXXUnit, RegionOfInterest); | 
| Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4298 |  | 
|  | 4299 | // Run the worker within a CrashRecoveryContext. | 
|  | 4300 | llvm::CrashRecoveryContext CRC; | 
|  | 4301 | if (!RunSafely(CRC, runAnnotateTokensWorker, &W)) { | 
|  | 4302 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); | 
|  | 4303 | } | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4304 | } | 
| Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4305 | } // end: extern "C" | 
|  | 4306 |  | 
|  | 4307 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 4308 | // Operations for querying linkage of a cursor. | 
|  | 4309 | //===----------------------------------------------------------------------===// | 
|  | 4310 |  | 
|  | 4311 | extern "C" { | 
|  | 4312 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { | 
| Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 4313 | if (!clang_isDeclaration(cursor.kind)) | 
|  | 4314 | return CXLinkage_Invalid; | 
|  | 4315 |  | 
| Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 4316 | Decl *D = cxcursor::getCursorDecl(cursor); | 
|  | 4317 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) | 
|  | 4318 | switch (ND->getLinkage()) { | 
|  | 4319 | case NoLinkage: return CXLinkage_NoLinkage; | 
|  | 4320 | case InternalLinkage: return CXLinkage_Internal; | 
|  | 4321 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; | 
|  | 4322 | case ExternalLinkage: return CXLinkage_External; | 
|  | 4323 | }; | 
|  | 4324 |  | 
|  | 4325 | return CXLinkage_Invalid; | 
|  | 4326 | } | 
|  | 4327 | } // end: extern "C" | 
|  | 4328 |  | 
|  | 4329 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4330 | // Operations for querying language of a cursor. | 
|  | 4331 | //===----------------------------------------------------------------------===// | 
|  | 4332 |  | 
|  | 4333 | static CXLanguageKind getDeclLanguage(const Decl *D) { | 
|  | 4334 | switch (D->getKind()) { | 
|  | 4335 | default: | 
|  | 4336 | break; | 
|  | 4337 | case Decl::ImplicitParam: | 
|  | 4338 | case Decl::ObjCAtDefsField: | 
|  | 4339 | case Decl::ObjCCategory: | 
|  | 4340 | case Decl::ObjCCategoryImpl: | 
|  | 4341 | case Decl::ObjCClass: | 
|  | 4342 | case Decl::ObjCCompatibleAlias: | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4343 | case Decl::ObjCForwardProtocol: | 
|  | 4344 | case Decl::ObjCImplementation: | 
|  | 4345 | case Decl::ObjCInterface: | 
|  | 4346 | case Decl::ObjCIvar: | 
|  | 4347 | case Decl::ObjCMethod: | 
|  | 4348 | case Decl::ObjCProperty: | 
|  | 4349 | case Decl::ObjCPropertyImpl: | 
|  | 4350 | case Decl::ObjCProtocol: | 
|  | 4351 | return CXLanguage_ObjC; | 
|  | 4352 | case Decl::CXXConstructor: | 
|  | 4353 | case Decl::CXXConversion: | 
|  | 4354 | case Decl::CXXDestructor: | 
|  | 4355 | case Decl::CXXMethod: | 
|  | 4356 | case Decl::CXXRecord: | 
|  | 4357 | case Decl::ClassTemplate: | 
|  | 4358 | case Decl::ClassTemplatePartialSpecialization: | 
|  | 4359 | case Decl::ClassTemplateSpecialization: | 
|  | 4360 | case Decl::Friend: | 
|  | 4361 | case Decl::FriendTemplate: | 
|  | 4362 | case Decl::FunctionTemplate: | 
|  | 4363 | case Decl::LinkageSpec: | 
|  | 4364 | case Decl::Namespace: | 
|  | 4365 | case Decl::NamespaceAlias: | 
|  | 4366 | case Decl::NonTypeTemplateParm: | 
|  | 4367 | case Decl::StaticAssert: | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4368 | case Decl::TemplateTemplateParm: | 
|  | 4369 | case Decl::TemplateTypeParm: | 
|  | 4370 | case Decl::UnresolvedUsingTypename: | 
|  | 4371 | case Decl::UnresolvedUsingValue: | 
|  | 4372 | case Decl::Using: | 
|  | 4373 | case Decl::UsingDirective: | 
|  | 4374 | case Decl::UsingShadow: | 
|  | 4375 | return CXLanguage_CPlusPlus; | 
|  | 4376 | } | 
|  | 4377 |  | 
|  | 4378 | return CXLanguage_C; | 
|  | 4379 | } | 
|  | 4380 |  | 
|  | 4381 | extern "C" { | 
| Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 4382 |  | 
|  | 4383 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { | 
|  | 4384 | if (clang_isDeclaration(cursor.kind)) | 
|  | 4385 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { | 
|  | 4386 | if (D->hasAttr<UnavailableAttr>() || | 
|  | 4387 | (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())) | 
|  | 4388 | return CXAvailability_Available; | 
|  | 4389 |  | 
|  | 4390 | if (D->hasAttr<DeprecatedAttr>()) | 
|  | 4391 | return CXAvailability_Deprecated; | 
|  | 4392 | } | 
|  | 4393 |  | 
|  | 4394 | return CXAvailability_Available; | 
|  | 4395 | } | 
|  | 4396 |  | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4397 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { | 
|  | 4398 | if (clang_isDeclaration(cursor.kind)) | 
|  | 4399 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); | 
|  | 4400 |  | 
|  | 4401 | return CXLanguage_Invalid; | 
|  | 4402 | } | 
| Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 4403 |  | 
|  | 4404 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { | 
|  | 4405 | if (clang_isDeclaration(cursor.kind)) { | 
|  | 4406 | if (Decl *D = getCursorDecl(cursor)) { | 
|  | 4407 | DeclContext *DC = D->getDeclContext(); | 
|  | 4408 | return MakeCXCursor(cast<Decl>(DC), getCursorASTUnit(cursor)); | 
|  | 4409 | } | 
|  | 4410 | } | 
|  | 4411 |  | 
|  | 4412 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { | 
|  | 4413 | if (Decl *D = getCursorDecl(cursor)) | 
|  | 4414 | return MakeCXCursor(D, getCursorASTUnit(cursor)); | 
|  | 4415 | } | 
|  | 4416 |  | 
|  | 4417 | return clang_getNullCursor(); | 
|  | 4418 | } | 
|  | 4419 |  | 
|  | 4420 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { | 
|  | 4421 | if (clang_isDeclaration(cursor.kind)) { | 
|  | 4422 | if (Decl *D = getCursorDecl(cursor)) { | 
|  | 4423 | DeclContext *DC = D->getLexicalDeclContext(); | 
|  | 4424 | return MakeCXCursor(cast<Decl>(DC), getCursorASTUnit(cursor)); | 
|  | 4425 | } | 
|  | 4426 | } | 
|  | 4427 |  | 
|  | 4428 | // FIXME: Note that we can't easily compute the lexical context of a | 
|  | 4429 | // statement or expression, so we return nothing. | 
|  | 4430 | return clang_getNullCursor(); | 
|  | 4431 | } | 
|  | 4432 |  | 
| Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 4433 | static void CollectOverriddenMethods(DeclContext *Ctx, | 
|  | 4434 | ObjCMethodDecl *Method, | 
|  | 4435 | llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) { | 
|  | 4436 | if (!Ctx) | 
|  | 4437 | return; | 
|  | 4438 |  | 
|  | 4439 | // If we have a class or category implementation, jump straight to the | 
|  | 4440 | // interface. | 
|  | 4441 | if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) | 
|  | 4442 | return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods); | 
|  | 4443 |  | 
|  | 4444 | ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); | 
|  | 4445 | if (!Container) | 
|  | 4446 | return; | 
|  | 4447 |  | 
|  | 4448 | // Check whether we have a matching method at this level. | 
|  | 4449 | if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), | 
|  | 4450 | Method->isInstanceMethod())) | 
|  | 4451 | if (Method != Overridden) { | 
|  | 4452 | // We found an override at this level; there is no need to look | 
|  | 4453 | // into other protocols or categories. | 
|  | 4454 | Methods.push_back(Overridden); | 
|  | 4455 | return; | 
|  | 4456 | } | 
|  | 4457 |  | 
|  | 4458 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | 
|  | 4459 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), | 
|  | 4460 | PEnd = Protocol->protocol_end(); | 
|  | 4461 | P != PEnd; ++P) | 
|  | 4462 | CollectOverriddenMethods(*P, Method, Methods); | 
|  | 4463 | } | 
|  | 4464 |  | 
|  | 4465 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { | 
|  | 4466 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), | 
|  | 4467 | PEnd = Category->protocol_end(); | 
|  | 4468 | P != PEnd; ++P) | 
|  | 4469 | CollectOverriddenMethods(*P, Method, Methods); | 
|  | 4470 | } | 
|  | 4471 |  | 
|  | 4472 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { | 
|  | 4473 | for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), | 
|  | 4474 | PEnd = Interface->protocol_end(); | 
|  | 4475 | P != PEnd; ++P) | 
|  | 4476 | CollectOverriddenMethods(*P, Method, Methods); | 
|  | 4477 |  | 
|  | 4478 | for (ObjCCategoryDecl *Category = Interface->getCategoryList(); | 
|  | 4479 | Category; Category = Category->getNextClassCategory()) | 
|  | 4480 | CollectOverriddenMethods(Category, Method, Methods); | 
|  | 4481 |  | 
|  | 4482 | // We only look into the superclass if we haven't found anything yet. | 
|  | 4483 | if (Methods.empty()) | 
|  | 4484 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) | 
|  | 4485 | return CollectOverriddenMethods(Super, Method, Methods); | 
|  | 4486 | } | 
|  | 4487 | } | 
|  | 4488 |  | 
|  | 4489 | void clang_getOverriddenCursors(CXCursor cursor, | 
|  | 4490 | CXCursor **overridden, | 
|  | 4491 | unsigned *num_overridden) { | 
|  | 4492 | if (overridden) | 
|  | 4493 | *overridden = 0; | 
|  | 4494 | if (num_overridden) | 
|  | 4495 | *num_overridden = 0; | 
|  | 4496 | if (!overridden || !num_overridden) | 
|  | 4497 | return; | 
|  | 4498 |  | 
|  | 4499 | if (!clang_isDeclaration(cursor.kind)) | 
|  | 4500 | return; | 
|  | 4501 |  | 
|  | 4502 | Decl *D = getCursorDecl(cursor); | 
|  | 4503 | if (!D) | 
|  | 4504 | return; | 
|  | 4505 |  | 
|  | 4506 | // Handle C++ member functions. | 
|  | 4507 | ASTUnit *CXXUnit = getCursorASTUnit(cursor); | 
|  | 4508 | if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { | 
|  | 4509 | *num_overridden = CXXMethod->size_overridden_methods(); | 
|  | 4510 | if (!*num_overridden) | 
|  | 4511 | return; | 
|  | 4512 |  | 
|  | 4513 | *overridden = new CXCursor [*num_overridden]; | 
|  | 4514 | unsigned I = 0; | 
|  | 4515 | for (CXXMethodDecl::method_iterator | 
|  | 4516 | M = CXXMethod->begin_overridden_methods(), | 
|  | 4517 | MEnd = CXXMethod->end_overridden_methods(); | 
|  | 4518 | M != MEnd; (void)++M, ++I) | 
|  | 4519 | (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), CXXUnit); | 
|  | 4520 | return; | 
|  | 4521 | } | 
|  | 4522 |  | 
|  | 4523 | ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); | 
|  | 4524 | if (!Method) | 
|  | 4525 | return; | 
|  | 4526 |  | 
|  | 4527 | // Handle Objective-C methods. | 
|  | 4528 | llvm::SmallVector<ObjCMethodDecl *, 4> Methods; | 
|  | 4529 | CollectOverriddenMethods(Method->getDeclContext(), Method, Methods); | 
|  | 4530 |  | 
|  | 4531 | if (Methods.empty()) | 
|  | 4532 | return; | 
|  | 4533 |  | 
|  | 4534 | *num_overridden = Methods.size(); | 
|  | 4535 | *overridden = new CXCursor [Methods.size()]; | 
|  | 4536 | for (unsigned I = 0, N = Methods.size(); I != N; ++I) | 
|  | 4537 | (*overridden)[I] = MakeCXCursor(Methods[I], CXXUnit); | 
|  | 4538 | } | 
|  | 4539 |  | 
|  | 4540 | void clang_disposeOverriddenCursors(CXCursor *overridden) { | 
|  | 4541 | delete [] overridden; | 
|  | 4542 | } | 
|  | 4543 |  | 
| Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 4544 | CXFile clang_getIncludedFile(CXCursor cursor) { | 
|  | 4545 | if (cursor.kind != CXCursor_InclusionDirective) | 
|  | 4546 | return 0; | 
|  | 4547 |  | 
|  | 4548 | InclusionDirective *ID = getCursorInclusionDirective(cursor); | 
|  | 4549 | return (void *)ID->getFile(); | 
|  | 4550 | } | 
|  | 4551 |  | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4552 | } // end: extern "C" | 
|  | 4553 |  | 
| Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 4554 |  | 
|  | 4555 | //===----------------------------------------------------------------------===// | 
|  | 4556 | // C++ AST instrospection. | 
|  | 4557 | //===----------------------------------------------------------------------===// | 
|  | 4558 |  | 
|  | 4559 | extern "C" { | 
|  | 4560 | unsigned clang_CXXMethod_isStatic(CXCursor C) { | 
|  | 4561 | if (!clang_isDeclaration(C.kind)) | 
|  | 4562 | return 0; | 
| Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 4563 |  | 
|  | 4564 | CXXMethodDecl *Method = 0; | 
|  | 4565 | Decl *D = cxcursor::getCursorDecl(C); | 
|  | 4566 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) | 
|  | 4567 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); | 
|  | 4568 | else | 
|  | 4569 | Method = dyn_cast_or_null<CXXMethodDecl>(D); | 
|  | 4570 | return (Method && Method->isStatic()) ? 1 : 0; | 
| Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 4571 | } | 
| Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 4572 |  | 
| Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 4573 | } // end: extern "C" | 
|  | 4574 |  | 
| Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 4575 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 4576 | // Attribute introspection. | 
|  | 4577 | //===----------------------------------------------------------------------===// | 
|  | 4578 |  | 
|  | 4579 | extern "C" { | 
|  | 4580 | CXType clang_getIBOutletCollectionType(CXCursor C) { | 
|  | 4581 | if (C.kind != CXCursor_IBOutletCollectionAttr) | 
|  | 4582 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorASTUnit(C)); | 
|  | 4583 |  | 
|  | 4584 | IBOutletCollectionAttr *A = | 
|  | 4585 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); | 
|  | 4586 |  | 
|  | 4587 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorASTUnit(C)); | 
|  | 4588 | } | 
|  | 4589 | } // end: extern "C" | 
|  | 4590 |  | 
|  | 4591 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4592 | // CXString Operations. | 
|  | 4593 | //===----------------------------------------------------------------------===// | 
|  | 4594 |  | 
|  | 4595 | extern "C" { | 
|  | 4596 | const char *clang_getCString(CXString string) { | 
|  | 4597 | return string.Spelling; | 
|  | 4598 | } | 
|  | 4599 |  | 
|  | 4600 | void clang_disposeString(CXString string) { | 
|  | 4601 | if (string.MustFreeString && string.Spelling) | 
|  | 4602 | free((void*)string.Spelling); | 
|  | 4603 | } | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 4604 |  | 
| Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4605 | } // end: extern "C" | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 4606 |  | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4607 | namespace clang { namespace cxstring { | 
|  | 4608 | CXString createCXString(const char *String, bool DupString){ | 
|  | 4609 | CXString Str; | 
|  | 4610 | if (DupString) { | 
|  | 4611 | Str.Spelling = strdup(String); | 
|  | 4612 | Str.MustFreeString = 1; | 
|  | 4613 | } else { | 
|  | 4614 | Str.Spelling = String; | 
|  | 4615 | Str.MustFreeString = 0; | 
|  | 4616 | } | 
|  | 4617 | return Str; | 
|  | 4618 | } | 
|  | 4619 |  | 
|  | 4620 | CXString createCXString(llvm::StringRef String, bool DupString) { | 
|  | 4621 | CXString Result; | 
|  | 4622 | if (DupString || (!String.empty() && String.data()[String.size()] != 0)) { | 
|  | 4623 | char *Spelling = (char *)malloc(String.size() + 1); | 
|  | 4624 | memmove(Spelling, String.data(), String.size()); | 
|  | 4625 | Spelling[String.size()] = 0; | 
|  | 4626 | Result.Spelling = Spelling; | 
|  | 4627 | Result.MustFreeString = 1; | 
|  | 4628 | } else { | 
|  | 4629 | Result.Spelling = String.data(); | 
|  | 4630 | Result.MustFreeString = 0; | 
|  | 4631 | } | 
|  | 4632 | return Result; | 
|  | 4633 | } | 
|  | 4634 | }} | 
|  | 4635 |  | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 4636 | //===----------------------------------------------------------------------===// | 
|  | 4637 | // Misc. utility functions. | 
|  | 4638 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4639 |  | 
| Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 4640 | /// Default to using an 8 MB stack size on "safety" threads. | 
|  | 4641 | static unsigned SafetyStackThreadSize = 8 << 20; | 
| Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 4642 |  | 
|  | 4643 | namespace clang { | 
|  | 4644 |  | 
|  | 4645 | bool RunSafely(llvm::CrashRecoveryContext &CRC, | 
|  | 4646 | void (*Fn)(void*), void *UserData) { | 
|  | 4647 | if (unsigned Size = GetSafetyThreadStackSize()) | 
|  | 4648 | return CRC.RunSafelyOnThread(Fn, UserData, Size); | 
|  | 4649 | return CRC.RunSafely(Fn, UserData); | 
|  | 4650 | } | 
|  | 4651 |  | 
|  | 4652 | unsigned GetSafetyThreadStackSize() { | 
|  | 4653 | return SafetyStackThreadSize; | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | void SetSafetyThreadStackSize(unsigned Value) { | 
|  | 4657 | SafetyStackThreadSize = Value; | 
|  | 4658 | } | 
|  | 4659 |  | 
|  | 4660 | } | 
|  | 4661 |  | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 4662 | extern "C" { | 
|  | 4663 |  | 
| Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 4664 | CXString clang_getClangVersion() { | 
| Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4665 | return createCXString(getClangFullVersion()); | 
| Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 4666 | } | 
|  | 4667 |  | 
|  | 4668 | } // end: extern "C" |