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, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 144 | NestedNameSpecifierLocVisitKind, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 145 | DeclarationNameInfoVisitKind, |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 146 | MemberRefVisitKind, SizeOfPackExprPartsKind }; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 147 | protected: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 148 | void *data[3]; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 149 | CXCursor parent; |
| 150 | Kind K; |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 151 | VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0) |
| 152 | : parent(C), K(k) { |
| 153 | data[0] = d1; |
| 154 | data[1] = d2; |
| 155 | data[2] = d3; |
| 156 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 157 | public: |
| 158 | Kind getKind() const { return K; } |
| 159 | const CXCursor &getParent() const { return parent; } |
| 160 | static bool classof(VisitorJob *VJ) { return true; } |
| 161 | }; |
| 162 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 163 | typedef SmallVector<VisitorJob, 10> VisitorWorkList; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 165 | // Cursor visitor. |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 166 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 167 | public TypeLocVisitor<CursorVisitor, bool> |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 168 | { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 169 | /// \brief The translation unit we are traversing. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 170 | CXTranslationUnit TU; |
| 171 | ASTUnit *AU; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 173 | /// \brief The parent cursor whose children we are traversing. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 174 | CXCursor Parent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 176 | /// \brief The declaration that serves at the parent of any statement or |
| 177 | /// expression nodes. |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 178 | Decl *StmtParent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 180 | /// \brief The visitor function. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 181 | CXCursorVisitor Visitor; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 183 | /// \brief The opaque client data, to be passed along to the visitor. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 184 | CXClientData ClientData; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 186 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 187 | // to the visitor. Declarations with a PCH level greater than this value will |
| 188 | // be suppressed. |
| 189 | unsigned MaxPCHLevel; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 191 | /// \brief Whether we should visit the preprocessing record entries last, |
| 192 | /// after visiting other declarations. |
| 193 | bool VisitPreprocessorLast; |
| 194 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 195 | /// \brief When valid, a source range to which the cursor should restrict |
| 196 | /// its search. |
| 197 | SourceRange RegionOfInterest; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 199 | // FIXME: Eventually remove. This part of a hack to support proper |
| 200 | // iteration over all Decls contained lexically within an ObjC container. |
| 201 | DeclContext::decl_iterator *DI_current; |
| 202 | DeclContext::decl_iterator DE_current; |
| 203 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 204 | // Cache of pre-allocated worklists for data-recursion walk of Stmts. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 205 | SmallVector<VisitorWorkList*, 5> WorkListFreeList; |
| 206 | SmallVector<VisitorWorkList*, 5> WorkListCache; |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 208 | using DeclVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 209 | using TypeLocVisitor<CursorVisitor, bool>::Visit; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 210 | |
| 211 | /// \brief Determine whether this particular source range comes before, comes |
| 212 | /// after, or overlaps the region of interest. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 213 | /// |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 214 | /// \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] | 215 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); |
| 216 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 217 | class SetParentRAII { |
| 218 | CXCursor &Parent; |
| 219 | Decl *&StmtParent; |
| 220 | CXCursor OldParent; |
| 221 | |
| 222 | public: |
| 223 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 224 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
| 225 | { |
| 226 | Parent = NewParent; |
| 227 | if (clang_isDeclaration(Parent.kind)) |
| 228 | StmtParent = getCursorDecl(Parent); |
| 229 | } |
| 230 | |
| 231 | ~SetParentRAII() { |
| 232 | Parent = OldParent; |
| 233 | if (clang_isDeclaration(Parent.kind)) |
| 234 | StmtParent = getCursorDecl(Parent); |
| 235 | } |
| 236 | }; |
| 237 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 238 | public: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 239 | CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor, |
| 240 | CXClientData ClientData, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 241 | unsigned MaxPCHLevel, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 242 | bool VisitPreprocessorLast, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 243 | SourceRange RegionOfInterest = SourceRange()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 244 | : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)), |
| 245 | Visitor(Visitor), ClientData(ClientData), |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 246 | MaxPCHLevel(MaxPCHLevel), VisitPreprocessorLast(VisitPreprocessorLast), |
| 247 | RegionOfInterest(RegionOfInterest), DI_current(0) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 248 | { |
| 249 | Parent.kind = CXCursor_NoDeclFound; |
| 250 | Parent.data[0] = 0; |
| 251 | Parent.data[1] = 0; |
| 252 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 253 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 254 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 256 | ~CursorVisitor() { |
| 257 | // Free the pre-allocated worklists for data-recursion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 258 | for (SmallVectorImpl<VisitorWorkList*>::iterator |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 259 | I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) { |
| 260 | delete *I; |
| 261 | } |
| 262 | } |
| 263 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 264 | ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); } |
| 265 | CXTranslationUnit getTU() const { return TU; } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 267 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 269 | bool visitPreprocessedEntitiesInRegion(); |
| 270 | |
| 271 | template<typename InputIterator> |
| 272 | bool visitPreprocessedEntitiesInRegion(InputIterator First, |
| 273 | InputIterator Last); |
| 274 | |
| 275 | template<typename InputIterator> |
| 276 | bool visitPreprocessedEntities(InputIterator First, InputIterator Last); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 277 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 278 | bool VisitChildren(CXCursor Parent); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 280 | // Declaration visitors |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 281 | bool VisitTypeAliasDecl(TypeAliasDecl *D); |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 282 | bool VisitAttributes(Decl *D); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 283 | bool VisitBlockDecl(BlockDecl *B); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 284 | bool VisitCXXRecordDecl(CXXRecordDecl *D); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 285 | llvm::Optional<bool> shouldVisitCursor(CXCursor C); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 286 | bool VisitDeclContext(DeclContext *DC); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 287 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 288 | bool VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 289 | bool VisitTagDecl(TagDecl *D); |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 290 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 291 | bool VisitClassTemplatePartialSpecializationDecl( |
| 292 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 293 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 294 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
| 295 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
| 296 | bool VisitFunctionDecl(FunctionDecl *ND); |
| 297 | bool VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 298 | bool VisitVarDecl(VarDecl *); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 299 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 300 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 301 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 302 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 303 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 304 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 305 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 306 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 307 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 308 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 309 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 310 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 311 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 312 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 313 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 314 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 315 | bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 316 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 317 | bool VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 318 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 319 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 320 | bool VisitUsingDecl(UsingDecl *D); |
| 321 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 322 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 323 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 324 | // Name visitor |
| 325 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 326 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 327 | bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 329 | // Template visitors |
| 330 | bool VisitTemplateParameters(const TemplateParameterList *Params); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 331 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 332 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); |
| 333 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 334 | // Type visitors |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 335 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 336 | #define TYPELOC(CLASS, PARENT) \ |
| 337 | bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
| 338 | #include "clang/AST/TypeLocNodes.def" |
| 339 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 340 | bool VisitTagTypeLoc(TagTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 341 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 342 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); |
| 343 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 344 | // Data-recursive visitor functions. |
| 345 | bool IsInRegionOfInterest(CXCursor C); |
| 346 | bool RunVisitorWorkList(VisitorWorkList &WL); |
| 347 | void EnqueueWorkList(VisitorWorkList &WL, Stmt *S); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 348 | LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 349 | }; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 351 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 353 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 354 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 355 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 357 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 358 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 361 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 362 | /// its children. |
| 363 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 364 | /// \param Cursor the cursor to visit. |
| 365 | /// |
| 366 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 367 | /// this cursor is within the region of interest. |
| 368 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 369 | /// \returns true if the visitation should be aborted, false if it |
| 370 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 371 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 372 | if (clang_isInvalid(Cursor.kind)) |
| 373 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 375 | if (clang_isDeclaration(Cursor.kind)) { |
| 376 | Decl *D = getCursorDecl(Cursor); |
| 377 | assert(D && "Invalid declaration cursor"); |
Douglas Gregor | 6bf2b9f | 2011-08-12 00:15:20 +0000 | [diff] [blame] | 378 | if (D->getPCHLevel() > MaxPCHLevel && !isa<TranslationUnitDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 379 | return false; |
| 380 | |
| 381 | if (D->isImplicit()) |
| 382 | return false; |
| 383 | } |
| 384 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 385 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 386 | // we're done. |
| 387 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 388 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 389 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 390 | return false; |
| 391 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 393 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 394 | case CXChildVisit_Break: |
| 395 | return true; |
| 396 | |
| 397 | case CXChildVisit_Continue: |
| 398 | return false; |
| 399 | |
| 400 | case CXChildVisit_Recurse: |
| 401 | return VisitChildren(Cursor); |
| 402 | } |
| 403 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 404 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 407 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 408 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 409 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 410 | |
| 411 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 412 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 413 | |
| 414 | if (OnlyLocalDecls && RegionOfInterest.isValid()) { |
| 415 | // If we would only look at local declarations but we have a region of |
| 416 | // interest, check whether that region of interest is in the main file. |
| 417 | // If not, we should traverse all declarations. |
| 418 | // FIXME: My kingdom for a proper binary search approach to finding |
| 419 | // cursors! |
| 420 | std::pair<FileID, unsigned> Location |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 421 | = AU->getSourceManager().getDecomposedExpansionLoc( |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 422 | RegionOfInterest.getBegin()); |
| 423 | if (Location.first != AU->getSourceManager().getMainFileID()) |
| 424 | OnlyLocalDecls = false; |
| 425 | } |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 426 | |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 427 | PreprocessingRecord::iterator StartEntity, EndEntity; |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 428 | if (OnlyLocalDecls && AU->pp_entity_begin() != AU->pp_entity_end()) |
| 429 | return visitPreprocessedEntitiesInRegion(AU->pp_entity_begin(), |
| 430 | AU->pp_entity_end()); |
| 431 | else |
| 432 | return visitPreprocessedEntitiesInRegion(PPRec.begin(), PPRec.end()); |
| 433 | } |
| 434 | |
| 435 | template<typename InputIterator> |
| 436 | bool CursorVisitor::visitPreprocessedEntitiesInRegion(InputIterator First, |
| 437 | InputIterator Last) { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 438 | // There is no region of interest; we have to walk everything. |
| 439 | if (RegionOfInterest.isInvalid()) |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 440 | return visitPreprocessedEntities(First, Last); |
| 441 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 442 | // Find the file in which the region of interest lands. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 443 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 444 | std::pair<FileID, unsigned> Begin |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 445 | = SM.getDecomposedExpansionLoc(RegionOfInterest.getBegin()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 446 | std::pair<FileID, unsigned> End |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 447 | = SM.getDecomposedExpansionLoc(RegionOfInterest.getEnd()); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 448 | |
| 449 | // The region of interest spans files; we have to walk everything. |
| 450 | if (Begin.first != End.first) |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 451 | return visitPreprocessedEntities(First, Last); |
| 452 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 453 | ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 454 | = AU->getPreprocessedEntitiesByFile(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 455 | if (ByFileMap.empty()) { |
| 456 | // Build the mapping from files to sets of preprocessed entities. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 457 | for (; First != Last; ++First) { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 458 | std::pair<FileID, unsigned> P |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 459 | = SM.getDecomposedExpansionLoc((*First)->getSourceRange().getBegin()); |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 461 | ByFileMap[P.first].push_back(*First); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return visitPreprocessedEntities(ByFileMap[Begin.first].begin(), |
| 466 | ByFileMap[Begin.first].end()); |
| 467 | } |
| 468 | |
| 469 | template<typename InputIterator> |
| 470 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 471 | InputIterator Last) { |
| 472 | for (; First != Last; ++First) { |
| 473 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*First)) { |
| 474 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 475 | return true; |
| 476 | |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*First)) { |
| 481 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 482 | return true; |
| 483 | |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*First)) { |
| 488 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 489 | return true; |
| 490 | |
| 491 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 495 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 498 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 499 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 500 | /// \returns true if the visitation should be aborted, false if it |
| 501 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 502 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 503 | if (clang_isReference(Cursor.kind) && |
| 504 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 505 | // By definition, references have no children. |
| 506 | return false; |
| 507 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 508 | |
| 509 | // 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] | 510 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 511 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 513 | if (clang_isDeclaration(Cursor.kind)) { |
| 514 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 515 | if (!D) |
| 516 | return false; |
| 517 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 518 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 519 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 520 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 521 | if (clang_isStatement(Cursor.kind)) { |
| 522 | if (Stmt *S = getCursorStmt(Cursor)) |
| 523 | return Visit(S); |
| 524 | |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | if (clang_isExpression(Cursor.kind)) { |
| 529 | if (Expr *E = getCursorExpr(Cursor)) |
| 530 | return Visit(E); |
| 531 | |
| 532 | return false; |
| 533 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 535 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 536 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 537 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 538 | |
| 539 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 540 | for (unsigned I = 0; I != 2; ++I) { |
| 541 | if (VisitOrder[I]) { |
| 542 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 543 | RegionOfInterest.isInvalid()) { |
| 544 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 545 | TLEnd = CXXUnit->top_level_end(); |
| 546 | TL != TLEnd; ++TL) { |
| 547 | if (Visit(MakeCXCursor(*TL, tu), true)) |
| 548 | return true; |
| 549 | } |
| 550 | } else if (VisitDeclContext( |
| 551 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 552 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 553 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 554 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 556 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 557 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 558 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 559 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 561 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 562 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 564 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 565 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 566 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 567 | return Visit(BaseTSInfo->getTypeLoc()); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 572 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 576 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 577 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 578 | if (Visit(TSInfo->getTypeLoc())) |
| 579 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 580 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 581 | if (Stmt *Body = B->getBody()) |
| 582 | return Visit(MakeCXCursor(Body, StmtParent, TU)); |
| 583 | |
| 584 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 587 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 588 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 589 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 590 | if (Range.isInvalid()) |
| 591 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 592 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 593 | switch (CompareRegionOfInterest(Range)) { |
| 594 | case RangeBefore: |
| 595 | // This declaration comes before the region of interest; skip it. |
| 596 | return llvm::Optional<bool>(); |
| 597 | |
| 598 | case RangeAfter: |
| 599 | // This declaration comes after the region of interest; we're done. |
| 600 | return false; |
| 601 | |
| 602 | case RangeOverlap: |
| 603 | // This declaration overlaps the region of interest; visit it. |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 611 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 612 | |
| 613 | // FIXME: Eventually remove. This part of a hack to support proper |
| 614 | // iteration over all Decls contained lexically within an ObjC container. |
| 615 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 616 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 617 | |
| 618 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 619 | Decl *D = *I; |
| 620 | if (D->getLexicalDeclContext() != DC) |
| 621 | continue; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 622 | CXCursor Cursor = MakeCXCursor(D, TU); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 623 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 624 | if (!V.hasValue()) |
| 625 | continue; |
| 626 | if (!V.getValue()) |
| 627 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 628 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 629 | return true; |
| 630 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 631 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 634 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 635 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 636 | return false; |
| 637 | } |
| 638 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 639 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 640 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 641 | return Visit(TSInfo->getTypeLoc()); |
| 642 | |
| 643 | return false; |
| 644 | } |
| 645 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 646 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 647 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 648 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
| 653 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 654 | return VisitDeclContext(D); |
| 655 | } |
| 656 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 657 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 658 | ClassTemplateSpecializationDecl *D) { |
| 659 | bool ShouldVisitBody = false; |
| 660 | switch (D->getSpecializationKind()) { |
| 661 | case TSK_Undeclared: |
| 662 | case TSK_ImplicitInstantiation: |
| 663 | // Nothing to visit |
| 664 | return false; |
| 665 | |
| 666 | case TSK_ExplicitInstantiationDeclaration: |
| 667 | case TSK_ExplicitInstantiationDefinition: |
| 668 | break; |
| 669 | |
| 670 | case TSK_ExplicitSpecialization: |
| 671 | ShouldVisitBody = true; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | // Visit the template arguments used in the specialization. |
| 676 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 677 | TypeLoc TL = SpecType->getTypeLoc(); |
| 678 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 679 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 680 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 681 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 687 | return true; |
| 688 | |
| 689 | return false; |
| 690 | } |
| 691 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 692 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 693 | ClassTemplatePartialSpecializationDecl *D) { |
| 694 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 695 | // before visiting these template parameters. |
| 696 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 697 | return true; |
| 698 | |
| 699 | // Visit the partial specialization arguments. |
| 700 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 701 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 702 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 703 | return true; |
| 704 | |
| 705 | return VisitCXXRecordDecl(D); |
| 706 | } |
| 707 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 708 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 709 | // Visit the default argument. |
| 710 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 711 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 712 | if (Visit(DefArg->getTypeLoc())) |
| 713 | return true; |
| 714 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 718 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 719 | if (Expr *Init = D->getInitExpr()) |
| 720 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
| 721 | return false; |
| 722 | } |
| 723 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 724 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 725 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 726 | if (Visit(TSInfo->getTypeLoc())) |
| 727 | return true; |
| 728 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 729 | // Visit the nested-name-specifier, if present. |
| 730 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 731 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 732 | return true; |
| 733 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 734 | return false; |
| 735 | } |
| 736 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 737 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 738 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 739 | CXXCtorInitializer const * const *X |
| 740 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 741 | CXXCtorInitializer const * const *Y |
| 742 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 743 | |
| 744 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 745 | return -1; |
| 746 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 747 | return 1; |
| 748 | else |
| 749 | return 0; |
| 750 | } |
| 751 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 752 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 753 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 754 | // Visit the function declaration's syntactic components in the order |
| 755 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 756 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 757 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 758 | |
| 759 | // If we have a function declared directly (without the use of a typedef), |
| 760 | // visit just the return type. Otherwise, just visit the function's type |
| 761 | // now. |
| 762 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 763 | (!FTL && Visit(TL))) |
| 764 | return true; |
| 765 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 766 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 767 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 768 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 769 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 770 | |
| 771 | // Visit the declaration name. |
| 772 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 773 | return true; |
| 774 | |
| 775 | // FIXME: Visit explicitly-specified template arguments! |
| 776 | |
| 777 | // Visit the function parameters, if we have a function type. |
| 778 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 779 | return true; |
| 780 | |
| 781 | // FIXME: Attributes? |
| 782 | } |
| 783 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 784 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 785 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 786 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 787 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 788 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 789 | IEnd = Constructor->init_end(); |
| 790 | I != IEnd; ++I) { |
| 791 | if (!(*I)->isWritten()) |
| 792 | continue; |
| 793 | |
| 794 | WrittenInits.push_back(*I); |
| 795 | } |
| 796 | |
| 797 | // Sort the initializers in source order |
| 798 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 799 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 800 | |
| 801 | // Visit the initializers in source order |
| 802 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 803 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 804 | if (Init->isAnyMemberInitializer()) { |
| 805 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 806 | Init->getMemberLocation(), TU))) |
| 807 | return true; |
| 808 | } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) { |
| 809 | if (Visit(BaseInfo->getTypeLoc())) |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | // Visit the initializer value. |
| 814 | if (Expr *Initializer = Init->getInit()) |
| 815 | if (Visit(MakeCXCursor(Initializer, ND, TU))) |
| 816 | return true; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 821 | return true; |
| 822 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 824 | return false; |
| 825 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 827 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 828 | if (VisitDeclaratorDecl(D)) |
| 829 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 831 | if (Expr *BitWidth = D->getBitWidth()) |
| 832 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 834 | return false; |
| 835 | } |
| 836 | |
| 837 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 838 | if (VisitDeclaratorDecl(D)) |
| 839 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 840 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 841 | if (Expr *Init = D->getInit()) |
| 842 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 847 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 848 | if (VisitDeclaratorDecl(D)) |
| 849 | return true; |
| 850 | |
| 851 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 852 | if (Expr *DefArg = D->getDefaultArgument()) |
| 853 | return Visit(MakeCXCursor(DefArg, StmtParent, TU)); |
| 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 858 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 859 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 860 | // before visiting these template parameters. |
| 861 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 862 | return true; |
| 863 | |
| 864 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 865 | } |
| 866 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 867 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 868 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 869 | // before visiting these template parameters. |
| 870 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 871 | return true; |
| 872 | |
| 873 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 874 | } |
| 875 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 876 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 877 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 878 | return true; |
| 879 | |
| 880 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 881 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 882 | return true; |
| 883 | |
| 884 | return false; |
| 885 | } |
| 886 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 887 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 888 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 889 | if (Visit(TSInfo->getTypeLoc())) |
| 890 | return true; |
| 891 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 892 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 893 | PEnd = ND->param_end(); |
| 894 | P != PEnd; ++P) { |
| 895 | if (Visit(MakeCXCursor(*P, TU))) |
| 896 | return true; |
| 897 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 899 | if (ND->isThisDeclarationADefinition() && |
| 900 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 901 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 903 | return false; |
| 904 | } |
| 905 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 906 | namespace { |
| 907 | struct ContainerDeclsSort { |
| 908 | SourceManager &SM; |
| 909 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 910 | bool operator()(Decl *A, Decl *B) { |
| 911 | SourceLocation L_A = A->getLocStart(); |
| 912 | SourceLocation L_B = B->getLocStart(); |
| 913 | assert(L_A.isValid() && L_B.isValid()); |
| 914 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 915 | } |
| 916 | }; |
| 917 | } |
| 918 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 919 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 920 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 921 | // an @implementation can lexically contain Decls that are not properly |
| 922 | // nested in the AST. When we identify such cases, we need to retrofit |
| 923 | // this nesting here. |
| 924 | if (!DI_current) |
| 925 | return VisitDeclContext(D); |
| 926 | |
| 927 | // Scan the Decls that immediately come after the container |
| 928 | // in the current DeclContext. If any fall within the |
| 929 | // container's lexical region, stash them into a vector |
| 930 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 931 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 932 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 933 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 934 | if (EndLoc.isValid()) { |
| 935 | DeclContext::decl_iterator next = *DI_current; |
| 936 | while (++next != DE_current) { |
| 937 | Decl *D_next = *next; |
| 938 | if (!D_next) |
| 939 | break; |
| 940 | SourceLocation L = D_next->getLocStart(); |
| 941 | if (!L.isValid()) |
| 942 | break; |
| 943 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 944 | *DI_current = next; |
| 945 | DeclsInContainer.push_back(D_next); |
| 946 | continue; |
| 947 | } |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | // The common case. |
| 953 | if (DeclsInContainer.empty()) |
| 954 | return VisitDeclContext(D); |
| 955 | |
| 956 | // Get all the Decls in the DeclContext, and sort them with the |
| 957 | // additional ones we've collected. Then visit them. |
| 958 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 959 | I!=E; ++I) { |
| 960 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 961 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 962 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 963 | continue; |
| 964 | DeclsInContainer.push_back(subDecl); |
| 965 | } |
| 966 | |
| 967 | // Now sort the Decls so that they appear in lexical order. |
| 968 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 969 | ContainerDeclsSort(SM)); |
| 970 | |
| 971 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 972 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 973 | E = DeclsInContainer.end(); I != E; ++I) { |
| 974 | CXCursor Cursor = MakeCXCursor(*I, TU); |
| 975 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 976 | if (!V.hasValue()) |
| 977 | continue; |
| 978 | if (!V.getValue()) |
| 979 | return false; |
| 980 | if (Visit(Cursor, true)) |
| 981 | return true; |
| 982 | } |
| 983 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 986 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 987 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 988 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 989 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 991 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 992 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 993 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 994 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 995 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 996 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 997 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1000 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1001 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1002 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1003 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1004 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1005 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1007 | return VisitObjCContainerDecl(PID); |
| 1008 | } |
| 1009 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1010 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 1011 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 1012 | return true; |
| 1013 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1014 | // FIXME: This implements a workaround with @property declarations also being |
| 1015 | // installed in the DeclContext for the @interface. Eventually this code |
| 1016 | // should be removed. |
| 1017 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1018 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1019 | return false; |
| 1020 | |
| 1021 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1022 | if (!ID) |
| 1023 | return false; |
| 1024 | |
| 1025 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1026 | ObjCPropertyDecl *prevDecl = |
| 1027 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 1028 | |
| 1029 | if (!prevDecl) |
| 1030 | return false; |
| 1031 | |
| 1032 | // Visit synthesized methods since they will be skipped when visiting |
| 1033 | // the @interface. |
| 1034 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1035 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1036 | if (Visit(MakeCXCursor(MD, TU))) |
| 1037 | return true; |
| 1038 | |
| 1039 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1040 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1041 | if (Visit(MakeCXCursor(MD, TU))) |
| 1042 | return true; |
| 1043 | |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1047 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1048 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1049 | if (D->getSuperClass() && |
| 1050 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1051 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1052 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1053 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1055 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1056 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1057 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1058 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1059 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1061 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1064 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1065 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1068 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1069 | // 'ID' could be null when dealing with invalid code. |
| 1070 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1071 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1072 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1074 | return VisitObjCImplDecl(D); |
| 1075 | } |
| 1076 | |
| 1077 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1078 | #if 0 |
| 1079 | // Issue callbacks for super class. |
| 1080 | // FIXME: No source location information! |
| 1081 | if (D->getSuperClass() && |
| 1082 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1083 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1084 | TU))) |
| 1085 | return true; |
| 1086 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1088 | return VisitObjCImplDecl(D); |
| 1089 | } |
| 1090 | |
| 1091 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1092 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1093 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1094 | E = D->protocol_end(); |
| 1095 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1096 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1097 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1098 | |
| 1099 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1102 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1103 | if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), |
| 1104 | D->getForwardDecl()->getLocation(), TU))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1105 | return true; |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1106 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1109 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1110 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1111 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1112 | |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1116 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1117 | return VisitDeclContext(D); |
| 1118 | } |
| 1119 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1120 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1121 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1122 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1123 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1124 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1125 | |
| 1126 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1127 | D->getTargetNameLoc(), TU)); |
| 1128 | } |
| 1129 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1130 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1131 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1132 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1133 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1134 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1135 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1136 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1137 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1138 | return true; |
| 1139 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1140 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1141 | } |
| 1142 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1143 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1144 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1145 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1146 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1147 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1148 | |
| 1149 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1150 | D->getIdentLocation(), TU)); |
| 1151 | } |
| 1152 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1153 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1154 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1155 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1156 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1157 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1158 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1160 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1161 | } |
| 1162 | |
| 1163 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1164 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1165 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1166 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1167 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1168 | return true; |
| 1169 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1170 | return false; |
| 1171 | } |
| 1172 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1173 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1174 | switch (Name.getName().getNameKind()) { |
| 1175 | case clang::DeclarationName::Identifier: |
| 1176 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1177 | case clang::DeclarationName::CXXOperatorName: |
| 1178 | case clang::DeclarationName::CXXUsingDirective: |
| 1179 | return false; |
| 1180 | |
| 1181 | case clang::DeclarationName::CXXConstructorName: |
| 1182 | case clang::DeclarationName::CXXDestructorName: |
| 1183 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1184 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1185 | return Visit(TSInfo->getTypeLoc()); |
| 1186 | return false; |
| 1187 | |
| 1188 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1189 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1190 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1191 | // FIXME: Per-identifier location info? |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1198 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1199 | SourceRange Range) { |
| 1200 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1201 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1202 | // a beginning source location, we can visit the first component of the |
| 1203 | // nested-name-specifier, if it's a single-token component. |
| 1204 | if (!NNS) |
| 1205 | return false; |
| 1206 | |
| 1207 | // Get the first component in the nested-name-specifier. |
| 1208 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1209 | NNS = Prefix; |
| 1210 | |
| 1211 | switch (NNS->getKind()) { |
| 1212 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1213 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1214 | TU)); |
| 1215 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1216 | case NestedNameSpecifier::NamespaceAlias: |
| 1217 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1218 | Range.getBegin(), TU)); |
| 1219 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1220 | case NestedNameSpecifier::TypeSpec: { |
| 1221 | // If the type has a form where we know that the beginning of the source |
| 1222 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1223 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1224 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1225 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1226 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1227 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1228 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1229 | if (const TemplateSpecializationType *TST |
| 1230 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1231 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1232 | break; |
| 1233 | } |
| 1234 | |
| 1235 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1236 | case NestedNameSpecifier::Global: |
| 1237 | case NestedNameSpecifier::Identifier: |
| 1238 | break; |
| 1239 | } |
| 1240 | |
| 1241 | return false; |
| 1242 | } |
| 1243 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1244 | bool |
| 1245 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1246 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1247 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1248 | Qualifiers.push_back(Qualifier); |
| 1249 | |
| 1250 | while (!Qualifiers.empty()) { |
| 1251 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1252 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1253 | switch (NNS->getKind()) { |
| 1254 | case NestedNameSpecifier::Namespace: |
| 1255 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1256 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1257 | TU))) |
| 1258 | return true; |
| 1259 | |
| 1260 | break; |
| 1261 | |
| 1262 | case NestedNameSpecifier::NamespaceAlias: |
| 1263 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1264 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1265 | TU))) |
| 1266 | return true; |
| 1267 | |
| 1268 | break; |
| 1269 | |
| 1270 | case NestedNameSpecifier::TypeSpec: |
| 1271 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1272 | if (Visit(Q.getTypeLoc())) |
| 1273 | return true; |
| 1274 | |
| 1275 | break; |
| 1276 | |
| 1277 | case NestedNameSpecifier::Global: |
| 1278 | case NestedNameSpecifier::Identifier: |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1286 | bool CursorVisitor::VisitTemplateParameters( |
| 1287 | const TemplateParameterList *Params) { |
| 1288 | if (!Params) |
| 1289 | return false; |
| 1290 | |
| 1291 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1292 | PEnd = Params->end(); |
| 1293 | P != PEnd; ++P) { |
| 1294 | if (Visit(MakeCXCursor(*P, TU))) |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1301 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1302 | switch (Name.getKind()) { |
| 1303 | case TemplateName::Template: |
| 1304 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1305 | |
| 1306 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1307 | // Visit the overloaded template set. |
| 1308 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1309 | return true; |
| 1310 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1311 | return false; |
| 1312 | |
| 1313 | case TemplateName::DependentTemplate: |
| 1314 | // FIXME: Visit nested-name-specifier. |
| 1315 | return false; |
| 1316 | |
| 1317 | case TemplateName::QualifiedTemplate: |
| 1318 | // FIXME: Visit nested-name-specifier. |
| 1319 | return Visit(MakeCursorTemplateRef( |
| 1320 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1321 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1322 | |
| 1323 | case TemplateName::SubstTemplateTemplateParm: |
| 1324 | return Visit(MakeCursorTemplateRef( |
| 1325 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1326 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1327 | |
| 1328 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1329 | return Visit(MakeCursorTemplateRef( |
| 1330 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1331 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1337 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1338 | switch (TAL.getArgument().getKind()) { |
| 1339 | case TemplateArgument::Null: |
| 1340 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1341 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1342 | return false; |
| 1343 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1344 | case TemplateArgument::Type: |
| 1345 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1346 | return Visit(TSInfo->getTypeLoc()); |
| 1347 | return false; |
| 1348 | |
| 1349 | case TemplateArgument::Declaration: |
| 1350 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1351 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1352 | return false; |
| 1353 | |
| 1354 | case TemplateArgument::Expression: |
| 1355 | if (Expr *E = TAL.getSourceExpression()) |
| 1356 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1357 | return false; |
| 1358 | |
| 1359 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1360 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1361 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1362 | return true; |
| 1363 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1364 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1365 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1371 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1372 | return VisitDeclContext(D); |
| 1373 | } |
| 1374 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1375 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1376 | return Visit(TL.getUnqualifiedLoc()); |
| 1377 | } |
| 1378 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1379 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1380 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1383 | // "Class") have associated declarations. Create cursors for those. |
| 1384 | QualType VisitType; |
| 1385 | switch (TL.getType()->getAs<BuiltinType>()->getKind()) { |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1386 | case BuiltinType::Void: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1387 | case BuiltinType::Bool: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1388 | case BuiltinType::Char_U: |
| 1389 | case BuiltinType::UChar: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1390 | case BuiltinType::Char16: |
| 1391 | case BuiltinType::Char32: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1392 | case BuiltinType::UShort: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1393 | case BuiltinType::UInt: |
| 1394 | case BuiltinType::ULong: |
| 1395 | case BuiltinType::ULongLong: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1396 | case BuiltinType::UInt128: |
| 1397 | case BuiltinType::Char_S: |
| 1398 | case BuiltinType::SChar: |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1399 | case BuiltinType::WChar_U: |
| 1400 | case BuiltinType::WChar_S: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1401 | case BuiltinType::Short: |
| 1402 | case BuiltinType::Int: |
| 1403 | case BuiltinType::Long: |
| 1404 | case BuiltinType::LongLong: |
| 1405 | case BuiltinType::Int128: |
| 1406 | case BuiltinType::Float: |
| 1407 | case BuiltinType::Double: |
| 1408 | case BuiltinType::LongDouble: |
| 1409 | case BuiltinType::NullPtr: |
| 1410 | case BuiltinType::Overload: |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1411 | case BuiltinType::BoundMember: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1412 | case BuiltinType::Dependent: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1413 | case BuiltinType::UnknownAny: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1414 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1415 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1416 | case BuiltinType::ObjCId: |
| 1417 | VisitType = Context.getObjCIdType(); |
| 1418 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1419 | |
| 1420 | case BuiltinType::ObjCClass: |
| 1421 | VisitType = Context.getObjCClassType(); |
| 1422 | break; |
| 1423 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1424 | case BuiltinType::ObjCSel: |
| 1425 | VisitType = Context.getObjCSelType(); |
| 1426 | break; |
| 1427 | } |
| 1428 | |
| 1429 | if (!VisitType.isNull()) { |
| 1430 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1431 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1432 | TU)); |
| 1433 | } |
| 1434 | |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1438 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1439 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1442 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1443 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1444 | } |
| 1445 | |
| 1446 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1447 | if (TL.isDefinition()) |
| 1448 | return Visit(MakeCXCursor(TL.getDecl(), TU)); |
| 1449 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1450 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1451 | } |
| 1452 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1453 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1454 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1457 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1458 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1459 | return true; |
| 1460 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1461 | return false; |
| 1462 | } |
| 1463 | |
| 1464 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1465 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1466 | return true; |
| 1467 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1468 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1469 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1470 | TU))) |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
| 1477 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1478 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1481 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1482 | return Visit(TL.getInnerLoc()); |
| 1483 | } |
| 1484 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1485 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1486 | return Visit(TL.getPointeeLoc()); |
| 1487 | } |
| 1488 | |
| 1489 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1490 | return Visit(TL.getPointeeLoc()); |
| 1491 | } |
| 1492 | |
| 1493 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1494 | return Visit(TL.getPointeeLoc()); |
| 1495 | } |
| 1496 | |
| 1497 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1498 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1502 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1505 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1506 | return Visit(TL.getModifiedLoc()); |
| 1507 | } |
| 1508 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1509 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1510 | bool SkipResultType) { |
| 1511 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1512 | return true; |
| 1513 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1514 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1515 | if (Decl *D = TL.getArg(I)) |
| 1516 | if (Visit(MakeCXCursor(D, TU))) |
| 1517 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1518 | |
| 1519 | return false; |
| 1520 | } |
| 1521 | |
| 1522 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1523 | if (Visit(TL.getElementLoc())) |
| 1524 | return true; |
| 1525 | |
| 1526 | if (Expr *Size = TL.getSizeExpr()) |
| 1527 | return Visit(MakeCXCursor(Size, StmtParent, TU)); |
| 1528 | |
| 1529 | return false; |
| 1530 | } |
| 1531 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1532 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1533 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1534 | // Visit the template name. |
| 1535 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1536 | TL.getTemplateNameLoc())) |
| 1537 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1538 | |
| 1539 | // Visit the template arguments. |
| 1540 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1541 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1542 | return true; |
| 1543 | |
| 1544 | return false; |
| 1545 | } |
| 1546 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1547 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1548 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1549 | } |
| 1550 | |
| 1551 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1552 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1553 | return Visit(TSInfo->getTypeLoc()); |
| 1554 | |
| 1555 | return false; |
| 1556 | } |
| 1557 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1558 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1559 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1560 | return Visit(TSInfo->getTypeLoc()); |
| 1561 | |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1565 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1566 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1567 | return true; |
| 1568 | |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1572 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1573 | DependentTemplateSpecializationTypeLoc TL) { |
| 1574 | // Visit the nested-name-specifier, if there is one. |
| 1575 | if (TL.getQualifierLoc() && |
| 1576 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1577 | return true; |
| 1578 | |
| 1579 | // Visit the template arguments. |
| 1580 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1581 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1582 | return true; |
| 1583 | |
| 1584 | return false; |
| 1585 | } |
| 1586 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1587 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1588 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1589 | return true; |
| 1590 | |
| 1591 | return Visit(TL.getNamedTypeLoc()); |
| 1592 | } |
| 1593 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1594 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1595 | return Visit(TL.getPatternLoc()); |
| 1596 | } |
| 1597 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1598 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1599 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1600 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1601 | |
| 1602 | return false; |
| 1603 | } |
| 1604 | |
| 1605 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1606 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1607 | } |
| 1608 | |
| 1609 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1610 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1611 | return Visit##PARENT##Loc(TL); \ |
| 1612 | } |
| 1613 | |
| 1614 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1615 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1616 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1617 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1618 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1619 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1620 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1621 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1622 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1623 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1624 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1625 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1626 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1627 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1628 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1629 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1630 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1631 | // Visit the nested-name-specifier, if present. |
| 1632 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1633 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1634 | return true; |
| 1635 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1636 | if (D->isDefinition()) { |
| 1637 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1638 | E = D->bases_end(); I != E; ++I) { |
| 1639 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1640 | return true; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return VisitTagDecl(D); |
| 1645 | } |
| 1646 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1647 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1648 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1649 | i != e; ++i) |
| 1650 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1651 | return true; |
| 1652 | |
| 1653 | return false; |
| 1654 | } |
| 1655 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1656 | //===----------------------------------------------------------------------===// |
| 1657 | // Data-recursive visitor methods. |
| 1658 | //===----------------------------------------------------------------------===// |
| 1659 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1660 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1661 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1662 | class NAME : public VisitorJob {\ |
| 1663 | public:\ |
| 1664 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1665 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1666 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1667 | }; |
| 1668 | |
| 1669 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1670 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1671 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1672 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1673 | DEF_JOB(ExplicitTemplateArgsVisit, ExplicitTemplateArgumentList, |
| 1674 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1675 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1676 | #undef DEF_JOB |
| 1677 | |
| 1678 | class DeclVisit : public VisitorJob { |
| 1679 | public: |
| 1680 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1681 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1682 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1683 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1684 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1685 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1686 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1687 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1688 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1689 | class TypeLocVisit : public VisitorJob { |
| 1690 | public: |
| 1691 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1692 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1693 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1694 | |
| 1695 | static bool classof(const VisitorJob *VJ) { |
| 1696 | return VJ->getKind() == TypeLocVisitKind; |
| 1697 | } |
| 1698 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1699 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1700 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1701 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1702 | } |
| 1703 | }; |
| 1704 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1705 | class LabelRefVisit : public VisitorJob { |
| 1706 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1707 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1708 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1709 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1710 | |
| 1711 | static bool classof(const VisitorJob *VJ) { |
| 1712 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1713 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1714 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1715 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1716 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1717 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1718 | |
| 1719 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1720 | public: |
| 1721 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1722 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1723 | Qualifier.getNestedNameSpecifier(), |
| 1724 | Qualifier.getOpaqueData()) { } |
| 1725 | |
| 1726 | static bool classof(const VisitorJob *VJ) { |
| 1727 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1728 | } |
| 1729 | |
| 1730 | NestedNameSpecifierLoc get() const { |
| 1731 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1732 | data[1]); |
| 1733 | } |
| 1734 | }; |
| 1735 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1736 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1737 | public: |
| 1738 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1739 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1740 | static bool classof(const VisitorJob *VJ) { |
| 1741 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1742 | } |
| 1743 | DeclarationNameInfo get() const { |
| 1744 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1745 | switch (S->getStmtClass()) { |
| 1746 | default: |
| 1747 | llvm_unreachable("Unhandled Stmt"); |
| 1748 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1749 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1750 | case Stmt::DependentScopeDeclRefExprClass: |
| 1751 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1752 | } |
| 1753 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1754 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1755 | class MemberRefVisit : public VisitorJob { |
| 1756 | public: |
| 1757 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1758 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1759 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1760 | static bool classof(const VisitorJob *VJ) { |
| 1761 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1762 | } |
| 1763 | FieldDecl *get() const { |
| 1764 | return static_cast<FieldDecl*>(data[0]); |
| 1765 | } |
| 1766 | SourceLocation getLoc() const { |
| 1767 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1768 | } |
| 1769 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1770 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1771 | VisitorWorkList &WL; |
| 1772 | CXCursor Parent; |
| 1773 | public: |
| 1774 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1775 | : WL(wl), Parent(parent) {} |
| 1776 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1777 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1778 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1779 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1780 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1781 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1782 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1783 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1784 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1785 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1786 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1787 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1788 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1789 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1790 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1791 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1792 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1793 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1794 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1795 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1796 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1797 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1798 | void VisitIfStmt(IfStmt *If); |
| 1799 | void VisitInitListExpr(InitListExpr *IE); |
| 1800 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1801 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1802 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1803 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1804 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1805 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1806 | void VisitStmt(Stmt *S); |
| 1807 | void VisitSwitchStmt(SwitchStmt *S); |
| 1808 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1809 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1810 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1811 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1812 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1813 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1814 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1815 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1816 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1817 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1818 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1819 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1820 | void AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1821 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1822 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1823 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1824 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1825 | void EnqueueChildren(Stmt *S); |
| 1826 | }; |
| 1827 | } // end anonyous namespace |
| 1828 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1829 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1830 | // 'S' should always be non-null, since it comes from the |
| 1831 | // statement we are visiting. |
| 1832 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1833 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1834 | |
| 1835 | void |
| 1836 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1837 | if (Qualifier) |
| 1838 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1839 | } |
| 1840 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1841 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1842 | if (S) |
| 1843 | WL.push_back(StmtVisit(S, Parent)); |
| 1844 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1845 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1846 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1847 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1848 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1849 | void EnqueueVisitor:: |
| 1850 | AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A) { |
| 1851 | if (A) |
| 1852 | WL.push_back(ExplicitTemplateArgsVisit( |
| 1853 | const_cast<ExplicitTemplateArgumentList*>(A), Parent)); |
| 1854 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1855 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1856 | if (D) |
| 1857 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1858 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1859 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1860 | if (TI) |
| 1861 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1862 | } |
| 1863 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1864 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1865 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1866 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1867 | } |
| 1868 | if (size == WL.size()) |
| 1869 | return; |
| 1870 | // Now reverse the entries we just added. This will match the DFS |
| 1871 | // ordering performed by the worklist. |
| 1872 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1873 | std::reverse(I, E); |
| 1874 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1875 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1876 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1877 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1878 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1879 | AddDecl(B->getBlockDecl()); |
| 1880 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1881 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1882 | EnqueueChildren(E); |
| 1883 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1884 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1885 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1886 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1887 | E = S->body_rend(); I != E; ++I) { |
| 1888 | AddStmt(*I); |
| 1889 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1890 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1891 | void EnqueueVisitor:: |
| 1892 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1893 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1894 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1895 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1896 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1897 | if (!E->isImplicitAccess()) |
| 1898 | AddStmt(E->getBase()); |
| 1899 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1900 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1901 | // Enqueue the initializer or constructor arguments. |
| 1902 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 1903 | AddStmt(E->getConstructorArg(I-1)); |
| 1904 | // Enqueue the array size, if any. |
| 1905 | AddStmt(E->getArraySize()); |
| 1906 | // Enqueue the allocated type. |
| 1907 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1908 | // Enqueue the placement arguments. |
| 1909 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1910 | AddStmt(E->getPlacementArg(I-1)); |
| 1911 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1912 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1913 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1914 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1915 | AddStmt(CE->getCallee()); |
| 1916 | AddStmt(CE->getArg(0)); |
| 1917 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1918 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1919 | // Visit the name of the type being destroyed. |
| 1920 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1921 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1922 | // but isn't. |
| 1923 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1924 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1925 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1926 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1927 | // Visit base expression. |
| 1928 | AddStmt(E->getBase()); |
| 1929 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1930 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1931 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1932 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1933 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1934 | EnqueueChildren(E); |
| 1935 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1936 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1937 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1938 | EnqueueChildren(E); |
| 1939 | if (E->isTypeOperand()) |
| 1940 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1941 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1942 | |
| 1943 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1944 | *E) { |
| 1945 | EnqueueChildren(E); |
| 1946 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1947 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1948 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1949 | EnqueueChildren(E); |
| 1950 | if (E->isTypeOperand()) |
| 1951 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1952 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1953 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1954 | if (DR->hasExplicitTemplateArgs()) { |
| 1955 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1956 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1957 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1958 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1959 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1960 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1961 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1962 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1963 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1964 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1965 | unsigned size = WL.size(); |
| 1966 | bool isFirst = true; |
| 1967 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1968 | D != DEnd; ++D) { |
| 1969 | AddDecl(*D, isFirst); |
| 1970 | isFirst = false; |
| 1971 | } |
| 1972 | if (size == WL.size()) |
| 1973 | return; |
| 1974 | // Now reverse the entries we just added. This will match the DFS |
| 1975 | // ordering performed by the worklist. |
| 1976 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1977 | std::reverse(I, E); |
| 1978 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1979 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1980 | AddStmt(E->getInit()); |
| 1981 | typedef DesignatedInitExpr::Designator Designator; |
| 1982 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1983 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1984 | D != DEnd; ++D) { |
| 1985 | if (D->isFieldDesignator()) { |
| 1986 | if (FieldDecl *Field = D->getField()) |
| 1987 | AddMemberRef(Field, D->getFieldLoc()); |
| 1988 | continue; |
| 1989 | } |
| 1990 | if (D->isArrayDesignator()) { |
| 1991 | AddStmt(E->getArrayIndex(*D)); |
| 1992 | continue; |
| 1993 | } |
| 1994 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 1995 | AddStmt(E->getArrayRangeEnd(*D)); |
| 1996 | AddStmt(E->getArrayRangeStart(*D)); |
| 1997 | } |
| 1998 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1999 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 2000 | EnqueueChildren(E); |
| 2001 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2002 | } |
| 2003 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 2004 | AddStmt(FS->getBody()); |
| 2005 | AddStmt(FS->getInc()); |
| 2006 | AddStmt(FS->getCond()); |
| 2007 | AddDecl(FS->getConditionVariable()); |
| 2008 | AddStmt(FS->getInit()); |
| 2009 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2010 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 2011 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2012 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2013 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 2014 | AddStmt(If->getElse()); |
| 2015 | AddStmt(If->getThen()); |
| 2016 | AddStmt(If->getCond()); |
| 2017 | AddDecl(If->getConditionVariable()); |
| 2018 | } |
| 2019 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 2020 | // We care about the syntactic form of the initializer list, only. |
| 2021 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2022 | IE = Syntactic; |
| 2023 | EnqueueChildren(IE); |
| 2024 | } |
| 2025 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 2026 | WL.push_back(MemberExprParts(M, Parent)); |
| 2027 | |
| 2028 | // If the base of the member access expression is an implicit 'this', don't |
| 2029 | // visit it. |
| 2030 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2031 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2032 | if (!M->isImplicitAccess()) |
| 2033 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2034 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2035 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2036 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2037 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2038 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2039 | EnqueueChildren(M); |
| 2040 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2041 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2042 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2043 | // Visit the components of the offsetof expression. |
| 2044 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2045 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2046 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2047 | switch (Node.getKind()) { |
| 2048 | case OffsetOfNode::Array: |
| 2049 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2050 | break; |
| 2051 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2052 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2053 | break; |
| 2054 | case OffsetOfNode::Identifier: |
| 2055 | case OffsetOfNode::Base: |
| 2056 | continue; |
| 2057 | } |
| 2058 | } |
| 2059 | // Visit the type into which we're computing the offset. |
| 2060 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2061 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2062 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2063 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2064 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2065 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2066 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2067 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2068 | EnqueueChildren(E); |
| 2069 | if (E->isArgumentType()) |
| 2070 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2071 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2072 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2073 | EnqueueChildren(S); |
| 2074 | } |
| 2075 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2076 | AddStmt(S->getBody()); |
| 2077 | AddStmt(S->getCond()); |
| 2078 | AddDecl(S->getConditionVariable()); |
| 2079 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2080 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2081 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2082 | AddStmt(W->getBody()); |
| 2083 | AddStmt(W->getCond()); |
| 2084 | AddDecl(W->getConditionVariable()); |
| 2085 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2086 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2087 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2088 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2089 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2090 | |
| 2091 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2092 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2093 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2096 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2097 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2098 | } |
| 2099 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2100 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2101 | EnqueueChildren(E); |
| 2102 | } |
| 2103 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2104 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2105 | VisitOverloadExpr(U); |
| 2106 | if (!U->isImplicitAccess()) |
| 2107 | AddStmt(U->getBase()); |
| 2108 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2109 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2110 | AddStmt(E->getSubExpr()); |
| 2111 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2112 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2113 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2114 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2115 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2116 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2117 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2118 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2122 | if (RegionOfInterest.isValid()) { |
| 2123 | SourceRange Range = getRawCursorExtent(C); |
| 2124 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2125 | return false; |
| 2126 | } |
| 2127 | return true; |
| 2128 | } |
| 2129 | |
| 2130 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2131 | while (!WL.empty()) { |
| 2132 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2133 | VisitorJob LI = WL.back(); |
| 2134 | WL.pop_back(); |
| 2135 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2136 | // Set the Parent field, then back to its old value once we're done. |
| 2137 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2138 | |
| 2139 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2140 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2141 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2142 | if (!D) |
| 2143 | continue; |
| 2144 | |
| 2145 | // For now, perform default visitation for Decls. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2146 | if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2147 | return true; |
| 2148 | |
| 2149 | continue; |
| 2150 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2151 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
| 2152 | const ExplicitTemplateArgumentList *ArgList = |
| 2153 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2154 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2155 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2156 | Arg != ArgEnd; ++Arg) { |
| 2157 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2158 | return true; |
| 2159 | } |
| 2160 | continue; |
| 2161 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2162 | case VisitorJob::TypeLocVisitKind: { |
| 2163 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2164 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2165 | return true; |
| 2166 | continue; |
| 2167 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2168 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2169 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2170 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2171 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2172 | TU))) { |
| 2173 | return true; |
| 2174 | } |
| 2175 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2176 | continue; |
| 2177 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2178 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2179 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2180 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2181 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2182 | return true; |
| 2183 | continue; |
| 2184 | } |
| 2185 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2186 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2187 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2188 | ->get())) |
| 2189 | return true; |
| 2190 | continue; |
| 2191 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2192 | case VisitorJob::MemberRefVisitKind: { |
| 2193 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2194 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2195 | return true; |
| 2196 | continue; |
| 2197 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2198 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2199 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2200 | if (!S) |
| 2201 | continue; |
| 2202 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2203 | // Update the current cursor. |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2204 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2205 | if (!IsInRegionOfInterest(Cursor)) |
| 2206 | continue; |
| 2207 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2208 | case CXChildVisit_Break: return true; |
| 2209 | case CXChildVisit_Continue: break; |
| 2210 | case CXChildVisit_Recurse: |
| 2211 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2212 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2213 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2214 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2215 | } |
| 2216 | case VisitorJob::MemberExprPartsKind: { |
| 2217 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2218 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2219 | |
| 2220 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2221 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2222 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2223 | return true; |
| 2224 | |
| 2225 | // Visit the declaration name. |
| 2226 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2227 | return true; |
| 2228 | |
| 2229 | // Visit the explicitly-specified template arguments, if any. |
| 2230 | if (M->hasExplicitTemplateArgs()) { |
| 2231 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2232 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2233 | Arg != ArgEnd; ++Arg) { |
| 2234 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2235 | return true; |
| 2236 | } |
| 2237 | } |
| 2238 | continue; |
| 2239 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2240 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2241 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2242 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2243 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2244 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2245 | return true; |
| 2246 | // Visit declaration name. |
| 2247 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2248 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2249 | continue; |
| 2250 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2251 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2252 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2253 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2254 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2255 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2256 | return true; |
| 2257 | // Visit the declaration name. |
| 2258 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2259 | return true; |
| 2260 | // Visit the overloaded declaration reference. |
| 2261 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2262 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2263 | continue; |
| 2264 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2265 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2266 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2267 | NamedDecl *Pack = E->getPack(); |
| 2268 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2269 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2270 | E->getPackLoc(), TU))) |
| 2271 | return true; |
| 2272 | |
| 2273 | continue; |
| 2274 | } |
| 2275 | |
| 2276 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2277 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2278 | E->getPackLoc(), TU))) |
| 2279 | return true; |
| 2280 | |
| 2281 | continue; |
| 2282 | } |
| 2283 | |
| 2284 | // Non-type template parameter packs and function parameter packs are |
| 2285 | // treated like DeclRefExpr cursors. |
| 2286 | continue; |
| 2287 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | return false; |
| 2291 | } |
| 2292 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2293 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2294 | VisitorWorkList *WL = 0; |
| 2295 | if (!WorkListFreeList.empty()) { |
| 2296 | WL = WorkListFreeList.back(); |
| 2297 | WL->clear(); |
| 2298 | WorkListFreeList.pop_back(); |
| 2299 | } |
| 2300 | else { |
| 2301 | WL = new VisitorWorkList(); |
| 2302 | WorkListCache.push_back(WL); |
| 2303 | } |
| 2304 | EnqueueWorkList(*WL, S); |
| 2305 | bool result = RunVisitorWorkList(*WL); |
| 2306 | WorkListFreeList.push_back(WL); |
| 2307 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2310 | namespace { |
| 2311 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2312 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2313 | const DeclarationNameInfo &NI, |
| 2314 | const SourceRange &QLoc, |
| 2315 | const ExplicitTemplateArgumentList *TemplateArgs = 0){ |
| 2316 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2317 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2318 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2319 | |
| 2320 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2321 | |
| 2322 | RefNamePieces Pieces; |
| 2323 | |
| 2324 | if (WantQualifier && QLoc.isValid()) |
| 2325 | Pieces.push_back(QLoc); |
| 2326 | |
| 2327 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2328 | Pieces.push_back(NI.getLoc()); |
| 2329 | |
| 2330 | if (WantTemplateArgs && TemplateArgs) |
| 2331 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2332 | TemplateArgs->RAngleLoc)); |
| 2333 | |
| 2334 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2335 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2336 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2337 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2338 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2339 | } |
| 2340 | |
| 2341 | if (WantSinglePiece) { |
| 2342 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2343 | Pieces.clear(); |
| 2344 | Pieces.push_back(R); |
| 2345 | } |
| 2346 | |
| 2347 | return Pieces; |
| 2348 | } |
| 2349 | } |
| 2350 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2351 | //===----------------------------------------------------------------------===// |
| 2352 | // Misc. API hooks. |
| 2353 | //===----------------------------------------------------------------------===// |
| 2354 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2355 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2356 | static bool EnabledMultithreading; |
| 2357 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2358 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2359 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2360 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2361 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2362 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2363 | llvm::DisablePrettyStackTrace = true; |
| 2364 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2365 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2366 | // enable it. |
| 2367 | llvm::CrashRecoveryContext::Enable(); |
| 2368 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2369 | // Enable support for multithreading in LLVM. |
| 2370 | { |
| 2371 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2372 | if (!EnabledMultithreading) { |
| 2373 | llvm::llvm_start_multithreaded(); |
| 2374 | EnabledMultithreading = true; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2378 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2379 | if (excludeDeclarationsFromPCH) |
| 2380 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2381 | if (displayDiagnostics) |
| 2382 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2383 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2386 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2387 | if (CIdx) |
| 2388 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2391 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2392 | if (isEnabled) |
| 2393 | llvm::CrashRecoveryContext::Enable(); |
| 2394 | else |
| 2395 | llvm::CrashRecoveryContext::Disable(); |
| 2396 | } |
| 2397 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2398 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2399 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2400 | if (!CIdx) |
| 2401 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2403 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2404 | FileSystemOptions FileSystemOpts; |
| 2405 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2406 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 2407 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2408 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2409 | CXXIdx->getOnlyLocalDecls(), |
| 2410 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2411 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2414 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2415 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2416 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2419 | CXTranslationUnit |
| 2420 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2421 | const char *source_filename, |
| 2422 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2423 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2424 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2425 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2426 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2427 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2428 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2429 | command_line_args, num_command_line_args, |
| 2430 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2431 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2432 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2433 | |
| 2434 | struct ParseTranslationUnitInfo { |
| 2435 | CXIndex CIdx; |
| 2436 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2437 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2438 | int num_command_line_args; |
| 2439 | struct CXUnsavedFile *unsaved_files; |
| 2440 | unsigned num_unsaved_files; |
| 2441 | unsigned options; |
| 2442 | CXTranslationUnit result; |
| 2443 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2444 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2445 | ParseTranslationUnitInfo *PTUI = |
| 2446 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2447 | CXIndex CIdx = PTUI->CIdx; |
| 2448 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2449 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2450 | int num_command_line_args = PTUI->num_command_line_args; |
| 2451 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2452 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2453 | unsigned options = PTUI->options; |
| 2454 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2456 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2457 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2458 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2459 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2460 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2461 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2462 | // FIXME: Add a flag for modules. |
| 2463 | TranslationUnitKind TUKind |
| 2464 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2465 | bool CacheCodeCompetionResults |
| 2466 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2467 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2468 | // Configure the diagnostics. |
| 2469 | DiagnosticOptions DiagOpts; |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2470 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 2471 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2472 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2473 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2474 | // Recover resources if we crash before exiting this function. |
| 2475 | llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic, |
| 2476 | llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> > |
| 2477 | DiagCleanup(Diags.getPtr()); |
| 2478 | |
| 2479 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2480 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2481 | |
| 2482 | // Recover resources if we crash before exiting this function. |
| 2483 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2484 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2485 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2486 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2487 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2488 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2489 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2490 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2491 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2492 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2493 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2494 | llvm::OwningPtr<std::vector<const char *> > |
| 2495 | Args(new std::vector<const char*>()); |
| 2496 | |
| 2497 | // Recover resources if we crash before exiting this method. |
| 2498 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2499 | ArgsCleanup(Args.get()); |
| 2500 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2501 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2502 | // (often very broken) source code, where spell-checking can have a |
| 2503 | // significant negative impact on performance (particularly when |
| 2504 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2505 | // Only do this if we haven't found a spell-checking-related argument. |
| 2506 | bool FoundSpellCheckingArgument = false; |
| 2507 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2508 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2509 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2510 | FoundSpellCheckingArgument = true; |
| 2511 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2512 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2513 | } |
| 2514 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2515 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2516 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2517 | Args->insert(Args->end(), command_line_args, |
| 2518 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2519 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2520 | // The 'source_filename' argument is optional. If the caller does not |
| 2521 | // specify it then it is assumed that the source file is specified |
| 2522 | // in the actual argument list. |
| 2523 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2524 | // present it will be unused. |
| 2525 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2526 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2528 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2529 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2530 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2531 | Args->push_back("-Xclang"); |
| 2532 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2533 | NestedMacroExpansions |
| 2534 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2537 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2538 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2539 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2540 | /* vector::data() not portable */, |
| 2541 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2542 | Diags, |
| 2543 | CXXIdx->getClangResourcesPath(), |
| 2544 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2545 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2546 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2547 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2548 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2549 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2550 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2551 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2552 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2553 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2554 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2555 | // Make sure to check that 'Unit' is non-NULL. |
| 2556 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2557 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2558 | DEnd = Unit->stored_diag_end(); |
| 2559 | D != DEnd; ++D) { |
| 2560 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2561 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2562 | clang_defaultDiagnosticDisplayOptions()); |
| 2563 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2564 | clang_disposeString(Msg); |
| 2565 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2566 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2567 | // On Windows, force a flush, since there may be multiple copies of |
| 2568 | // stderr and stdout in the file system, all with different buffers |
| 2569 | // but writing to the same device. |
| 2570 | fflush(stderr); |
| 2571 | #endif |
| 2572 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2573 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2574 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2575 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2576 | } |
| 2577 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2578 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2579 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2580 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2581 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2582 | unsigned num_unsaved_files, |
| 2583 | unsigned options) { |
| 2584 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2585 | num_command_line_args, unsaved_files, |
| 2586 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2587 | llvm::CrashRecoveryContext CRC; |
| 2588 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2589 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2590 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2591 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2592 | fprintf(stderr, " 'command_line_args' : ["); |
| 2593 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2594 | if (i) |
| 2595 | fprintf(stderr, ", "); |
| 2596 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2597 | } |
| 2598 | fprintf(stderr, "],\n"); |
| 2599 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2600 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2601 | if (i) |
| 2602 | fprintf(stderr, ", "); |
| 2603 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2604 | unsaved_files[i].Length); |
| 2605 | } |
| 2606 | fprintf(stderr, "],\n"); |
| 2607 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2608 | fprintf(stderr, "}\n"); |
| 2609 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2610 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2611 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2612 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2613 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2614 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2615 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2618 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2619 | return CXSaveTranslationUnit_None; |
| 2620 | } |
| 2621 | |
| 2622 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2623 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2624 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2625 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2627 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2628 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2629 | PrintLibclangResourceUsage(TU); |
| 2630 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2631 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2632 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2633 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2634 | if (CTUnit) { |
| 2635 | // If the translation unit has been marked as unsafe to free, just discard |
| 2636 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2637 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2638 | return; |
| 2639 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2640 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2641 | disposeCXStringPool(CTUnit->StringPool); |
| 2642 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2643 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2644 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2645 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2646 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2647 | return CXReparse_None; |
| 2648 | } |
| 2649 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2650 | struct ReparseTranslationUnitInfo { |
| 2651 | CXTranslationUnit TU; |
| 2652 | unsigned num_unsaved_files; |
| 2653 | struct CXUnsavedFile *unsaved_files; |
| 2654 | unsigned options; |
| 2655 | int result; |
| 2656 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2657 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2658 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2659 | ReparseTranslationUnitInfo *RTUI = |
| 2660 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2661 | CXTranslationUnit TU = RTUI->TU; |
| 2662 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2663 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2664 | unsigned options = RTUI->options; |
| 2665 | (void) options; |
| 2666 | RTUI->result = 1; |
| 2667 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2668 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2669 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2670 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2671 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2672 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2673 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2674 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2675 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2676 | |
| 2677 | // Recover resources if we crash before exiting this function. |
| 2678 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2679 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2680 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2681 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2682 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2683 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2684 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2685 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2686 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2689 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2690 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2691 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2692 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2693 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2694 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2695 | unsigned num_unsaved_files, |
| 2696 | struct CXUnsavedFile *unsaved_files, |
| 2697 | unsigned options) { |
| 2698 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2699 | options, 0 }; |
| 2700 | llvm::CrashRecoveryContext CRC; |
| 2701 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2702 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2703 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2704 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2705 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2706 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2707 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2708 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2709 | return RTUI.result; |
| 2710 | } |
| 2711 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2712 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2713 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2714 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2715 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2716 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2717 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2718 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2719 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2720 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2721 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2722 | CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2723 | return Result; |
| 2724 | } |
| 2725 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2726 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2727 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2728 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2729 | // CXSourceLocation and CXSourceRange Operations. |
| 2730 | //===----------------------------------------------------------------------===// |
| 2731 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2732 | extern "C" { |
| 2733 | CXSourceLocation clang_getNullLocation() { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2734 | CXSourceLocation Result = { { 0, 0 }, 0 }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2735 | return Result; |
| 2736 | } |
| 2737 | |
| 2738 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { |
Daniel Dunbar | 90a6b9e | 2010-01-30 23:58:27 +0000 | [diff] [blame] | 2739 | return (loc1.ptr_data[0] == loc2.ptr_data[0] && |
| 2740 | loc1.ptr_data[1] == loc2.ptr_data[1] && |
| 2741 | loc1.int_data == loc2.int_data); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, |
| 2745 | CXFile file, |
| 2746 | unsigned line, |
| 2747 | unsigned column) { |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2748 | if (!tu || !file) |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2749 | return clang_getNullLocation(); |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 2750 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2751 | bool Logging = ::getenv("LIBCLANG_LOGGING"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2752 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2753 | const FileEntry *File = static_cast<const FileEntry *>(file); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2754 | SourceLocation SLoc |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 2755 | = CXXUnit->getSourceManager().getLocation(File, line, column); |
| 2756 | if (SLoc.isInvalid()) { |
| 2757 | if (Logging) |
| 2758 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2759 | << "\", " << line << ", " << column << ") = invalid\n"; |
| 2760 | return clang_getNullLocation(); |
| 2761 | } |
| 2762 | |
| 2763 | if (Logging) |
| 2764 | llvm::errs() << "clang_getLocation(\"" << File->getName() |
| 2765 | << "\", " << line << ", " << column << ") = " |
| 2766 | << SLoc.getRawEncoding() << "\n"; |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2767 | |
| 2768 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
| 2769 | } |
| 2770 | |
| 2771 | CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, |
| 2772 | CXFile file, |
| 2773 | unsigned offset) { |
| 2774 | if (!tu || !file) |
| 2775 | return clang_getNullLocation(); |
| 2776 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2777 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
David Chisnall | 83889a7 | 2010-10-15 17:07:39 +0000 | [diff] [blame] | 2778 | SourceLocation Start |
| 2779 | = CXXUnit->getSourceManager().getLocation( |
| 2780 | static_cast<const FileEntry *>(file), |
| 2781 | 1, 1); |
| 2782 | if (Start.isInvalid()) return clang_getNullLocation(); |
| 2783 | |
| 2784 | SourceLocation SLoc = Start.getFileLocWithOffset(offset); |
| 2785 | |
| 2786 | if (SLoc.isInvalid()) return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2787 | |
Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 2788 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2791 | CXSourceRange clang_getNullRange() { |
| 2792 | CXSourceRange Result = { { 0, 0 }, 0, 0 }; |
| 2793 | return Result; |
| 2794 | } |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2796 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { |
| 2797 | if (begin.ptr_data[0] != end.ptr_data[0] || |
| 2798 | begin.ptr_data[1] != end.ptr_data[1]) |
| 2799 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2800 | |
| 2801 | CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2802 | begin.int_data, end.int_data }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2803 | return Result; |
| 2804 | } |
Douglas Gregor | ab4e83b | 2011-07-23 19:35:14 +0000 | [diff] [blame] | 2805 | |
| 2806 | unsigned clang_equalRanges(CXSourceRange range1, CXSourceRange range2) |
| 2807 | { |
| 2808 | return range1.ptr_data[0] == range2.ptr_data[0] |
| 2809 | && range1.ptr_data[1] == range2.ptr_data[1] |
| 2810 | && range1.begin_int_data == range2.begin_int_data |
| 2811 | && range1.end_int_data == range2.end_int_data; |
| 2812 | } |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2813 | } // end: extern "C" |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2814 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2815 | static void createNullLocation(CXFile *file, unsigned *line, |
| 2816 | unsigned *column, unsigned *offset) { |
| 2817 | if (file) |
| 2818 | *file = 0; |
| 2819 | if (line) |
| 2820 | *line = 0; |
| 2821 | if (column) |
| 2822 | *column = 0; |
| 2823 | if (offset) |
| 2824 | *offset = 0; |
| 2825 | return; |
| 2826 | } |
| 2827 | |
| 2828 | extern "C" { |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2829 | void clang_getExpansionLocation(CXSourceLocation location, |
| 2830 | CXFile *file, |
| 2831 | unsigned *line, |
| 2832 | unsigned *column, |
| 2833 | unsigned *offset) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2834 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2835 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2836 | if (!location.ptr_data[0] || Loc.isInvalid()) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2837 | createNullLocation(file, line, column, offset); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2838 | return; |
| 2839 | } |
| 2840 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2841 | const SourceManager &SM = |
| 2842 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2843 | SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2844 | |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 2845 | // Check that the FileID is invalid on the expansion location. |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2846 | // This can manifest in invalid code. |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2847 | FileID fileID = SM.getFileID(ExpansionLoc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 2848 | bool Invalid = false; |
| 2849 | const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid); |
| 2850 | if (!sloc.isFile() || Invalid) { |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2851 | createNullLocation(file, line, column, offset); |
| 2852 | return; |
| 2853 | } |
| 2854 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2855 | if (file) |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 2856 | *file = (void *)SM.getFileEntryForSLocEntry(sloc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2857 | if (line) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2858 | *line = SM.getExpansionLineNumber(ExpansionLoc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2859 | if (column) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2860 | *column = SM.getExpansionColumnNumber(ExpansionLoc); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2861 | if (offset) |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 2862 | *offset = SM.getDecomposedLoc(ExpansionLoc).second; |
| 2863 | } |
| 2864 | |
| 2865 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 2866 | CXFile *file, |
| 2867 | unsigned *line, |
| 2868 | unsigned *column, |
| 2869 | unsigned *offset) { |
| 2870 | // Redirect to new API. |
| 2871 | clang_getExpansionLocation(location, file, line, column, offset); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2874 | void clang_getSpellingLocation(CXSourceLocation location, |
| 2875 | CXFile *file, |
| 2876 | unsigned *line, |
| 2877 | unsigned *column, |
| 2878 | unsigned *offset) { |
| 2879 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 2880 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2881 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 2882 | return createNullLocation(file, line, column, offset); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2883 | |
| 2884 | const SourceManager &SM = |
| 2885 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
| 2886 | SourceLocation SpellLoc = Loc; |
| 2887 | if (SpellLoc.isMacroID()) { |
| 2888 | SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); |
| 2889 | if (SimpleSpellingLoc.isFileID() && |
| 2890 | SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) |
| 2891 | SpellLoc = SimpleSpellingLoc; |
| 2892 | else |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2893 | SpellLoc = SM.getExpansionLoc(SpellLoc); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); |
| 2897 | FileID FID = LocInfo.first; |
| 2898 | unsigned FileOffset = LocInfo.second; |
| 2899 | |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 2900 | if (FID.isInvalid()) |
| 2901 | return createNullLocation(file, line, column, offset); |
| 2902 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2903 | if (file) |
| 2904 | *file = (void *)SM.getFileEntryForID(FID); |
| 2905 | if (line) |
| 2906 | *line = SM.getLineNumber(FID, FileOffset); |
| 2907 | if (column) |
| 2908 | *column = SM.getColumnNumber(FID, FileOffset); |
| 2909 | if (offset) |
| 2910 | *offset = FileOffset; |
| 2911 | } |
| 2912 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2913 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2914 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2915 | range.begin_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2916 | return Result; |
| 2917 | } |
| 2918 | |
| 2919 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2920 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2921 | range.end_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2922 | return Result; |
| 2923 | } |
| 2924 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2925 | } // end: extern "C" |
| 2926 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2927 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2928 | // CXFile Operations. |
| 2929 | //===----------------------------------------------------------------------===// |
| 2930 | |
| 2931 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2932 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2933 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2934 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2935 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2936 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2937 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2941 | if (!SFile) |
| 2942 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2943 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2944 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2945 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2946 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2948 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2949 | if (!tu) |
| 2950 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2951 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2952 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2954 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2955 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2956 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2958 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2959 | if (!tu || !file) |
| 2960 | return 0; |
| 2961 | |
| 2962 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2963 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2964 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2965 | .isFileMultipleIncludeGuarded(FEnt); |
| 2966 | } |
| 2967 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2968 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2969 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2970 | //===----------------------------------------------------------------------===// |
| 2971 | // CXCursor Operations. |
| 2972 | //===----------------------------------------------------------------------===// |
| 2973 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2974 | static Decl *getDeclFromExpr(Stmt *E) { |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2975 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 2976 | return getDeclFromExpr(CE->getSubExpr()); |
| 2977 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2978 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2979 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2980 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2981 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2982 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2983 | return ME->getMemberDecl(); |
| 2984 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2985 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2986 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2987 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2988 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2989 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2990 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2991 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2992 | if (!CE->isElidable()) |
| 2993 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2994 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2995 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2996 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2997 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2998 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2999 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 3000 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 3001 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3002 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3003 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 3004 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 3005 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 3006 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3007 | return 0; |
| 3008 | } |
| 3009 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3010 | static SourceLocation getLocationFromExpr(Expr *E) { |
| 3011 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 3012 | return /*FIXME:*/Msg->getLeftLoc(); |
| 3013 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3014 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 3015 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 3016 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3017 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 3018 | return Member->getMemberLoc(); |
| 3019 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3020 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 3021 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 3022 | return SizeOfPack->getPackLoc(); |
| 3023 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 3024 | return E->getLocStart(); |
| 3025 | } |
| 3026 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3027 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3028 | |
| 3029 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3030 | CXCursorVisitor visitor, |
| 3031 | CXClientData client_data) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3032 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 3033 | getCursorASTUnit(parent)->getMaxPCHLevel(), |
| 3034 | false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 3035 | return CursorVis.VisitChildren(parent); |
| 3036 | } |
| 3037 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3038 | #ifndef __has_feature |
| 3039 | #define __has_feature(x) 0 |
| 3040 | #endif |
| 3041 | #if __has_feature(blocks) |
| 3042 | typedef enum CXChildVisitResult |
| 3043 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 3044 | |
| 3045 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3046 | CXClientData client_data) { |
| 3047 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3048 | return block(cursor, parent); |
| 3049 | } |
| 3050 | #else |
| 3051 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 3052 | // define and call the block manually, so the |
| 3053 | typedef struct _CXChildVisitResult |
| 3054 | { |
| 3055 | void *isa; |
| 3056 | int flags; |
| 3057 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3058 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 3059 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3060 | } *CXCursorVisitorBlock; |
| 3061 | |
| 3062 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3063 | CXClientData client_data) { |
| 3064 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3065 | return block->invoke(block, cursor, parent); |
| 3066 | } |
| 3067 | #endif |
| 3068 | |
| 3069 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3070 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 3071 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3072 | return clang_visitChildren(parent, visitWithBlock, block); |
| 3073 | } |
| 3074 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3075 | static CXString getDeclSpelling(Decl *D) { |
| 3076 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3077 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3078 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3079 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3080 | return createCXString(Property->getIdentifier()->getName()); |
| 3081 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3082 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3085 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3086 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3088 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 3089 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 3090 | // and returns different names. NamedDecl returns the class name and |
| 3091 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3092 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3094 | if (isa<UsingDirectiveDecl>(D)) |
| 3095 | return createCXString(""); |
| 3096 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 3097 | llvm::SmallString<1024> S; |
| 3098 | llvm::raw_svector_ostream os(S); |
| 3099 | ND->printName(os); |
| 3100 | |
| 3101 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3102 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3103 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3104 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3105 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3106 | return clang_getTranslationUnitSpelling( |
| 3107 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3108 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3109 | if (clang_isReference(C.kind)) { |
| 3110 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3111 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 3112 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3113 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3114 | } |
| 3115 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 3116 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3117 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3118 | } |
| 3119 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 3120 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3121 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3122 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3123 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3124 | case CXCursor_CXXBaseSpecifier: { |
| 3125 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 3126 | return createCXString(B->getType().getAsString()); |
| 3127 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3128 | case CXCursor_TypeRef: { |
| 3129 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 3130 | assert(Type && "Missing type decl"); |
| 3131 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3132 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 3133 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3134 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3135 | case CXCursor_TemplateRef: { |
| 3136 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3137 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3138 | |
| 3139 | return createCXString(Template->getNameAsString()); |
| 3140 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3141 | |
| 3142 | case CXCursor_NamespaceRef: { |
| 3143 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 3144 | assert(NS && "Missing namespace decl"); |
| 3145 | |
| 3146 | return createCXString(NS->getNameAsString()); |
| 3147 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3149 | case CXCursor_MemberRef: { |
| 3150 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3151 | assert(Field && "Missing member decl"); |
| 3152 | |
| 3153 | return createCXString(Field->getNameAsString()); |
| 3154 | } |
| 3155 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3156 | case CXCursor_LabelRef: { |
| 3157 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3158 | assert(Label && "Missing label"); |
| 3159 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3160 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3163 | case CXCursor_OverloadedDeclRef: { |
| 3164 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3165 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3166 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3167 | return createCXString(ND->getNameAsString()); |
| 3168 | return createCXString(""); |
| 3169 | } |
| 3170 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3171 | return createCXString(E->getName().getAsString()); |
| 3172 | OverloadedTemplateStorage *Ovl |
| 3173 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3174 | if (Ovl->size() == 0) |
| 3175 | return createCXString(""); |
| 3176 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3177 | } |
| 3178 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3179 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3180 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3181 | } |
| 3182 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3183 | |
| 3184 | if (clang_isExpression(C.kind)) { |
| 3185 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3186 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3187 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3188 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3191 | if (clang_isStatement(C.kind)) { |
| 3192 | Stmt *S = getCursorStmt(C); |
| 3193 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3194 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3195 | |
| 3196 | return createCXString(""); |
| 3197 | } |
| 3198 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3199 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3200 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3201 | ->getNameStart()); |
| 3202 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3203 | if (C.kind == CXCursor_MacroDefinition) |
| 3204 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3205 | ->getNameStart()); |
| 3206 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3207 | if (C.kind == CXCursor_InclusionDirective) |
| 3208 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3209 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3210 | if (clang_isDeclaration(C.kind)) |
| 3211 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3212 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3213 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3216 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3217 | if (!clang_isDeclaration(C.kind)) |
| 3218 | return clang_getCursorSpelling(C); |
| 3219 | |
| 3220 | Decl *D = getCursorDecl(C); |
| 3221 | if (!D) |
| 3222 | return createCXString(""); |
| 3223 | |
| 3224 | PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy; |
| 3225 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3226 | D = FunTmpl->getTemplatedDecl(); |
| 3227 | |
| 3228 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 3229 | llvm::SmallString<64> Str; |
| 3230 | llvm::raw_svector_ostream OS(Str); |
| 3231 | OS << Function->getNameAsString(); |
| 3232 | if (Function->getPrimaryTemplate()) |
| 3233 | OS << "<>"; |
| 3234 | OS << "("; |
| 3235 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3236 | if (I) |
| 3237 | OS << ", "; |
| 3238 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3239 | } |
| 3240 | |
| 3241 | if (Function->isVariadic()) { |
| 3242 | if (Function->getNumParams()) |
| 3243 | OS << ", "; |
| 3244 | OS << "..."; |
| 3245 | } |
| 3246 | OS << ")"; |
| 3247 | return createCXString(OS.str()); |
| 3248 | } |
| 3249 | |
| 3250 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 3251 | llvm::SmallString<64> Str; |
| 3252 | llvm::raw_svector_ostream OS(Str); |
| 3253 | OS << ClassTemplate->getNameAsString(); |
| 3254 | OS << "<"; |
| 3255 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3256 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3257 | if (I) |
| 3258 | OS << ", "; |
| 3259 | |
| 3260 | NamedDecl *Param = Params->getParam(I); |
| 3261 | if (Param->getIdentifier()) { |
| 3262 | OS << Param->getIdentifier()->getName(); |
| 3263 | continue; |
| 3264 | } |
| 3265 | |
| 3266 | // There is no parameter name, which makes this tricky. Try to come up |
| 3267 | // with something useful that isn't too long. |
| 3268 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3269 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3270 | else if (NonTypeTemplateParmDecl *NTTP |
| 3271 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3272 | OS << NTTP->getType().getAsString(Policy); |
| 3273 | else |
| 3274 | OS << "template<...> class"; |
| 3275 | } |
| 3276 | |
| 3277 | OS << ">"; |
| 3278 | return createCXString(OS.str()); |
| 3279 | } |
| 3280 | |
| 3281 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3282 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3283 | // If the type was explicitly written, use that. |
| 3284 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3285 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3286 | |
| 3287 | llvm::SmallString<64> Str; |
| 3288 | llvm::raw_svector_ostream OS(Str); |
| 3289 | OS << ClassSpec->getNameAsString(); |
| 3290 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3291 | ClassSpec->getTemplateArgs().data(), |
| 3292 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3293 | Policy); |
| 3294 | return createCXString(OS.str()); |
| 3295 | } |
| 3296 | |
| 3297 | return clang_getCursorSpelling(C); |
| 3298 | } |
| 3299 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3300 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3301 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3302 | case CXCursor_FunctionDecl: |
| 3303 | return createCXString("FunctionDecl"); |
| 3304 | case CXCursor_TypedefDecl: |
| 3305 | return createCXString("TypedefDecl"); |
| 3306 | case CXCursor_EnumDecl: |
| 3307 | return createCXString("EnumDecl"); |
| 3308 | case CXCursor_EnumConstantDecl: |
| 3309 | return createCXString("EnumConstantDecl"); |
| 3310 | case CXCursor_StructDecl: |
| 3311 | return createCXString("StructDecl"); |
| 3312 | case CXCursor_UnionDecl: |
| 3313 | return createCXString("UnionDecl"); |
| 3314 | case CXCursor_ClassDecl: |
| 3315 | return createCXString("ClassDecl"); |
| 3316 | case CXCursor_FieldDecl: |
| 3317 | return createCXString("FieldDecl"); |
| 3318 | case CXCursor_VarDecl: |
| 3319 | return createCXString("VarDecl"); |
| 3320 | case CXCursor_ParmDecl: |
| 3321 | return createCXString("ParmDecl"); |
| 3322 | case CXCursor_ObjCInterfaceDecl: |
| 3323 | return createCXString("ObjCInterfaceDecl"); |
| 3324 | case CXCursor_ObjCCategoryDecl: |
| 3325 | return createCXString("ObjCCategoryDecl"); |
| 3326 | case CXCursor_ObjCProtocolDecl: |
| 3327 | return createCXString("ObjCProtocolDecl"); |
| 3328 | case CXCursor_ObjCPropertyDecl: |
| 3329 | return createCXString("ObjCPropertyDecl"); |
| 3330 | case CXCursor_ObjCIvarDecl: |
| 3331 | return createCXString("ObjCIvarDecl"); |
| 3332 | case CXCursor_ObjCInstanceMethodDecl: |
| 3333 | return createCXString("ObjCInstanceMethodDecl"); |
| 3334 | case CXCursor_ObjCClassMethodDecl: |
| 3335 | return createCXString("ObjCClassMethodDecl"); |
| 3336 | case CXCursor_ObjCImplementationDecl: |
| 3337 | return createCXString("ObjCImplementationDecl"); |
| 3338 | case CXCursor_ObjCCategoryImplDecl: |
| 3339 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3340 | case CXCursor_CXXMethod: |
| 3341 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3342 | case CXCursor_UnexposedDecl: |
| 3343 | return createCXString("UnexposedDecl"); |
| 3344 | case CXCursor_ObjCSuperClassRef: |
| 3345 | return createCXString("ObjCSuperClassRef"); |
| 3346 | case CXCursor_ObjCProtocolRef: |
| 3347 | return createCXString("ObjCProtocolRef"); |
| 3348 | case CXCursor_ObjCClassRef: |
| 3349 | return createCXString("ObjCClassRef"); |
| 3350 | case CXCursor_TypeRef: |
| 3351 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3352 | case CXCursor_TemplateRef: |
| 3353 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3354 | case CXCursor_NamespaceRef: |
| 3355 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3356 | case CXCursor_MemberRef: |
| 3357 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3358 | case CXCursor_LabelRef: |
| 3359 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3360 | case CXCursor_OverloadedDeclRef: |
| 3361 | return createCXString("OverloadedDeclRef"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3362 | case CXCursor_UnexposedExpr: |
| 3363 | return createCXString("UnexposedExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3364 | case CXCursor_BlockExpr: |
| 3365 | return createCXString("BlockExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3366 | case CXCursor_DeclRefExpr: |
| 3367 | return createCXString("DeclRefExpr"); |
| 3368 | case CXCursor_MemberRefExpr: |
| 3369 | return createCXString("MemberRefExpr"); |
| 3370 | case CXCursor_CallExpr: |
| 3371 | return createCXString("CallExpr"); |
| 3372 | case CXCursor_ObjCMessageExpr: |
| 3373 | return createCXString("ObjCMessageExpr"); |
| 3374 | case CXCursor_UnexposedStmt: |
| 3375 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3376 | case CXCursor_LabelStmt: |
| 3377 | return createCXString("LabelStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3378 | case CXCursor_InvalidFile: |
| 3379 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3380 | case CXCursor_InvalidCode: |
| 3381 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3382 | case CXCursor_NoDeclFound: |
| 3383 | return createCXString("NoDeclFound"); |
| 3384 | case CXCursor_NotImplemented: |
| 3385 | return createCXString("NotImplemented"); |
| 3386 | case CXCursor_TranslationUnit: |
| 3387 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3388 | case CXCursor_UnexposedAttr: |
| 3389 | return createCXString("UnexposedAttr"); |
| 3390 | case CXCursor_IBActionAttr: |
| 3391 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3392 | case CXCursor_IBOutletAttr: |
| 3393 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3394 | case CXCursor_IBOutletCollectionAttr: |
| 3395 | return createCXString("attribute(iboutletcollection)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3396 | case CXCursor_PreprocessingDirective: |
| 3397 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3398 | case CXCursor_MacroDefinition: |
| 3399 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3400 | case CXCursor_MacroExpansion: |
| 3401 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3402 | case CXCursor_InclusionDirective: |
| 3403 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3404 | case CXCursor_Namespace: |
| 3405 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3406 | case CXCursor_LinkageSpec: |
| 3407 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3408 | case CXCursor_CXXBaseSpecifier: |
| 3409 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3410 | case CXCursor_Constructor: |
| 3411 | return createCXString("CXXConstructor"); |
| 3412 | case CXCursor_Destructor: |
| 3413 | return createCXString("CXXDestructor"); |
| 3414 | case CXCursor_ConversionFunction: |
| 3415 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3416 | case CXCursor_TemplateTypeParameter: |
| 3417 | return createCXString("TemplateTypeParameter"); |
| 3418 | case CXCursor_NonTypeTemplateParameter: |
| 3419 | return createCXString("NonTypeTemplateParameter"); |
| 3420 | case CXCursor_TemplateTemplateParameter: |
| 3421 | return createCXString("TemplateTemplateParameter"); |
| 3422 | case CXCursor_FunctionTemplate: |
| 3423 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3424 | case CXCursor_ClassTemplate: |
| 3425 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3426 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3427 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3428 | case CXCursor_NamespaceAlias: |
| 3429 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3430 | case CXCursor_UsingDirective: |
| 3431 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3432 | case CXCursor_UsingDeclaration: |
| 3433 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3434 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3435 | return createCXString("TypeAliasDecl"); |
| 3436 | case CXCursor_ObjCSynthesizeDecl: |
| 3437 | return createCXString("ObjCSynthesizeDecl"); |
| 3438 | case CXCursor_ObjCDynamicDecl: |
| 3439 | return createCXString("ObjCDynamicDecl"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3440 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3441 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3442 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3443 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3444 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3445 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3446 | struct GetCursorData { |
| 3447 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3448 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3449 | CXCursor &BestCursor; |
| 3450 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3451 | GetCursorData(SourceManager &SM, |
| 3452 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3453 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3454 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3455 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3456 | }; |
| 3457 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3458 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3459 | CXCursor parent, |
| 3460 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3461 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3462 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3463 | |
| 3464 | // If we point inside a macro argument we should provide info of what the |
| 3465 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3466 | // cursor. |
| 3467 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3468 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3469 | |
| 3470 | if (clang_isExpression(cursor.kind) && |
| 3471 | clang_isDeclaration(BestCursor->kind)) { |
| 3472 | Decl *D = getCursorDecl(*BestCursor); |
| 3473 | |
| 3474 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3475 | // when the expression source range overlaps the declaration range. |
| 3476 | // This can happen for C++ constructor expressions whose range generally |
| 3477 | // include the variable declaration, e.g.: |
| 3478 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3479 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3480 | D->getLocation() == Data->TokenBeginLoc) |
| 3481 | return CXChildVisit_Break; |
| 3482 | } |
| 3483 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3484 | // If our current best cursor is the construction of a temporary object, |
| 3485 | // don't replace that cursor with a type reference, because we want |
| 3486 | // clang_getCursor() to point at the constructor. |
| 3487 | if (clang_isExpression(BestCursor->kind) && |
| 3488 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 3489 | cursor.kind == CXCursor_TypeRef) |
| 3490 | return CXChildVisit_Recurse; |
| 3491 | |
Douglas Gregor | 85fe156 | 2010-12-10 07:23:11 +0000 | [diff] [blame] | 3492 | // Don't override a preprocessing cursor with another preprocessing |
| 3493 | // cursor; we want the outermost preprocessing cursor. |
| 3494 | if (clang_isPreprocessing(cursor.kind) && |
| 3495 | clang_isPreprocessing(BestCursor->kind)) |
| 3496 | return CXChildVisit_Recurse; |
| 3497 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3498 | *BestCursor = cursor; |
| 3499 | return CXChildVisit_Recurse; |
| 3500 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3502 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3503 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3504 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3505 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3506 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3507 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3508 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3509 | // Translate the given source location to make it point at the beginning of |
| 3510 | // the token under the cursor. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3511 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3512 | |
| 3513 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3514 | // of the following calls. |
| 3515 | if (SLoc.isInvalid()) |
| 3516 | return clang_getNullCursor(); |
| 3517 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3518 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3519 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3520 | CXXUnit->getASTContext().getLangOptions()); |
| 3521 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3522 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3523 | if (SLoc.isValid()) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3524 | // FIXME: Would be great to have a "hint" cursor, then walk from that |
| 3525 | // hint cursor upward until we find a cursor whose source range encloses |
| 3526 | // the region of interest, rather than starting from the translation unit. |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3527 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3528 | CXCursor Parent = clang_getTranslationUnitCursor(TU); |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3529 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 3530 | Decl::MaxPCHLevel, true, SourceLocation(SLoc)); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3531 | CursorVis.VisitChildren(Parent); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3532 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3533 | |
| 3534 | if (Logging) { |
| 3535 | CXFile SearchFile; |
| 3536 | unsigned SearchLine, SearchColumn; |
| 3537 | CXFile ResultFile; |
| 3538 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3539 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3540 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3541 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3542 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 3543 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3544 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3545 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3546 | SearchFileName = clang_getFileName(SearchFile); |
| 3547 | ResultFileName = clang_getFileName(ResultFile); |
| 3548 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3549 | USR = clang_getCursorUSR(Result); |
| 3550 | 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] | 3551 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3552 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3553 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3554 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3555 | clang_disposeString(SearchFileName); |
| 3556 | clang_disposeString(ResultFileName); |
| 3557 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3558 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3559 | |
| 3560 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3561 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3562 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3563 | CXString DefinitionKindSpelling |
| 3564 | = clang_getCursorKindSpelling(Definition.kind); |
| 3565 | CXFile DefinitionFile; |
| 3566 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame^] | 3567 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3568 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3569 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3570 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3571 | clang_getCString(DefinitionKindSpelling), |
| 3572 | clang_getCString(DefinitionFileName), |
| 3573 | DefinitionLine, DefinitionColumn); |
| 3574 | clang_disposeString(DefinitionFileName); |
| 3575 | clang_disposeString(DefinitionKindSpelling); |
| 3576 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3579 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3582 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3583 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3587 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3588 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3590 | unsigned clang_hashCursor(CXCursor C) { |
| 3591 | unsigned Index = 0; |
| 3592 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3593 | Index = 1; |
| 3594 | |
| 3595 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3596 | std::make_pair(C.kind, C.data[Index])); |
| 3597 | } |
| 3598 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3599 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3600 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3601 | } |
| 3602 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3603 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3604 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3605 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3606 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3607 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3608 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3609 | } |
| 3610 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3611 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3612 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3613 | } |
| 3614 | |
| 3615 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3616 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3617 | } |
| 3618 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3619 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3620 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3621 | } |
| 3622 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3623 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3624 | return K == CXCursor_TranslationUnit; |
| 3625 | } |
| 3626 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3627 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3628 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3629 | } |
| 3630 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3631 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3632 | switch (K) { |
| 3633 | case CXCursor_UnexposedDecl: |
| 3634 | case CXCursor_UnexposedExpr: |
| 3635 | case CXCursor_UnexposedStmt: |
| 3636 | case CXCursor_UnexposedAttr: |
| 3637 | return true; |
| 3638 | default: |
| 3639 | return false; |
| 3640 | } |
| 3641 | } |
| 3642 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3643 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3644 | return C.kind; |
| 3645 | } |
| 3646 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3647 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3648 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3649 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3650 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3651 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3652 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3653 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3656 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3657 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3658 | = getCursorObjCProtocolRef(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_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3663 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3664 | = getCursorObjCClassRef(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 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3667 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3668 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3669 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3670 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3671 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3672 | |
| 3673 | case CXCursor_TemplateRef: { |
| 3674 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3675 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3676 | } |
| 3677 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3678 | case CXCursor_NamespaceRef: { |
| 3679 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3680 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3681 | } |
| 3682 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3683 | case CXCursor_MemberRef: { |
| 3684 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3685 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3686 | } |
| 3687 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3688 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3689 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3690 | if (!BaseSpec) |
| 3691 | return clang_getNullLocation(); |
| 3692 | |
| 3693 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3694 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3695 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3696 | |
| 3697 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3698 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3699 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3701 | case CXCursor_LabelRef: { |
| 3702 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3703 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3704 | } |
| 3705 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3706 | case CXCursor_OverloadedDeclRef: |
| 3707 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3708 | getCursorOverloadedDeclRef(C).second); |
| 3709 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3710 | default: |
| 3711 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3712 | llvm_unreachable("Missed a reference kind"); |
| 3713 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3714 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3715 | |
| 3716 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3717 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3718 | getLocationFromExpr(getCursorExpr(C))); |
| 3719 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3720 | if (clang_isStatement(C.kind)) |
| 3721 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3722 | getCursorStmt(C)->getLocStart()); |
| 3723 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3724 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3725 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3726 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3727 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3728 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3729 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3730 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3731 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3732 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3733 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3734 | |
| 3735 | if (C.kind == CXCursor_MacroDefinition) { |
| 3736 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3737 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3738 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3739 | |
| 3740 | if (C.kind == CXCursor_InclusionDirective) { |
| 3741 | SourceLocation L |
| 3742 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3743 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3744 | } |
| 3745 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3746 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3747 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3748 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3749 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3750 | SourceLocation Loc = D->getLocation(); |
| 3751 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 3752 | Loc = Class->getClassLoc(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3753 | // FIXME: Multiple variables declared in a single declaration |
| 3754 | // currently lack the information needed to correctly determine their |
| 3755 | // ranges when accounting for the type-specifier. We use context |
| 3756 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3757 | // and if so, whether it is the first decl. |
| 3758 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3759 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3760 | Loc = VD->getLocation(); |
| 3761 | } |
| 3762 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3763 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3764 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3766 | } // end extern "C" |
| 3767 | |
| 3768 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3769 | if (clang_isReference(C.kind)) { |
| 3770 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3771 | case CXCursor_ObjCSuperClassRef: |
| 3772 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3774 | case CXCursor_ObjCProtocolRef: |
| 3775 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3777 | case CXCursor_ObjCClassRef: |
| 3778 | return getCursorObjCClassRef(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_TypeRef: |
| 3781 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3782 | |
| 3783 | case CXCursor_TemplateRef: |
| 3784 | return getCursorTemplateRef(C).second; |
| 3785 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3786 | case CXCursor_NamespaceRef: |
| 3787 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3788 | |
| 3789 | case CXCursor_MemberRef: |
| 3790 | return getCursorMemberRef(C).second; |
| 3791 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3792 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3793 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3795 | case CXCursor_LabelRef: |
| 3796 | return getCursorLabelRef(C).second; |
| 3797 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3798 | case CXCursor_OverloadedDeclRef: |
| 3799 | return getCursorOverloadedDeclRef(C).second; |
| 3800 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3801 | default: |
| 3802 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3803 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3804 | } |
| 3805 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3806 | |
| 3807 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3808 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3809 | |
| 3810 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3811 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3812 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3813 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3814 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3815 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3816 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3817 | return cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3818 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3819 | if (C.kind == CXCursor_MacroDefinition) |
| 3820 | return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3821 | |
| 3822 | if (C.kind == CXCursor_InclusionDirective) |
| 3823 | return cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3824 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3825 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3826 | Decl *D = cxcursor::getCursorDecl(C); |
| 3827 | SourceRange R = D->getSourceRange(); |
| 3828 | // FIXME: Multiple variables declared in a single declaration |
| 3829 | // currently lack the information needed to correctly determine their |
| 3830 | // ranges when accounting for the type-specifier. We use context |
| 3831 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3832 | // and if so, whether it is the first decl. |
| 3833 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3834 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3835 | R.setBegin(VD->getLocation()); |
| 3836 | } |
| 3837 | return R; |
| 3838 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3839 | return SourceRange(); |
| 3840 | } |
| 3841 | |
| 3842 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3843 | /// the decl-specifier-seq for declarations. |
| 3844 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3845 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3846 | Decl *D = cxcursor::getCursorDecl(C); |
| 3847 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3849 | // Adjust the start of the location for declarations preceded by |
| 3850 | // declaration specifiers. |
| 3851 | SourceLocation StartLoc; |
| 3852 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3853 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3854 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3855 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3856 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3857 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3858 | } |
| 3859 | |
| 3860 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 3861 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 3862 | R.setBegin(StartLoc); |
| 3863 | |
| 3864 | // FIXME: Multiple variables declared in a single declaration |
| 3865 | // currently lack the information needed to correctly determine their |
| 3866 | // ranges when accounting for the type-specifier. We use context |
| 3867 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3868 | // and if so, whether it is the first decl. |
| 3869 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3870 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3871 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | return R; |
| 3875 | } |
| 3876 | |
| 3877 | return getRawCursorExtent(C); |
| 3878 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3879 | |
| 3880 | extern "C" { |
| 3881 | |
| 3882 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 3883 | SourceRange R = getRawCursorExtent(C); |
| 3884 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3885 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3887 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3888 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3889 | |
| 3890 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3891 | if (clang_isInvalid(C.kind)) |
| 3892 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3893 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3894 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3895 | if (clang_isDeclaration(C.kind)) { |
| 3896 | Decl *D = getCursorDecl(C); |
| 3897 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3898 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3899 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3900 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3901 | if (ObjCForwardProtocolDecl *Protocols |
| 3902 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3903 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3904 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3905 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3906 | return MakeCXCursor(Property, tu); |
| 3907 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3908 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3911 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3912 | Expr *E = getCursorExpr(C); |
| 3913 | Decl *D = getDeclFromExpr(E); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3914 | if (D) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3915 | return MakeCXCursor(D, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3916 | |
| 3917 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3918 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3920 | return clang_getNullCursor(); |
| 3921 | } |
| 3922 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3923 | if (clang_isStatement(C.kind)) { |
| 3924 | Stmt *S = getCursorStmt(C); |
| 3925 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 3926 | if (LabelDecl *label = Goto->getLabel()) |
| 3927 | if (LabelStmt *labelS = label->getStmt()) |
| 3928 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3929 | |
| 3930 | return clang_getNullCursor(); |
| 3931 | } |
| 3932 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3933 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3934 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3935 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3938 | if (!clang_isReference(C.kind)) |
| 3939 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3941 | switch (C.kind) { |
| 3942 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3943 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3944 | |
| 3945 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3946 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3947 | |
| 3948 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3949 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3950 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3951 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3952 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3953 | |
| 3954 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3955 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3957 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3958 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3959 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3960 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3961 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3962 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3963 | case CXCursor_CXXBaseSpecifier: { |
| 3964 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 3965 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3966 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3967 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3968 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3969 | case CXCursor_LabelRef: |
| 3970 | // FIXME: We end up faking the "parent" declaration here because we |
| 3971 | // don't want to make CXCursor larger. |
| 3972 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3973 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 3974 | .getTranslationUnitDecl(), |
| 3975 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3977 | case CXCursor_OverloadedDeclRef: |
| 3978 | return C; |
| 3979 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3980 | default: |
| 3981 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 3982 | llvm_unreachable("Unhandled reference cursor kind"); |
| 3983 | break; |
| 3984 | } |
| 3985 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3987 | return clang_getNullCursor(); |
| 3988 | } |
| 3989 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3990 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3991 | if (clang_isInvalid(C.kind)) |
| 3992 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3993 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3994 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3995 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3996 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3997 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3998 | C = clang_getCursorReferenced(C); |
| 3999 | WasReference = true; |
| 4000 | } |
| 4001 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4002 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4003 | return clang_getCursorReferenced(C); |
| 4004 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4005 | if (!clang_isDeclaration(C.kind)) |
| 4006 | return clang_getNullCursor(); |
| 4007 | |
| 4008 | Decl *D = getCursorDecl(C); |
| 4009 | if (!D) |
| 4010 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4012 | switch (D->getKind()) { |
| 4013 | // Declaration kinds that don't really separate the notions of |
| 4014 | // declaration and definition. |
| 4015 | case Decl::Namespace: |
| 4016 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4017 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4018 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4019 | case Decl::TemplateTypeParm: |
| 4020 | case Decl::EnumConstant: |
| 4021 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4022 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4023 | case Decl::ObjCIvar: |
| 4024 | case Decl::ObjCAtDefsField: |
| 4025 | case Decl::ImplicitParam: |
| 4026 | case Decl::ParmVar: |
| 4027 | case Decl::NonTypeTemplateParm: |
| 4028 | case Decl::TemplateTemplateParm: |
| 4029 | case Decl::ObjCCategoryImpl: |
| 4030 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4031 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4032 | case Decl::LinkageSpec: |
| 4033 | case Decl::ObjCPropertyImpl: |
| 4034 | case Decl::FileScopeAsm: |
| 4035 | case Decl::StaticAssert: |
| 4036 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4037 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4038 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4039 | return C; |
| 4040 | |
| 4041 | // Declaration kinds that don't make any sense here, but are |
| 4042 | // nonetheless harmless. |
| 4043 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4044 | break; |
| 4045 | |
| 4046 | // Declaration kinds for which the definition is not resolvable. |
| 4047 | case Decl::UnresolvedUsingTypename: |
| 4048 | case Decl::UnresolvedUsingValue: |
| 4049 | break; |
| 4050 | |
| 4051 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4052 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4053 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4054 | |
| 4055 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4056 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4057 | |
| 4058 | case Decl::Enum: |
| 4059 | case Decl::Record: |
| 4060 | case Decl::CXXRecord: |
| 4061 | case Decl::ClassTemplateSpecialization: |
| 4062 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4063 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4064 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4065 | return clang_getNullCursor(); |
| 4066 | |
| 4067 | case Decl::Function: |
| 4068 | case Decl::CXXMethod: |
| 4069 | case Decl::CXXConstructor: |
| 4070 | case Decl::CXXDestructor: |
| 4071 | case Decl::CXXConversion: { |
| 4072 | const FunctionDecl *Def = 0; |
| 4073 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4074 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4075 | return clang_getNullCursor(); |
| 4076 | } |
| 4077 | |
| 4078 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4079 | // Ask the variable if it has a definition. |
| 4080 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4081 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4082 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4083 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4084 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4085 | case Decl::FunctionTemplate: { |
| 4086 | const FunctionDecl *Def = 0; |
| 4087 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4088 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4089 | return clang_getNullCursor(); |
| 4090 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4091 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4092 | case Decl::ClassTemplate: { |
| 4093 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4094 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4095 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4096 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4097 | return clang_getNullCursor(); |
| 4098 | } |
| 4099 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4100 | case Decl::Using: |
| 4101 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4102 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4103 | |
| 4104 | case Decl::UsingShadow: |
| 4105 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4106 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4107 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4108 | |
| 4109 | case Decl::ObjCMethod: { |
| 4110 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4111 | if (Method->isThisDeclarationADefinition()) |
| 4112 | return C; |
| 4113 | |
| 4114 | // Dig out the method definition in the associated |
| 4115 | // @implementation, if we have it. |
| 4116 | // FIXME: The ASTs should make finding the definition easier. |
| 4117 | if (ObjCInterfaceDecl *Class |
| 4118 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4119 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4120 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4121 | Method->isInstanceMethod())) |
| 4122 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4123 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4124 | |
| 4125 | return clang_getNullCursor(); |
| 4126 | } |
| 4127 | |
| 4128 | case Decl::ObjCCategory: |
| 4129 | if (ObjCCategoryImplDecl *Impl |
| 4130 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4131 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4132 | return clang_getNullCursor(); |
| 4133 | |
| 4134 | case Decl::ObjCProtocol: |
| 4135 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4136 | return C; |
| 4137 | return clang_getNullCursor(); |
| 4138 | |
| 4139 | case Decl::ObjCInterface: |
| 4140 | // There are two notions of a "definition" for an Objective-C |
| 4141 | // class: the interface and its implementation. When we resolved a |
| 4142 | // reference to an Objective-C class, produce the @interface as |
| 4143 | // the definition; when we were provided with the interface, |
| 4144 | // produce the @implementation as the definition. |
| 4145 | if (WasReference) { |
| 4146 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4147 | return C; |
| 4148 | } else if (ObjCImplementationDecl *Impl |
| 4149 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4150 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4151 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4153 | case Decl::ObjCProperty: |
| 4154 | // FIXME: We don't really know where to find the |
| 4155 | // ObjCPropertyImplDecls that implement this property. |
| 4156 | return clang_getNullCursor(); |
| 4157 | |
| 4158 | case Decl::ObjCCompatibleAlias: |
| 4159 | if (ObjCInterfaceDecl *Class |
| 4160 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4161 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4162 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4164 | return clang_getNullCursor(); |
| 4165 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4166 | case Decl::ObjCForwardProtocol: |
| 4167 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4168 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4169 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4170 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4171 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4172 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4173 | |
| 4174 | case Decl::Friend: |
| 4175 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4176 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4177 | return clang_getNullCursor(); |
| 4178 | |
| 4179 | case Decl::FriendTemplate: |
| 4180 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(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 | |
| 4185 | return clang_getNullCursor(); |
| 4186 | } |
| 4187 | |
| 4188 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4189 | if (!clang_isDeclaration(C.kind)) |
| 4190 | return 0; |
| 4191 | |
| 4192 | return clang_getCursorDefinition(C) == C; |
| 4193 | } |
| 4194 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4195 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4196 | if (!clang_isDeclaration(C.kind)) |
| 4197 | return C; |
| 4198 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4199 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4200 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4201 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4202 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4203 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4204 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4205 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4206 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4207 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4208 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4209 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4210 | |
| 4211 | return C; |
| 4212 | } |
| 4213 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4214 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4215 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4216 | return 0; |
| 4217 | |
| 4218 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4219 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4220 | return E->getNumDecls(); |
| 4221 | |
| 4222 | if (OverloadedTemplateStorage *S |
| 4223 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4224 | return S->size(); |
| 4225 | |
| 4226 | Decl *D = Storage.get<Decl*>(); |
| 4227 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4228 | return Using->shadow_size(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4229 | if (isa<ObjCClassDecl>(D)) |
| 4230 | return 1; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4231 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4232 | return Protocols->protocol_size(); |
| 4233 | |
| 4234 | return 0; |
| 4235 | } |
| 4236 | |
| 4237 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4238 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4239 | return clang_getNullCursor(); |
| 4240 | |
| 4241 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4242 | return clang_getNullCursor(); |
| 4243 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4244 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4245 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4246 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4247 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4248 | |
| 4249 | if (OverloadedTemplateStorage *S |
| 4250 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4251 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4252 | |
| 4253 | Decl *D = Storage.get<Decl*>(); |
| 4254 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4255 | // FIXME: This is, unfortunately, linear time. |
| 4256 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4257 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4258 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4259 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4260 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4261 | return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4262 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4263 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4264 | |
| 4265 | return clang_getNullCursor(); |
| 4266 | } |
| 4267 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4268 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4269 | const char **startBuf, |
| 4270 | const char **endBuf, |
| 4271 | unsigned *startLine, |
| 4272 | unsigned *startColumn, |
| 4273 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4274 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4275 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4276 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4277 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4278 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4279 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4280 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4281 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4282 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4283 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4284 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4285 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4286 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4287 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4289 | |
| 4290 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4291 | unsigned PieceIndex) { |
| 4292 | RefNamePieces Pieces; |
| 4293 | |
| 4294 | switch (C.kind) { |
| 4295 | case CXCursor_MemberRefExpr: |
| 4296 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4297 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4298 | E->getQualifierLoc().getSourceRange()); |
| 4299 | break; |
| 4300 | |
| 4301 | case CXCursor_DeclRefExpr: |
| 4302 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4303 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4304 | E->getQualifierLoc().getSourceRange(), |
| 4305 | E->getExplicitTemplateArgsOpt()); |
| 4306 | break; |
| 4307 | |
| 4308 | case CXCursor_CallExpr: |
| 4309 | if (CXXOperatorCallExpr *OCE = |
| 4310 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4311 | Expr *Callee = OCE->getCallee(); |
| 4312 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4313 | Callee = ICE->getSubExpr(); |
| 4314 | |
| 4315 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4316 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4317 | DRE->getQualifierLoc().getSourceRange()); |
| 4318 | } |
| 4319 | break; |
| 4320 | |
| 4321 | default: |
| 4322 | break; |
| 4323 | } |
| 4324 | |
| 4325 | if (Pieces.empty()) { |
| 4326 | if (PieceIndex == 0) |
| 4327 | return clang_getCursorExtent(C); |
| 4328 | } else if (PieceIndex < Pieces.size()) { |
| 4329 | SourceRange R = Pieces[PieceIndex]; |
| 4330 | if (R.isValid()) |
| 4331 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4332 | } |
| 4333 | |
| 4334 | return clang_getNullRange(); |
| 4335 | } |
| 4336 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4337 | void clang_enableStackTraces(void) { |
| 4338 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4339 | } |
| 4340 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4341 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4342 | unsigned stack_size) { |
| 4343 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4344 | } |
| 4345 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4346 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4347 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4348 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4349 | // Token-based Operations. |
| 4350 | //===----------------------------------------------------------------------===// |
| 4351 | |
| 4352 | /* CXToken layout: |
| 4353 | * int_data[0]: a CXTokenKind |
| 4354 | * int_data[1]: starting token location |
| 4355 | * int_data[2]: token length |
| 4356 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4357 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4358 | * otherwise unused. |
| 4359 | */ |
| 4360 | extern "C" { |
| 4361 | |
| 4362 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4363 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4364 | } |
| 4365 | |
| 4366 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4367 | switch (clang_getTokenKind(CXTok)) { |
| 4368 | case CXToken_Identifier: |
| 4369 | case CXToken_Keyword: |
| 4370 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4371 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4372 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4373 | |
| 4374 | case CXToken_Literal: { |
| 4375 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4376 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4377 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4378 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4380 | case CXToken_Punctuation: |
| 4381 | case CXToken_Comment: |
| 4382 | break; |
| 4383 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4384 | |
| 4385 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4386 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4387 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4388 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4389 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4390 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4391 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4392 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4393 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4394 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4395 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4396 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4397 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4398 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4399 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4400 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4401 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4403 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4404 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4405 | if (!CXXUnit) |
| 4406 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4408 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4409 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4410 | } |
| 4411 | |
| 4412 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4413 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4414 | if (!CXXUnit) |
| 4415 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4416 | |
| 4417 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4418 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4419 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4421 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4422 | CXToken **Tokens, unsigned *NumTokens) { |
| 4423 | if (Tokens) |
| 4424 | *Tokens = 0; |
| 4425 | if (NumTokens) |
| 4426 | *NumTokens = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4427 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4428 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4429 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4430 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4431 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 4432 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4433 | |
Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 4434 | SourceRange R = cxloc::translateCXSourceRange(Range); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4435 | if (R.isInvalid()) |
| 4436 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4438 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4439 | std::pair<FileID, unsigned> BeginLocInfo |
| 4440 | = SourceMgr.getDecomposedLoc(R.getBegin()); |
| 4441 | std::pair<FileID, unsigned> EndLocInfo |
| 4442 | = SourceMgr.getDecomposedLoc(R.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4444 | // Cannot tokenize across files. |
| 4445 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4446 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4447 | |
| 4448 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4449 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4450 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4451 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4452 | if (Invalid) |
| 4453 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4455 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4456 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4457 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4458 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4460 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4461 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4462 | SmallVector<CXToken, 32> CXTokens; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4463 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4464 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4465 | do { |
| 4466 | // Lex the next token |
| 4467 | Lex.LexFromRawLexer(Tok); |
| 4468 | if (Tok.is(tok::eof)) |
| 4469 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4471 | // Initialize the CXToken. |
| 4472 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4473 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4474 | // - Common fields |
| 4475 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4476 | CXTok.int_data[2] = Tok.getLength(); |
| 4477 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4479 | // - Kind-specific fields |
| 4480 | if (Tok.isLiteral()) { |
| 4481 | CXTok.int_data[0] = CXToken_Literal; |
| 4482 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4483 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4484 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4485 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4486 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4487 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4488 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4489 | CXTok.int_data[0] = CXToken_Keyword; |
| 4490 | } |
| 4491 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4492 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4493 | ? CXToken_Identifier |
| 4494 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4495 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4496 | CXTok.ptr_data = II; |
| 4497 | } else if (Tok.is(tok::comment)) { |
| 4498 | CXTok.int_data[0] = CXToken_Comment; |
| 4499 | CXTok.ptr_data = 0; |
| 4500 | } else { |
| 4501 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4502 | CXTok.ptr_data = 0; |
| 4503 | } |
| 4504 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4505 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4506 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4507 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4508 | if (CXTokens.empty()) |
| 4509 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4510 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4511 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4512 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4513 | *NumTokens = CXTokens.size(); |
| 4514 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4515 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4516 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4517 | CXToken *Tokens, unsigned NumTokens) { |
| 4518 | free(Tokens); |
| 4519 | } |
| 4520 | |
| 4521 | } // end: extern "C" |
| 4522 | |
| 4523 | //===----------------------------------------------------------------------===// |
| 4524 | // Token annotation APIs. |
| 4525 | //===----------------------------------------------------------------------===// |
| 4526 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4527 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4528 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4529 | CXCursor parent, |
| 4530 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4531 | namespace { |
| 4532 | class AnnotateTokensWorker { |
| 4533 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4534 | CXToken *Tokens; |
| 4535 | CXCursor *Cursors; |
| 4536 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4537 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4538 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4539 | CursorVisitor AnnotateVis; |
| 4540 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4541 | bool HasContextSensitiveKeywords; |
| 4542 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4543 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4544 | unsigned NextToken() const { return TokIdx; } |
| 4545 | void AdvanceToken() { ++TokIdx; } |
| 4546 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4547 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4548 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4549 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4550 | return Tokens[tokI].int_data[3] != 0; |
| 4551 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4552 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4553 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4554 | } |
| 4555 | |
| 4556 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4557 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4558 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4559 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4560 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4561 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4562 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4563 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4564 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4565 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4566 | AnnotateVis(tu, |
| 4567 | AnnotateTokensVisitor, this, |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 4568 | Decl::MaxPCHLevel, true, RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4569 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4570 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4571 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4572 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4573 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4574 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4575 | void AnnotateTokens() { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4576 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4577 | } |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4578 | |
| 4579 | /// \brief Determine whether the annotator saw any cursors that have |
| 4580 | /// context-sensitive keywords. |
| 4581 | bool hasContextSensitiveKeywords() const { |
| 4582 | return HasContextSensitiveKeywords; |
| 4583 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4584 | }; |
| 4585 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4586 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4587 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 4588 | // Walk the AST within the region of interest, annotating tokens |
| 4589 | // along the way. |
| 4590 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4591 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4592 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4593 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4594 | if (Pos != Annotated.end() && |
| 4595 | (clang_isInvalid(Cursors[I].kind) || |
| 4596 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4597 | Cursors[I] = Pos->second; |
| 4598 | } |
| 4599 | |
| 4600 | // Finish up annotating any tokens left. |
| 4601 | if (!MoreTokens()) |
| 4602 | return; |
| 4603 | |
| 4604 | const CXCursor &C = clang_getNullCursor(); |
| 4605 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 4606 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4607 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4608 | } |
| 4609 | } |
| 4610 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4611 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4612 | //// between the cursor location and the source range is the same as |
| 4613 | /// \arg compResult. |
| 4614 | /// |
| 4615 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4616 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4617 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4618 | RangeComparisonResult compResult, |
| 4619 | SourceRange range) { |
| 4620 | while (MoreTokens()) { |
| 4621 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4622 | if (isFunctionMacroToken(I)) |
| 4623 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4624 | |
| 4625 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4626 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4627 | Cursors[I] = updateC; |
| 4628 | AdvanceToken(); |
| 4629 | continue; |
| 4630 | } |
| 4631 | break; |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4636 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4637 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4638 | RangeComparisonResult compResult, |
| 4639 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4640 | assert(MoreTokens()); |
| 4641 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4642 | "Should be called only for macro arg tokens"); |
| 4643 | |
| 4644 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4645 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4646 | // advance the token index one by one until a token fails the range test. |
| 4647 | // We only advance once past all of the macro arg tokens if all of them |
| 4648 | // pass the range test. If one of them fails we keep the token index pointing |
| 4649 | // at the start of the macro arg tokens so that the failing token will be |
| 4650 | // annotated by a subsequent annotation try. |
| 4651 | |
| 4652 | bool atLeastOneCompFail = false; |
| 4653 | |
| 4654 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4655 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4656 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4657 | if (TokLoc.isFileID()) |
| 4658 | continue; // not macro arg token, it's parens or comma. |
| 4659 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4660 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4661 | Cursors[I] = updateC; |
| 4662 | } else |
| 4663 | atLeastOneCompFail = true; |
| 4664 | } |
| 4665 | |
| 4666 | if (!atLeastOneCompFail) |
| 4667 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4668 | } |
| 4669 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4670 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4671 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4672 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4673 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4674 | if (cursorRange.isInvalid()) |
| 4675 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4676 | |
| 4677 | if (!HasContextSensitiveKeywords) { |
| 4678 | // Objective-C properties can have context-sensitive keywords. |
| 4679 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4680 | if (ObjCPropertyDecl *Property |
| 4681 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4682 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4683 | } |
| 4684 | // Objective-C methods can have context-sensitive keywords. |
| 4685 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4686 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4687 | if (ObjCMethodDecl *Method |
| 4688 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4689 | if (Method->getObjCDeclQualifier()) |
| 4690 | HasContextSensitiveKeywords = true; |
| 4691 | else { |
| 4692 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4693 | PEnd = Method->param_end(); |
| 4694 | P != PEnd; ++P) { |
| 4695 | if ((*P)->getObjCDeclQualifier()) { |
| 4696 | HasContextSensitiveKeywords = true; |
| 4697 | break; |
| 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | // C++ methods can have context-sensitive keywords. |
| 4704 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4705 | if (CXXMethodDecl *Method |
| 4706 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4707 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4708 | HasContextSensitiveKeywords = true; |
| 4709 | } |
| 4710 | } |
| 4711 | // C++ classes can have context-sensitive keywords. |
| 4712 | else if (cursor.kind == CXCursor_StructDecl || |
| 4713 | cursor.kind == CXCursor_ClassDecl || |
| 4714 | cursor.kind == CXCursor_ClassTemplate || |
| 4715 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4716 | if (Decl *D = getCursorDecl(cursor)) |
| 4717 | if (D->hasAttr<FinalAttr>()) |
| 4718 | HasContextSensitiveKeywords = true; |
| 4719 | } |
| 4720 | } |
| 4721 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4722 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4723 | // For macro expansions, just note where the beginning of the macro |
| 4724 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4725 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4726 | Annotated[Loc.int_data] = cursor; |
| 4727 | return CXChildVisit_Recurse; |
| 4728 | } |
| 4729 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4730 | // Items in the preprocessing record are kept separate from items in |
| 4731 | // declarations, so we keep a separate token index. |
| 4732 | unsigned SavedTokIdx = TokIdx; |
| 4733 | TokIdx = PreprocessingTokIdx; |
| 4734 | |
| 4735 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4736 | // entry. |
| 4737 | while (MoreTokens()) { |
| 4738 | const unsigned I = NextToken(); |
| 4739 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4740 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4741 | case RangeBefore: |
| 4742 | AdvanceToken(); |
| 4743 | continue; |
| 4744 | case RangeAfter: |
| 4745 | case RangeOverlap: |
| 4746 | break; |
| 4747 | } |
| 4748 | break; |
| 4749 | } |
| 4750 | |
| 4751 | // Look at all of the tokens within this range. |
| 4752 | while (MoreTokens()) { |
| 4753 | const unsigned I = NextToken(); |
| 4754 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4755 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4756 | case RangeBefore: |
| 4757 | assert(0 && "Infeasible"); |
| 4758 | case RangeAfter: |
| 4759 | break; |
| 4760 | case RangeOverlap: |
| 4761 | Cursors[I] = cursor; |
| 4762 | AdvanceToken(); |
| 4763 | continue; |
| 4764 | } |
| 4765 | break; |
| 4766 | } |
| 4767 | |
| 4768 | // Save the preprocessing token index; restore the non-preprocessing |
| 4769 | // token index. |
| 4770 | PreprocessingTokIdx = TokIdx; |
| 4771 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4772 | return CXChildVisit_Recurse; |
| 4773 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4774 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4775 | if (cursorRange.isInvalid()) |
| 4776 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4777 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4778 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4779 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4780 | // Adjust the annotated range based specific declarations. |
| 4781 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4782 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4783 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4784 | |
| 4785 | SourceLocation StartLoc; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4786 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4787 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4788 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4789 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4790 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4791 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4792 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4793 | |
| 4794 | if (StartLoc.isValid() && L.isValid() && |
| 4795 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4796 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4797 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4798 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4799 | // If the location of the cursor occurs within a macro instantiation, record |
| 4800 | // the spelling location of the cursor in our annotation map. We can then |
| 4801 | // paper over the token labelings during a post-processing step to try and |
| 4802 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4803 | // instantiation. |
| 4804 | if (L.isMacroID()) { |
| 4805 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4806 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4807 | // directive. Here we assume that the default construction of CXCursor |
| 4808 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4809 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4810 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4811 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4812 | if (!clang_isPreprocessing(oldC.kind)) |
| 4813 | oldC = cursor; |
| 4814 | } |
| 4815 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4816 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4817 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4818 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4819 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4820 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4821 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4822 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4823 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4824 | // variable declaration that it belongs to. |
| 4825 | // This can happen for C++ constructor expressions whose range generally |
| 4826 | // include the variable declaration, e.g.: |
| 4827 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4828 | if (clang_isExpression(cursorK)) { |
| 4829 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4830 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4831 | const unsigned I = NextToken(); |
| 4832 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4833 | E->getLocStart() == D->getLocation() && |
| 4834 | E->getLocStart() == GetTokenLoc(I)) { |
| 4835 | Cursors[I] = updateC; |
| 4836 | AdvanceToken(); |
| 4837 | } |
| 4838 | } |
| 4839 | } |
| 4840 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4841 | // Visit children to get their cursor information. |
| 4842 | const unsigned BeforeChildren = NextToken(); |
| 4843 | VisitChildren(cursor); |
| 4844 | const unsigned AfterChildren = NextToken(); |
| 4845 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4846 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 4847 | // but the child cursors. |
| 4848 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4849 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4850 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 4851 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4852 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 4853 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4854 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4855 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4856 | Cursors[I] = cursor; |
| 4857 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4858 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4859 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4862 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4863 | CXCursor parent, |
| 4864 | CXClientData client_data) { |
| 4865 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 4866 | } |
| 4867 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4868 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4869 | |
| 4870 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 4871 | /// and mark tokens that are macro arguments. This info is used by the |
| 4872 | /// AnnotateTokensWorker. |
| 4873 | class MarkMacroArgTokensVisitor { |
| 4874 | SourceManager &SM; |
| 4875 | CXToken *Tokens; |
| 4876 | unsigned NumTokens; |
| 4877 | unsigned CurIdx; |
| 4878 | |
| 4879 | public: |
| 4880 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 4881 | CXToken *tokens, unsigned numTokens) |
| 4882 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 4883 | |
| 4884 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 4885 | if (cursor.kind != CXCursor_MacroExpansion) |
| 4886 | return CXChildVisit_Continue; |
| 4887 | |
| 4888 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 4889 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 4890 | return CXChildVisit_Continue; // it's not a function macro. |
| 4891 | |
| 4892 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4893 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 4894 | macroRange.getBegin())) |
| 4895 | break; |
| 4896 | } |
| 4897 | |
| 4898 | if (CurIdx == NumTokens) |
| 4899 | return CXChildVisit_Break; |
| 4900 | |
| 4901 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4902 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 4903 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 4904 | break; |
| 4905 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4906 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | if (CurIdx == NumTokens) |
| 4910 | return CXChildVisit_Break; |
| 4911 | |
| 4912 | return CXChildVisit_Continue; |
| 4913 | } |
| 4914 | |
| 4915 | private: |
| 4916 | SourceLocation getTokenLoc(unsigned tokI) { |
| 4917 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4918 | } |
| 4919 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4920 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4921 | // The third field is reserved and currently not used. Use it here |
| 4922 | // to mark macro arg expanded tokens with their expanded locations. |
| 4923 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 4924 | } |
| 4925 | }; |
| 4926 | |
| 4927 | } // end anonymous namespace |
| 4928 | |
| 4929 | static CXChildVisitResult |
| 4930 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 4931 | CXClientData client_data) { |
| 4932 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 4933 | parent); |
| 4934 | } |
| 4935 | |
| 4936 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4937 | struct clang_annotateTokens_Data { |
| 4938 | CXTranslationUnit TU; |
| 4939 | ASTUnit *CXXUnit; |
| 4940 | CXToken *Tokens; |
| 4941 | unsigned NumTokens; |
| 4942 | CXCursor *Cursors; |
| 4943 | }; |
| 4944 | } |
| 4945 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4946 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4947 | static void clang_annotateTokensImpl(void *UserData) { |
| 4948 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 4949 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 4950 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 4951 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 4952 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 4953 | |
| 4954 | // Determine the region of interest, which contains all of the tokens. |
| 4955 | SourceRange RegionOfInterest; |
| 4956 | RegionOfInterest.setBegin( |
| 4957 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 4958 | RegionOfInterest.setEnd( |
| 4959 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 4960 | Tokens[NumTokens-1]))); |
| 4961 | |
| 4962 | // A mapping from the source locations found when re-lexing or traversing the |
| 4963 | // region of interest to the corresponding cursors. |
| 4964 | AnnotateTokensData Annotated; |
| 4965 | |
| 4966 | // Relex the tokens within the source range to look for preprocessing |
| 4967 | // directives. |
| 4968 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4969 | std::pair<FileID, unsigned> BeginLocInfo |
| 4970 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 4971 | std::pair<FileID, unsigned> EndLocInfo |
| 4972 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 4973 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4974 | StringRef Buffer; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4975 | bool Invalid = false; |
| 4976 | if (BeginLocInfo.first == EndLocInfo.first && |
| 4977 | ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && |
| 4978 | !Invalid) { |
| 4979 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4980 | CXXUnit->getASTContext().getLangOptions(), |
| 4981 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 4982 | Buffer.end()); |
| 4983 | Lex.SetCommentRetentionState(true); |
| 4984 | |
| 4985 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 4986 | // entering #includes or expanding macros. |
| 4987 | while (true) { |
| 4988 | Token Tok; |
| 4989 | Lex.LexFromRawLexer(Tok); |
| 4990 | |
| 4991 | reprocess: |
| 4992 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 4993 | // We have found a preprocessing directive. Gobble it up so that we |
| 4994 | // don't see it while preprocessing these tokens later, but keep track |
| 4995 | // of all of the token locations inside this preprocessing directive so |
| 4996 | // that we can annotate them appropriately. |
| 4997 | // |
| 4998 | // FIXME: Some simple tests here could identify macro definitions and |
| 4999 | // #undefs, to provide specific cursor kinds for those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5000 | SmallVector<SourceLocation, 32> Locations; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5001 | do { |
| 5002 | Locations.push_back(Tok.getLocation()); |
| 5003 | Lex.LexFromRawLexer(Tok); |
| 5004 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5005 | |
| 5006 | using namespace cxcursor; |
| 5007 | CXCursor Cursor |
| 5008 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5009 | Locations.back()), |
| 5010 | TU); |
| 5011 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5012 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5013 | } |
| 5014 | |
| 5015 | if (Tok.isAtStartOfLine()) |
| 5016 | goto reprocess; |
| 5017 | |
| 5018 | continue; |
| 5019 | } |
| 5020 | |
| 5021 | if (Tok.is(tok::eof)) |
| 5022 | break; |
| 5023 | } |
| 5024 | } |
| 5025 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5026 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5027 | // Search and mark tokens that are macro argument expansions. |
| 5028 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5029 | Tokens, NumTokens); |
| 5030 | CursorVisitor MacroArgMarker(TU, |
| 5031 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 5032 | Decl::MaxPCHLevel, true, RegionOfInterest); |
| 5033 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5034 | } |
| 5035 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5036 | // Annotate all of the source locations in the region of interest that map to |
| 5037 | // a specific cursor. |
| 5038 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5039 | TU, RegionOfInterest); |
| 5040 | |
| 5041 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5042 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5043 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5044 | // algorithm back into a traditional recursion by explicitly calling |
| 5045 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5046 | W.AnnotateTokens(); |
| 5047 | |
| 5048 | // If we ran into any entities that involve context-sensitive keywords, |
| 5049 | // take another pass through the tokens to mark them as such. |
| 5050 | if (W.hasContextSensitiveKeywords()) { |
| 5051 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5052 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5053 | continue; |
| 5054 | |
| 5055 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5056 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5057 | if (ObjCPropertyDecl *Property |
| 5058 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5059 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5060 | llvm::StringSwitch<bool>(II->getName()) |
| 5061 | .Case("readonly", true) |
| 5062 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5063 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5064 | .Case("readwrite", true) |
| 5065 | .Case("retain", true) |
| 5066 | .Case("copy", true) |
| 5067 | .Case("nonatomic", true) |
| 5068 | .Case("atomic", true) |
| 5069 | .Case("getter", true) |
| 5070 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5071 | .Case("strong", true) |
| 5072 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5073 | .Default(false)) |
| 5074 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5075 | } |
| 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5080 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5081 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5082 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5083 | .Case("in", true) |
| 5084 | .Case("out", true) |
| 5085 | .Case("inout", true) |
| 5086 | .Case("oneway", true) |
| 5087 | .Case("bycopy", true) |
| 5088 | .Case("byref", true) |
| 5089 | .Default(false)) |
| 5090 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5091 | continue; |
| 5092 | } |
| 5093 | |
| 5094 | if (Cursors[I].kind == CXCursor_CXXMethod) { |
| 5095 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5096 | if (CXXMethodDecl *Method |
| 5097 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(Cursors[I]))) { |
| 5098 | if ((Method->hasAttr<FinalAttr>() || |
| 5099 | Method->hasAttr<OverrideAttr>()) && |
| 5100 | Method->getLocation().getRawEncoding() != Tokens[I].int_data[1] && |
| 5101 | llvm::StringSwitch<bool>(II->getName()) |
| 5102 | .Case("final", true) |
| 5103 | .Case("override", true) |
| 5104 | .Default(false)) |
| 5105 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5106 | } |
| 5107 | continue; |
| 5108 | } |
| 5109 | |
| 5110 | if (Cursors[I].kind == CXCursor_ClassDecl || |
| 5111 | Cursors[I].kind == CXCursor_StructDecl || |
| 5112 | Cursors[I].kind == CXCursor_ClassTemplate) { |
| 5113 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5114 | if (II->getName() == "final") { |
| 5115 | // We have to be careful with 'final', since it could be the name |
| 5116 | // of a member class rather than the context-sensitive keyword. |
| 5117 | // So, check whether the cursor associated with this |
| 5118 | Decl *D = getCursorDecl(Cursors[I]); |
| 5119 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(D)) { |
| 5120 | if ((Record->hasAttr<FinalAttr>()) && |
| 5121 | Record->getIdentifier() != II) |
| 5122 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5123 | } else if (ClassTemplateDecl *ClassTemplate |
| 5124 | = dyn_cast_or_null<ClassTemplateDecl>(D)) { |
| 5125 | CXXRecordDecl *Record = ClassTemplate->getTemplatedDecl(); |
| 5126 | if ((Record->hasAttr<FinalAttr>()) && |
| 5127 | Record->getIdentifier() != II) |
| 5128 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5129 | } |
| 5130 | } |
| 5131 | continue; |
| 5132 | } |
| 5133 | } |
| 5134 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5137 | extern "C" { |
| 5138 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5139 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5140 | CXToken *Tokens, unsigned NumTokens, |
| 5141 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5142 | |
| 5143 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5144 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5146 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5147 | CXCursor C = clang_getNullCursor(); |
| 5148 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5149 | Cursors[I] = C; |
| 5150 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5151 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5152 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5153 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5154 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5155 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5156 | |
| 5157 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5158 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5159 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5160 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5161 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5162 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5163 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5165 | } // end: extern "C" |
| 5166 | |
| 5167 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5168 | // Operations for querying linkage of a cursor. |
| 5169 | //===----------------------------------------------------------------------===// |
| 5170 | |
| 5171 | extern "C" { |
| 5172 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5173 | if (!clang_isDeclaration(cursor.kind)) |
| 5174 | return CXLinkage_Invalid; |
| 5175 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5176 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5177 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5178 | switch (ND->getLinkage()) { |
| 5179 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5180 | case InternalLinkage: return CXLinkage_Internal; |
| 5181 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5182 | case ExternalLinkage: return CXLinkage_External; |
| 5183 | }; |
| 5184 | |
| 5185 | return CXLinkage_Invalid; |
| 5186 | } |
| 5187 | } // end: extern "C" |
| 5188 | |
| 5189 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5190 | // Operations for querying language of a cursor. |
| 5191 | //===----------------------------------------------------------------------===// |
| 5192 | |
| 5193 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 5194 | switch (D->getKind()) { |
| 5195 | default: |
| 5196 | break; |
| 5197 | case Decl::ImplicitParam: |
| 5198 | case Decl::ObjCAtDefsField: |
| 5199 | case Decl::ObjCCategory: |
| 5200 | case Decl::ObjCCategoryImpl: |
| 5201 | case Decl::ObjCClass: |
| 5202 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5203 | case Decl::ObjCForwardProtocol: |
| 5204 | case Decl::ObjCImplementation: |
| 5205 | case Decl::ObjCInterface: |
| 5206 | case Decl::ObjCIvar: |
| 5207 | case Decl::ObjCMethod: |
| 5208 | case Decl::ObjCProperty: |
| 5209 | case Decl::ObjCPropertyImpl: |
| 5210 | case Decl::ObjCProtocol: |
| 5211 | return CXLanguage_ObjC; |
| 5212 | case Decl::CXXConstructor: |
| 5213 | case Decl::CXXConversion: |
| 5214 | case Decl::CXXDestructor: |
| 5215 | case Decl::CXXMethod: |
| 5216 | case Decl::CXXRecord: |
| 5217 | case Decl::ClassTemplate: |
| 5218 | case Decl::ClassTemplatePartialSpecialization: |
| 5219 | case Decl::ClassTemplateSpecialization: |
| 5220 | case Decl::Friend: |
| 5221 | case Decl::FriendTemplate: |
| 5222 | case Decl::FunctionTemplate: |
| 5223 | case Decl::LinkageSpec: |
| 5224 | case Decl::Namespace: |
| 5225 | case Decl::NamespaceAlias: |
| 5226 | case Decl::NonTypeTemplateParm: |
| 5227 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5228 | case Decl::TemplateTemplateParm: |
| 5229 | case Decl::TemplateTypeParm: |
| 5230 | case Decl::UnresolvedUsingTypename: |
| 5231 | case Decl::UnresolvedUsingValue: |
| 5232 | case Decl::Using: |
| 5233 | case Decl::UsingDirective: |
| 5234 | case Decl::UsingShadow: |
| 5235 | return CXLanguage_CPlusPlus; |
| 5236 | } |
| 5237 | |
| 5238 | return CXLanguage_C; |
| 5239 | } |
| 5240 | |
| 5241 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5242 | |
| 5243 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5244 | if (clang_isDeclaration(cursor.kind)) |
| 5245 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5246 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5247 | return CXAvailability_Available; |
| 5248 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5249 | switch (D->getAvailability()) { |
| 5250 | case AR_Available: |
| 5251 | case AR_NotYetIntroduced: |
| 5252 | return CXAvailability_Available; |
| 5253 | |
| 5254 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5255 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5256 | |
| 5257 | case AR_Unavailable: |
| 5258 | return CXAvailability_NotAvailable; |
| 5259 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5260 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5262 | return CXAvailability_Available; |
| 5263 | } |
| 5264 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5265 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5266 | if (clang_isDeclaration(cursor.kind)) |
| 5267 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5268 | |
| 5269 | return CXLanguage_Invalid; |
| 5270 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5271 | |
| 5272 | /// \brief If the given cursor is the "templated" declaration |
| 5273 | /// descibing a class or function template, return the class or |
| 5274 | /// function template. |
| 5275 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5276 | if (!D) |
| 5277 | return 0; |
| 5278 | |
| 5279 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5280 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5281 | return FunTmpl; |
| 5282 | |
| 5283 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5284 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5285 | return ClassTmpl; |
| 5286 | |
| 5287 | return D; |
| 5288 | } |
| 5289 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5290 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5291 | if (clang_isDeclaration(cursor.kind)) { |
| 5292 | if (Decl *D = getCursorDecl(cursor)) { |
| 5293 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5294 | if (!DC) |
| 5295 | return clang_getNullCursor(); |
| 5296 | |
| 5297 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5298 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | |
| 5302 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5303 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5304 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | return clang_getNullCursor(); |
| 5308 | } |
| 5309 | |
| 5310 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5311 | if (clang_isDeclaration(cursor.kind)) { |
| 5312 | if (Decl *D = getCursorDecl(cursor)) { |
| 5313 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5314 | if (!DC) |
| 5315 | return clang_getNullCursor(); |
| 5316 | |
| 5317 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5318 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5319 | } |
| 5320 | } |
| 5321 | |
| 5322 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5323 | // statement or expression, so we return nothing. |
| 5324 | return clang_getNullCursor(); |
| 5325 | } |
| 5326 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5327 | static void CollectOverriddenMethods(DeclContext *Ctx, |
| 5328 | ObjCMethodDecl *Method, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5329 | SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5330 | if (!Ctx) |
| 5331 | return; |
| 5332 | |
| 5333 | // If we have a class or category implementation, jump straight to the |
| 5334 | // interface. |
| 5335 | if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) |
| 5336 | return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods); |
| 5337 | |
| 5338 | ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); |
| 5339 | if (!Container) |
| 5340 | return; |
| 5341 | |
| 5342 | // Check whether we have a matching method at this level. |
| 5343 | if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), |
| 5344 | Method->isInstanceMethod())) |
| 5345 | if (Method != Overridden) { |
| 5346 | // We found an override at this level; there is no need to look |
| 5347 | // into other protocols or categories. |
| 5348 | Methods.push_back(Overridden); |
| 5349 | return; |
| 5350 | } |
| 5351 | |
| 5352 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5353 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 5354 | PEnd = Protocol->protocol_end(); |
| 5355 | P != PEnd; ++P) |
| 5356 | CollectOverriddenMethods(*P, Method, Methods); |
| 5357 | } |
| 5358 | |
| 5359 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5360 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 5361 | PEnd = Category->protocol_end(); |
| 5362 | P != PEnd; ++P) |
| 5363 | CollectOverriddenMethods(*P, Method, Methods); |
| 5364 | } |
| 5365 | |
| 5366 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5367 | for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), |
| 5368 | PEnd = Interface->protocol_end(); |
| 5369 | P != PEnd; ++P) |
| 5370 | CollectOverriddenMethods(*P, Method, Methods); |
| 5371 | |
| 5372 | for (ObjCCategoryDecl *Category = Interface->getCategoryList(); |
| 5373 | Category; Category = Category->getNextClassCategory()) |
| 5374 | CollectOverriddenMethods(Category, Method, Methods); |
| 5375 | |
| 5376 | // We only look into the superclass if we haven't found anything yet. |
| 5377 | if (Methods.empty()) |
| 5378 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) |
| 5379 | return CollectOverriddenMethods(Super, Method, Methods); |
| 5380 | } |
| 5381 | } |
| 5382 | |
| 5383 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5384 | CXCursor **overridden, |
| 5385 | unsigned *num_overridden) { |
| 5386 | if (overridden) |
| 5387 | *overridden = 0; |
| 5388 | if (num_overridden) |
| 5389 | *num_overridden = 0; |
| 5390 | if (!overridden || !num_overridden) |
| 5391 | return; |
| 5392 | |
| 5393 | if (!clang_isDeclaration(cursor.kind)) |
| 5394 | return; |
| 5395 | |
| 5396 | Decl *D = getCursorDecl(cursor); |
| 5397 | if (!D) |
| 5398 | return; |
| 5399 | |
| 5400 | // Handle C++ member functions. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5401 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5402 | if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
| 5403 | *num_overridden = CXXMethod->size_overridden_methods(); |
| 5404 | if (!*num_overridden) |
| 5405 | return; |
| 5406 | |
| 5407 | *overridden = new CXCursor [*num_overridden]; |
| 5408 | unsigned I = 0; |
| 5409 | for (CXXMethodDecl::method_iterator |
| 5410 | M = CXXMethod->begin_overridden_methods(), |
| 5411 | MEnd = CXXMethod->end_overridden_methods(); |
| 5412 | M != MEnd; (void)++M, ++I) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5413 | (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5414 | return; |
| 5415 | } |
| 5416 | |
| 5417 | ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 5418 | if (!Method) |
| 5419 | return; |
| 5420 | |
| 5421 | // Handle Objective-C methods. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5422 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5423 | CollectOverriddenMethods(Method->getDeclContext(), Method, Methods); |
| 5424 | |
| 5425 | if (Methods.empty()) |
| 5426 | return; |
| 5427 | |
| 5428 | *num_overridden = Methods.size(); |
| 5429 | *overridden = new CXCursor [Methods.size()]; |
| 5430 | for (unsigned I = 0, N = Methods.size(); I != N; ++I) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5431 | (*overridden)[I] = MakeCXCursor(Methods[I], TU); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5435 | delete [] overridden; |
| 5436 | } |
| 5437 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5438 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5439 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5440 | return 0; |
| 5441 | |
| 5442 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5443 | return (void *)ID->getFile(); |
| 5444 | } |
| 5445 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5446 | } // end: extern "C" |
| 5447 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5448 | |
| 5449 | //===----------------------------------------------------------------------===// |
| 5450 | // C++ AST instrospection. |
| 5451 | //===----------------------------------------------------------------------===// |
| 5452 | |
| 5453 | extern "C" { |
| 5454 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5455 | if (!clang_isDeclaration(C.kind)) |
| 5456 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5457 | |
| 5458 | CXXMethodDecl *Method = 0; |
| 5459 | Decl *D = cxcursor::getCursorDecl(C); |
| 5460 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5461 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5462 | else |
| 5463 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5464 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5465 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5466 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5467 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5468 | if (!clang_isDeclaration(C.kind)) |
| 5469 | return 0; |
| 5470 | |
| 5471 | CXXMethodDecl *Method = 0; |
| 5472 | Decl *D = cxcursor::getCursorDecl(C); |
| 5473 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5474 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5475 | else |
| 5476 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5477 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5478 | } |
| 5479 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5480 | } // end: extern "C" |
| 5481 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5482 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5483 | // Attribute introspection. |
| 5484 | //===----------------------------------------------------------------------===// |
| 5485 | |
| 5486 | extern "C" { |
| 5487 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5488 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5489 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5490 | |
| 5491 | IBOutletCollectionAttr *A = |
| 5492 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5493 | |
Douglas Gregor | 841b238 | 2011-03-06 18:55:32 +0000 | [diff] [blame] | 5494 | return cxtype::MakeCXType(A->getInterFace(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5495 | } |
| 5496 | } // end: extern "C" |
| 5497 | |
| 5498 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5499 | // Inspecting memory usage. |
| 5500 | //===----------------------------------------------------------------------===// |
| 5501 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5502 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5503 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5504 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5505 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5506 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5507 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5508 | entries.push_back(entry); |
| 5509 | } |
| 5510 | |
| 5511 | extern "C" { |
| 5512 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5513 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5514 | const char *str = ""; |
| 5515 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5516 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5517 | str = "ASTContext: expressions, declarations, and types"; |
| 5518 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5519 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5520 | str = "ASTContext: identifiers"; |
| 5521 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5522 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5523 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5524 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5525 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5526 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5527 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5528 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5529 | str = "SourceManager: content cache allocator"; |
| 5530 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5531 | case CXTUResourceUsage_AST_SideTables: |
| 5532 | str = "ASTContext: side tables"; |
| 5533 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5534 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5535 | str = "SourceManager: malloc'ed memory buffers"; |
| 5536 | break; |
| 5537 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5538 | str = "SourceManager: mmap'ed memory buffers"; |
| 5539 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5540 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5541 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5542 | break; |
| 5543 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5544 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5545 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5546 | case CXTUResourceUsage_Preprocessor: |
| 5547 | str = "Preprocessor: malloc'ed memory"; |
| 5548 | break; |
| 5549 | case CXTUResourceUsage_PreprocessingRecord: |
| 5550 | str = "Preprocessor: PreprocessingRecord"; |
| 5551 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5552 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5553 | str = "SourceManager: data structures and tables"; |
| 5554 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5555 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5556 | str = "Preprocessor: header search tables"; |
| 5557 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5558 | } |
| 5559 | return str; |
| 5560 | } |
| 5561 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5562 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5563 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5564 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5565 | return usage; |
| 5566 | } |
| 5567 | |
| 5568 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5569 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5570 | ASTContext &astContext = astUnit->getASTContext(); |
| 5571 | |
| 5572 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5573 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5574 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5575 | |
| 5576 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5577 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5578 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5579 | |
| 5580 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5581 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5582 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5583 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5584 | // How much memory is used by ASTContext's side tables? |
| 5585 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5586 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5587 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5588 | // How much memory is used for caching global code completion results? |
| 5589 | unsigned long completionBytes = 0; |
| 5590 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5591 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5592 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5593 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5594 | createCXTUResourceUsageEntry(*entries, |
| 5595 | CXTUResourceUsage_GlobalCompletionResults, |
| 5596 | completionBytes); |
| 5597 | |
| 5598 | // How much memory is being used by SourceManager's content cache? |
| 5599 | createCXTUResourceUsageEntry(*entries, |
| 5600 | CXTUResourceUsage_SourceManagerContentCache, |
| 5601 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5602 | |
| 5603 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5604 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5605 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5606 | |
| 5607 | createCXTUResourceUsageEntry(*entries, |
| 5608 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5609 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5610 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5611 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5612 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5613 | createCXTUResourceUsageEntry(*entries, |
| 5614 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5615 | (unsigned long) astContext.getSourceManager() |
| 5616 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5617 | |
| 5618 | // How much memory is being used by the ExternalASTSource? |
| 5619 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5620 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5621 | esrc->getMemoryBufferSizes(); |
| 5622 | |
| 5623 | createCXTUResourceUsageEntry(*entries, |
| 5624 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5625 | (unsigned long) sizes.malloc_bytes); |
| 5626 | createCXTUResourceUsageEntry(*entries, |
| 5627 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5628 | (unsigned long) sizes.mmap_bytes); |
| 5629 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5630 | |
| 5631 | // How much memory is being used by the Preprocessor? |
| 5632 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5633 | createCXTUResourceUsageEntry(*entries, |
| 5634 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5635 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5636 | |
| 5637 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5638 | createCXTUResourceUsageEntry(*entries, |
| 5639 | CXTUResourceUsage_PreprocessingRecord, |
| 5640 | pRec->getTotalMemory()); |
| 5641 | } |
| 5642 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5643 | createCXTUResourceUsageEntry(*entries, |
| 5644 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5645 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5646 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5647 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5648 | (unsigned) entries->size(), |
| 5649 | entries->size() ? &(*entries)[0] : 0 }; |
| 5650 | entries.take(); |
| 5651 | return usage; |
| 5652 | } |
| 5653 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5654 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5655 | if (usage.data) |
| 5656 | delete (MemUsageEntries*) usage.data; |
| 5657 | } |
| 5658 | |
| 5659 | } // end extern "C" |
| 5660 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5661 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5662 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5663 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5664 | fprintf(stderr, " %s: %lu\n", |
| 5665 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5666 | Usage.entries[I].amount); |
| 5667 | |
| 5668 | clang_disposeCXTUResourceUsage(Usage); |
| 5669 | } |
| 5670 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5671 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5672 | // Misc. utility functions. |
| 5673 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5674 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5675 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5676 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5677 | |
| 5678 | namespace clang { |
| 5679 | |
| 5680 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5681 | void (*Fn)(void*), void *UserData, |
| 5682 | unsigned Size) { |
| 5683 | if (!Size) |
| 5684 | Size = GetSafetyThreadStackSize(); |
| 5685 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5686 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5687 | return CRC.RunSafely(Fn, UserData); |
| 5688 | } |
| 5689 | |
| 5690 | unsigned GetSafetyThreadStackSize() { |
| 5691 | return SafetyStackThreadSize; |
| 5692 | } |
| 5693 | |
| 5694 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5695 | SafetyStackThreadSize = Value; |
| 5696 | } |
| 5697 | |
| 5698 | } |
| 5699 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5700 | extern "C" { |
| 5701 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5702 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5703 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5707 | |