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 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 196 | /// \brief Whether we should visit declarations or preprocessing record |
| 197 | /// entries that are #included inside the \arg RegionOfInterest. |
| 198 | bool VisitIncludedEntities; |
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 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame^] | 222 | CXChildVisitResult invokeVisitor(CXCursor cursor, CXCursor parent) { |
| 223 | return Visitor(cursor, parent, ClientData); |
| 224 | } |
| 225 | |
| 226 | void visitDeclsFromFileRegion(FileID File, unsigned Offset, unsigned Length); |
| 227 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 228 | class SetParentRAII { |
| 229 | CXCursor &Parent; |
| 230 | Decl *&StmtParent; |
| 231 | CXCursor OldParent; |
| 232 | |
| 233 | public: |
| 234 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 235 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
| 236 | { |
| 237 | Parent = NewParent; |
| 238 | if (clang_isDeclaration(Parent.kind)) |
| 239 | StmtParent = getCursorDecl(Parent); |
| 240 | } |
| 241 | |
| 242 | ~SetParentRAII() { |
| 243 | Parent = OldParent; |
| 244 | if (clang_isDeclaration(Parent.kind)) |
| 245 | StmtParent = getCursorDecl(Parent); |
| 246 | } |
| 247 | }; |
| 248 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 249 | public: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 250 | CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor, |
| 251 | CXClientData ClientData, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 252 | bool VisitPreprocessorLast, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 253 | bool VisitIncludedPreprocessingEntries = false, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 254 | SourceRange RegionOfInterest = SourceRange()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 255 | : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)), |
| 256 | Visitor(Visitor), ClientData(ClientData), |
Douglas Gregor | 08e0bc1 | 2011-09-10 00:09:20 +0000 | [diff] [blame] | 257 | VisitPreprocessorLast(VisitPreprocessorLast), |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 258 | VisitIncludedEntities(VisitIncludedPreprocessingEntries), |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 259 | RegionOfInterest(RegionOfInterest), DI_current(0) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 260 | { |
| 261 | Parent.kind = CXCursor_NoDeclFound; |
| 262 | Parent.data[0] = 0; |
| 263 | Parent.data[1] = 0; |
| 264 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 265 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 266 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 268 | ~CursorVisitor() { |
| 269 | // Free the pre-allocated worklists for data-recursion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 270 | for (SmallVectorImpl<VisitorWorkList*>::iterator |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 271 | I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) { |
| 272 | delete *I; |
| 273 | } |
| 274 | } |
| 275 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 276 | ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); } |
| 277 | CXTranslationUnit getTU() const { return TU; } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 279 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame^] | 280 | |
| 281 | /// \brief Visit declarations and preprocessed entities for the file region |
| 282 | /// designated by \see RegionOfInterest. |
| 283 | void visitFileRegion(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 285 | bool visitPreprocessedEntitiesInRegion(); |
| 286 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 287 | bool shouldVisitIncludedEntities() const { |
| 288 | return VisitIncludedEntities; |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 291 | template<typename InputIterator> |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 292 | bool visitPreprocessedEntities(InputIterator First, InputIterator Last, |
| 293 | PreprocessingRecord &PPRec, |
| 294 | FileID FID = FileID()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 296 | bool VisitChildren(CXCursor Parent); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 298 | // Declaration visitors |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 299 | bool VisitTypeAliasDecl(TypeAliasDecl *D); |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 300 | bool VisitAttributes(Decl *D); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 301 | bool VisitBlockDecl(BlockDecl *B); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 302 | bool VisitCXXRecordDecl(CXXRecordDecl *D); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 303 | llvm::Optional<bool> shouldVisitCursor(CXCursor C); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 304 | bool VisitDeclContext(DeclContext *DC); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 305 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 306 | bool VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 307 | bool VisitTagDecl(TagDecl *D); |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 308 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 309 | bool VisitClassTemplatePartialSpecializationDecl( |
| 310 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 311 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 312 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
| 313 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
| 314 | bool VisitFunctionDecl(FunctionDecl *ND); |
| 315 | bool VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 316 | bool VisitVarDecl(VarDecl *); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 317 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 318 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 319 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 320 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 321 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 322 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 323 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 324 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 325 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 326 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 327 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 328 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 329 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 330 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 331 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 332 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 333 | bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 334 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 335 | bool VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 336 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 337 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 338 | bool VisitUsingDecl(UsingDecl *D); |
| 339 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 340 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 342 | // Name visitor |
| 343 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 344 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 345 | bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 347 | // Template visitors |
| 348 | bool VisitTemplateParameters(const TemplateParameterList *Params); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 349 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 350 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); |
| 351 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 352 | // Type visitors |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 353 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 354 | #define TYPELOC(CLASS, PARENT) \ |
| 355 | bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
| 356 | #include "clang/AST/TypeLocNodes.def" |
| 357 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 358 | bool VisitTagTypeLoc(TagTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 359 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 360 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); |
| 361 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 362 | // Data-recursive visitor functions. |
| 363 | bool IsInRegionOfInterest(CXCursor C); |
| 364 | bool RunVisitorWorkList(VisitorWorkList &WL); |
| 365 | void EnqueueWorkList(VisitorWorkList &WL, Stmt *S); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 366 | LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 367 | }; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 369 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 371 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 372 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 373 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 375 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 376 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 379 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 380 | /// its children. |
| 381 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 382 | /// \param Cursor the cursor to visit. |
| 383 | /// |
| 384 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 385 | /// this cursor is within the region of interest. |
| 386 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 387 | /// \returns true if the visitation should be aborted, false if it |
| 388 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 389 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 390 | if (clang_isInvalid(Cursor.kind)) |
| 391 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 393 | if (clang_isDeclaration(Cursor.kind)) { |
| 394 | Decl *D = getCursorDecl(Cursor); |
| 395 | assert(D && "Invalid declaration cursor"); |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 396 | // Ignore implicit declarations, unless it's an objc method because |
| 397 | // currently we should report implicit methods for properties when indexing. |
| 398 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 402 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 403 | // we're done. |
| 404 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 405 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 406 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 410 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 411 | case CXChildVisit_Break: |
| 412 | return true; |
| 413 | |
| 414 | case CXChildVisit_Continue: |
| 415 | return false; |
| 416 | |
| 417 | case CXChildVisit_Recurse: |
| 418 | return VisitChildren(Cursor); |
| 419 | } |
| 420 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 421 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 424 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 425 | PreprocessingRecord &PPRec, |
| 426 | CursorVisitor &Visitor) { |
| 427 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 428 | FileID FID; |
| 429 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 430 | if (!Visitor.shouldVisitIncludedEntities()) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 431 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 432 | // where we skip preprocessed entities that do not come from the same FileID. |
| 433 | FID = SM.getFileID(R.getBegin()); |
| 434 | if (FID != SM.getFileID(R.getEnd())) |
| 435 | FID = FileID(); |
| 436 | } |
| 437 | |
| 438 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 439 | Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 440 | return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, |
| 441 | PPRec, FID); |
| 442 | } |
| 443 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame^] | 444 | void CursorVisitor::visitFileRegion() { |
| 445 | if (RegionOfInterest.isInvalid()) |
| 446 | return; |
| 447 | |
| 448 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 449 | SourceManager &SM = Unit->getSourceManager(); |
| 450 | |
| 451 | std::pair<FileID, unsigned> |
| 452 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 453 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 454 | |
| 455 | if (End.first != Begin.first) { |
| 456 | // If the end does not reside in the same file, try to recover by |
| 457 | // picking the end of the file of begin location. |
| 458 | End.first = Begin.first; |
| 459 | End.second = SM.getFileIDSize(Begin.first); |
| 460 | } |
| 461 | |
| 462 | assert(Begin.first == End.first); |
| 463 | if (Begin.second > End.second) |
| 464 | return; |
| 465 | |
| 466 | FileID File = Begin.first; |
| 467 | unsigned Offset = Begin.second; |
| 468 | unsigned Length = End.second - Begin.second; |
| 469 | |
| 470 | if (!VisitPreprocessorLast && |
| 471 | Unit->getPreprocessor().getPreprocessingRecord()) |
| 472 | visitPreprocessedEntitiesInRegion(); |
| 473 | |
| 474 | visitDeclsFromFileRegion(File, Offset, Length); |
| 475 | |
| 476 | if (VisitPreprocessorLast && |
| 477 | Unit->getPreprocessor().getPreprocessingRecord()) |
| 478 | visitPreprocessedEntitiesInRegion(); |
| 479 | } |
| 480 | |
| 481 | void CursorVisitor::visitDeclsFromFileRegion(FileID File, |
| 482 | unsigned Offset, unsigned Length) { |
| 483 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 484 | SourceManager &SM = Unit->getSourceManager(); |
| 485 | |
| 486 | SourceRange Range = RegionOfInterest; |
| 487 | CXCursor Parent = clang_getTranslationUnitCursor(TU); |
| 488 | |
| 489 | SmallVector<Decl *, 16> Decls; |
| 490 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 491 | |
| 492 | // If we didn't find any file level decls for the file, try looking at the |
| 493 | // file that it was included from. |
| 494 | while (Decls.empty()) { |
| 495 | bool Invalid = false; |
| 496 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 497 | if (Invalid) |
| 498 | return; |
| 499 | |
| 500 | SourceLocation Outer; |
| 501 | if (SLEntry.isFile()) |
| 502 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 503 | else |
| 504 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 505 | if (Outer.isInvalid()) |
| 506 | return; |
| 507 | |
| 508 | llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
| 509 | Length = 0; |
| 510 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 511 | } |
| 512 | |
| 513 | assert(!Decls.empty()); |
| 514 | |
| 515 | bool VisitedAtLeastOnce = false; |
| 516 | SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); |
| 517 | for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
| 518 | Decl *D = *DIt; |
| 519 | |
| 520 | // We handle forward decls via ObjCClassDecl. |
| 521 | if (ObjCInterfaceDecl *InterD = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 522 | if (InterD->isForwardDecl()) |
| 523 | continue; |
| 524 | // An interface that started as a forward decl may have changed location |
| 525 | // because its @interface was parsed. |
| 526 | if (InterD->isInitiallyForwardDecl() && |
| 527 | !SM.isInFileID(SM.getFileLoc(InterD->getLocation()), File)) |
| 528 | continue; |
| 529 | } |
| 530 | |
| 531 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 532 | if (CompRes == RangeBefore) |
| 533 | continue; |
| 534 | if (CompRes == RangeAfter) |
| 535 | break; |
| 536 | |
| 537 | assert(CompRes == RangeOverlap); |
| 538 | VisitedAtLeastOnce = true; |
| 539 | CXCursor C = MakeCXCursor(D, TU, Range); |
| 540 | CXChildVisitResult |
| 541 | Res = invokeVisitor(C, Parent); |
| 542 | if (Res == CXChildVisit_Break) |
| 543 | break; |
| 544 | if (Res == CXChildVisit_Recurse) |
| 545 | if (VisitChildren(C)) |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | if (VisitedAtLeastOnce) |
| 550 | return; |
| 551 | |
| 552 | // No Decls overlapped with the range. Move up the lexical context until there |
| 553 | // is a context that contains the range or we reach the translation unit |
| 554 | // level. |
| 555 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 556 | : (*(DIt-1))->getLexicalDeclContext(); |
| 557 | |
| 558 | while (DC && !DC->isTranslationUnit()) { |
| 559 | Decl *D = cast<Decl>(DC); |
| 560 | SourceRange CurDeclRange = D->getSourceRange(); |
| 561 | if (CurDeclRange.isInvalid()) |
| 562 | break; |
| 563 | |
| 564 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
| 565 | CXCursor C = MakeCXCursor(D, TU, Range); |
| 566 | CXChildVisitResult |
| 567 | Res = invokeVisitor(C, Parent); |
| 568 | if (Res == CXChildVisit_Recurse) |
| 569 | VisitChildren(C); |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | DC = D->getLexicalDeclContext(); |
| 574 | } |
| 575 | } |
| 576 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 577 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 578 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 579 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 580 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 581 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 582 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 583 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 584 | SourceLocation B = MappedRange.getBegin(); |
| 585 | SourceLocation E = MappedRange.getEnd(); |
| 586 | |
| 587 | if (AU->isInPreambleFileID(B)) { |
| 588 | if (SM.isLoadedSourceLocation(E)) |
| 589 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 590 | PPRec, *this); |
| 591 | |
| 592 | // Beginning of range lies in the preamble but it also extends beyond |
| 593 | // it into the main file. Split the range into 2 parts, one covering |
| 594 | // the preamble and another covering the main file. This allows subsequent |
| 595 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 596 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 597 | // do not come from the same FileID. |
| 598 | bool breaked = |
| 599 | visitPreprocessedEntitiesInRange( |
| 600 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 601 | PPRec, *this); |
| 602 | if (breaked) return true; |
| 603 | return visitPreprocessedEntitiesInRange( |
| 604 | SourceRange(AU->getStartOfMainFileID(), E), |
| 605 | PPRec, *this); |
| 606 | } |
| 607 | |
| 608 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 611 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 612 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 613 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 614 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 615 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 616 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 617 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 618 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | template<typename InputIterator> |
| 622 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 623 | InputIterator Last, |
| 624 | PreprocessingRecord &PPRec, |
| 625 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 626 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 627 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 628 | continue; |
| 629 | |
| 630 | PreprocessedEntity *PPE = *First; |
| 631 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 632 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 633 | return true; |
| 634 | |
| 635 | continue; |
| 636 | } |
| 637 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 638 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 639 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 640 | return true; |
| 641 | |
| 642 | continue; |
| 643 | } |
| 644 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 645 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 646 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 647 | return true; |
| 648 | |
| 649 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 653 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 656 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 657 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 658 | /// \returns true if the visitation should be aborted, false if it |
| 659 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 660 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 661 | if (clang_isReference(Cursor.kind) && |
| 662 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 663 | // By definition, references have no children. |
| 664 | return false; |
| 665 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 666 | |
| 667 | // 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] | 668 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 669 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 671 | if (clang_isDeclaration(Cursor.kind)) { |
| 672 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 673 | if (!D) |
| 674 | return false; |
| 675 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 676 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 677 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 679 | if (clang_isStatement(Cursor.kind)) { |
| 680 | if (Stmt *S = getCursorStmt(Cursor)) |
| 681 | return Visit(S); |
| 682 | |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | if (clang_isExpression(Cursor.kind)) { |
| 687 | if (Expr *E = getCursorExpr(Cursor)) |
| 688 | return Visit(E); |
| 689 | |
| 690 | return false; |
| 691 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 693 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 694 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 695 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 696 | |
| 697 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 698 | for (unsigned I = 0; I != 2; ++I) { |
| 699 | if (VisitOrder[I]) { |
| 700 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 701 | RegionOfInterest.isInvalid()) { |
| 702 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 703 | TLEnd = CXXUnit->top_level_end(); |
| 704 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 705 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 706 | return true; |
| 707 | } |
| 708 | } else if (VisitDeclContext( |
| 709 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 710 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 711 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 712 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 713 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 714 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 715 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 716 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 717 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 718 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 719 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 720 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 721 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 722 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 723 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 724 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 725 | return Visit(BaseTSInfo->getTypeLoc()); |
| 726 | } |
| 727 | } |
| 728 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 729 | |
| 730 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 731 | IBOutletCollectionAttr *A = |
| 732 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 733 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 734 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 735 | A->getInterfaceLoc(), TU)); |
| 736 | } |
| 737 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 738 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 742 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 743 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 744 | if (Visit(TSInfo->getTypeLoc())) |
| 745 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 746 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 747 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 748 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 749 | |
| 750 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 753 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 754 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 755 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 756 | if (Range.isInvalid()) |
| 757 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 758 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 759 | switch (CompareRegionOfInterest(Range)) { |
| 760 | case RangeBefore: |
| 761 | // This declaration comes before the region of interest; skip it. |
| 762 | return llvm::Optional<bool>(); |
| 763 | |
| 764 | case RangeAfter: |
| 765 | // This declaration comes after the region of interest; we're done. |
| 766 | return false; |
| 767 | |
| 768 | case RangeOverlap: |
| 769 | // This declaration overlaps the region of interest; visit it. |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 777 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 778 | |
| 779 | // FIXME: Eventually remove. This part of a hack to support proper |
| 780 | // iteration over all Decls contained lexically within an ObjC container. |
| 781 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 782 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 783 | |
| 784 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 785 | Decl *D = *I; |
| 786 | if (D->getLexicalDeclContext() != DC) |
| 787 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 788 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 789 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 790 | if (!V.hasValue()) |
| 791 | continue; |
| 792 | if (!V.getValue()) |
| 793 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 794 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 795 | return true; |
| 796 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 797 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 800 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 801 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 802 | return false; |
| 803 | } |
| 804 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 805 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 806 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 807 | return Visit(TSInfo->getTypeLoc()); |
| 808 | |
| 809 | return false; |
| 810 | } |
| 811 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 812 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 813 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 814 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
| 819 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 820 | return VisitDeclContext(D); |
| 821 | } |
| 822 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 823 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 824 | ClassTemplateSpecializationDecl *D) { |
| 825 | bool ShouldVisitBody = false; |
| 826 | switch (D->getSpecializationKind()) { |
| 827 | case TSK_Undeclared: |
| 828 | case TSK_ImplicitInstantiation: |
| 829 | // Nothing to visit |
| 830 | return false; |
| 831 | |
| 832 | case TSK_ExplicitInstantiationDeclaration: |
| 833 | case TSK_ExplicitInstantiationDefinition: |
| 834 | break; |
| 835 | |
| 836 | case TSK_ExplicitSpecialization: |
| 837 | ShouldVisitBody = true; |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | // Visit the template arguments used in the specialization. |
| 842 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 843 | TypeLoc TL = SpecType->getTypeLoc(); |
| 844 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 845 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 846 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 847 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 848 | return true; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 853 | return true; |
| 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 858 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 859 | ClassTemplatePartialSpecializationDecl *D) { |
| 860 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 861 | // before visiting these template parameters. |
| 862 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 863 | return true; |
| 864 | |
| 865 | // Visit the partial specialization arguments. |
| 866 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 867 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 868 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 869 | return true; |
| 870 | |
| 871 | return VisitCXXRecordDecl(D); |
| 872 | } |
| 873 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 874 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 875 | // Visit the default argument. |
| 876 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 877 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 878 | if (Visit(DefArg->getTypeLoc())) |
| 879 | return true; |
| 880 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 884 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 885 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 886 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 890 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 891 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 892 | if (Visit(TSInfo->getTypeLoc())) |
| 893 | return true; |
| 894 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 895 | // Visit the nested-name-specifier, if present. |
| 896 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 897 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 898 | return true; |
| 899 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 900 | return false; |
| 901 | } |
| 902 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 903 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 904 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 905 | CXXCtorInitializer const * const *X |
| 906 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 907 | CXXCtorInitializer const * const *Y |
| 908 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 909 | |
| 910 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 911 | return -1; |
| 912 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 913 | return 1; |
| 914 | else |
| 915 | return 0; |
| 916 | } |
| 917 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 918 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 919 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 920 | // Visit the function declaration's syntactic components in the order |
| 921 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 922 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 923 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 924 | |
| 925 | // If we have a function declared directly (without the use of a typedef), |
| 926 | // visit just the return type. Otherwise, just visit the function's type |
| 927 | // now. |
| 928 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 929 | (!FTL && Visit(TL))) |
| 930 | return true; |
| 931 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 932 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 933 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 934 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 935 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 936 | |
| 937 | // Visit the declaration name. |
| 938 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 939 | return true; |
| 940 | |
| 941 | // FIXME: Visit explicitly-specified template arguments! |
| 942 | |
| 943 | // Visit the function parameters, if we have a function type. |
| 944 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 945 | return true; |
| 946 | |
| 947 | // FIXME: Attributes? |
| 948 | } |
| 949 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 950 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 951 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 952 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 953 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 954 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 955 | IEnd = Constructor->init_end(); |
| 956 | I != IEnd; ++I) { |
| 957 | if (!(*I)->isWritten()) |
| 958 | continue; |
| 959 | |
| 960 | WrittenInits.push_back(*I); |
| 961 | } |
| 962 | |
| 963 | // Sort the initializers in source order |
| 964 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 965 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 966 | |
| 967 | // Visit the initializers in source order |
| 968 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 969 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 970 | if (Init->isAnyMemberInitializer()) { |
| 971 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 972 | Init->getMemberLocation(), TU))) |
| 973 | return true; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 974 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 975 | if (Visit(TInfo->getTypeLoc())) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 976 | return true; |
| 977 | } |
| 978 | |
| 979 | // Visit the initializer value. |
| 980 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 981 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 982 | return true; |
| 983 | } |
| 984 | } |
| 985 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 986 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 987 | return true; |
| 988 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 990 | return false; |
| 991 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 993 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 994 | if (VisitDeclaratorDecl(D)) |
| 995 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 996 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 997 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 998 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 999 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 1004 | if (VisitDeclaratorDecl(D)) |
| 1005 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1007 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1008 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1010 | return false; |
| 1011 | } |
| 1012 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 1013 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 1014 | if (VisitDeclaratorDecl(D)) |
| 1015 | return true; |
| 1016 | |
| 1017 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 1018 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1019 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 1020 | |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1024 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 1025 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 1026 | // before visiting these template parameters. |
| 1027 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 1028 | return true; |
| 1029 | |
| 1030 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 1031 | } |
| 1032 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 1033 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 1034 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 1035 | // before visiting these template parameters. |
| 1036 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 1037 | return true; |
| 1038 | |
| 1039 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 1040 | } |
| 1041 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 1042 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 1043 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 1044 | return true; |
| 1045 | |
| 1046 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 1047 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 1048 | return true; |
| 1049 | |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1053 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1054 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 1055 | if (Visit(TSInfo->getTypeLoc())) |
| 1056 | return true; |
| 1057 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1058 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1059 | PEnd = ND->param_end(); |
| 1060 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1061 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1065 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1066 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1067 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1072 | namespace { |
| 1073 | struct ContainerDeclsSort { |
| 1074 | SourceManager &SM; |
| 1075 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 1076 | bool operator()(Decl *A, Decl *B) { |
| 1077 | SourceLocation L_A = A->getLocStart(); |
| 1078 | SourceLocation L_B = B->getLocStart(); |
| 1079 | assert(L_A.isValid() && L_B.isValid()); |
| 1080 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 1081 | } |
| 1082 | }; |
| 1083 | } |
| 1084 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1085 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1086 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 1087 | // an @implementation can lexically contain Decls that are not properly |
| 1088 | // nested in the AST. When we identify such cases, we need to retrofit |
| 1089 | // this nesting here. |
| 1090 | if (!DI_current) |
| 1091 | return VisitDeclContext(D); |
| 1092 | |
| 1093 | // Scan the Decls that immediately come after the container |
| 1094 | // in the current DeclContext. If any fall within the |
| 1095 | // container's lexical region, stash them into a vector |
| 1096 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1097 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1098 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1099 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1100 | if (EndLoc.isValid()) { |
| 1101 | DeclContext::decl_iterator next = *DI_current; |
| 1102 | while (++next != DE_current) { |
| 1103 | Decl *D_next = *next; |
| 1104 | if (!D_next) |
| 1105 | break; |
| 1106 | SourceLocation L = D_next->getLocStart(); |
| 1107 | if (!L.isValid()) |
| 1108 | break; |
| 1109 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 1110 | *DI_current = next; |
| 1111 | DeclsInContainer.push_back(D_next); |
| 1112 | continue; |
| 1113 | } |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // The common case. |
| 1119 | if (DeclsInContainer.empty()) |
| 1120 | return VisitDeclContext(D); |
| 1121 | |
| 1122 | // Get all the Decls in the DeclContext, and sort them with the |
| 1123 | // additional ones we've collected. Then visit them. |
| 1124 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 1125 | I!=E; ++I) { |
| 1126 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 1127 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 1128 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1129 | continue; |
| 1130 | DeclsInContainer.push_back(subDecl); |
| 1131 | } |
| 1132 | |
| 1133 | // Now sort the Decls so that they appear in lexical order. |
| 1134 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 1135 | ContainerDeclsSort(SM)); |
| 1136 | |
| 1137 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1138 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1139 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1140 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 1141 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 1142 | if (!V.hasValue()) |
| 1143 | continue; |
| 1144 | if (!V.getValue()) |
| 1145 | return false; |
| 1146 | if (Visit(Cursor, true)) |
| 1147 | return true; |
| 1148 | } |
| 1149 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1152 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1153 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1154 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1155 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1157 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1158 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1159 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1160 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1161 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1163 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1166 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1167 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1168 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1169 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1170 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1171 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1172 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1173 | return VisitObjCContainerDecl(PID); |
| 1174 | } |
| 1175 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1176 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 1177 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 1178 | return true; |
| 1179 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1180 | // FIXME: This implements a workaround with @property declarations also being |
| 1181 | // installed in the DeclContext for the @interface. Eventually this code |
| 1182 | // should be removed. |
| 1183 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1184 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1185 | return false; |
| 1186 | |
| 1187 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1188 | if (!ID) |
| 1189 | return false; |
| 1190 | |
| 1191 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1192 | ObjCPropertyDecl *prevDecl = |
| 1193 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 1194 | |
| 1195 | if (!prevDecl) |
| 1196 | return false; |
| 1197 | |
| 1198 | // Visit synthesized methods since they will be skipped when visiting |
| 1199 | // the @interface. |
| 1200 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1201 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1202 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1203 | return true; |
| 1204 | |
| 1205 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1206 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1207 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1208 | return true; |
| 1209 | |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1213 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1214 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1215 | if (D->getSuperClass() && |
| 1216 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1217 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1218 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1219 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1221 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1222 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1223 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1224 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1225 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1226 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1227 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1230 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1231 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1234 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1235 | // 'ID' could be null when dealing with invalid code. |
| 1236 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1237 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1238 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1240 | return VisitObjCImplDecl(D); |
| 1241 | } |
| 1242 | |
| 1243 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1244 | #if 0 |
| 1245 | // Issue callbacks for super class. |
| 1246 | // FIXME: No source location information! |
| 1247 | if (D->getSuperClass() && |
| 1248 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1249 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1250 | TU))) |
| 1251 | return true; |
| 1252 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1254 | return VisitObjCImplDecl(D); |
| 1255 | } |
| 1256 | |
| 1257 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1258 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1259 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1260 | E = D->protocol_end(); |
| 1261 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1262 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1263 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1264 | |
| 1265 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1268 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1269 | if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), |
| 1270 | D->getForwardDecl()->getLocation(), TU))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1271 | return true; |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1272 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1275 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1276 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1277 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1282 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1283 | return VisitDeclContext(D); |
| 1284 | } |
| 1285 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1286 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1287 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1288 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1289 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1290 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1291 | |
| 1292 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1293 | D->getTargetNameLoc(), TU)); |
| 1294 | } |
| 1295 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1296 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1297 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1298 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1299 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1300 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1301 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1303 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1304 | return true; |
| 1305 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1306 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1307 | } |
| 1308 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1309 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1310 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1311 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1312 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1313 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1314 | |
| 1315 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1316 | D->getIdentLocation(), TU)); |
| 1317 | } |
| 1318 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1319 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1320 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1321 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1322 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1323 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1324 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1325 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1326 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1327 | } |
| 1328 | |
| 1329 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1330 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1331 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1332 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1333 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1334 | return true; |
| 1335 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1336 | return false; |
| 1337 | } |
| 1338 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1339 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1340 | switch (Name.getName().getNameKind()) { |
| 1341 | case clang::DeclarationName::Identifier: |
| 1342 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1343 | case clang::DeclarationName::CXXOperatorName: |
| 1344 | case clang::DeclarationName::CXXUsingDirective: |
| 1345 | return false; |
| 1346 | |
| 1347 | case clang::DeclarationName::CXXConstructorName: |
| 1348 | case clang::DeclarationName::CXXDestructorName: |
| 1349 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1350 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1351 | return Visit(TSInfo->getTypeLoc()); |
| 1352 | return false; |
| 1353 | |
| 1354 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1355 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1356 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1357 | // FIXME: Per-identifier location info? |
| 1358 | return false; |
| 1359 | } |
| 1360 | |
| 1361 | return false; |
| 1362 | } |
| 1363 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1364 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1365 | SourceRange Range) { |
| 1366 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1367 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1368 | // a beginning source location, we can visit the first component of the |
| 1369 | // nested-name-specifier, if it's a single-token component. |
| 1370 | if (!NNS) |
| 1371 | return false; |
| 1372 | |
| 1373 | // Get the first component in the nested-name-specifier. |
| 1374 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1375 | NNS = Prefix; |
| 1376 | |
| 1377 | switch (NNS->getKind()) { |
| 1378 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1379 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1380 | TU)); |
| 1381 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1382 | case NestedNameSpecifier::NamespaceAlias: |
| 1383 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1384 | Range.getBegin(), TU)); |
| 1385 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1386 | case NestedNameSpecifier::TypeSpec: { |
| 1387 | // If the type has a form where we know that the beginning of the source |
| 1388 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1389 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1390 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1391 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1392 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1393 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1394 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1395 | if (const TemplateSpecializationType *TST |
| 1396 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1397 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1402 | case NestedNameSpecifier::Global: |
| 1403 | case NestedNameSpecifier::Identifier: |
| 1404 | break; |
| 1405 | } |
| 1406 | |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1410 | bool |
| 1411 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1412 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1413 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1414 | Qualifiers.push_back(Qualifier); |
| 1415 | |
| 1416 | while (!Qualifiers.empty()) { |
| 1417 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1418 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1419 | switch (NNS->getKind()) { |
| 1420 | case NestedNameSpecifier::Namespace: |
| 1421 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1422 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1423 | TU))) |
| 1424 | return true; |
| 1425 | |
| 1426 | break; |
| 1427 | |
| 1428 | case NestedNameSpecifier::NamespaceAlias: |
| 1429 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1430 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1431 | TU))) |
| 1432 | return true; |
| 1433 | |
| 1434 | break; |
| 1435 | |
| 1436 | case NestedNameSpecifier::TypeSpec: |
| 1437 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1438 | if (Visit(Q.getTypeLoc())) |
| 1439 | return true; |
| 1440 | |
| 1441 | break; |
| 1442 | |
| 1443 | case NestedNameSpecifier::Global: |
| 1444 | case NestedNameSpecifier::Identifier: |
| 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1452 | bool CursorVisitor::VisitTemplateParameters( |
| 1453 | const TemplateParameterList *Params) { |
| 1454 | if (!Params) |
| 1455 | return false; |
| 1456 | |
| 1457 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1458 | PEnd = Params->end(); |
| 1459 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1460 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1461 | return true; |
| 1462 | } |
| 1463 | |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1467 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1468 | switch (Name.getKind()) { |
| 1469 | case TemplateName::Template: |
| 1470 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1471 | |
| 1472 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1473 | // Visit the overloaded template set. |
| 1474 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1475 | return true; |
| 1476 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1477 | return false; |
| 1478 | |
| 1479 | case TemplateName::DependentTemplate: |
| 1480 | // FIXME: Visit nested-name-specifier. |
| 1481 | return false; |
| 1482 | |
| 1483 | case TemplateName::QualifiedTemplate: |
| 1484 | // FIXME: Visit nested-name-specifier. |
| 1485 | return Visit(MakeCursorTemplateRef( |
| 1486 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1487 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1488 | |
| 1489 | case TemplateName::SubstTemplateTemplateParm: |
| 1490 | return Visit(MakeCursorTemplateRef( |
| 1491 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1492 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1493 | |
| 1494 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1495 | return Visit(MakeCursorTemplateRef( |
| 1496 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1497 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1503 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1504 | switch (TAL.getArgument().getKind()) { |
| 1505 | case TemplateArgument::Null: |
| 1506 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1507 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1508 | return false; |
| 1509 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1510 | case TemplateArgument::Type: |
| 1511 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1512 | return Visit(TSInfo->getTypeLoc()); |
| 1513 | return false; |
| 1514 | |
| 1515 | case TemplateArgument::Declaration: |
| 1516 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1517 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1518 | return false; |
| 1519 | |
| 1520 | case TemplateArgument::Expression: |
| 1521 | if (Expr *E = TAL.getSourceExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1522 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1523 | return false; |
| 1524 | |
| 1525 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1526 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1527 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1528 | return true; |
| 1529 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1530 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1531 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1537 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1538 | return VisitDeclContext(D); |
| 1539 | } |
| 1540 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1541 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1542 | return Visit(TL.getUnqualifiedLoc()); |
| 1543 | } |
| 1544 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1545 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1546 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1549 | // "Class") have associated declarations. Create cursors for those. |
| 1550 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1551 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1553 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1554 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1555 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1556 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1557 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1558 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1559 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1560 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1561 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1562 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1563 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1564 | case BuiltinType::ObjCId: |
| 1565 | VisitType = Context.getObjCIdType(); |
| 1566 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1567 | |
| 1568 | case BuiltinType::ObjCClass: |
| 1569 | VisitType = Context.getObjCClassType(); |
| 1570 | break; |
| 1571 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1572 | case BuiltinType::ObjCSel: |
| 1573 | VisitType = Context.getObjCSelType(); |
| 1574 | break; |
| 1575 | } |
| 1576 | |
| 1577 | if (!VisitType.isNull()) { |
| 1578 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1579 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1580 | TU)); |
| 1581 | } |
| 1582 | |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1586 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1587 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1590 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1591 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1592 | } |
| 1593 | |
| 1594 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1595 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1596 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1598 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1599 | } |
| 1600 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1601 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1602 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1605 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1606 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1607 | return true; |
| 1608 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1609 | return false; |
| 1610 | } |
| 1611 | |
| 1612 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1613 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1614 | return true; |
| 1615 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1616 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1617 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1618 | TU))) |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | return false; |
| 1623 | } |
| 1624 | |
| 1625 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1626 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1629 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1630 | return Visit(TL.getInnerLoc()); |
| 1631 | } |
| 1632 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1633 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1634 | return Visit(TL.getPointeeLoc()); |
| 1635 | } |
| 1636 | |
| 1637 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1638 | return Visit(TL.getPointeeLoc()); |
| 1639 | } |
| 1640 | |
| 1641 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1642 | return Visit(TL.getPointeeLoc()); |
| 1643 | } |
| 1644 | |
| 1645 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1646 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1650 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1653 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1654 | return Visit(TL.getModifiedLoc()); |
| 1655 | } |
| 1656 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1657 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1658 | bool SkipResultType) { |
| 1659 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1660 | return true; |
| 1661 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1662 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1663 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1664 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1665 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1666 | |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
| 1670 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1671 | if (Visit(TL.getElementLoc())) |
| 1672 | return true; |
| 1673 | |
| 1674 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1675 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1676 | |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1680 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1681 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1682 | // Visit the template name. |
| 1683 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1684 | TL.getTemplateNameLoc())) |
| 1685 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1686 | |
| 1687 | // Visit the template arguments. |
| 1688 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1689 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1690 | return true; |
| 1691 | |
| 1692 | return false; |
| 1693 | } |
| 1694 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1695 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1696 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1697 | } |
| 1698 | |
| 1699 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1700 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1701 | return Visit(TSInfo->getTypeLoc()); |
| 1702 | |
| 1703 | return false; |
| 1704 | } |
| 1705 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1706 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1707 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1708 | return Visit(TSInfo->getTypeLoc()); |
| 1709 | |
| 1710 | return false; |
| 1711 | } |
| 1712 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1713 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1714 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1715 | return true; |
| 1716 | |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1720 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1721 | DependentTemplateSpecializationTypeLoc TL) { |
| 1722 | // Visit the nested-name-specifier, if there is one. |
| 1723 | if (TL.getQualifierLoc() && |
| 1724 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1725 | return true; |
| 1726 | |
| 1727 | // Visit the template arguments. |
| 1728 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1729 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1730 | return true; |
| 1731 | |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1735 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1736 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1737 | return true; |
| 1738 | |
| 1739 | return Visit(TL.getNamedTypeLoc()); |
| 1740 | } |
| 1741 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1742 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1743 | return Visit(TL.getPatternLoc()); |
| 1744 | } |
| 1745 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1746 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1747 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1748 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1749 | |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1754 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1755 | } |
| 1756 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1757 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1758 | return Visit(TL.getValueLoc()); |
| 1759 | } |
| 1760 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1761 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1762 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1763 | return Visit##PARENT##Loc(TL); \ |
| 1764 | } |
| 1765 | |
| 1766 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1767 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1768 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1769 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1770 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1771 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1772 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1773 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1774 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1775 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1776 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1777 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1778 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1779 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1780 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1781 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1782 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1783 | // Visit the nested-name-specifier, if present. |
| 1784 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1785 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1786 | return true; |
| 1787 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1788 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1789 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1790 | E = D->bases_end(); I != E; ++I) { |
| 1791 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1792 | return true; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | return VisitTagDecl(D); |
| 1797 | } |
| 1798 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1799 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1800 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1801 | i != e; ++i) |
| 1802 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1803 | return true; |
| 1804 | |
| 1805 | return false; |
| 1806 | } |
| 1807 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1808 | //===----------------------------------------------------------------------===// |
| 1809 | // Data-recursive visitor methods. |
| 1810 | //===----------------------------------------------------------------------===// |
| 1811 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1812 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1813 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1814 | class NAME : public VisitorJob {\ |
| 1815 | public:\ |
| 1816 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1817 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1818 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1819 | }; |
| 1820 | |
| 1821 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1822 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1823 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1824 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1825 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1826 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1827 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1828 | #undef DEF_JOB |
| 1829 | |
| 1830 | class DeclVisit : public VisitorJob { |
| 1831 | public: |
| 1832 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1833 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1834 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1835 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1836 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1837 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1838 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1839 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1840 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1841 | class TypeLocVisit : public VisitorJob { |
| 1842 | public: |
| 1843 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1844 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1845 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1846 | |
| 1847 | static bool classof(const VisitorJob *VJ) { |
| 1848 | return VJ->getKind() == TypeLocVisitKind; |
| 1849 | } |
| 1850 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1851 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1852 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1853 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1854 | } |
| 1855 | }; |
| 1856 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1857 | class LabelRefVisit : public VisitorJob { |
| 1858 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1859 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1860 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1861 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1862 | |
| 1863 | static bool classof(const VisitorJob *VJ) { |
| 1864 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1865 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1866 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1867 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1868 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1869 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1870 | |
| 1871 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1872 | public: |
| 1873 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1874 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1875 | Qualifier.getNestedNameSpecifier(), |
| 1876 | Qualifier.getOpaqueData()) { } |
| 1877 | |
| 1878 | static bool classof(const VisitorJob *VJ) { |
| 1879 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1880 | } |
| 1881 | |
| 1882 | NestedNameSpecifierLoc get() const { |
| 1883 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1884 | data[1]); |
| 1885 | } |
| 1886 | }; |
| 1887 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1888 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1889 | public: |
| 1890 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1891 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1892 | static bool classof(const VisitorJob *VJ) { |
| 1893 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1894 | } |
| 1895 | DeclarationNameInfo get() const { |
| 1896 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1897 | switch (S->getStmtClass()) { |
| 1898 | default: |
| 1899 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1900 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1901 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1902 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1903 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1904 | case Stmt::DependentScopeDeclRefExprClass: |
| 1905 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1906 | } |
| 1907 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1908 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1909 | class MemberRefVisit : public VisitorJob { |
| 1910 | public: |
| 1911 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1912 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1913 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1914 | static bool classof(const VisitorJob *VJ) { |
| 1915 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1916 | } |
| 1917 | FieldDecl *get() const { |
| 1918 | return static_cast<FieldDecl*>(data[0]); |
| 1919 | } |
| 1920 | SourceLocation getLoc() const { |
| 1921 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1922 | } |
| 1923 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1924 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1925 | VisitorWorkList &WL; |
| 1926 | CXCursor Parent; |
| 1927 | public: |
| 1928 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1929 | : WL(wl), Parent(parent) {} |
| 1930 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1931 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1932 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1933 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1934 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1935 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1936 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1937 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1938 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1939 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1940 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1941 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1942 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1943 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1944 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1945 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1946 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1947 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1948 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1949 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1950 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1951 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1952 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1953 | void VisitIfStmt(IfStmt *If); |
| 1954 | void VisitInitListExpr(InitListExpr *IE); |
| 1955 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1956 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1957 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1958 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1959 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1960 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1961 | void VisitStmt(Stmt *S); |
| 1962 | void VisitSwitchStmt(SwitchStmt *S); |
| 1963 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1964 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1965 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1966 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1967 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1968 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1969 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1970 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1971 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1972 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1973 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1974 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1975 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1976 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1977 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1978 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1979 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1980 | void EnqueueChildren(Stmt *S); |
| 1981 | }; |
| 1982 | } // end anonyous namespace |
| 1983 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1984 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1985 | // 'S' should always be non-null, since it comes from the |
| 1986 | // statement we are visiting. |
| 1987 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1988 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1989 | |
| 1990 | void |
| 1991 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1992 | if (Qualifier) |
| 1993 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1994 | } |
| 1995 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1996 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1997 | if (S) |
| 1998 | WL.push_back(StmtVisit(S, Parent)); |
| 1999 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 2000 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2001 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 2002 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2003 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2004 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2005 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2006 | if (A) |
| 2007 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2008 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2009 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2010 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 2011 | if (D) |
| 2012 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2013 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2014 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2015 | if (TI) |
| 2016 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2017 | } |
| 2018 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 2019 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 2020 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2021 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 2022 | } |
| 2023 | if (size == WL.size()) |
| 2024 | return; |
| 2025 | // Now reverse the entries we just added. This will match the DFS |
| 2026 | // ordering performed by the worklist. |
| 2027 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2028 | std::reverse(I, E); |
| 2029 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2030 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 2031 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2032 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2033 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 2034 | AddDecl(B->getBlockDecl()); |
| 2035 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2036 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 2037 | EnqueueChildren(E); |
| 2038 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2039 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 2040 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 2041 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 2042 | E = S->body_rend(); I != E; ++I) { |
| 2043 | AddStmt(*I); |
| 2044 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 2045 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2046 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2047 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 2048 | AddStmt(S->getSubStmt()); |
| 2049 | AddDeclarationNameInfo(S); |
| 2050 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2051 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2052 | } |
| 2053 | |
| 2054 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2055 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 2056 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 2057 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2058 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2059 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2060 | if (!E->isImplicitAccess()) |
| 2061 | AddStmt(E->getBase()); |
| 2062 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 2063 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 2064 | // Enqueue the initializer or constructor arguments. |
| 2065 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 2066 | AddStmt(E->getConstructorArg(I-1)); |
| 2067 | // Enqueue the array size, if any. |
| 2068 | AddStmt(E->getArraySize()); |
| 2069 | // Enqueue the allocated type. |
| 2070 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2071 | // Enqueue the placement arguments. |
| 2072 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2073 | AddStmt(E->getPlacementArg(I-1)); |
| 2074 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2075 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 2076 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2077 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2078 | AddStmt(CE->getCallee()); |
| 2079 | AddStmt(CE->getArg(0)); |
| 2080 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2081 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 2082 | // Visit the name of the type being destroyed. |
| 2083 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2084 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2085 | // but isn't. |
| 2086 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2087 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2088 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2089 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2090 | // Visit base expression. |
| 2091 | AddStmt(E->getBase()); |
| 2092 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2093 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 2094 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2095 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2096 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 2097 | EnqueueChildren(E); |
| 2098 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2099 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 2100 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 2101 | EnqueueChildren(E); |
| 2102 | if (E->isTypeOperand()) |
| 2103 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2104 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 2105 | |
| 2106 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 2107 | *E) { |
| 2108 | EnqueueChildren(E); |
| 2109 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2110 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 2111 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 2112 | EnqueueChildren(E); |
| 2113 | if (E->isTypeOperand()) |
| 2114 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2115 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2116 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2117 | if (DR->hasExplicitTemplateArgs()) { |
| 2118 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 2119 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2120 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2121 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2122 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 2123 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 2124 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2125 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2126 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 2127 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 2128 | unsigned size = WL.size(); |
| 2129 | bool isFirst = true; |
| 2130 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 2131 | D != DEnd; ++D) { |
| 2132 | AddDecl(*D, isFirst); |
| 2133 | isFirst = false; |
| 2134 | } |
| 2135 | if (size == WL.size()) |
| 2136 | return; |
| 2137 | // Now reverse the entries we just added. This will match the DFS |
| 2138 | // ordering performed by the worklist. |
| 2139 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2140 | std::reverse(I, E); |
| 2141 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2142 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 2143 | AddStmt(E->getInit()); |
| 2144 | typedef DesignatedInitExpr::Designator Designator; |
| 2145 | for (DesignatedInitExpr::reverse_designators_iterator |
| 2146 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 2147 | D != DEnd; ++D) { |
| 2148 | if (D->isFieldDesignator()) { |
| 2149 | if (FieldDecl *Field = D->getField()) |
| 2150 | AddMemberRef(Field, D->getFieldLoc()); |
| 2151 | continue; |
| 2152 | } |
| 2153 | if (D->isArrayDesignator()) { |
| 2154 | AddStmt(E->getArrayIndex(*D)); |
| 2155 | continue; |
| 2156 | } |
| 2157 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 2158 | AddStmt(E->getArrayRangeEnd(*D)); |
| 2159 | AddStmt(E->getArrayRangeStart(*D)); |
| 2160 | } |
| 2161 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2162 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 2163 | EnqueueChildren(E); |
| 2164 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2165 | } |
| 2166 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 2167 | AddStmt(FS->getBody()); |
| 2168 | AddStmt(FS->getInc()); |
| 2169 | AddStmt(FS->getCond()); |
| 2170 | AddDecl(FS->getConditionVariable()); |
| 2171 | AddStmt(FS->getInit()); |
| 2172 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2173 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 2174 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2175 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2176 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 2177 | AddStmt(If->getElse()); |
| 2178 | AddStmt(If->getThen()); |
| 2179 | AddStmt(If->getCond()); |
| 2180 | AddDecl(If->getConditionVariable()); |
| 2181 | } |
| 2182 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 2183 | // We care about the syntactic form of the initializer list, only. |
| 2184 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2185 | IE = Syntactic; |
| 2186 | EnqueueChildren(IE); |
| 2187 | } |
| 2188 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 2189 | WL.push_back(MemberExprParts(M, Parent)); |
| 2190 | |
| 2191 | // If the base of the member access expression is an implicit 'this', don't |
| 2192 | // visit it. |
| 2193 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2194 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2195 | if (!M->isImplicitAccess()) |
| 2196 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2197 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2198 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2199 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2200 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2201 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2202 | EnqueueChildren(M); |
| 2203 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2204 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2205 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2206 | // Visit the components of the offsetof expression. |
| 2207 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2208 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2209 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2210 | switch (Node.getKind()) { |
| 2211 | case OffsetOfNode::Array: |
| 2212 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2213 | break; |
| 2214 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2215 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | case OffsetOfNode::Identifier: |
| 2218 | case OffsetOfNode::Base: |
| 2219 | continue; |
| 2220 | } |
| 2221 | } |
| 2222 | // Visit the type into which we're computing the offset. |
| 2223 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2224 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2225 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2226 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2227 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2228 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2229 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2230 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2231 | EnqueueChildren(E); |
| 2232 | if (E->isArgumentType()) |
| 2233 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2234 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2235 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2236 | EnqueueChildren(S); |
| 2237 | } |
| 2238 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2239 | AddStmt(S->getBody()); |
| 2240 | AddStmt(S->getCond()); |
| 2241 | AddDecl(S->getConditionVariable()); |
| 2242 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2243 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2244 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2245 | AddStmt(W->getBody()); |
| 2246 | AddStmt(W->getCond()); |
| 2247 | AddDecl(W->getConditionVariable()); |
| 2248 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2249 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2250 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2251 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2252 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2253 | |
| 2254 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2255 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2256 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2259 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2260 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2261 | } |
| 2262 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2263 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2264 | EnqueueChildren(E); |
| 2265 | } |
| 2266 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2267 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2268 | VisitOverloadExpr(U); |
| 2269 | if (!U->isImplicitAccess()) |
| 2270 | AddStmt(U->getBase()); |
| 2271 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2272 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2273 | AddStmt(E->getSubExpr()); |
| 2274 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2275 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2276 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2277 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2278 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2279 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2280 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2281 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2285 | if (RegionOfInterest.isValid()) { |
| 2286 | SourceRange Range = getRawCursorExtent(C); |
| 2287 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2288 | return false; |
| 2289 | } |
| 2290 | return true; |
| 2291 | } |
| 2292 | |
| 2293 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2294 | while (!WL.empty()) { |
| 2295 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2296 | VisitorJob LI = WL.back(); |
| 2297 | WL.pop_back(); |
| 2298 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2299 | // Set the Parent field, then back to its old value once we're done. |
| 2300 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2301 | |
| 2302 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2303 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2304 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2305 | if (!D) |
| 2306 | continue; |
| 2307 | |
| 2308 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2309 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2310 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2311 | return true; |
| 2312 | |
| 2313 | continue; |
| 2314 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2315 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2316 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2317 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2318 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2319 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2320 | Arg != ArgEnd; ++Arg) { |
| 2321 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2322 | return true; |
| 2323 | } |
| 2324 | continue; |
| 2325 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2326 | case VisitorJob::TypeLocVisitKind: { |
| 2327 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2328 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2329 | return true; |
| 2330 | continue; |
| 2331 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2332 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2333 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2334 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2335 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2336 | TU))) { |
| 2337 | return true; |
| 2338 | } |
| 2339 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2340 | continue; |
| 2341 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2342 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2343 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2344 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2345 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2346 | return true; |
| 2347 | continue; |
| 2348 | } |
| 2349 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2350 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2351 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2352 | ->get())) |
| 2353 | return true; |
| 2354 | continue; |
| 2355 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2356 | case VisitorJob::MemberRefVisitKind: { |
| 2357 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2358 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2359 | return true; |
| 2360 | continue; |
| 2361 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2362 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2363 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2364 | if (!S) |
| 2365 | continue; |
| 2366 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2367 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2368 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2369 | if (!IsInRegionOfInterest(Cursor)) |
| 2370 | continue; |
| 2371 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2372 | case CXChildVisit_Break: return true; |
| 2373 | case CXChildVisit_Continue: break; |
| 2374 | case CXChildVisit_Recurse: |
| 2375 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2376 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2377 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2378 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2379 | } |
| 2380 | case VisitorJob::MemberExprPartsKind: { |
| 2381 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2382 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2383 | |
| 2384 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2385 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2386 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2387 | return true; |
| 2388 | |
| 2389 | // Visit the declaration name. |
| 2390 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2391 | return true; |
| 2392 | |
| 2393 | // Visit the explicitly-specified template arguments, if any. |
| 2394 | if (M->hasExplicitTemplateArgs()) { |
| 2395 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2396 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2397 | Arg != ArgEnd; ++Arg) { |
| 2398 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2399 | return true; |
| 2400 | } |
| 2401 | } |
| 2402 | continue; |
| 2403 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2404 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2405 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2406 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2407 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2408 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2409 | return true; |
| 2410 | // Visit declaration name. |
| 2411 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2412 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2413 | continue; |
| 2414 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2415 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2416 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2417 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2418 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2419 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2420 | return true; |
| 2421 | // Visit the declaration name. |
| 2422 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2423 | return true; |
| 2424 | // Visit the overloaded declaration reference. |
| 2425 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2426 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2427 | continue; |
| 2428 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2429 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2430 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2431 | NamedDecl *Pack = E->getPack(); |
| 2432 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2433 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2434 | E->getPackLoc(), TU))) |
| 2435 | return true; |
| 2436 | |
| 2437 | continue; |
| 2438 | } |
| 2439 | |
| 2440 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2441 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2442 | E->getPackLoc(), TU))) |
| 2443 | return true; |
| 2444 | |
| 2445 | continue; |
| 2446 | } |
| 2447 | |
| 2448 | // Non-type template parameter packs and function parameter packs are |
| 2449 | // treated like DeclRefExpr cursors. |
| 2450 | continue; |
| 2451 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | return false; |
| 2455 | } |
| 2456 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2457 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2458 | VisitorWorkList *WL = 0; |
| 2459 | if (!WorkListFreeList.empty()) { |
| 2460 | WL = WorkListFreeList.back(); |
| 2461 | WL->clear(); |
| 2462 | WorkListFreeList.pop_back(); |
| 2463 | } |
| 2464 | else { |
| 2465 | WL = new VisitorWorkList(); |
| 2466 | WorkListCache.push_back(WL); |
| 2467 | } |
| 2468 | EnqueueWorkList(*WL, S); |
| 2469 | bool result = RunVisitorWorkList(*WL); |
| 2470 | WorkListFreeList.push_back(WL); |
| 2471 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2474 | namespace { |
| 2475 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2476 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2477 | const DeclarationNameInfo &NI, |
| 2478 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2479 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2480 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2481 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2482 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2483 | |
| 2484 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2485 | |
| 2486 | RefNamePieces Pieces; |
| 2487 | |
| 2488 | if (WantQualifier && QLoc.isValid()) |
| 2489 | Pieces.push_back(QLoc); |
| 2490 | |
| 2491 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2492 | Pieces.push_back(NI.getLoc()); |
| 2493 | |
| 2494 | if (WantTemplateArgs && TemplateArgs) |
| 2495 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2496 | TemplateArgs->RAngleLoc)); |
| 2497 | |
| 2498 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2499 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2500 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2501 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2502 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2503 | } |
| 2504 | |
| 2505 | if (WantSinglePiece) { |
| 2506 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2507 | Pieces.clear(); |
| 2508 | Pieces.push_back(R); |
| 2509 | } |
| 2510 | |
| 2511 | return Pieces; |
| 2512 | } |
| 2513 | } |
| 2514 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2515 | //===----------------------------------------------------------------------===// |
| 2516 | // Misc. API hooks. |
| 2517 | //===----------------------------------------------------------------------===// |
| 2518 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2519 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2520 | static bool EnabledMultithreading; |
| 2521 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2522 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2523 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2524 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2525 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2526 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2527 | llvm::DisablePrettyStackTrace = true; |
| 2528 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2529 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2530 | // enable it. |
| 2531 | llvm::CrashRecoveryContext::Enable(); |
| 2532 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2533 | // Enable support for multithreading in LLVM. |
| 2534 | { |
| 2535 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2536 | if (!EnabledMultithreading) { |
| 2537 | llvm::llvm_start_multithreaded(); |
| 2538 | EnabledMultithreading = true; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2542 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2543 | if (excludeDeclarationsFromPCH) |
| 2544 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2545 | if (displayDiagnostics) |
| 2546 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2547 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2550 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2551 | if (CIdx) |
| 2552 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2555 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2556 | if (isEnabled) |
| 2557 | llvm::CrashRecoveryContext::Enable(); |
| 2558 | else |
| 2559 | llvm::CrashRecoveryContext::Disable(); |
| 2560 | } |
| 2561 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2562 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2563 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2564 | if (!CIdx) |
| 2565 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2567 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2568 | FileSystemOptions FileSystemOpts; |
| 2569 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2570 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2571 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2572 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2573 | CXXIdx->getOnlyLocalDecls(), |
| 2574 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2575 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2578 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2579 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2580 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2583 | CXTranslationUnit |
| 2584 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2585 | const char *source_filename, |
| 2586 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2587 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2588 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2589 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2590 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2591 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2592 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2593 | command_line_args, num_command_line_args, |
| 2594 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2595 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2596 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2597 | |
| 2598 | struct ParseTranslationUnitInfo { |
| 2599 | CXIndex CIdx; |
| 2600 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2601 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2602 | int num_command_line_args; |
| 2603 | struct CXUnsavedFile *unsaved_files; |
| 2604 | unsigned num_unsaved_files; |
| 2605 | unsigned options; |
| 2606 | CXTranslationUnit result; |
| 2607 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2608 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2609 | ParseTranslationUnitInfo *PTUI = |
| 2610 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2611 | CXIndex CIdx = PTUI->CIdx; |
| 2612 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2613 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2614 | int num_command_line_args = PTUI->num_command_line_args; |
| 2615 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2616 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2617 | unsigned options = PTUI->options; |
| 2618 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2619 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2620 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2621 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2622 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2623 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2624 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2625 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2626 | // FIXME: Add a flag for modules. |
| 2627 | TranslationUnitKind TUKind |
| 2628 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2629 | bool CacheCodeCompetionResults |
| 2630 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2631 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2632 | // Configure the diagnostics. |
| 2633 | DiagnosticOptions DiagOpts; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2634 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2635 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2636 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2637 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2638 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2639 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2640 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2641 | DiagCleanup(Diags.getPtr()); |
| 2642 | |
| 2643 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2644 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2645 | |
| 2646 | // Recover resources if we crash before exiting this function. |
| 2647 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2648 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2649 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2650 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2651 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2652 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2653 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2654 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2655 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2656 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2657 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2658 | llvm::OwningPtr<std::vector<const char *> > |
| 2659 | Args(new std::vector<const char*>()); |
| 2660 | |
| 2661 | // Recover resources if we crash before exiting this method. |
| 2662 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2663 | ArgsCleanup(Args.get()); |
| 2664 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2665 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2666 | // (often very broken) source code, where spell-checking can have a |
| 2667 | // significant negative impact on performance (particularly when |
| 2668 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2669 | // Only do this if we haven't found a spell-checking-related argument. |
| 2670 | bool FoundSpellCheckingArgument = false; |
| 2671 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2672 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2673 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2674 | FoundSpellCheckingArgument = true; |
| 2675 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2676 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2677 | } |
| 2678 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2679 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2680 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2681 | Args->insert(Args->end(), command_line_args, |
| 2682 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2683 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2684 | // The 'source_filename' argument is optional. If the caller does not |
| 2685 | // specify it then it is assumed that the source file is specified |
| 2686 | // in the actual argument list. |
| 2687 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2688 | // present it will be unused. |
| 2689 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2690 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2691 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2692 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2693 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2694 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2695 | Args->push_back("-Xclang"); |
| 2696 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2697 | NestedMacroExpansions |
| 2698 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2701 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2702 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2703 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2704 | /* vector::data() not portable */, |
| 2705 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2706 | Diags, |
| 2707 | CXXIdx->getClangResourcesPath(), |
| 2708 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2709 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2710 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2711 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2712 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2713 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2714 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2715 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2716 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2717 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2718 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2719 | // Make sure to check that 'Unit' is non-NULL. |
| 2720 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2721 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2722 | DEnd = Unit->stored_diag_end(); |
| 2723 | D != DEnd; ++D) { |
| 2724 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2725 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2726 | clang_defaultDiagnosticDisplayOptions()); |
| 2727 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2728 | clang_disposeString(Msg); |
| 2729 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2730 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2731 | // On Windows, force a flush, since there may be multiple copies of |
| 2732 | // stderr and stdout in the file system, all with different buffers |
| 2733 | // but writing to the same device. |
| 2734 | fflush(stderr); |
| 2735 | #endif |
| 2736 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2737 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2738 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2739 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2740 | } |
| 2741 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2742 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2743 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2744 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2745 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2746 | unsigned num_unsaved_files, |
| 2747 | unsigned options) { |
| 2748 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2749 | num_command_line_args, unsaved_files, |
| 2750 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2751 | llvm::CrashRecoveryContext CRC; |
| 2752 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2753 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2754 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2755 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2756 | fprintf(stderr, " 'command_line_args' : ["); |
| 2757 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2758 | if (i) |
| 2759 | fprintf(stderr, ", "); |
| 2760 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2761 | } |
| 2762 | fprintf(stderr, "],\n"); |
| 2763 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2764 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2765 | if (i) |
| 2766 | fprintf(stderr, ", "); |
| 2767 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2768 | unsaved_files[i].Length); |
| 2769 | } |
| 2770 | fprintf(stderr, "],\n"); |
| 2771 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2772 | fprintf(stderr, "}\n"); |
| 2773 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2774 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2775 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2776 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2777 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2778 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2779 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2782 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2783 | return CXSaveTranslationUnit_None; |
| 2784 | } |
| 2785 | |
| 2786 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2787 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2788 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2789 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2791 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2792 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2793 | PrintLibclangResourceUsage(TU); |
| 2794 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2795 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2796 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2797 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2798 | if (CTUnit) { |
| 2799 | // If the translation unit has been marked as unsafe to free, just discard |
| 2800 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2801 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2804 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2805 | disposeCXStringPool(CTUnit->StringPool); |
| 2806 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2807 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2808 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2810 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2811 | return CXReparse_None; |
| 2812 | } |
| 2813 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2814 | struct ReparseTranslationUnitInfo { |
| 2815 | CXTranslationUnit TU; |
| 2816 | unsigned num_unsaved_files; |
| 2817 | struct CXUnsavedFile *unsaved_files; |
| 2818 | unsigned options; |
| 2819 | int result; |
| 2820 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2821 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2822 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2823 | ReparseTranslationUnitInfo *RTUI = |
| 2824 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2825 | CXTranslationUnit TU = RTUI->TU; |
| 2826 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2827 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2828 | unsigned options = RTUI->options; |
| 2829 | (void) options; |
| 2830 | RTUI->result = 1; |
| 2831 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2832 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2833 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2834 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2835 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2836 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2837 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2838 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2839 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2840 | |
| 2841 | // Recover resources if we crash before exiting this function. |
| 2842 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2843 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2844 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2845 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2846 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2847 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2848 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2849 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2850 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2853 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2854 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2855 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2856 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2857 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2858 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2859 | unsigned num_unsaved_files, |
| 2860 | struct CXUnsavedFile *unsaved_files, |
| 2861 | unsigned options) { |
| 2862 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2863 | options, 0 }; |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2864 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 2865 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2866 | clang_reparseTranslationUnit_Impl(&RTUI); |
| 2867 | return RTUI.result; |
| 2868 | } |
| 2869 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2870 | llvm::CrashRecoveryContext CRC; |
| 2871 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2872 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2873 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2874 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2875 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2876 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2877 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2878 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2879 | return RTUI.result; |
| 2880 | } |
| 2881 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2882 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2883 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2884 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2885 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2886 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2887 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2888 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2889 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2891 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2892 | CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2893 | return Result; |
| 2894 | } |
| 2895 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2896 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2897 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2898 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2899 | // CXFile Operations. |
| 2900 | //===----------------------------------------------------------------------===// |
| 2901 | |
| 2902 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2903 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2904 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2905 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2906 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2907 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2908 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2912 | if (!SFile) |
| 2913 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2914 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2915 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2916 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2917 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2918 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2919 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2920 | if (!tu) |
| 2921 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2922 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2923 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2925 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2926 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2927 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2929 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2930 | if (!tu || !file) |
| 2931 | return 0; |
| 2932 | |
| 2933 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2934 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2935 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2936 | .isFileMultipleIncludeGuarded(FEnt); |
| 2937 | } |
| 2938 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2939 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2940 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2941 | //===----------------------------------------------------------------------===// |
| 2942 | // CXCursor Operations. |
| 2943 | //===----------------------------------------------------------------------===// |
| 2944 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2945 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2946 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2947 | return getDeclFromExpr(CE->getSubExpr()); |
| 2948 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2949 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2950 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2951 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2952 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2953 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2954 | return ME->getMemberDecl(); |
| 2955 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2956 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2957 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2958 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2959 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2960 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2961 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2962 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2963 | if (!CE->isElidable()) |
| 2964 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2965 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2966 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2968 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2969 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2970 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 2971 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 2972 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2973 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2974 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 2975 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 2976 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2977 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2978 | return 0; |
| 2979 | } |
| 2980 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2981 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2982 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 2983 | return getLocationFromExpr(CE->getSubExpr()); |
| 2984 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2985 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2986 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2987 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2988 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2989 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2990 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2991 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2992 | return Member->getMemberLoc(); |
| 2993 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2994 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2995 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2996 | return SizeOfPack->getPackLoc(); |
| 2997 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2998 | return E->getLocStart(); |
| 2999 | } |
| 3000 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3001 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3002 | |
| 3003 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3004 | CXCursorVisitor visitor, |
| 3005 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 3006 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 3007 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3008 | return CursorVis.VisitChildren(parent); |
| 3009 | } |
| 3010 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3011 | #ifndef __has_feature |
| 3012 | #define __has_feature(x) 0 |
| 3013 | #endif |
| 3014 | #if __has_feature(blocks) |
| 3015 | typedef enum CXChildVisitResult |
| 3016 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 3017 | |
| 3018 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3019 | CXClientData client_data) { |
| 3020 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3021 | return block(cursor, parent); |
| 3022 | } |
| 3023 | #else |
| 3024 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 3025 | // define and call the block manually, so the |
| 3026 | typedef struct _CXChildVisitResult |
| 3027 | { |
| 3028 | void *isa; |
| 3029 | int flags; |
| 3030 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3031 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 3032 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3033 | } *CXCursorVisitorBlock; |
| 3034 | |
| 3035 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3036 | CXClientData client_data) { |
| 3037 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3038 | return block->invoke(block, cursor, parent); |
| 3039 | } |
| 3040 | #endif |
| 3041 | |
| 3042 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3043 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 3044 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3045 | return clang_visitChildren(parent, visitWithBlock, block); |
| 3046 | } |
| 3047 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3048 | static CXString getDeclSpelling(Decl *D) { |
| 3049 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3050 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3051 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3052 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3053 | return createCXString(Property->getIdentifier()->getName()); |
| 3054 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3055 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3058 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3059 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3061 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 3062 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 3063 | // and returns different names. NamedDecl returns the class name and |
| 3064 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3065 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3066 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3067 | if (isa<UsingDirectiveDecl>(D)) |
| 3068 | return createCXString(""); |
| 3069 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 3070 | llvm::SmallString<1024> S; |
| 3071 | llvm::raw_svector_ostream os(S); |
| 3072 | ND->printName(os); |
| 3073 | |
| 3074 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3075 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3076 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3077 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3078 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3079 | return clang_getTranslationUnitSpelling( |
| 3080 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3081 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3082 | if (clang_isReference(C.kind)) { |
| 3083 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3084 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 3085 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3086 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3087 | } |
| 3088 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 3089 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3090 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3091 | } |
| 3092 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 3093 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3094 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3095 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3096 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3097 | case CXCursor_CXXBaseSpecifier: { |
| 3098 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 3099 | return createCXString(B->getType().getAsString()); |
| 3100 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3101 | case CXCursor_TypeRef: { |
| 3102 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 3103 | assert(Type && "Missing type decl"); |
| 3104 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3105 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 3106 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3107 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3108 | case CXCursor_TemplateRef: { |
| 3109 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3110 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3111 | |
| 3112 | return createCXString(Template->getNameAsString()); |
| 3113 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3114 | |
| 3115 | case CXCursor_NamespaceRef: { |
| 3116 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 3117 | assert(NS && "Missing namespace decl"); |
| 3118 | |
| 3119 | return createCXString(NS->getNameAsString()); |
| 3120 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3121 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3122 | case CXCursor_MemberRef: { |
| 3123 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3124 | assert(Field && "Missing member decl"); |
| 3125 | |
| 3126 | return createCXString(Field->getNameAsString()); |
| 3127 | } |
| 3128 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3129 | case CXCursor_LabelRef: { |
| 3130 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3131 | assert(Label && "Missing label"); |
| 3132 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3133 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3136 | case CXCursor_OverloadedDeclRef: { |
| 3137 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3138 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3139 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3140 | return createCXString(ND->getNameAsString()); |
| 3141 | return createCXString(""); |
| 3142 | } |
| 3143 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3144 | return createCXString(E->getName().getAsString()); |
| 3145 | OverloadedTemplateStorage *Ovl |
| 3146 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3147 | if (Ovl->size() == 0) |
| 3148 | return createCXString(""); |
| 3149 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3150 | } |
| 3151 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3152 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3153 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3154 | } |
| 3155 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3156 | |
| 3157 | if (clang_isExpression(C.kind)) { |
| 3158 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3159 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3160 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3161 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3164 | if (clang_isStatement(C.kind)) { |
| 3165 | Stmt *S = getCursorStmt(C); |
| 3166 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3167 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3168 | |
| 3169 | return createCXString(""); |
| 3170 | } |
| 3171 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3172 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3173 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3174 | ->getNameStart()); |
| 3175 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3176 | if (C.kind == CXCursor_MacroDefinition) |
| 3177 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3178 | ->getNameStart()); |
| 3179 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3180 | if (C.kind == CXCursor_InclusionDirective) |
| 3181 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3182 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3183 | if (clang_isDeclaration(C.kind)) |
| 3184 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3185 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3186 | if (C.kind == CXCursor_AnnotateAttr) { |
| 3187 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 3188 | return createCXString(AA->getAnnotation()); |
| 3189 | } |
| 3190 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3191 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3194 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3195 | if (!clang_isDeclaration(C.kind)) |
| 3196 | return clang_getCursorSpelling(C); |
| 3197 | |
| 3198 | Decl *D = getCursorDecl(C); |
| 3199 | if (!D) |
| 3200 | return createCXString(""); |
| 3201 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3202 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3203 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3204 | D = FunTmpl->getTemplatedDecl(); |
| 3205 | |
| 3206 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 3207 | llvm::SmallString<64> Str; |
| 3208 | llvm::raw_svector_ostream OS(Str); |
| 3209 | OS << Function->getNameAsString(); |
| 3210 | if (Function->getPrimaryTemplate()) |
| 3211 | OS << "<>"; |
| 3212 | OS << "("; |
| 3213 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3214 | if (I) |
| 3215 | OS << ", "; |
| 3216 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3217 | } |
| 3218 | |
| 3219 | if (Function->isVariadic()) { |
| 3220 | if (Function->getNumParams()) |
| 3221 | OS << ", "; |
| 3222 | OS << "..."; |
| 3223 | } |
| 3224 | OS << ")"; |
| 3225 | return createCXString(OS.str()); |
| 3226 | } |
| 3227 | |
| 3228 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 3229 | llvm::SmallString<64> Str; |
| 3230 | llvm::raw_svector_ostream OS(Str); |
| 3231 | OS << ClassTemplate->getNameAsString(); |
| 3232 | OS << "<"; |
| 3233 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3234 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3235 | if (I) |
| 3236 | OS << ", "; |
| 3237 | |
| 3238 | NamedDecl *Param = Params->getParam(I); |
| 3239 | if (Param->getIdentifier()) { |
| 3240 | OS << Param->getIdentifier()->getName(); |
| 3241 | continue; |
| 3242 | } |
| 3243 | |
| 3244 | // There is no parameter name, which makes this tricky. Try to come up |
| 3245 | // with something useful that isn't too long. |
| 3246 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3247 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3248 | else if (NonTypeTemplateParmDecl *NTTP |
| 3249 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3250 | OS << NTTP->getType().getAsString(Policy); |
| 3251 | else |
| 3252 | OS << "template<...> class"; |
| 3253 | } |
| 3254 | |
| 3255 | OS << ">"; |
| 3256 | return createCXString(OS.str()); |
| 3257 | } |
| 3258 | |
| 3259 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3260 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3261 | // If the type was explicitly written, use that. |
| 3262 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3263 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3264 | |
| 3265 | llvm::SmallString<64> Str; |
| 3266 | llvm::raw_svector_ostream OS(Str); |
| 3267 | OS << ClassSpec->getNameAsString(); |
| 3268 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3269 | ClassSpec->getTemplateArgs().data(), |
| 3270 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3271 | Policy); |
| 3272 | return createCXString(OS.str()); |
| 3273 | } |
| 3274 | |
| 3275 | return clang_getCursorSpelling(C); |
| 3276 | } |
| 3277 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3278 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3279 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3280 | case CXCursor_FunctionDecl: |
| 3281 | return createCXString("FunctionDecl"); |
| 3282 | case CXCursor_TypedefDecl: |
| 3283 | return createCXString("TypedefDecl"); |
| 3284 | case CXCursor_EnumDecl: |
| 3285 | return createCXString("EnumDecl"); |
| 3286 | case CXCursor_EnumConstantDecl: |
| 3287 | return createCXString("EnumConstantDecl"); |
| 3288 | case CXCursor_StructDecl: |
| 3289 | return createCXString("StructDecl"); |
| 3290 | case CXCursor_UnionDecl: |
| 3291 | return createCXString("UnionDecl"); |
| 3292 | case CXCursor_ClassDecl: |
| 3293 | return createCXString("ClassDecl"); |
| 3294 | case CXCursor_FieldDecl: |
| 3295 | return createCXString("FieldDecl"); |
| 3296 | case CXCursor_VarDecl: |
| 3297 | return createCXString("VarDecl"); |
| 3298 | case CXCursor_ParmDecl: |
| 3299 | return createCXString("ParmDecl"); |
| 3300 | case CXCursor_ObjCInterfaceDecl: |
| 3301 | return createCXString("ObjCInterfaceDecl"); |
| 3302 | case CXCursor_ObjCCategoryDecl: |
| 3303 | return createCXString("ObjCCategoryDecl"); |
| 3304 | case CXCursor_ObjCProtocolDecl: |
| 3305 | return createCXString("ObjCProtocolDecl"); |
| 3306 | case CXCursor_ObjCPropertyDecl: |
| 3307 | return createCXString("ObjCPropertyDecl"); |
| 3308 | case CXCursor_ObjCIvarDecl: |
| 3309 | return createCXString("ObjCIvarDecl"); |
| 3310 | case CXCursor_ObjCInstanceMethodDecl: |
| 3311 | return createCXString("ObjCInstanceMethodDecl"); |
| 3312 | case CXCursor_ObjCClassMethodDecl: |
| 3313 | return createCXString("ObjCClassMethodDecl"); |
| 3314 | case CXCursor_ObjCImplementationDecl: |
| 3315 | return createCXString("ObjCImplementationDecl"); |
| 3316 | case CXCursor_ObjCCategoryImplDecl: |
| 3317 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3318 | case CXCursor_CXXMethod: |
| 3319 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3320 | case CXCursor_UnexposedDecl: |
| 3321 | return createCXString("UnexposedDecl"); |
| 3322 | case CXCursor_ObjCSuperClassRef: |
| 3323 | return createCXString("ObjCSuperClassRef"); |
| 3324 | case CXCursor_ObjCProtocolRef: |
| 3325 | return createCXString("ObjCProtocolRef"); |
| 3326 | case CXCursor_ObjCClassRef: |
| 3327 | return createCXString("ObjCClassRef"); |
| 3328 | case CXCursor_TypeRef: |
| 3329 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3330 | case CXCursor_TemplateRef: |
| 3331 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3332 | case CXCursor_NamespaceRef: |
| 3333 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3334 | case CXCursor_MemberRef: |
| 3335 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3336 | case CXCursor_LabelRef: |
| 3337 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3338 | case CXCursor_OverloadedDeclRef: |
| 3339 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3340 | case CXCursor_IntegerLiteral: |
| 3341 | return createCXString("IntegerLiteral"); |
| 3342 | case CXCursor_FloatingLiteral: |
| 3343 | return createCXString("FloatingLiteral"); |
| 3344 | case CXCursor_ImaginaryLiteral: |
| 3345 | return createCXString("ImaginaryLiteral"); |
| 3346 | case CXCursor_StringLiteral: |
| 3347 | return createCXString("StringLiteral"); |
| 3348 | case CXCursor_CharacterLiteral: |
| 3349 | return createCXString("CharacterLiteral"); |
| 3350 | case CXCursor_ParenExpr: |
| 3351 | return createCXString("ParenExpr"); |
| 3352 | case CXCursor_UnaryOperator: |
| 3353 | return createCXString("UnaryOperator"); |
| 3354 | case CXCursor_ArraySubscriptExpr: |
| 3355 | return createCXString("ArraySubscriptExpr"); |
| 3356 | case CXCursor_BinaryOperator: |
| 3357 | return createCXString("BinaryOperator"); |
| 3358 | case CXCursor_CompoundAssignOperator: |
| 3359 | return createCXString("CompoundAssignOperator"); |
| 3360 | case CXCursor_ConditionalOperator: |
| 3361 | return createCXString("ConditionalOperator"); |
| 3362 | case CXCursor_CStyleCastExpr: |
| 3363 | return createCXString("CStyleCastExpr"); |
| 3364 | case CXCursor_CompoundLiteralExpr: |
| 3365 | return createCXString("CompoundLiteralExpr"); |
| 3366 | case CXCursor_InitListExpr: |
| 3367 | return createCXString("InitListExpr"); |
| 3368 | case CXCursor_AddrLabelExpr: |
| 3369 | return createCXString("AddrLabelExpr"); |
| 3370 | case CXCursor_StmtExpr: |
| 3371 | return createCXString("StmtExpr"); |
| 3372 | case CXCursor_GenericSelectionExpr: |
| 3373 | return createCXString("GenericSelectionExpr"); |
| 3374 | case CXCursor_GNUNullExpr: |
| 3375 | return createCXString("GNUNullExpr"); |
| 3376 | case CXCursor_CXXStaticCastExpr: |
| 3377 | return createCXString("CXXStaticCastExpr"); |
| 3378 | case CXCursor_CXXDynamicCastExpr: |
| 3379 | return createCXString("CXXDynamicCastExpr"); |
| 3380 | case CXCursor_CXXReinterpretCastExpr: |
| 3381 | return createCXString("CXXReinterpretCastExpr"); |
| 3382 | case CXCursor_CXXConstCastExpr: |
| 3383 | return createCXString("CXXConstCastExpr"); |
| 3384 | case CXCursor_CXXFunctionalCastExpr: |
| 3385 | return createCXString("CXXFunctionalCastExpr"); |
| 3386 | case CXCursor_CXXTypeidExpr: |
| 3387 | return createCXString("CXXTypeidExpr"); |
| 3388 | case CXCursor_CXXBoolLiteralExpr: |
| 3389 | return createCXString("CXXBoolLiteralExpr"); |
| 3390 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3391 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3392 | case CXCursor_CXXThisExpr: |
| 3393 | return createCXString("CXXThisExpr"); |
| 3394 | case CXCursor_CXXThrowExpr: |
| 3395 | return createCXString("CXXThrowExpr"); |
| 3396 | case CXCursor_CXXNewExpr: |
| 3397 | return createCXString("CXXNewExpr"); |
| 3398 | case CXCursor_CXXDeleteExpr: |
| 3399 | return createCXString("CXXDeleteExpr"); |
| 3400 | case CXCursor_UnaryExpr: |
| 3401 | return createCXString("UnaryExpr"); |
| 3402 | case CXCursor_ObjCStringLiteral: |
| 3403 | return createCXString("ObjCStringLiteral"); |
| 3404 | case CXCursor_ObjCEncodeExpr: |
| 3405 | return createCXString("ObjCEncodeExpr"); |
| 3406 | case CXCursor_ObjCSelectorExpr: |
| 3407 | return createCXString("ObjCSelectorExpr"); |
| 3408 | case CXCursor_ObjCProtocolExpr: |
| 3409 | return createCXString("ObjCProtocolExpr"); |
| 3410 | case CXCursor_ObjCBridgedCastExpr: |
| 3411 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3412 | case CXCursor_BlockExpr: |
| 3413 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3414 | case CXCursor_PackExpansionExpr: |
| 3415 | return createCXString("PackExpansionExpr"); |
| 3416 | case CXCursor_SizeOfPackExpr: |
| 3417 | return createCXString("SizeOfPackExpr"); |
| 3418 | case CXCursor_UnexposedExpr: |
| 3419 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3420 | case CXCursor_DeclRefExpr: |
| 3421 | return createCXString("DeclRefExpr"); |
| 3422 | case CXCursor_MemberRefExpr: |
| 3423 | return createCXString("MemberRefExpr"); |
| 3424 | case CXCursor_CallExpr: |
| 3425 | return createCXString("CallExpr"); |
| 3426 | case CXCursor_ObjCMessageExpr: |
| 3427 | return createCXString("ObjCMessageExpr"); |
| 3428 | case CXCursor_UnexposedStmt: |
| 3429 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3430 | case CXCursor_DeclStmt: |
| 3431 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3432 | case CXCursor_LabelStmt: |
| 3433 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3434 | case CXCursor_CompoundStmt: |
| 3435 | return createCXString("CompoundStmt"); |
| 3436 | case CXCursor_CaseStmt: |
| 3437 | return createCXString("CaseStmt"); |
| 3438 | case CXCursor_DefaultStmt: |
| 3439 | return createCXString("DefaultStmt"); |
| 3440 | case CXCursor_IfStmt: |
| 3441 | return createCXString("IfStmt"); |
| 3442 | case CXCursor_SwitchStmt: |
| 3443 | return createCXString("SwitchStmt"); |
| 3444 | case CXCursor_WhileStmt: |
| 3445 | return createCXString("WhileStmt"); |
| 3446 | case CXCursor_DoStmt: |
| 3447 | return createCXString("DoStmt"); |
| 3448 | case CXCursor_ForStmt: |
| 3449 | return createCXString("ForStmt"); |
| 3450 | case CXCursor_GotoStmt: |
| 3451 | return createCXString("GotoStmt"); |
| 3452 | case CXCursor_IndirectGotoStmt: |
| 3453 | return createCXString("IndirectGotoStmt"); |
| 3454 | case CXCursor_ContinueStmt: |
| 3455 | return createCXString("ContinueStmt"); |
| 3456 | case CXCursor_BreakStmt: |
| 3457 | return createCXString("BreakStmt"); |
| 3458 | case CXCursor_ReturnStmt: |
| 3459 | return createCXString("ReturnStmt"); |
| 3460 | case CXCursor_AsmStmt: |
| 3461 | return createCXString("AsmStmt"); |
| 3462 | case CXCursor_ObjCAtTryStmt: |
| 3463 | return createCXString("ObjCAtTryStmt"); |
| 3464 | case CXCursor_ObjCAtCatchStmt: |
| 3465 | return createCXString("ObjCAtCatchStmt"); |
| 3466 | case CXCursor_ObjCAtFinallyStmt: |
| 3467 | return createCXString("ObjCAtFinallyStmt"); |
| 3468 | case CXCursor_ObjCAtThrowStmt: |
| 3469 | return createCXString("ObjCAtThrowStmt"); |
| 3470 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3471 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3472 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3473 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3474 | case CXCursor_ObjCForCollectionStmt: |
| 3475 | return createCXString("ObjCForCollectionStmt"); |
| 3476 | case CXCursor_CXXCatchStmt: |
| 3477 | return createCXString("CXXCatchStmt"); |
| 3478 | case CXCursor_CXXTryStmt: |
| 3479 | return createCXString("CXXTryStmt"); |
| 3480 | case CXCursor_CXXForRangeStmt: |
| 3481 | return createCXString("CXXForRangeStmt"); |
| 3482 | case CXCursor_SEHTryStmt: |
| 3483 | return createCXString("SEHTryStmt"); |
| 3484 | case CXCursor_SEHExceptStmt: |
| 3485 | return createCXString("SEHExceptStmt"); |
| 3486 | case CXCursor_SEHFinallyStmt: |
| 3487 | return createCXString("SEHFinallyStmt"); |
| 3488 | case CXCursor_NullStmt: |
| 3489 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3490 | case CXCursor_InvalidFile: |
| 3491 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3492 | case CXCursor_InvalidCode: |
| 3493 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3494 | case CXCursor_NoDeclFound: |
| 3495 | return createCXString("NoDeclFound"); |
| 3496 | case CXCursor_NotImplemented: |
| 3497 | return createCXString("NotImplemented"); |
| 3498 | case CXCursor_TranslationUnit: |
| 3499 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3500 | case CXCursor_UnexposedAttr: |
| 3501 | return createCXString("UnexposedAttr"); |
| 3502 | case CXCursor_IBActionAttr: |
| 3503 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3504 | case CXCursor_IBOutletAttr: |
| 3505 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3506 | case CXCursor_IBOutletCollectionAttr: |
| 3507 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3508 | case CXCursor_CXXFinalAttr: |
| 3509 | return createCXString("attribute(final)"); |
| 3510 | case CXCursor_CXXOverrideAttr: |
| 3511 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3512 | case CXCursor_AnnotateAttr: |
| 3513 | return createCXString("attribute(annotate)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3514 | case CXCursor_PreprocessingDirective: |
| 3515 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3516 | case CXCursor_MacroDefinition: |
| 3517 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3518 | case CXCursor_MacroExpansion: |
| 3519 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3520 | case CXCursor_InclusionDirective: |
| 3521 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3522 | case CXCursor_Namespace: |
| 3523 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3524 | case CXCursor_LinkageSpec: |
| 3525 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3526 | case CXCursor_CXXBaseSpecifier: |
| 3527 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3528 | case CXCursor_Constructor: |
| 3529 | return createCXString("CXXConstructor"); |
| 3530 | case CXCursor_Destructor: |
| 3531 | return createCXString("CXXDestructor"); |
| 3532 | case CXCursor_ConversionFunction: |
| 3533 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3534 | case CXCursor_TemplateTypeParameter: |
| 3535 | return createCXString("TemplateTypeParameter"); |
| 3536 | case CXCursor_NonTypeTemplateParameter: |
| 3537 | return createCXString("NonTypeTemplateParameter"); |
| 3538 | case CXCursor_TemplateTemplateParameter: |
| 3539 | return createCXString("TemplateTemplateParameter"); |
| 3540 | case CXCursor_FunctionTemplate: |
| 3541 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3542 | case CXCursor_ClassTemplate: |
| 3543 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3544 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3545 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3546 | case CXCursor_NamespaceAlias: |
| 3547 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3548 | case CXCursor_UsingDirective: |
| 3549 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3550 | case CXCursor_UsingDeclaration: |
| 3551 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3552 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3553 | return createCXString("TypeAliasDecl"); |
| 3554 | case CXCursor_ObjCSynthesizeDecl: |
| 3555 | return createCXString("ObjCSynthesizeDecl"); |
| 3556 | case CXCursor_ObjCDynamicDecl: |
| 3557 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3558 | case CXCursor_CXXAccessSpecifier: |
| 3559 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3560 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3561 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3562 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3563 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3564 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3565 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3566 | struct GetCursorData { |
| 3567 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3568 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3569 | CXCursor &BestCursor; |
| 3570 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3571 | GetCursorData(SourceManager &SM, |
| 3572 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3573 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3574 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3575 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3576 | }; |
| 3577 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3578 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3579 | CXCursor parent, |
| 3580 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3581 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3582 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3583 | |
| 3584 | // If we point inside a macro argument we should provide info of what the |
| 3585 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3586 | // cursor. |
| 3587 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3588 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3589 | |
| 3590 | if (clang_isDeclaration(cursor.kind)) { |
| 3591 | // Avoid having the implicit methods override the property decls. |
| 3592 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCursorDecl(cursor))) |
| 3593 | if (MD->isImplicit()) |
| 3594 | return CXChildVisit_Break; |
| 3595 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3596 | |
| 3597 | if (clang_isExpression(cursor.kind) && |
| 3598 | clang_isDeclaration(BestCursor->kind)) { |
| 3599 | Decl *D = getCursorDecl(*BestCursor); |
| 3600 | |
| 3601 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3602 | // when the expression source range overlaps the declaration range. |
| 3603 | // This can happen for C++ constructor expressions whose range generally |
| 3604 | // include the variable declaration, e.g.: |
| 3605 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3606 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3607 | D->getLocation() == Data->TokenBeginLoc) |
| 3608 | return CXChildVisit_Break; |
| 3609 | } |
| 3610 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3611 | // If our current best cursor is the construction of a temporary object, |
| 3612 | // don't replace that cursor with a type reference, because we want |
| 3613 | // clang_getCursor() to point at the constructor. |
| 3614 | if (clang_isExpression(BestCursor->kind) && |
| 3615 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3616 | cursor.kind == CXCursor_TypeRef) { |
| 3617 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3618 | // as having the actual point on the type reference. |
| 3619 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3620 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3621 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3623 | *BestCursor = cursor; |
| 3624 | return CXChildVisit_Recurse; |
| 3625 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3626 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3627 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3628 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3629 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3630 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3631 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3632 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3633 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3634 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3635 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3636 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3637 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3638 | if (Logging) { |
| 3639 | CXFile SearchFile; |
| 3640 | unsigned SearchLine, SearchColumn; |
| 3641 | CXFile ResultFile; |
| 3642 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3643 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3644 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3645 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3646 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3647 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3648 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3649 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3650 | SearchFileName = clang_getFileName(SearchFile); |
| 3651 | ResultFileName = clang_getFileName(ResultFile); |
| 3652 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3653 | USR = clang_getCursorUSR(Result); |
| 3654 | 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] | 3655 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3656 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3657 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3658 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3659 | clang_disposeString(SearchFileName); |
| 3660 | clang_disposeString(ResultFileName); |
| 3661 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3662 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3663 | |
| 3664 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3665 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3666 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3667 | CXString DefinitionKindSpelling |
| 3668 | = clang_getCursorKindSpelling(Definition.kind); |
| 3669 | CXFile DefinitionFile; |
| 3670 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3671 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3672 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3673 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3674 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3675 | clang_getCString(DefinitionKindSpelling), |
| 3676 | clang_getCString(DefinitionFileName), |
| 3677 | DefinitionLine, DefinitionColumn); |
| 3678 | clang_disposeString(DefinitionFileName); |
| 3679 | clang_disposeString(DefinitionKindSpelling); |
| 3680 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3683 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3686 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3687 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3691 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3692 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3693 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3694 | unsigned clang_hashCursor(CXCursor C) { |
| 3695 | unsigned Index = 0; |
| 3696 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3697 | Index = 1; |
| 3698 | |
| 3699 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3700 | std::make_pair(C.kind, C.data[Index])); |
| 3701 | } |
| 3702 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3703 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3704 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3705 | } |
| 3706 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3707 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3708 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3709 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3710 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3711 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3712 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3713 | } |
| 3714 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3715 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3716 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3717 | } |
| 3718 | |
| 3719 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3720 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3721 | } |
| 3722 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3723 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3724 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3725 | } |
| 3726 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3727 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3728 | return K == CXCursor_TranslationUnit; |
| 3729 | } |
| 3730 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3731 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3732 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3733 | } |
| 3734 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3735 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3736 | switch (K) { |
| 3737 | case CXCursor_UnexposedDecl: |
| 3738 | case CXCursor_UnexposedExpr: |
| 3739 | case CXCursor_UnexposedStmt: |
| 3740 | case CXCursor_UnexposedAttr: |
| 3741 | return true; |
| 3742 | default: |
| 3743 | return false; |
| 3744 | } |
| 3745 | } |
| 3746 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3747 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3748 | return C.kind; |
| 3749 | } |
| 3750 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3751 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3752 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3753 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3754 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3755 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3756 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3757 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3760 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3761 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3762 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3763 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3766 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3767 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3768 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3769 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3770 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3771 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3772 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3773 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3774 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3775 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3776 | |
| 3777 | case CXCursor_TemplateRef: { |
| 3778 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3779 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3780 | } |
| 3781 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3782 | case CXCursor_NamespaceRef: { |
| 3783 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3784 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3785 | } |
| 3786 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3787 | case CXCursor_MemberRef: { |
| 3788 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3789 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3790 | } |
| 3791 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3792 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3793 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3794 | if (!BaseSpec) |
| 3795 | return clang_getNullLocation(); |
| 3796 | |
| 3797 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3798 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3799 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3800 | |
| 3801 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3802 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3803 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3805 | case CXCursor_LabelRef: { |
| 3806 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3807 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3808 | } |
| 3809 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3810 | case CXCursor_OverloadedDeclRef: |
| 3811 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3812 | getCursorOverloadedDeclRef(C).second); |
| 3813 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3814 | default: |
| 3815 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3816 | llvm_unreachable("Missed a reference kind"); |
| 3817 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3818 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3819 | |
| 3820 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3821 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3822 | getLocationFromExpr(getCursorExpr(C))); |
| 3823 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3824 | if (clang_isStatement(C.kind)) |
| 3825 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3826 | getCursorStmt(C)->getLocStart()); |
| 3827 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3828 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3829 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3830 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3831 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3832 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3833 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3834 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3835 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3836 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3837 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3838 | |
| 3839 | if (C.kind == CXCursor_MacroDefinition) { |
| 3840 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3841 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3842 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3843 | |
| 3844 | if (C.kind == CXCursor_InclusionDirective) { |
| 3845 | SourceLocation L |
| 3846 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3847 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3848 | } |
| 3849 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3850 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3851 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3852 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3853 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3854 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3855 | // FIXME: Multiple variables declared in a single declaration |
| 3856 | // currently lack the information needed to correctly determine their |
| 3857 | // ranges when accounting for the type-specifier. We use context |
| 3858 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3859 | // and if so, whether it is the first decl. |
| 3860 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3861 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3862 | Loc = VD->getLocation(); |
| 3863 | } |
| 3864 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3865 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3866 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3867 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3868 | } // end extern "C" |
| 3869 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3870 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3871 | assert(TU); |
| 3872 | |
| 3873 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3874 | // of the following calls. |
| 3875 | if (SLoc.isInvalid()) |
| 3876 | return clang_getNullCursor(); |
| 3877 | |
| 3878 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3879 | |
| 3880 | // Translate the given source location to make it point at the beginning of |
| 3881 | // the token under the cursor. |
| 3882 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3883 | CXXUnit->getASTContext().getLangOptions()); |
| 3884 | |
| 3885 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3886 | if (SLoc.isValid()) { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3887 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3888 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3889 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 3890 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3891 | SourceLocation(SLoc)); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame^] | 3892 | CursorVis.visitFileRegion(); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | return Result; |
| 3896 | } |
| 3897 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3898 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3899 | if (clang_isReference(C.kind)) { |
| 3900 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3901 | case CXCursor_ObjCSuperClassRef: |
| 3902 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3903 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3904 | case CXCursor_ObjCProtocolRef: |
| 3905 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3907 | case CXCursor_ObjCClassRef: |
| 3908 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3909 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3910 | case CXCursor_TypeRef: |
| 3911 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3912 | |
| 3913 | case CXCursor_TemplateRef: |
| 3914 | return getCursorTemplateRef(C).second; |
| 3915 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3916 | case CXCursor_NamespaceRef: |
| 3917 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3918 | |
| 3919 | case CXCursor_MemberRef: |
| 3920 | return getCursorMemberRef(C).second; |
| 3921 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3922 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3923 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3925 | case CXCursor_LabelRef: |
| 3926 | return getCursorLabelRef(C).second; |
| 3927 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3928 | case CXCursor_OverloadedDeclRef: |
| 3929 | return getCursorOverloadedDeclRef(C).second; |
| 3930 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3931 | default: |
| 3932 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3933 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3936 | |
| 3937 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3938 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3939 | |
| 3940 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3941 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3942 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3943 | if (clang_isAttribute(C.kind)) |
| 3944 | return getCursorAttr(C)->getRange(); |
| 3945 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3946 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3947 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3948 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3949 | if (C.kind == CXCursor_MacroExpansion) { |
| 3950 | ASTUnit *TU = getCursorASTUnit(C); |
| 3951 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 3952 | return TU->mapRangeFromPreamble(Range); |
| 3953 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3954 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3955 | if (C.kind == CXCursor_MacroDefinition) { |
| 3956 | ASTUnit *TU = getCursorASTUnit(C); |
| 3957 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 3958 | return TU->mapRangeFromPreamble(Range); |
| 3959 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3960 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3961 | if (C.kind == CXCursor_InclusionDirective) { |
| 3962 | ASTUnit *TU = getCursorASTUnit(C); |
| 3963 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3964 | return TU->mapRangeFromPreamble(Range); |
| 3965 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3966 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3967 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3968 | Decl *D = cxcursor::getCursorDecl(C); |
| 3969 | SourceRange R = D->getSourceRange(); |
| 3970 | // FIXME: Multiple variables declared in a single declaration |
| 3971 | // currently lack the information needed to correctly determine their |
| 3972 | // ranges when accounting for the type-specifier. We use context |
| 3973 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3974 | // and if so, whether it is the first decl. |
| 3975 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3976 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3977 | R.setBegin(VD->getLocation()); |
| 3978 | } |
| 3979 | return R; |
| 3980 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3981 | return SourceRange(); |
| 3982 | } |
| 3983 | |
| 3984 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3985 | /// the decl-specifier-seq for declarations. |
| 3986 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3987 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3988 | Decl *D = cxcursor::getCursorDecl(C); |
| 3989 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3991 | // Adjust the start of the location for declarations preceded by |
| 3992 | // declaration specifiers. |
| 3993 | SourceLocation StartLoc; |
| 3994 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3995 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3996 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3997 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3998 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3999 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4000 | } |
| 4001 | |
| 4002 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 4003 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 4004 | R.setBegin(StartLoc); |
| 4005 | |
| 4006 | // FIXME: Multiple variables declared in a single declaration |
| 4007 | // currently lack the information needed to correctly determine their |
| 4008 | // ranges when accounting for the type-specifier. We use context |
| 4009 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 4010 | // and if so, whether it is the first decl. |
| 4011 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4012 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 4013 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | return R; |
| 4017 | } |
| 4018 | |
| 4019 | return getRawCursorExtent(C); |
| 4020 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4021 | |
| 4022 | extern "C" { |
| 4023 | |
| 4024 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 4025 | SourceRange R = getRawCursorExtent(C); |
| 4026 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4027 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4028 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4029 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4030 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4031 | |
| 4032 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4033 | if (clang_isInvalid(C.kind)) |
| 4034 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4035 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4036 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4037 | if (clang_isDeclaration(C.kind)) { |
| 4038 | Decl *D = getCursorDecl(C); |
| 4039 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4040 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4041 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4042 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4043 | if (ObjCForwardProtocolDecl *Protocols |
| 4044 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4045 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4046 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 4047 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 4048 | return MakeCXCursor(Property, tu); |
| 4049 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4050 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4053 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4054 | Expr *E = getCursorExpr(C); |
| 4055 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 4056 | if (D) { |
| 4057 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 4058 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 4059 | declCursor); |
| 4060 | return declCursor; |
| 4061 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4062 | |
| 4063 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4064 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4066 | return clang_getNullCursor(); |
| 4067 | } |
| 4068 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4069 | if (clang_isStatement(C.kind)) { |
| 4070 | Stmt *S = getCursorStmt(C); |
| 4071 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 4072 | if (LabelDecl *label = Goto->getLabel()) |
| 4073 | if (LabelStmt *labelS = label->getStmt()) |
| 4074 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4075 | |
| 4076 | return clang_getNullCursor(); |
| 4077 | } |
| 4078 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4079 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 4080 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4081 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4084 | if (!clang_isReference(C.kind)) |
| 4085 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4086 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4087 | switch (C.kind) { |
| 4088 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4089 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4090 | |
| 4091 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4092 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4093 | |
| 4094 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4095 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4096 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4097 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4098 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4099 | |
| 4100 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4101 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4103 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4104 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4105 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4106 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4107 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4108 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4109 | case CXCursor_CXXBaseSpecifier: { |
| 4110 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 4111 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4112 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4113 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4115 | case CXCursor_LabelRef: |
| 4116 | // FIXME: We end up faking the "parent" declaration here because we |
| 4117 | // don't want to make CXCursor larger. |
| 4118 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4119 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 4120 | .getTranslationUnitDecl(), |
| 4121 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4122 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4123 | case CXCursor_OverloadedDeclRef: |
| 4124 | return C; |
| 4125 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4126 | default: |
| 4127 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 4128 | llvm_unreachable("Unhandled reference cursor kind"); |
| 4129 | break; |
| 4130 | } |
| 4131 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4132 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4133 | return clang_getNullCursor(); |
| 4134 | } |
| 4135 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4136 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4137 | if (clang_isInvalid(C.kind)) |
| 4138 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4139 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4140 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4142 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4143 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4144 | C = clang_getCursorReferenced(C); |
| 4145 | WasReference = true; |
| 4146 | } |
| 4147 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4148 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4149 | return clang_getCursorReferenced(C); |
| 4150 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4151 | if (!clang_isDeclaration(C.kind)) |
| 4152 | return clang_getNullCursor(); |
| 4153 | |
| 4154 | Decl *D = getCursorDecl(C); |
| 4155 | if (!D) |
| 4156 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4157 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4158 | switch (D->getKind()) { |
| 4159 | // Declaration kinds that don't really separate the notions of |
| 4160 | // declaration and definition. |
| 4161 | case Decl::Namespace: |
| 4162 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4163 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4164 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4165 | case Decl::TemplateTypeParm: |
| 4166 | case Decl::EnumConstant: |
| 4167 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4168 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4169 | case Decl::ObjCIvar: |
| 4170 | case Decl::ObjCAtDefsField: |
| 4171 | case Decl::ImplicitParam: |
| 4172 | case Decl::ParmVar: |
| 4173 | case Decl::NonTypeTemplateParm: |
| 4174 | case Decl::TemplateTemplateParm: |
| 4175 | case Decl::ObjCCategoryImpl: |
| 4176 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4177 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4178 | case Decl::LinkageSpec: |
| 4179 | case Decl::ObjCPropertyImpl: |
| 4180 | case Decl::FileScopeAsm: |
| 4181 | case Decl::StaticAssert: |
| 4182 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4183 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4184 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4185 | return C; |
| 4186 | |
| 4187 | // Declaration kinds that don't make any sense here, but are |
| 4188 | // nonetheless harmless. |
| 4189 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4190 | break; |
| 4191 | |
| 4192 | // Declaration kinds for which the definition is not resolvable. |
| 4193 | case Decl::UnresolvedUsingTypename: |
| 4194 | case Decl::UnresolvedUsingValue: |
| 4195 | break; |
| 4196 | |
| 4197 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4198 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4199 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4200 | |
| 4201 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4202 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4203 | |
| 4204 | case Decl::Enum: |
| 4205 | case Decl::Record: |
| 4206 | case Decl::CXXRecord: |
| 4207 | case Decl::ClassTemplateSpecialization: |
| 4208 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4209 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4210 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4211 | return clang_getNullCursor(); |
| 4212 | |
| 4213 | case Decl::Function: |
| 4214 | case Decl::CXXMethod: |
| 4215 | case Decl::CXXConstructor: |
| 4216 | case Decl::CXXDestructor: |
| 4217 | case Decl::CXXConversion: { |
| 4218 | const FunctionDecl *Def = 0; |
| 4219 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4220 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4221 | return clang_getNullCursor(); |
| 4222 | } |
| 4223 | |
| 4224 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4225 | // Ask the variable if it has a definition. |
| 4226 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4227 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4228 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4229 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4231 | case Decl::FunctionTemplate: { |
| 4232 | const FunctionDecl *Def = 0; |
| 4233 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4234 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4235 | return clang_getNullCursor(); |
| 4236 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4238 | case Decl::ClassTemplate: { |
| 4239 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4240 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4241 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4242 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4243 | return clang_getNullCursor(); |
| 4244 | } |
| 4245 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4246 | case Decl::Using: |
| 4247 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4248 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4249 | |
| 4250 | case Decl::UsingShadow: |
| 4251 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4252 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4253 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4254 | |
| 4255 | case Decl::ObjCMethod: { |
| 4256 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4257 | if (Method->isThisDeclarationADefinition()) |
| 4258 | return C; |
| 4259 | |
| 4260 | // Dig out the method definition in the associated |
| 4261 | // @implementation, if we have it. |
| 4262 | // FIXME: The ASTs should make finding the definition easier. |
| 4263 | if (ObjCInterfaceDecl *Class |
| 4264 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4265 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4266 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4267 | Method->isInstanceMethod())) |
| 4268 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4269 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4270 | |
| 4271 | return clang_getNullCursor(); |
| 4272 | } |
| 4273 | |
| 4274 | case Decl::ObjCCategory: |
| 4275 | if (ObjCCategoryImplDecl *Impl |
| 4276 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4277 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4278 | return clang_getNullCursor(); |
| 4279 | |
| 4280 | case Decl::ObjCProtocol: |
| 4281 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4282 | return C; |
| 4283 | return clang_getNullCursor(); |
| 4284 | |
| 4285 | case Decl::ObjCInterface: |
| 4286 | // There are two notions of a "definition" for an Objective-C |
| 4287 | // class: the interface and its implementation. When we resolved a |
| 4288 | // reference to an Objective-C class, produce the @interface as |
| 4289 | // the definition; when we were provided with the interface, |
| 4290 | // produce the @implementation as the definition. |
| 4291 | if (WasReference) { |
| 4292 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4293 | return C; |
| 4294 | } else if (ObjCImplementationDecl *Impl |
| 4295 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4296 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4297 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4299 | case Decl::ObjCProperty: |
| 4300 | // FIXME: We don't really know where to find the |
| 4301 | // ObjCPropertyImplDecls that implement this property. |
| 4302 | return clang_getNullCursor(); |
| 4303 | |
| 4304 | case Decl::ObjCCompatibleAlias: |
| 4305 | if (ObjCInterfaceDecl *Class |
| 4306 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4307 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4308 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4310 | return clang_getNullCursor(); |
| 4311 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4312 | case Decl::ObjCForwardProtocol: |
| 4313 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4314 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4316 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4317 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4318 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4319 | |
| 4320 | case Decl::Friend: |
| 4321 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4322 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4323 | return clang_getNullCursor(); |
| 4324 | |
| 4325 | case Decl::FriendTemplate: |
| 4326 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4327 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4328 | return clang_getNullCursor(); |
| 4329 | } |
| 4330 | |
| 4331 | return clang_getNullCursor(); |
| 4332 | } |
| 4333 | |
| 4334 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4335 | if (!clang_isDeclaration(C.kind)) |
| 4336 | return 0; |
| 4337 | |
| 4338 | return clang_getCursorDefinition(C) == C; |
| 4339 | } |
| 4340 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4341 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4342 | if (!clang_isDeclaration(C.kind)) |
| 4343 | return C; |
| 4344 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4345 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4346 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4347 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4348 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4349 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4350 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4351 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4352 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4353 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4354 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4355 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4356 | |
| 4357 | return C; |
| 4358 | } |
| 4359 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4360 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4361 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4362 | return 0; |
| 4363 | |
| 4364 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4365 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4366 | return E->getNumDecls(); |
| 4367 | |
| 4368 | if (OverloadedTemplateStorage *S |
| 4369 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4370 | return S->size(); |
| 4371 | |
| 4372 | Decl *D = Storage.get<Decl*>(); |
| 4373 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4374 | return Using->shadow_size(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4375 | if (isa<ObjCClassDecl>(D)) |
| 4376 | return 1; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4377 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4378 | return Protocols->protocol_size(); |
| 4379 | |
| 4380 | return 0; |
| 4381 | } |
| 4382 | |
| 4383 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4384 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4385 | return clang_getNullCursor(); |
| 4386 | |
| 4387 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4388 | return clang_getNullCursor(); |
| 4389 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4390 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4391 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4392 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4393 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4394 | |
| 4395 | if (OverloadedTemplateStorage *S |
| 4396 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4397 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4398 | |
| 4399 | Decl *D = Storage.get<Decl*>(); |
| 4400 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4401 | // FIXME: This is, unfortunately, linear time. |
| 4402 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4403 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4404 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4405 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4406 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4407 | return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4408 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4409 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4410 | |
| 4411 | return clang_getNullCursor(); |
| 4412 | } |
| 4413 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4414 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4415 | const char **startBuf, |
| 4416 | const char **endBuf, |
| 4417 | unsigned *startLine, |
| 4418 | unsigned *startColumn, |
| 4419 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4420 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4421 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4422 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4423 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4424 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4425 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4426 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4427 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4428 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4429 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4430 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4431 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4432 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4433 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4435 | |
| 4436 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4437 | unsigned PieceIndex) { |
| 4438 | RefNamePieces Pieces; |
| 4439 | |
| 4440 | switch (C.kind) { |
| 4441 | case CXCursor_MemberRefExpr: |
| 4442 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4443 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4444 | E->getQualifierLoc().getSourceRange()); |
| 4445 | break; |
| 4446 | |
| 4447 | case CXCursor_DeclRefExpr: |
| 4448 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4449 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4450 | E->getQualifierLoc().getSourceRange(), |
| 4451 | E->getExplicitTemplateArgsOpt()); |
| 4452 | break; |
| 4453 | |
| 4454 | case CXCursor_CallExpr: |
| 4455 | if (CXXOperatorCallExpr *OCE = |
| 4456 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4457 | Expr *Callee = OCE->getCallee(); |
| 4458 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4459 | Callee = ICE->getSubExpr(); |
| 4460 | |
| 4461 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4462 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4463 | DRE->getQualifierLoc().getSourceRange()); |
| 4464 | } |
| 4465 | break; |
| 4466 | |
| 4467 | default: |
| 4468 | break; |
| 4469 | } |
| 4470 | |
| 4471 | if (Pieces.empty()) { |
| 4472 | if (PieceIndex == 0) |
| 4473 | return clang_getCursorExtent(C); |
| 4474 | } else if (PieceIndex < Pieces.size()) { |
| 4475 | SourceRange R = Pieces[PieceIndex]; |
| 4476 | if (R.isValid()) |
| 4477 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4478 | } |
| 4479 | |
| 4480 | return clang_getNullRange(); |
| 4481 | } |
| 4482 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4483 | void clang_enableStackTraces(void) { |
| 4484 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4485 | } |
| 4486 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4487 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4488 | unsigned stack_size) { |
| 4489 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4490 | } |
| 4491 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4492 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4493 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4494 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4495 | // Token-based Operations. |
| 4496 | //===----------------------------------------------------------------------===// |
| 4497 | |
| 4498 | /* CXToken layout: |
| 4499 | * int_data[0]: a CXTokenKind |
| 4500 | * int_data[1]: starting token location |
| 4501 | * int_data[2]: token length |
| 4502 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4503 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4504 | * otherwise unused. |
| 4505 | */ |
| 4506 | extern "C" { |
| 4507 | |
| 4508 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4509 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4510 | } |
| 4511 | |
| 4512 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4513 | switch (clang_getTokenKind(CXTok)) { |
| 4514 | case CXToken_Identifier: |
| 4515 | case CXToken_Keyword: |
| 4516 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4517 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4518 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4519 | |
| 4520 | case CXToken_Literal: { |
| 4521 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4522 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4523 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4524 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4525 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4526 | case CXToken_Punctuation: |
| 4527 | case CXToken_Comment: |
| 4528 | break; |
| 4529 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4530 | |
| 4531 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4532 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4533 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4534 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4535 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4537 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4538 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4539 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4540 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4541 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4542 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4543 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4544 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4545 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4546 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4547 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4549 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4550 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4551 | if (!CXXUnit) |
| 4552 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4553 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4554 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4555 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4556 | } |
| 4557 | |
| 4558 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4559 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4560 | if (!CXXUnit) |
| 4561 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4562 | |
| 4563 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4564 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4565 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4566 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4567 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4568 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4569 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4570 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4571 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4572 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4573 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4574 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4575 | // Cannot tokenize across files. |
| 4576 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4577 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4580 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4581 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4582 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4583 | if (Invalid) |
| 4584 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4586 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4587 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4588 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4589 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4590 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4591 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4592 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4593 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4594 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4595 | do { |
| 4596 | // Lex the next token |
| 4597 | Lex.LexFromRawLexer(Tok); |
| 4598 | if (Tok.is(tok::eof)) |
| 4599 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4600 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4601 | // Initialize the CXToken. |
| 4602 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4604 | // - Common fields |
| 4605 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4606 | CXTok.int_data[2] = Tok.getLength(); |
| 4607 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4609 | // - Kind-specific fields |
| 4610 | if (Tok.isLiteral()) { |
| 4611 | CXTok.int_data[0] = CXToken_Literal; |
| 4612 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4613 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4614 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4615 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4616 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4617 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4618 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4619 | CXTok.int_data[0] = CXToken_Keyword; |
| 4620 | } |
| 4621 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4622 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4623 | ? CXToken_Identifier |
| 4624 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4625 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4626 | CXTok.ptr_data = II; |
| 4627 | } else if (Tok.is(tok::comment)) { |
| 4628 | CXTok.int_data[0] = CXToken_Comment; |
| 4629 | CXTok.ptr_data = 0; |
| 4630 | } else { |
| 4631 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4632 | CXTok.ptr_data = 0; |
| 4633 | } |
| 4634 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4635 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4636 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4640 | CXToken **Tokens, unsigned *NumTokens) { |
| 4641 | if (Tokens) |
| 4642 | *Tokens = 0; |
| 4643 | if (NumTokens) |
| 4644 | *NumTokens = 0; |
| 4645 | |
| 4646 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4647 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4648 | return; |
| 4649 | |
| 4650 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4651 | |
| 4652 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4653 | if (R.isInvalid()) |
| 4654 | return; |
| 4655 | |
| 4656 | SmallVector<CXToken, 32> CXTokens; |
| 4657 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4658 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4659 | if (CXTokens.empty()) |
| 4660 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4661 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4662 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4663 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4664 | *NumTokens = CXTokens.size(); |
| 4665 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4666 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4667 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4668 | CXToken *Tokens, unsigned NumTokens) { |
| 4669 | free(Tokens); |
| 4670 | } |
| 4671 | |
| 4672 | } // end: extern "C" |
| 4673 | |
| 4674 | //===----------------------------------------------------------------------===// |
| 4675 | // Token annotation APIs. |
| 4676 | //===----------------------------------------------------------------------===// |
| 4677 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4678 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4679 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4680 | CXCursor parent, |
| 4681 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4682 | namespace { |
| 4683 | class AnnotateTokensWorker { |
| 4684 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4685 | CXToken *Tokens; |
| 4686 | CXCursor *Cursors; |
| 4687 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4688 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4689 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4690 | CursorVisitor AnnotateVis; |
| 4691 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4692 | bool HasContextSensitiveKeywords; |
| 4693 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4694 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4695 | unsigned NextToken() const { return TokIdx; } |
| 4696 | void AdvanceToken() { ++TokIdx; } |
| 4697 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4698 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4699 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4700 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4701 | return Tokens[tokI].int_data[3] != 0; |
| 4702 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4703 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4704 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4705 | } |
| 4706 | |
| 4707 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4708 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4709 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4710 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4711 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4712 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4713 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4714 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4715 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4716 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4717 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4718 | AnnotateTokensVisitor, this, |
| 4719 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4720 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4721 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4722 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4723 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4724 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4725 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4726 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4727 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4728 | void AnnotateTokens() { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4729 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4730 | } |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4731 | |
| 4732 | /// \brief Determine whether the annotator saw any cursors that have |
| 4733 | /// context-sensitive keywords. |
| 4734 | bool hasContextSensitiveKeywords() const { |
| 4735 | return HasContextSensitiveKeywords; |
| 4736 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4737 | }; |
| 4738 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4739 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4740 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 4741 | // Walk the AST within the region of interest, annotating tokens |
| 4742 | // along the way. |
| 4743 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4744 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4745 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4746 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4747 | if (Pos != Annotated.end() && |
| 4748 | (clang_isInvalid(Cursors[I].kind) || |
| 4749 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4750 | Cursors[I] = Pos->second; |
| 4751 | } |
| 4752 | |
| 4753 | // Finish up annotating any tokens left. |
| 4754 | if (!MoreTokens()) |
| 4755 | return; |
| 4756 | |
| 4757 | const CXCursor &C = clang_getNullCursor(); |
| 4758 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 4759 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4760 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4761 | } |
| 4762 | } |
| 4763 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4764 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4765 | //// between the cursor location and the source range is the same as |
| 4766 | /// \arg compResult. |
| 4767 | /// |
| 4768 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4769 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4770 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4771 | RangeComparisonResult compResult, |
| 4772 | SourceRange range) { |
| 4773 | while (MoreTokens()) { |
| 4774 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4775 | if (isFunctionMacroToken(I)) |
| 4776 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4777 | |
| 4778 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4779 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4780 | Cursors[I] = updateC; |
| 4781 | AdvanceToken(); |
| 4782 | continue; |
| 4783 | } |
| 4784 | break; |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4789 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4790 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4791 | RangeComparisonResult compResult, |
| 4792 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4793 | assert(MoreTokens()); |
| 4794 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4795 | "Should be called only for macro arg tokens"); |
| 4796 | |
| 4797 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4798 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4799 | // advance the token index one by one until a token fails the range test. |
| 4800 | // We only advance once past all of the macro arg tokens if all of them |
| 4801 | // pass the range test. If one of them fails we keep the token index pointing |
| 4802 | // at the start of the macro arg tokens so that the failing token will be |
| 4803 | // annotated by a subsequent annotation try. |
| 4804 | |
| 4805 | bool atLeastOneCompFail = false; |
| 4806 | |
| 4807 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4808 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4809 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4810 | if (TokLoc.isFileID()) |
| 4811 | continue; // not macro arg token, it's parens or comma. |
| 4812 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4813 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4814 | Cursors[I] = updateC; |
| 4815 | } else |
| 4816 | atLeastOneCompFail = true; |
| 4817 | } |
| 4818 | |
| 4819 | if (!atLeastOneCompFail) |
| 4820 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4821 | } |
| 4822 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4823 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4824 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4825 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4826 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4827 | if (cursorRange.isInvalid()) |
| 4828 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4829 | |
| 4830 | if (!HasContextSensitiveKeywords) { |
| 4831 | // Objective-C properties can have context-sensitive keywords. |
| 4832 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4833 | if (ObjCPropertyDecl *Property |
| 4834 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4835 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4836 | } |
| 4837 | // Objective-C methods can have context-sensitive keywords. |
| 4838 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4839 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4840 | if (ObjCMethodDecl *Method |
| 4841 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4842 | if (Method->getObjCDeclQualifier()) |
| 4843 | HasContextSensitiveKeywords = true; |
| 4844 | else { |
| 4845 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4846 | PEnd = Method->param_end(); |
| 4847 | P != PEnd; ++P) { |
| 4848 | if ((*P)->getObjCDeclQualifier()) { |
| 4849 | HasContextSensitiveKeywords = true; |
| 4850 | break; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | } |
| 4855 | } |
| 4856 | // C++ methods can have context-sensitive keywords. |
| 4857 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4858 | if (CXXMethodDecl *Method |
| 4859 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4860 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4861 | HasContextSensitiveKeywords = true; |
| 4862 | } |
| 4863 | } |
| 4864 | // C++ classes can have context-sensitive keywords. |
| 4865 | else if (cursor.kind == CXCursor_StructDecl || |
| 4866 | cursor.kind == CXCursor_ClassDecl || |
| 4867 | cursor.kind == CXCursor_ClassTemplate || |
| 4868 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4869 | if (Decl *D = getCursorDecl(cursor)) |
| 4870 | if (D->hasAttr<FinalAttr>()) |
| 4871 | HasContextSensitiveKeywords = true; |
| 4872 | } |
| 4873 | } |
| 4874 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4875 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4876 | // For macro expansions, just note where the beginning of the macro |
| 4877 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4878 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4879 | Annotated[Loc.int_data] = cursor; |
| 4880 | return CXChildVisit_Recurse; |
| 4881 | } |
| 4882 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4883 | // Items in the preprocessing record are kept separate from items in |
| 4884 | // declarations, so we keep a separate token index. |
| 4885 | unsigned SavedTokIdx = TokIdx; |
| 4886 | TokIdx = PreprocessingTokIdx; |
| 4887 | |
| 4888 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4889 | // entry. |
| 4890 | while (MoreTokens()) { |
| 4891 | const unsigned I = NextToken(); |
| 4892 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4893 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4894 | case RangeBefore: |
| 4895 | AdvanceToken(); |
| 4896 | continue; |
| 4897 | case RangeAfter: |
| 4898 | case RangeOverlap: |
| 4899 | break; |
| 4900 | } |
| 4901 | break; |
| 4902 | } |
| 4903 | |
| 4904 | // Look at all of the tokens within this range. |
| 4905 | while (MoreTokens()) { |
| 4906 | const unsigned I = NextToken(); |
| 4907 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4908 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4909 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4910 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4911 | case RangeAfter: |
| 4912 | break; |
| 4913 | case RangeOverlap: |
| 4914 | Cursors[I] = cursor; |
| 4915 | AdvanceToken(); |
| 4916 | continue; |
| 4917 | } |
| 4918 | break; |
| 4919 | } |
| 4920 | |
| 4921 | // Save the preprocessing token index; restore the non-preprocessing |
| 4922 | // token index. |
| 4923 | PreprocessingTokIdx = TokIdx; |
| 4924 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4925 | return CXChildVisit_Recurse; |
| 4926 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4927 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4928 | if (cursorRange.isInvalid()) |
| 4929 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4930 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4931 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4932 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4933 | // Adjust the annotated range based specific declarations. |
| 4934 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4935 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4936 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4937 | |
| 4938 | SourceLocation StartLoc; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4939 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4940 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4941 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4942 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4943 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4944 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4945 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4946 | |
| 4947 | if (StartLoc.isValid() && L.isValid() && |
| 4948 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4949 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4950 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4951 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4952 | // If the location of the cursor occurs within a macro instantiation, record |
| 4953 | // the spelling location of the cursor in our annotation map. We can then |
| 4954 | // paper over the token labelings during a post-processing step to try and |
| 4955 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4956 | // instantiation. |
| 4957 | if (L.isMacroID()) { |
| 4958 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4959 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4960 | // directive. Here we assume that the default construction of CXCursor |
| 4961 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4962 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4963 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4964 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4965 | if (!clang_isPreprocessing(oldC.kind)) |
| 4966 | oldC = cursor; |
| 4967 | } |
| 4968 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4969 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4970 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4971 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4972 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4973 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4974 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4975 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4976 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4977 | // variable declaration that it belongs to. |
| 4978 | // This can happen for C++ constructor expressions whose range generally |
| 4979 | // include the variable declaration, e.g.: |
| 4980 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4981 | if (clang_isExpression(cursorK)) { |
| 4982 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4983 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4984 | const unsigned I = NextToken(); |
| 4985 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4986 | E->getLocStart() == D->getLocation() && |
| 4987 | E->getLocStart() == GetTokenLoc(I)) { |
| 4988 | Cursors[I] = updateC; |
| 4989 | AdvanceToken(); |
| 4990 | } |
| 4991 | } |
| 4992 | } |
| 4993 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4994 | // Visit children to get their cursor information. |
| 4995 | const unsigned BeforeChildren = NextToken(); |
| 4996 | VisitChildren(cursor); |
| 4997 | const unsigned AfterChildren = NextToken(); |
| 4998 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4999 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 5000 | // but the child cursors. |
| 5001 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5002 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5003 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 5004 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5005 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 5006 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 5007 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5008 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5009 | Cursors[I] = cursor; |
| 5010 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5011 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5012 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5015 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 5016 | CXCursor parent, |
| 5017 | CXClientData client_data) { |
| 5018 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 5019 | } |
| 5020 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5021 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5022 | |
| 5023 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 5024 | /// and mark tokens that are macro arguments. This info is used by the |
| 5025 | /// AnnotateTokensWorker. |
| 5026 | class MarkMacroArgTokensVisitor { |
| 5027 | SourceManager &SM; |
| 5028 | CXToken *Tokens; |
| 5029 | unsigned NumTokens; |
| 5030 | unsigned CurIdx; |
| 5031 | |
| 5032 | public: |
| 5033 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 5034 | CXToken *tokens, unsigned numTokens) |
| 5035 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 5036 | |
| 5037 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 5038 | if (cursor.kind != CXCursor_MacroExpansion) |
| 5039 | return CXChildVisit_Continue; |
| 5040 | |
| 5041 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 5042 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 5043 | return CXChildVisit_Continue; // it's not a function macro. |
| 5044 | |
| 5045 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5046 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 5047 | macroRange.getBegin())) |
| 5048 | break; |
| 5049 | } |
| 5050 | |
| 5051 | if (CurIdx == NumTokens) |
| 5052 | return CXChildVisit_Break; |
| 5053 | |
| 5054 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5055 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 5056 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 5057 | break; |
| 5058 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5059 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | if (CurIdx == NumTokens) |
| 5063 | return CXChildVisit_Break; |
| 5064 | |
| 5065 | return CXChildVisit_Continue; |
| 5066 | } |
| 5067 | |
| 5068 | private: |
| 5069 | SourceLocation getTokenLoc(unsigned tokI) { |
| 5070 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 5071 | } |
| 5072 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5073 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5074 | // The third field is reserved and currently not used. Use it here |
| 5075 | // to mark macro arg expanded tokens with their expanded locations. |
| 5076 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 5077 | } |
| 5078 | }; |
| 5079 | |
| 5080 | } // end anonymous namespace |
| 5081 | |
| 5082 | static CXChildVisitResult |
| 5083 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 5084 | CXClientData client_data) { |
| 5085 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 5086 | parent); |
| 5087 | } |
| 5088 | |
| 5089 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5090 | struct clang_annotateTokens_Data { |
| 5091 | CXTranslationUnit TU; |
| 5092 | ASTUnit *CXXUnit; |
| 5093 | CXToken *Tokens; |
| 5094 | unsigned NumTokens; |
| 5095 | CXCursor *Cursors; |
| 5096 | }; |
| 5097 | } |
| 5098 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5099 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 5100 | SourceRange RegionOfInterest, |
| 5101 | AnnotateTokensData &Annotated) { |
| 5102 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 5103 | |
| 5104 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 5105 | std::pair<FileID, unsigned> BeginLocInfo |
| 5106 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 5107 | std::pair<FileID, unsigned> EndLocInfo |
| 5108 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 5109 | |
| 5110 | if (BeginLocInfo.first != EndLocInfo.first) |
| 5111 | return; |
| 5112 | |
| 5113 | StringRef Buffer; |
| 5114 | bool Invalid = false; |
| 5115 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 5116 | if (Buffer.empty() || Invalid) |
| 5117 | return; |
| 5118 | |
| 5119 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 5120 | CXXUnit->getASTContext().getLangOptions(), |
| 5121 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 5122 | Buffer.end()); |
| 5123 | Lex.SetCommentRetentionState(true); |
| 5124 | |
| 5125 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 5126 | // entering #includes or expanding macros. |
| 5127 | while (true) { |
| 5128 | Token Tok; |
| 5129 | Lex.LexFromRawLexer(Tok); |
| 5130 | |
| 5131 | reprocess: |
| 5132 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 5133 | // We have found a preprocessing directive. Gobble it up so that we |
| 5134 | // don't see it while preprocessing these tokens later, but keep track |
| 5135 | // of all of the token locations inside this preprocessing directive so |
| 5136 | // that we can annotate them appropriately. |
| 5137 | // |
| 5138 | // FIXME: Some simple tests here could identify macro definitions and |
| 5139 | // #undefs, to provide specific cursor kinds for those. |
| 5140 | SmallVector<SourceLocation, 32> Locations; |
| 5141 | do { |
| 5142 | Locations.push_back(Tok.getLocation()); |
| 5143 | Lex.LexFromRawLexer(Tok); |
| 5144 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5145 | |
| 5146 | using namespace cxcursor; |
| 5147 | CXCursor Cursor |
| 5148 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5149 | Locations.back()), |
| 5150 | TU); |
| 5151 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5152 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5153 | } |
| 5154 | |
| 5155 | if (Tok.isAtStartOfLine()) |
| 5156 | goto reprocess; |
| 5157 | |
| 5158 | continue; |
| 5159 | } |
| 5160 | |
| 5161 | if (Tok.is(tok::eof)) |
| 5162 | break; |
| 5163 | } |
| 5164 | } |
| 5165 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5166 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5167 | static void clang_annotateTokensImpl(void *UserData) { |
| 5168 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 5169 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 5170 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 5171 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 5172 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 5173 | |
| 5174 | // Determine the region of interest, which contains all of the tokens. |
| 5175 | SourceRange RegionOfInterest; |
| 5176 | RegionOfInterest.setBegin( |
| 5177 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 5178 | RegionOfInterest.setEnd( |
| 5179 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 5180 | Tokens[NumTokens-1]))); |
| 5181 | |
| 5182 | // A mapping from the source locations found when re-lexing or traversing the |
| 5183 | // region of interest to the corresponding cursors. |
| 5184 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5185 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5186 | // Relex the tokens within the source range to look for preprocessing |
| 5187 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5188 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5189 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5190 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5191 | // Search and mark tokens that are macro argument expansions. |
| 5192 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5193 | Tokens, NumTokens); |
| 5194 | CursorVisitor MacroArgMarker(TU, |
| 5195 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5196 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 5197 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5198 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5199 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5200 | } |
| 5201 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5202 | // Annotate all of the source locations in the region of interest that map to |
| 5203 | // a specific cursor. |
| 5204 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5205 | TU, RegionOfInterest); |
| 5206 | |
| 5207 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5208 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5209 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5210 | // algorithm back into a traditional recursion by explicitly calling |
| 5211 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5212 | W.AnnotateTokens(); |
| 5213 | |
| 5214 | // If we ran into any entities that involve context-sensitive keywords, |
| 5215 | // take another pass through the tokens to mark them as such. |
| 5216 | if (W.hasContextSensitiveKeywords()) { |
| 5217 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5218 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5219 | continue; |
| 5220 | |
| 5221 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5222 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5223 | if (ObjCPropertyDecl *Property |
| 5224 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5225 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5226 | llvm::StringSwitch<bool>(II->getName()) |
| 5227 | .Case("readonly", true) |
| 5228 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5229 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5230 | .Case("readwrite", true) |
| 5231 | .Case("retain", true) |
| 5232 | .Case("copy", true) |
| 5233 | .Case("nonatomic", true) |
| 5234 | .Case("atomic", true) |
| 5235 | .Case("getter", true) |
| 5236 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5237 | .Case("strong", true) |
| 5238 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5239 | .Default(false)) |
| 5240 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5241 | } |
| 5242 | continue; |
| 5243 | } |
| 5244 | |
| 5245 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5246 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5247 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5248 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5249 | .Case("in", true) |
| 5250 | .Case("out", true) |
| 5251 | .Case("inout", true) |
| 5252 | .Case("oneway", true) |
| 5253 | .Case("bycopy", true) |
| 5254 | .Case("byref", true) |
| 5255 | .Default(false)) |
| 5256 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5257 | continue; |
| 5258 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5259 | |
| 5260 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5261 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5262 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5263 | continue; |
| 5264 | } |
| 5265 | } |
| 5266 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5269 | extern "C" { |
| 5270 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5271 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5272 | CXToken *Tokens, unsigned NumTokens, |
| 5273 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5274 | |
| 5275 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5276 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5277 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5278 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5279 | CXCursor C = clang_getNullCursor(); |
| 5280 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5281 | Cursors[I] = C; |
| 5282 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5283 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5284 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5285 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5286 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5287 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5288 | |
| 5289 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5290 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5291 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5292 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5293 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5294 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5295 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5297 | } // end: extern "C" |
| 5298 | |
| 5299 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5300 | // Operations for querying linkage of a cursor. |
| 5301 | //===----------------------------------------------------------------------===// |
| 5302 | |
| 5303 | extern "C" { |
| 5304 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5305 | if (!clang_isDeclaration(cursor.kind)) |
| 5306 | return CXLinkage_Invalid; |
| 5307 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5308 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5309 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5310 | switch (ND->getLinkage()) { |
| 5311 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5312 | case InternalLinkage: return CXLinkage_Internal; |
| 5313 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5314 | case ExternalLinkage: return CXLinkage_External; |
| 5315 | }; |
| 5316 | |
| 5317 | return CXLinkage_Invalid; |
| 5318 | } |
| 5319 | } // end: extern "C" |
| 5320 | |
| 5321 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5322 | // Operations for querying language of a cursor. |
| 5323 | //===----------------------------------------------------------------------===// |
| 5324 | |
| 5325 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 5326 | switch (D->getKind()) { |
| 5327 | default: |
| 5328 | break; |
| 5329 | case Decl::ImplicitParam: |
| 5330 | case Decl::ObjCAtDefsField: |
| 5331 | case Decl::ObjCCategory: |
| 5332 | case Decl::ObjCCategoryImpl: |
| 5333 | case Decl::ObjCClass: |
| 5334 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5335 | case Decl::ObjCForwardProtocol: |
| 5336 | case Decl::ObjCImplementation: |
| 5337 | case Decl::ObjCInterface: |
| 5338 | case Decl::ObjCIvar: |
| 5339 | case Decl::ObjCMethod: |
| 5340 | case Decl::ObjCProperty: |
| 5341 | case Decl::ObjCPropertyImpl: |
| 5342 | case Decl::ObjCProtocol: |
| 5343 | return CXLanguage_ObjC; |
| 5344 | case Decl::CXXConstructor: |
| 5345 | case Decl::CXXConversion: |
| 5346 | case Decl::CXXDestructor: |
| 5347 | case Decl::CXXMethod: |
| 5348 | case Decl::CXXRecord: |
| 5349 | case Decl::ClassTemplate: |
| 5350 | case Decl::ClassTemplatePartialSpecialization: |
| 5351 | case Decl::ClassTemplateSpecialization: |
| 5352 | case Decl::Friend: |
| 5353 | case Decl::FriendTemplate: |
| 5354 | case Decl::FunctionTemplate: |
| 5355 | case Decl::LinkageSpec: |
| 5356 | case Decl::Namespace: |
| 5357 | case Decl::NamespaceAlias: |
| 5358 | case Decl::NonTypeTemplateParm: |
| 5359 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5360 | case Decl::TemplateTemplateParm: |
| 5361 | case Decl::TemplateTypeParm: |
| 5362 | case Decl::UnresolvedUsingTypename: |
| 5363 | case Decl::UnresolvedUsingValue: |
| 5364 | case Decl::Using: |
| 5365 | case Decl::UsingDirective: |
| 5366 | case Decl::UsingShadow: |
| 5367 | return CXLanguage_CPlusPlus; |
| 5368 | } |
| 5369 | |
| 5370 | return CXLanguage_C; |
| 5371 | } |
| 5372 | |
| 5373 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5374 | |
| 5375 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5376 | if (clang_isDeclaration(cursor.kind)) |
| 5377 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5378 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5379 | return CXAvailability_Available; |
| 5380 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5381 | switch (D->getAvailability()) { |
| 5382 | case AR_Available: |
| 5383 | case AR_NotYetIntroduced: |
| 5384 | return CXAvailability_Available; |
| 5385 | |
| 5386 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5387 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5388 | |
| 5389 | case AR_Unavailable: |
| 5390 | return CXAvailability_NotAvailable; |
| 5391 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5392 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5393 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5394 | return CXAvailability_Available; |
| 5395 | } |
| 5396 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5397 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5398 | if (clang_isDeclaration(cursor.kind)) |
| 5399 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5400 | |
| 5401 | return CXLanguage_Invalid; |
| 5402 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5403 | |
| 5404 | /// \brief If the given cursor is the "templated" declaration |
| 5405 | /// descibing a class or function template, return the class or |
| 5406 | /// function template. |
| 5407 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5408 | if (!D) |
| 5409 | return 0; |
| 5410 | |
| 5411 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5412 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5413 | return FunTmpl; |
| 5414 | |
| 5415 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5416 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5417 | return ClassTmpl; |
| 5418 | |
| 5419 | return D; |
| 5420 | } |
| 5421 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5422 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5423 | if (clang_isDeclaration(cursor.kind)) { |
| 5424 | if (Decl *D = getCursorDecl(cursor)) { |
| 5425 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5426 | if (!DC) |
| 5427 | return clang_getNullCursor(); |
| 5428 | |
| 5429 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5430 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5435 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5436 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | return clang_getNullCursor(); |
| 5440 | } |
| 5441 | |
| 5442 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5443 | if (clang_isDeclaration(cursor.kind)) { |
| 5444 | if (Decl *D = getCursorDecl(cursor)) { |
| 5445 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5446 | if (!DC) |
| 5447 | return clang_getNullCursor(); |
| 5448 | |
| 5449 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5450 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5455 | // statement or expression, so we return nothing. |
| 5456 | return clang_getNullCursor(); |
| 5457 | } |
| 5458 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5459 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5460 | CXCursor **overridden, |
| 5461 | unsigned *num_overridden) { |
| 5462 | if (overridden) |
| 5463 | *overridden = 0; |
| 5464 | if (num_overridden) |
| 5465 | *num_overridden = 0; |
| 5466 | if (!overridden || !num_overridden) |
| 5467 | return; |
| 5468 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5469 | SmallVector<CXCursor, 8> Overridden; |
| 5470 | cxcursor::getOverriddenCursors(cursor, Overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5471 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5472 | *num_overridden = Overridden.size(); |
| 5473 | *overridden = new CXCursor [Overridden.size()]; |
| 5474 | std::copy(Overridden.begin(), Overridden.end(), *overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5478 | delete [] overridden; |
| 5479 | } |
| 5480 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5481 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5482 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5483 | return 0; |
| 5484 | |
| 5485 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5486 | return (void *)ID->getFile(); |
| 5487 | } |
| 5488 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5489 | } // end: extern "C" |
| 5490 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5491 | |
| 5492 | //===----------------------------------------------------------------------===// |
| 5493 | // C++ AST instrospection. |
| 5494 | //===----------------------------------------------------------------------===// |
| 5495 | |
| 5496 | extern "C" { |
| 5497 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5498 | if (!clang_isDeclaration(C.kind)) |
| 5499 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5500 | |
| 5501 | CXXMethodDecl *Method = 0; |
| 5502 | Decl *D = cxcursor::getCursorDecl(C); |
| 5503 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5504 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5505 | else |
| 5506 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5507 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5508 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5509 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5510 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5511 | if (!clang_isDeclaration(C.kind)) |
| 5512 | return 0; |
| 5513 | |
| 5514 | CXXMethodDecl *Method = 0; |
| 5515 | Decl *D = cxcursor::getCursorDecl(C); |
| 5516 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5517 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5518 | else |
| 5519 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5520 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5521 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5522 | } // end: extern "C" |
| 5523 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5524 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5525 | // Attribute introspection. |
| 5526 | //===----------------------------------------------------------------------===// |
| 5527 | |
| 5528 | extern "C" { |
| 5529 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5530 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5531 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5532 | |
| 5533 | IBOutletCollectionAttr *A = |
| 5534 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5535 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5536 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5537 | } |
| 5538 | } // end: extern "C" |
| 5539 | |
| 5540 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5541 | // Inspecting memory usage. |
| 5542 | //===----------------------------------------------------------------------===// |
| 5543 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5544 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5545 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5546 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5547 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5548 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5549 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5550 | entries.push_back(entry); |
| 5551 | } |
| 5552 | |
| 5553 | extern "C" { |
| 5554 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5555 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5556 | const char *str = ""; |
| 5557 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5558 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5559 | str = "ASTContext: expressions, declarations, and types"; |
| 5560 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5561 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5562 | str = "ASTContext: identifiers"; |
| 5563 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5564 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5565 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5566 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5567 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5568 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5569 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5570 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5571 | str = "SourceManager: content cache allocator"; |
| 5572 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5573 | case CXTUResourceUsage_AST_SideTables: |
| 5574 | str = "ASTContext: side tables"; |
| 5575 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5576 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5577 | str = "SourceManager: malloc'ed memory buffers"; |
| 5578 | break; |
| 5579 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5580 | str = "SourceManager: mmap'ed memory buffers"; |
| 5581 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5582 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5583 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5584 | break; |
| 5585 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5586 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5587 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5588 | case CXTUResourceUsage_Preprocessor: |
| 5589 | str = "Preprocessor: malloc'ed memory"; |
| 5590 | break; |
| 5591 | case CXTUResourceUsage_PreprocessingRecord: |
| 5592 | str = "Preprocessor: PreprocessingRecord"; |
| 5593 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5594 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5595 | str = "SourceManager: data structures and tables"; |
| 5596 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5597 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5598 | str = "Preprocessor: header search tables"; |
| 5599 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5600 | } |
| 5601 | return str; |
| 5602 | } |
| 5603 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5604 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5605 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5606 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5607 | return usage; |
| 5608 | } |
| 5609 | |
| 5610 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5611 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5612 | ASTContext &astContext = astUnit->getASTContext(); |
| 5613 | |
| 5614 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5615 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5616 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5617 | |
| 5618 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5619 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5620 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5621 | |
| 5622 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5623 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5624 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5625 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5626 | // How much memory is used by ASTContext's side tables? |
| 5627 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5628 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5629 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5630 | // How much memory is used for caching global code completion results? |
| 5631 | unsigned long completionBytes = 0; |
| 5632 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5633 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5634 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5635 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5636 | createCXTUResourceUsageEntry(*entries, |
| 5637 | CXTUResourceUsage_GlobalCompletionResults, |
| 5638 | completionBytes); |
| 5639 | |
| 5640 | // How much memory is being used by SourceManager's content cache? |
| 5641 | createCXTUResourceUsageEntry(*entries, |
| 5642 | CXTUResourceUsage_SourceManagerContentCache, |
| 5643 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5644 | |
| 5645 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5646 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5647 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5648 | |
| 5649 | createCXTUResourceUsageEntry(*entries, |
| 5650 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5651 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5652 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5653 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5654 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5655 | createCXTUResourceUsageEntry(*entries, |
| 5656 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5657 | (unsigned long) astContext.getSourceManager() |
| 5658 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5659 | |
| 5660 | // How much memory is being used by the ExternalASTSource? |
| 5661 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5662 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5663 | esrc->getMemoryBufferSizes(); |
| 5664 | |
| 5665 | createCXTUResourceUsageEntry(*entries, |
| 5666 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5667 | (unsigned long) sizes.malloc_bytes); |
| 5668 | createCXTUResourceUsageEntry(*entries, |
| 5669 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5670 | (unsigned long) sizes.mmap_bytes); |
| 5671 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5672 | |
| 5673 | // How much memory is being used by the Preprocessor? |
| 5674 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5675 | createCXTUResourceUsageEntry(*entries, |
| 5676 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5677 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5678 | |
| 5679 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5680 | createCXTUResourceUsageEntry(*entries, |
| 5681 | CXTUResourceUsage_PreprocessingRecord, |
| 5682 | pRec->getTotalMemory()); |
| 5683 | } |
| 5684 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5685 | createCXTUResourceUsageEntry(*entries, |
| 5686 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5687 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5688 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5689 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5690 | (unsigned) entries->size(), |
| 5691 | entries->size() ? &(*entries)[0] : 0 }; |
| 5692 | entries.take(); |
| 5693 | return usage; |
| 5694 | } |
| 5695 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5696 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5697 | if (usage.data) |
| 5698 | delete (MemUsageEntries*) usage.data; |
| 5699 | } |
| 5700 | |
| 5701 | } // end extern "C" |
| 5702 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5703 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5704 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5705 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5706 | fprintf(stderr, " %s: %lu\n", |
| 5707 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5708 | Usage.entries[I].amount); |
| 5709 | |
| 5710 | clang_disposeCXTUResourceUsage(Usage); |
| 5711 | } |
| 5712 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5713 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5714 | // Misc. utility functions. |
| 5715 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5716 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5717 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5718 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5719 | |
| 5720 | namespace clang { |
| 5721 | |
| 5722 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5723 | void (*Fn)(void*), void *UserData, |
| 5724 | unsigned Size) { |
| 5725 | if (!Size) |
| 5726 | Size = GetSafetyThreadStackSize(); |
| 5727 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5728 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5729 | return CRC.RunSafely(Fn, UserData); |
| 5730 | } |
| 5731 | |
| 5732 | unsigned GetSafetyThreadStackSize() { |
| 5733 | return SafetyStackThreadSize; |
| 5734 | } |
| 5735 | |
| 5736 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5737 | SafetyStackThreadSize = Value; |
| 5738 | } |
| 5739 | |
| 5740 | } |
| 5741 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5742 | extern "C" { |
| 5743 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5744 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5745 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5749 | |