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