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