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