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" |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 33 | #include "clang/Lex/HeaderSearch.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 34 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 35 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Optional.h" |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 39 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 41 | #include "llvm/Support/PrettyStackTrace.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 42 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Mutex.h" |
| 46 | #include "llvm/Support/Program.h" |
| 47 | #include "llvm/Support/Signals.h" |
| 48 | #include "llvm/Support/Threading.h" |
Ted Kremenek | 37f1ea0 | 2010-11-15 23:11:54 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 50 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 51 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 52 | using namespace clang::cxcursor; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 53 | using namespace clang::cxstring; |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 54 | using namespace clang::cxtu; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 55 | |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 56 | CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 57 | if (!TU) |
| 58 | return 0; |
| 59 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 60 | D->TUData = TU; |
| 61 | D->StringPool = createCXStringPool(); |
| 62 | return D; |
| 63 | } |
| 64 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 65 | cxtu::CXTUOwner::~CXTUOwner() { |
| 66 | if (TU) |
| 67 | clang_disposeTranslationUnit(TU); |
| 68 | } |
| 69 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 70 | /// \brief The result of comparing two source ranges. |
| 71 | enum RangeComparisonResult { |
| 72 | /// \brief Either the ranges overlap or one of the ranges is invalid. |
| 73 | RangeOverlap, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 75 | /// \brief The first range ends before the second range starts. |
| 76 | RangeBefore, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 78 | /// \brief The first range starts after the second range ends. |
| 79 | RangeAfter |
| 80 | }; |
| 81 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 82 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 83 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 84 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 85 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 86 | SourceRange R2) { |
| 87 | assert(R1.isValid() && "First range is invalid?"); |
| 88 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 89 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 90 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 91 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 92 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 93 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 94 | return RangeAfter; |
| 95 | return RangeOverlap; |
| 96 | } |
| 97 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 98 | /// \brief Determine if a source location falls within, before, or after a |
| 99 | /// a given source range. |
| 100 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 101 | SourceLocation L, SourceRange R) { |
| 102 | assert(R.isValid() && "First range is invalid?"); |
| 103 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 104 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 105 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 106 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 107 | return RangeBefore; |
| 108 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 109 | return RangeAfter; |
| 110 | return RangeOverlap; |
| 111 | } |
| 112 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 113 | /// \brief Translate a Clang source range into a CIndex source range. |
| 114 | /// |
| 115 | /// Clang internally represents ranges where the end location points to the |
| 116 | /// start of the token at the end. However, for external clients it is more |
| 117 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 118 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 119 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 120 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 121 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 122 | // 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] | 123 | // location accordingly. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 124 | SourceLocation EndLoc = R.getEnd(); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 125 | if (EndLoc.isValid() && EndLoc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 126 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 127 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 128 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 129 | EndLoc = EndLoc.getLocWithOffset(Length); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 133 | R.getBegin().getRawEncoding(), |
| 134 | EndLoc.getRawEncoding() }; |
| 135 | return Result; |
| 136 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 139 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 142 | namespace { |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 143 | |
| 144 | class VisitorJob { |
| 145 | public: |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 146 | enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind, |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 147 | TypeLocVisitKind, OverloadExprPartsKind, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 148 | DeclRefExprPartsKind, LabelRefVisitKind, |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 149 | ExplicitTemplateArgsVisitKind, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 150 | NestedNameSpecifierLocVisitKind, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 151 | DeclarationNameInfoVisitKind, |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 152 | MemberRefVisitKind, SizeOfPackExprPartsKind }; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 153 | protected: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 154 | void *data[3]; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 155 | CXCursor parent; |
| 156 | Kind K; |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 157 | VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0) |
| 158 | : parent(C), K(k) { |
| 159 | data[0] = d1; |
| 160 | data[1] = d2; |
| 161 | data[2] = d3; |
| 162 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 163 | public: |
| 164 | Kind getKind() const { return K; } |
| 165 | const CXCursor &getParent() const { return parent; } |
| 166 | static bool classof(VisitorJob *VJ) { return true; } |
| 167 | }; |
| 168 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 169 | typedef SmallVector<VisitorJob, 10> VisitorWorkList; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 171 | // Cursor visitor. |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 172 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 173 | public TypeLocVisitor<CursorVisitor, bool> |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 174 | { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 175 | /// \brief The translation unit we are traversing. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 176 | CXTranslationUnit TU; |
| 177 | ASTUnit *AU; |
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 parent cursor whose children we are traversing. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 180 | CXCursor Parent; |
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 declaration that serves at the parent of any statement or |
| 183 | /// expression nodes. |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 184 | Decl *StmtParent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 186 | /// \brief The visitor function. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 187 | CXCursorVisitor Visitor; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 189 | /// \brief The opaque client data, to be passed along to the visitor. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 190 | CXClientData ClientData; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 192 | /// \brief Whether we should visit the preprocessing record entries last, |
| 193 | /// after visiting other declarations. |
| 194 | bool VisitPreprocessorLast; |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 195 | |
| 196 | /// \brief Whether we should visit the preprocessing record entries that are |
| 197 | /// #included inside the \arg RegionOfInterest. |
| 198 | bool VisitIncludedPreprocessingEntries; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 200 | /// \brief When valid, a source range to which the cursor should restrict |
| 201 | /// its search. |
| 202 | SourceRange RegionOfInterest; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 204 | // FIXME: Eventually remove. This part of a hack to support proper |
| 205 | // iteration over all Decls contained lexically within an ObjC container. |
| 206 | DeclContext::decl_iterator *DI_current; |
| 207 | DeclContext::decl_iterator DE_current; |
| 208 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 209 | // Cache of pre-allocated worklists for data-recursion walk of Stmts. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 210 | SmallVector<VisitorWorkList*, 5> WorkListFreeList; |
| 211 | SmallVector<VisitorWorkList*, 5> WorkListCache; |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 213 | using DeclVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 214 | using TypeLocVisitor<CursorVisitor, bool>::Visit; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 215 | |
| 216 | /// \brief Determine whether this particular source range comes before, comes |
| 217 | /// after, or overlaps the region of interest. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 218 | /// |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 219 | /// \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] | 220 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); |
| 221 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 222 | class SetParentRAII { |
| 223 | CXCursor &Parent; |
| 224 | Decl *&StmtParent; |
| 225 | CXCursor OldParent; |
| 226 | |
| 227 | public: |
| 228 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 229 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
| 230 | { |
| 231 | Parent = NewParent; |
| 232 | if (clang_isDeclaration(Parent.kind)) |
| 233 | StmtParent = getCursorDecl(Parent); |
| 234 | } |
| 235 | |
| 236 | ~SetParentRAII() { |
| 237 | Parent = OldParent; |
| 238 | if (clang_isDeclaration(Parent.kind)) |
| 239 | StmtParent = getCursorDecl(Parent); |
| 240 | } |
| 241 | }; |
| 242 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 243 | public: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 244 | CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor, |
| 245 | CXClientData ClientData, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 246 | bool VisitPreprocessorLast, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 247 | bool VisitIncludedPreprocessingEntries = false, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 248 | SourceRange RegionOfInterest = SourceRange()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 249 | : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)), |
| 250 | Visitor(Visitor), ClientData(ClientData), |
Douglas Gregor | 08e0bc1 | 2011-09-10 00:09:20 +0000 | [diff] [blame] | 251 | VisitPreprocessorLast(VisitPreprocessorLast), |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 252 | VisitIncludedPreprocessingEntries(VisitIncludedPreprocessingEntries), |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 253 | RegionOfInterest(RegionOfInterest), DI_current(0) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 254 | { |
| 255 | Parent.kind = CXCursor_NoDeclFound; |
| 256 | Parent.data[0] = 0; |
| 257 | Parent.data[1] = 0; |
| 258 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 259 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 260 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 262 | ~CursorVisitor() { |
| 263 | // Free the pre-allocated worklists for data-recursion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 264 | for (SmallVectorImpl<VisitorWorkList*>::iterator |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 265 | I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) { |
| 266 | delete *I; |
| 267 | } |
| 268 | } |
| 269 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 270 | ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); } |
| 271 | CXTranslationUnit getTU() const { return TU; } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 273 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 274 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 275 | bool visitPreprocessedEntitiesInRegion(); |
| 276 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 277 | bool shouldVisitIncludedPreprocessingEntries() const { |
| 278 | return VisitIncludedPreprocessingEntries; |
| 279 | } |
| 280 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 281 | template<typename InputIterator> |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 282 | bool visitPreprocessedEntities(InputIterator First, InputIterator Last, |
| 283 | PreprocessingRecord &PPRec, |
| 284 | FileID FID = FileID()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 286 | bool VisitChildren(CXCursor Parent); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 288 | // Declaration visitors |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 289 | bool VisitTypeAliasDecl(TypeAliasDecl *D); |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 290 | bool VisitAttributes(Decl *D); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 291 | bool VisitBlockDecl(BlockDecl *B); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 292 | bool VisitCXXRecordDecl(CXXRecordDecl *D); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 293 | llvm::Optional<bool> shouldVisitCursor(CXCursor C); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 294 | bool VisitDeclContext(DeclContext *DC); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 295 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 296 | bool VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 297 | bool VisitTagDecl(TagDecl *D); |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 298 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 299 | bool VisitClassTemplatePartialSpecializationDecl( |
| 300 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 301 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 302 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
| 303 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
| 304 | bool VisitFunctionDecl(FunctionDecl *ND); |
| 305 | bool VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 306 | bool VisitVarDecl(VarDecl *); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 307 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 308 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 309 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 310 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 311 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 312 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 313 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 314 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 315 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 316 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 317 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 318 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 319 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 320 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 321 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 322 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 323 | bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 324 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 325 | bool VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 326 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 327 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 328 | bool VisitUsingDecl(UsingDecl *D); |
| 329 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 330 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 332 | // Name visitor |
| 333 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 334 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 335 | bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 336 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 337 | // Template visitors |
| 338 | bool VisitTemplateParameters(const TemplateParameterList *Params); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 339 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 340 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); |
| 341 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 342 | // Type visitors |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 343 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 344 | #define TYPELOC(CLASS, PARENT) \ |
| 345 | bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
| 346 | #include "clang/AST/TypeLocNodes.def" |
| 347 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 348 | bool VisitTagTypeLoc(TagTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 349 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 350 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); |
| 351 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 352 | // Data-recursive visitor functions. |
| 353 | bool IsInRegionOfInterest(CXCursor C); |
| 354 | bool RunVisitorWorkList(VisitorWorkList &WL); |
| 355 | void EnqueueWorkList(VisitorWorkList &WL, Stmt *S); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 356 | LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 357 | }; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 359 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 361 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 362 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 363 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 365 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 366 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 369 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 370 | /// its children. |
| 371 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 372 | /// \param Cursor the cursor to visit. |
| 373 | /// |
| 374 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 375 | /// this cursor is within the region of interest. |
| 376 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 377 | /// \returns true if the visitation should be aborted, false if it |
| 378 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 379 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 380 | if (clang_isInvalid(Cursor.kind)) |
| 381 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 383 | if (clang_isDeclaration(Cursor.kind)) { |
| 384 | Decl *D = getCursorDecl(Cursor); |
| 385 | assert(D && "Invalid declaration cursor"); |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 386 | // Ignore implicit declarations, unless it's an objc method because |
| 387 | // currently we should report implicit methods for properties when indexing. |
| 388 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 392 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 393 | // we're done. |
| 394 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 395 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 396 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 397 | return false; |
| 398 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 400 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 401 | case CXChildVisit_Break: |
| 402 | return true; |
| 403 | |
| 404 | case CXChildVisit_Continue: |
| 405 | return false; |
| 406 | |
| 407 | case CXChildVisit_Recurse: |
| 408 | return VisitChildren(Cursor); |
| 409 | } |
| 410 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 411 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 414 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 415 | PreprocessingRecord &PPRec, |
| 416 | CursorVisitor &Visitor) { |
| 417 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 418 | FileID FID; |
| 419 | |
| 420 | if (!Visitor.shouldVisitIncludedPreprocessingEntries()) { |
| 421 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 422 | // where we skip preprocessed entities that do not come from the same FileID. |
| 423 | FID = SM.getFileID(R.getBegin()); |
| 424 | if (FID != SM.getFileID(R.getEnd())) |
| 425 | FID = FileID(); |
| 426 | } |
| 427 | |
| 428 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 429 | Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 430 | return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, |
| 431 | PPRec, FID); |
| 432 | } |
| 433 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 434 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 435 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 436 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 437 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 438 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 439 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 440 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 441 | SourceLocation B = MappedRange.getBegin(); |
| 442 | SourceLocation E = MappedRange.getEnd(); |
| 443 | |
| 444 | if (AU->isInPreambleFileID(B)) { |
| 445 | if (SM.isLoadedSourceLocation(E)) |
| 446 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 447 | PPRec, *this); |
| 448 | |
| 449 | // Beginning of range lies in the preamble but it also extends beyond |
| 450 | // it into the main file. Split the range into 2 parts, one covering |
| 451 | // the preamble and another covering the main file. This allows subsequent |
| 452 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 453 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 454 | // do not come from the same FileID. |
| 455 | bool breaked = |
| 456 | visitPreprocessedEntitiesInRange( |
| 457 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 458 | PPRec, *this); |
| 459 | if (breaked) return true; |
| 460 | return visitPreprocessedEntitiesInRange( |
| 461 | SourceRange(AU->getStartOfMainFileID(), E), |
| 462 | PPRec, *this); |
| 463 | } |
| 464 | |
| 465 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 468 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 469 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 470 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 471 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 472 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 473 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 474 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 475 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | template<typename InputIterator> |
| 479 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 480 | InputIterator Last, |
| 481 | PreprocessingRecord &PPRec, |
| 482 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 483 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 484 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 485 | continue; |
| 486 | |
| 487 | PreprocessedEntity *PPE = *First; |
| 488 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 489 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 490 | return true; |
| 491 | |
| 492 | continue; |
| 493 | } |
| 494 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 495 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 496 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 497 | return true; |
| 498 | |
| 499 | continue; |
| 500 | } |
| 501 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 502 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 503 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 504 | return true; |
| 505 | |
| 506 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 510 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 513 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 514 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 515 | /// \returns true if the visitation should be aborted, false if it |
| 516 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 517 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 518 | if (clang_isReference(Cursor.kind) && |
| 519 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 520 | // By definition, references have no children. |
| 521 | return false; |
| 522 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 523 | |
| 524 | // 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] | 525 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 526 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 528 | if (clang_isDeclaration(Cursor.kind)) { |
| 529 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 530 | if (!D) |
| 531 | return false; |
| 532 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 533 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 534 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 535 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 536 | if (clang_isStatement(Cursor.kind)) { |
| 537 | if (Stmt *S = getCursorStmt(Cursor)) |
| 538 | return Visit(S); |
| 539 | |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | if (clang_isExpression(Cursor.kind)) { |
| 544 | if (Expr *E = getCursorExpr(Cursor)) |
| 545 | return Visit(E); |
| 546 | |
| 547 | return false; |
| 548 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 550 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 551 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 552 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 553 | |
| 554 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 555 | for (unsigned I = 0; I != 2; ++I) { |
| 556 | if (VisitOrder[I]) { |
| 557 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 558 | RegionOfInterest.isInvalid()) { |
| 559 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 560 | TLEnd = CXXUnit->top_level_end(); |
| 561 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 562 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | } else if (VisitDeclContext( |
| 566 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 567 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 568 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 569 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 571 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 572 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 573 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 574 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 576 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 577 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 578 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 579 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 580 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 581 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 582 | return Visit(BaseTSInfo->getTypeLoc()); |
| 583 | } |
| 584 | } |
| 585 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 586 | |
| 587 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 588 | IBOutletCollectionAttr *A = |
| 589 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 590 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 591 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 592 | A->getInterfaceLoc(), TU)); |
| 593 | } |
| 594 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 595 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 599 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 600 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 601 | if (Visit(TSInfo->getTypeLoc())) |
| 602 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 603 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 604 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 605 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 606 | |
| 607 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 610 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 611 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 612 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 613 | if (Range.isInvalid()) |
| 614 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 616 | switch (CompareRegionOfInterest(Range)) { |
| 617 | case RangeBefore: |
| 618 | // This declaration comes before the region of interest; skip it. |
| 619 | return llvm::Optional<bool>(); |
| 620 | |
| 621 | case RangeAfter: |
| 622 | // This declaration comes after the region of interest; we're done. |
| 623 | return false; |
| 624 | |
| 625 | case RangeOverlap: |
| 626 | // This declaration overlaps the region of interest; visit it. |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | |
| 633 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 634 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 635 | |
| 636 | // FIXME: Eventually remove. This part of a hack to support proper |
| 637 | // iteration over all Decls contained lexically within an ObjC container. |
| 638 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 639 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 640 | |
| 641 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 642 | Decl *D = *I; |
| 643 | if (D->getLexicalDeclContext() != DC) |
| 644 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 645 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 646 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 647 | if (!V.hasValue()) |
| 648 | continue; |
| 649 | if (!V.getValue()) |
| 650 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 651 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 652 | return true; |
| 653 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 654 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 657 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 658 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 659 | return false; |
| 660 | } |
| 661 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 662 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 663 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 664 | return Visit(TSInfo->getTypeLoc()); |
| 665 | |
| 666 | return false; |
| 667 | } |
| 668 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 669 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 670 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 671 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 673 | return false; |
| 674 | } |
| 675 | |
| 676 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 677 | return VisitDeclContext(D); |
| 678 | } |
| 679 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 680 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 681 | ClassTemplateSpecializationDecl *D) { |
| 682 | bool ShouldVisitBody = false; |
| 683 | switch (D->getSpecializationKind()) { |
| 684 | case TSK_Undeclared: |
| 685 | case TSK_ImplicitInstantiation: |
| 686 | // Nothing to visit |
| 687 | return false; |
| 688 | |
| 689 | case TSK_ExplicitInstantiationDeclaration: |
| 690 | case TSK_ExplicitInstantiationDefinition: |
| 691 | break; |
| 692 | |
| 693 | case TSK_ExplicitSpecialization: |
| 694 | ShouldVisitBody = true; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | // Visit the template arguments used in the specialization. |
| 699 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 700 | TypeLoc TL = SpecType->getTypeLoc(); |
| 701 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 702 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 703 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 704 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 705 | return true; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 710 | return true; |
| 711 | |
| 712 | return false; |
| 713 | } |
| 714 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 715 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 716 | ClassTemplatePartialSpecializationDecl *D) { |
| 717 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 718 | // before visiting these template parameters. |
| 719 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 720 | return true; |
| 721 | |
| 722 | // Visit the partial specialization arguments. |
| 723 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 724 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 725 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 726 | return true; |
| 727 | |
| 728 | return VisitCXXRecordDecl(D); |
| 729 | } |
| 730 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 731 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 732 | // Visit the default argument. |
| 733 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 734 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 735 | if (Visit(DefArg->getTypeLoc())) |
| 736 | return true; |
| 737 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 741 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 742 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 743 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 747 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 748 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 749 | if (Visit(TSInfo->getTypeLoc())) |
| 750 | return true; |
| 751 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 752 | // Visit the nested-name-specifier, if present. |
| 753 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 754 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 755 | return true; |
| 756 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 757 | return false; |
| 758 | } |
| 759 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 760 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 761 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 762 | CXXCtorInitializer const * const *X |
| 763 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 764 | CXXCtorInitializer const * const *Y |
| 765 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 766 | |
| 767 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 768 | return -1; |
| 769 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 770 | return 1; |
| 771 | else |
| 772 | return 0; |
| 773 | } |
| 774 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 775 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 776 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 777 | // Visit the function declaration's syntactic components in the order |
| 778 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 779 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 780 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 781 | |
| 782 | // If we have a function declared directly (without the use of a typedef), |
| 783 | // visit just the return type. Otherwise, just visit the function's type |
| 784 | // now. |
| 785 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 786 | (!FTL && Visit(TL))) |
| 787 | return true; |
| 788 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 789 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 790 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 791 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 792 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 793 | |
| 794 | // Visit the declaration name. |
| 795 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 796 | return true; |
| 797 | |
| 798 | // FIXME: Visit explicitly-specified template arguments! |
| 799 | |
| 800 | // Visit the function parameters, if we have a function type. |
| 801 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 802 | return true; |
| 803 | |
| 804 | // FIXME: Attributes? |
| 805 | } |
| 806 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 807 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 808 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 809 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 810 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 811 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 812 | IEnd = Constructor->init_end(); |
| 813 | I != IEnd; ++I) { |
| 814 | if (!(*I)->isWritten()) |
| 815 | continue; |
| 816 | |
| 817 | WrittenInits.push_back(*I); |
| 818 | } |
| 819 | |
| 820 | // Sort the initializers in source order |
| 821 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 822 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 823 | |
| 824 | // Visit the initializers in source order |
| 825 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 826 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 827 | if (Init->isAnyMemberInitializer()) { |
| 828 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 829 | Init->getMemberLocation(), TU))) |
| 830 | return true; |
| 831 | } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) { |
| 832 | if (Visit(BaseInfo->getTypeLoc())) |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | // Visit the initializer value. |
| 837 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 838 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 839 | return true; |
| 840 | } |
| 841 | } |
| 842 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 843 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 844 | return true; |
| 845 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 846 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 847 | return false; |
| 848 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 850 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 851 | if (VisitDeclaratorDecl(D)) |
| 852 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 854 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 855 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
| 859 | |
| 860 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 861 | if (VisitDeclaratorDecl(D)) |
| 862 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 864 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 865 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 870 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 871 | if (VisitDeclaratorDecl(D)) |
| 872 | return true; |
| 873 | |
| 874 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 875 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 876 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 877 | |
| 878 | return false; |
| 879 | } |
| 880 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 881 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 882 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 883 | // before visiting these template parameters. |
| 884 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 885 | return true; |
| 886 | |
| 887 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 888 | } |
| 889 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 890 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 891 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 892 | // before visiting these template parameters. |
| 893 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 894 | return true; |
| 895 | |
| 896 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 897 | } |
| 898 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 899 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 900 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 901 | return true; |
| 902 | |
| 903 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 904 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 905 | return true; |
| 906 | |
| 907 | return false; |
| 908 | } |
| 909 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 910 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 911 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 912 | if (Visit(TSInfo->getTypeLoc())) |
| 913 | return true; |
| 914 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 915 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 916 | PEnd = ND->param_end(); |
| 917 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 918 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 919 | return true; |
| 920 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 922 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 923 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 924 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 929 | namespace { |
| 930 | struct ContainerDeclsSort { |
| 931 | SourceManager &SM; |
| 932 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 933 | bool operator()(Decl *A, Decl *B) { |
| 934 | SourceLocation L_A = A->getLocStart(); |
| 935 | SourceLocation L_B = B->getLocStart(); |
| 936 | assert(L_A.isValid() && L_B.isValid()); |
| 937 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 938 | } |
| 939 | }; |
| 940 | } |
| 941 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 942 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 943 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 944 | // an @implementation can lexically contain Decls that are not properly |
| 945 | // nested in the AST. When we identify such cases, we need to retrofit |
| 946 | // this nesting here. |
| 947 | if (!DI_current) |
| 948 | return VisitDeclContext(D); |
| 949 | |
| 950 | // Scan the Decls that immediately come after the container |
| 951 | // in the current DeclContext. If any fall within the |
| 952 | // container's lexical region, stash them into a vector |
| 953 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 954 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 955 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 956 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 957 | if (EndLoc.isValid()) { |
| 958 | DeclContext::decl_iterator next = *DI_current; |
| 959 | while (++next != DE_current) { |
| 960 | Decl *D_next = *next; |
| 961 | if (!D_next) |
| 962 | break; |
| 963 | SourceLocation L = D_next->getLocStart(); |
| 964 | if (!L.isValid()) |
| 965 | break; |
| 966 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 967 | *DI_current = next; |
| 968 | DeclsInContainer.push_back(D_next); |
| 969 | continue; |
| 970 | } |
| 971 | break; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // The common case. |
| 976 | if (DeclsInContainer.empty()) |
| 977 | return VisitDeclContext(D); |
| 978 | |
| 979 | // Get all the Decls in the DeclContext, and sort them with the |
| 980 | // additional ones we've collected. Then visit them. |
| 981 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 982 | I!=E; ++I) { |
| 983 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 984 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 985 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 986 | continue; |
| 987 | DeclsInContainer.push_back(subDecl); |
| 988 | } |
| 989 | |
| 990 | // Now sort the Decls so that they appear in lexical order. |
| 991 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 992 | ContainerDeclsSort(SM)); |
| 993 | |
| 994 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 995 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 996 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 997 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 998 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 999 | if (!V.hasValue()) |
| 1000 | continue; |
| 1001 | if (!V.getValue()) |
| 1002 | return false; |
| 1003 | if (Visit(Cursor, true)) |
| 1004 | return true; |
| 1005 | } |
| 1006 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1009 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1010 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1011 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1012 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1014 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1015 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1016 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1017 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1018 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1020 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1023 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1024 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1025 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1026 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1027 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1028 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1030 | return VisitObjCContainerDecl(PID); |
| 1031 | } |
| 1032 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1033 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 1034 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 1035 | return true; |
| 1036 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1037 | // FIXME: This implements a workaround with @property declarations also being |
| 1038 | // installed in the DeclContext for the @interface. Eventually this code |
| 1039 | // should be removed. |
| 1040 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1041 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1042 | return false; |
| 1043 | |
| 1044 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1045 | if (!ID) |
| 1046 | return false; |
| 1047 | |
| 1048 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1049 | ObjCPropertyDecl *prevDecl = |
| 1050 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 1051 | |
| 1052 | if (!prevDecl) |
| 1053 | return false; |
| 1054 | |
| 1055 | // Visit synthesized methods since they will be skipped when visiting |
| 1056 | // the @interface. |
| 1057 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1058 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1059 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1060 | return true; |
| 1061 | |
| 1062 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1063 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1064 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1065 | return true; |
| 1066 | |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1070 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1071 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1072 | if (D->getSuperClass() && |
| 1073 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1074 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1075 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1076 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1078 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1079 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1080 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1081 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1082 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1084 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1087 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1088 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1091 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1092 | // 'ID' could be null when dealing with invalid code. |
| 1093 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1094 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1095 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1096 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1097 | return VisitObjCImplDecl(D); |
| 1098 | } |
| 1099 | |
| 1100 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1101 | #if 0 |
| 1102 | // Issue callbacks for super class. |
| 1103 | // FIXME: No source location information! |
| 1104 | if (D->getSuperClass() && |
| 1105 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1106 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1107 | TU))) |
| 1108 | return true; |
| 1109 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1111 | return VisitObjCImplDecl(D); |
| 1112 | } |
| 1113 | |
| 1114 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1115 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1116 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1117 | E = D->protocol_end(); |
| 1118 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1119 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1120 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1121 | |
| 1122 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1125 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1126 | if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), |
| 1127 | D->getForwardDecl()->getLocation(), TU))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1128 | return true; |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1129 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1132 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1133 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1134 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1135 | |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1139 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1140 | return VisitDeclContext(D); |
| 1141 | } |
| 1142 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1143 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1144 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1145 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1146 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1147 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1148 | |
| 1149 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1150 | D->getTargetNameLoc(), TU)); |
| 1151 | } |
| 1152 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1153 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1154 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1155 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1156 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1157 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1158 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1160 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1161 | return true; |
| 1162 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1163 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1164 | } |
| 1165 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1166 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1167 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1168 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1169 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1170 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1171 | |
| 1172 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1173 | D->getIdentLocation(), TU)); |
| 1174 | } |
| 1175 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1176 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1177 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1178 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1179 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1180 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1181 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1183 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1184 | } |
| 1185 | |
| 1186 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1187 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1188 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1189 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1190 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1191 | return true; |
| 1192 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1196 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1197 | switch (Name.getName().getNameKind()) { |
| 1198 | case clang::DeclarationName::Identifier: |
| 1199 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1200 | case clang::DeclarationName::CXXOperatorName: |
| 1201 | case clang::DeclarationName::CXXUsingDirective: |
| 1202 | return false; |
| 1203 | |
| 1204 | case clang::DeclarationName::CXXConstructorName: |
| 1205 | case clang::DeclarationName::CXXDestructorName: |
| 1206 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1207 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1208 | return Visit(TSInfo->getTypeLoc()); |
| 1209 | return false; |
| 1210 | |
| 1211 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1212 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1213 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1214 | // FIXME: Per-identifier location info? |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | return false; |
| 1219 | } |
| 1220 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1221 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1222 | SourceRange Range) { |
| 1223 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1224 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1225 | // a beginning source location, we can visit the first component of the |
| 1226 | // nested-name-specifier, if it's a single-token component. |
| 1227 | if (!NNS) |
| 1228 | return false; |
| 1229 | |
| 1230 | // Get the first component in the nested-name-specifier. |
| 1231 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1232 | NNS = Prefix; |
| 1233 | |
| 1234 | switch (NNS->getKind()) { |
| 1235 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1236 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1237 | TU)); |
| 1238 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1239 | case NestedNameSpecifier::NamespaceAlias: |
| 1240 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1241 | Range.getBegin(), TU)); |
| 1242 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1243 | case NestedNameSpecifier::TypeSpec: { |
| 1244 | // If the type has a form where we know that the beginning of the source |
| 1245 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1246 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1247 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1248 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1249 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1250 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1251 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1252 | if (const TemplateSpecializationType *TST |
| 1253 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1254 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1259 | case NestedNameSpecifier::Global: |
| 1260 | case NestedNameSpecifier::Identifier: |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1267 | bool |
| 1268 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1269 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1270 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1271 | Qualifiers.push_back(Qualifier); |
| 1272 | |
| 1273 | while (!Qualifiers.empty()) { |
| 1274 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1275 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1276 | switch (NNS->getKind()) { |
| 1277 | case NestedNameSpecifier::Namespace: |
| 1278 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1279 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1280 | TU))) |
| 1281 | return true; |
| 1282 | |
| 1283 | break; |
| 1284 | |
| 1285 | case NestedNameSpecifier::NamespaceAlias: |
| 1286 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1287 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1288 | TU))) |
| 1289 | return true; |
| 1290 | |
| 1291 | break; |
| 1292 | |
| 1293 | case NestedNameSpecifier::TypeSpec: |
| 1294 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1295 | if (Visit(Q.getTypeLoc())) |
| 1296 | return true; |
| 1297 | |
| 1298 | break; |
| 1299 | |
| 1300 | case NestedNameSpecifier::Global: |
| 1301 | case NestedNameSpecifier::Identifier: |
| 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | return false; |
| 1307 | } |
| 1308 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1309 | bool CursorVisitor::VisitTemplateParameters( |
| 1310 | const TemplateParameterList *Params) { |
| 1311 | if (!Params) |
| 1312 | return false; |
| 1313 | |
| 1314 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1315 | PEnd = Params->end(); |
| 1316 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1317 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1318 | return true; |
| 1319 | } |
| 1320 | |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1324 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1325 | switch (Name.getKind()) { |
| 1326 | case TemplateName::Template: |
| 1327 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1328 | |
| 1329 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1330 | // Visit the overloaded template set. |
| 1331 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1332 | return true; |
| 1333 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1334 | return false; |
| 1335 | |
| 1336 | case TemplateName::DependentTemplate: |
| 1337 | // FIXME: Visit nested-name-specifier. |
| 1338 | return false; |
| 1339 | |
| 1340 | case TemplateName::QualifiedTemplate: |
| 1341 | // FIXME: Visit nested-name-specifier. |
| 1342 | return Visit(MakeCursorTemplateRef( |
| 1343 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1344 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1345 | |
| 1346 | case TemplateName::SubstTemplateTemplateParm: |
| 1347 | return Visit(MakeCursorTemplateRef( |
| 1348 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1349 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1350 | |
| 1351 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1352 | return Visit(MakeCursorTemplateRef( |
| 1353 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1354 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1360 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1361 | switch (TAL.getArgument().getKind()) { |
| 1362 | case TemplateArgument::Null: |
| 1363 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1364 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1365 | return false; |
| 1366 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1367 | case TemplateArgument::Type: |
| 1368 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1369 | return Visit(TSInfo->getTypeLoc()); |
| 1370 | return false; |
| 1371 | |
| 1372 | case TemplateArgument::Declaration: |
| 1373 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1374 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1375 | return false; |
| 1376 | |
| 1377 | case TemplateArgument::Expression: |
| 1378 | if (Expr *E = TAL.getSourceExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1379 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1380 | return false; |
| 1381 | |
| 1382 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1383 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1384 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1385 | return true; |
| 1386 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1387 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1388 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1394 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1395 | return VisitDeclContext(D); |
| 1396 | } |
| 1397 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1398 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1399 | return Visit(TL.getUnqualifiedLoc()); |
| 1400 | } |
| 1401 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1402 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1403 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1406 | // "Class") have associated declarations. Create cursors for those. |
| 1407 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1408 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1409 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1410 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1411 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1412 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1413 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1414 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1415 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1416 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1417 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1418 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1419 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1421 | case BuiltinType::ObjCId: |
| 1422 | VisitType = Context.getObjCIdType(); |
| 1423 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1424 | |
| 1425 | case BuiltinType::ObjCClass: |
| 1426 | VisitType = Context.getObjCClassType(); |
| 1427 | break; |
| 1428 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1429 | case BuiltinType::ObjCSel: |
| 1430 | VisitType = Context.getObjCSelType(); |
| 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | if (!VisitType.isNull()) { |
| 1435 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1436 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1437 | TU)); |
| 1438 | } |
| 1439 | |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1443 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1444 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1447 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1448 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1449 | } |
| 1450 | |
| 1451 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1452 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1453 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1455 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1456 | } |
| 1457 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1458 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1459 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1462 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1463 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1464 | return true; |
| 1465 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
| 1468 | |
| 1469 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1470 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1471 | return true; |
| 1472 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1473 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1474 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1475 | TU))) |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1483 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1486 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1487 | return Visit(TL.getInnerLoc()); |
| 1488 | } |
| 1489 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1490 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1491 | return Visit(TL.getPointeeLoc()); |
| 1492 | } |
| 1493 | |
| 1494 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1495 | return Visit(TL.getPointeeLoc()); |
| 1496 | } |
| 1497 | |
| 1498 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1499 | return Visit(TL.getPointeeLoc()); |
| 1500 | } |
| 1501 | |
| 1502 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1503 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1507 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1510 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1511 | return Visit(TL.getModifiedLoc()); |
| 1512 | } |
| 1513 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1514 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1515 | bool SkipResultType) { |
| 1516 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1517 | return true; |
| 1518 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1519 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1520 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1521 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1522 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1523 | |
| 1524 | return false; |
| 1525 | } |
| 1526 | |
| 1527 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1528 | if (Visit(TL.getElementLoc())) |
| 1529 | return true; |
| 1530 | |
| 1531 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1532 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1533 | |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1537 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1538 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1539 | // Visit the template name. |
| 1540 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1541 | TL.getTemplateNameLoc())) |
| 1542 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1543 | |
| 1544 | // Visit the template arguments. |
| 1545 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1546 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1547 | return true; |
| 1548 | |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1552 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1553 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1554 | } |
| 1555 | |
| 1556 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1557 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1558 | return Visit(TSInfo->getTypeLoc()); |
| 1559 | |
| 1560 | return false; |
| 1561 | } |
| 1562 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1563 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1564 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1565 | return Visit(TSInfo->getTypeLoc()); |
| 1566 | |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1570 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1571 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1572 | return true; |
| 1573 | |
| 1574 | return false; |
| 1575 | } |
| 1576 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1577 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1578 | DependentTemplateSpecializationTypeLoc TL) { |
| 1579 | // Visit the nested-name-specifier, if there is one. |
| 1580 | if (TL.getQualifierLoc() && |
| 1581 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1582 | return true; |
| 1583 | |
| 1584 | // Visit the template arguments. |
| 1585 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1586 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1587 | return true; |
| 1588 | |
| 1589 | return false; |
| 1590 | } |
| 1591 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1592 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1593 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1594 | return true; |
| 1595 | |
| 1596 | return Visit(TL.getNamedTypeLoc()); |
| 1597 | } |
| 1598 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1599 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1600 | return Visit(TL.getPatternLoc()); |
| 1601 | } |
| 1602 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1603 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1604 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1605 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1606 | |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1611 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1612 | } |
| 1613 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1614 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1615 | return Visit(TL.getValueLoc()); |
| 1616 | } |
| 1617 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1618 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1619 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1620 | return Visit##PARENT##Loc(TL); \ |
| 1621 | } |
| 1622 | |
| 1623 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1624 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1625 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1626 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1627 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1628 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1629 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1630 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1631 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1632 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1633 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1634 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1635 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1636 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1637 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1638 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1639 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1640 | // Visit the nested-name-specifier, if present. |
| 1641 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1642 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1643 | return true; |
| 1644 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1645 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1646 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1647 | E = D->bases_end(); I != E; ++I) { |
| 1648 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1649 | return true; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | return VisitTagDecl(D); |
| 1654 | } |
| 1655 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1656 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1657 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1658 | i != e; ++i) |
| 1659 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1660 | return true; |
| 1661 | |
| 1662 | return false; |
| 1663 | } |
| 1664 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1665 | //===----------------------------------------------------------------------===// |
| 1666 | // Data-recursive visitor methods. |
| 1667 | //===----------------------------------------------------------------------===// |
| 1668 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1669 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1670 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1671 | class NAME : public VisitorJob {\ |
| 1672 | public:\ |
| 1673 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1674 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1675 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1676 | }; |
| 1677 | |
| 1678 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1679 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1680 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1681 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1682 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1683 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1684 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1685 | #undef DEF_JOB |
| 1686 | |
| 1687 | class DeclVisit : public VisitorJob { |
| 1688 | public: |
| 1689 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1690 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1691 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1692 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1693 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1694 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1695 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1696 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1697 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1698 | class TypeLocVisit : public VisitorJob { |
| 1699 | public: |
| 1700 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1701 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1702 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1703 | |
| 1704 | static bool classof(const VisitorJob *VJ) { |
| 1705 | return VJ->getKind() == TypeLocVisitKind; |
| 1706 | } |
| 1707 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1708 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1709 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1710 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1711 | } |
| 1712 | }; |
| 1713 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1714 | class LabelRefVisit : public VisitorJob { |
| 1715 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1716 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1717 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1718 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1719 | |
| 1720 | static bool classof(const VisitorJob *VJ) { |
| 1721 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1722 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1723 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1724 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1725 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1726 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1727 | |
| 1728 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1729 | public: |
| 1730 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1731 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1732 | Qualifier.getNestedNameSpecifier(), |
| 1733 | Qualifier.getOpaqueData()) { } |
| 1734 | |
| 1735 | static bool classof(const VisitorJob *VJ) { |
| 1736 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1737 | } |
| 1738 | |
| 1739 | NestedNameSpecifierLoc get() const { |
| 1740 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1741 | data[1]); |
| 1742 | } |
| 1743 | }; |
| 1744 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1745 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1746 | public: |
| 1747 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1748 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1749 | static bool classof(const VisitorJob *VJ) { |
| 1750 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1751 | } |
| 1752 | DeclarationNameInfo get() const { |
| 1753 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1754 | switch (S->getStmtClass()) { |
| 1755 | default: |
| 1756 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame^] | 1757 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1758 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1759 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1760 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1761 | case Stmt::DependentScopeDeclRefExprClass: |
| 1762 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1763 | } |
| 1764 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1765 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1766 | class MemberRefVisit : public VisitorJob { |
| 1767 | public: |
| 1768 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1769 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1770 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1771 | static bool classof(const VisitorJob *VJ) { |
| 1772 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1773 | } |
| 1774 | FieldDecl *get() const { |
| 1775 | return static_cast<FieldDecl*>(data[0]); |
| 1776 | } |
| 1777 | SourceLocation getLoc() const { |
| 1778 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1779 | } |
| 1780 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1781 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1782 | VisitorWorkList &WL; |
| 1783 | CXCursor Parent; |
| 1784 | public: |
| 1785 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1786 | : WL(wl), Parent(parent) {} |
| 1787 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1788 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1789 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1790 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1791 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1792 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame^] | 1793 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1794 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1795 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1796 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1797 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1798 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1799 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1800 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1801 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1802 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1803 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1804 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1805 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1806 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1807 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1808 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1809 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1810 | void VisitIfStmt(IfStmt *If); |
| 1811 | void VisitInitListExpr(InitListExpr *IE); |
| 1812 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1813 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1814 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1815 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1816 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1817 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1818 | void VisitStmt(Stmt *S); |
| 1819 | void VisitSwitchStmt(SwitchStmt *S); |
| 1820 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1821 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1822 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1823 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1824 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1825 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1826 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1827 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1828 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1829 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1830 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1831 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1832 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1833 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1834 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1835 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1836 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1837 | void EnqueueChildren(Stmt *S); |
| 1838 | }; |
| 1839 | } // end anonyous namespace |
| 1840 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1841 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1842 | // 'S' should always be non-null, since it comes from the |
| 1843 | // statement we are visiting. |
| 1844 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1845 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1846 | |
| 1847 | void |
| 1848 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1849 | if (Qualifier) |
| 1850 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1851 | } |
| 1852 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1853 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1854 | if (S) |
| 1855 | WL.push_back(StmtVisit(S, Parent)); |
| 1856 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1857 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1858 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1859 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1860 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1861 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1862 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1863 | if (A) |
| 1864 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1865 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1866 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1867 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1868 | if (D) |
| 1869 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1870 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1871 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1872 | if (TI) |
| 1873 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1874 | } |
| 1875 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1876 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1877 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1878 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1879 | } |
| 1880 | if (size == WL.size()) |
| 1881 | return; |
| 1882 | // Now reverse the entries we just added. This will match the DFS |
| 1883 | // ordering performed by the worklist. |
| 1884 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1885 | std::reverse(I, E); |
| 1886 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1887 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1888 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1889 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1890 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1891 | AddDecl(B->getBlockDecl()); |
| 1892 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1893 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1894 | EnqueueChildren(E); |
| 1895 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1896 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1897 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1898 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1899 | E = S->body_rend(); I != E; ++I) { |
| 1900 | AddStmt(*I); |
| 1901 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1902 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1903 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame^] | 1904 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 1905 | AddStmt(S->getSubStmt()); |
| 1906 | AddDeclarationNameInfo(S); |
| 1907 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 1908 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 1909 | } |
| 1910 | |
| 1911 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1912 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1913 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1914 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1915 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1916 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1917 | if (!E->isImplicitAccess()) |
| 1918 | AddStmt(E->getBase()); |
| 1919 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1920 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1921 | // Enqueue the initializer or constructor arguments. |
| 1922 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 1923 | AddStmt(E->getConstructorArg(I-1)); |
| 1924 | // Enqueue the array size, if any. |
| 1925 | AddStmt(E->getArraySize()); |
| 1926 | // Enqueue the allocated type. |
| 1927 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1928 | // Enqueue the placement arguments. |
| 1929 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1930 | AddStmt(E->getPlacementArg(I-1)); |
| 1931 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1932 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1933 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1934 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1935 | AddStmt(CE->getCallee()); |
| 1936 | AddStmt(CE->getArg(0)); |
| 1937 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1938 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1939 | // Visit the name of the type being destroyed. |
| 1940 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1941 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1942 | // but isn't. |
| 1943 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1944 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1945 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1946 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1947 | // Visit base expression. |
| 1948 | AddStmt(E->getBase()); |
| 1949 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1950 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1951 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1952 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1953 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1954 | EnqueueChildren(E); |
| 1955 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1956 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1957 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1958 | EnqueueChildren(E); |
| 1959 | if (E->isTypeOperand()) |
| 1960 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1961 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1962 | |
| 1963 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1964 | *E) { |
| 1965 | EnqueueChildren(E); |
| 1966 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1967 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1968 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1969 | EnqueueChildren(E); |
| 1970 | if (E->isTypeOperand()) |
| 1971 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1972 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1973 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1974 | if (DR->hasExplicitTemplateArgs()) { |
| 1975 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1976 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1977 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1978 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1979 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1980 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1981 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1982 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1983 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1984 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1985 | unsigned size = WL.size(); |
| 1986 | bool isFirst = true; |
| 1987 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1988 | D != DEnd; ++D) { |
| 1989 | AddDecl(*D, isFirst); |
| 1990 | isFirst = false; |
| 1991 | } |
| 1992 | if (size == WL.size()) |
| 1993 | return; |
| 1994 | // Now reverse the entries we just added. This will match the DFS |
| 1995 | // ordering performed by the worklist. |
| 1996 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1997 | std::reverse(I, E); |
| 1998 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1999 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 2000 | AddStmt(E->getInit()); |
| 2001 | typedef DesignatedInitExpr::Designator Designator; |
| 2002 | for (DesignatedInitExpr::reverse_designators_iterator |
| 2003 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 2004 | D != DEnd; ++D) { |
| 2005 | if (D->isFieldDesignator()) { |
| 2006 | if (FieldDecl *Field = D->getField()) |
| 2007 | AddMemberRef(Field, D->getFieldLoc()); |
| 2008 | continue; |
| 2009 | } |
| 2010 | if (D->isArrayDesignator()) { |
| 2011 | AddStmt(E->getArrayIndex(*D)); |
| 2012 | continue; |
| 2013 | } |
| 2014 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 2015 | AddStmt(E->getArrayRangeEnd(*D)); |
| 2016 | AddStmt(E->getArrayRangeStart(*D)); |
| 2017 | } |
| 2018 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2019 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 2020 | EnqueueChildren(E); |
| 2021 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2022 | } |
| 2023 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 2024 | AddStmt(FS->getBody()); |
| 2025 | AddStmt(FS->getInc()); |
| 2026 | AddStmt(FS->getCond()); |
| 2027 | AddDecl(FS->getConditionVariable()); |
| 2028 | AddStmt(FS->getInit()); |
| 2029 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2030 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 2031 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2032 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2033 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 2034 | AddStmt(If->getElse()); |
| 2035 | AddStmt(If->getThen()); |
| 2036 | AddStmt(If->getCond()); |
| 2037 | AddDecl(If->getConditionVariable()); |
| 2038 | } |
| 2039 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 2040 | // We care about the syntactic form of the initializer list, only. |
| 2041 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2042 | IE = Syntactic; |
| 2043 | EnqueueChildren(IE); |
| 2044 | } |
| 2045 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 2046 | WL.push_back(MemberExprParts(M, Parent)); |
| 2047 | |
| 2048 | // If the base of the member access expression is an implicit 'this', don't |
| 2049 | // visit it. |
| 2050 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2051 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2052 | if (!M->isImplicitAccess()) |
| 2053 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2054 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2055 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2056 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2057 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2058 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2059 | EnqueueChildren(M); |
| 2060 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2061 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2062 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2063 | // Visit the components of the offsetof expression. |
| 2064 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2065 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2066 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2067 | switch (Node.getKind()) { |
| 2068 | case OffsetOfNode::Array: |
| 2069 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2070 | break; |
| 2071 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2072 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2073 | break; |
| 2074 | case OffsetOfNode::Identifier: |
| 2075 | case OffsetOfNode::Base: |
| 2076 | continue; |
| 2077 | } |
| 2078 | } |
| 2079 | // Visit the type into which we're computing the offset. |
| 2080 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2081 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2082 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2083 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2084 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2085 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2086 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2087 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2088 | EnqueueChildren(E); |
| 2089 | if (E->isArgumentType()) |
| 2090 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2091 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2092 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2093 | EnqueueChildren(S); |
| 2094 | } |
| 2095 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2096 | AddStmt(S->getBody()); |
| 2097 | AddStmt(S->getCond()); |
| 2098 | AddDecl(S->getConditionVariable()); |
| 2099 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2100 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2101 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2102 | AddStmt(W->getBody()); |
| 2103 | AddStmt(W->getCond()); |
| 2104 | AddDecl(W->getConditionVariable()); |
| 2105 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2106 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2107 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2108 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2109 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2110 | |
| 2111 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2112 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2113 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2116 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2117 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2118 | } |
| 2119 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2120 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2121 | EnqueueChildren(E); |
| 2122 | } |
| 2123 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2124 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2125 | VisitOverloadExpr(U); |
| 2126 | if (!U->isImplicitAccess()) |
| 2127 | AddStmt(U->getBase()); |
| 2128 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2129 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2130 | AddStmt(E->getSubExpr()); |
| 2131 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2132 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2133 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2134 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2135 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2136 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2137 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2138 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2142 | if (RegionOfInterest.isValid()) { |
| 2143 | SourceRange Range = getRawCursorExtent(C); |
| 2144 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2145 | return false; |
| 2146 | } |
| 2147 | return true; |
| 2148 | } |
| 2149 | |
| 2150 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2151 | while (!WL.empty()) { |
| 2152 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2153 | VisitorJob LI = WL.back(); |
| 2154 | WL.pop_back(); |
| 2155 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2156 | // Set the Parent field, then back to its old value once we're done. |
| 2157 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2158 | |
| 2159 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2160 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2161 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2162 | if (!D) |
| 2163 | continue; |
| 2164 | |
| 2165 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2166 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2167 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2168 | return true; |
| 2169 | |
| 2170 | continue; |
| 2171 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2172 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2173 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2174 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2175 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2176 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2177 | Arg != ArgEnd; ++Arg) { |
| 2178 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2179 | return true; |
| 2180 | } |
| 2181 | continue; |
| 2182 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2183 | case VisitorJob::TypeLocVisitKind: { |
| 2184 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2185 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2186 | return true; |
| 2187 | continue; |
| 2188 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2189 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2190 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2191 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2192 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2193 | TU))) { |
| 2194 | return true; |
| 2195 | } |
| 2196 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2197 | continue; |
| 2198 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2200 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2201 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2202 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2203 | return true; |
| 2204 | continue; |
| 2205 | } |
| 2206 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2207 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2208 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2209 | ->get())) |
| 2210 | return true; |
| 2211 | continue; |
| 2212 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2213 | case VisitorJob::MemberRefVisitKind: { |
| 2214 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2215 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2216 | return true; |
| 2217 | continue; |
| 2218 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2219 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2220 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2221 | if (!S) |
| 2222 | continue; |
| 2223 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2224 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2225 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2226 | if (!IsInRegionOfInterest(Cursor)) |
| 2227 | continue; |
| 2228 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2229 | case CXChildVisit_Break: return true; |
| 2230 | case CXChildVisit_Continue: break; |
| 2231 | case CXChildVisit_Recurse: |
| 2232 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2233 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2234 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2235 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2236 | } |
| 2237 | case VisitorJob::MemberExprPartsKind: { |
| 2238 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2239 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2240 | |
| 2241 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2242 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2243 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2244 | return true; |
| 2245 | |
| 2246 | // Visit the declaration name. |
| 2247 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2248 | return true; |
| 2249 | |
| 2250 | // Visit the explicitly-specified template arguments, if any. |
| 2251 | if (M->hasExplicitTemplateArgs()) { |
| 2252 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2253 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2254 | Arg != ArgEnd; ++Arg) { |
| 2255 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2256 | return true; |
| 2257 | } |
| 2258 | } |
| 2259 | continue; |
| 2260 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2261 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2262 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2263 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2264 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2265 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2266 | return true; |
| 2267 | // Visit declaration name. |
| 2268 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2269 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2270 | continue; |
| 2271 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2272 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2273 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2274 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2275 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2276 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2277 | return true; |
| 2278 | // Visit the declaration name. |
| 2279 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2280 | return true; |
| 2281 | // Visit the overloaded declaration reference. |
| 2282 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2283 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2284 | continue; |
| 2285 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2286 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2287 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2288 | NamedDecl *Pack = E->getPack(); |
| 2289 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2290 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2291 | E->getPackLoc(), TU))) |
| 2292 | return true; |
| 2293 | |
| 2294 | continue; |
| 2295 | } |
| 2296 | |
| 2297 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2298 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2299 | E->getPackLoc(), TU))) |
| 2300 | return true; |
| 2301 | |
| 2302 | continue; |
| 2303 | } |
| 2304 | |
| 2305 | // Non-type template parameter packs and function parameter packs are |
| 2306 | // treated like DeclRefExpr cursors. |
| 2307 | continue; |
| 2308 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | return false; |
| 2312 | } |
| 2313 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2314 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2315 | VisitorWorkList *WL = 0; |
| 2316 | if (!WorkListFreeList.empty()) { |
| 2317 | WL = WorkListFreeList.back(); |
| 2318 | WL->clear(); |
| 2319 | WorkListFreeList.pop_back(); |
| 2320 | } |
| 2321 | else { |
| 2322 | WL = new VisitorWorkList(); |
| 2323 | WorkListCache.push_back(WL); |
| 2324 | } |
| 2325 | EnqueueWorkList(*WL, S); |
| 2326 | bool result = RunVisitorWorkList(*WL); |
| 2327 | WorkListFreeList.push_back(WL); |
| 2328 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2331 | namespace { |
| 2332 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2333 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2334 | const DeclarationNameInfo &NI, |
| 2335 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2336 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2337 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2338 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2339 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2340 | |
| 2341 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2342 | |
| 2343 | RefNamePieces Pieces; |
| 2344 | |
| 2345 | if (WantQualifier && QLoc.isValid()) |
| 2346 | Pieces.push_back(QLoc); |
| 2347 | |
| 2348 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2349 | Pieces.push_back(NI.getLoc()); |
| 2350 | |
| 2351 | if (WantTemplateArgs && TemplateArgs) |
| 2352 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2353 | TemplateArgs->RAngleLoc)); |
| 2354 | |
| 2355 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2356 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2357 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2358 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2359 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2360 | } |
| 2361 | |
| 2362 | if (WantSinglePiece) { |
| 2363 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2364 | Pieces.clear(); |
| 2365 | Pieces.push_back(R); |
| 2366 | } |
| 2367 | |
| 2368 | return Pieces; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2372 | //===----------------------------------------------------------------------===// |
| 2373 | // Misc. API hooks. |
| 2374 | //===----------------------------------------------------------------------===// |
| 2375 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2376 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2377 | static bool EnabledMultithreading; |
| 2378 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2379 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2380 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2381 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2382 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2383 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2384 | llvm::DisablePrettyStackTrace = true; |
| 2385 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2386 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2387 | // enable it. |
| 2388 | llvm::CrashRecoveryContext::Enable(); |
| 2389 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2390 | // Enable support for multithreading in LLVM. |
| 2391 | { |
| 2392 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2393 | if (!EnabledMultithreading) { |
| 2394 | llvm::llvm_start_multithreaded(); |
| 2395 | EnabledMultithreading = true; |
| 2396 | } |
| 2397 | } |
| 2398 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2399 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2400 | if (excludeDeclarationsFromPCH) |
| 2401 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2402 | if (displayDiagnostics) |
| 2403 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2404 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2407 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2408 | if (CIdx) |
| 2409 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2412 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2413 | if (isEnabled) |
| 2414 | llvm::CrashRecoveryContext::Enable(); |
| 2415 | else |
| 2416 | llvm::CrashRecoveryContext::Disable(); |
| 2417 | } |
| 2418 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2419 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2420 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2421 | if (!CIdx) |
| 2422 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2423 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2424 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2425 | FileSystemOptions FileSystemOpts; |
| 2426 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2427 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2428 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2429 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2430 | CXXIdx->getOnlyLocalDecls(), |
| 2431 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2432 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2435 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2436 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2437 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2440 | CXTranslationUnit |
| 2441 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2442 | const char *source_filename, |
| 2443 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2444 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2445 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2446 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2447 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2448 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2449 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2450 | command_line_args, num_command_line_args, |
| 2451 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2452 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2453 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2454 | |
| 2455 | struct ParseTranslationUnitInfo { |
| 2456 | CXIndex CIdx; |
| 2457 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2458 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2459 | int num_command_line_args; |
| 2460 | struct CXUnsavedFile *unsaved_files; |
| 2461 | unsigned num_unsaved_files; |
| 2462 | unsigned options; |
| 2463 | CXTranslationUnit result; |
| 2464 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2465 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2466 | ParseTranslationUnitInfo *PTUI = |
| 2467 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2468 | CXIndex CIdx = PTUI->CIdx; |
| 2469 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2470 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2471 | int num_command_line_args = PTUI->num_command_line_args; |
| 2472 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2473 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2474 | unsigned options = PTUI->options; |
| 2475 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2476 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2477 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2478 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2479 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2480 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2481 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2482 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2483 | // FIXME: Add a flag for modules. |
| 2484 | TranslationUnitKind TUKind |
| 2485 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2486 | bool CacheCodeCompetionResults |
| 2487 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2488 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2489 | // Configure the diagnostics. |
| 2490 | DiagnosticOptions DiagOpts; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2491 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2492 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2493 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2495 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2496 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2497 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2498 | DiagCleanup(Diags.getPtr()); |
| 2499 | |
| 2500 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2501 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2502 | |
| 2503 | // Recover resources if we crash before exiting this function. |
| 2504 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2505 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2506 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2507 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2508 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2509 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2510 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2511 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2512 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2513 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2514 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2515 | llvm::OwningPtr<std::vector<const char *> > |
| 2516 | Args(new std::vector<const char*>()); |
| 2517 | |
| 2518 | // Recover resources if we crash before exiting this method. |
| 2519 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2520 | ArgsCleanup(Args.get()); |
| 2521 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2522 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2523 | // (often very broken) source code, where spell-checking can have a |
| 2524 | // significant negative impact on performance (particularly when |
| 2525 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2526 | // Only do this if we haven't found a spell-checking-related argument. |
| 2527 | bool FoundSpellCheckingArgument = false; |
| 2528 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2529 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2530 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2531 | FoundSpellCheckingArgument = true; |
| 2532 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2533 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2534 | } |
| 2535 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2536 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2537 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2538 | Args->insert(Args->end(), command_line_args, |
| 2539 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2540 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2541 | // The 'source_filename' argument is optional. If the caller does not |
| 2542 | // specify it then it is assumed that the source file is specified |
| 2543 | // in the actual argument list. |
| 2544 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2545 | // present it will be unused. |
| 2546 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2547 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2548 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2549 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2550 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2551 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2552 | Args->push_back("-Xclang"); |
| 2553 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2554 | NestedMacroExpansions |
| 2555 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2558 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2559 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2560 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2561 | /* vector::data() not portable */, |
| 2562 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2563 | Diags, |
| 2564 | CXXIdx->getClangResourcesPath(), |
| 2565 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2566 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2567 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2568 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2569 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2570 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2571 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2572 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2573 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2574 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2575 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2576 | // Make sure to check that 'Unit' is non-NULL. |
| 2577 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2578 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2579 | DEnd = Unit->stored_diag_end(); |
| 2580 | D != DEnd; ++D) { |
| 2581 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2582 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2583 | clang_defaultDiagnosticDisplayOptions()); |
| 2584 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2585 | clang_disposeString(Msg); |
| 2586 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2587 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2588 | // On Windows, force a flush, since there may be multiple copies of |
| 2589 | // stderr and stdout in the file system, all with different buffers |
| 2590 | // but writing to the same device. |
| 2591 | fflush(stderr); |
| 2592 | #endif |
| 2593 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2594 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2595 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2596 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2597 | } |
| 2598 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2599 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2600 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2601 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2602 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2603 | unsigned num_unsaved_files, |
| 2604 | unsigned options) { |
| 2605 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2606 | num_command_line_args, unsaved_files, |
| 2607 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2608 | llvm::CrashRecoveryContext CRC; |
| 2609 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2610 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2611 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2612 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2613 | fprintf(stderr, " 'command_line_args' : ["); |
| 2614 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2615 | if (i) |
| 2616 | fprintf(stderr, ", "); |
| 2617 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2618 | } |
| 2619 | fprintf(stderr, "],\n"); |
| 2620 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2621 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2622 | if (i) |
| 2623 | fprintf(stderr, ", "); |
| 2624 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2625 | unsaved_files[i].Length); |
| 2626 | } |
| 2627 | fprintf(stderr, "],\n"); |
| 2628 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2629 | fprintf(stderr, "}\n"); |
| 2630 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2631 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2632 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2633 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2634 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2635 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2636 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2639 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2640 | return CXSaveTranslationUnit_None; |
| 2641 | } |
| 2642 | |
| 2643 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2644 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2645 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2646 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2648 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2649 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2650 | PrintLibclangResourceUsage(TU); |
| 2651 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2652 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2653 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2654 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2655 | if (CTUnit) { |
| 2656 | // If the translation unit has been marked as unsafe to free, just discard |
| 2657 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2658 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2659 | return; |
| 2660 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2661 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2662 | disposeCXStringPool(CTUnit->StringPool); |
| 2663 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2664 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2665 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2667 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2668 | return CXReparse_None; |
| 2669 | } |
| 2670 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2671 | struct ReparseTranslationUnitInfo { |
| 2672 | CXTranslationUnit TU; |
| 2673 | unsigned num_unsaved_files; |
| 2674 | struct CXUnsavedFile *unsaved_files; |
| 2675 | unsigned options; |
| 2676 | int result; |
| 2677 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2678 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2679 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2680 | ReparseTranslationUnitInfo *RTUI = |
| 2681 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2682 | CXTranslationUnit TU = RTUI->TU; |
| 2683 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2684 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2685 | unsigned options = RTUI->options; |
| 2686 | (void) options; |
| 2687 | RTUI->result = 1; |
| 2688 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2689 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2690 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2691 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2692 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2693 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2694 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2695 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2696 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2697 | |
| 2698 | // Recover resources if we crash before exiting this function. |
| 2699 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2700 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2701 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2702 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2703 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2704 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2705 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2706 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2707 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2710 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2711 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2712 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2713 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2714 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2715 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2716 | unsigned num_unsaved_files, |
| 2717 | struct CXUnsavedFile *unsaved_files, |
| 2718 | unsigned options) { |
| 2719 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2720 | options, 0 }; |
| 2721 | llvm::CrashRecoveryContext CRC; |
| 2722 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2723 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2724 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2725 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2726 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2727 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2728 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2729 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2730 | return RTUI.result; |
| 2731 | } |
| 2732 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2733 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2734 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2735 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2736 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2737 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2738 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2739 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2740 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2742 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2743 | CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2744 | return Result; |
| 2745 | } |
| 2746 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2747 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2748 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2749 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2750 | // CXSourceLocation and CXSourceRange Operations. |
| 2751 | //===----------------------------------------------------------------------===// |
| 2752 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2753 | extern "C" { |
| 2754 | CXSourceLocation clang_getNullLocation() { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2755 | CXSourceLocation Result = { { 0, 0 }, 0 }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2756 | return Result; |
| 2757 | } |
| 2758 | |
| 2759 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { |
Daniel Dunbar | 90a6b9e | 2010-01-30 23:58:27 +0000 | [diff] [blame] | 2760 | return (loc1.ptr_data[0] == loc2.ptr_data[0] && |
| 2761 | loc1.ptr_data[1] == loc2.ptr_data[1] && |
| 2762 | loc1.int_data == loc2.int_data); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, |
| 2766 | CXFile file, |
| 2767 | unsigned line, |
| 2768 | unsigned column) { |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2769 | if (!tu || !file) |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2770 | return clang_getNullLocation(); |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2772 | bool Logging = ::getenv("LIBCLANG_LOGGING"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2773 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Argyrios Kyrtzidis | 57165be | 2011-10-10 21:57:15 +0000 | [diff] [blame] | 2774 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2775 | const FileEntry *File = static_cast<const FileEntry *>(file); |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2776 | SourceLocation SLoc = CXXUnit->getLocation(File, line, column); |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2777 | if (SLoc.isInvalid()) { |
| 2778 | if (Logging) |
| 2779 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2780 | << "\", " << line << ", " << column << ") = invalid\n"; |
| 2781 | return clang_getNullLocation(); |
| 2782 | } |
| 2783 | |
| 2784 | if (Logging) |
| 2785 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2786 | << "\", " << line << ", " << column << ") = " |
| 2787 | << SLoc.getRawEncoding() << "\n"; |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2788 | |
| 2789 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
| 2790 | } |
| 2791 | |
| 2792 | CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, |
| 2793 | CXFile file, |
| 2794 | unsigned offset) { |
| 2795 | if (!tu || !file) |
| 2796 | return clang_getNullLocation(); |
| 2797 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2798 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2799 | SourceLocation SLoc |
| 2800 | = CXXUnit->getLocation(static_cast<const FileEntry *>(file), offset); |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2801 | if (SLoc.isInvalid()) return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2802 | |
Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 2803 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2806 | CXSourceRange clang_getNullRange() { |
| 2807 | CXSourceRange Result = { { 0, 0 }, 0, 0 }; |
| 2808 | return Result; |
| 2809 | } |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 2810 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2811 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { |
| 2812 | if (begin.ptr_data[0] != end.ptr_data[0] || |
| 2813 | begin.ptr_data[1] != end.ptr_data[1]) |
| 2814 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2815 | |
| 2816 | CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2817 | begin.int_data, end.int_data }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2818 | return Result; |
| 2819 | } |
Douglas Gregor | ab4e83b | 2011-07-23 19:35:14 +0000 | [diff] [blame] | 2820 | |
| 2821 | unsigned clang_equalRanges(CXSourceRange range1, CXSourceRange range2) |
| 2822 | { |
| 2823 | return range1.ptr_data[0] == range2.ptr_data[0] |
| 2824 | && range1.ptr_data[1] == range2.ptr_data[1] |
| 2825 | && range1.begin_int_data == range2.begin_int_data |
| 2826 | && range1.end_int_data == range2.end_int_data; |
| 2827 | } |
Argyrios Kyrtzidis | de5db64 | 2011-09-28 18:14:21 +0000 | [diff] [blame] | 2828 | |
| 2829 | int clang_Range_isNull(CXSourceRange range) { |
| 2830 | return clang_equalRanges(range, clang_getNullRange()); |
| 2831 | } |
| 2832 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2833 | } // end: extern "C" |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2834 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2835 | static void createNullLocation(CXFile *file, unsigned *line, |
| 2836 | unsigned *column, unsigned *offset) { |
| 2837 | if (file) |
| 2838 | *file = 0; |
| 2839 | if (line) |
| 2840 | *line = 0; |
| 2841 | if (column) |
| 2842 | *column = 0; |
| 2843 | if (offset) |
| 2844 | *offset = 0; |
| 2845 | return; |
| 2846 | } |
| 2847 | |
| 2848 | extern "C" { |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2849 | void clang_getExpansionLocation(CXSourceLocation location, |
| 2850 | CXFile *file, |
| 2851 | unsigned *line, |
| 2852 | unsigned *column, |
| 2853 | unsigned *offset) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2854 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2855 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2856 | if (!location.ptr_data[0] || Loc.isInvalid()) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2857 | createNullLocation(file, line, column, offset); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2858 | return; |
| 2859 | } |
| 2860 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2861 | const SourceManager &SM = |
| 2862 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2863 | SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2864 | |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 2865 | // Check that the FileID is invalid on the expansion location. |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2866 | // This can manifest in invalid code. |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2867 | FileID fileID = SM.getFileID(ExpansionLoc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 2868 | bool Invalid = false; |
| 2869 | const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid); |
Argyrios Kyrtzidis | c705d25 | 2011-10-18 21:59:54 +0000 | [diff] [blame] | 2870 | if (Invalid || !sloc.isFile()) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2871 | createNullLocation(file, line, column, offset); |
| 2872 | return; |
| 2873 | } |
| 2874 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2875 | if (file) |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2876 | *file = (void *)SM.getFileEntryForSLocEntry(sloc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2877 | if (line) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2878 | *line = SM.getExpansionLineNumber(ExpansionLoc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2879 | if (column) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2880 | *column = SM.getExpansionColumnNumber(ExpansionLoc); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2881 | if (offset) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2882 | *offset = SM.getDecomposedLoc(ExpansionLoc).second; |
| 2883 | } |
| 2884 | |
Argyrios Kyrtzidis | e6be34d | 2011-09-13 21:49:08 +0000 | [diff] [blame] | 2885 | void clang_getPresumedLocation(CXSourceLocation location, |
| 2886 | CXString *filename, |
| 2887 | unsigned *line, |
| 2888 | unsigned *column) { |
| 2889 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2890 | |
| 2891 | if (!location.ptr_data[0] || Loc.isInvalid()) { |
| 2892 | if (filename) |
| 2893 | *filename = createCXString(""); |
| 2894 | if (line) |
| 2895 | *line = 0; |
| 2896 | if (column) |
| 2897 | *column = 0; |
| 2898 | } |
| 2899 | else { |
| 2900 | const SourceManager &SM = |
| 2901 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
| 2902 | PresumedLoc PreLoc = SM.getPresumedLoc(Loc); |
| 2903 | |
| 2904 | if (filename) |
| 2905 | *filename = createCXString(PreLoc.getFilename()); |
| 2906 | if (line) |
| 2907 | *line = PreLoc.getLine(); |
| 2908 | if (column) |
| 2909 | *column = PreLoc.getColumn(); |
| 2910 | } |
| 2911 | } |
| 2912 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 2913 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 2914 | CXFile *file, |
| 2915 | unsigned *line, |
| 2916 | unsigned *column, |
| 2917 | unsigned *offset) { |
| 2918 | // Redirect to new API. |
| 2919 | clang_getExpansionLocation(location, file, line, column, offset); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2922 | void clang_getSpellingLocation(CXSourceLocation location, |
| 2923 | CXFile *file, |
| 2924 | unsigned *line, |
| 2925 | unsigned *column, |
| 2926 | unsigned *offset) { |
| 2927 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2928 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2929 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 2930 | return createNullLocation(file, line, column, offset); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2931 | |
| 2932 | const SourceManager &SM = |
| 2933 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
| 2934 | SourceLocation SpellLoc = Loc; |
| 2935 | if (SpellLoc.isMacroID()) { |
| 2936 | SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); |
| 2937 | if (SimpleSpellingLoc.isFileID() && |
| 2938 | SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) |
| 2939 | SpellLoc = SimpleSpellingLoc; |
| 2940 | else |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2941 | SpellLoc = SM.getExpansionLoc(SpellLoc); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); |
| 2945 | FileID FID = LocInfo.first; |
| 2946 | unsigned FileOffset = LocInfo.second; |
| 2947 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2948 | if (FID.isInvalid()) |
| 2949 | return createNullLocation(file, line, column, offset); |
| 2950 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2951 | if (file) |
| 2952 | *file = (void *)SM.getFileEntryForID(FID); |
| 2953 | if (line) |
| 2954 | *line = SM.getLineNumber(FID, FileOffset); |
| 2955 | if (column) |
| 2956 | *column = SM.getColumnNumber(FID, FileOffset); |
| 2957 | if (offset) |
| 2958 | *offset = FileOffset; |
| 2959 | } |
| 2960 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2961 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2962 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2963 | range.begin_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2964 | return Result; |
| 2965 | } |
| 2966 | |
| 2967 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2968 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2969 | range.end_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2970 | return Result; |
| 2971 | } |
| 2972 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2973 | } // end: extern "C" |
| 2974 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2975 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2976 | // CXFile Operations. |
| 2977 | //===----------------------------------------------------------------------===// |
| 2978 | |
| 2979 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2980 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2981 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2982 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2983 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2984 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2985 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2989 | if (!SFile) |
| 2990 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2991 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2992 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2993 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2994 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2996 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2997 | if (!tu) |
| 2998 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2999 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3000 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3002 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 3003 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3004 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 3006 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 3007 | if (!tu || !file) |
| 3008 | return 0; |
| 3009 | |
| 3010 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 3011 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 3012 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 3013 | .isFileMultipleIncludeGuarded(FEnt); |
| 3014 | } |
| 3015 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3016 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 3017 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3018 | //===----------------------------------------------------------------------===// |
| 3019 | // CXCursor Operations. |
| 3020 | //===----------------------------------------------------------------------===// |
| 3021 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3022 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 3023 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3024 | return getDeclFromExpr(CE->getSubExpr()); |
| 3025 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3026 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 3027 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 3028 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 3029 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3030 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 3031 | return ME->getMemberDecl(); |
| 3032 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3033 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3034 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 3035 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3036 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3037 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 3038 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3039 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3040 | if (!CE->isElidable()) |
| 3041 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3042 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 3043 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3045 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 3046 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 3047 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 3048 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 3049 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3050 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3051 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 3052 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 3053 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3054 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3055 | return 0; |
| 3056 | } |
| 3057 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3058 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 3059 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 3060 | return getLocationFromExpr(CE->getSubExpr()); |
| 3061 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3062 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 3063 | return /*FIXME:*/Msg->getLeftLoc(); |
| 3064 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3065 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 3066 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 3067 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3068 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 3069 | return Member->getMemberLoc(); |
| 3070 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3071 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3072 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3073 | return SizeOfPack->getPackLoc(); |
| 3074 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3075 | return E->getLocStart(); |
| 3076 | } |
| 3077 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3078 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3079 | |
| 3080 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3081 | CXCursorVisitor visitor, |
| 3082 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 3083 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 3084 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3085 | return CursorVis.VisitChildren(parent); |
| 3086 | } |
| 3087 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3088 | #ifndef __has_feature |
| 3089 | #define __has_feature(x) 0 |
| 3090 | #endif |
| 3091 | #if __has_feature(blocks) |
| 3092 | typedef enum CXChildVisitResult |
| 3093 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 3094 | |
| 3095 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3096 | CXClientData client_data) { |
| 3097 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3098 | return block(cursor, parent); |
| 3099 | } |
| 3100 | #else |
| 3101 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 3102 | // define and call the block manually, so the |
| 3103 | typedef struct _CXChildVisitResult |
| 3104 | { |
| 3105 | void *isa; |
| 3106 | int flags; |
| 3107 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3108 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 3109 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3110 | } *CXCursorVisitorBlock; |
| 3111 | |
| 3112 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3113 | CXClientData client_data) { |
| 3114 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3115 | return block->invoke(block, cursor, parent); |
| 3116 | } |
| 3117 | #endif |
| 3118 | |
| 3119 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3120 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 3121 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3122 | return clang_visitChildren(parent, visitWithBlock, block); |
| 3123 | } |
| 3124 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3125 | static CXString getDeclSpelling(Decl *D) { |
| 3126 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3127 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3128 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3129 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3130 | return createCXString(Property->getIdentifier()->getName()); |
| 3131 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3132 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3135 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3136 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3137 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3138 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 3139 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 3140 | // and returns different names. NamedDecl returns the class name and |
| 3141 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3142 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3143 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3144 | if (isa<UsingDirectiveDecl>(D)) |
| 3145 | return createCXString(""); |
| 3146 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 3147 | llvm::SmallString<1024> S; |
| 3148 | llvm::raw_svector_ostream os(S); |
| 3149 | ND->printName(os); |
| 3150 | |
| 3151 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3152 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3153 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3154 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3155 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3156 | return clang_getTranslationUnitSpelling( |
| 3157 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3158 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3159 | if (clang_isReference(C.kind)) { |
| 3160 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3161 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 3162 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3163 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3164 | } |
| 3165 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 3166 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3167 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3168 | } |
| 3169 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 3170 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3171 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3172 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3173 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3174 | case CXCursor_CXXBaseSpecifier: { |
| 3175 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 3176 | return createCXString(B->getType().getAsString()); |
| 3177 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3178 | case CXCursor_TypeRef: { |
| 3179 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 3180 | assert(Type && "Missing type decl"); |
| 3181 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3182 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 3183 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3184 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3185 | case CXCursor_TemplateRef: { |
| 3186 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3187 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3188 | |
| 3189 | return createCXString(Template->getNameAsString()); |
| 3190 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3191 | |
| 3192 | case CXCursor_NamespaceRef: { |
| 3193 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 3194 | assert(NS && "Missing namespace decl"); |
| 3195 | |
| 3196 | return createCXString(NS->getNameAsString()); |
| 3197 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3198 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3199 | case CXCursor_MemberRef: { |
| 3200 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3201 | assert(Field && "Missing member decl"); |
| 3202 | |
| 3203 | return createCXString(Field->getNameAsString()); |
| 3204 | } |
| 3205 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3206 | case CXCursor_LabelRef: { |
| 3207 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3208 | assert(Label && "Missing label"); |
| 3209 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3210 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3213 | case CXCursor_OverloadedDeclRef: { |
| 3214 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3215 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3216 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3217 | return createCXString(ND->getNameAsString()); |
| 3218 | return createCXString(""); |
| 3219 | } |
| 3220 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3221 | return createCXString(E->getName().getAsString()); |
| 3222 | OverloadedTemplateStorage *Ovl |
| 3223 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3224 | if (Ovl->size() == 0) |
| 3225 | return createCXString(""); |
| 3226 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3227 | } |
| 3228 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3229 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3230 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3231 | } |
| 3232 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3233 | |
| 3234 | if (clang_isExpression(C.kind)) { |
| 3235 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3236 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3237 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3238 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3241 | if (clang_isStatement(C.kind)) { |
| 3242 | Stmt *S = getCursorStmt(C); |
| 3243 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3244 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3245 | |
| 3246 | return createCXString(""); |
| 3247 | } |
| 3248 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3249 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3250 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3251 | ->getNameStart()); |
| 3252 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3253 | if (C.kind == CXCursor_MacroDefinition) |
| 3254 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3255 | ->getNameStart()); |
| 3256 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3257 | if (C.kind == CXCursor_InclusionDirective) |
| 3258 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3259 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3260 | if (clang_isDeclaration(C.kind)) |
| 3261 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3262 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3263 | if (C.kind == CXCursor_AnnotateAttr) { |
| 3264 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 3265 | return createCXString(AA->getAnnotation()); |
| 3266 | } |
| 3267 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3268 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3271 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3272 | if (!clang_isDeclaration(C.kind)) |
| 3273 | return clang_getCursorSpelling(C); |
| 3274 | |
| 3275 | Decl *D = getCursorDecl(C); |
| 3276 | if (!D) |
| 3277 | return createCXString(""); |
| 3278 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3279 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3280 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3281 | D = FunTmpl->getTemplatedDecl(); |
| 3282 | |
| 3283 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 3284 | llvm::SmallString<64> Str; |
| 3285 | llvm::raw_svector_ostream OS(Str); |
| 3286 | OS << Function->getNameAsString(); |
| 3287 | if (Function->getPrimaryTemplate()) |
| 3288 | OS << "<>"; |
| 3289 | OS << "("; |
| 3290 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3291 | if (I) |
| 3292 | OS << ", "; |
| 3293 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3294 | } |
| 3295 | |
| 3296 | if (Function->isVariadic()) { |
| 3297 | if (Function->getNumParams()) |
| 3298 | OS << ", "; |
| 3299 | OS << "..."; |
| 3300 | } |
| 3301 | OS << ")"; |
| 3302 | return createCXString(OS.str()); |
| 3303 | } |
| 3304 | |
| 3305 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 3306 | llvm::SmallString<64> Str; |
| 3307 | llvm::raw_svector_ostream OS(Str); |
| 3308 | OS << ClassTemplate->getNameAsString(); |
| 3309 | OS << "<"; |
| 3310 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3311 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3312 | if (I) |
| 3313 | OS << ", "; |
| 3314 | |
| 3315 | NamedDecl *Param = Params->getParam(I); |
| 3316 | if (Param->getIdentifier()) { |
| 3317 | OS << Param->getIdentifier()->getName(); |
| 3318 | continue; |
| 3319 | } |
| 3320 | |
| 3321 | // There is no parameter name, which makes this tricky. Try to come up |
| 3322 | // with something useful that isn't too long. |
| 3323 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3324 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3325 | else if (NonTypeTemplateParmDecl *NTTP |
| 3326 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3327 | OS << NTTP->getType().getAsString(Policy); |
| 3328 | else |
| 3329 | OS << "template<...> class"; |
| 3330 | } |
| 3331 | |
| 3332 | OS << ">"; |
| 3333 | return createCXString(OS.str()); |
| 3334 | } |
| 3335 | |
| 3336 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3337 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3338 | // If the type was explicitly written, use that. |
| 3339 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3340 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3341 | |
| 3342 | llvm::SmallString<64> Str; |
| 3343 | llvm::raw_svector_ostream OS(Str); |
| 3344 | OS << ClassSpec->getNameAsString(); |
| 3345 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3346 | ClassSpec->getTemplateArgs().data(), |
| 3347 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3348 | Policy); |
| 3349 | return createCXString(OS.str()); |
| 3350 | } |
| 3351 | |
| 3352 | return clang_getCursorSpelling(C); |
| 3353 | } |
| 3354 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3355 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3356 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3357 | case CXCursor_FunctionDecl: |
| 3358 | return createCXString("FunctionDecl"); |
| 3359 | case CXCursor_TypedefDecl: |
| 3360 | return createCXString("TypedefDecl"); |
| 3361 | case CXCursor_EnumDecl: |
| 3362 | return createCXString("EnumDecl"); |
| 3363 | case CXCursor_EnumConstantDecl: |
| 3364 | return createCXString("EnumConstantDecl"); |
| 3365 | case CXCursor_StructDecl: |
| 3366 | return createCXString("StructDecl"); |
| 3367 | case CXCursor_UnionDecl: |
| 3368 | return createCXString("UnionDecl"); |
| 3369 | case CXCursor_ClassDecl: |
| 3370 | return createCXString("ClassDecl"); |
| 3371 | case CXCursor_FieldDecl: |
| 3372 | return createCXString("FieldDecl"); |
| 3373 | case CXCursor_VarDecl: |
| 3374 | return createCXString("VarDecl"); |
| 3375 | case CXCursor_ParmDecl: |
| 3376 | return createCXString("ParmDecl"); |
| 3377 | case CXCursor_ObjCInterfaceDecl: |
| 3378 | return createCXString("ObjCInterfaceDecl"); |
| 3379 | case CXCursor_ObjCCategoryDecl: |
| 3380 | return createCXString("ObjCCategoryDecl"); |
| 3381 | case CXCursor_ObjCProtocolDecl: |
| 3382 | return createCXString("ObjCProtocolDecl"); |
| 3383 | case CXCursor_ObjCPropertyDecl: |
| 3384 | return createCXString("ObjCPropertyDecl"); |
| 3385 | case CXCursor_ObjCIvarDecl: |
| 3386 | return createCXString("ObjCIvarDecl"); |
| 3387 | case CXCursor_ObjCInstanceMethodDecl: |
| 3388 | return createCXString("ObjCInstanceMethodDecl"); |
| 3389 | case CXCursor_ObjCClassMethodDecl: |
| 3390 | return createCXString("ObjCClassMethodDecl"); |
| 3391 | case CXCursor_ObjCImplementationDecl: |
| 3392 | return createCXString("ObjCImplementationDecl"); |
| 3393 | case CXCursor_ObjCCategoryImplDecl: |
| 3394 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3395 | case CXCursor_CXXMethod: |
| 3396 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3397 | case CXCursor_UnexposedDecl: |
| 3398 | return createCXString("UnexposedDecl"); |
| 3399 | case CXCursor_ObjCSuperClassRef: |
| 3400 | return createCXString("ObjCSuperClassRef"); |
| 3401 | case CXCursor_ObjCProtocolRef: |
| 3402 | return createCXString("ObjCProtocolRef"); |
| 3403 | case CXCursor_ObjCClassRef: |
| 3404 | return createCXString("ObjCClassRef"); |
| 3405 | case CXCursor_TypeRef: |
| 3406 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3407 | case CXCursor_TemplateRef: |
| 3408 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3409 | case CXCursor_NamespaceRef: |
| 3410 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3411 | case CXCursor_MemberRef: |
| 3412 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3413 | case CXCursor_LabelRef: |
| 3414 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3415 | case CXCursor_OverloadedDeclRef: |
| 3416 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3417 | case CXCursor_IntegerLiteral: |
| 3418 | return createCXString("IntegerLiteral"); |
| 3419 | case CXCursor_FloatingLiteral: |
| 3420 | return createCXString("FloatingLiteral"); |
| 3421 | case CXCursor_ImaginaryLiteral: |
| 3422 | return createCXString("ImaginaryLiteral"); |
| 3423 | case CXCursor_StringLiteral: |
| 3424 | return createCXString("StringLiteral"); |
| 3425 | case CXCursor_CharacterLiteral: |
| 3426 | return createCXString("CharacterLiteral"); |
| 3427 | case CXCursor_ParenExpr: |
| 3428 | return createCXString("ParenExpr"); |
| 3429 | case CXCursor_UnaryOperator: |
| 3430 | return createCXString("UnaryOperator"); |
| 3431 | case CXCursor_ArraySubscriptExpr: |
| 3432 | return createCXString("ArraySubscriptExpr"); |
| 3433 | case CXCursor_BinaryOperator: |
| 3434 | return createCXString("BinaryOperator"); |
| 3435 | case CXCursor_CompoundAssignOperator: |
| 3436 | return createCXString("CompoundAssignOperator"); |
| 3437 | case CXCursor_ConditionalOperator: |
| 3438 | return createCXString("ConditionalOperator"); |
| 3439 | case CXCursor_CStyleCastExpr: |
| 3440 | return createCXString("CStyleCastExpr"); |
| 3441 | case CXCursor_CompoundLiteralExpr: |
| 3442 | return createCXString("CompoundLiteralExpr"); |
| 3443 | case CXCursor_InitListExpr: |
| 3444 | return createCXString("InitListExpr"); |
| 3445 | case CXCursor_AddrLabelExpr: |
| 3446 | return createCXString("AddrLabelExpr"); |
| 3447 | case CXCursor_StmtExpr: |
| 3448 | return createCXString("StmtExpr"); |
| 3449 | case CXCursor_GenericSelectionExpr: |
| 3450 | return createCXString("GenericSelectionExpr"); |
| 3451 | case CXCursor_GNUNullExpr: |
| 3452 | return createCXString("GNUNullExpr"); |
| 3453 | case CXCursor_CXXStaticCastExpr: |
| 3454 | return createCXString("CXXStaticCastExpr"); |
| 3455 | case CXCursor_CXXDynamicCastExpr: |
| 3456 | return createCXString("CXXDynamicCastExpr"); |
| 3457 | case CXCursor_CXXReinterpretCastExpr: |
| 3458 | return createCXString("CXXReinterpretCastExpr"); |
| 3459 | case CXCursor_CXXConstCastExpr: |
| 3460 | return createCXString("CXXConstCastExpr"); |
| 3461 | case CXCursor_CXXFunctionalCastExpr: |
| 3462 | return createCXString("CXXFunctionalCastExpr"); |
| 3463 | case CXCursor_CXXTypeidExpr: |
| 3464 | return createCXString("CXXTypeidExpr"); |
| 3465 | case CXCursor_CXXBoolLiteralExpr: |
| 3466 | return createCXString("CXXBoolLiteralExpr"); |
| 3467 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3468 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3469 | case CXCursor_CXXThisExpr: |
| 3470 | return createCXString("CXXThisExpr"); |
| 3471 | case CXCursor_CXXThrowExpr: |
| 3472 | return createCXString("CXXThrowExpr"); |
| 3473 | case CXCursor_CXXNewExpr: |
| 3474 | return createCXString("CXXNewExpr"); |
| 3475 | case CXCursor_CXXDeleteExpr: |
| 3476 | return createCXString("CXXDeleteExpr"); |
| 3477 | case CXCursor_UnaryExpr: |
| 3478 | return createCXString("UnaryExpr"); |
| 3479 | case CXCursor_ObjCStringLiteral: |
| 3480 | return createCXString("ObjCStringLiteral"); |
| 3481 | case CXCursor_ObjCEncodeExpr: |
| 3482 | return createCXString("ObjCEncodeExpr"); |
| 3483 | case CXCursor_ObjCSelectorExpr: |
| 3484 | return createCXString("ObjCSelectorExpr"); |
| 3485 | case CXCursor_ObjCProtocolExpr: |
| 3486 | return createCXString("ObjCProtocolExpr"); |
| 3487 | case CXCursor_ObjCBridgedCastExpr: |
| 3488 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3489 | case CXCursor_BlockExpr: |
| 3490 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3491 | case CXCursor_PackExpansionExpr: |
| 3492 | return createCXString("PackExpansionExpr"); |
| 3493 | case CXCursor_SizeOfPackExpr: |
| 3494 | return createCXString("SizeOfPackExpr"); |
| 3495 | case CXCursor_UnexposedExpr: |
| 3496 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3497 | case CXCursor_DeclRefExpr: |
| 3498 | return createCXString("DeclRefExpr"); |
| 3499 | case CXCursor_MemberRefExpr: |
| 3500 | return createCXString("MemberRefExpr"); |
| 3501 | case CXCursor_CallExpr: |
| 3502 | return createCXString("CallExpr"); |
| 3503 | case CXCursor_ObjCMessageExpr: |
| 3504 | return createCXString("ObjCMessageExpr"); |
| 3505 | case CXCursor_UnexposedStmt: |
| 3506 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3507 | case CXCursor_DeclStmt: |
| 3508 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3509 | case CXCursor_LabelStmt: |
| 3510 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3511 | case CXCursor_CompoundStmt: |
| 3512 | return createCXString("CompoundStmt"); |
| 3513 | case CXCursor_CaseStmt: |
| 3514 | return createCXString("CaseStmt"); |
| 3515 | case CXCursor_DefaultStmt: |
| 3516 | return createCXString("DefaultStmt"); |
| 3517 | case CXCursor_IfStmt: |
| 3518 | return createCXString("IfStmt"); |
| 3519 | case CXCursor_SwitchStmt: |
| 3520 | return createCXString("SwitchStmt"); |
| 3521 | case CXCursor_WhileStmt: |
| 3522 | return createCXString("WhileStmt"); |
| 3523 | case CXCursor_DoStmt: |
| 3524 | return createCXString("DoStmt"); |
| 3525 | case CXCursor_ForStmt: |
| 3526 | return createCXString("ForStmt"); |
| 3527 | case CXCursor_GotoStmt: |
| 3528 | return createCXString("GotoStmt"); |
| 3529 | case CXCursor_IndirectGotoStmt: |
| 3530 | return createCXString("IndirectGotoStmt"); |
| 3531 | case CXCursor_ContinueStmt: |
| 3532 | return createCXString("ContinueStmt"); |
| 3533 | case CXCursor_BreakStmt: |
| 3534 | return createCXString("BreakStmt"); |
| 3535 | case CXCursor_ReturnStmt: |
| 3536 | return createCXString("ReturnStmt"); |
| 3537 | case CXCursor_AsmStmt: |
| 3538 | return createCXString("AsmStmt"); |
| 3539 | case CXCursor_ObjCAtTryStmt: |
| 3540 | return createCXString("ObjCAtTryStmt"); |
| 3541 | case CXCursor_ObjCAtCatchStmt: |
| 3542 | return createCXString("ObjCAtCatchStmt"); |
| 3543 | case CXCursor_ObjCAtFinallyStmt: |
| 3544 | return createCXString("ObjCAtFinallyStmt"); |
| 3545 | case CXCursor_ObjCAtThrowStmt: |
| 3546 | return createCXString("ObjCAtThrowStmt"); |
| 3547 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3548 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3549 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3550 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3551 | case CXCursor_ObjCForCollectionStmt: |
| 3552 | return createCXString("ObjCForCollectionStmt"); |
| 3553 | case CXCursor_CXXCatchStmt: |
| 3554 | return createCXString("CXXCatchStmt"); |
| 3555 | case CXCursor_CXXTryStmt: |
| 3556 | return createCXString("CXXTryStmt"); |
| 3557 | case CXCursor_CXXForRangeStmt: |
| 3558 | return createCXString("CXXForRangeStmt"); |
| 3559 | case CXCursor_SEHTryStmt: |
| 3560 | return createCXString("SEHTryStmt"); |
| 3561 | case CXCursor_SEHExceptStmt: |
| 3562 | return createCXString("SEHExceptStmt"); |
| 3563 | case CXCursor_SEHFinallyStmt: |
| 3564 | return createCXString("SEHFinallyStmt"); |
| 3565 | case CXCursor_NullStmt: |
| 3566 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3567 | case CXCursor_InvalidFile: |
| 3568 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3569 | case CXCursor_InvalidCode: |
| 3570 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3571 | case CXCursor_NoDeclFound: |
| 3572 | return createCXString("NoDeclFound"); |
| 3573 | case CXCursor_NotImplemented: |
| 3574 | return createCXString("NotImplemented"); |
| 3575 | case CXCursor_TranslationUnit: |
| 3576 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3577 | case CXCursor_UnexposedAttr: |
| 3578 | return createCXString("UnexposedAttr"); |
| 3579 | case CXCursor_IBActionAttr: |
| 3580 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3581 | case CXCursor_IBOutletAttr: |
| 3582 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3583 | case CXCursor_IBOutletCollectionAttr: |
| 3584 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3585 | case CXCursor_CXXFinalAttr: |
| 3586 | return createCXString("attribute(final)"); |
| 3587 | case CXCursor_CXXOverrideAttr: |
| 3588 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3589 | case CXCursor_AnnotateAttr: |
| 3590 | return createCXString("attribute(annotate)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3591 | case CXCursor_PreprocessingDirective: |
| 3592 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3593 | case CXCursor_MacroDefinition: |
| 3594 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3595 | case CXCursor_MacroExpansion: |
| 3596 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3597 | case CXCursor_InclusionDirective: |
| 3598 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3599 | case CXCursor_Namespace: |
| 3600 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3601 | case CXCursor_LinkageSpec: |
| 3602 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3603 | case CXCursor_CXXBaseSpecifier: |
| 3604 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3605 | case CXCursor_Constructor: |
| 3606 | return createCXString("CXXConstructor"); |
| 3607 | case CXCursor_Destructor: |
| 3608 | return createCXString("CXXDestructor"); |
| 3609 | case CXCursor_ConversionFunction: |
| 3610 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3611 | case CXCursor_TemplateTypeParameter: |
| 3612 | return createCXString("TemplateTypeParameter"); |
| 3613 | case CXCursor_NonTypeTemplateParameter: |
| 3614 | return createCXString("NonTypeTemplateParameter"); |
| 3615 | case CXCursor_TemplateTemplateParameter: |
| 3616 | return createCXString("TemplateTemplateParameter"); |
| 3617 | case CXCursor_FunctionTemplate: |
| 3618 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3619 | case CXCursor_ClassTemplate: |
| 3620 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3621 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3622 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3623 | case CXCursor_NamespaceAlias: |
| 3624 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3625 | case CXCursor_UsingDirective: |
| 3626 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3627 | case CXCursor_UsingDeclaration: |
| 3628 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3629 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3630 | return createCXString("TypeAliasDecl"); |
| 3631 | case CXCursor_ObjCSynthesizeDecl: |
| 3632 | return createCXString("ObjCSynthesizeDecl"); |
| 3633 | case CXCursor_ObjCDynamicDecl: |
| 3634 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3635 | case CXCursor_CXXAccessSpecifier: |
| 3636 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3637 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3638 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3639 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3640 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3641 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3642 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3643 | struct GetCursorData { |
| 3644 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3645 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3646 | CXCursor &BestCursor; |
| 3647 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3648 | GetCursorData(SourceManager &SM, |
| 3649 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3650 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3651 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3652 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3653 | }; |
| 3654 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3655 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3656 | CXCursor parent, |
| 3657 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3658 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3659 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3660 | |
| 3661 | // If we point inside a macro argument we should provide info of what the |
| 3662 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3663 | // cursor. |
| 3664 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3665 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3666 | |
| 3667 | if (clang_isDeclaration(cursor.kind)) { |
| 3668 | // Avoid having the implicit methods override the property decls. |
| 3669 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCursorDecl(cursor))) |
| 3670 | if (MD->isImplicit()) |
| 3671 | return CXChildVisit_Break; |
| 3672 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3673 | |
| 3674 | if (clang_isExpression(cursor.kind) && |
| 3675 | clang_isDeclaration(BestCursor->kind)) { |
| 3676 | Decl *D = getCursorDecl(*BestCursor); |
| 3677 | |
| 3678 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3679 | // when the expression source range overlaps the declaration range. |
| 3680 | // This can happen for C++ constructor expressions whose range generally |
| 3681 | // include the variable declaration, e.g.: |
| 3682 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3683 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3684 | D->getLocation() == Data->TokenBeginLoc) |
| 3685 | return CXChildVisit_Break; |
| 3686 | } |
| 3687 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3688 | // If our current best cursor is the construction of a temporary object, |
| 3689 | // don't replace that cursor with a type reference, because we want |
| 3690 | // clang_getCursor() to point at the constructor. |
| 3691 | if (clang_isExpression(BestCursor->kind) && |
| 3692 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3693 | cursor.kind == CXCursor_TypeRef) { |
| 3694 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3695 | // as having the actual point on the type reference. |
| 3696 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3697 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3698 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3700 | *BestCursor = cursor; |
| 3701 | return CXChildVisit_Recurse; |
| 3702 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3704 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3705 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3706 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3707 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3708 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3709 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3710 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3711 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3712 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3714 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3715 | if (Logging) { |
| 3716 | CXFile SearchFile; |
| 3717 | unsigned SearchLine, SearchColumn; |
| 3718 | CXFile ResultFile; |
| 3719 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3720 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3721 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3722 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3723 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3724 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3725 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3726 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3727 | SearchFileName = clang_getFileName(SearchFile); |
| 3728 | ResultFileName = clang_getFileName(ResultFile); |
| 3729 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3730 | USR = clang_getCursorUSR(Result); |
| 3731 | 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] | 3732 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3733 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3734 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3735 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3736 | clang_disposeString(SearchFileName); |
| 3737 | clang_disposeString(ResultFileName); |
| 3738 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3739 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3740 | |
| 3741 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3742 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3743 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3744 | CXString DefinitionKindSpelling |
| 3745 | = clang_getCursorKindSpelling(Definition.kind); |
| 3746 | CXFile DefinitionFile; |
| 3747 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3748 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3749 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3750 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3751 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3752 | clang_getCString(DefinitionKindSpelling), |
| 3753 | clang_getCString(DefinitionFileName), |
| 3754 | DefinitionLine, DefinitionColumn); |
| 3755 | clang_disposeString(DefinitionFileName); |
| 3756 | clang_disposeString(DefinitionKindSpelling); |
| 3757 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3760 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3763 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3764 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3768 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3769 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3771 | unsigned clang_hashCursor(CXCursor C) { |
| 3772 | unsigned Index = 0; |
| 3773 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3774 | Index = 1; |
| 3775 | |
| 3776 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3777 | std::make_pair(C.kind, C.data[Index])); |
| 3778 | } |
| 3779 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3780 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3781 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3782 | } |
| 3783 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3784 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3785 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3786 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3787 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3788 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3789 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3790 | } |
| 3791 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3792 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3793 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3794 | } |
| 3795 | |
| 3796 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3797 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3798 | } |
| 3799 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3800 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3801 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3802 | } |
| 3803 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3804 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3805 | return K == CXCursor_TranslationUnit; |
| 3806 | } |
| 3807 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3808 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3809 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3810 | } |
| 3811 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3812 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3813 | switch (K) { |
| 3814 | case CXCursor_UnexposedDecl: |
| 3815 | case CXCursor_UnexposedExpr: |
| 3816 | case CXCursor_UnexposedStmt: |
| 3817 | case CXCursor_UnexposedAttr: |
| 3818 | return true; |
| 3819 | default: |
| 3820 | return false; |
| 3821 | } |
| 3822 | } |
| 3823 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3824 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3825 | return C.kind; |
| 3826 | } |
| 3827 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3828 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3829 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3830 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3831 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3832 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3833 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3834 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3837 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3838 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3839 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3840 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3843 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3844 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3845 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3846 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3847 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3848 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3849 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3850 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3851 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3852 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3853 | |
| 3854 | case CXCursor_TemplateRef: { |
| 3855 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3856 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3857 | } |
| 3858 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3859 | case CXCursor_NamespaceRef: { |
| 3860 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3861 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3862 | } |
| 3863 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3864 | case CXCursor_MemberRef: { |
| 3865 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3866 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3867 | } |
| 3868 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3869 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3870 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3871 | if (!BaseSpec) |
| 3872 | return clang_getNullLocation(); |
| 3873 | |
| 3874 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3875 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3876 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3877 | |
| 3878 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3879 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3880 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3882 | case CXCursor_LabelRef: { |
| 3883 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3884 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3885 | } |
| 3886 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3887 | case CXCursor_OverloadedDeclRef: |
| 3888 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3889 | getCursorOverloadedDeclRef(C).second); |
| 3890 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3891 | default: |
| 3892 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3893 | llvm_unreachable("Missed a reference kind"); |
| 3894 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3895 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3896 | |
| 3897 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3898 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3899 | getLocationFromExpr(getCursorExpr(C))); |
| 3900 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3901 | if (clang_isStatement(C.kind)) |
| 3902 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3903 | getCursorStmt(C)->getLocStart()); |
| 3904 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3905 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3906 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3907 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3908 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3909 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3910 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3911 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3912 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3913 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3914 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3915 | |
| 3916 | if (C.kind == CXCursor_MacroDefinition) { |
| 3917 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3918 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3919 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3920 | |
| 3921 | if (C.kind == CXCursor_InclusionDirective) { |
| 3922 | SourceLocation L |
| 3923 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3924 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3925 | } |
| 3926 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3927 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3928 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3929 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3930 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3931 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3932 | // FIXME: Multiple variables declared in a single declaration |
| 3933 | // currently lack the information needed to correctly determine their |
| 3934 | // ranges when accounting for the type-specifier. We use context |
| 3935 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3936 | // and if so, whether it is the first decl. |
| 3937 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3938 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3939 | Loc = VD->getLocation(); |
| 3940 | } |
| 3941 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3942 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3943 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3945 | } // end extern "C" |
| 3946 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3947 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3948 | assert(TU); |
| 3949 | |
| 3950 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3951 | // of the following calls. |
| 3952 | if (SLoc.isInvalid()) |
| 3953 | return clang_getNullCursor(); |
| 3954 | |
| 3955 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3956 | |
| 3957 | // Translate the given source location to make it point at the beginning of |
| 3958 | // the token under the cursor. |
| 3959 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3960 | CXXUnit->getASTContext().getLangOptions()); |
| 3961 | |
| 3962 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3963 | if (SLoc.isValid()) { |
| 3964 | // FIXME: Would be great to have a "hint" cursor, then walk from that |
| 3965 | // hint cursor upward until we find a cursor whose source range encloses |
| 3966 | // the region of interest, rather than starting from the translation unit. |
| 3967 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 3968 | CXCursor Parent = clang_getTranslationUnitCursor(TU); |
| 3969 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3970 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 3971 | /*VisitIncludedPreprocessingEntries=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3972 | SourceLocation(SLoc)); |
| 3973 | CursorVis.VisitChildren(Parent); |
| 3974 | } |
| 3975 | |
| 3976 | return Result; |
| 3977 | } |
| 3978 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3979 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3980 | if (clang_isReference(C.kind)) { |
| 3981 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3982 | case CXCursor_ObjCSuperClassRef: |
| 3983 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3985 | case CXCursor_ObjCProtocolRef: |
| 3986 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3988 | case CXCursor_ObjCClassRef: |
| 3989 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3991 | case CXCursor_TypeRef: |
| 3992 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3993 | |
| 3994 | case CXCursor_TemplateRef: |
| 3995 | return getCursorTemplateRef(C).second; |
| 3996 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3997 | case CXCursor_NamespaceRef: |
| 3998 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3999 | |
| 4000 | case CXCursor_MemberRef: |
| 4001 | return getCursorMemberRef(C).second; |
| 4002 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4003 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 4004 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4005 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4006 | case CXCursor_LabelRef: |
| 4007 | return getCursorLabelRef(C).second; |
| 4008 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4009 | case CXCursor_OverloadedDeclRef: |
| 4010 | return getCursorOverloadedDeclRef(C).second; |
| 4011 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4012 | default: |
| 4013 | // FIXME: Need a way to enumerate all non-reference cases. |
| 4014 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4017 | |
| 4018 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4019 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 4020 | |
| 4021 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4022 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4023 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 4024 | if (clang_isAttribute(C.kind)) |
| 4025 | return getCursorAttr(C)->getRange(); |
| 4026 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4027 | if (C.kind == CXCursor_PreprocessingDirective) |
| 4028 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 4029 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4030 | if (C.kind == CXCursor_MacroExpansion) { |
| 4031 | ASTUnit *TU = getCursorASTUnit(C); |
| 4032 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 4033 | return TU->mapRangeFromPreamble(Range); |
| 4034 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 4035 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4036 | if (C.kind == CXCursor_MacroDefinition) { |
| 4037 | ASTUnit *TU = getCursorASTUnit(C); |
| 4038 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 4039 | return TU->mapRangeFromPreamble(Range); |
| 4040 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 4041 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4042 | if (C.kind == CXCursor_InclusionDirective) { |
| 4043 | ASTUnit *TU = getCursorASTUnit(C); |
| 4044 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 4045 | return TU->mapRangeFromPreamble(Range); |
| 4046 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 4047 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 4048 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 4049 | Decl *D = cxcursor::getCursorDecl(C); |
| 4050 | SourceRange R = D->getSourceRange(); |
| 4051 | // FIXME: Multiple variables declared in a single declaration |
| 4052 | // currently lack the information needed to correctly determine their |
| 4053 | // ranges when accounting for the type-specifier. We use context |
| 4054 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 4055 | // and if so, whether it is the first decl. |
| 4056 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4057 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 4058 | R.setBegin(VD->getLocation()); |
| 4059 | } |
| 4060 | return R; |
| 4061 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4062 | return SourceRange(); |
| 4063 | } |
| 4064 | |
| 4065 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 4066 | /// the decl-specifier-seq for declarations. |
| 4067 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 4068 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 4069 | Decl *D = cxcursor::getCursorDecl(C); |
| 4070 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4071 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4072 | // Adjust the start of the location for declarations preceded by |
| 4073 | // declaration specifiers. |
| 4074 | SourceLocation StartLoc; |
| 4075 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 4076 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4077 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4078 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4079 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4080 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4081 | } |
| 4082 | |
| 4083 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 4084 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 4085 | R.setBegin(StartLoc); |
| 4086 | |
| 4087 | // FIXME: Multiple variables declared in a single declaration |
| 4088 | // currently lack the information needed to correctly determine their |
| 4089 | // ranges when accounting for the type-specifier. We use context |
| 4090 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 4091 | // and if so, whether it is the first decl. |
| 4092 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4093 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 4094 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | return R; |
| 4098 | } |
| 4099 | |
| 4100 | return getRawCursorExtent(C); |
| 4101 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4102 | |
| 4103 | extern "C" { |
| 4104 | |
| 4105 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 4106 | SourceRange R = getRawCursorExtent(C); |
| 4107 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4108 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4110 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4111 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4112 | |
| 4113 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4114 | if (clang_isInvalid(C.kind)) |
| 4115 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4116 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4117 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4118 | if (clang_isDeclaration(C.kind)) { |
| 4119 | Decl *D = getCursorDecl(C); |
| 4120 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4121 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4122 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4123 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4124 | if (ObjCForwardProtocolDecl *Protocols |
| 4125 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4126 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4127 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 4128 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 4129 | return MakeCXCursor(Property, tu); |
| 4130 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4131 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4134 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4135 | Expr *E = getCursorExpr(C); |
| 4136 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 4137 | if (D) { |
| 4138 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 4139 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 4140 | declCursor); |
| 4141 | return declCursor; |
| 4142 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4143 | |
| 4144 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4145 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4146 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4147 | return clang_getNullCursor(); |
| 4148 | } |
| 4149 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4150 | if (clang_isStatement(C.kind)) { |
| 4151 | Stmt *S = getCursorStmt(C); |
| 4152 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 4153 | if (LabelDecl *label = Goto->getLabel()) |
| 4154 | if (LabelStmt *labelS = label->getStmt()) |
| 4155 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4156 | |
| 4157 | return clang_getNullCursor(); |
| 4158 | } |
| 4159 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4160 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 4161 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4162 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4165 | if (!clang_isReference(C.kind)) |
| 4166 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4167 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4168 | switch (C.kind) { |
| 4169 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4170 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4171 | |
| 4172 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4173 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4174 | |
| 4175 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4176 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4177 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4178 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4179 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4180 | |
| 4181 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4182 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4184 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4185 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4187 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4188 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4189 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4190 | case CXCursor_CXXBaseSpecifier: { |
| 4191 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 4192 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4193 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4194 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4195 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4196 | case CXCursor_LabelRef: |
| 4197 | // FIXME: We end up faking the "parent" declaration here because we |
| 4198 | // don't want to make CXCursor larger. |
| 4199 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4200 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 4201 | .getTranslationUnitDecl(), |
| 4202 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4204 | case CXCursor_OverloadedDeclRef: |
| 4205 | return C; |
| 4206 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4207 | default: |
| 4208 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 4209 | llvm_unreachable("Unhandled reference cursor kind"); |
| 4210 | break; |
| 4211 | } |
| 4212 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4213 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4214 | return clang_getNullCursor(); |
| 4215 | } |
| 4216 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4217 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4218 | if (clang_isInvalid(C.kind)) |
| 4219 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4220 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4221 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4222 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4223 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4224 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4225 | C = clang_getCursorReferenced(C); |
| 4226 | WasReference = true; |
| 4227 | } |
| 4228 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4229 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4230 | return clang_getCursorReferenced(C); |
| 4231 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4232 | if (!clang_isDeclaration(C.kind)) |
| 4233 | return clang_getNullCursor(); |
| 4234 | |
| 4235 | Decl *D = getCursorDecl(C); |
| 4236 | if (!D) |
| 4237 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4239 | switch (D->getKind()) { |
| 4240 | // Declaration kinds that don't really separate the notions of |
| 4241 | // declaration and definition. |
| 4242 | case Decl::Namespace: |
| 4243 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4244 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4245 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4246 | case Decl::TemplateTypeParm: |
| 4247 | case Decl::EnumConstant: |
| 4248 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4249 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4250 | case Decl::ObjCIvar: |
| 4251 | case Decl::ObjCAtDefsField: |
| 4252 | case Decl::ImplicitParam: |
| 4253 | case Decl::ParmVar: |
| 4254 | case Decl::NonTypeTemplateParm: |
| 4255 | case Decl::TemplateTemplateParm: |
| 4256 | case Decl::ObjCCategoryImpl: |
| 4257 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4258 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4259 | case Decl::LinkageSpec: |
| 4260 | case Decl::ObjCPropertyImpl: |
| 4261 | case Decl::FileScopeAsm: |
| 4262 | case Decl::StaticAssert: |
| 4263 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4264 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4265 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4266 | return C; |
| 4267 | |
| 4268 | // Declaration kinds that don't make any sense here, but are |
| 4269 | // nonetheless harmless. |
| 4270 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4271 | break; |
| 4272 | |
| 4273 | // Declaration kinds for which the definition is not resolvable. |
| 4274 | case Decl::UnresolvedUsingTypename: |
| 4275 | case Decl::UnresolvedUsingValue: |
| 4276 | break; |
| 4277 | |
| 4278 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4279 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4280 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4281 | |
| 4282 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4283 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4284 | |
| 4285 | case Decl::Enum: |
| 4286 | case Decl::Record: |
| 4287 | case Decl::CXXRecord: |
| 4288 | case Decl::ClassTemplateSpecialization: |
| 4289 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4290 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4291 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4292 | return clang_getNullCursor(); |
| 4293 | |
| 4294 | case Decl::Function: |
| 4295 | case Decl::CXXMethod: |
| 4296 | case Decl::CXXConstructor: |
| 4297 | case Decl::CXXDestructor: |
| 4298 | case Decl::CXXConversion: { |
| 4299 | const FunctionDecl *Def = 0; |
| 4300 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4301 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4302 | return clang_getNullCursor(); |
| 4303 | } |
| 4304 | |
| 4305 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4306 | // Ask the variable if it has a definition. |
| 4307 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4308 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4309 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4310 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4311 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4312 | case Decl::FunctionTemplate: { |
| 4313 | const FunctionDecl *Def = 0; |
| 4314 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4315 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4316 | return clang_getNullCursor(); |
| 4317 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4318 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4319 | case Decl::ClassTemplate: { |
| 4320 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4321 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4322 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4323 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4324 | return clang_getNullCursor(); |
| 4325 | } |
| 4326 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4327 | case Decl::Using: |
| 4328 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4329 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4330 | |
| 4331 | case Decl::UsingShadow: |
| 4332 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4333 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4334 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4335 | |
| 4336 | case Decl::ObjCMethod: { |
| 4337 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4338 | if (Method->isThisDeclarationADefinition()) |
| 4339 | return C; |
| 4340 | |
| 4341 | // Dig out the method definition in the associated |
| 4342 | // @implementation, if we have it. |
| 4343 | // FIXME: The ASTs should make finding the definition easier. |
| 4344 | if (ObjCInterfaceDecl *Class |
| 4345 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4346 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4347 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4348 | Method->isInstanceMethod())) |
| 4349 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4350 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4351 | |
| 4352 | return clang_getNullCursor(); |
| 4353 | } |
| 4354 | |
| 4355 | case Decl::ObjCCategory: |
| 4356 | if (ObjCCategoryImplDecl *Impl |
| 4357 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4358 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4359 | return clang_getNullCursor(); |
| 4360 | |
| 4361 | case Decl::ObjCProtocol: |
| 4362 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4363 | return C; |
| 4364 | return clang_getNullCursor(); |
| 4365 | |
| 4366 | case Decl::ObjCInterface: |
| 4367 | // There are two notions of a "definition" for an Objective-C |
| 4368 | // class: the interface and its implementation. When we resolved a |
| 4369 | // reference to an Objective-C class, produce the @interface as |
| 4370 | // the definition; when we were provided with the interface, |
| 4371 | // produce the @implementation as the definition. |
| 4372 | if (WasReference) { |
| 4373 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4374 | return C; |
| 4375 | } else if (ObjCImplementationDecl *Impl |
| 4376 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4377 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4378 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4380 | case Decl::ObjCProperty: |
| 4381 | // FIXME: We don't really know where to find the |
| 4382 | // ObjCPropertyImplDecls that implement this property. |
| 4383 | return clang_getNullCursor(); |
| 4384 | |
| 4385 | case Decl::ObjCCompatibleAlias: |
| 4386 | if (ObjCInterfaceDecl *Class |
| 4387 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4388 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4389 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4390 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4391 | return clang_getNullCursor(); |
| 4392 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4393 | case Decl::ObjCForwardProtocol: |
| 4394 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4395 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4396 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4397 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4398 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4399 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4400 | |
| 4401 | case Decl::Friend: |
| 4402 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4403 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4404 | return clang_getNullCursor(); |
| 4405 | |
| 4406 | case Decl::FriendTemplate: |
| 4407 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4408 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4409 | return clang_getNullCursor(); |
| 4410 | } |
| 4411 | |
| 4412 | return clang_getNullCursor(); |
| 4413 | } |
| 4414 | |
| 4415 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4416 | if (!clang_isDeclaration(C.kind)) |
| 4417 | return 0; |
| 4418 | |
| 4419 | return clang_getCursorDefinition(C) == C; |
| 4420 | } |
| 4421 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4422 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4423 | if (!clang_isDeclaration(C.kind)) |
| 4424 | return C; |
| 4425 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4426 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4427 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4428 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4429 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4430 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4431 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4432 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4433 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4434 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4435 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4436 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4437 | |
| 4438 | return C; |
| 4439 | } |
| 4440 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4441 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4442 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4443 | return 0; |
| 4444 | |
| 4445 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4446 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4447 | return E->getNumDecls(); |
| 4448 | |
| 4449 | if (OverloadedTemplateStorage *S |
| 4450 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4451 | return S->size(); |
| 4452 | |
| 4453 | Decl *D = Storage.get<Decl*>(); |
| 4454 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4455 | return Using->shadow_size(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4456 | if (isa<ObjCClassDecl>(D)) |
| 4457 | return 1; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4458 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4459 | return Protocols->protocol_size(); |
| 4460 | |
| 4461 | return 0; |
| 4462 | } |
| 4463 | |
| 4464 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4465 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4466 | return clang_getNullCursor(); |
| 4467 | |
| 4468 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4469 | return clang_getNullCursor(); |
| 4470 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4471 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4472 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4473 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4474 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4475 | |
| 4476 | if (OverloadedTemplateStorage *S |
| 4477 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4478 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4479 | |
| 4480 | Decl *D = Storage.get<Decl*>(); |
| 4481 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4482 | // FIXME: This is, unfortunately, linear time. |
| 4483 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4484 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4485 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4486 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4487 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4488 | return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4489 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4490 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4491 | |
| 4492 | return clang_getNullCursor(); |
| 4493 | } |
| 4494 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4495 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4496 | const char **startBuf, |
| 4497 | const char **endBuf, |
| 4498 | unsigned *startLine, |
| 4499 | unsigned *startColumn, |
| 4500 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4501 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4502 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4503 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4504 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4505 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4506 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4507 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4508 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4509 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4510 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4511 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4512 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4513 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4514 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4516 | |
| 4517 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4518 | unsigned PieceIndex) { |
| 4519 | RefNamePieces Pieces; |
| 4520 | |
| 4521 | switch (C.kind) { |
| 4522 | case CXCursor_MemberRefExpr: |
| 4523 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4524 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4525 | E->getQualifierLoc().getSourceRange()); |
| 4526 | break; |
| 4527 | |
| 4528 | case CXCursor_DeclRefExpr: |
| 4529 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4530 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4531 | E->getQualifierLoc().getSourceRange(), |
| 4532 | E->getExplicitTemplateArgsOpt()); |
| 4533 | break; |
| 4534 | |
| 4535 | case CXCursor_CallExpr: |
| 4536 | if (CXXOperatorCallExpr *OCE = |
| 4537 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4538 | Expr *Callee = OCE->getCallee(); |
| 4539 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4540 | Callee = ICE->getSubExpr(); |
| 4541 | |
| 4542 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4543 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4544 | DRE->getQualifierLoc().getSourceRange()); |
| 4545 | } |
| 4546 | break; |
| 4547 | |
| 4548 | default: |
| 4549 | break; |
| 4550 | } |
| 4551 | |
| 4552 | if (Pieces.empty()) { |
| 4553 | if (PieceIndex == 0) |
| 4554 | return clang_getCursorExtent(C); |
| 4555 | } else if (PieceIndex < Pieces.size()) { |
| 4556 | SourceRange R = Pieces[PieceIndex]; |
| 4557 | if (R.isValid()) |
| 4558 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4559 | } |
| 4560 | |
| 4561 | return clang_getNullRange(); |
| 4562 | } |
| 4563 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4564 | void clang_enableStackTraces(void) { |
| 4565 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4566 | } |
| 4567 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4568 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4569 | unsigned stack_size) { |
| 4570 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4571 | } |
| 4572 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4573 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4574 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4575 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4576 | // Token-based Operations. |
| 4577 | //===----------------------------------------------------------------------===// |
| 4578 | |
| 4579 | /* CXToken layout: |
| 4580 | * int_data[0]: a CXTokenKind |
| 4581 | * int_data[1]: starting token location |
| 4582 | * int_data[2]: token length |
| 4583 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4584 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4585 | * otherwise unused. |
| 4586 | */ |
| 4587 | extern "C" { |
| 4588 | |
| 4589 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4590 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4591 | } |
| 4592 | |
| 4593 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4594 | switch (clang_getTokenKind(CXTok)) { |
| 4595 | case CXToken_Identifier: |
| 4596 | case CXToken_Keyword: |
| 4597 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4598 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4599 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4600 | |
| 4601 | case CXToken_Literal: { |
| 4602 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4603 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4604 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4605 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4606 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4607 | case CXToken_Punctuation: |
| 4608 | case CXToken_Comment: |
| 4609 | break; |
| 4610 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4611 | |
| 4612 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4613 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4614 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4615 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4616 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4618 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4619 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4620 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4621 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4622 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4623 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4624 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4625 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4626 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4627 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4628 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4629 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4630 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4631 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4632 | if (!CXXUnit) |
| 4633 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4634 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4635 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4636 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4637 | } |
| 4638 | |
| 4639 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4640 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4641 | if (!CXXUnit) |
| 4642 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4643 | |
| 4644 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4645 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4646 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4647 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4648 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4649 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4650 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4651 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4652 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4653 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4654 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4656 | // Cannot tokenize across files. |
| 4657 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4658 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4659 | |
| 4660 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4661 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4662 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4663 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4664 | if (Invalid) |
| 4665 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4667 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4668 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4669 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4670 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4671 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4672 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4673 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4674 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4675 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4676 | do { |
| 4677 | // Lex the next token |
| 4678 | Lex.LexFromRawLexer(Tok); |
| 4679 | if (Tok.is(tok::eof)) |
| 4680 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4682 | // Initialize the CXToken. |
| 4683 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4684 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4685 | // - Common fields |
| 4686 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4687 | CXTok.int_data[2] = Tok.getLength(); |
| 4688 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4690 | // - Kind-specific fields |
| 4691 | if (Tok.isLiteral()) { |
| 4692 | CXTok.int_data[0] = CXToken_Literal; |
| 4693 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4694 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4695 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4696 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4697 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4698 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4699 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4700 | CXTok.int_data[0] = CXToken_Keyword; |
| 4701 | } |
| 4702 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4703 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4704 | ? CXToken_Identifier |
| 4705 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4706 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4707 | CXTok.ptr_data = II; |
| 4708 | } else if (Tok.is(tok::comment)) { |
| 4709 | CXTok.int_data[0] = CXToken_Comment; |
| 4710 | CXTok.ptr_data = 0; |
| 4711 | } else { |
| 4712 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4713 | CXTok.ptr_data = 0; |
| 4714 | } |
| 4715 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4716 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4717 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4721 | CXToken **Tokens, unsigned *NumTokens) { |
| 4722 | if (Tokens) |
| 4723 | *Tokens = 0; |
| 4724 | if (NumTokens) |
| 4725 | *NumTokens = 0; |
| 4726 | |
| 4727 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4728 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4729 | return; |
| 4730 | |
| 4731 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4732 | |
| 4733 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4734 | if (R.isInvalid()) |
| 4735 | return; |
| 4736 | |
| 4737 | SmallVector<CXToken, 32> CXTokens; |
| 4738 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4740 | if (CXTokens.empty()) |
| 4741 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4743 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4744 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4745 | *NumTokens = CXTokens.size(); |
| 4746 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4747 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4748 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4749 | CXToken *Tokens, unsigned NumTokens) { |
| 4750 | free(Tokens); |
| 4751 | } |
| 4752 | |
| 4753 | } // end: extern "C" |
| 4754 | |
| 4755 | //===----------------------------------------------------------------------===// |
| 4756 | // Token annotation APIs. |
| 4757 | //===----------------------------------------------------------------------===// |
| 4758 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4759 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4760 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4761 | CXCursor parent, |
| 4762 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4763 | namespace { |
| 4764 | class AnnotateTokensWorker { |
| 4765 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4766 | CXToken *Tokens; |
| 4767 | CXCursor *Cursors; |
| 4768 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4769 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4770 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4771 | CursorVisitor AnnotateVis; |
| 4772 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4773 | bool HasContextSensitiveKeywords; |
| 4774 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4775 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4776 | unsigned NextToken() const { return TokIdx; } |
| 4777 | void AdvanceToken() { ++TokIdx; } |
| 4778 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4779 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4780 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4781 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4782 | return Tokens[tokI].int_data[3] != 0; |
| 4783 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4784 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4785 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4786 | } |
| 4787 | |
| 4788 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4789 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4790 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4791 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4792 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4793 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4794 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4795 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4796 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4797 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4798 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4799 | AnnotateTokensVisitor, this, |
| 4800 | /*VisitPreprocessorLast=*/true, |
| 4801 | /*VisitIncludedPreprocessingEntries=*/false, |
| 4802 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4803 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4804 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4805 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4806 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4807 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4808 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4809 | void AnnotateTokens() { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4810 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4811 | } |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4812 | |
| 4813 | /// \brief Determine whether the annotator saw any cursors that have |
| 4814 | /// context-sensitive keywords. |
| 4815 | bool hasContextSensitiveKeywords() const { |
| 4816 | return HasContextSensitiveKeywords; |
| 4817 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4818 | }; |
| 4819 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4820 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4821 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 4822 | // Walk the AST within the region of interest, annotating tokens |
| 4823 | // along the way. |
| 4824 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4825 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4826 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4827 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4828 | if (Pos != Annotated.end() && |
| 4829 | (clang_isInvalid(Cursors[I].kind) || |
| 4830 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4831 | Cursors[I] = Pos->second; |
| 4832 | } |
| 4833 | |
| 4834 | // Finish up annotating any tokens left. |
| 4835 | if (!MoreTokens()) |
| 4836 | return; |
| 4837 | |
| 4838 | const CXCursor &C = clang_getNullCursor(); |
| 4839 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 4840 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4841 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4845 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4846 | //// between the cursor location and the source range is the same as |
| 4847 | /// \arg compResult. |
| 4848 | /// |
| 4849 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4850 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4851 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4852 | RangeComparisonResult compResult, |
| 4853 | SourceRange range) { |
| 4854 | while (MoreTokens()) { |
| 4855 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4856 | if (isFunctionMacroToken(I)) |
| 4857 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4858 | |
| 4859 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4860 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4861 | Cursors[I] = updateC; |
| 4862 | AdvanceToken(); |
| 4863 | continue; |
| 4864 | } |
| 4865 | break; |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4870 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4871 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4872 | RangeComparisonResult compResult, |
| 4873 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4874 | assert(MoreTokens()); |
| 4875 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4876 | "Should be called only for macro arg tokens"); |
| 4877 | |
| 4878 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4879 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4880 | // advance the token index one by one until a token fails the range test. |
| 4881 | // We only advance once past all of the macro arg tokens if all of them |
| 4882 | // pass the range test. If one of them fails we keep the token index pointing |
| 4883 | // at the start of the macro arg tokens so that the failing token will be |
| 4884 | // annotated by a subsequent annotation try. |
| 4885 | |
| 4886 | bool atLeastOneCompFail = false; |
| 4887 | |
| 4888 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4889 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4890 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4891 | if (TokLoc.isFileID()) |
| 4892 | continue; // not macro arg token, it's parens or comma. |
| 4893 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4894 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4895 | Cursors[I] = updateC; |
| 4896 | } else |
| 4897 | atLeastOneCompFail = true; |
| 4898 | } |
| 4899 | |
| 4900 | if (!atLeastOneCompFail) |
| 4901 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4902 | } |
| 4903 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4904 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4905 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4906 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4907 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4908 | if (cursorRange.isInvalid()) |
| 4909 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4910 | |
| 4911 | if (!HasContextSensitiveKeywords) { |
| 4912 | // Objective-C properties can have context-sensitive keywords. |
| 4913 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4914 | if (ObjCPropertyDecl *Property |
| 4915 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4916 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4917 | } |
| 4918 | // Objective-C methods can have context-sensitive keywords. |
| 4919 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4920 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4921 | if (ObjCMethodDecl *Method |
| 4922 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4923 | if (Method->getObjCDeclQualifier()) |
| 4924 | HasContextSensitiveKeywords = true; |
| 4925 | else { |
| 4926 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4927 | PEnd = Method->param_end(); |
| 4928 | P != PEnd; ++P) { |
| 4929 | if ((*P)->getObjCDeclQualifier()) { |
| 4930 | HasContextSensitiveKeywords = true; |
| 4931 | break; |
| 4932 | } |
| 4933 | } |
| 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | // C++ methods can have context-sensitive keywords. |
| 4938 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4939 | if (CXXMethodDecl *Method |
| 4940 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4941 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4942 | HasContextSensitiveKeywords = true; |
| 4943 | } |
| 4944 | } |
| 4945 | // C++ classes can have context-sensitive keywords. |
| 4946 | else if (cursor.kind == CXCursor_StructDecl || |
| 4947 | cursor.kind == CXCursor_ClassDecl || |
| 4948 | cursor.kind == CXCursor_ClassTemplate || |
| 4949 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4950 | if (Decl *D = getCursorDecl(cursor)) |
| 4951 | if (D->hasAttr<FinalAttr>()) |
| 4952 | HasContextSensitiveKeywords = true; |
| 4953 | } |
| 4954 | } |
| 4955 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4956 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4957 | // For macro expansions, just note where the beginning of the macro |
| 4958 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4959 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4960 | Annotated[Loc.int_data] = cursor; |
| 4961 | return CXChildVisit_Recurse; |
| 4962 | } |
| 4963 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4964 | // Items in the preprocessing record are kept separate from items in |
| 4965 | // declarations, so we keep a separate token index. |
| 4966 | unsigned SavedTokIdx = TokIdx; |
| 4967 | TokIdx = PreprocessingTokIdx; |
| 4968 | |
| 4969 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4970 | // entry. |
| 4971 | while (MoreTokens()) { |
| 4972 | const unsigned I = NextToken(); |
| 4973 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4974 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4975 | case RangeBefore: |
| 4976 | AdvanceToken(); |
| 4977 | continue; |
| 4978 | case RangeAfter: |
| 4979 | case RangeOverlap: |
| 4980 | break; |
| 4981 | } |
| 4982 | break; |
| 4983 | } |
| 4984 | |
| 4985 | // Look at all of the tokens within this range. |
| 4986 | while (MoreTokens()) { |
| 4987 | const unsigned I = NextToken(); |
| 4988 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4989 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4990 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4991 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4992 | case RangeAfter: |
| 4993 | break; |
| 4994 | case RangeOverlap: |
| 4995 | Cursors[I] = cursor; |
| 4996 | AdvanceToken(); |
| 4997 | continue; |
| 4998 | } |
| 4999 | break; |
| 5000 | } |
| 5001 | |
| 5002 | // Save the preprocessing token index; restore the non-preprocessing |
| 5003 | // token index. |
| 5004 | PreprocessingTokIdx = TokIdx; |
| 5005 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5006 | return CXChildVisit_Recurse; |
| 5007 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5008 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5009 | if (cursorRange.isInvalid()) |
| 5010 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5011 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5012 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 5013 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5014 | // Adjust the annotated range based specific declarations. |
| 5015 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 5016 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 5017 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5018 | |
| 5019 | SourceLocation StartLoc; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 5020 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5021 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 5022 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 5023 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 5024 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 5025 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5026 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5027 | |
| 5028 | if (StartLoc.isValid() && L.isValid() && |
| 5029 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 5030 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5031 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 5032 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 5033 | // If the location of the cursor occurs within a macro instantiation, record |
| 5034 | // the spelling location of the cursor in our annotation map. We can then |
| 5035 | // paper over the token labelings during a post-processing step to try and |
| 5036 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 5037 | // instantiation. |
| 5038 | if (L.isMacroID()) { |
| 5039 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 5040 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 5041 | // directive. Here we assume that the default construction of CXCursor |
| 5042 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 5043 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5044 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 5045 | CXCursor &oldC = Annotated[rawEncoding]; |
| 5046 | if (!clang_isPreprocessing(oldC.kind)) |
| 5047 | oldC = cursor; |
| 5048 | } |
| 5049 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5050 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 5051 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 5052 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 5053 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5054 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5055 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5056 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 5057 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 5058 | // variable declaration that it belongs to. |
| 5059 | // This can happen for C++ constructor expressions whose range generally |
| 5060 | // include the variable declaration, e.g.: |
| 5061 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 5062 | if (clang_isExpression(cursorK)) { |
| 5063 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 5064 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 5065 | const unsigned I = NextToken(); |
| 5066 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 5067 | E->getLocStart() == D->getLocation() && |
| 5068 | E->getLocStart() == GetTokenLoc(I)) { |
| 5069 | Cursors[I] = updateC; |
| 5070 | AdvanceToken(); |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5075 | // Visit children to get their cursor information. |
| 5076 | const unsigned BeforeChildren = NextToken(); |
| 5077 | VisitChildren(cursor); |
| 5078 | const unsigned AfterChildren = NextToken(); |
| 5079 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5080 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 5081 | // but the child cursors. |
| 5082 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5083 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5084 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 5085 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5086 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 5087 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 5088 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5089 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5090 | Cursors[I] = cursor; |
| 5091 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5092 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5093 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5096 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 5097 | CXCursor parent, |
| 5098 | CXClientData client_data) { |
| 5099 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 5100 | } |
| 5101 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5102 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5103 | |
| 5104 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 5105 | /// and mark tokens that are macro arguments. This info is used by the |
| 5106 | /// AnnotateTokensWorker. |
| 5107 | class MarkMacroArgTokensVisitor { |
| 5108 | SourceManager &SM; |
| 5109 | CXToken *Tokens; |
| 5110 | unsigned NumTokens; |
| 5111 | unsigned CurIdx; |
| 5112 | |
| 5113 | public: |
| 5114 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 5115 | CXToken *tokens, unsigned numTokens) |
| 5116 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 5117 | |
| 5118 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 5119 | if (cursor.kind != CXCursor_MacroExpansion) |
| 5120 | return CXChildVisit_Continue; |
| 5121 | |
| 5122 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 5123 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 5124 | return CXChildVisit_Continue; // it's not a function macro. |
| 5125 | |
| 5126 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5127 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 5128 | macroRange.getBegin())) |
| 5129 | break; |
| 5130 | } |
| 5131 | |
| 5132 | if (CurIdx == NumTokens) |
| 5133 | return CXChildVisit_Break; |
| 5134 | |
| 5135 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5136 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 5137 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 5138 | break; |
| 5139 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5140 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | if (CurIdx == NumTokens) |
| 5144 | return CXChildVisit_Break; |
| 5145 | |
| 5146 | return CXChildVisit_Continue; |
| 5147 | } |
| 5148 | |
| 5149 | private: |
| 5150 | SourceLocation getTokenLoc(unsigned tokI) { |
| 5151 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 5152 | } |
| 5153 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5154 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5155 | // The third field is reserved and currently not used. Use it here |
| 5156 | // to mark macro arg expanded tokens with their expanded locations. |
| 5157 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 5158 | } |
| 5159 | }; |
| 5160 | |
| 5161 | } // end anonymous namespace |
| 5162 | |
| 5163 | static CXChildVisitResult |
| 5164 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 5165 | CXClientData client_data) { |
| 5166 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 5167 | parent); |
| 5168 | } |
| 5169 | |
| 5170 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5171 | struct clang_annotateTokens_Data { |
| 5172 | CXTranslationUnit TU; |
| 5173 | ASTUnit *CXXUnit; |
| 5174 | CXToken *Tokens; |
| 5175 | unsigned NumTokens; |
| 5176 | CXCursor *Cursors; |
| 5177 | }; |
| 5178 | } |
| 5179 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5180 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 5181 | SourceRange RegionOfInterest, |
| 5182 | AnnotateTokensData &Annotated) { |
| 5183 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 5184 | |
| 5185 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 5186 | std::pair<FileID, unsigned> BeginLocInfo |
| 5187 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 5188 | std::pair<FileID, unsigned> EndLocInfo |
| 5189 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 5190 | |
| 5191 | if (BeginLocInfo.first != EndLocInfo.first) |
| 5192 | return; |
| 5193 | |
| 5194 | StringRef Buffer; |
| 5195 | bool Invalid = false; |
| 5196 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 5197 | if (Buffer.empty() || Invalid) |
| 5198 | return; |
| 5199 | |
| 5200 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 5201 | CXXUnit->getASTContext().getLangOptions(), |
| 5202 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 5203 | Buffer.end()); |
| 5204 | Lex.SetCommentRetentionState(true); |
| 5205 | |
| 5206 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 5207 | // entering #includes or expanding macros. |
| 5208 | while (true) { |
| 5209 | Token Tok; |
| 5210 | Lex.LexFromRawLexer(Tok); |
| 5211 | |
| 5212 | reprocess: |
| 5213 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 5214 | // We have found a preprocessing directive. Gobble it up so that we |
| 5215 | // don't see it while preprocessing these tokens later, but keep track |
| 5216 | // of all of the token locations inside this preprocessing directive so |
| 5217 | // that we can annotate them appropriately. |
| 5218 | // |
| 5219 | // FIXME: Some simple tests here could identify macro definitions and |
| 5220 | // #undefs, to provide specific cursor kinds for those. |
| 5221 | SmallVector<SourceLocation, 32> Locations; |
| 5222 | do { |
| 5223 | Locations.push_back(Tok.getLocation()); |
| 5224 | Lex.LexFromRawLexer(Tok); |
| 5225 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5226 | |
| 5227 | using namespace cxcursor; |
| 5228 | CXCursor Cursor |
| 5229 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5230 | Locations.back()), |
| 5231 | TU); |
| 5232 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5233 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5234 | } |
| 5235 | |
| 5236 | if (Tok.isAtStartOfLine()) |
| 5237 | goto reprocess; |
| 5238 | |
| 5239 | continue; |
| 5240 | } |
| 5241 | |
| 5242 | if (Tok.is(tok::eof)) |
| 5243 | break; |
| 5244 | } |
| 5245 | } |
| 5246 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5247 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5248 | static void clang_annotateTokensImpl(void *UserData) { |
| 5249 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 5250 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 5251 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 5252 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 5253 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 5254 | |
| 5255 | // Determine the region of interest, which contains all of the tokens. |
| 5256 | SourceRange RegionOfInterest; |
| 5257 | RegionOfInterest.setBegin( |
| 5258 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 5259 | RegionOfInterest.setEnd( |
| 5260 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 5261 | Tokens[NumTokens-1]))); |
| 5262 | |
| 5263 | // A mapping from the source locations found when re-lexing or traversing the |
| 5264 | // region of interest to the corresponding cursors. |
| 5265 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5266 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5267 | // Relex the tokens within the source range to look for preprocessing |
| 5268 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5269 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5270 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5271 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5272 | // Search and mark tokens that are macro argument expansions. |
| 5273 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5274 | Tokens, NumTokens); |
| 5275 | CursorVisitor MacroArgMarker(TU, |
| 5276 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5277 | /*VisitPreprocessorLast=*/true, |
| 5278 | /*VisitIncludedPreprocessingEntries=*/false, |
| 5279 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5280 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5281 | } |
| 5282 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5283 | // Annotate all of the source locations in the region of interest that map to |
| 5284 | // a specific cursor. |
| 5285 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5286 | TU, RegionOfInterest); |
| 5287 | |
| 5288 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5289 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5290 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5291 | // algorithm back into a traditional recursion by explicitly calling |
| 5292 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5293 | W.AnnotateTokens(); |
| 5294 | |
| 5295 | // If we ran into any entities that involve context-sensitive keywords, |
| 5296 | // take another pass through the tokens to mark them as such. |
| 5297 | if (W.hasContextSensitiveKeywords()) { |
| 5298 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5299 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5300 | continue; |
| 5301 | |
| 5302 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5303 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5304 | if (ObjCPropertyDecl *Property |
| 5305 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5306 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5307 | llvm::StringSwitch<bool>(II->getName()) |
| 5308 | .Case("readonly", true) |
| 5309 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5310 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5311 | .Case("readwrite", true) |
| 5312 | .Case("retain", true) |
| 5313 | .Case("copy", true) |
| 5314 | .Case("nonatomic", true) |
| 5315 | .Case("atomic", true) |
| 5316 | .Case("getter", true) |
| 5317 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5318 | .Case("strong", true) |
| 5319 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5320 | .Default(false)) |
| 5321 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5322 | } |
| 5323 | continue; |
| 5324 | } |
| 5325 | |
| 5326 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5327 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5328 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5329 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5330 | .Case("in", true) |
| 5331 | .Case("out", true) |
| 5332 | .Case("inout", true) |
| 5333 | .Case("oneway", true) |
| 5334 | .Case("bycopy", true) |
| 5335 | .Case("byref", true) |
| 5336 | .Default(false)) |
| 5337 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5338 | continue; |
| 5339 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5340 | |
| 5341 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5342 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5343 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5344 | continue; |
| 5345 | } |
| 5346 | } |
| 5347 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5350 | extern "C" { |
| 5351 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5352 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5353 | CXToken *Tokens, unsigned NumTokens, |
| 5354 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5355 | |
| 5356 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5357 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5358 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5359 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5360 | CXCursor C = clang_getNullCursor(); |
| 5361 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5362 | Cursors[I] = C; |
| 5363 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5364 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5365 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5366 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5367 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5368 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5369 | |
| 5370 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5371 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5372 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5373 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5374 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5375 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5376 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5377 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5378 | } // end: extern "C" |
| 5379 | |
| 5380 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5381 | // Operations for querying linkage of a cursor. |
| 5382 | //===----------------------------------------------------------------------===// |
| 5383 | |
| 5384 | extern "C" { |
| 5385 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5386 | if (!clang_isDeclaration(cursor.kind)) |
| 5387 | return CXLinkage_Invalid; |
| 5388 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5389 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5390 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5391 | switch (ND->getLinkage()) { |
| 5392 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5393 | case InternalLinkage: return CXLinkage_Internal; |
| 5394 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5395 | case ExternalLinkage: return CXLinkage_External; |
| 5396 | }; |
| 5397 | |
| 5398 | return CXLinkage_Invalid; |
| 5399 | } |
| 5400 | } // end: extern "C" |
| 5401 | |
| 5402 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5403 | // Operations for querying language of a cursor. |
| 5404 | //===----------------------------------------------------------------------===// |
| 5405 | |
| 5406 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 5407 | switch (D->getKind()) { |
| 5408 | default: |
| 5409 | break; |
| 5410 | case Decl::ImplicitParam: |
| 5411 | case Decl::ObjCAtDefsField: |
| 5412 | case Decl::ObjCCategory: |
| 5413 | case Decl::ObjCCategoryImpl: |
| 5414 | case Decl::ObjCClass: |
| 5415 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5416 | case Decl::ObjCForwardProtocol: |
| 5417 | case Decl::ObjCImplementation: |
| 5418 | case Decl::ObjCInterface: |
| 5419 | case Decl::ObjCIvar: |
| 5420 | case Decl::ObjCMethod: |
| 5421 | case Decl::ObjCProperty: |
| 5422 | case Decl::ObjCPropertyImpl: |
| 5423 | case Decl::ObjCProtocol: |
| 5424 | return CXLanguage_ObjC; |
| 5425 | case Decl::CXXConstructor: |
| 5426 | case Decl::CXXConversion: |
| 5427 | case Decl::CXXDestructor: |
| 5428 | case Decl::CXXMethod: |
| 5429 | case Decl::CXXRecord: |
| 5430 | case Decl::ClassTemplate: |
| 5431 | case Decl::ClassTemplatePartialSpecialization: |
| 5432 | case Decl::ClassTemplateSpecialization: |
| 5433 | case Decl::Friend: |
| 5434 | case Decl::FriendTemplate: |
| 5435 | case Decl::FunctionTemplate: |
| 5436 | case Decl::LinkageSpec: |
| 5437 | case Decl::Namespace: |
| 5438 | case Decl::NamespaceAlias: |
| 5439 | case Decl::NonTypeTemplateParm: |
| 5440 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5441 | case Decl::TemplateTemplateParm: |
| 5442 | case Decl::TemplateTypeParm: |
| 5443 | case Decl::UnresolvedUsingTypename: |
| 5444 | case Decl::UnresolvedUsingValue: |
| 5445 | case Decl::Using: |
| 5446 | case Decl::UsingDirective: |
| 5447 | case Decl::UsingShadow: |
| 5448 | return CXLanguage_CPlusPlus; |
| 5449 | } |
| 5450 | |
| 5451 | return CXLanguage_C; |
| 5452 | } |
| 5453 | |
| 5454 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5455 | |
| 5456 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5457 | if (clang_isDeclaration(cursor.kind)) |
| 5458 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5459 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5460 | return CXAvailability_Available; |
| 5461 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5462 | switch (D->getAvailability()) { |
| 5463 | case AR_Available: |
| 5464 | case AR_NotYetIntroduced: |
| 5465 | return CXAvailability_Available; |
| 5466 | |
| 5467 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5468 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5469 | |
| 5470 | case AR_Unavailable: |
| 5471 | return CXAvailability_NotAvailable; |
| 5472 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5473 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5475 | return CXAvailability_Available; |
| 5476 | } |
| 5477 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5478 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5479 | if (clang_isDeclaration(cursor.kind)) |
| 5480 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5481 | |
| 5482 | return CXLanguage_Invalid; |
| 5483 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5484 | |
| 5485 | /// \brief If the given cursor is the "templated" declaration |
| 5486 | /// descibing a class or function template, return the class or |
| 5487 | /// function template. |
| 5488 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5489 | if (!D) |
| 5490 | return 0; |
| 5491 | |
| 5492 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5493 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5494 | return FunTmpl; |
| 5495 | |
| 5496 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5497 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5498 | return ClassTmpl; |
| 5499 | |
| 5500 | return D; |
| 5501 | } |
| 5502 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5503 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5504 | if (clang_isDeclaration(cursor.kind)) { |
| 5505 | if (Decl *D = getCursorDecl(cursor)) { |
| 5506 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5507 | if (!DC) |
| 5508 | return clang_getNullCursor(); |
| 5509 | |
| 5510 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5511 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | |
| 5515 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5516 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5517 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | return clang_getNullCursor(); |
| 5521 | } |
| 5522 | |
| 5523 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5524 | if (clang_isDeclaration(cursor.kind)) { |
| 5525 | if (Decl *D = getCursorDecl(cursor)) { |
| 5526 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5527 | if (!DC) |
| 5528 | return clang_getNullCursor(); |
| 5529 | |
| 5530 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5531 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5536 | // statement or expression, so we return nothing. |
| 5537 | return clang_getNullCursor(); |
| 5538 | } |
| 5539 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5540 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5541 | CXCursor **overridden, |
| 5542 | unsigned *num_overridden) { |
| 5543 | if (overridden) |
| 5544 | *overridden = 0; |
| 5545 | if (num_overridden) |
| 5546 | *num_overridden = 0; |
| 5547 | if (!overridden || !num_overridden) |
| 5548 | return; |
| 5549 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5550 | SmallVector<CXCursor, 8> Overridden; |
| 5551 | cxcursor::getOverriddenCursors(cursor, Overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5552 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5553 | *num_overridden = Overridden.size(); |
| 5554 | *overridden = new CXCursor [Overridden.size()]; |
| 5555 | std::copy(Overridden.begin(), Overridden.end(), *overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5559 | delete [] overridden; |
| 5560 | } |
| 5561 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5562 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5563 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5564 | return 0; |
| 5565 | |
| 5566 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5567 | return (void *)ID->getFile(); |
| 5568 | } |
| 5569 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5570 | } // end: extern "C" |
| 5571 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5572 | |
| 5573 | //===----------------------------------------------------------------------===// |
| 5574 | // C++ AST instrospection. |
| 5575 | //===----------------------------------------------------------------------===// |
| 5576 | |
| 5577 | extern "C" { |
| 5578 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5579 | if (!clang_isDeclaration(C.kind)) |
| 5580 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5581 | |
| 5582 | CXXMethodDecl *Method = 0; |
| 5583 | Decl *D = cxcursor::getCursorDecl(C); |
| 5584 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5585 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5586 | else |
| 5587 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5588 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5589 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5591 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5592 | if (!clang_isDeclaration(C.kind)) |
| 5593 | return 0; |
| 5594 | |
| 5595 | CXXMethodDecl *Method = 0; |
| 5596 | Decl *D = cxcursor::getCursorDecl(C); |
| 5597 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5598 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5599 | else |
| 5600 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5601 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5602 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5603 | } // end: extern "C" |
| 5604 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5605 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5606 | // Attribute introspection. |
| 5607 | //===----------------------------------------------------------------------===// |
| 5608 | |
| 5609 | extern "C" { |
| 5610 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5611 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5612 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5613 | |
| 5614 | IBOutletCollectionAttr *A = |
| 5615 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5616 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5617 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5618 | } |
| 5619 | } // end: extern "C" |
| 5620 | |
| 5621 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5622 | // Inspecting memory usage. |
| 5623 | //===----------------------------------------------------------------------===// |
| 5624 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5625 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5626 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5627 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5628 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5629 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5630 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5631 | entries.push_back(entry); |
| 5632 | } |
| 5633 | |
| 5634 | extern "C" { |
| 5635 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5636 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5637 | const char *str = ""; |
| 5638 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5639 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5640 | str = "ASTContext: expressions, declarations, and types"; |
| 5641 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5642 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5643 | str = "ASTContext: identifiers"; |
| 5644 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5645 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5646 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5647 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5648 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5649 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5650 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5651 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5652 | str = "SourceManager: content cache allocator"; |
| 5653 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5654 | case CXTUResourceUsage_AST_SideTables: |
| 5655 | str = "ASTContext: side tables"; |
| 5656 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5657 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5658 | str = "SourceManager: malloc'ed memory buffers"; |
| 5659 | break; |
| 5660 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5661 | str = "SourceManager: mmap'ed memory buffers"; |
| 5662 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5663 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5664 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5665 | break; |
| 5666 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5667 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5668 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5669 | case CXTUResourceUsage_Preprocessor: |
| 5670 | str = "Preprocessor: malloc'ed memory"; |
| 5671 | break; |
| 5672 | case CXTUResourceUsage_PreprocessingRecord: |
| 5673 | str = "Preprocessor: PreprocessingRecord"; |
| 5674 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5675 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5676 | str = "SourceManager: data structures and tables"; |
| 5677 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5678 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5679 | str = "Preprocessor: header search tables"; |
| 5680 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5681 | } |
| 5682 | return str; |
| 5683 | } |
| 5684 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5685 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5686 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5687 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5688 | return usage; |
| 5689 | } |
| 5690 | |
| 5691 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5692 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5693 | ASTContext &astContext = astUnit->getASTContext(); |
| 5694 | |
| 5695 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5696 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5697 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5698 | |
| 5699 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5700 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5701 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5702 | |
| 5703 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5704 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5705 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5706 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5707 | // How much memory is used by ASTContext's side tables? |
| 5708 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5709 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5710 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5711 | // How much memory is used for caching global code completion results? |
| 5712 | unsigned long completionBytes = 0; |
| 5713 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5714 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5715 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5716 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5717 | createCXTUResourceUsageEntry(*entries, |
| 5718 | CXTUResourceUsage_GlobalCompletionResults, |
| 5719 | completionBytes); |
| 5720 | |
| 5721 | // How much memory is being used by SourceManager's content cache? |
| 5722 | createCXTUResourceUsageEntry(*entries, |
| 5723 | CXTUResourceUsage_SourceManagerContentCache, |
| 5724 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5725 | |
| 5726 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5727 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5728 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5729 | |
| 5730 | createCXTUResourceUsageEntry(*entries, |
| 5731 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5732 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5733 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5734 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5735 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5736 | createCXTUResourceUsageEntry(*entries, |
| 5737 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5738 | (unsigned long) astContext.getSourceManager() |
| 5739 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5740 | |
| 5741 | // How much memory is being used by the ExternalASTSource? |
| 5742 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5743 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5744 | esrc->getMemoryBufferSizes(); |
| 5745 | |
| 5746 | createCXTUResourceUsageEntry(*entries, |
| 5747 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5748 | (unsigned long) sizes.malloc_bytes); |
| 5749 | createCXTUResourceUsageEntry(*entries, |
| 5750 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5751 | (unsigned long) sizes.mmap_bytes); |
| 5752 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5753 | |
| 5754 | // How much memory is being used by the Preprocessor? |
| 5755 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5756 | createCXTUResourceUsageEntry(*entries, |
| 5757 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5758 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5759 | |
| 5760 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5761 | createCXTUResourceUsageEntry(*entries, |
| 5762 | CXTUResourceUsage_PreprocessingRecord, |
| 5763 | pRec->getTotalMemory()); |
| 5764 | } |
| 5765 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5766 | createCXTUResourceUsageEntry(*entries, |
| 5767 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5768 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5769 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5770 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5771 | (unsigned) entries->size(), |
| 5772 | entries->size() ? &(*entries)[0] : 0 }; |
| 5773 | entries.take(); |
| 5774 | return usage; |
| 5775 | } |
| 5776 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5777 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5778 | if (usage.data) |
| 5779 | delete (MemUsageEntries*) usage.data; |
| 5780 | } |
| 5781 | |
| 5782 | } // end extern "C" |
| 5783 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5784 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5785 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5786 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5787 | fprintf(stderr, " %s: %lu\n", |
| 5788 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5789 | Usage.entries[I].amount); |
| 5790 | |
| 5791 | clang_disposeCXTUResourceUsage(Usage); |
| 5792 | } |
| 5793 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5794 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5795 | // Misc. utility functions. |
| 5796 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5797 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5798 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5799 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5800 | |
| 5801 | namespace clang { |
| 5802 | |
| 5803 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5804 | void (*Fn)(void*), void *UserData, |
| 5805 | unsigned Size) { |
| 5806 | if (!Size) |
| 5807 | Size = GetSafetyThreadStackSize(); |
| 5808 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5809 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5810 | return CRC.RunSafely(Fn, UserData); |
| 5811 | } |
| 5812 | |
| 5813 | unsigned GetSafetyThreadStackSize() { |
| 5814 | return SafetyStackThreadSize; |
| 5815 | } |
| 5816 | |
| 5817 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5818 | SafetyStackThreadSize = Value; |
| 5819 | } |
| 5820 | |
| 5821 | } |
| 5822 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5823 | extern "C" { |
| 5824 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5825 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5826 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5830 | |