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; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 55 | static CXTranslationUnit MakeCXTranslationUnit(ASTUnit *TU) { |
| 56 | if (!TU) |
| 57 | return 0; |
| 58 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 59 | D->TUData = TU; |
| 60 | D->StringPool = createCXStringPool(); |
| 61 | return D; |
| 62 | } |
| 63 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 64 | /// \brief The result of comparing two source ranges. |
| 65 | enum RangeComparisonResult { |
| 66 | /// \brief Either the ranges overlap or one of the ranges is invalid. |
| 67 | RangeOverlap, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 69 | /// \brief The first range ends before the second range starts. |
| 70 | RangeBefore, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 72 | /// \brief The first range starts after the second range ends. |
| 73 | RangeAfter |
| 74 | }; |
| 75 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 76 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 77 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 78 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 79 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 80 | SourceRange R2) { |
| 81 | assert(R1.isValid() && "First range is invalid?"); |
| 82 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 83 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 84 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 85 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 86 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 87 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 88 | return RangeAfter; |
| 89 | return RangeOverlap; |
| 90 | } |
| 91 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 92 | /// \brief Determine if a source location falls within, before, or after a |
| 93 | /// a given source range. |
| 94 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 95 | SourceLocation L, SourceRange R) { |
| 96 | assert(R.isValid() && "First range is invalid?"); |
| 97 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 98 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 99 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 100 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 101 | return RangeBefore; |
| 102 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 103 | return RangeAfter; |
| 104 | return RangeOverlap; |
| 105 | } |
| 106 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 107 | /// \brief Translate a Clang source range into a CIndex source range. |
| 108 | /// |
| 109 | /// Clang internally represents ranges where the end location points to the |
| 110 | /// start of the token at the end. However, for external clients it is more |
| 111 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 112 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 113 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 114 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 115 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 116 | // 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] | 117 | // location accordingly. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 118 | SourceLocation EndLoc = R.getEnd(); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 119 | if (EndLoc.isValid() && EndLoc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 120 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 121 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 122 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 123 | EndLoc = EndLoc.getFileLocWithOffset(Length); |
| 124 | } |
| 125 | |
| 126 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 127 | R.getBegin().getRawEncoding(), |
| 128 | EndLoc.getRawEncoding() }; |
| 129 | return Result; |
| 130 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 133 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 136 | namespace { |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 137 | |
| 138 | class VisitorJob { |
| 139 | public: |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 140 | enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind, |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 141 | TypeLocVisitKind, OverloadExprPartsKind, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 142 | DeclRefExprPartsKind, LabelRefVisitKind, |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 143 | ExplicitTemplateArgsVisitKind, |
| 144 | NestedNameSpecifierVisitKind, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 145 | NestedNameSpecifierLocVisitKind, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 146 | DeclarationNameInfoVisitKind, |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 147 | MemberRefVisitKind, SizeOfPackExprPartsKind }; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 148 | protected: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 149 | void *data[3]; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 150 | CXCursor parent; |
| 151 | Kind K; |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 152 | VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0) |
| 153 | : parent(C), K(k) { |
| 154 | data[0] = d1; |
| 155 | data[1] = d2; |
| 156 | data[2] = d3; |
| 157 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 158 | public: |
| 159 | Kind getKind() const { return K; } |
| 160 | const CXCursor &getParent() const { return parent; } |
| 161 | static bool classof(VisitorJob *VJ) { return true; } |
| 162 | }; |
| 163 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 164 | typedef SmallVector<VisitorJob, 10> VisitorWorkList; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 166 | // Cursor visitor. |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 167 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 168 | public TypeLocVisitor<CursorVisitor, bool> |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 169 | { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 170 | /// \brief The translation unit we are traversing. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 171 | CXTranslationUnit TU; |
| 172 | ASTUnit *AU; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 173 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 174 | /// \brief The parent cursor whose children we are traversing. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 175 | CXCursor Parent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 177 | /// \brief The declaration that serves at the parent of any statement or |
| 178 | /// expression nodes. |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 179 | Decl *StmtParent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 181 | /// \brief The visitor function. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 182 | CXCursorVisitor Visitor; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 184 | /// \brief The opaque client data, to be passed along to the visitor. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 185 | CXClientData ClientData; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 187 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 188 | // to the visitor. Declarations with a PCH level greater than this value will |
| 189 | // be suppressed. |
| 190 | unsigned MaxPCHLevel; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +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; |
| 195 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 196 | /// \brief When valid, a source range to which the cursor should restrict |
| 197 | /// its search. |
| 198 | SourceRange RegionOfInterest; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 200 | // FIXME: Eventually remove. This part of a hack to support proper |
| 201 | // iteration over all Decls contained lexically within an ObjC container. |
| 202 | DeclContext::decl_iterator *DI_current; |
| 203 | DeclContext::decl_iterator DE_current; |
| 204 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 205 | // Cache of pre-allocated worklists for data-recursion walk of Stmts. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 206 | SmallVector<VisitorWorkList*, 5> WorkListFreeList; |
| 207 | SmallVector<VisitorWorkList*, 5> WorkListCache; |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 209 | using DeclVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 210 | using TypeLocVisitor<CursorVisitor, bool>::Visit; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 211 | |
| 212 | /// \brief Determine whether this particular source range comes before, comes |
| 213 | /// after, or overlaps the region of interest. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 214 | /// |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 215 | /// \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] | 216 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); |
| 217 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 218 | class SetParentRAII { |
| 219 | CXCursor &Parent; |
| 220 | Decl *&StmtParent; |
| 221 | CXCursor OldParent; |
| 222 | |
| 223 | public: |
| 224 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 225 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
| 226 | { |
| 227 | Parent = NewParent; |
| 228 | if (clang_isDeclaration(Parent.kind)) |
| 229 | StmtParent = getCursorDecl(Parent); |
| 230 | } |
| 231 | |
| 232 | ~SetParentRAII() { |
| 233 | Parent = OldParent; |
| 234 | if (clang_isDeclaration(Parent.kind)) |
| 235 | StmtParent = getCursorDecl(Parent); |
| 236 | } |
| 237 | }; |
| 238 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 239 | public: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 240 | CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor, |
| 241 | CXClientData ClientData, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 242 | unsigned MaxPCHLevel, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 243 | bool VisitPreprocessorLast, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 244 | SourceRange RegionOfInterest = SourceRange()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 245 | : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)), |
| 246 | Visitor(Visitor), ClientData(ClientData), |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 247 | MaxPCHLevel(MaxPCHLevel), VisitPreprocessorLast(VisitPreprocessorLast), |
| 248 | RegionOfInterest(RegionOfInterest), DI_current(0) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 249 | { |
| 250 | Parent.kind = CXCursor_NoDeclFound; |
| 251 | Parent.data[0] = 0; |
| 252 | Parent.data[1] = 0; |
| 253 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 254 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 255 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 257 | ~CursorVisitor() { |
| 258 | // Free the pre-allocated worklists for data-recursion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 259 | for (SmallVectorImpl<VisitorWorkList*>::iterator |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 260 | I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) { |
| 261 | delete *I; |
| 262 | } |
| 263 | } |
| 264 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 265 | ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); } |
| 266 | CXTranslationUnit getTU() const { return TU; } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 268 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 270 | bool visitPreprocessedEntitiesInRegion(); |
| 271 | |
| 272 | template<typename InputIterator> |
| 273 | bool visitPreprocessedEntitiesInRegion(InputIterator First, |
| 274 | InputIterator Last); |
| 275 | |
| 276 | template<typename InputIterator> |
| 277 | bool visitPreprocessedEntities(InputIterator First, InputIterator Last); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 279 | bool VisitChildren(CXCursor Parent); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 281 | // Declaration visitors |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 282 | bool VisitTypeAliasDecl(TypeAliasDecl *D); |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 283 | bool VisitAttributes(Decl *D); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 284 | bool VisitBlockDecl(BlockDecl *B); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 285 | bool VisitCXXRecordDecl(CXXRecordDecl *D); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 286 | llvm::Optional<bool> shouldVisitCursor(CXCursor C); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 287 | bool VisitDeclContext(DeclContext *DC); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 288 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 289 | bool VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 290 | bool VisitTagDecl(TagDecl *D); |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 291 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 292 | bool VisitClassTemplatePartialSpecializationDecl( |
| 293 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 294 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 295 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
| 296 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
| 297 | bool VisitFunctionDecl(FunctionDecl *ND); |
| 298 | bool VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 299 | bool VisitVarDecl(VarDecl *); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 300 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 301 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 302 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 303 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 304 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 305 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 306 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 307 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 308 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 309 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 310 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 311 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 312 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 313 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 314 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 315 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 316 | bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 317 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 318 | bool VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 319 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 320 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 321 | bool VisitUsingDecl(UsingDecl *D); |
| 322 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 323 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 325 | // Name visitor |
| 326 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 327 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 328 | bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 330 | // Template visitors |
| 331 | bool VisitTemplateParameters(const TemplateParameterList *Params); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 332 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 333 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); |
| 334 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 335 | // Type visitors |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 336 | bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 337 | bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 338 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 339 | bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); |
| 340 | bool VisitTagTypeLoc(TagTypeLoc TL); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 341 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 342 | bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 343 | bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 344 | bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 345 | bool VisitParenTypeLoc(ParenTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 346 | bool VisitPointerTypeLoc(PointerTypeLoc TL); |
| 347 | bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL); |
| 348 | bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL); |
| 349 | bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL); |
| 350 | bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 351 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 352 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 353 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL); |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 354 | // FIXME: Implement visitors here when the unimplemented TypeLocs get |
| 355 | // implemented |
| 356 | bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 357 | bool VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL); |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 358 | bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 359 | bool VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 360 | bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 361 | bool VisitDependentTemplateSpecializationTypeLoc( |
| 362 | DependentTemplateSpecializationTypeLoc TL); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 363 | bool VisitElaboratedTypeLoc(ElaboratedTypeLoc TL); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 365 | // Data-recursive visitor functions. |
| 366 | bool IsInRegionOfInterest(CXCursor C); |
| 367 | bool RunVisitorWorkList(VisitorWorkList &WL); |
| 368 | void EnqueueWorkList(VisitorWorkList &WL, Stmt *S); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 369 | LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 370 | }; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 372 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 374 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 375 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 376 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 378 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 379 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 382 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 383 | /// its children. |
| 384 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 385 | /// \param Cursor the cursor to visit. |
| 386 | /// |
| 387 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 388 | /// this cursor is within the region of interest. |
| 389 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 390 | /// \returns true if the visitation should be aborted, false if it |
| 391 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 392 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 393 | if (clang_isInvalid(Cursor.kind)) |
| 394 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 396 | if (clang_isDeclaration(Cursor.kind)) { |
| 397 | Decl *D = getCursorDecl(Cursor); |
| 398 | assert(D && "Invalid declaration cursor"); |
| 399 | if (D->getPCHLevel() > MaxPCHLevel) |
| 400 | return false; |
| 401 | |
| 402 | if (D->isImplicit()) |
| 403 | return false; |
| 404 | } |
| 405 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 406 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 407 | // we're done. |
| 408 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 409 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 410 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 414 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 415 | case CXChildVisit_Break: |
| 416 | return true; |
| 417 | |
| 418 | case CXChildVisit_Continue: |
| 419 | return false; |
| 420 | |
| 421 | case CXChildVisit_Recurse: |
| 422 | return VisitChildren(Cursor); |
| 423 | } |
| 424 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 425 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 428 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 429 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 430 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 431 | |
| 432 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 433 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 434 | |
| 435 | if (OnlyLocalDecls && RegionOfInterest.isValid()) { |
| 436 | // If we would only look at local declarations but we have a region of |
| 437 | // interest, check whether that region of interest is in the main file. |
| 438 | // If not, we should traverse all declarations. |
| 439 | // FIXME: My kingdom for a proper binary search approach to finding |
| 440 | // cursors! |
| 441 | std::pair<FileID, unsigned> Location |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 442 | = AU->getSourceManager().getDecomposedExpansionLoc( |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 443 | RegionOfInterest.getBegin()); |
| 444 | if (Location.first != AU->getSourceManager().getMainFileID()) |
| 445 | OnlyLocalDecls = false; |
| 446 | } |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 448 | PreprocessingRecord::iterator StartEntity, EndEntity; |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 449 | if (OnlyLocalDecls && AU->pp_entity_begin() != AU->pp_entity_end()) |
| 450 | return visitPreprocessedEntitiesInRegion(AU->pp_entity_begin(), |
| 451 | AU->pp_entity_end()); |
| 452 | else |
| 453 | return visitPreprocessedEntitiesInRegion(PPRec.begin(), PPRec.end()); |
| 454 | } |
| 455 | |
| 456 | template<typename InputIterator> |
| 457 | bool CursorVisitor::visitPreprocessedEntitiesInRegion(InputIterator First, |
| 458 | InputIterator Last) { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 459 | // There is no region of interest; we have to walk everything. |
| 460 | if (RegionOfInterest.isInvalid()) |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 461 | return visitPreprocessedEntities(First, Last); |
| 462 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 463 | // Find the file in which the region of interest lands. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 464 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 465 | std::pair<FileID, unsigned> Begin |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 466 | = SM.getDecomposedExpansionLoc(RegionOfInterest.getBegin()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 467 | std::pair<FileID, unsigned> End |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 468 | = SM.getDecomposedExpansionLoc(RegionOfInterest.getEnd()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 469 | |
| 470 | // The region of interest spans files; we have to walk everything. |
| 471 | if (Begin.first != End.first) |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 472 | return visitPreprocessedEntities(First, Last); |
| 473 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 474 | ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 475 | = AU->getPreprocessedEntitiesByFile(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 476 | if (ByFileMap.empty()) { |
| 477 | // Build the mapping from files to sets of preprocessed entities. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 478 | for (; First != Last; ++First) { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 479 | std::pair<FileID, unsigned> P |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 480 | = SM.getDecomposedExpansionLoc((*First)->getSourceRange().getBegin()); |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 482 | ByFileMap[P.first].push_back(*First); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return visitPreprocessedEntities(ByFileMap[Begin.first].begin(), |
| 487 | ByFileMap[Begin.first].end()); |
| 488 | } |
| 489 | |
| 490 | template<typename InputIterator> |
| 491 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 492 | InputIterator Last) { |
| 493 | for (; First != Last; ++First) { |
| 494 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*First)) { |
| 495 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 496 | return true; |
| 497 | |
| 498 | continue; |
| 499 | } |
| 500 | |
| 501 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*First)) { |
| 502 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 503 | return true; |
| 504 | |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*First)) { |
| 509 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 510 | return true; |
| 511 | |
| 512 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 516 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 519 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 520 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 521 | /// \returns true if the visitation should be aborted, false if it |
| 522 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 523 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 524 | if (clang_isReference(Cursor.kind) && |
| 525 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 526 | // By definition, references have no children. |
| 527 | return false; |
| 528 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 529 | |
| 530 | // 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] | 531 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 532 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 534 | if (clang_isDeclaration(Cursor.kind)) { |
| 535 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 536 | if (!D) |
| 537 | return false; |
| 538 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 539 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 540 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 541 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 542 | if (clang_isStatement(Cursor.kind)) { |
| 543 | if (Stmt *S = getCursorStmt(Cursor)) |
| 544 | return Visit(S); |
| 545 | |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | if (clang_isExpression(Cursor.kind)) { |
| 550 | if (Expr *E = getCursorExpr(Cursor)) |
| 551 | return Visit(E); |
| 552 | |
| 553 | return false; |
| 554 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 556 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 557 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 558 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 559 | |
| 560 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 561 | for (unsigned I = 0; I != 2; ++I) { |
| 562 | if (VisitOrder[I]) { |
| 563 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 564 | RegionOfInterest.isInvalid()) { |
| 565 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 566 | TLEnd = CXXUnit->top_level_end(); |
| 567 | TL != TLEnd; ++TL) { |
| 568 | if (Visit(MakeCXCursor(*TL, tu), true)) |
| 569 | return true; |
| 570 | } |
| 571 | } else if (VisitDeclContext( |
| 572 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 573 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 574 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 575 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 577 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 578 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 579 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 580 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 582 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 583 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 585 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 586 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 587 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 588 | return Visit(BaseTSInfo->getTypeLoc()); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 593 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 594 | return false; |
| 595 | } |
| 596 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 597 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 598 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 599 | if (Visit(TSInfo->getTypeLoc())) |
| 600 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 601 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 602 | if (Stmt *Body = B->getBody()) |
| 603 | return Visit(MakeCXCursor(Body, StmtParent, TU)); |
| 604 | |
| 605 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 608 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 609 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 610 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 611 | if (Range.isInvalid()) |
| 612 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 613 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 614 | switch (CompareRegionOfInterest(Range)) { |
| 615 | case RangeBefore: |
| 616 | // This declaration comes before the region of interest; skip it. |
| 617 | return llvm::Optional<bool>(); |
| 618 | |
| 619 | case RangeAfter: |
| 620 | // This declaration comes after the region of interest; we're done. |
| 621 | return false; |
| 622 | |
| 623 | case RangeOverlap: |
| 624 | // This declaration overlaps the region of interest; visit it. |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 632 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 633 | |
| 634 | // FIXME: Eventually remove. This part of a hack to support proper |
| 635 | // iteration over all Decls contained lexically within an ObjC container. |
| 636 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 637 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 638 | |
| 639 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 640 | Decl *D = *I; |
| 641 | if (D->getLexicalDeclContext() != DC) |
| 642 | continue; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 643 | CXCursor Cursor = MakeCXCursor(D, TU); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 644 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 645 | if (!V.hasValue()) |
| 646 | continue; |
| 647 | if (!V.getValue()) |
| 648 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 649 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 650 | return true; |
| 651 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 652 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 655 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 656 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 657 | return false; |
| 658 | } |
| 659 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 660 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 661 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 662 | return Visit(TSInfo->getTypeLoc()); |
| 663 | |
| 664 | return false; |
| 665 | } |
| 666 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 667 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 668 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 669 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
| 674 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 675 | return VisitDeclContext(D); |
| 676 | } |
| 677 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 678 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 679 | ClassTemplateSpecializationDecl *D) { |
| 680 | bool ShouldVisitBody = false; |
| 681 | switch (D->getSpecializationKind()) { |
| 682 | case TSK_Undeclared: |
| 683 | case TSK_ImplicitInstantiation: |
| 684 | // Nothing to visit |
| 685 | return false; |
| 686 | |
| 687 | case TSK_ExplicitInstantiationDeclaration: |
| 688 | case TSK_ExplicitInstantiationDefinition: |
| 689 | break; |
| 690 | |
| 691 | case TSK_ExplicitSpecialization: |
| 692 | ShouldVisitBody = true; |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | // Visit the template arguments used in the specialization. |
| 697 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 698 | TypeLoc TL = SpecType->getTypeLoc(); |
| 699 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 700 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 701 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 702 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 703 | return true; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 708 | return true; |
| 709 | |
| 710 | return false; |
| 711 | } |
| 712 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 713 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 714 | ClassTemplatePartialSpecializationDecl *D) { |
| 715 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 716 | // before visiting these template parameters. |
| 717 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 718 | return true; |
| 719 | |
| 720 | // Visit the partial specialization arguments. |
| 721 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 722 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 723 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 724 | return true; |
| 725 | |
| 726 | return VisitCXXRecordDecl(D); |
| 727 | } |
| 728 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 729 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 730 | // Visit the default argument. |
| 731 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 732 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 733 | if (Visit(DefArg->getTypeLoc())) |
| 734 | return true; |
| 735 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 739 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 740 | if (Expr *Init = D->getInitExpr()) |
| 741 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
| 742 | return false; |
| 743 | } |
| 744 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 745 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 746 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 747 | if (Visit(TSInfo->getTypeLoc())) |
| 748 | return true; |
| 749 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 750 | // Visit the nested-name-specifier, if present. |
| 751 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 752 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 753 | return true; |
| 754 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 758 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 759 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 760 | CXXCtorInitializer const * const *X |
| 761 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 762 | CXXCtorInitializer const * const *Y |
| 763 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 764 | |
| 765 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 766 | return -1; |
| 767 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 768 | return 1; |
| 769 | else |
| 770 | return 0; |
| 771 | } |
| 772 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 773 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 774 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 775 | // Visit the function declaration's syntactic components in the order |
| 776 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 777 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 778 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 779 | |
| 780 | // If we have a function declared directly (without the use of a typedef), |
| 781 | // visit just the return type. Otherwise, just visit the function's type |
| 782 | // now. |
| 783 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 784 | (!FTL && Visit(TL))) |
| 785 | return true; |
| 786 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 787 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 788 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 789 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 790 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 791 | |
| 792 | // Visit the declaration name. |
| 793 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 794 | return true; |
| 795 | |
| 796 | // FIXME: Visit explicitly-specified template arguments! |
| 797 | |
| 798 | // Visit the function parameters, if we have a function type. |
| 799 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 800 | return true; |
| 801 | |
| 802 | // FIXME: Attributes? |
| 803 | } |
| 804 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 805 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 806 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 807 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 808 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 809 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 810 | IEnd = Constructor->init_end(); |
| 811 | I != IEnd; ++I) { |
| 812 | if (!(*I)->isWritten()) |
| 813 | continue; |
| 814 | |
| 815 | WrittenInits.push_back(*I); |
| 816 | } |
| 817 | |
| 818 | // Sort the initializers in source order |
| 819 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 820 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 821 | |
| 822 | // Visit the initializers in source order |
| 823 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 824 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 825 | if (Init->isAnyMemberInitializer()) { |
| 826 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 827 | Init->getMemberLocation(), TU))) |
| 828 | return true; |
| 829 | } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) { |
| 830 | if (Visit(BaseInfo->getTypeLoc())) |
| 831 | return true; |
| 832 | } |
| 833 | |
| 834 | // Visit the initializer value. |
| 835 | if (Expr *Initializer = Init->getInit()) |
| 836 | if (Visit(MakeCXCursor(Initializer, ND, TU))) |
| 837 | return true; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 842 | return true; |
| 843 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 845 | return false; |
| 846 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 848 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 849 | if (VisitDeclaratorDecl(D)) |
| 850 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 852 | if (Expr *BitWidth = D->getBitWidth()) |
| 853 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 854 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 855 | return false; |
| 856 | } |
| 857 | |
| 858 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 859 | if (VisitDeclaratorDecl(D)) |
| 860 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 862 | if (Expr *Init = D->getInit()) |
| 863 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 864 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 868 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 869 | if (VisitDeclaratorDecl(D)) |
| 870 | return true; |
| 871 | |
| 872 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 873 | if (Expr *DefArg = D->getDefaultArgument()) |
| 874 | return Visit(MakeCXCursor(DefArg, StmtParent, TU)); |
| 875 | |
| 876 | return false; |
| 877 | } |
| 878 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 879 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 880 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 881 | // before visiting these template parameters. |
| 882 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 883 | return true; |
| 884 | |
| 885 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 886 | } |
| 887 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 888 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 889 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 890 | // before visiting these template parameters. |
| 891 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 892 | return true; |
| 893 | |
| 894 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 895 | } |
| 896 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 897 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 898 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 899 | return true; |
| 900 | |
| 901 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 902 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 903 | return true; |
| 904 | |
| 905 | return false; |
| 906 | } |
| 907 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 908 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 909 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 910 | if (Visit(TSInfo->getTypeLoc())) |
| 911 | return true; |
| 912 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 913 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 914 | PEnd = ND->param_end(); |
| 915 | P != PEnd; ++P) { |
| 916 | if (Visit(MakeCXCursor(*P, TU))) |
| 917 | return true; |
| 918 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 920 | if (ND->isThisDeclarationADefinition() && |
| 921 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 922 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 924 | return false; |
| 925 | } |
| 926 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 927 | namespace { |
| 928 | struct ContainerDeclsSort { |
| 929 | SourceManager &SM; |
| 930 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 931 | bool operator()(Decl *A, Decl *B) { |
| 932 | SourceLocation L_A = A->getLocStart(); |
| 933 | SourceLocation L_B = B->getLocStart(); |
| 934 | assert(L_A.isValid() && L_B.isValid()); |
| 935 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 936 | } |
| 937 | }; |
| 938 | } |
| 939 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 940 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 941 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 942 | // an @implementation can lexically contain Decls that are not properly |
| 943 | // nested in the AST. When we identify such cases, we need to retrofit |
| 944 | // this nesting here. |
| 945 | if (!DI_current) |
| 946 | return VisitDeclContext(D); |
| 947 | |
| 948 | // Scan the Decls that immediately come after the container |
| 949 | // in the current DeclContext. If any fall within the |
| 950 | // container's lexical region, stash them into a vector |
| 951 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 952 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 953 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 954 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 955 | if (EndLoc.isValid()) { |
| 956 | DeclContext::decl_iterator next = *DI_current; |
| 957 | while (++next != DE_current) { |
| 958 | Decl *D_next = *next; |
| 959 | if (!D_next) |
| 960 | break; |
| 961 | SourceLocation L = D_next->getLocStart(); |
| 962 | if (!L.isValid()) |
| 963 | break; |
| 964 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 965 | *DI_current = next; |
| 966 | DeclsInContainer.push_back(D_next); |
| 967 | continue; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // The common case. |
| 974 | if (DeclsInContainer.empty()) |
| 975 | return VisitDeclContext(D); |
| 976 | |
| 977 | // Get all the Decls in the DeclContext, and sort them with the |
| 978 | // additional ones we've collected. Then visit them. |
| 979 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 980 | I!=E; ++I) { |
| 981 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 982 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 983 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 984 | continue; |
| 985 | DeclsInContainer.push_back(subDecl); |
| 986 | } |
| 987 | |
| 988 | // Now sort the Decls so that they appear in lexical order. |
| 989 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 990 | ContainerDeclsSort(SM)); |
| 991 | |
| 992 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 993 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 994 | E = DeclsInContainer.end(); I != E; ++I) { |
| 995 | CXCursor Cursor = MakeCXCursor(*I, TU); |
| 996 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 997 | if (!V.hasValue()) |
| 998 | continue; |
| 999 | if (!V.getValue()) |
| 1000 | return false; |
| 1001 | if (Visit(Cursor, true)) |
| 1002 | return true; |
| 1003 | } |
| 1004 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1007 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1008 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1009 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1010 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1011 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1012 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1013 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1014 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1015 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1016 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1017 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1018 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1021 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1022 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1023 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1024 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1025 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1026 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1028 | return VisitObjCContainerDecl(PID); |
| 1029 | } |
| 1030 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1031 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 1032 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 1033 | return true; |
| 1034 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1035 | // FIXME: This implements a workaround with @property declarations also being |
| 1036 | // installed in the DeclContext for the @interface. Eventually this code |
| 1037 | // should be removed. |
| 1038 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1039 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1040 | return false; |
| 1041 | |
| 1042 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1043 | if (!ID) |
| 1044 | return false; |
| 1045 | |
| 1046 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1047 | ObjCPropertyDecl *prevDecl = |
| 1048 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 1049 | |
| 1050 | if (!prevDecl) |
| 1051 | return false; |
| 1052 | |
| 1053 | // Visit synthesized methods since they will be skipped when visiting |
| 1054 | // the @interface. |
| 1055 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1056 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1057 | if (Visit(MakeCXCursor(MD, TU))) |
| 1058 | return true; |
| 1059 | |
| 1060 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1061 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1062 | if (Visit(MakeCXCursor(MD, TU))) |
| 1063 | return true; |
| 1064 | |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1068 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1069 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1070 | if (D->getSuperClass() && |
| 1071 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1072 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1073 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1074 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1076 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1077 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1078 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1079 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1080 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1082 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1085 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1086 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1089 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1090 | // 'ID' could be null when dealing with invalid code. |
| 1091 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1092 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1093 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1095 | return VisitObjCImplDecl(D); |
| 1096 | } |
| 1097 | |
| 1098 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1099 | #if 0 |
| 1100 | // Issue callbacks for super class. |
| 1101 | // FIXME: No source location information! |
| 1102 | if (D->getSuperClass() && |
| 1103 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1104 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1105 | TU))) |
| 1106 | return true; |
| 1107 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1109 | return VisitObjCImplDecl(D); |
| 1110 | } |
| 1111 | |
| 1112 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1113 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1114 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1115 | E = D->protocol_end(); |
| 1116 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1117 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1118 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1119 | |
| 1120 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1123 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 1124 | for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C) |
| 1125 | if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU))) |
| 1126 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1127 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1128 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1131 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1132 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1133 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1134 | |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1138 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1139 | return VisitDeclContext(D); |
| 1140 | } |
| 1141 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1142 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1143 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1144 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1145 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1146 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1147 | |
| 1148 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1149 | D->getTargetNameLoc(), TU)); |
| 1150 | } |
| 1151 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1152 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1153 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1154 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1155 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1156 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1157 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1158 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1159 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1160 | return true; |
| 1161 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1162 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1163 | } |
| 1164 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1165 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1166 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1167 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1168 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1169 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1170 | |
| 1171 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1172 | D->getIdentLocation(), TU)); |
| 1173 | } |
| 1174 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1175 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1176 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1177 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1178 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1179 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1180 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1182 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1183 | } |
| 1184 | |
| 1185 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1186 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1187 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1188 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1189 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1190 | return true; |
| 1191 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
| 1194 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1195 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1196 | switch (Name.getName().getNameKind()) { |
| 1197 | case clang::DeclarationName::Identifier: |
| 1198 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1199 | case clang::DeclarationName::CXXOperatorName: |
| 1200 | case clang::DeclarationName::CXXUsingDirective: |
| 1201 | return false; |
| 1202 | |
| 1203 | case clang::DeclarationName::CXXConstructorName: |
| 1204 | case clang::DeclarationName::CXXDestructorName: |
| 1205 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1206 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1207 | return Visit(TSInfo->getTypeLoc()); |
| 1208 | return false; |
| 1209 | |
| 1210 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1211 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1212 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1213 | // FIXME: Per-identifier location info? |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1220 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1221 | SourceRange Range) { |
| 1222 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1223 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1224 | // a beginning source location, we can visit the first component of the |
| 1225 | // nested-name-specifier, if it's a single-token component. |
| 1226 | if (!NNS) |
| 1227 | return false; |
| 1228 | |
| 1229 | // Get the first component in the nested-name-specifier. |
| 1230 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1231 | NNS = Prefix; |
| 1232 | |
| 1233 | switch (NNS->getKind()) { |
| 1234 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1235 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1236 | TU)); |
| 1237 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1238 | case NestedNameSpecifier::NamespaceAlias: |
| 1239 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1240 | Range.getBegin(), TU)); |
| 1241 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1242 | case NestedNameSpecifier::TypeSpec: { |
| 1243 | // If the type has a form where we know that the beginning of the source |
| 1244 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1245 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1246 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1247 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1248 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1249 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1250 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1251 | if (const TemplateSpecializationType *TST |
| 1252 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1253 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1258 | case NestedNameSpecifier::Global: |
| 1259 | case NestedNameSpecifier::Identifier: |
| 1260 | break; |
| 1261 | } |
| 1262 | |
| 1263 | return false; |
| 1264 | } |
| 1265 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1266 | bool |
| 1267 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1268 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1269 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1270 | Qualifiers.push_back(Qualifier); |
| 1271 | |
| 1272 | while (!Qualifiers.empty()) { |
| 1273 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1274 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1275 | switch (NNS->getKind()) { |
| 1276 | case NestedNameSpecifier::Namespace: |
| 1277 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1278 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1279 | TU))) |
| 1280 | return true; |
| 1281 | |
| 1282 | break; |
| 1283 | |
| 1284 | case NestedNameSpecifier::NamespaceAlias: |
| 1285 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1286 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1287 | TU))) |
| 1288 | return true; |
| 1289 | |
| 1290 | break; |
| 1291 | |
| 1292 | case NestedNameSpecifier::TypeSpec: |
| 1293 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1294 | if (Visit(Q.getTypeLoc())) |
| 1295 | return true; |
| 1296 | |
| 1297 | break; |
| 1298 | |
| 1299 | case NestedNameSpecifier::Global: |
| 1300 | case NestedNameSpecifier::Identifier: |
| 1301 | break; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1308 | bool CursorVisitor::VisitTemplateParameters( |
| 1309 | const TemplateParameterList *Params) { |
| 1310 | if (!Params) |
| 1311 | return false; |
| 1312 | |
| 1313 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1314 | PEnd = Params->end(); |
| 1315 | P != PEnd; ++P) { |
| 1316 | if (Visit(MakeCXCursor(*P, TU))) |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1323 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1324 | switch (Name.getKind()) { |
| 1325 | case TemplateName::Template: |
| 1326 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1327 | |
| 1328 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1329 | // Visit the overloaded template set. |
| 1330 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1331 | return true; |
| 1332 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1333 | return false; |
| 1334 | |
| 1335 | case TemplateName::DependentTemplate: |
| 1336 | // FIXME: Visit nested-name-specifier. |
| 1337 | return false; |
| 1338 | |
| 1339 | case TemplateName::QualifiedTemplate: |
| 1340 | // FIXME: Visit nested-name-specifier. |
| 1341 | return Visit(MakeCursorTemplateRef( |
| 1342 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1343 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1344 | |
| 1345 | case TemplateName::SubstTemplateTemplateParm: |
| 1346 | return Visit(MakeCursorTemplateRef( |
| 1347 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1348 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1349 | |
| 1350 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1351 | return Visit(MakeCursorTemplateRef( |
| 1352 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1353 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1359 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1360 | switch (TAL.getArgument().getKind()) { |
| 1361 | case TemplateArgument::Null: |
| 1362 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1363 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1364 | return false; |
| 1365 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1366 | case TemplateArgument::Type: |
| 1367 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1368 | return Visit(TSInfo->getTypeLoc()); |
| 1369 | return false; |
| 1370 | |
| 1371 | case TemplateArgument::Declaration: |
| 1372 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1373 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1374 | return false; |
| 1375 | |
| 1376 | case TemplateArgument::Expression: |
| 1377 | if (Expr *E = TAL.getSourceExpression()) |
| 1378 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1379 | return false; |
| 1380 | |
| 1381 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1382 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1383 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1384 | return true; |
| 1385 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1386 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1387 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1393 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1394 | return VisitDeclContext(D); |
| 1395 | } |
| 1396 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1397 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1398 | return Visit(TL.getUnqualifiedLoc()); |
| 1399 | } |
| 1400 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1401 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1402 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1405 | // "Class") have associated declarations. Create cursors for those. |
| 1406 | QualType VisitType; |
| 1407 | switch (TL.getType()->getAs<BuiltinType>()->getKind()) { |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1408 | case BuiltinType::Void: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1409 | case BuiltinType::Bool: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1410 | case BuiltinType::Char_U: |
| 1411 | case BuiltinType::UChar: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1412 | case BuiltinType::Char16: |
| 1413 | case BuiltinType::Char32: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1414 | case BuiltinType::UShort: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1415 | case BuiltinType::UInt: |
| 1416 | case BuiltinType::ULong: |
| 1417 | case BuiltinType::ULongLong: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1418 | case BuiltinType::UInt128: |
| 1419 | case BuiltinType::Char_S: |
| 1420 | case BuiltinType::SChar: |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1421 | case BuiltinType::WChar_U: |
| 1422 | case BuiltinType::WChar_S: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1423 | case BuiltinType::Short: |
| 1424 | case BuiltinType::Int: |
| 1425 | case BuiltinType::Long: |
| 1426 | case BuiltinType::LongLong: |
| 1427 | case BuiltinType::Int128: |
| 1428 | case BuiltinType::Float: |
| 1429 | case BuiltinType::Double: |
| 1430 | case BuiltinType::LongDouble: |
| 1431 | case BuiltinType::NullPtr: |
| 1432 | case BuiltinType::Overload: |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1433 | case BuiltinType::BoundMember: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1434 | case BuiltinType::Dependent: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1435 | case BuiltinType::UnknownAny: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1436 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1437 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1438 | case BuiltinType::ObjCId: |
| 1439 | VisitType = Context.getObjCIdType(); |
| 1440 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1441 | |
| 1442 | case BuiltinType::ObjCClass: |
| 1443 | VisitType = Context.getObjCClassType(); |
| 1444 | break; |
| 1445 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1446 | case BuiltinType::ObjCSel: |
| 1447 | VisitType = Context.getObjCSelType(); |
| 1448 | break; |
| 1449 | } |
| 1450 | |
| 1451 | if (!VisitType.isNull()) { |
| 1452 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1453 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1454 | TU)); |
| 1455 | } |
| 1456 | |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1460 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1461 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1464 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1465 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1466 | } |
| 1467 | |
| 1468 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1469 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1470 | } |
| 1471 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1472 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1473 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1476 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1477 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1478 | return true; |
| 1479 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1484 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1485 | return true; |
| 1486 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1487 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1488 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1489 | TU))) |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1497 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1500 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1501 | return Visit(TL.getInnerLoc()); |
| 1502 | } |
| 1503 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1504 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1505 | return Visit(TL.getPointeeLoc()); |
| 1506 | } |
| 1507 | |
| 1508 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1509 | return Visit(TL.getPointeeLoc()); |
| 1510 | } |
| 1511 | |
| 1512 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1513 | return Visit(TL.getPointeeLoc()); |
| 1514 | } |
| 1515 | |
| 1516 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1517 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1521 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1524 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1525 | bool SkipResultType) { |
| 1526 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1527 | return true; |
| 1528 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1529 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1530 | if (Decl *D = TL.getArg(I)) |
| 1531 | if (Visit(MakeCXCursor(D, TU))) |
| 1532 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1533 | |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1538 | if (Visit(TL.getElementLoc())) |
| 1539 | return true; |
| 1540 | |
| 1541 | if (Expr *Size = TL.getSizeExpr()) |
| 1542 | return Visit(MakeCXCursor(Size, StmtParent, TU)); |
| 1543 | |
| 1544 | return false; |
| 1545 | } |
| 1546 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1547 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1548 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1549 | // Visit the template name. |
| 1550 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1551 | TL.getTemplateNameLoc())) |
| 1552 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Visit the template arguments. |
| 1555 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1556 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1557 | return true; |
| 1558 | |
| 1559 | return false; |
| 1560 | } |
| 1561 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1562 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1563 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1564 | } |
| 1565 | |
| 1566 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1567 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1568 | return Visit(TSInfo->getTypeLoc()); |
| 1569 | |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1573 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1574 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1575 | return Visit(TSInfo->getTypeLoc()); |
| 1576 | |
| 1577 | return false; |
| 1578 | } |
| 1579 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1580 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1581 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1582 | return true; |
| 1583 | |
| 1584 | return false; |
| 1585 | } |
| 1586 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1587 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1588 | DependentTemplateSpecializationTypeLoc TL) { |
| 1589 | // Visit the nested-name-specifier, if there is one. |
| 1590 | if (TL.getQualifierLoc() && |
| 1591 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1592 | return true; |
| 1593 | |
| 1594 | // Visit the template arguments. |
| 1595 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1596 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1597 | return true; |
| 1598 | |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1602 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1603 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1604 | return true; |
| 1605 | |
| 1606 | return Visit(TL.getNamedTypeLoc()); |
| 1607 | } |
| 1608 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1609 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1610 | return Visit(TL.getPatternLoc()); |
| 1611 | } |
| 1612 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1613 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1614 | // Visit the nested-name-specifier, if present. |
| 1615 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1616 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1617 | return true; |
| 1618 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1619 | if (D->isDefinition()) { |
| 1620 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1621 | E = D->bases_end(); I != E; ++I) { |
| 1622 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1623 | return true; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | return VisitTagDecl(D); |
| 1628 | } |
| 1629 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1630 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1631 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1632 | i != e; ++i) |
| 1633 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1634 | return true; |
| 1635 | |
| 1636 | return false; |
| 1637 | } |
| 1638 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1639 | //===----------------------------------------------------------------------===// |
| 1640 | // Data-recursive visitor methods. |
| 1641 | //===----------------------------------------------------------------------===// |
| 1642 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1643 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1644 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1645 | class NAME : public VisitorJob {\ |
| 1646 | public:\ |
| 1647 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1648 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1649 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1650 | }; |
| 1651 | |
| 1652 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1653 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1654 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1655 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1656 | DEF_JOB(ExplicitTemplateArgsVisit, ExplicitTemplateArgumentList, |
| 1657 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1658 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1659 | #undef DEF_JOB |
| 1660 | |
| 1661 | class DeclVisit : public VisitorJob { |
| 1662 | public: |
| 1663 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1664 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1665 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1666 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1667 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1668 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1669 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1670 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1671 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1672 | class TypeLocVisit : public VisitorJob { |
| 1673 | public: |
| 1674 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1675 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1676 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1677 | |
| 1678 | static bool classof(const VisitorJob *VJ) { |
| 1679 | return VJ->getKind() == TypeLocVisitKind; |
| 1680 | } |
| 1681 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1682 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1683 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1684 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1685 | } |
| 1686 | }; |
| 1687 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1688 | class LabelRefVisit : public VisitorJob { |
| 1689 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1690 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1691 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1692 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1693 | |
| 1694 | static bool classof(const VisitorJob *VJ) { |
| 1695 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1696 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1697 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1698 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1699 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1700 | }; |
| 1701 | class NestedNameSpecifierVisit : public VisitorJob { |
| 1702 | public: |
| 1703 | NestedNameSpecifierVisit(NestedNameSpecifier *NS, SourceRange R, |
| 1704 | CXCursor parent) |
| 1705 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierVisitKind, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1706 | NS, R.getBegin().getPtrEncoding(), |
| 1707 | R.getEnd().getPtrEncoding()) {} |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1708 | static bool classof(const VisitorJob *VJ) { |
| 1709 | return VJ->getKind() == VisitorJob::NestedNameSpecifierVisitKind; |
| 1710 | } |
| 1711 | NestedNameSpecifier *get() const { |
| 1712 | return static_cast<NestedNameSpecifier*>(data[0]); |
| 1713 | } |
| 1714 | SourceRange getSourceRange() const { |
| 1715 | SourceLocation A = |
| 1716 | SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1717 | SourceLocation B = |
| 1718 | SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[2]); |
| 1719 | return SourceRange(A, B); |
| 1720 | } |
| 1721 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1722 | |
| 1723 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1724 | public: |
| 1725 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1726 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1727 | Qualifier.getNestedNameSpecifier(), |
| 1728 | Qualifier.getOpaqueData()) { } |
| 1729 | |
| 1730 | static bool classof(const VisitorJob *VJ) { |
| 1731 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1732 | } |
| 1733 | |
| 1734 | NestedNameSpecifierLoc get() const { |
| 1735 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1736 | data[1]); |
| 1737 | } |
| 1738 | }; |
| 1739 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1740 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1741 | public: |
| 1742 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1743 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1744 | static bool classof(const VisitorJob *VJ) { |
| 1745 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1746 | } |
| 1747 | DeclarationNameInfo get() const { |
| 1748 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1749 | switch (S->getStmtClass()) { |
| 1750 | default: |
| 1751 | llvm_unreachable("Unhandled Stmt"); |
| 1752 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1753 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1754 | case Stmt::DependentScopeDeclRefExprClass: |
| 1755 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1756 | } |
| 1757 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1758 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1759 | class MemberRefVisit : public VisitorJob { |
| 1760 | public: |
| 1761 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1762 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1763 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1764 | static bool classof(const VisitorJob *VJ) { |
| 1765 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1766 | } |
| 1767 | FieldDecl *get() const { |
| 1768 | return static_cast<FieldDecl*>(data[0]); |
| 1769 | } |
| 1770 | SourceLocation getLoc() const { |
| 1771 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1772 | } |
| 1773 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1774 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1775 | VisitorWorkList &WL; |
| 1776 | CXCursor Parent; |
| 1777 | public: |
| 1778 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1779 | : WL(wl), Parent(parent) {} |
| 1780 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1781 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1782 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1783 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1784 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1785 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1786 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1787 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1788 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1789 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1790 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1791 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1792 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1793 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1794 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1795 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1796 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1797 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1798 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1799 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1800 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1801 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1802 | void VisitIfStmt(IfStmt *If); |
| 1803 | void VisitInitListExpr(InitListExpr *IE); |
| 1804 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1805 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1806 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1807 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1808 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1809 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1810 | void VisitStmt(Stmt *S); |
| 1811 | void VisitSwitchStmt(SwitchStmt *S); |
| 1812 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1813 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1814 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1815 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1816 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1817 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1818 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1819 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1820 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1821 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1822 | void AddDeclarationNameInfo(Stmt *S); |
| 1823 | void AddNestedNameSpecifier(NestedNameSpecifier *NS, SourceRange R); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1824 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1825 | void AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1826 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1827 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1828 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1829 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1830 | void EnqueueChildren(Stmt *S); |
| 1831 | }; |
| 1832 | } // end anonyous namespace |
| 1833 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1834 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1835 | // 'S' should always be non-null, since it comes from the |
| 1836 | // statement we are visiting. |
| 1837 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1838 | } |
| 1839 | void EnqueueVisitor::AddNestedNameSpecifier(NestedNameSpecifier *N, |
| 1840 | SourceRange R) { |
| 1841 | if (N) |
| 1842 | WL.push_back(NestedNameSpecifierVisit(N, R, Parent)); |
| 1843 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1844 | |
| 1845 | void |
| 1846 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1847 | if (Qualifier) |
| 1848 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1849 | } |
| 1850 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1851 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1852 | if (S) |
| 1853 | WL.push_back(StmtVisit(S, Parent)); |
| 1854 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1855 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1856 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1857 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1858 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1859 | void EnqueueVisitor:: |
| 1860 | AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A) { |
| 1861 | if (A) |
| 1862 | WL.push_back(ExplicitTemplateArgsVisit( |
| 1863 | const_cast<ExplicitTemplateArgumentList*>(A), Parent)); |
| 1864 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1865 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1866 | if (D) |
| 1867 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1868 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1869 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1870 | if (TI) |
| 1871 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1872 | } |
| 1873 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1874 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1875 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1876 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1877 | } |
| 1878 | if (size == WL.size()) |
| 1879 | return; |
| 1880 | // Now reverse the entries we just added. This will match the DFS |
| 1881 | // ordering performed by the worklist. |
| 1882 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1883 | std::reverse(I, E); |
| 1884 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1885 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1886 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1887 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1888 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1889 | AddDecl(B->getBlockDecl()); |
| 1890 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1891 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1892 | EnqueueChildren(E); |
| 1893 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1894 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1895 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1896 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1897 | E = S->body_rend(); I != E; ++I) { |
| 1898 | AddStmt(*I); |
| 1899 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1900 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1901 | void EnqueueVisitor:: |
| 1902 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1903 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1904 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1905 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1906 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1907 | if (!E->isImplicitAccess()) |
| 1908 | AddStmt(E->getBase()); |
| 1909 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1910 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1911 | // Enqueue the initializer or constructor arguments. |
| 1912 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 1913 | AddStmt(E->getConstructorArg(I-1)); |
| 1914 | // Enqueue the array size, if any. |
| 1915 | AddStmt(E->getArraySize()); |
| 1916 | // Enqueue the allocated type. |
| 1917 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1918 | // Enqueue the placement arguments. |
| 1919 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1920 | AddStmt(E->getPlacementArg(I-1)); |
| 1921 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1922 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1923 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1924 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1925 | AddStmt(CE->getCallee()); |
| 1926 | AddStmt(CE->getArg(0)); |
| 1927 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1928 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1929 | // Visit the name of the type being destroyed. |
| 1930 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1931 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1932 | // but isn't. |
| 1933 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1934 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1935 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1936 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1937 | // Visit base expression. |
| 1938 | AddStmt(E->getBase()); |
| 1939 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1940 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1941 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1942 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1943 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1944 | EnqueueChildren(E); |
| 1945 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1946 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1947 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1948 | EnqueueChildren(E); |
| 1949 | if (E->isTypeOperand()) |
| 1950 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1951 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1952 | |
| 1953 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1954 | *E) { |
| 1955 | EnqueueChildren(E); |
| 1956 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1957 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1958 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1959 | EnqueueChildren(E); |
| 1960 | if (E->isTypeOperand()) |
| 1961 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1962 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1963 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1964 | if (DR->hasExplicitTemplateArgs()) { |
| 1965 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1966 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1967 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1968 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1969 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1970 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1971 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1972 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1973 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1974 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1975 | unsigned size = WL.size(); |
| 1976 | bool isFirst = true; |
| 1977 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1978 | D != DEnd; ++D) { |
| 1979 | AddDecl(*D, isFirst); |
| 1980 | isFirst = false; |
| 1981 | } |
| 1982 | if (size == WL.size()) |
| 1983 | return; |
| 1984 | // Now reverse the entries we just added. This will match the DFS |
| 1985 | // ordering performed by the worklist. |
| 1986 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1987 | std::reverse(I, E); |
| 1988 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1989 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1990 | AddStmt(E->getInit()); |
| 1991 | typedef DesignatedInitExpr::Designator Designator; |
| 1992 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1993 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1994 | D != DEnd; ++D) { |
| 1995 | if (D->isFieldDesignator()) { |
| 1996 | if (FieldDecl *Field = D->getField()) |
| 1997 | AddMemberRef(Field, D->getFieldLoc()); |
| 1998 | continue; |
| 1999 | } |
| 2000 | if (D->isArrayDesignator()) { |
| 2001 | AddStmt(E->getArrayIndex(*D)); |
| 2002 | continue; |
| 2003 | } |
| 2004 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 2005 | AddStmt(E->getArrayRangeEnd(*D)); |
| 2006 | AddStmt(E->getArrayRangeStart(*D)); |
| 2007 | } |
| 2008 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2009 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 2010 | EnqueueChildren(E); |
| 2011 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2012 | } |
| 2013 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 2014 | AddStmt(FS->getBody()); |
| 2015 | AddStmt(FS->getInc()); |
| 2016 | AddStmt(FS->getCond()); |
| 2017 | AddDecl(FS->getConditionVariable()); |
| 2018 | AddStmt(FS->getInit()); |
| 2019 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2020 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 2021 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2022 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2023 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 2024 | AddStmt(If->getElse()); |
| 2025 | AddStmt(If->getThen()); |
| 2026 | AddStmt(If->getCond()); |
| 2027 | AddDecl(If->getConditionVariable()); |
| 2028 | } |
| 2029 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 2030 | // We care about the syntactic form of the initializer list, only. |
| 2031 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2032 | IE = Syntactic; |
| 2033 | EnqueueChildren(IE); |
| 2034 | } |
| 2035 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 2036 | WL.push_back(MemberExprParts(M, Parent)); |
| 2037 | |
| 2038 | // If the base of the member access expression is an implicit 'this', don't |
| 2039 | // visit it. |
| 2040 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2041 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2042 | if (!M->isImplicitAccess()) |
| 2043 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2044 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2045 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2046 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2047 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2048 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2049 | EnqueueChildren(M); |
| 2050 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2051 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2052 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2053 | // Visit the components of the offsetof expression. |
| 2054 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2055 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2056 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2057 | switch (Node.getKind()) { |
| 2058 | case OffsetOfNode::Array: |
| 2059 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2060 | break; |
| 2061 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2062 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2063 | break; |
| 2064 | case OffsetOfNode::Identifier: |
| 2065 | case OffsetOfNode::Base: |
| 2066 | continue; |
| 2067 | } |
| 2068 | } |
| 2069 | // Visit the type into which we're computing the offset. |
| 2070 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2071 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2072 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2073 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2074 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2075 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2076 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2077 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2078 | EnqueueChildren(E); |
| 2079 | if (E->isArgumentType()) |
| 2080 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2081 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2082 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2083 | EnqueueChildren(S); |
| 2084 | } |
| 2085 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2086 | AddStmt(S->getBody()); |
| 2087 | AddStmt(S->getCond()); |
| 2088 | AddDecl(S->getConditionVariable()); |
| 2089 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2090 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2091 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2092 | AddStmt(W->getBody()); |
| 2093 | AddStmt(W->getCond()); |
| 2094 | AddDecl(W->getConditionVariable()); |
| 2095 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2096 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2097 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2098 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2099 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2100 | |
| 2101 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2102 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2103 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2106 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2107 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2108 | } |
| 2109 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2110 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2111 | EnqueueChildren(E); |
| 2112 | } |
| 2113 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2114 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2115 | VisitOverloadExpr(U); |
| 2116 | if (!U->isImplicitAccess()) |
| 2117 | AddStmt(U->getBase()); |
| 2118 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2119 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2120 | AddStmt(E->getSubExpr()); |
| 2121 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2122 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2123 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2124 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2125 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2126 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2127 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2128 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2132 | if (RegionOfInterest.isValid()) { |
| 2133 | SourceRange Range = getRawCursorExtent(C); |
| 2134 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2135 | return false; |
| 2136 | } |
| 2137 | return true; |
| 2138 | } |
| 2139 | |
| 2140 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2141 | while (!WL.empty()) { |
| 2142 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2143 | VisitorJob LI = WL.back(); |
| 2144 | WL.pop_back(); |
| 2145 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2146 | // Set the Parent field, then back to its old value once we're done. |
| 2147 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2148 | |
| 2149 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2150 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2151 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2152 | if (!D) |
| 2153 | continue; |
| 2154 | |
| 2155 | // For now, perform default visitation for Decls. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2156 | if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2157 | return true; |
| 2158 | |
| 2159 | continue; |
| 2160 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2161 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
| 2162 | const ExplicitTemplateArgumentList *ArgList = |
| 2163 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2164 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2165 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2166 | Arg != ArgEnd; ++Arg) { |
| 2167 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2168 | return true; |
| 2169 | } |
| 2170 | continue; |
| 2171 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2172 | case VisitorJob::TypeLocVisitKind: { |
| 2173 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2174 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2175 | return true; |
| 2176 | continue; |
| 2177 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2178 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2179 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2180 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2181 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2182 | TU))) { |
| 2183 | return true; |
| 2184 | } |
| 2185 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2186 | continue; |
| 2187 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2188 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2189 | case VisitorJob::NestedNameSpecifierVisitKind: { |
| 2190 | NestedNameSpecifierVisit *V = cast<NestedNameSpecifierVisit>(&LI); |
| 2191 | if (VisitNestedNameSpecifier(V->get(), V->getSourceRange())) |
| 2192 | return true; |
| 2193 | continue; |
| 2194 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2195 | |
| 2196 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2197 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2198 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2199 | return true; |
| 2200 | continue; |
| 2201 | } |
| 2202 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2203 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2204 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2205 | ->get())) |
| 2206 | return true; |
| 2207 | continue; |
| 2208 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2209 | case VisitorJob::MemberRefVisitKind: { |
| 2210 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2211 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2212 | return true; |
| 2213 | continue; |
| 2214 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2215 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2216 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2217 | if (!S) |
| 2218 | continue; |
| 2219 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2220 | // Update the current cursor. |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2221 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2222 | if (!IsInRegionOfInterest(Cursor)) |
| 2223 | continue; |
| 2224 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2225 | case CXChildVisit_Break: return true; |
| 2226 | case CXChildVisit_Continue: break; |
| 2227 | case CXChildVisit_Recurse: |
| 2228 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2229 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2230 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2231 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2232 | } |
| 2233 | case VisitorJob::MemberExprPartsKind: { |
| 2234 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2235 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2238 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2239 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2240 | return true; |
| 2241 | |
| 2242 | // Visit the declaration name. |
| 2243 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2244 | return true; |
| 2245 | |
| 2246 | // Visit the explicitly-specified template arguments, if any. |
| 2247 | if (M->hasExplicitTemplateArgs()) { |
| 2248 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2249 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2250 | Arg != ArgEnd; ++Arg) { |
| 2251 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2252 | return true; |
| 2253 | } |
| 2254 | } |
| 2255 | continue; |
| 2256 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2257 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2258 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2259 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2260 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2261 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2262 | return true; |
| 2263 | // Visit declaration name. |
| 2264 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2265 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2266 | continue; |
| 2267 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2268 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2269 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2270 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2271 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2272 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2273 | return true; |
| 2274 | // Visit the declaration name. |
| 2275 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2276 | return true; |
| 2277 | // Visit the overloaded declaration reference. |
| 2278 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2279 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2280 | continue; |
| 2281 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2282 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2283 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2284 | NamedDecl *Pack = E->getPack(); |
| 2285 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2286 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2287 | E->getPackLoc(), TU))) |
| 2288 | return true; |
| 2289 | |
| 2290 | continue; |
| 2291 | } |
| 2292 | |
| 2293 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2294 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2295 | E->getPackLoc(), TU))) |
| 2296 | return true; |
| 2297 | |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | // Non-type template parameter packs and function parameter packs are |
| 2302 | // treated like DeclRefExpr cursors. |
| 2303 | continue; |
| 2304 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | return false; |
| 2308 | } |
| 2309 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2310 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2311 | VisitorWorkList *WL = 0; |
| 2312 | if (!WorkListFreeList.empty()) { |
| 2313 | WL = WorkListFreeList.back(); |
| 2314 | WL->clear(); |
| 2315 | WorkListFreeList.pop_back(); |
| 2316 | } |
| 2317 | else { |
| 2318 | WL = new VisitorWorkList(); |
| 2319 | WorkListCache.push_back(WL); |
| 2320 | } |
| 2321 | EnqueueWorkList(*WL, S); |
| 2322 | bool result = RunVisitorWorkList(*WL); |
| 2323 | WorkListFreeList.push_back(WL); |
| 2324 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2327 | namespace { |
| 2328 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2329 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2330 | const DeclarationNameInfo &NI, |
| 2331 | const SourceRange &QLoc, |
| 2332 | const ExplicitTemplateArgumentList *TemplateArgs = 0){ |
| 2333 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2334 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2335 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2336 | |
| 2337 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2338 | |
| 2339 | RefNamePieces Pieces; |
| 2340 | |
| 2341 | if (WantQualifier && QLoc.isValid()) |
| 2342 | Pieces.push_back(QLoc); |
| 2343 | |
| 2344 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2345 | Pieces.push_back(NI.getLoc()); |
| 2346 | |
| 2347 | if (WantTemplateArgs && TemplateArgs) |
| 2348 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2349 | TemplateArgs->RAngleLoc)); |
| 2350 | |
| 2351 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2352 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2353 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2354 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2355 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2356 | } |
| 2357 | |
| 2358 | if (WantSinglePiece) { |
| 2359 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2360 | Pieces.clear(); |
| 2361 | Pieces.push_back(R); |
| 2362 | } |
| 2363 | |
| 2364 | return Pieces; |
| 2365 | } |
| 2366 | } |
| 2367 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2368 | //===----------------------------------------------------------------------===// |
| 2369 | // Misc. API hooks. |
| 2370 | //===----------------------------------------------------------------------===// |
| 2371 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2372 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2373 | static bool EnabledMultithreading; |
| 2374 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2375 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2376 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2377 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2378 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2379 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2380 | llvm::DisablePrettyStackTrace = true; |
| 2381 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2382 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2383 | // enable it. |
| 2384 | llvm::CrashRecoveryContext::Enable(); |
| 2385 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2386 | // Enable support for multithreading in LLVM. |
| 2387 | { |
| 2388 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2389 | if (!EnabledMultithreading) { |
| 2390 | llvm::llvm_start_multithreaded(); |
| 2391 | EnabledMultithreading = true; |
| 2392 | } |
| 2393 | } |
| 2394 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2395 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2396 | if (excludeDeclarationsFromPCH) |
| 2397 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2398 | if (displayDiagnostics) |
| 2399 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2400 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2403 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2404 | if (CIdx) |
| 2405 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2408 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2409 | if (isEnabled) |
| 2410 | llvm::CrashRecoveryContext::Enable(); |
| 2411 | else |
| 2412 | llvm::CrashRecoveryContext::Disable(); |
| 2413 | } |
| 2414 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2415 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2416 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2417 | if (!CIdx) |
| 2418 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2420 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2421 | FileSystemOptions FileSystemOpts; |
| 2422 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2423 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 2424 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2425 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2426 | CXXIdx->getOnlyLocalDecls(), |
| 2427 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2428 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2431 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2432 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2433 | CXTranslationUnit_CacheCompletionResults | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2434 | CXTranslationUnit_CXXPrecompiledPreamble | |
| 2435 | CXTranslationUnit_CXXChainedPCH; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2438 | CXTranslationUnit |
| 2439 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2440 | const char *source_filename, |
| 2441 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2442 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2443 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2444 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2445 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2446 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2447 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2448 | command_line_args, num_command_line_args, |
| 2449 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2450 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2451 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2452 | |
| 2453 | struct ParseTranslationUnitInfo { |
| 2454 | CXIndex CIdx; |
| 2455 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2456 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2457 | int num_command_line_args; |
| 2458 | struct CXUnsavedFile *unsaved_files; |
| 2459 | unsigned num_unsaved_files; |
| 2460 | unsigned options; |
| 2461 | CXTranslationUnit result; |
| 2462 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2463 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2464 | ParseTranslationUnitInfo *PTUI = |
| 2465 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2466 | CXIndex CIdx = PTUI->CIdx; |
| 2467 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2468 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2469 | int num_command_line_args = PTUI->num_command_line_args; |
| 2470 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2471 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2472 | unsigned options = PTUI->options; |
| 2473 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2474 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2475 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2476 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2477 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2478 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2479 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2480 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2481 | bool CompleteTranslationUnit |
| 2482 | = ((options & CXTranslationUnit_Incomplete) == 0); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2483 | bool CacheCodeCompetionResults |
| 2484 | = options & CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2485 | bool CXXPrecompilePreamble |
| 2486 | = options & CXTranslationUnit_CXXPrecompiledPreamble; |
| 2487 | bool CXXChainedPCH |
| 2488 | = options & CXTranslationUnit_CXXChainedPCH; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2490 | // Configure the diagnostics. |
| 2491 | DiagnosticOptions DiagOpts; |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2492 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 2493 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2494 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2495 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2496 | // Recover resources if we crash before exiting this function. |
| 2497 | llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic, |
| 2498 | llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> > |
| 2499 | DiagCleanup(Diags.getPtr()); |
| 2500 | |
| 2501 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2502 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2503 | |
| 2504 | // Recover resources if we crash before exiting this function. |
| 2505 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2506 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2507 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2508 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2509 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2510 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2511 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2512 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2513 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2514 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2515 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2516 | llvm::OwningPtr<std::vector<const char *> > |
| 2517 | Args(new std::vector<const char*>()); |
| 2518 | |
| 2519 | // Recover resources if we crash before exiting this method. |
| 2520 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2521 | ArgsCleanup(Args.get()); |
| 2522 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2523 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2524 | // (often very broken) source code, where spell-checking can have a |
| 2525 | // significant negative impact on performance (particularly when |
| 2526 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2527 | // Only do this if we haven't found a spell-checking-related argument. |
| 2528 | bool FoundSpellCheckingArgument = false; |
| 2529 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2530 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2531 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2532 | FoundSpellCheckingArgument = true; |
| 2533 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2534 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2537 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2538 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2539 | Args->insert(Args->end(), command_line_args, |
| 2540 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2541 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2542 | // The 'source_filename' argument is optional. If the caller does not |
| 2543 | // specify it then it is assumed that the source file is specified |
| 2544 | // in the actual argument list. |
| 2545 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2546 | // present it will be unused. |
| 2547 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2548 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2550 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2551 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2552 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2553 | Args->push_back("-Xclang"); |
| 2554 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2555 | NestedMacroExpansions |
| 2556 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2559 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2560 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2561 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2562 | /* vector::data() not portable */, |
| 2563 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2564 | Diags, |
| 2565 | CXXIdx->getClangResourcesPath(), |
| 2566 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2567 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2568 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2569 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2570 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2571 | PrecompilePreamble, |
| 2572 | CompleteTranslationUnit, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2573 | CacheCodeCompetionResults, |
| 2574 | CXXPrecompilePreamble, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2575 | CXXChainedPCH, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2576 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2577 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2578 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2579 | // Make sure to check that 'Unit' is non-NULL. |
| 2580 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2581 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2582 | DEnd = Unit->stored_diag_end(); |
| 2583 | D != DEnd; ++D) { |
| 2584 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2585 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2586 | clang_defaultDiagnosticDisplayOptions()); |
| 2587 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2588 | clang_disposeString(Msg); |
| 2589 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2590 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2591 | // On Windows, force a flush, since there may be multiple copies of |
| 2592 | // stderr and stdout in the file system, all with different buffers |
| 2593 | // but writing to the same device. |
| 2594 | fflush(stderr); |
| 2595 | #endif |
| 2596 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2597 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2598 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2599 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2600 | } |
| 2601 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2602 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2603 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2604 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2605 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2606 | unsigned num_unsaved_files, |
| 2607 | unsigned options) { |
| 2608 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2609 | num_command_line_args, unsaved_files, |
| 2610 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2611 | llvm::CrashRecoveryContext CRC; |
| 2612 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2613 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2614 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2615 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2616 | fprintf(stderr, " 'command_line_args' : ["); |
| 2617 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2618 | if (i) |
| 2619 | fprintf(stderr, ", "); |
| 2620 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2621 | } |
| 2622 | fprintf(stderr, "],\n"); |
| 2623 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2624 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2625 | if (i) |
| 2626 | fprintf(stderr, ", "); |
| 2627 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2628 | unsaved_files[i].Length); |
| 2629 | } |
| 2630 | fprintf(stderr, "],\n"); |
| 2631 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2632 | fprintf(stderr, "}\n"); |
| 2633 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2634 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2635 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2636 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2637 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2638 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2639 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2642 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2643 | return CXSaveTranslationUnit_None; |
| 2644 | } |
| 2645 | |
| 2646 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2647 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2648 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2649 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2651 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2652 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2653 | PrintLibclangResourceUsage(TU); |
| 2654 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2655 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2656 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2657 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2658 | if (CTUnit) { |
| 2659 | // If the translation unit has been marked as unsafe to free, just discard |
| 2660 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2661 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2664 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2665 | disposeCXStringPool(CTUnit->StringPool); |
| 2666 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2667 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2668 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2670 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2671 | return CXReparse_None; |
| 2672 | } |
| 2673 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2674 | struct ReparseTranslationUnitInfo { |
| 2675 | CXTranslationUnit TU; |
| 2676 | unsigned num_unsaved_files; |
| 2677 | struct CXUnsavedFile *unsaved_files; |
| 2678 | unsigned options; |
| 2679 | int result; |
| 2680 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2681 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2682 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2683 | ReparseTranslationUnitInfo *RTUI = |
| 2684 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2685 | CXTranslationUnit TU = RTUI->TU; |
| 2686 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2687 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2688 | unsigned options = RTUI->options; |
| 2689 | (void) options; |
| 2690 | RTUI->result = 1; |
| 2691 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2692 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2693 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2694 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2695 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2696 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2697 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2698 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2699 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2700 | |
| 2701 | // Recover resources if we crash before exiting this function. |
| 2702 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2703 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2704 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2705 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2706 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2707 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2708 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2709 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2710 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2713 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2714 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2715 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2716 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2717 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2718 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2719 | unsigned num_unsaved_files, |
| 2720 | struct CXUnsavedFile *unsaved_files, |
| 2721 | unsigned options) { |
| 2722 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2723 | options, 0 }; |
| 2724 | llvm::CrashRecoveryContext CRC; |
| 2725 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2726 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2727 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2728 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2729 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2730 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2731 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2732 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2733 | return RTUI.result; |
| 2734 | } |
| 2735 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2736 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2737 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2738 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2739 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2740 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2741 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2742 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2743 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2745 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2746 | CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2747 | return Result; |
| 2748 | } |
| 2749 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2750 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2751 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2752 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2753 | // CXSourceLocation and CXSourceRange Operations. |
| 2754 | //===----------------------------------------------------------------------===// |
| 2755 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2756 | extern "C" { |
| 2757 | CXSourceLocation clang_getNullLocation() { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2758 | CXSourceLocation Result = { { 0, 0 }, 0 }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2759 | return Result; |
| 2760 | } |
| 2761 | |
| 2762 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { |
Daniel Dunbar | 90a6b9e | 2010-01-30 23:58:27 +0000 | [diff] [blame] | 2763 | return (loc1.ptr_data[0] == loc2.ptr_data[0] && |
| 2764 | loc1.ptr_data[1] == loc2.ptr_data[1] && |
| 2765 | loc1.int_data == loc2.int_data); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, |
| 2769 | CXFile file, |
| 2770 | unsigned line, |
| 2771 | unsigned column) { |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2772 | if (!tu || !file) |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2773 | return clang_getNullLocation(); |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2775 | bool Logging = ::getenv("LIBCLANG_LOGGING"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2776 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2777 | const FileEntry *File = static_cast<const FileEntry *>(file); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2778 | SourceLocation SLoc |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2779 | = CXXUnit->getSourceManager().getLocation(File, line, column); |
| 2780 | if (SLoc.isInvalid()) { |
| 2781 | if (Logging) |
| 2782 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2783 | << "\", " << line << ", " << column << ") = invalid\n"; |
| 2784 | return clang_getNullLocation(); |
| 2785 | } |
| 2786 | |
| 2787 | if (Logging) |
| 2788 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2789 | << "\", " << line << ", " << column << ") = " |
| 2790 | << SLoc.getRawEncoding() << "\n"; |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2791 | |
| 2792 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
| 2793 | } |
| 2794 | |
| 2795 | CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, |
| 2796 | CXFile file, |
| 2797 | unsigned offset) { |
| 2798 | if (!tu || !file) |
| 2799 | return clang_getNullLocation(); |
| 2800 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2801 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2802 | SourceLocation Start |
| 2803 | = CXXUnit->getSourceManager().getLocation( |
| 2804 | static_cast<const FileEntry *>(file), |
| 2805 | 1, 1); |
| 2806 | if (Start.isInvalid()) return clang_getNullLocation(); |
| 2807 | |
| 2808 | SourceLocation SLoc = Start.getFileLocWithOffset(offset); |
| 2809 | |
| 2810 | if (SLoc.isInvalid()) return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2811 | |
Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 2812 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2815 | CXSourceRange clang_getNullRange() { |
| 2816 | CXSourceRange Result = { { 0, 0 }, 0, 0 }; |
| 2817 | return Result; |
| 2818 | } |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2820 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { |
| 2821 | if (begin.ptr_data[0] != end.ptr_data[0] || |
| 2822 | begin.ptr_data[1] != end.ptr_data[1]) |
| 2823 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2824 | |
| 2825 | CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2826 | begin.int_data, end.int_data }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2827 | return Result; |
| 2828 | } |
Douglas Gregor | ab4e83b | 2011-07-23 19:35:14 +0000 | [diff] [blame] | 2829 | |
| 2830 | unsigned clang_equalRanges(CXSourceRange range1, CXSourceRange range2) |
| 2831 | { |
| 2832 | return range1.ptr_data[0] == range2.ptr_data[0] |
| 2833 | && range1.ptr_data[1] == range2.ptr_data[1] |
| 2834 | && range1.begin_int_data == range2.begin_int_data |
| 2835 | && range1.end_int_data == range2.end_int_data; |
| 2836 | } |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2837 | } // end: extern "C" |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2838 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2839 | static void createNullLocation(CXFile *file, unsigned *line, |
| 2840 | unsigned *column, unsigned *offset) { |
| 2841 | if (file) |
| 2842 | *file = 0; |
| 2843 | if (line) |
| 2844 | *line = 0; |
| 2845 | if (column) |
| 2846 | *column = 0; |
| 2847 | if (offset) |
| 2848 | *offset = 0; |
| 2849 | return; |
| 2850 | } |
| 2851 | |
| 2852 | extern "C" { |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2853 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 2854 | CXFile *file, |
| 2855 | unsigned *line, |
| 2856 | unsigned *column, |
| 2857 | unsigned *offset) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2858 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2859 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2860 | if (!location.ptr_data[0] || Loc.isInvalid()) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2861 | createNullLocation(file, line, column, offset); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2862 | return; |
| 2863 | } |
| 2864 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2865 | const SourceManager &SM = |
| 2866 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2867 | SourceLocation InstLoc = SM.getExpansionLoc(Loc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2868 | |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 2869 | // Check that the FileID is invalid on the expansion location. |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2870 | // This can manifest in invalid code. |
| 2871 | FileID fileID = SM.getFileID(InstLoc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 2872 | bool Invalid = false; |
| 2873 | const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid); |
| 2874 | if (!sloc.isFile() || Invalid) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2875 | createNullLocation(file, line, column, offset); |
| 2876 | return; |
| 2877 | } |
| 2878 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2879 | if (file) |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2880 | *file = (void *)SM.getFileEntryForSLocEntry(sloc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2881 | if (line) |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 2882 | *line = SM.getExpansionLineNumber(InstLoc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2883 | if (column) |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 2884 | *column = SM.getExpansionColumnNumber(InstLoc); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2885 | if (offset) |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2886 | *offset = SM.getDecomposedLoc(InstLoc).second; |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2889 | void clang_getSpellingLocation(CXSourceLocation location, |
| 2890 | CXFile *file, |
| 2891 | unsigned *line, |
| 2892 | unsigned *column, |
| 2893 | unsigned *offset) { |
| 2894 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2895 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2896 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 2897 | return createNullLocation(file, line, column, offset); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2898 | |
| 2899 | const SourceManager &SM = |
| 2900 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
| 2901 | SourceLocation SpellLoc = Loc; |
| 2902 | if (SpellLoc.isMacroID()) { |
| 2903 | SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); |
| 2904 | if (SimpleSpellingLoc.isFileID() && |
| 2905 | SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) |
| 2906 | SpellLoc = SimpleSpellingLoc; |
| 2907 | else |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2908 | SpellLoc = SM.getExpansionLoc(SpellLoc); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); |
| 2912 | FileID FID = LocInfo.first; |
| 2913 | unsigned FileOffset = LocInfo.second; |
| 2914 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2915 | if (FID.isInvalid()) |
| 2916 | return createNullLocation(file, line, column, offset); |
| 2917 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2918 | if (file) |
| 2919 | *file = (void *)SM.getFileEntryForID(FID); |
| 2920 | if (line) |
| 2921 | *line = SM.getLineNumber(FID, FileOffset); |
| 2922 | if (column) |
| 2923 | *column = SM.getColumnNumber(FID, FileOffset); |
| 2924 | if (offset) |
| 2925 | *offset = FileOffset; |
| 2926 | } |
| 2927 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2928 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2929 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2930 | range.begin_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2931 | return Result; |
| 2932 | } |
| 2933 | |
| 2934 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2935 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2936 | range.end_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2937 | return Result; |
| 2938 | } |
| 2939 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2940 | } // end: extern "C" |
| 2941 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2942 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2943 | // CXFile Operations. |
| 2944 | //===----------------------------------------------------------------------===// |
| 2945 | |
| 2946 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2947 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2948 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2949 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2950 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2951 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2952 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2956 | if (!SFile) |
| 2957 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2958 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2959 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2960 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2961 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2963 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2964 | if (!tu) |
| 2965 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2966 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2967 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2969 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2970 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2971 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2973 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2974 | if (!tu || !file) |
| 2975 | return 0; |
| 2976 | |
| 2977 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2978 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2979 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2980 | .isFileMultipleIncludeGuarded(FEnt); |
| 2981 | } |
| 2982 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2983 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2984 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2985 | //===----------------------------------------------------------------------===// |
| 2986 | // CXCursor Operations. |
| 2987 | //===----------------------------------------------------------------------===// |
| 2988 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2989 | static Decl *getDeclFromExpr(Stmt *E) { |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2990 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 2991 | return getDeclFromExpr(CE->getSubExpr()); |
| 2992 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2993 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2994 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2995 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2996 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2997 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2998 | return ME->getMemberDecl(); |
| 2999 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3000 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3001 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 3002 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3003 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3004 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 3005 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3006 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3007 | if (!CE->isElidable()) |
| 3008 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3009 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 3010 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3012 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 3013 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 3014 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 3015 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 3016 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3017 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3018 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 3019 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 3020 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3021 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3022 | return 0; |
| 3023 | } |
| 3024 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3025 | static SourceLocation getLocationFromExpr(Expr *E) { |
| 3026 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 3027 | return /*FIXME:*/Msg->getLeftLoc(); |
| 3028 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3029 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 3030 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 3031 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3032 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 3033 | return Member->getMemberLoc(); |
| 3034 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3035 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3036 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3037 | return SizeOfPack->getPackLoc(); |
| 3038 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3039 | return E->getLocStart(); |
| 3040 | } |
| 3041 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3042 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3043 | |
| 3044 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3045 | CXCursorVisitor visitor, |
| 3046 | CXClientData client_data) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3047 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 3048 | getCursorASTUnit(parent)->getMaxPCHLevel(), |
| 3049 | false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3050 | return CursorVis.VisitChildren(parent); |
| 3051 | } |
| 3052 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3053 | #ifndef __has_feature |
| 3054 | #define __has_feature(x) 0 |
| 3055 | #endif |
| 3056 | #if __has_feature(blocks) |
| 3057 | typedef enum CXChildVisitResult |
| 3058 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 3059 | |
| 3060 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3061 | CXClientData client_data) { |
| 3062 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3063 | return block(cursor, parent); |
| 3064 | } |
| 3065 | #else |
| 3066 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 3067 | // define and call the block manually, so the |
| 3068 | typedef struct _CXChildVisitResult |
| 3069 | { |
| 3070 | void *isa; |
| 3071 | int flags; |
| 3072 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3073 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 3074 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3075 | } *CXCursorVisitorBlock; |
| 3076 | |
| 3077 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3078 | CXClientData client_data) { |
| 3079 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3080 | return block->invoke(block, cursor, parent); |
| 3081 | } |
| 3082 | #endif |
| 3083 | |
| 3084 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3085 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 3086 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3087 | return clang_visitChildren(parent, visitWithBlock, block); |
| 3088 | } |
| 3089 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3090 | static CXString getDeclSpelling(Decl *D) { |
| 3091 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3092 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3093 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3094 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3095 | return createCXString(Property->getIdentifier()->getName()); |
| 3096 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3097 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3100 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3101 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3103 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 3104 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 3105 | // and returns different names. NamedDecl returns the class name and |
| 3106 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3107 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3108 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3109 | if (isa<UsingDirectiveDecl>(D)) |
| 3110 | return createCXString(""); |
| 3111 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 3112 | llvm::SmallString<1024> S; |
| 3113 | llvm::raw_svector_ostream os(S); |
| 3114 | ND->printName(os); |
| 3115 | |
| 3116 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3117 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3118 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3119 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3120 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3121 | return clang_getTranslationUnitSpelling( |
| 3122 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3123 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3124 | if (clang_isReference(C.kind)) { |
| 3125 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3126 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 3127 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3128 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3129 | } |
| 3130 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 3131 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3132 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3133 | } |
| 3134 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 3135 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3136 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3137 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3138 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3139 | case CXCursor_CXXBaseSpecifier: { |
| 3140 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 3141 | return createCXString(B->getType().getAsString()); |
| 3142 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3143 | case CXCursor_TypeRef: { |
| 3144 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 3145 | assert(Type && "Missing type decl"); |
| 3146 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3147 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 3148 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3149 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3150 | case CXCursor_TemplateRef: { |
| 3151 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3152 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3153 | |
| 3154 | return createCXString(Template->getNameAsString()); |
| 3155 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3156 | |
| 3157 | case CXCursor_NamespaceRef: { |
| 3158 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 3159 | assert(NS && "Missing namespace decl"); |
| 3160 | |
| 3161 | return createCXString(NS->getNameAsString()); |
| 3162 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3163 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3164 | case CXCursor_MemberRef: { |
| 3165 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3166 | assert(Field && "Missing member decl"); |
| 3167 | |
| 3168 | return createCXString(Field->getNameAsString()); |
| 3169 | } |
| 3170 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3171 | case CXCursor_LabelRef: { |
| 3172 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3173 | assert(Label && "Missing label"); |
| 3174 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3175 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3178 | case CXCursor_OverloadedDeclRef: { |
| 3179 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3180 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3181 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3182 | return createCXString(ND->getNameAsString()); |
| 3183 | return createCXString(""); |
| 3184 | } |
| 3185 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3186 | return createCXString(E->getName().getAsString()); |
| 3187 | OverloadedTemplateStorage *Ovl |
| 3188 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3189 | if (Ovl->size() == 0) |
| 3190 | return createCXString(""); |
| 3191 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3192 | } |
| 3193 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3194 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3195 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3196 | } |
| 3197 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3198 | |
| 3199 | if (clang_isExpression(C.kind)) { |
| 3200 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3201 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3202 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3203 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3206 | if (clang_isStatement(C.kind)) { |
| 3207 | Stmt *S = getCursorStmt(C); |
| 3208 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3209 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3210 | |
| 3211 | return createCXString(""); |
| 3212 | } |
| 3213 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3214 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3215 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3216 | ->getNameStart()); |
| 3217 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3218 | if (C.kind == CXCursor_MacroDefinition) |
| 3219 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3220 | ->getNameStart()); |
| 3221 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3222 | if (C.kind == CXCursor_InclusionDirective) |
| 3223 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3224 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3225 | if (clang_isDeclaration(C.kind)) |
| 3226 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3227 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3228 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3231 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3232 | if (!clang_isDeclaration(C.kind)) |
| 3233 | return clang_getCursorSpelling(C); |
| 3234 | |
| 3235 | Decl *D = getCursorDecl(C); |
| 3236 | if (!D) |
| 3237 | return createCXString(""); |
| 3238 | |
| 3239 | PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy; |
| 3240 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3241 | D = FunTmpl->getTemplatedDecl(); |
| 3242 | |
| 3243 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 3244 | llvm::SmallString<64> Str; |
| 3245 | llvm::raw_svector_ostream OS(Str); |
| 3246 | OS << Function->getNameAsString(); |
| 3247 | if (Function->getPrimaryTemplate()) |
| 3248 | OS << "<>"; |
| 3249 | OS << "("; |
| 3250 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3251 | if (I) |
| 3252 | OS << ", "; |
| 3253 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3254 | } |
| 3255 | |
| 3256 | if (Function->isVariadic()) { |
| 3257 | if (Function->getNumParams()) |
| 3258 | OS << ", "; |
| 3259 | OS << "..."; |
| 3260 | } |
| 3261 | OS << ")"; |
| 3262 | return createCXString(OS.str()); |
| 3263 | } |
| 3264 | |
| 3265 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 3266 | llvm::SmallString<64> Str; |
| 3267 | llvm::raw_svector_ostream OS(Str); |
| 3268 | OS << ClassTemplate->getNameAsString(); |
| 3269 | OS << "<"; |
| 3270 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3271 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3272 | if (I) |
| 3273 | OS << ", "; |
| 3274 | |
| 3275 | NamedDecl *Param = Params->getParam(I); |
| 3276 | if (Param->getIdentifier()) { |
| 3277 | OS << Param->getIdentifier()->getName(); |
| 3278 | continue; |
| 3279 | } |
| 3280 | |
| 3281 | // There is no parameter name, which makes this tricky. Try to come up |
| 3282 | // with something useful that isn't too long. |
| 3283 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3284 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3285 | else if (NonTypeTemplateParmDecl *NTTP |
| 3286 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3287 | OS << NTTP->getType().getAsString(Policy); |
| 3288 | else |
| 3289 | OS << "template<...> class"; |
| 3290 | } |
| 3291 | |
| 3292 | OS << ">"; |
| 3293 | return createCXString(OS.str()); |
| 3294 | } |
| 3295 | |
| 3296 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3297 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3298 | // If the type was explicitly written, use that. |
| 3299 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3300 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3301 | |
| 3302 | llvm::SmallString<64> Str; |
| 3303 | llvm::raw_svector_ostream OS(Str); |
| 3304 | OS << ClassSpec->getNameAsString(); |
| 3305 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3306 | ClassSpec->getTemplateArgs().data(), |
| 3307 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3308 | Policy); |
| 3309 | return createCXString(OS.str()); |
| 3310 | } |
| 3311 | |
| 3312 | return clang_getCursorSpelling(C); |
| 3313 | } |
| 3314 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3315 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3316 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3317 | case CXCursor_FunctionDecl: |
| 3318 | return createCXString("FunctionDecl"); |
| 3319 | case CXCursor_TypedefDecl: |
| 3320 | return createCXString("TypedefDecl"); |
| 3321 | case CXCursor_EnumDecl: |
| 3322 | return createCXString("EnumDecl"); |
| 3323 | case CXCursor_EnumConstantDecl: |
| 3324 | return createCXString("EnumConstantDecl"); |
| 3325 | case CXCursor_StructDecl: |
| 3326 | return createCXString("StructDecl"); |
| 3327 | case CXCursor_UnionDecl: |
| 3328 | return createCXString("UnionDecl"); |
| 3329 | case CXCursor_ClassDecl: |
| 3330 | return createCXString("ClassDecl"); |
| 3331 | case CXCursor_FieldDecl: |
| 3332 | return createCXString("FieldDecl"); |
| 3333 | case CXCursor_VarDecl: |
| 3334 | return createCXString("VarDecl"); |
| 3335 | case CXCursor_ParmDecl: |
| 3336 | return createCXString("ParmDecl"); |
| 3337 | case CXCursor_ObjCInterfaceDecl: |
| 3338 | return createCXString("ObjCInterfaceDecl"); |
| 3339 | case CXCursor_ObjCCategoryDecl: |
| 3340 | return createCXString("ObjCCategoryDecl"); |
| 3341 | case CXCursor_ObjCProtocolDecl: |
| 3342 | return createCXString("ObjCProtocolDecl"); |
| 3343 | case CXCursor_ObjCPropertyDecl: |
| 3344 | return createCXString("ObjCPropertyDecl"); |
| 3345 | case CXCursor_ObjCIvarDecl: |
| 3346 | return createCXString("ObjCIvarDecl"); |
| 3347 | case CXCursor_ObjCInstanceMethodDecl: |
| 3348 | return createCXString("ObjCInstanceMethodDecl"); |
| 3349 | case CXCursor_ObjCClassMethodDecl: |
| 3350 | return createCXString("ObjCClassMethodDecl"); |
| 3351 | case CXCursor_ObjCImplementationDecl: |
| 3352 | return createCXString("ObjCImplementationDecl"); |
| 3353 | case CXCursor_ObjCCategoryImplDecl: |
| 3354 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3355 | case CXCursor_CXXMethod: |
| 3356 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3357 | case CXCursor_UnexposedDecl: |
| 3358 | return createCXString("UnexposedDecl"); |
| 3359 | case CXCursor_ObjCSuperClassRef: |
| 3360 | return createCXString("ObjCSuperClassRef"); |
| 3361 | case CXCursor_ObjCProtocolRef: |
| 3362 | return createCXString("ObjCProtocolRef"); |
| 3363 | case CXCursor_ObjCClassRef: |
| 3364 | return createCXString("ObjCClassRef"); |
| 3365 | case CXCursor_TypeRef: |
| 3366 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3367 | case CXCursor_TemplateRef: |
| 3368 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3369 | case CXCursor_NamespaceRef: |
| 3370 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3371 | case CXCursor_MemberRef: |
| 3372 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3373 | case CXCursor_LabelRef: |
| 3374 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3375 | case CXCursor_OverloadedDeclRef: |
| 3376 | return createCXString("OverloadedDeclRef"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3377 | case CXCursor_UnexposedExpr: |
| 3378 | return createCXString("UnexposedExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3379 | case CXCursor_BlockExpr: |
| 3380 | return createCXString("BlockExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3381 | case CXCursor_DeclRefExpr: |
| 3382 | return createCXString("DeclRefExpr"); |
| 3383 | case CXCursor_MemberRefExpr: |
| 3384 | return createCXString("MemberRefExpr"); |
| 3385 | case CXCursor_CallExpr: |
| 3386 | return createCXString("CallExpr"); |
| 3387 | case CXCursor_ObjCMessageExpr: |
| 3388 | return createCXString("ObjCMessageExpr"); |
| 3389 | case CXCursor_UnexposedStmt: |
| 3390 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3391 | case CXCursor_LabelStmt: |
| 3392 | return createCXString("LabelStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3393 | case CXCursor_InvalidFile: |
| 3394 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3395 | case CXCursor_InvalidCode: |
| 3396 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3397 | case CXCursor_NoDeclFound: |
| 3398 | return createCXString("NoDeclFound"); |
| 3399 | case CXCursor_NotImplemented: |
| 3400 | return createCXString("NotImplemented"); |
| 3401 | case CXCursor_TranslationUnit: |
| 3402 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3403 | case CXCursor_UnexposedAttr: |
| 3404 | return createCXString("UnexposedAttr"); |
| 3405 | case CXCursor_IBActionAttr: |
| 3406 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3407 | case CXCursor_IBOutletAttr: |
| 3408 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3409 | case CXCursor_IBOutletCollectionAttr: |
| 3410 | return createCXString("attribute(iboutletcollection)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3411 | case CXCursor_PreprocessingDirective: |
| 3412 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3413 | case CXCursor_MacroDefinition: |
| 3414 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3415 | case CXCursor_MacroExpansion: |
| 3416 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3417 | case CXCursor_InclusionDirective: |
| 3418 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3419 | case CXCursor_Namespace: |
| 3420 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3421 | case CXCursor_LinkageSpec: |
| 3422 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3423 | case CXCursor_CXXBaseSpecifier: |
| 3424 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3425 | case CXCursor_Constructor: |
| 3426 | return createCXString("CXXConstructor"); |
| 3427 | case CXCursor_Destructor: |
| 3428 | return createCXString("CXXDestructor"); |
| 3429 | case CXCursor_ConversionFunction: |
| 3430 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3431 | case CXCursor_TemplateTypeParameter: |
| 3432 | return createCXString("TemplateTypeParameter"); |
| 3433 | case CXCursor_NonTypeTemplateParameter: |
| 3434 | return createCXString("NonTypeTemplateParameter"); |
| 3435 | case CXCursor_TemplateTemplateParameter: |
| 3436 | return createCXString("TemplateTemplateParameter"); |
| 3437 | case CXCursor_FunctionTemplate: |
| 3438 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3439 | case CXCursor_ClassTemplate: |
| 3440 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3441 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3442 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3443 | case CXCursor_NamespaceAlias: |
| 3444 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3445 | case CXCursor_UsingDirective: |
| 3446 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3447 | case CXCursor_UsingDeclaration: |
| 3448 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3449 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3450 | return createCXString("TypeAliasDecl"); |
| 3451 | case CXCursor_ObjCSynthesizeDecl: |
| 3452 | return createCXString("ObjCSynthesizeDecl"); |
| 3453 | case CXCursor_ObjCDynamicDecl: |
| 3454 | return createCXString("ObjCDynamicDecl"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3455 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3456 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3457 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3458 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3459 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3460 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3461 | struct GetCursorData { |
| 3462 | SourceLocation TokenBeginLoc; |
| 3463 | CXCursor &BestCursor; |
| 3464 | |
| 3465 | GetCursorData(SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3466 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { } |
| 3467 | }; |
| 3468 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3469 | enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3470 | CXCursor parent, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3471 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3472 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3473 | CXCursor *BestCursor = &Data->BestCursor; |
| 3474 | |
| 3475 | if (clang_isExpression(cursor.kind) && |
| 3476 | clang_isDeclaration(BestCursor->kind)) { |
| 3477 | Decl *D = getCursorDecl(*BestCursor); |
| 3478 | |
| 3479 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3480 | // when the expression source range overlaps the declaration range. |
| 3481 | // This can happen for C++ constructor expressions whose range generally |
| 3482 | // include the variable declaration, e.g.: |
| 3483 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3484 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3485 | D->getLocation() == Data->TokenBeginLoc) |
| 3486 | return CXChildVisit_Break; |
| 3487 | } |
| 3488 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3489 | // If our current best cursor is the construction of a temporary object, |
| 3490 | // don't replace that cursor with a type reference, because we want |
| 3491 | // clang_getCursor() to point at the constructor. |
| 3492 | if (clang_isExpression(BestCursor->kind) && |
| 3493 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 3494 | cursor.kind == CXCursor_TypeRef) |
| 3495 | return CXChildVisit_Recurse; |
| 3496 | |
Douglas Gregor | 85fe156 | 2010-12-10 07:23:11 +0000 | [diff] [blame] | 3497 | // Don't override a preprocessing cursor with another preprocessing |
| 3498 | // cursor; we want the outermost preprocessing cursor. |
| 3499 | if (clang_isPreprocessing(cursor.kind) && |
| 3500 | clang_isPreprocessing(BestCursor->kind)) |
| 3501 | return CXChildVisit_Recurse; |
| 3502 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3503 | *BestCursor = cursor; |
| 3504 | return CXChildVisit_Recurse; |
| 3505 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3507 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3508 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3509 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3510 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3511 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3512 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3513 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3514 | // Translate the given source location to make it point at the beginning of |
| 3515 | // the token under the cursor. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3516 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3517 | |
| 3518 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3519 | // of the following calls. |
| 3520 | if (SLoc.isInvalid()) |
| 3521 | return clang_getNullCursor(); |
| 3522 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3523 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3524 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3525 | CXXUnit->getASTContext().getLangOptions()); |
| 3526 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3527 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3528 | if (SLoc.isValid()) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3529 | // FIXME: Would be great to have a "hint" cursor, then walk from that |
| 3530 | // hint cursor upward until we find a cursor whose source range encloses |
| 3531 | // the region of interest, rather than starting from the translation unit. |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3532 | GetCursorData ResultData(SLoc, Result); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3533 | CXCursor Parent = clang_getTranslationUnitCursor(TU); |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3534 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 3535 | Decl::MaxPCHLevel, true, SourceLocation(SLoc)); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3536 | CursorVis.VisitChildren(Parent); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3537 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3538 | |
| 3539 | if (Logging) { |
| 3540 | CXFile SearchFile; |
| 3541 | unsigned SearchLine, SearchColumn; |
| 3542 | CXFile ResultFile; |
| 3543 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3544 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3545 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3546 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3547 | |
| 3548 | clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 3549 | 0); |
| 3550 | clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3551 | &ResultColumn, 0); |
| 3552 | SearchFileName = clang_getFileName(SearchFile); |
| 3553 | ResultFileName = clang_getFileName(ResultFile); |
| 3554 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3555 | USR = clang_getCursorUSR(Result); |
| 3556 | 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] | 3557 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3558 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3559 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3560 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3561 | clang_disposeString(SearchFileName); |
| 3562 | clang_disposeString(ResultFileName); |
| 3563 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3564 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3565 | |
| 3566 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3567 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3568 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3569 | CXString DefinitionKindSpelling |
| 3570 | = clang_getCursorKindSpelling(Definition.kind); |
| 3571 | CXFile DefinitionFile; |
| 3572 | unsigned DefinitionLine, DefinitionColumn; |
| 3573 | clang_getInstantiationLocation(DefinitionLoc, &DefinitionFile, |
| 3574 | &DefinitionLine, &DefinitionColumn, 0); |
| 3575 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3576 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3577 | clang_getCString(DefinitionKindSpelling), |
| 3578 | clang_getCString(DefinitionFileName), |
| 3579 | DefinitionLine, DefinitionColumn); |
| 3580 | clang_disposeString(DefinitionFileName); |
| 3581 | clang_disposeString(DefinitionKindSpelling); |
| 3582 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3585 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3588 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3589 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3593 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3594 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3595 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3596 | unsigned clang_hashCursor(CXCursor C) { |
| 3597 | unsigned Index = 0; |
| 3598 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3599 | Index = 1; |
| 3600 | |
| 3601 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3602 | std::make_pair(C.kind, C.data[Index])); |
| 3603 | } |
| 3604 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3605 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3606 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3607 | } |
| 3608 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3609 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3610 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3611 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3612 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3613 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3614 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3615 | } |
| 3616 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3617 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3618 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3619 | } |
| 3620 | |
| 3621 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3622 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3623 | } |
| 3624 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3625 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3626 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3627 | } |
| 3628 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3629 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3630 | return K == CXCursor_TranslationUnit; |
| 3631 | } |
| 3632 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3633 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3634 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3635 | } |
| 3636 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3637 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3638 | switch (K) { |
| 3639 | case CXCursor_UnexposedDecl: |
| 3640 | case CXCursor_UnexposedExpr: |
| 3641 | case CXCursor_UnexposedStmt: |
| 3642 | case CXCursor_UnexposedAttr: |
| 3643 | return true; |
| 3644 | default: |
| 3645 | return false; |
| 3646 | } |
| 3647 | } |
| 3648 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3649 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3650 | return C.kind; |
| 3651 | } |
| 3652 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3653 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3654 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3655 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3656 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3657 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3658 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3659 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3662 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3663 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3664 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3665 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3668 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3669 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3670 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3671 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3672 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3673 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3674 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3675 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3676 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3677 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3678 | |
| 3679 | case CXCursor_TemplateRef: { |
| 3680 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3681 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3682 | } |
| 3683 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3684 | case CXCursor_NamespaceRef: { |
| 3685 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3686 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3687 | } |
| 3688 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3689 | case CXCursor_MemberRef: { |
| 3690 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3691 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3692 | } |
| 3693 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3694 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3695 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3696 | if (!BaseSpec) |
| 3697 | return clang_getNullLocation(); |
| 3698 | |
| 3699 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3700 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3701 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3702 | |
| 3703 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3704 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3705 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3706 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3707 | case CXCursor_LabelRef: { |
| 3708 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3709 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3710 | } |
| 3711 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3712 | case CXCursor_OverloadedDeclRef: |
| 3713 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3714 | getCursorOverloadedDeclRef(C).second); |
| 3715 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3716 | default: |
| 3717 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3718 | llvm_unreachable("Missed a reference kind"); |
| 3719 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3720 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3721 | |
| 3722 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3723 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3724 | getLocationFromExpr(getCursorExpr(C))); |
| 3725 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3726 | if (clang_isStatement(C.kind)) |
| 3727 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3728 | getCursorStmt(C)->getLocStart()); |
| 3729 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3730 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3731 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3732 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3733 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3734 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3735 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3736 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3737 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3738 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3739 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (C.kind == CXCursor_MacroDefinition) { |
| 3742 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3743 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3744 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3745 | |
| 3746 | if (C.kind == CXCursor_InclusionDirective) { |
| 3747 | SourceLocation L |
| 3748 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3749 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3750 | } |
| 3751 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3752 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3753 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3755 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3756 | SourceLocation Loc = D->getLocation(); |
| 3757 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 3758 | Loc = Class->getClassLoc(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3759 | // FIXME: Multiple variables declared in a single declaration |
| 3760 | // currently lack the information needed to correctly determine their |
| 3761 | // ranges when accounting for the type-specifier. We use context |
| 3762 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3763 | // and if so, whether it is the first decl. |
| 3764 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3765 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3766 | Loc = VD->getLocation(); |
| 3767 | } |
| 3768 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3769 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3770 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3772 | } // end extern "C" |
| 3773 | |
| 3774 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3775 | if (clang_isReference(C.kind)) { |
| 3776 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3777 | case CXCursor_ObjCSuperClassRef: |
| 3778 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3779 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3780 | case CXCursor_ObjCProtocolRef: |
| 3781 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3783 | case CXCursor_ObjCClassRef: |
| 3784 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3785 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3786 | case CXCursor_TypeRef: |
| 3787 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3788 | |
| 3789 | case CXCursor_TemplateRef: |
| 3790 | return getCursorTemplateRef(C).second; |
| 3791 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3792 | case CXCursor_NamespaceRef: |
| 3793 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3794 | |
| 3795 | case CXCursor_MemberRef: |
| 3796 | return getCursorMemberRef(C).second; |
| 3797 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3798 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3799 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3801 | case CXCursor_LabelRef: |
| 3802 | return getCursorLabelRef(C).second; |
| 3803 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3804 | case CXCursor_OverloadedDeclRef: |
| 3805 | return getCursorOverloadedDeclRef(C).second; |
| 3806 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3807 | default: |
| 3808 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3809 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3812 | |
| 3813 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3814 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3815 | |
| 3816 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3817 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3818 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3819 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3820 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3821 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3822 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3823 | return cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3825 | if (C.kind == CXCursor_MacroDefinition) |
| 3826 | return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3827 | |
| 3828 | if (C.kind == CXCursor_InclusionDirective) |
| 3829 | return cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3830 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3831 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3832 | Decl *D = cxcursor::getCursorDecl(C); |
| 3833 | SourceRange R = D->getSourceRange(); |
| 3834 | // FIXME: Multiple variables declared in a single declaration |
| 3835 | // currently lack the information needed to correctly determine their |
| 3836 | // ranges when accounting for the type-specifier. We use context |
| 3837 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3838 | // and if so, whether it is the first decl. |
| 3839 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3840 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3841 | R.setBegin(VD->getLocation()); |
| 3842 | } |
| 3843 | return R; |
| 3844 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3845 | return SourceRange(); |
| 3846 | } |
| 3847 | |
| 3848 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3849 | /// the decl-specifier-seq for declarations. |
| 3850 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3851 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3852 | Decl *D = cxcursor::getCursorDecl(C); |
| 3853 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3855 | // Adjust the start of the location for declarations preceded by |
| 3856 | // declaration specifiers. |
| 3857 | SourceLocation StartLoc; |
| 3858 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3859 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3860 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3861 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3862 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3863 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3864 | } |
| 3865 | |
| 3866 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 3867 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 3868 | R.setBegin(StartLoc); |
| 3869 | |
| 3870 | // FIXME: Multiple variables declared in a single declaration |
| 3871 | // currently lack the information needed to correctly determine their |
| 3872 | // ranges when accounting for the type-specifier. We use context |
| 3873 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3874 | // and if so, whether it is the first decl. |
| 3875 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3876 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3877 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | return R; |
| 3881 | } |
| 3882 | |
| 3883 | return getRawCursorExtent(C); |
| 3884 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3885 | |
| 3886 | extern "C" { |
| 3887 | |
| 3888 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 3889 | SourceRange R = getRawCursorExtent(C); |
| 3890 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3891 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3893 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3894 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3895 | |
| 3896 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3897 | if (clang_isInvalid(C.kind)) |
| 3898 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3899 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3900 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3901 | if (clang_isDeclaration(C.kind)) { |
| 3902 | Decl *D = getCursorDecl(C); |
| 3903 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3904 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3905 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3906 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3907 | if (ObjCForwardProtocolDecl *Protocols |
| 3908 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3909 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3910 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3911 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3912 | return MakeCXCursor(Property, tu); |
| 3913 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3914 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3917 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3918 | Expr *E = getCursorExpr(C); |
| 3919 | Decl *D = getDeclFromExpr(E); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3920 | if (D) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3921 | return MakeCXCursor(D, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3922 | |
| 3923 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3924 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3926 | return clang_getNullCursor(); |
| 3927 | } |
| 3928 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3929 | if (clang_isStatement(C.kind)) { |
| 3930 | Stmt *S = getCursorStmt(C); |
| 3931 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 3932 | if (LabelDecl *label = Goto->getLabel()) |
| 3933 | if (LabelStmt *labelS = label->getStmt()) |
| 3934 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3935 | |
| 3936 | return clang_getNullCursor(); |
| 3937 | } |
| 3938 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3939 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3940 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3941 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3944 | if (!clang_isReference(C.kind)) |
| 3945 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3946 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3947 | switch (C.kind) { |
| 3948 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3949 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3950 | |
| 3951 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3952 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3953 | |
| 3954 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3955 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3956 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3957 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3958 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3959 | |
| 3960 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3961 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3963 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3964 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3966 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3967 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3968 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3969 | case CXCursor_CXXBaseSpecifier: { |
| 3970 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 3971 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3972 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3973 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3974 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3975 | case CXCursor_LabelRef: |
| 3976 | // FIXME: We end up faking the "parent" declaration here because we |
| 3977 | // don't want to make CXCursor larger. |
| 3978 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3979 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 3980 | .getTranslationUnitDecl(), |
| 3981 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3983 | case CXCursor_OverloadedDeclRef: |
| 3984 | return C; |
| 3985 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3986 | default: |
| 3987 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 3988 | llvm_unreachable("Unhandled reference cursor kind"); |
| 3989 | break; |
| 3990 | } |
| 3991 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3992 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3993 | return clang_getNullCursor(); |
| 3994 | } |
| 3995 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3996 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3997 | if (clang_isInvalid(C.kind)) |
| 3998 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3999 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4000 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4001 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4002 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4003 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4004 | C = clang_getCursorReferenced(C); |
| 4005 | WasReference = true; |
| 4006 | } |
| 4007 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4008 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4009 | return clang_getCursorReferenced(C); |
| 4010 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4011 | if (!clang_isDeclaration(C.kind)) |
| 4012 | return clang_getNullCursor(); |
| 4013 | |
| 4014 | Decl *D = getCursorDecl(C); |
| 4015 | if (!D) |
| 4016 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4017 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4018 | switch (D->getKind()) { |
| 4019 | // Declaration kinds that don't really separate the notions of |
| 4020 | // declaration and definition. |
| 4021 | case Decl::Namespace: |
| 4022 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4023 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4024 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4025 | case Decl::TemplateTypeParm: |
| 4026 | case Decl::EnumConstant: |
| 4027 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4028 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4029 | case Decl::ObjCIvar: |
| 4030 | case Decl::ObjCAtDefsField: |
| 4031 | case Decl::ImplicitParam: |
| 4032 | case Decl::ParmVar: |
| 4033 | case Decl::NonTypeTemplateParm: |
| 4034 | case Decl::TemplateTemplateParm: |
| 4035 | case Decl::ObjCCategoryImpl: |
| 4036 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4037 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4038 | case Decl::LinkageSpec: |
| 4039 | case Decl::ObjCPropertyImpl: |
| 4040 | case Decl::FileScopeAsm: |
| 4041 | case Decl::StaticAssert: |
| 4042 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4043 | case Decl::Label: // FIXME: Is this right?? |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4044 | return C; |
| 4045 | |
| 4046 | // Declaration kinds that don't make any sense here, but are |
| 4047 | // nonetheless harmless. |
| 4048 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4049 | break; |
| 4050 | |
| 4051 | // Declaration kinds for which the definition is not resolvable. |
| 4052 | case Decl::UnresolvedUsingTypename: |
| 4053 | case Decl::UnresolvedUsingValue: |
| 4054 | break; |
| 4055 | |
| 4056 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4057 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4058 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4059 | |
| 4060 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4061 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4062 | |
| 4063 | case Decl::Enum: |
| 4064 | case Decl::Record: |
| 4065 | case Decl::CXXRecord: |
| 4066 | case Decl::ClassTemplateSpecialization: |
| 4067 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4068 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4069 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4070 | return clang_getNullCursor(); |
| 4071 | |
| 4072 | case Decl::Function: |
| 4073 | case Decl::CXXMethod: |
| 4074 | case Decl::CXXConstructor: |
| 4075 | case Decl::CXXDestructor: |
| 4076 | case Decl::CXXConversion: { |
| 4077 | const FunctionDecl *Def = 0; |
| 4078 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4079 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4080 | return clang_getNullCursor(); |
| 4081 | } |
| 4082 | |
| 4083 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4084 | // Ask the variable if it has a definition. |
| 4085 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4086 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4087 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4088 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4089 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4090 | case Decl::FunctionTemplate: { |
| 4091 | const FunctionDecl *Def = 0; |
| 4092 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4093 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4094 | return clang_getNullCursor(); |
| 4095 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4097 | case Decl::ClassTemplate: { |
| 4098 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4099 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4100 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4101 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4102 | return clang_getNullCursor(); |
| 4103 | } |
| 4104 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4105 | case Decl::Using: |
| 4106 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4107 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4108 | |
| 4109 | case Decl::UsingShadow: |
| 4110 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4111 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4112 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4113 | |
| 4114 | case Decl::ObjCMethod: { |
| 4115 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4116 | if (Method->isThisDeclarationADefinition()) |
| 4117 | return C; |
| 4118 | |
| 4119 | // Dig out the method definition in the associated |
| 4120 | // @implementation, if we have it. |
| 4121 | // FIXME: The ASTs should make finding the definition easier. |
| 4122 | if (ObjCInterfaceDecl *Class |
| 4123 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4124 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4125 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4126 | Method->isInstanceMethod())) |
| 4127 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4128 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4129 | |
| 4130 | return clang_getNullCursor(); |
| 4131 | } |
| 4132 | |
| 4133 | case Decl::ObjCCategory: |
| 4134 | if (ObjCCategoryImplDecl *Impl |
| 4135 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4136 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4137 | return clang_getNullCursor(); |
| 4138 | |
| 4139 | case Decl::ObjCProtocol: |
| 4140 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4141 | return C; |
| 4142 | return clang_getNullCursor(); |
| 4143 | |
| 4144 | case Decl::ObjCInterface: |
| 4145 | // There are two notions of a "definition" for an Objective-C |
| 4146 | // class: the interface and its implementation. When we resolved a |
| 4147 | // reference to an Objective-C class, produce the @interface as |
| 4148 | // the definition; when we were provided with the interface, |
| 4149 | // produce the @implementation as the definition. |
| 4150 | if (WasReference) { |
| 4151 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4152 | return C; |
| 4153 | } else if (ObjCImplementationDecl *Impl |
| 4154 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4155 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 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 | case Decl::ObjCProperty: |
| 4159 | // FIXME: We don't really know where to find the |
| 4160 | // ObjCPropertyImplDecls that implement this property. |
| 4161 | return clang_getNullCursor(); |
| 4162 | |
| 4163 | case Decl::ObjCCompatibleAlias: |
| 4164 | if (ObjCInterfaceDecl *Class |
| 4165 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4166 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4167 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4168 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4169 | return clang_getNullCursor(); |
| 4170 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4171 | case Decl::ObjCForwardProtocol: |
| 4172 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4173 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4175 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4176 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4177 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4178 | |
| 4179 | case Decl::Friend: |
| 4180 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4181 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4182 | return clang_getNullCursor(); |
| 4183 | |
| 4184 | case Decl::FriendTemplate: |
| 4185 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4186 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4187 | return clang_getNullCursor(); |
| 4188 | } |
| 4189 | |
| 4190 | return clang_getNullCursor(); |
| 4191 | } |
| 4192 | |
| 4193 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4194 | if (!clang_isDeclaration(C.kind)) |
| 4195 | return 0; |
| 4196 | |
| 4197 | return clang_getCursorDefinition(C) == C; |
| 4198 | } |
| 4199 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4200 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4201 | if (!clang_isDeclaration(C.kind)) |
| 4202 | return C; |
| 4203 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4204 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4205 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4206 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4207 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4208 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4209 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4210 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4211 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4212 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4213 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4214 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4215 | |
| 4216 | return C; |
| 4217 | } |
| 4218 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4219 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4220 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4221 | return 0; |
| 4222 | |
| 4223 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4224 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4225 | return E->getNumDecls(); |
| 4226 | |
| 4227 | if (OverloadedTemplateStorage *S |
| 4228 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4229 | return S->size(); |
| 4230 | |
| 4231 | Decl *D = Storage.get<Decl*>(); |
| 4232 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4233 | return Using->shadow_size(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4234 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
| 4235 | return Classes->size(); |
| 4236 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4237 | return Protocols->protocol_size(); |
| 4238 | |
| 4239 | return 0; |
| 4240 | } |
| 4241 | |
| 4242 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4243 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4244 | return clang_getNullCursor(); |
| 4245 | |
| 4246 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4247 | return clang_getNullCursor(); |
| 4248 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4249 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4250 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4251 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4252 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4253 | |
| 4254 | if (OverloadedTemplateStorage *S |
| 4255 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4256 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4257 | |
| 4258 | Decl *D = Storage.get<Decl*>(); |
| 4259 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4260 | // FIXME: This is, unfortunately, linear time. |
| 4261 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4262 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4263 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4267 | return MakeCXCursor(Classes->begin()[index].getInterface(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4268 | |
| 4269 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4270 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4271 | |
| 4272 | return clang_getNullCursor(); |
| 4273 | } |
| 4274 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4275 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4276 | const char **startBuf, |
| 4277 | const char **endBuf, |
| 4278 | unsigned *startLine, |
| 4279 | unsigned *startColumn, |
| 4280 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4281 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4282 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4283 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4284 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4285 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4286 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4287 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4288 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4289 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4290 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4291 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4292 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4293 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4294 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4296 | |
| 4297 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4298 | unsigned PieceIndex) { |
| 4299 | RefNamePieces Pieces; |
| 4300 | |
| 4301 | switch (C.kind) { |
| 4302 | case CXCursor_MemberRefExpr: |
| 4303 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4304 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4305 | E->getQualifierLoc().getSourceRange()); |
| 4306 | break; |
| 4307 | |
| 4308 | case CXCursor_DeclRefExpr: |
| 4309 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4310 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4311 | E->getQualifierLoc().getSourceRange(), |
| 4312 | E->getExplicitTemplateArgsOpt()); |
| 4313 | break; |
| 4314 | |
| 4315 | case CXCursor_CallExpr: |
| 4316 | if (CXXOperatorCallExpr *OCE = |
| 4317 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4318 | Expr *Callee = OCE->getCallee(); |
| 4319 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4320 | Callee = ICE->getSubExpr(); |
| 4321 | |
| 4322 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4323 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4324 | DRE->getQualifierLoc().getSourceRange()); |
| 4325 | } |
| 4326 | break; |
| 4327 | |
| 4328 | default: |
| 4329 | break; |
| 4330 | } |
| 4331 | |
| 4332 | if (Pieces.empty()) { |
| 4333 | if (PieceIndex == 0) |
| 4334 | return clang_getCursorExtent(C); |
| 4335 | } else if (PieceIndex < Pieces.size()) { |
| 4336 | SourceRange R = Pieces[PieceIndex]; |
| 4337 | if (R.isValid()) |
| 4338 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4339 | } |
| 4340 | |
| 4341 | return clang_getNullRange(); |
| 4342 | } |
| 4343 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4344 | void clang_enableStackTraces(void) { |
| 4345 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4346 | } |
| 4347 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4348 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4349 | unsigned stack_size) { |
| 4350 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4351 | } |
| 4352 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4353 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4354 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4355 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4356 | // Token-based Operations. |
| 4357 | //===----------------------------------------------------------------------===// |
| 4358 | |
| 4359 | /* CXToken layout: |
| 4360 | * int_data[0]: a CXTokenKind |
| 4361 | * int_data[1]: starting token location |
| 4362 | * int_data[2]: token length |
| 4363 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4364 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4365 | * otherwise unused. |
| 4366 | */ |
| 4367 | extern "C" { |
| 4368 | |
| 4369 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4370 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4371 | } |
| 4372 | |
| 4373 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4374 | switch (clang_getTokenKind(CXTok)) { |
| 4375 | case CXToken_Identifier: |
| 4376 | case CXToken_Keyword: |
| 4377 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4378 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4379 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4380 | |
| 4381 | case CXToken_Literal: { |
| 4382 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4383 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4384 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4385 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4387 | case CXToken_Punctuation: |
| 4388 | case CXToken_Comment: |
| 4389 | break; |
| 4390 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4391 | |
| 4392 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4393 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4394 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4395 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4396 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4397 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4398 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4399 | std::pair<FileID, unsigned> LocInfo |
| 4400 | = CXXUnit->getSourceManager().getDecomposedLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4401 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4402 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4403 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4404 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4405 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4406 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4407 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4408 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4410 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4411 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4412 | if (!CXXUnit) |
| 4413 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4415 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4416 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4417 | } |
| 4418 | |
| 4419 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4420 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4421 | if (!CXXUnit) |
| 4422 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4423 | |
| 4424 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4425 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4426 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4428 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4429 | CXToken **Tokens, unsigned *NumTokens) { |
| 4430 | if (Tokens) |
| 4431 | *Tokens = 0; |
| 4432 | if (NumTokens) |
| 4433 | *NumTokens = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4434 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4435 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4436 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4437 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4438 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 4439 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4440 | |
Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 4441 | SourceRange R = cxloc::translateCXSourceRange(Range); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4442 | if (R.isInvalid()) |
| 4443 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4444 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4445 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4446 | std::pair<FileID, unsigned> BeginLocInfo |
| 4447 | = SourceMgr.getDecomposedLoc(R.getBegin()); |
| 4448 | std::pair<FileID, unsigned> EndLocInfo |
| 4449 | = SourceMgr.getDecomposedLoc(R.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4450 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4451 | // Cannot tokenize across files. |
| 4452 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4453 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4454 | |
| 4455 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4456 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4457 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4458 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4459 | if (Invalid) |
| 4460 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4461 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4462 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4463 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4464 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4465 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4467 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4468 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4469 | SmallVector<CXToken, 32> CXTokens; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4470 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4471 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4472 | do { |
| 4473 | // Lex the next token |
| 4474 | Lex.LexFromRawLexer(Tok); |
| 4475 | if (Tok.is(tok::eof)) |
| 4476 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4478 | // Initialize the CXToken. |
| 4479 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4481 | // - Common fields |
| 4482 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4483 | CXTok.int_data[2] = Tok.getLength(); |
| 4484 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4486 | // - Kind-specific fields |
| 4487 | if (Tok.isLiteral()) { |
| 4488 | CXTok.int_data[0] = CXToken_Literal; |
| 4489 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4490 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4491 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4492 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4493 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4494 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4495 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4496 | CXTok.int_data[0] = CXToken_Keyword; |
| 4497 | } |
| 4498 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4499 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4500 | ? CXToken_Identifier |
| 4501 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4502 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4503 | CXTok.ptr_data = II; |
| 4504 | } else if (Tok.is(tok::comment)) { |
| 4505 | CXTok.int_data[0] = CXToken_Comment; |
| 4506 | CXTok.ptr_data = 0; |
| 4507 | } else { |
| 4508 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4509 | CXTok.ptr_data = 0; |
| 4510 | } |
| 4511 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4512 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4513 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4514 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4515 | if (CXTokens.empty()) |
| 4516 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4517 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4518 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4519 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4520 | *NumTokens = CXTokens.size(); |
| 4521 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4522 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4523 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4524 | CXToken *Tokens, unsigned NumTokens) { |
| 4525 | free(Tokens); |
| 4526 | } |
| 4527 | |
| 4528 | } // end: extern "C" |
| 4529 | |
| 4530 | //===----------------------------------------------------------------------===// |
| 4531 | // Token annotation APIs. |
| 4532 | //===----------------------------------------------------------------------===// |
| 4533 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4534 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4535 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4536 | CXCursor parent, |
| 4537 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4538 | namespace { |
| 4539 | class AnnotateTokensWorker { |
| 4540 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4541 | CXToken *Tokens; |
| 4542 | CXCursor *Cursors; |
| 4543 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4544 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4545 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4546 | CursorVisitor AnnotateVis; |
| 4547 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4548 | bool HasContextSensitiveKeywords; |
| 4549 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4550 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4551 | unsigned NextToken() const { return TokIdx; } |
| 4552 | void AdvanceToken() { ++TokIdx; } |
| 4553 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4554 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4555 | } |
| 4556 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4557 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4558 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4559 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4560 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4561 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4562 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4563 | AnnotateVis(tu, |
| 4564 | AnnotateTokensVisitor, this, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 4565 | Decl::MaxPCHLevel, true, RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4566 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4567 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4568 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4569 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4570 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4571 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4572 | void AnnotateTokens() { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4573 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4574 | } |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4575 | |
| 4576 | /// \brief Determine whether the annotator saw any cursors that have |
| 4577 | /// context-sensitive keywords. |
| 4578 | bool hasContextSensitiveKeywords() const { |
| 4579 | return HasContextSensitiveKeywords; |
| 4580 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4581 | }; |
| 4582 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4583 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4584 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 4585 | // Walk the AST within the region of interest, annotating tokens |
| 4586 | // along the way. |
| 4587 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4588 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4589 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4590 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4591 | if (Pos != Annotated.end() && |
| 4592 | (clang_isInvalid(Cursors[I].kind) || |
| 4593 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4594 | Cursors[I] = Pos->second; |
| 4595 | } |
| 4596 | |
| 4597 | // Finish up annotating any tokens left. |
| 4598 | if (!MoreTokens()) |
| 4599 | return; |
| 4600 | |
| 4601 | const CXCursor &C = clang_getNullCursor(); |
| 4602 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 4603 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4604 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
| 4607 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4608 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4609 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4610 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4611 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4612 | if (cursorRange.isInvalid()) |
| 4613 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4614 | |
| 4615 | if (!HasContextSensitiveKeywords) { |
| 4616 | // Objective-C properties can have context-sensitive keywords. |
| 4617 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4618 | if (ObjCPropertyDecl *Property |
| 4619 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4620 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4621 | } |
| 4622 | // Objective-C methods can have context-sensitive keywords. |
| 4623 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4624 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4625 | if (ObjCMethodDecl *Method |
| 4626 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4627 | if (Method->getObjCDeclQualifier()) |
| 4628 | HasContextSensitiveKeywords = true; |
| 4629 | else { |
| 4630 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4631 | PEnd = Method->param_end(); |
| 4632 | P != PEnd; ++P) { |
| 4633 | if ((*P)->getObjCDeclQualifier()) { |
| 4634 | HasContextSensitiveKeywords = true; |
| 4635 | break; |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | } |
| 4641 | // C++ methods can have context-sensitive keywords. |
| 4642 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4643 | if (CXXMethodDecl *Method |
| 4644 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4645 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4646 | HasContextSensitiveKeywords = true; |
| 4647 | } |
| 4648 | } |
| 4649 | // C++ classes can have context-sensitive keywords. |
| 4650 | else if (cursor.kind == CXCursor_StructDecl || |
| 4651 | cursor.kind == CXCursor_ClassDecl || |
| 4652 | cursor.kind == CXCursor_ClassTemplate || |
| 4653 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4654 | if (Decl *D = getCursorDecl(cursor)) |
| 4655 | if (D->hasAttr<FinalAttr>()) |
| 4656 | HasContextSensitiveKeywords = true; |
| 4657 | } |
| 4658 | } |
| 4659 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4660 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4661 | // For macro expansions, just note where the beginning of the macro |
| 4662 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4663 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4664 | Annotated[Loc.int_data] = cursor; |
| 4665 | return CXChildVisit_Recurse; |
| 4666 | } |
| 4667 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4668 | // Items in the preprocessing record are kept separate from items in |
| 4669 | // declarations, so we keep a separate token index. |
| 4670 | unsigned SavedTokIdx = TokIdx; |
| 4671 | TokIdx = PreprocessingTokIdx; |
| 4672 | |
| 4673 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4674 | // entry. |
| 4675 | while (MoreTokens()) { |
| 4676 | const unsigned I = NextToken(); |
| 4677 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4678 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4679 | case RangeBefore: |
| 4680 | AdvanceToken(); |
| 4681 | continue; |
| 4682 | case RangeAfter: |
| 4683 | case RangeOverlap: |
| 4684 | break; |
| 4685 | } |
| 4686 | break; |
| 4687 | } |
| 4688 | |
| 4689 | // Look at all of the tokens within this range. |
| 4690 | while (MoreTokens()) { |
| 4691 | const unsigned I = NextToken(); |
| 4692 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4693 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4694 | case RangeBefore: |
| 4695 | assert(0 && "Infeasible"); |
| 4696 | case RangeAfter: |
| 4697 | break; |
| 4698 | case RangeOverlap: |
| 4699 | Cursors[I] = cursor; |
| 4700 | AdvanceToken(); |
| 4701 | continue; |
| 4702 | } |
| 4703 | break; |
| 4704 | } |
| 4705 | |
| 4706 | // Save the preprocessing token index; restore the non-preprocessing |
| 4707 | // token index. |
| 4708 | PreprocessingTokIdx = TokIdx; |
| 4709 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4710 | return CXChildVisit_Recurse; |
| 4711 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4712 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4713 | if (cursorRange.isInvalid()) |
| 4714 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4715 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4716 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4717 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4718 | // Adjust the annotated range based specific declarations. |
| 4719 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4720 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4721 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 4722 | // Don't visit synthesized ObjC methods, since they have no syntatic |
| 4723 | // representation in the source. |
| 4724 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 4725 | if (MD->isSynthesized()) |
| 4726 | return CXChildVisit_Continue; |
| 4727 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4728 | |
| 4729 | SourceLocation StartLoc; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4730 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4731 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4732 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4733 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4734 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4735 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4736 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4737 | |
| 4738 | if (StartLoc.isValid() && L.isValid() && |
| 4739 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4740 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4741 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4742 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4743 | // If the location of the cursor occurs within a macro instantiation, record |
| 4744 | // the spelling location of the cursor in our annotation map. We can then |
| 4745 | // paper over the token labelings during a post-processing step to try and |
| 4746 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4747 | // instantiation. |
| 4748 | if (L.isMacroID()) { |
| 4749 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4750 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4751 | // directive. Here we assume that the default construction of CXCursor |
| 4752 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4753 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4754 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4755 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4756 | if (!clang_isPreprocessing(oldC.kind)) |
| 4757 | oldC = cursor; |
| 4758 | } |
| 4759 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4760 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4761 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4762 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4763 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4764 | |
| 4765 | while (MoreTokens()) { |
| 4766 | const unsigned I = NextToken(); |
| 4767 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4768 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4769 | case RangeBefore: |
| 4770 | Cursors[I] = updateC; |
| 4771 | AdvanceToken(); |
| 4772 | continue; |
| 4773 | case RangeAfter: |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4774 | case RangeOverlap: |
| 4775 | break; |
| 4776 | } |
| 4777 | break; |
| 4778 | } |
| 4779 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4780 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4781 | // variable declaration that it belongs to. |
| 4782 | // This can happen for C++ constructor expressions whose range generally |
| 4783 | // include the variable declaration, e.g.: |
| 4784 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4785 | if (clang_isExpression(cursorK)) { |
| 4786 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4787 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4788 | const unsigned I = NextToken(); |
| 4789 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4790 | E->getLocStart() == D->getLocation() && |
| 4791 | E->getLocStart() == GetTokenLoc(I)) { |
| 4792 | Cursors[I] = updateC; |
| 4793 | AdvanceToken(); |
| 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4798 | // Visit children to get their cursor information. |
| 4799 | const unsigned BeforeChildren = NextToken(); |
| 4800 | VisitChildren(cursor); |
| 4801 | const unsigned AfterChildren = NextToken(); |
| 4802 | |
| 4803 | // Adjust 'Last' to the last token within the extent of the cursor. |
| 4804 | while (MoreTokens()) { |
| 4805 | const unsigned I = NextToken(); |
| 4806 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4807 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4808 | case RangeBefore: |
| 4809 | assert(0 && "Infeasible"); |
| 4810 | case RangeAfter: |
| 4811 | break; |
| 4812 | case RangeOverlap: |
| 4813 | Cursors[I] = updateC; |
| 4814 | AdvanceToken(); |
| 4815 | continue; |
| 4816 | } |
| 4817 | break; |
| 4818 | } |
| 4819 | const unsigned Last = NextToken(); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4820 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4821 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 4822 | // capture by the child cursors. |
| 4823 | |
| 4824 | // For AST elements within macros, rely on a post-annotate pass to |
| 4825 | // to correctly annotate the tokens with cursors. Otherwise we can |
| 4826 | // get confusing results of having tokens that map to cursors that really |
| 4827 | // are expanded by an instantiation. |
| 4828 | if (L.isMacroID()) |
| 4829 | cursor = clang_getNullCursor(); |
| 4830 | |
| 4831 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 4832 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4833 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4834 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4835 | Cursors[I] = cursor; |
| 4836 | } |
| 4837 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 4838 | // but the child cursors. |
| 4839 | for (unsigned I = AfterChildren; I != Last; ++I) |
| 4840 | Cursors[I] = cursor; |
| 4841 | |
| 4842 | TokIdx = Last; |
| 4843 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4846 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4847 | CXCursor parent, |
| 4848 | CXClientData client_data) { |
| 4849 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 4850 | } |
| 4851 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4852 | namespace { |
| 4853 | struct clang_annotateTokens_Data { |
| 4854 | CXTranslationUnit TU; |
| 4855 | ASTUnit *CXXUnit; |
| 4856 | CXToken *Tokens; |
| 4857 | unsigned NumTokens; |
| 4858 | CXCursor *Cursors; |
| 4859 | }; |
| 4860 | } |
| 4861 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4862 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4863 | static void clang_annotateTokensImpl(void *UserData) { |
| 4864 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 4865 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 4866 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 4867 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 4868 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 4869 | |
| 4870 | // Determine the region of interest, which contains all of the tokens. |
| 4871 | SourceRange RegionOfInterest; |
| 4872 | RegionOfInterest.setBegin( |
| 4873 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 4874 | RegionOfInterest.setEnd( |
| 4875 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 4876 | Tokens[NumTokens-1]))); |
| 4877 | |
| 4878 | // A mapping from the source locations found when re-lexing or traversing the |
| 4879 | // region of interest to the corresponding cursors. |
| 4880 | AnnotateTokensData Annotated; |
| 4881 | |
| 4882 | // Relex the tokens within the source range to look for preprocessing |
| 4883 | // directives. |
| 4884 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4885 | std::pair<FileID, unsigned> BeginLocInfo |
| 4886 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 4887 | std::pair<FileID, unsigned> EndLocInfo |
| 4888 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 4889 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4890 | StringRef Buffer; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4891 | bool Invalid = false; |
| 4892 | if (BeginLocInfo.first == EndLocInfo.first && |
| 4893 | ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && |
| 4894 | !Invalid) { |
| 4895 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4896 | CXXUnit->getASTContext().getLangOptions(), |
| 4897 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 4898 | Buffer.end()); |
| 4899 | Lex.SetCommentRetentionState(true); |
| 4900 | |
| 4901 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 4902 | // entering #includes or expanding macros. |
| 4903 | while (true) { |
| 4904 | Token Tok; |
| 4905 | Lex.LexFromRawLexer(Tok); |
| 4906 | |
| 4907 | reprocess: |
| 4908 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 4909 | // We have found a preprocessing directive. Gobble it up so that we |
| 4910 | // don't see it while preprocessing these tokens later, but keep track |
| 4911 | // of all of the token locations inside this preprocessing directive so |
| 4912 | // that we can annotate them appropriately. |
| 4913 | // |
| 4914 | // FIXME: Some simple tests here could identify macro definitions and |
| 4915 | // #undefs, to provide specific cursor kinds for those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4916 | SmallVector<SourceLocation, 32> Locations; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4917 | do { |
| 4918 | Locations.push_back(Tok.getLocation()); |
| 4919 | Lex.LexFromRawLexer(Tok); |
| 4920 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 4921 | |
| 4922 | using namespace cxcursor; |
| 4923 | CXCursor Cursor |
| 4924 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 4925 | Locations.back()), |
| 4926 | TU); |
| 4927 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 4928 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 4929 | } |
| 4930 | |
| 4931 | if (Tok.isAtStartOfLine()) |
| 4932 | goto reprocess; |
| 4933 | |
| 4934 | continue; |
| 4935 | } |
| 4936 | |
| 4937 | if (Tok.is(tok::eof)) |
| 4938 | break; |
| 4939 | } |
| 4940 | } |
| 4941 | |
| 4942 | // Annotate all of the source locations in the region of interest that map to |
| 4943 | // a specific cursor. |
| 4944 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 4945 | TU, RegionOfInterest); |
| 4946 | |
| 4947 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 4948 | // algorithm uses a large stack frame than the non-data recursive version, |
| 4949 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 4950 | // algorithm back into a traditional recursion by explicitly calling |
| 4951 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 4952 | W.AnnotateTokens(); |
| 4953 | |
| 4954 | // If we ran into any entities that involve context-sensitive keywords, |
| 4955 | // take another pass through the tokens to mark them as such. |
| 4956 | if (W.hasContextSensitiveKeywords()) { |
| 4957 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 4958 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 4959 | continue; |
| 4960 | |
| 4961 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 4962 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 4963 | if (ObjCPropertyDecl *Property |
| 4964 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 4965 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 4966 | llvm::StringSwitch<bool>(II->getName()) |
| 4967 | .Case("readonly", true) |
| 4968 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4969 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4970 | .Case("readwrite", true) |
| 4971 | .Case("retain", true) |
| 4972 | .Case("copy", true) |
| 4973 | .Case("nonatomic", true) |
| 4974 | .Case("atomic", true) |
| 4975 | .Case("getter", true) |
| 4976 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4977 | .Case("strong", true) |
| 4978 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4979 | .Default(false)) |
| 4980 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 4981 | } |
| 4982 | continue; |
| 4983 | } |
| 4984 | |
| 4985 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 4986 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 4987 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 4988 | if (llvm::StringSwitch<bool>(II->getName()) |
| 4989 | .Case("in", true) |
| 4990 | .Case("out", true) |
| 4991 | .Case("inout", true) |
| 4992 | .Case("oneway", true) |
| 4993 | .Case("bycopy", true) |
| 4994 | .Case("byref", true) |
| 4995 | .Default(false)) |
| 4996 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 4997 | continue; |
| 4998 | } |
| 4999 | |
| 5000 | if (Cursors[I].kind == CXCursor_CXXMethod) { |
| 5001 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5002 | if (CXXMethodDecl *Method |
| 5003 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(Cursors[I]))) { |
| 5004 | if ((Method->hasAttr<FinalAttr>() || |
| 5005 | Method->hasAttr<OverrideAttr>()) && |
| 5006 | Method->getLocation().getRawEncoding() != Tokens[I].int_data[1] && |
| 5007 | llvm::StringSwitch<bool>(II->getName()) |
| 5008 | .Case("final", true) |
| 5009 | .Case("override", true) |
| 5010 | .Default(false)) |
| 5011 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5012 | } |
| 5013 | continue; |
| 5014 | } |
| 5015 | |
| 5016 | if (Cursors[I].kind == CXCursor_ClassDecl || |
| 5017 | Cursors[I].kind == CXCursor_StructDecl || |
| 5018 | Cursors[I].kind == CXCursor_ClassTemplate) { |
| 5019 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5020 | if (II->getName() == "final") { |
| 5021 | // We have to be careful with 'final', since it could be the name |
| 5022 | // of a member class rather than the context-sensitive keyword. |
| 5023 | // So, check whether the cursor associated with this |
| 5024 | Decl *D = getCursorDecl(Cursors[I]); |
| 5025 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(D)) { |
| 5026 | if ((Record->hasAttr<FinalAttr>()) && |
| 5027 | Record->getIdentifier() != II) |
| 5028 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5029 | } else if (ClassTemplateDecl *ClassTemplate |
| 5030 | = dyn_cast_or_null<ClassTemplateDecl>(D)) { |
| 5031 | CXXRecordDecl *Record = ClassTemplate->getTemplatedDecl(); |
| 5032 | if ((Record->hasAttr<FinalAttr>()) && |
| 5033 | Record->getIdentifier() != II) |
| 5034 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5035 | } |
| 5036 | } |
| 5037 | continue; |
| 5038 | } |
| 5039 | } |
| 5040 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5043 | extern "C" { |
| 5044 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5045 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5046 | CXToken *Tokens, unsigned NumTokens, |
| 5047 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5048 | |
| 5049 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5050 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5051 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5052 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5053 | CXCursor C = clang_getNullCursor(); |
| 5054 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5055 | Cursors[I] = C; |
| 5056 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5057 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5058 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5059 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5061 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5062 | |
| 5063 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5064 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5065 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5066 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5067 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5068 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5069 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5070 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5071 | } // end: extern "C" |
| 5072 | |
| 5073 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5074 | // Operations for querying linkage of a cursor. |
| 5075 | //===----------------------------------------------------------------------===// |
| 5076 | |
| 5077 | extern "C" { |
| 5078 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5079 | if (!clang_isDeclaration(cursor.kind)) |
| 5080 | return CXLinkage_Invalid; |
| 5081 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5082 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5083 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5084 | switch (ND->getLinkage()) { |
| 5085 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5086 | case InternalLinkage: return CXLinkage_Internal; |
| 5087 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5088 | case ExternalLinkage: return CXLinkage_External; |
| 5089 | }; |
| 5090 | |
| 5091 | return CXLinkage_Invalid; |
| 5092 | } |
| 5093 | } // end: extern "C" |
| 5094 | |
| 5095 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5096 | // Operations for querying language of a cursor. |
| 5097 | //===----------------------------------------------------------------------===// |
| 5098 | |
| 5099 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 5100 | switch (D->getKind()) { |
| 5101 | default: |
| 5102 | break; |
| 5103 | case Decl::ImplicitParam: |
| 5104 | case Decl::ObjCAtDefsField: |
| 5105 | case Decl::ObjCCategory: |
| 5106 | case Decl::ObjCCategoryImpl: |
| 5107 | case Decl::ObjCClass: |
| 5108 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5109 | case Decl::ObjCForwardProtocol: |
| 5110 | case Decl::ObjCImplementation: |
| 5111 | case Decl::ObjCInterface: |
| 5112 | case Decl::ObjCIvar: |
| 5113 | case Decl::ObjCMethod: |
| 5114 | case Decl::ObjCProperty: |
| 5115 | case Decl::ObjCPropertyImpl: |
| 5116 | case Decl::ObjCProtocol: |
| 5117 | return CXLanguage_ObjC; |
| 5118 | case Decl::CXXConstructor: |
| 5119 | case Decl::CXXConversion: |
| 5120 | case Decl::CXXDestructor: |
| 5121 | case Decl::CXXMethod: |
| 5122 | case Decl::CXXRecord: |
| 5123 | case Decl::ClassTemplate: |
| 5124 | case Decl::ClassTemplatePartialSpecialization: |
| 5125 | case Decl::ClassTemplateSpecialization: |
| 5126 | case Decl::Friend: |
| 5127 | case Decl::FriendTemplate: |
| 5128 | case Decl::FunctionTemplate: |
| 5129 | case Decl::LinkageSpec: |
| 5130 | case Decl::Namespace: |
| 5131 | case Decl::NamespaceAlias: |
| 5132 | case Decl::NonTypeTemplateParm: |
| 5133 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5134 | case Decl::TemplateTemplateParm: |
| 5135 | case Decl::TemplateTypeParm: |
| 5136 | case Decl::UnresolvedUsingTypename: |
| 5137 | case Decl::UnresolvedUsingValue: |
| 5138 | case Decl::Using: |
| 5139 | case Decl::UsingDirective: |
| 5140 | case Decl::UsingShadow: |
| 5141 | return CXLanguage_CPlusPlus; |
| 5142 | } |
| 5143 | |
| 5144 | return CXLanguage_C; |
| 5145 | } |
| 5146 | |
| 5147 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5148 | |
| 5149 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5150 | if (clang_isDeclaration(cursor.kind)) |
| 5151 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5152 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5153 | return CXAvailability_Available; |
| 5154 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5155 | switch (D->getAvailability()) { |
| 5156 | case AR_Available: |
| 5157 | case AR_NotYetIntroduced: |
| 5158 | return CXAvailability_Available; |
| 5159 | |
| 5160 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5161 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5162 | |
| 5163 | case AR_Unavailable: |
| 5164 | return CXAvailability_NotAvailable; |
| 5165 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5166 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5168 | return CXAvailability_Available; |
| 5169 | } |
| 5170 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5171 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5172 | if (clang_isDeclaration(cursor.kind)) |
| 5173 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5174 | |
| 5175 | return CXLanguage_Invalid; |
| 5176 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5177 | |
| 5178 | /// \brief If the given cursor is the "templated" declaration |
| 5179 | /// descibing a class or function template, return the class or |
| 5180 | /// function template. |
| 5181 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5182 | if (!D) |
| 5183 | return 0; |
| 5184 | |
| 5185 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5186 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5187 | return FunTmpl; |
| 5188 | |
| 5189 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5190 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5191 | return ClassTmpl; |
| 5192 | |
| 5193 | return D; |
| 5194 | } |
| 5195 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5196 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5197 | if (clang_isDeclaration(cursor.kind)) { |
| 5198 | if (Decl *D = getCursorDecl(cursor)) { |
| 5199 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5200 | if (!DC) |
| 5201 | return clang_getNullCursor(); |
| 5202 | |
| 5203 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5204 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5209 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5210 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | return clang_getNullCursor(); |
| 5214 | } |
| 5215 | |
| 5216 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5217 | if (clang_isDeclaration(cursor.kind)) { |
| 5218 | if (Decl *D = getCursorDecl(cursor)) { |
| 5219 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5220 | if (!DC) |
| 5221 | return clang_getNullCursor(); |
| 5222 | |
| 5223 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5224 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5229 | // statement or expression, so we return nothing. |
| 5230 | return clang_getNullCursor(); |
| 5231 | } |
| 5232 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5233 | static void CollectOverriddenMethods(DeclContext *Ctx, |
| 5234 | ObjCMethodDecl *Method, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5235 | SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5236 | if (!Ctx) |
| 5237 | return; |
| 5238 | |
| 5239 | // If we have a class or category implementation, jump straight to the |
| 5240 | // interface. |
| 5241 | if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) |
| 5242 | return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods); |
| 5243 | |
| 5244 | ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); |
| 5245 | if (!Container) |
| 5246 | return; |
| 5247 | |
| 5248 | // Check whether we have a matching method at this level. |
| 5249 | if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), |
| 5250 | Method->isInstanceMethod())) |
| 5251 | if (Method != Overridden) { |
| 5252 | // We found an override at this level; there is no need to look |
| 5253 | // into other protocols or categories. |
| 5254 | Methods.push_back(Overridden); |
| 5255 | return; |
| 5256 | } |
| 5257 | |
| 5258 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5259 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 5260 | PEnd = Protocol->protocol_end(); |
| 5261 | P != PEnd; ++P) |
| 5262 | CollectOverriddenMethods(*P, Method, Methods); |
| 5263 | } |
| 5264 | |
| 5265 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5266 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 5267 | PEnd = Category->protocol_end(); |
| 5268 | P != PEnd; ++P) |
| 5269 | CollectOverriddenMethods(*P, Method, Methods); |
| 5270 | } |
| 5271 | |
| 5272 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5273 | for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), |
| 5274 | PEnd = Interface->protocol_end(); |
| 5275 | P != PEnd; ++P) |
| 5276 | CollectOverriddenMethods(*P, Method, Methods); |
| 5277 | |
| 5278 | for (ObjCCategoryDecl *Category = Interface->getCategoryList(); |
| 5279 | Category; Category = Category->getNextClassCategory()) |
| 5280 | CollectOverriddenMethods(Category, Method, Methods); |
| 5281 | |
| 5282 | // We only look into the superclass if we haven't found anything yet. |
| 5283 | if (Methods.empty()) |
| 5284 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) |
| 5285 | return CollectOverriddenMethods(Super, Method, Methods); |
| 5286 | } |
| 5287 | } |
| 5288 | |
| 5289 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5290 | CXCursor **overridden, |
| 5291 | unsigned *num_overridden) { |
| 5292 | if (overridden) |
| 5293 | *overridden = 0; |
| 5294 | if (num_overridden) |
| 5295 | *num_overridden = 0; |
| 5296 | if (!overridden || !num_overridden) |
| 5297 | return; |
| 5298 | |
| 5299 | if (!clang_isDeclaration(cursor.kind)) |
| 5300 | return; |
| 5301 | |
| 5302 | Decl *D = getCursorDecl(cursor); |
| 5303 | if (!D) |
| 5304 | return; |
| 5305 | |
| 5306 | // Handle C++ member functions. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5307 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5308 | if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
| 5309 | *num_overridden = CXXMethod->size_overridden_methods(); |
| 5310 | if (!*num_overridden) |
| 5311 | return; |
| 5312 | |
| 5313 | *overridden = new CXCursor [*num_overridden]; |
| 5314 | unsigned I = 0; |
| 5315 | for (CXXMethodDecl::method_iterator |
| 5316 | M = CXXMethod->begin_overridden_methods(), |
| 5317 | MEnd = CXXMethod->end_overridden_methods(); |
| 5318 | M != MEnd; (void)++M, ++I) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5319 | (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5320 | return; |
| 5321 | } |
| 5322 | |
| 5323 | ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 5324 | if (!Method) |
| 5325 | return; |
| 5326 | |
| 5327 | // Handle Objective-C methods. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5328 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5329 | CollectOverriddenMethods(Method->getDeclContext(), Method, Methods); |
| 5330 | |
| 5331 | if (Methods.empty()) |
| 5332 | return; |
| 5333 | |
| 5334 | *num_overridden = Methods.size(); |
| 5335 | *overridden = new CXCursor [Methods.size()]; |
| 5336 | for (unsigned I = 0, N = Methods.size(); I != N; ++I) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5337 | (*overridden)[I] = MakeCXCursor(Methods[I], TU); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5341 | delete [] overridden; |
| 5342 | } |
| 5343 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5344 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5345 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5346 | return 0; |
| 5347 | |
| 5348 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5349 | return (void *)ID->getFile(); |
| 5350 | } |
| 5351 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5352 | } // end: extern "C" |
| 5353 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5354 | |
| 5355 | //===----------------------------------------------------------------------===// |
| 5356 | // C++ AST instrospection. |
| 5357 | //===----------------------------------------------------------------------===// |
| 5358 | |
| 5359 | extern "C" { |
| 5360 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5361 | if (!clang_isDeclaration(C.kind)) |
| 5362 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5363 | |
| 5364 | CXXMethodDecl *Method = 0; |
| 5365 | Decl *D = cxcursor::getCursorDecl(C); |
| 5366 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5367 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5368 | else |
| 5369 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5370 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5371 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5372 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5373 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5374 | if (!clang_isDeclaration(C.kind)) |
| 5375 | return 0; |
| 5376 | |
| 5377 | CXXMethodDecl *Method = 0; |
| 5378 | Decl *D = cxcursor::getCursorDecl(C); |
| 5379 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5380 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5381 | else |
| 5382 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5383 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5384 | } |
| 5385 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5386 | } // end: extern "C" |
| 5387 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5388 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5389 | // Attribute introspection. |
| 5390 | //===----------------------------------------------------------------------===// |
| 5391 | |
| 5392 | extern "C" { |
| 5393 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5394 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5395 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5396 | |
| 5397 | IBOutletCollectionAttr *A = |
| 5398 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5399 | |
Douglas Gregor | 841b238 | 2011-03-06 18:55:32 +0000 | [diff] [blame] | 5400 | return cxtype::MakeCXType(A->getInterFace(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5401 | } |
| 5402 | } // end: extern "C" |
| 5403 | |
| 5404 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5405 | // Inspecting memory usage. |
| 5406 | //===----------------------------------------------------------------------===// |
| 5407 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5408 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5409 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5410 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5411 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5412 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5413 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5414 | entries.push_back(entry); |
| 5415 | } |
| 5416 | |
| 5417 | extern "C" { |
| 5418 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5419 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5420 | const char *str = ""; |
| 5421 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5422 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5423 | str = "ASTContext: expressions, declarations, and types"; |
| 5424 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5425 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5426 | str = "ASTContext: identifiers"; |
| 5427 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5428 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5429 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5430 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5431 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5432 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5433 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5434 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5435 | str = "SourceManager: content cache allocator"; |
| 5436 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5437 | case CXTUResourceUsage_AST_SideTables: |
| 5438 | str = "ASTContext: side tables"; |
| 5439 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5440 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5441 | str = "SourceManager: malloc'ed memory buffers"; |
| 5442 | break; |
| 5443 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5444 | str = "SourceManager: mmap'ed memory buffers"; |
| 5445 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5446 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5447 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5448 | break; |
| 5449 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5450 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5451 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5452 | case CXTUResourceUsage_Preprocessor: |
| 5453 | str = "Preprocessor: malloc'ed memory"; |
| 5454 | break; |
| 5455 | case CXTUResourceUsage_PreprocessingRecord: |
| 5456 | str = "Preprocessor: PreprocessingRecord"; |
| 5457 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5458 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5459 | str = "SourceManager: data structures and tables"; |
| 5460 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame^] | 5461 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5462 | str = "Preprocessor: header search tables"; |
| 5463 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5464 | } |
| 5465 | return str; |
| 5466 | } |
| 5467 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5468 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5469 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5470 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5471 | return usage; |
| 5472 | } |
| 5473 | |
| 5474 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5475 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5476 | ASTContext &astContext = astUnit->getASTContext(); |
| 5477 | |
| 5478 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5479 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5480 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5481 | |
| 5482 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5483 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5484 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5485 | |
| 5486 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5487 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5488 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5489 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5490 | // How much memory is used by ASTContext's side tables? |
| 5491 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5492 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5493 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5494 | // How much memory is used for caching global code completion results? |
| 5495 | unsigned long completionBytes = 0; |
| 5496 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5497 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5498 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5499 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5500 | createCXTUResourceUsageEntry(*entries, |
| 5501 | CXTUResourceUsage_GlobalCompletionResults, |
| 5502 | completionBytes); |
| 5503 | |
| 5504 | // How much memory is being used by SourceManager's content cache? |
| 5505 | createCXTUResourceUsageEntry(*entries, |
| 5506 | CXTUResourceUsage_SourceManagerContentCache, |
| 5507 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5508 | |
| 5509 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5510 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5511 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5512 | |
| 5513 | createCXTUResourceUsageEntry(*entries, |
| 5514 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5515 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5516 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5517 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5518 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5519 | createCXTUResourceUsageEntry(*entries, |
| 5520 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5521 | (unsigned long) astContext.getSourceManager() |
| 5522 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5523 | |
| 5524 | // How much memory is being used by the ExternalASTSource? |
| 5525 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5526 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5527 | esrc->getMemoryBufferSizes(); |
| 5528 | |
| 5529 | createCXTUResourceUsageEntry(*entries, |
| 5530 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5531 | (unsigned long) sizes.malloc_bytes); |
| 5532 | createCXTUResourceUsageEntry(*entries, |
| 5533 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5534 | (unsigned long) sizes.mmap_bytes); |
| 5535 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5536 | |
| 5537 | // How much memory is being used by the Preprocessor? |
| 5538 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5539 | createCXTUResourceUsageEntry(*entries, |
| 5540 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5541 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5542 | |
| 5543 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5544 | createCXTUResourceUsageEntry(*entries, |
| 5545 | CXTUResourceUsage_PreprocessingRecord, |
| 5546 | pRec->getTotalMemory()); |
| 5547 | } |
| 5548 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame^] | 5549 | createCXTUResourceUsageEntry(*entries, |
| 5550 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5551 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5552 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5553 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5554 | (unsigned) entries->size(), |
| 5555 | entries->size() ? &(*entries)[0] : 0 }; |
| 5556 | entries.take(); |
| 5557 | return usage; |
| 5558 | } |
| 5559 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5560 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5561 | if (usage.data) |
| 5562 | delete (MemUsageEntries*) usage.data; |
| 5563 | } |
| 5564 | |
| 5565 | } // end extern "C" |
| 5566 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5567 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5568 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5569 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5570 | fprintf(stderr, " %s: %lu\n", |
| 5571 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5572 | Usage.entries[I].amount); |
| 5573 | |
| 5574 | clang_disposeCXTUResourceUsage(Usage); |
| 5575 | } |
| 5576 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5577 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5578 | // Misc. utility functions. |
| 5579 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5580 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5581 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5582 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5583 | |
| 5584 | namespace clang { |
| 5585 | |
| 5586 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5587 | void (*Fn)(void*), void *UserData, |
| 5588 | unsigned Size) { |
| 5589 | if (!Size) |
| 5590 | Size = GetSafetyThreadStackSize(); |
| 5591 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5592 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5593 | return CRC.RunSafely(Fn, UserData); |
| 5594 | } |
| 5595 | |
| 5596 | unsigned GetSafetyThreadStackSize() { |
| 5597 | return SafetyStackThreadSize; |
| 5598 | } |
| 5599 | |
| 5600 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5601 | SafetyStackThreadSize = Value; |
| 5602 | } |
| 5603 | |
| 5604 | } |
| 5605 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5606 | extern "C" { |
| 5607 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5608 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5609 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5613 | |