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