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 | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 17 | #include "CXType.h" |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 18 | #include "CXSourceLocation.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 19 | #include "CIndexDiagnostic.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 20 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Version.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 22 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLocVisitor.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Diagnostic.h" |
| 27 | #include "clang/Frontend/ASTUnit.h" |
| 28 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 31 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CrashRecoveryContext.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 36 | #include "llvm/System/Program.h" |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 37 | #include "llvm/System/Signals.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 39 | // Needed to define L_TMPNAM on some systems. |
| 40 | #include <cstdio> |
| 41 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 42 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 43 | using namespace clang::cxcursor; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 44 | using namespace clang::cxstring; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Crash Reporting. |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Ted Kremenek | d7ffd08 | 2010-05-22 00:06:46 +0000 | [diff] [blame] | 50 | #ifdef USE_CRASHTRACER |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 51 | #include "clang/Analysis/Support/SaveAndRestore.h" |
| 52 | // Integrate with crash reporter. |
Daniel Dunbar | 439794e | 2010-05-20 23:50:23 +0000 | [diff] [blame] | 53 | static const char *__crashreporter_info__ = 0; |
| 54 | asm(".desc ___crashreporter_info__, 0x10"); |
Ted Kremenek | 6b56999 | 2010-02-17 21:12:23 +0000 | [diff] [blame] | 55 | #define NUM_CRASH_STRINGS 32 |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 56 | static unsigned crashtracer_counter = 0; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 57 | static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 }; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 58 | static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 59 | static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 60 | |
| 61 | static unsigned SetCrashTracerInfo(const char *str, |
| 62 | llvm::SmallString<1024> &AggStr) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 64 | unsigned slot = 0; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 65 | while (crashtracer_strings[slot]) { |
| 66 | if (++slot == NUM_CRASH_STRINGS) |
| 67 | slot = 0; |
| 68 | } |
| 69 | crashtracer_strings[slot] = str; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 70 | crashtracer_counter_id[slot] = ++crashtracer_counter; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 71 | |
| 72 | // We need to create an aggregate string because multiple threads |
| 73 | // may be in this method at one time. The crash reporter string |
| 74 | // will attempt to overapproximate the set of in-flight invocations |
| 75 | // of this function. Race conditions can still cause this goal |
| 76 | // to not be achieved. |
| 77 | { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 78 | llvm::raw_svector_ostream Out(AggStr); |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 79 | for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i) |
| 80 | if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n'; |
| 81 | } |
| 82 | __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str(); |
| 83 | return slot; |
| 84 | } |
| 85 | |
| 86 | static void ResetCrashTracerInfo(unsigned slot) { |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 87 | unsigned max_slot = 0; |
| 88 | unsigned max_value = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 90 | crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0; |
| 91 | |
| 92 | for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i) |
| 93 | if (agg_crashtracer_strings[i] && |
| 94 | crashtracer_counter_id[i] > max_value) { |
| 95 | max_slot = i; |
| 96 | max_value = crashtracer_counter_id[i]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 98 | |
| 99 | __crashreporter_info__ = agg_crashtracer_strings[max_slot]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | namespace { |
| 103 | class ArgsCrashTracerInfo { |
| 104 | llvm::SmallString<1024> CrashString; |
| 105 | llvm::SmallString<1024> AggregateString; |
| 106 | unsigned crashtracerSlot; |
| 107 | public: |
| 108 | ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args) |
| 109 | : crashtracerSlot(0) |
| 110 | { |
| 111 | { |
| 112 | llvm::raw_svector_ostream Out(CrashString); |
Ted Kremenek | 0baa952 | 2010-03-05 22:43:25 +0000 | [diff] [blame] | 113 | Out << "ClangCIndex [" << getClangFullVersion() << "]" |
| 114 | << "[createTranslationUnitFromSourceFile]: clang"; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 115 | for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(), |
| 116 | E=Args.end(); I!=E; ++I) |
| 117 | Out << ' ' << *I; |
| 118 | } |
| 119 | crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(), |
| 120 | AggregateString); |
| 121 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 123 | ~ArgsCrashTracerInfo() { |
| 124 | ResetCrashTracerInfo(crashtracerSlot); |
| 125 | } |
| 126 | }; |
| 127 | } |
| 128 | #endif |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 130 | /// \brief The result of comparing two source ranges. |
| 131 | enum RangeComparisonResult { |
| 132 | /// \brief Either the ranges overlap or one of the ranges is invalid. |
| 133 | RangeOverlap, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 135 | /// \brief The first range ends before the second range starts. |
| 136 | RangeBefore, |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 138 | /// \brief The first range starts after the second range ends. |
| 139 | RangeAfter |
| 140 | }; |
| 141 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 142 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 143 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 144 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 145 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 146 | SourceRange R2) { |
| 147 | assert(R1.isValid() && "First range is invalid?"); |
| 148 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 149 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 150 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 151 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 152 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 153 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 154 | return RangeAfter; |
| 155 | return RangeOverlap; |
| 156 | } |
| 157 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 158 | /// \brief Determine if a source location falls within, before, or after a |
| 159 | /// a given source range. |
| 160 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 161 | SourceLocation L, SourceRange R) { |
| 162 | assert(R.isValid() && "First range is invalid?"); |
| 163 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 164 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 165 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 166 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 167 | return RangeBefore; |
| 168 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 169 | return RangeAfter; |
| 170 | return RangeOverlap; |
| 171 | } |
| 172 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 173 | /// \brief Translate a Clang source range into a CIndex source range. |
| 174 | /// |
| 175 | /// Clang internally represents ranges where the end location points to the |
| 176 | /// start of the token at the end. However, for external clients it is more |
| 177 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 178 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 179 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 180 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 181 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 182 | // 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] | 183 | // location accordingly. |
| 184 | // FIXME: How do do this with a macro instantiation location? |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 185 | SourceLocation EndLoc = R.getEnd(); |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 186 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 187 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 188 | EndLoc = EndLoc.getFileLocWithOffset(Length); |
| 189 | } |
| 190 | |
| 191 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 192 | R.getBegin().getRawEncoding(), |
| 193 | EndLoc.getRawEncoding() }; |
| 194 | return Result; |
| 195 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 197 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 198 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
| 200 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 201 | namespace { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 202 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 203 | // Cursor visitor. |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 204 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 205 | public TypeLocVisitor<CursorVisitor, bool>, |
| 206 | public StmtVisitor<CursorVisitor, bool> |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 207 | { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 208 | /// \brief The translation unit we are traversing. |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 209 | ASTUnit *TU; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 211 | /// \brief The parent cursor whose children we are traversing. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 212 | CXCursor Parent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 214 | /// \brief The declaration that serves at the parent of any statement or |
| 215 | /// expression nodes. |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 216 | Decl *StmtParent; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 218 | /// \brief The visitor function. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 219 | CXCursorVisitor Visitor; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 220 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 221 | /// \brief The opaque client data, to be passed along to the visitor. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 222 | CXClientData ClientData; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 224 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 225 | // to the visitor. Declarations with a PCH level greater than this value will |
| 226 | // be suppressed. |
| 227 | unsigned MaxPCHLevel; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 228 | |
| 229 | /// \brief When valid, a source range to which the cursor should restrict |
| 230 | /// its search. |
| 231 | SourceRange RegionOfInterest; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 233 | using DeclVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 234 | using TypeLocVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 235 | using StmtVisitor<CursorVisitor, bool>::Visit; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 236 | |
| 237 | /// \brief Determine whether this particular source range comes before, comes |
| 238 | /// after, or overlaps the region of interest. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 239 | /// |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 240 | /// \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] | 241 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); |
| 242 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 243 | class SetParentRAII { |
| 244 | CXCursor &Parent; |
| 245 | Decl *&StmtParent; |
| 246 | CXCursor OldParent; |
| 247 | |
| 248 | public: |
| 249 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 250 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
| 251 | { |
| 252 | Parent = NewParent; |
| 253 | if (clang_isDeclaration(Parent.kind)) |
| 254 | StmtParent = getCursorDecl(Parent); |
| 255 | } |
| 256 | |
| 257 | ~SetParentRAII() { |
| 258 | Parent = OldParent; |
| 259 | if (clang_isDeclaration(Parent.kind)) |
| 260 | StmtParent = getCursorDecl(Parent); |
| 261 | } |
| 262 | }; |
| 263 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 264 | public: |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 265 | CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData, |
| 266 | unsigned MaxPCHLevel, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 267 | SourceRange RegionOfInterest = SourceRange()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 268 | : TU(TU), Visitor(Visitor), ClientData(ClientData), |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 269 | MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 270 | { |
| 271 | Parent.kind = CXCursor_NoDeclFound; |
| 272 | Parent.data[0] = 0; |
| 273 | Parent.data[1] = 0; |
| 274 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 275 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 276 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 277 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 278 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 279 | |
| 280 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 281 | getPreprocessedEntities(); |
| 282 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 283 | bool VisitChildren(CXCursor Parent); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 285 | // Declaration visitors |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 286 | bool VisitAttributes(Decl *D); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 287 | bool VisitBlockDecl(BlockDecl *B); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 288 | bool VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 289 | bool VisitDeclContext(DeclContext *DC); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 290 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 291 | bool VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 292 | bool VisitTagDecl(TagDecl *D); |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 293 | bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 294 | bool VisitClassTemplatePartialSpecializationDecl( |
| 295 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 296 | bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 297 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
| 298 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
| 299 | bool VisitFunctionDecl(FunctionDecl *ND); |
| 300 | bool VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 4540c9c | 2010-02-18 18:47:08 +0000 | [diff] [blame] | 301 | bool VisitVarDecl(VarDecl *); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame^] | 302 | bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 303 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 304 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame^] | 305 | bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 306 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 307 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
| 308 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 309 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 310 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD); |
Ted Kremenek | 79758f6 | 2010-02-18 22:36:18 +0000 | [diff] [blame] | 311 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 312 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 313 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 314 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 315 | // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations, |
| 316 | // etc. |
| 317 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 318 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 319 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 320 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 321 | bool VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 322 | bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 323 | bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 324 | bool VisitUsingDecl(UsingDecl *D); |
| 325 | bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 326 | bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 328 | // Name visitor |
| 329 | bool VisitDeclarationNameInfo(DeclarationNameInfo Name); |
| 330 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 331 | // Template visitors |
| 332 | bool VisitTemplateParameters(const TemplateParameterList *Params); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 333 | bool VisitTemplateName(TemplateName Name, SourceLocation Loc); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 334 | bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL); |
| 335 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 336 | // Type visitors |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 337 | bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 338 | bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 339 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 340 | bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); |
| 341 | bool VisitTagTypeLoc(TagTypeLoc TL); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 342 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 343 | bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 344 | bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 345 | bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); |
| 346 | bool VisitPointerTypeLoc(PointerTypeLoc TL); |
| 347 | bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL); |
| 348 | bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL); |
| 349 | bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL); |
| 350 | bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 351 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 352 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 353 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL); |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 354 | // FIXME: Implement visitors here when the unimplemented TypeLocs get |
| 355 | // implemented |
| 356 | bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL); |
| 357 | bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 359 | // Statement visitors |
| 360 | bool VisitStmt(Stmt *S); |
| 361 | bool VisitDeclStmt(DeclStmt *S); |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 362 | // FIXME: LabelStmt label? |
| 363 | bool VisitIfStmt(IfStmt *S); |
| 364 | bool VisitSwitchStmt(SwitchStmt *S); |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 365 | bool VisitCaseStmt(CaseStmt *S); |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 366 | bool VisitWhileStmt(WhileStmt *S); |
| 367 | bool VisitForStmt(ForStmt *S); |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 368 | // bool VisitSwitchCase(SwitchCase *S); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 370 | // Expression visitors |
Douglas Gregor | 648220e | 2010-08-10 15:02:34 +0000 | [diff] [blame] | 371 | // FIXME: DeclRefExpr with template arguments, nested-name-specifier |
| 372 | // FIXME: MemberExpr with template arguments, nested-name-specifier |
Douglas Gregor | 6cd24e2 | 2010-07-29 00:26:18 +0000 | [diff] [blame] | 373 | bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 374 | bool VisitBlockExpr(BlockExpr *B); |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 375 | bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 376 | bool VisitExplicitCastExpr(ExplicitCastExpr *E); |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 377 | bool VisitObjCMessageExpr(ObjCMessageExpr *E); |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 378 | bool VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 379 | bool VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 380 | bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
Douglas Gregor | 648220e | 2010-08-10 15:02:34 +0000 | [diff] [blame] | 381 | // FIXME: AddrLabelExpr (once we have cursors for labels) |
| 382 | bool VisitTypesCompatibleExpr(TypesCompatibleExpr *E); |
| 383 | bool VisitVAArgExpr(VAArgExpr *E); |
| 384 | // FIXME: InitListExpr (for the designators) |
| 385 | // FIXME: DesignatedInitExpr |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 386 | }; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 388 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 390 | static SourceRange getRawCursorExtent(CXCursor C); |
| 391 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 392 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 393 | return RangeCompare(TU->getSourceManager(), R, RegionOfInterest); |
| 394 | } |
| 395 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 396 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 397 | /// its children. |
| 398 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 399 | /// \param Cursor the cursor to visit. |
| 400 | /// |
| 401 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 402 | /// this cursor is within the region of interest. |
| 403 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 404 | /// \returns true if the visitation should be aborted, false if it |
| 405 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 406 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 407 | if (clang_isInvalid(Cursor.kind)) |
| 408 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 410 | if (clang_isDeclaration(Cursor.kind)) { |
| 411 | Decl *D = getCursorDecl(Cursor); |
| 412 | assert(D && "Invalid declaration cursor"); |
| 413 | if (D->getPCHLevel() > MaxPCHLevel) |
| 414 | return false; |
| 415 | |
| 416 | if (D->isImplicit()) |
| 417 | return false; |
| 418 | } |
| 419 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 420 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 421 | // we're done. |
| 422 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 423 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 424 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 425 | return false; |
| 426 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 427 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 428 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 429 | case CXChildVisit_Break: |
| 430 | return true; |
| 431 | |
| 432 | case CXChildVisit_Continue: |
| 433 | return false; |
| 434 | |
| 435 | case CXChildVisit_Recurse: |
| 436 | return VisitChildren(Cursor); |
| 437 | } |
| 438 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 439 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 442 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 443 | CursorVisitor::getPreprocessedEntities() { |
| 444 | PreprocessingRecord &PPRec |
| 445 | = *TU->getPreprocessor().getPreprocessingRecord(); |
| 446 | |
| 447 | bool OnlyLocalDecls |
| 448 | = !TU->isMainFileAST() && TU->getOnlyLocalDecls(); |
| 449 | |
| 450 | // There is no region of interest; we have to walk everything. |
| 451 | if (RegionOfInterest.isInvalid()) |
| 452 | return std::make_pair(PPRec.begin(OnlyLocalDecls), |
| 453 | PPRec.end(OnlyLocalDecls)); |
| 454 | |
| 455 | // Find the file in which the region of interest lands. |
| 456 | SourceManager &SM = TU->getSourceManager(); |
| 457 | std::pair<FileID, unsigned> Begin |
| 458 | = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin()); |
| 459 | std::pair<FileID, unsigned> End |
| 460 | = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd()); |
| 461 | |
| 462 | // The region of interest spans files; we have to walk everything. |
| 463 | if (Begin.first != End.first) |
| 464 | return std::make_pair(PPRec.begin(OnlyLocalDecls), |
| 465 | PPRec.end(OnlyLocalDecls)); |
| 466 | |
| 467 | ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap |
| 468 | = TU->getPreprocessedEntitiesByFile(); |
| 469 | if (ByFileMap.empty()) { |
| 470 | // Build the mapping from files to sets of preprocessed entities. |
| 471 | for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls), |
| 472 | EEnd = PPRec.end(OnlyLocalDecls); |
| 473 | E != EEnd; ++E) { |
| 474 | std::pair<FileID, unsigned> P |
| 475 | = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin()); |
| 476 | ByFileMap[P.first].push_back(*E); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return std::make_pair(ByFileMap[Begin.first].begin(), |
| 481 | ByFileMap[Begin.first].end()); |
| 482 | } |
| 483 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 484 | /// \brief Visit the children of the given cursor. |
| 485 | /// |
| 486 | /// \returns true if the visitation should be aborted, false if it |
| 487 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 488 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 489 | if (clang_isReference(Cursor.kind)) { |
| 490 | // By definition, references have no children. |
| 491 | return false; |
| 492 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 493 | |
| 494 | // 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] | 495 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 496 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 498 | if (clang_isDeclaration(Cursor.kind)) { |
| 499 | Decl *D = getCursorDecl(Cursor); |
| 500 | assert(D && "Invalid declaration cursor"); |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 501 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 502 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 504 | if (clang_isStatement(Cursor.kind)) |
| 505 | return Visit(getCursorStmt(Cursor)); |
| 506 | if (clang_isExpression(Cursor.kind)) |
| 507 | return Visit(getCursorExpr(Cursor)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 509 | if (clang_isTranslationUnit(Cursor.kind)) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 510 | ASTUnit *CXXUnit = getCursorASTUnit(Cursor); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 511 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 512 | RegionOfInterest.isInvalid()) { |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 513 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 514 | TLEnd = CXXUnit->top_level_end(); |
| 515 | TL != TLEnd; ++TL) { |
| 516 | if (Visit(MakeCXCursor(*TL, CXXUnit), true)) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 517 | return true; |
| 518 | } |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 519 | } else if (VisitDeclContext( |
| 520 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 521 | return true; |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 522 | |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 523 | // Walk the preprocessing record. |
Daniel Dunbar | 8de30ff | 2010-03-20 01:11:56 +0000 | [diff] [blame] | 524 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 525 | // FIXME: Once we have the ability to deserialize a preprocessing record, |
| 526 | // do so. |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 527 | PreprocessingRecord::iterator E, EEnd; |
| 528 | for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 529 | if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { |
| 530 | if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit))) |
| 531 | return true; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 533 | continue; |
| 534 | } |
| 535 | |
| 536 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { |
| 537 | if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit))) |
| 538 | return true; |
| 539 | |
| 540 | continue; |
| 541 | } |
| 542 | } |
| 543 | } |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 544 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 545 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 547 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 551 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 552 | if (Visit(B->getSignatureAsWritten()->getTypeLoc())) |
| 553 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 555 | if (Stmt *Body = B->getBody()) |
| 556 | return Visit(MakeCXCursor(Body, StmtParent, TU)); |
| 557 | |
| 558 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 561 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 562 | for (DeclContext::decl_iterator |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 563 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 564 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 565 | Decl *D = *I; |
| 566 | if (D->getLexicalDeclContext() != DC) |
| 567 | continue; |
| 568 | |
| 569 | CXCursor Cursor = MakeCXCursor(D, TU); |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 571 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 572 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 573 | if (Range.isInvalid()) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 574 | continue; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 575 | |
| 576 | switch (CompareRegionOfInterest(Range)) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 577 | case RangeBefore: |
| 578 | // This declaration comes before the region of interest; skip it. |
| 579 | continue; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 581 | case RangeAfter: |
| 582 | // This declaration comes after the region of interest; we're done. |
| 583 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 585 | case RangeOverlap: |
| 586 | // This declaration overlaps the region of interest; visit it. |
| 587 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 588 | } |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 589 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 590 | |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 591 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 592 | return true; |
| 593 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 595 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 598 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 599 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 604 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 605 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | |
| 610 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 611 | return VisitDeclContext(D); |
| 612 | } |
| 613 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 614 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 615 | ClassTemplateSpecializationDecl *D) { |
| 616 | bool ShouldVisitBody = false; |
| 617 | switch (D->getSpecializationKind()) { |
| 618 | case TSK_Undeclared: |
| 619 | case TSK_ImplicitInstantiation: |
| 620 | // Nothing to visit |
| 621 | return false; |
| 622 | |
| 623 | case TSK_ExplicitInstantiationDeclaration: |
| 624 | case TSK_ExplicitInstantiationDefinition: |
| 625 | break; |
| 626 | |
| 627 | case TSK_ExplicitSpecialization: |
| 628 | ShouldVisitBody = true; |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | // Visit the template arguments used in the specialization. |
| 633 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 634 | TypeLoc TL = SpecType->getTypeLoc(); |
| 635 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 636 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 637 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 638 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 644 | return true; |
| 645 | |
| 646 | return false; |
| 647 | } |
| 648 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 649 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 650 | ClassTemplatePartialSpecializationDecl *D) { |
| 651 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 652 | // before visiting these template parameters. |
| 653 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 654 | return true; |
| 655 | |
| 656 | // Visit the partial specialization arguments. |
| 657 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 658 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 659 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 660 | return true; |
| 661 | |
| 662 | return VisitCXXRecordDecl(D); |
| 663 | } |
| 664 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 665 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame^] | 666 | // Visit the default argument. |
| 667 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 668 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 669 | if (Visit(DefArg->getTypeLoc())) |
| 670 | return true; |
| 671 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 675 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 676 | if (Expr *Init = D->getInitExpr()) |
| 677 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
| 678 | return false; |
| 679 | } |
| 680 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 681 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 682 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 683 | if (Visit(TSInfo->getTypeLoc())) |
| 684 | return true; |
| 685 | |
| 686 | return false; |
| 687 | } |
| 688 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 689 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 690 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 691 | // Visit the function declaration's syntactic components in the order |
| 692 | // written. This requires a bit of work. |
| 693 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 694 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 695 | |
| 696 | // If we have a function declared directly (without the use of a typedef), |
| 697 | // visit just the return type. Otherwise, just visit the function's type |
| 698 | // now. |
| 699 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 700 | (!FTL && Visit(TL))) |
| 701 | return true; |
| 702 | |
| 703 | // FIXME: Visit the nested-name-specifier, if present. |
| 704 | |
| 705 | // Visit the declaration name. |
| 706 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 707 | return true; |
| 708 | |
| 709 | // FIXME: Visit explicitly-specified template arguments! |
| 710 | |
| 711 | // Visit the function parameters, if we have a function type. |
| 712 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 713 | return true; |
| 714 | |
| 715 | // FIXME: Attributes? |
| 716 | } |
| 717 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 718 | if (ND->isThisDeclarationADefinition() && |
| 719 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 720 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 721 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 722 | return false; |
| 723 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 725 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 726 | if (VisitDeclaratorDecl(D)) |
| 727 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 729 | if (Expr *BitWidth = D->getBitWidth()) |
| 730 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 732 | return false; |
| 733 | } |
| 734 | |
| 735 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 736 | if (VisitDeclaratorDecl(D)) |
| 737 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 739 | if (Expr *Init = D->getInit()) |
| 740 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame^] | 745 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 746 | if (VisitDeclaratorDecl(D)) |
| 747 | return true; |
| 748 | |
| 749 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 750 | if (Expr *DefArg = D->getDefaultArgument()) |
| 751 | return Visit(MakeCXCursor(DefArg, StmtParent, TU)); |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 756 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 757 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 758 | // before visiting these template parameters. |
| 759 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 760 | return true; |
| 761 | |
| 762 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 763 | } |
| 764 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 765 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 766 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 767 | // before visiting these template parameters. |
| 768 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 769 | return true; |
| 770 | |
| 771 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 772 | } |
| 773 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame^] | 774 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 775 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 776 | return true; |
| 777 | |
| 778 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 779 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 780 | return true; |
| 781 | |
| 782 | return false; |
| 783 | } |
| 784 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 785 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 786 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 787 | if (Visit(TSInfo->getTypeLoc())) |
| 788 | return true; |
| 789 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 790 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 791 | PEnd = ND->param_end(); |
| 792 | P != PEnd; ++P) { |
| 793 | if (Visit(MakeCXCursor(*P, TU))) |
| 794 | return true; |
| 795 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 797 | if (ND->isThisDeclarationADefinition() && |
| 798 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 799 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 804 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 805 | return VisitDeclContext(D); |
| 806 | } |
| 807 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 808 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 809 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 810 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 811 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 813 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 814 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 815 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 816 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 817 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 818 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 819 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 822 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 823 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 824 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 825 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 826 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 827 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 829 | return VisitObjCContainerDecl(PID); |
| 830 | } |
| 831 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 832 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 833 | if (Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 834 | return true; |
| 835 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 836 | // FIXME: This implements a workaround with @property declarations also being |
| 837 | // installed in the DeclContext for the @interface. Eventually this code |
| 838 | // should be removed. |
| 839 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 840 | if (!CDecl || !CDecl->IsClassExtension()) |
| 841 | return false; |
| 842 | |
| 843 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 844 | if (!ID) |
| 845 | return false; |
| 846 | |
| 847 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 848 | ObjCPropertyDecl *prevDecl = |
| 849 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 850 | |
| 851 | if (!prevDecl) |
| 852 | return false; |
| 853 | |
| 854 | // Visit synthesized methods since they will be skipped when visiting |
| 855 | // the @interface. |
| 856 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 857 | if (MD->isSynthesized()) |
| 858 | if (Visit(MakeCXCursor(MD, TU))) |
| 859 | return true; |
| 860 | |
| 861 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 862 | if (MD->isSynthesized()) |
| 863 | if (Visit(MakeCXCursor(MD, TU))) |
| 864 | return true; |
| 865 | |
| 866 | return false; |
| 867 | } |
| 868 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 869 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 870 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 871 | if (D->getSuperClass() && |
| 872 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 873 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 874 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 875 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 877 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 878 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 879 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 880 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 881 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 883 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 886 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 887 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 890 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 891 | // 'ID' could be null when dealing with invalid code. |
| 892 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 893 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 894 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 896 | return VisitObjCImplDecl(D); |
| 897 | } |
| 898 | |
| 899 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 900 | #if 0 |
| 901 | // Issue callbacks for super class. |
| 902 | // FIXME: No source location information! |
| 903 | if (D->getSuperClass() && |
| 904 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 905 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 906 | TU))) |
| 907 | return true; |
| 908 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 910 | return VisitObjCImplDecl(D); |
| 911 | } |
| 912 | |
| 913 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 914 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 915 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 916 | E = D->protocol_end(); |
| 917 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 918 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 919 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 920 | |
| 921 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 924 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 925 | for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C) |
| 926 | if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU))) |
| 927 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 929 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 932 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 933 | return VisitDeclContext(D); |
| 934 | } |
| 935 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 936 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 937 | // FIXME: Visit nested-name-specifier. |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 938 | |
| 939 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 940 | D->getTargetNameLoc(), TU)); |
| 941 | } |
| 942 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 943 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 944 | // FIXME: Visit nested-name-specifier. |
| 945 | |
| 946 | // FIXME: Provide a multi-reference of some kind for all of the declarations |
| 947 | // that the using declaration refers to. We don't have this kind of cursor |
| 948 | // yet. |
| 949 | |
| 950 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 951 | } |
| 952 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 953 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 954 | // FIXME: Visit nested-name-specifier. |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 955 | |
| 956 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 957 | D->getIdentLocation(), TU)); |
| 958 | } |
| 959 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 960 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 961 | // FIXME: Visit nested-name-specifier. |
| 962 | |
| 963 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 964 | } |
| 965 | |
| 966 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 967 | UnresolvedUsingTypenameDecl *D) { |
| 968 | // FIXME: Visit nested-name-specifier. |
| 969 | return false; |
| 970 | } |
| 971 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 972 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 973 | switch (Name.getName().getNameKind()) { |
| 974 | case clang::DeclarationName::Identifier: |
| 975 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 976 | case clang::DeclarationName::CXXOperatorName: |
| 977 | case clang::DeclarationName::CXXUsingDirective: |
| 978 | return false; |
| 979 | |
| 980 | case clang::DeclarationName::CXXConstructorName: |
| 981 | case clang::DeclarationName::CXXDestructorName: |
| 982 | case clang::DeclarationName::CXXConversionFunctionName: |
| 983 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 984 | return Visit(TSInfo->getTypeLoc()); |
| 985 | return false; |
| 986 | |
| 987 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 988 | case clang::DeclarationName::ObjCOneArgSelector: |
| 989 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 990 | // FIXME: Per-identifier location info? |
| 991 | return false; |
| 992 | } |
| 993 | |
| 994 | return false; |
| 995 | } |
| 996 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 997 | bool CursorVisitor::VisitTemplateParameters( |
| 998 | const TemplateParameterList *Params) { |
| 999 | if (!Params) |
| 1000 | return false; |
| 1001 | |
| 1002 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1003 | PEnd = Params->end(); |
| 1004 | P != PEnd; ++P) { |
| 1005 | if (Visit(MakeCXCursor(*P, TU))) |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1012 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1013 | switch (Name.getKind()) { |
| 1014 | case TemplateName::Template: |
| 1015 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1016 | |
| 1017 | case TemplateName::OverloadedTemplate: |
| 1018 | // FIXME: We need a way to return multiple lookup results in a single |
| 1019 | // cursor. |
| 1020 | return false; |
| 1021 | |
| 1022 | case TemplateName::DependentTemplate: |
| 1023 | // FIXME: Visit nested-name-specifier. |
| 1024 | return false; |
| 1025 | |
| 1026 | case TemplateName::QualifiedTemplate: |
| 1027 | // FIXME: Visit nested-name-specifier. |
| 1028 | return Visit(MakeCursorTemplateRef( |
| 1029 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1030 | Loc, TU)); |
| 1031 | } |
| 1032 | |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1036 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1037 | switch (TAL.getArgument().getKind()) { |
| 1038 | case TemplateArgument::Null: |
| 1039 | case TemplateArgument::Integral: |
| 1040 | return false; |
| 1041 | |
| 1042 | case TemplateArgument::Pack: |
| 1043 | // FIXME: Implement when variadic templates come along. |
| 1044 | return false; |
| 1045 | |
| 1046 | case TemplateArgument::Type: |
| 1047 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1048 | return Visit(TSInfo->getTypeLoc()); |
| 1049 | return false; |
| 1050 | |
| 1051 | case TemplateArgument::Declaration: |
| 1052 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1053 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1054 | return false; |
| 1055 | |
| 1056 | case TemplateArgument::Expression: |
| 1057 | if (Expr *E = TAL.getSourceExpression()) |
| 1058 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1059 | return false; |
| 1060 | |
| 1061 | case TemplateArgument::Template: |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1062 | return VisitTemplateName(TAL.getArgument().getAsTemplate(), |
| 1063 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1069 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1070 | return VisitDeclContext(D); |
| 1071 | } |
| 1072 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1073 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1074 | return Visit(TL.getUnqualifiedLoc()); |
| 1075 | } |
| 1076 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1077 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1078 | ASTContext &Context = TU->getASTContext(); |
| 1079 | |
| 1080 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1081 | // "Class") have associated declarations. Create cursors for those. |
| 1082 | QualType VisitType; |
| 1083 | switch (TL.getType()->getAs<BuiltinType>()->getKind()) { |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1084 | case BuiltinType::Void: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1085 | case BuiltinType::Bool: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1086 | case BuiltinType::Char_U: |
| 1087 | case BuiltinType::UChar: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1088 | case BuiltinType::Char16: |
| 1089 | case BuiltinType::Char32: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1090 | case BuiltinType::UShort: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1091 | case BuiltinType::UInt: |
| 1092 | case BuiltinType::ULong: |
| 1093 | case BuiltinType::ULongLong: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1094 | case BuiltinType::UInt128: |
| 1095 | case BuiltinType::Char_S: |
| 1096 | case BuiltinType::SChar: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1097 | case BuiltinType::WChar: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1098 | case BuiltinType::Short: |
| 1099 | case BuiltinType::Int: |
| 1100 | case BuiltinType::Long: |
| 1101 | case BuiltinType::LongLong: |
| 1102 | case BuiltinType::Int128: |
| 1103 | case BuiltinType::Float: |
| 1104 | case BuiltinType::Double: |
| 1105 | case BuiltinType::LongDouble: |
| 1106 | case BuiltinType::NullPtr: |
| 1107 | case BuiltinType::Overload: |
| 1108 | case BuiltinType::Dependent: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1109 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1110 | |
| 1111 | case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor? |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1112 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1113 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1114 | case BuiltinType::ObjCId: |
| 1115 | VisitType = Context.getObjCIdType(); |
| 1116 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1117 | |
| 1118 | case BuiltinType::ObjCClass: |
| 1119 | VisitType = Context.getObjCClassType(); |
| 1120 | break; |
| 1121 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1122 | case BuiltinType::ObjCSel: |
| 1123 | VisitType = Context.getObjCSelType(); |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (!VisitType.isNull()) { |
| 1128 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1129 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1130 | TU)); |
| 1131 | } |
| 1132 | |
| 1133 | return false; |
| 1134 | } |
| 1135 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1136 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1137 | return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU)); |
| 1138 | } |
| 1139 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1140 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1141 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1142 | } |
| 1143 | |
| 1144 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1145 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1146 | } |
| 1147 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1148 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1149 | // FIXME: We can't visit the template template parameter, but there's |
| 1150 | // no context information with which we can match up the depth/index in the |
| 1151 | // type to the appropriate |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1155 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1156 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1157 | return true; |
| 1158 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1163 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1164 | return true; |
| 1165 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1166 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1167 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1168 | TU))) |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1176 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1180 | return Visit(TL.getPointeeLoc()); |
| 1181 | } |
| 1182 | |
| 1183 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1184 | return Visit(TL.getPointeeLoc()); |
| 1185 | } |
| 1186 | |
| 1187 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1188 | return Visit(TL.getPointeeLoc()); |
| 1189 | } |
| 1190 | |
| 1191 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1192 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1196 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1199 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1200 | bool SkipResultType) { |
| 1201 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1202 | return true; |
| 1203 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1204 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1205 | if (Decl *D = TL.getArg(I)) |
| 1206 | if (Visit(MakeCXCursor(D, TU))) |
| 1207 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1208 | |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1213 | if (Visit(TL.getElementLoc())) |
| 1214 | return true; |
| 1215 | |
| 1216 | if (Expr *Size = TL.getSizeExpr()) |
| 1217 | return Visit(MakeCXCursor(Size, StmtParent, TU)); |
| 1218 | |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1222 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1223 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1224 | // Visit the template name. |
| 1225 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1226 | TL.getTemplateNameLoc())) |
| 1227 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Visit the template arguments. |
| 1230 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1231 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1232 | return true; |
| 1233 | |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1237 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1238 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1239 | } |
| 1240 | |
| 1241 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1242 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1243 | return Visit(TSInfo->getTypeLoc()); |
| 1244 | |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1248 | bool CursorVisitor::VisitStmt(Stmt *S) { |
| 1249 | for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end(); |
| 1250 | Child != ChildEnd; ++Child) { |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 1251 | if (Stmt *C = *Child) |
| 1252 | if (Visit(MakeCXCursor(C, StmtParent, TU))) |
| 1253 | return true; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1254 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1255 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
| 1258 | |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 1259 | bool CursorVisitor::VisitCaseStmt(CaseStmt *S) { |
| 1260 | // Specially handle CaseStmts because they can be nested, e.g.: |
| 1261 | // |
| 1262 | // case 1: |
| 1263 | // case 2: |
| 1264 | // |
| 1265 | // In this case the second CaseStmt is the child of the first. Walking |
| 1266 | // these recursively can blow out the stack. |
| 1267 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU); |
| 1268 | while (true) { |
| 1269 | // Set the Parent field to Cursor, then back to its old value once we're |
| 1270 | // done. |
| 1271 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 1272 | |
| 1273 | if (Stmt *LHS = S->getLHS()) |
| 1274 | if (Visit(MakeCXCursor(LHS, StmtParent, TU))) |
| 1275 | return true; |
| 1276 | if (Stmt *RHS = S->getRHS()) |
| 1277 | if (Visit(MakeCXCursor(RHS, StmtParent, TU))) |
| 1278 | return true; |
| 1279 | if (Stmt *SubStmt = S->getSubStmt()) { |
| 1280 | if (!isa<CaseStmt>(SubStmt)) |
| 1281 | return Visit(MakeCXCursor(SubStmt, StmtParent, TU)); |
| 1282 | |
| 1283 | // Specially handle 'CaseStmt' so that we don't blow out the stack. |
| 1284 | CaseStmt *CS = cast<CaseStmt>(SubStmt); |
| 1285 | Cursor = MakeCXCursor(CS, StmtParent, TU); |
| 1286 | if (RegionOfInterest.isValid()) { |
| 1287 | SourceRange Range = CS->getSourceRange(); |
| 1288 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
| 1292 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 1293 | case CXChildVisit_Break: return true; |
| 1294 | case CXChildVisit_Continue: return false; |
| 1295 | case CXChildVisit_Recurse: |
| 1296 | // Perform tail-recursion manually. |
| 1297 | S = CS; |
| 1298 | continue; |
| 1299 | } |
| 1300 | } |
| 1301 | return false; |
| 1302 | } |
| 1303 | } |
| 1304 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1305 | bool CursorVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1306 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1307 | D != DEnd; ++D) { |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1308 | if (*D && Visit(MakeCXCursor(*D, TU))) |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1309 | return true; |
| 1310 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1311 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1315 | bool CursorVisitor::VisitIfStmt(IfStmt *S) { |
| 1316 | if (VarDecl *Var = S->getConditionVariable()) { |
| 1317 | if (Visit(MakeCXCursor(Var, TU))) |
| 1318 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1321 | if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 1322 | return true; |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1323 | if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU))) |
| 1324 | return true; |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1325 | if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU))) |
| 1326 | return true; |
| 1327 | |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 1332 | if (VarDecl *Var = S->getConditionVariable()) { |
| 1333 | if (Visit(MakeCXCursor(Var, TU))) |
| 1334 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1337 | if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 1338 | return true; |
| 1339 | if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) |
| 1340 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1342 | return false; |
| 1343 | } |
| 1344 | |
| 1345 | bool CursorVisitor::VisitWhileStmt(WhileStmt *S) { |
| 1346 | if (VarDecl *Var = S->getConditionVariable()) { |
| 1347 | if (Visit(MakeCXCursor(Var, TU))) |
| 1348 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1351 | if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 1352 | return true; |
| 1353 | if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1354 | return true; |
| 1355 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | bool CursorVisitor::VisitForStmt(ForStmt *S) { |
| 1360 | if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU))) |
| 1361 | return true; |
| 1362 | if (VarDecl *Var = S->getConditionVariable()) { |
| 1363 | if (Visit(MakeCXCursor(Var, TU))) |
| 1364 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Douglas Gregor | 263b47b | 2010-01-25 16:12:32 +0000 | [diff] [blame] | 1367 | if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 1368 | return true; |
| 1369 | if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU))) |
| 1370 | return true; |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1371 | if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) |
| 1372 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1373 | |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 1374 | return false; |
| 1375 | } |
| 1376 | |
Douglas Gregor | 6cd24e2 | 2010-07-29 00:26:18 +0000 | [diff] [blame] | 1377 | bool CursorVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| 1378 | if (Visit(MakeCXCursor(E->getArg(0), StmtParent, TU))) |
| 1379 | return true; |
| 1380 | |
| 1381 | if (Visit(MakeCXCursor(E->getCallee(), StmtParent, TU))) |
| 1382 | return true; |
| 1383 | |
| 1384 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
| 1385 | if (Visit(MakeCXCursor(E->getArg(I), StmtParent, TU))) |
| 1386 | return true; |
| 1387 | |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1391 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1392 | if (D->isDefinition()) { |
| 1393 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1394 | E = D->bases_end(); I != E; ++I) { |
| 1395 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1396 | return true; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | return VisitTagDecl(D); |
| 1401 | } |
| 1402 | |
| 1403 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 1404 | bool CursorVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1405 | return Visit(B->getBlockDecl()); |
| 1406 | } |
| 1407 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1408 | bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 1409 | // FIXME: Visit fields as well? |
| 1410 | if (Visit(E->getTypeSourceInfo()->getTypeLoc())) |
| 1411 | return true; |
| 1412 | |
| 1413 | return VisitExpr(E); |
| 1414 | } |
| 1415 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1416 | bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 1417 | if (E->isArgumentType()) { |
| 1418 | if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo()) |
| 1419 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1420 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1421 | return false; |
| 1422 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1424 | return VisitExpr(E); |
| 1425 | } |
| 1426 | |
| 1427 | bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 1428 | if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten()) |
| 1429 | if (Visit(TSInfo->getTypeLoc())) |
| 1430 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1431 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1432 | return VisitCastExpr(E); |
| 1433 | } |
| 1434 | |
| 1435 | bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1436 | if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo()) |
| 1437 | if (Visit(TSInfo->getTypeLoc())) |
| 1438 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | 336fd81 | 2010-01-23 00:40:08 +0000 | [diff] [blame] | 1440 | return VisitExpr(E); |
| 1441 | } |
| 1442 | |
Douglas Gregor | 648220e | 2010-08-10 15:02:34 +0000 | [diff] [blame] | 1443 | bool CursorVisitor::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { |
| 1444 | return Visit(E->getArgTInfo1()->getTypeLoc()) || |
| 1445 | Visit(E->getArgTInfo2()->getTypeLoc()); |
| 1446 | } |
| 1447 | |
| 1448 | bool CursorVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 1449 | if (Visit(E->getWrittenTypeInfo()->getTypeLoc())) |
| 1450 | return true; |
| 1451 | |
| 1452 | return Visit(MakeCXCursor(E->getSubExpr(), StmtParent, TU)); |
| 1453 | } |
| 1454 | |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 1455 | bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1456 | if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo()) |
| 1457 | if (Visit(TSInfo->getTypeLoc())) |
| 1458 | return true; |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 1459 | |
| 1460 | return VisitExpr(E); |
| 1461 | } |
| 1462 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1463 | bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 1464 | return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc()); |
| 1465 | } |
| 1466 | |
| 1467 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1468 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1469 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1470 | i != e; ++i) |
| 1471 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1472 | return true; |
| 1473 | |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 1477 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1478 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 1479 | int displayDiagnostics) { |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 1480 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 1481 | // enable it. |
| 1482 | llvm::CrashRecoveryContext::Enable(); |
| 1483 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 1484 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 1485 | if (excludeDeclarationsFromPCH) |
| 1486 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1487 | if (displayDiagnostics) |
| 1488 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 1489 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1492 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1493 | if (CIdx) |
| 1494 | delete static_cast<CIndexer *>(CIdx); |
Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 1495 | if (getenv("LIBCLANG_TIMING")) |
| 1496 | llvm::TimerGroup::printAll(llvm::errs()); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 1499 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1500 | if (CIdx) { |
| 1501 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 1502 | CXXIdx->setUseExternalASTGeneration(value); |
| 1503 | } |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1506 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1507 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1508 | if (!CIdx) |
| 1509 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1510 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 1511 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1512 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1513 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 1514 | return ASTUnit::LoadFromASTFile(ast_filename, Diags, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1515 | CXXIdx->getOnlyLocalDecls(), |
| 1516 | 0, 0, true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 1519 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 1520 | return CXTranslationUnit_PrecompiledPreamble; |
| 1521 | } |
| 1522 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1523 | CXTranslationUnit |
| 1524 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 1525 | const char *source_filename, |
| 1526 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 1527 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1528 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1529 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 1530 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 1531 | command_line_args, num_command_line_args, |
| 1532 | unsaved_files, num_unsaved_files, |
| 1533 | CXTranslationUnit_DetailedPreprocessingRecord); |
| 1534 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1535 | |
| 1536 | struct ParseTranslationUnitInfo { |
| 1537 | CXIndex CIdx; |
| 1538 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 1539 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1540 | int num_command_line_args; |
| 1541 | struct CXUnsavedFile *unsaved_files; |
| 1542 | unsigned num_unsaved_files; |
| 1543 | unsigned options; |
| 1544 | CXTranslationUnit result; |
| 1545 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 1546 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1547 | ParseTranslationUnitInfo *PTUI = |
| 1548 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 1549 | CXIndex CIdx = PTUI->CIdx; |
| 1550 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 1551 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1552 | int num_command_line_args = PTUI->num_command_line_args; |
| 1553 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 1554 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 1555 | unsigned options = PTUI->options; |
| 1556 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 1557 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1558 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1559 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1560 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 1561 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 1562 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1563 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1564 | bool CompleteTranslationUnit |
| 1565 | = ((options & CXTranslationUnit_Incomplete) == 0); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1566 | bool CacheCodeCompetionResults |
| 1567 | = options & CXTranslationUnit_CacheCompletionResults; |
| 1568 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1569 | // Configure the diagnostics. |
| 1570 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1571 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags; |
| 1572 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1574 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
| 1575 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 1576 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1577 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 1578 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1579 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 1580 | Buffer)); |
| 1581 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1582 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 1583 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 1584 | llvm::SmallVector<const char *, 16> Args; |
| 1585 | |
| 1586 | // The 'source_filename' argument is optional. If the caller does not |
| 1587 | // specify it then it is assumed that the source file is specified |
| 1588 | // in the actual argument list. |
| 1589 | if (source_filename) |
| 1590 | Args.push_back(source_filename); |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 1591 | |
| 1592 | // Since the Clang C library is primarily used by batch tools dealing with |
| 1593 | // (often very broken) source code, where spell-checking can have a |
| 1594 | // significant negative impact on performance (particularly when |
| 1595 | // precompiled headers are involved), we disable it by default. |
| 1596 | // Note that we place this argument early in the list, so that it can be |
| 1597 | // overridden by the caller with "-fspell-checking". |
Douglas Gregor | a0068fc | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 1598 | Args.push_back("-fno-spell-checking"); |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 1599 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 1600 | Args.insert(Args.end(), command_line_args, |
| 1601 | command_line_args + num_command_line_args); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1603 | // Do we need the detailed preprocessing record? |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 1604 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 1605 | Args.push_back("-Xclang"); |
| 1606 | Args.push_back("-detailed-preprocessing-record"); |
| 1607 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1609 | unsigned NumErrors = Diags->getNumErrors(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1610 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 1611 | #ifdef USE_CRASHTRACER |
| 1612 | ArgsCrashTracerInfo ACTI(Args); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 1613 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1614 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 1615 | llvm::OwningPtr<ASTUnit> Unit( |
| 1616 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1617 | Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1618 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 1619 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1620 | RemappedFiles.data(), |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1621 | RemappedFiles.size(), |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1622 | /*CaptureDiagnostics=*/true, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1623 | PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1624 | CompleteTranslationUnit, |
| 1625 | CacheCodeCompetionResults)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1627 | if (NumErrors != Diags->getNumErrors()) { |
Ted Kremenek | 34f6a32 | 2010-03-05 22:43:29 +0000 | [diff] [blame] | 1628 | // Make sure to check that 'Unit' is non-NULL. |
| 1629 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 1630 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 1631 | DEnd = Unit->stored_diag_end(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1632 | D != DEnd; ++D) { |
| 1633 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 1634 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 1635 | clang_defaultDiagnosticDisplayOptions()); |
| 1636 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 1637 | clang_disposeString(Msg); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1638 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 1639 | #ifdef LLVM_ON_WIN32 |
| 1640 | // On Windows, force a flush, since there may be multiple copies of |
| 1641 | // stderr and stdout in the file system, all with different buffers |
| 1642 | // but writing to the same device. |
| 1643 | fflush(stderr); |
Ted Kremenek | 83c5184 | 2010-03-26 01:34:51 +0000 | [diff] [blame] | 1644 | #endif |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1645 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1646 | } |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 1647 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1648 | PTUI->result = Unit.take(); |
| 1649 | return; |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1652 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 1653 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1654 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1655 | // First add the complete path to the 'clang' executable. |
| 1656 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 1657 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1658 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1659 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 1660 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1661 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1662 | // The 'source_filename' argument is optional. If the caller does not |
| 1663 | // specify it then it is assumed that the source file is specified |
| 1664 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1665 | if (source_filename) |
| 1666 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1667 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 1668 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1669 | argv.push_back("-o"); |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 1670 | char astTmpFile[L_tmpnam]; |
| 1671 | argv.push_back(tmpnam(astTmpFile)); |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Since the Clang C library is primarily used by batch tools dealing with |
| 1674 | // (often very broken) source code, where spell-checking can have a |
| 1675 | // significant negative impact on performance (particularly when |
| 1676 | // precompiled headers are involved), we disable it by default. |
| 1677 | // Note that we place this argument early in the list, so that it can be |
| 1678 | // overridden by the caller with "-fspell-checking". |
Douglas Gregor | a0068fc | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 1679 | argv.push_back("-fno-spell-checking"); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1681 | // Remap any unsaved files to temporary files. |
| 1682 | std::vector<llvm::sys::Path> TemporaryFiles; |
| 1683 | std::vector<std::string> RemapArgs; |
| 1684 | if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1685 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1687 | // The pointers into the elements of RemapArgs are stable because we |
| 1688 | // won't be adding anything to RemapArgs after this point. |
| 1689 | for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) |
| 1690 | argv.push_back(RemapArgs[i].c_str()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1691 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1692 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 1693 | for (int i = 0; i < num_command_line_args; ++i) |
| 1694 | if (const char *arg = command_line_args[i]) { |
| 1695 | if (strcmp(arg, "-o") == 0) { |
| 1696 | ++i; // Also skip the matching argument. |
| 1697 | continue; |
| 1698 | } |
| 1699 | if (strcmp(arg, "-emit-ast") == 0 || |
| 1700 | strcmp(arg, "-c") == 0 || |
| 1701 | strcmp(arg, "-fsyntax-only") == 0) { |
| 1702 | continue; |
| 1703 | } |
| 1704 | |
| 1705 | // Keep the argument. |
| 1706 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 1707 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1708 | |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1709 | // Generate a temporary name for the diagnostics file. |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 1710 | char tmpFileResults[L_tmpnam]; |
| 1711 | char *tmpResultsFileName = tmpnam(tmpFileResults); |
| 1712 | llvm::sys::Path DiagnosticsFile(tmpResultsFileName); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1713 | TemporaryFiles.push_back(DiagnosticsFile); |
| 1714 | argv.push_back("-fdiagnostics-binary"); |
| 1715 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1716 | // Do we need the detailed preprocessing record? |
| 1717 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 1718 | argv.push_back("-Xclang"); |
| 1719 | argv.push_back("-detailed-preprocessing-record"); |
| 1720 | } |
| 1721 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 1722 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 1723 | argv.push_back(NULL); |
| 1724 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 1725 | // Invoke 'clang'. |
| 1726 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 1727 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 1728 | std::string ErrMsg; |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1729 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile, |
| 1730 | NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 1731 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 1732 | /* redirects */ &Redirects[0], |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 1733 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 1735 | if (!ErrMsg.empty()) { |
| 1736 | std::string AllArgs; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 1737 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 1738 | I != E; ++I) { |
| 1739 | AllArgs += ' '; |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 1740 | if (*I) |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 1741 | AllArgs += *I; |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 1742 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1743 | |
Daniel Dunbar | 32141c8 | 2010-02-23 20:23:45 +0000 | [diff] [blame] | 1744 | Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 1745 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 1746 | |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 1747 | ASTUnit *ATU = ASTUnit::LoadFromASTFile(astTmpFile, Diags, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1748 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1749 | RemappedFiles.data(), |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1750 | RemappedFiles.size(), |
| 1751 | /*CaptureDiagnostics=*/true); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1752 | if (ATU) { |
| 1753 | LoadSerializedDiagnostics(DiagnosticsFile, |
| 1754 | num_unsaved_files, unsaved_files, |
| 1755 | ATU->getFileManager(), |
| 1756 | ATU->getSourceManager(), |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 1757 | ATU->getStoredDiagnostics()); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1758 | } else if (CXXIdx->getDisplayDiagnostics()) { |
| 1759 | // We failed to load the ASTUnit, but we can still deserialize the |
| 1760 | // diagnostics and emit them. |
| 1761 | FileManager FileMgr; |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 1762 | Diagnostic Diag; |
| 1763 | SourceManager SourceMgr(Diag); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1764 | // FIXME: Faked LangOpts! |
| 1765 | LangOptions LangOpts; |
| 1766 | llvm::SmallVector<StoredDiagnostic, 4> Diags; |
| 1767 | LoadSerializedDiagnostics(DiagnosticsFile, |
| 1768 | num_unsaved_files, unsaved_files, |
| 1769 | FileMgr, SourceMgr, Diags); |
| 1770 | for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(), |
| 1771 | DEnd = Diags.end(); |
| 1772 | D != DEnd; ++D) { |
| 1773 | CXStoredDiagnostic Diag(*D, LangOpts); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 1774 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 1775 | clang_defaultDiagnosticDisplayOptions()); |
| 1776 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 1777 | clang_disposeString(Msg); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1778 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 1779 | |
| 1780 | #ifdef LLVM_ON_WIN32 |
| 1781 | // On Windows, force a flush, since there may be multiple copies of |
| 1782 | // stderr and stdout in the file system, all with different buffers |
| 1783 | // but writing to the same device. |
| 1784 | fflush(stderr); |
| 1785 | #endif |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1786 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1787 | |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 1788 | if (ATU) { |
| 1789 | // Make the translation unit responsible for destroying all temporary files. |
| 1790 | for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) |
| 1791 | ATU->addTemporaryFile(TemporaryFiles[i]); |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 1792 | ATU->addTemporaryFile(llvm::sys::Path(ATU->getASTFileName())); |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 1793 | } else { |
| 1794 | // Destroy all of the temporary files now; they can't be referenced any |
| 1795 | // longer. |
| 1796 | llvm::sys::Path(astTmpFile).eraseFromDisk(); |
| 1797 | for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) |
| 1798 | TemporaryFiles[i].eraseFromDisk(); |
| 1799 | } |
| 1800 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1801 | PTUI->result = ATU; |
| 1802 | } |
| 1803 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 1804 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 1805 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1806 | int num_command_line_args, |
| 1807 | struct CXUnsavedFile *unsaved_files, |
| 1808 | unsigned num_unsaved_files, |
| 1809 | unsigned options) { |
| 1810 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
| 1811 | num_command_line_args, unsaved_files, num_unsaved_files, |
| 1812 | options, 0 }; |
| 1813 | llvm::CrashRecoveryContext CRC; |
| 1814 | |
| 1815 | if (!CRC.RunSafely(clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 1816 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 1817 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 1818 | fprintf(stderr, " 'command_line_args' : ["); |
| 1819 | for (int i = 0; i != num_command_line_args; ++i) { |
| 1820 | if (i) |
| 1821 | fprintf(stderr, ", "); |
| 1822 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 1823 | } |
| 1824 | fprintf(stderr, "],\n"); |
| 1825 | fprintf(stderr, " 'unsaved_files' : ["); |
| 1826 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 1827 | if (i) |
| 1828 | fprintf(stderr, ", "); |
| 1829 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 1830 | unsaved_files[i].Length); |
| 1831 | } |
| 1832 | fprintf(stderr, "],\n"); |
| 1833 | fprintf(stderr, " 'options' : %d,\n", options); |
| 1834 | fprintf(stderr, "}\n"); |
| 1835 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1836 | return 0; |
| 1837 | } |
| 1838 | |
| 1839 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 1842 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 1843 | return CXSaveTranslationUnit_None; |
| 1844 | } |
| 1845 | |
| 1846 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 1847 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1848 | if (!TU) |
| 1849 | return 1; |
| 1850 | |
| 1851 | return static_cast<ASTUnit *>(TU)->Save(FileName); |
| 1852 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 1853 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1854 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1855 | if (CTUnit) { |
| 1856 | // If the translation unit has been marked as unsafe to free, just discard |
| 1857 | // it. |
| 1858 | if (static_cast<ASTUnit *>(CTUnit)->isUnsafeToFree()) |
| 1859 | return; |
| 1860 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1861 | delete static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1862 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 1863 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1864 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 1865 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 1866 | return CXReparse_None; |
| 1867 | } |
| 1868 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1869 | struct ReparseTranslationUnitInfo { |
| 1870 | CXTranslationUnit TU; |
| 1871 | unsigned num_unsaved_files; |
| 1872 | struct CXUnsavedFile *unsaved_files; |
| 1873 | unsigned options; |
| 1874 | int result; |
| 1875 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 1876 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1877 | ReparseTranslationUnitInfo *RTUI = |
| 1878 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 1879 | CXTranslationUnit TU = RTUI->TU; |
| 1880 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 1881 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 1882 | unsigned options = RTUI->options; |
| 1883 | (void) options; |
| 1884 | RTUI->result = 1; |
| 1885 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1886 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1887 | return; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1888 | |
| 1889 | llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
| 1890 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 1891 | llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
| 1892 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1893 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1894 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 1895 | Buffer)); |
| 1896 | } |
| 1897 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1898 | if (!static_cast<ASTUnit *>(TU)->Reparse(RemappedFiles.data(), |
| 1899 | RemappedFiles.size())) |
| 1900 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1901 | } |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1902 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 1903 | unsigned num_unsaved_files, |
| 1904 | struct CXUnsavedFile *unsaved_files, |
| 1905 | unsigned options) { |
| 1906 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 1907 | options, 0 }; |
| 1908 | llvm::CrashRecoveryContext CRC; |
| 1909 | |
| 1910 | if (!CRC.RunSafely(clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 1911 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 1912 | static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); |
| 1913 | return 1; |
| 1914 | } |
| 1915 | |
| 1916 | return RTUI.result; |
| 1917 | } |
| 1918 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1919 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1920 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 1921 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 1922 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1923 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 1924 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 1925 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 1926 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1928 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1929 | CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1930 | return Result; |
| 1931 | } |
| 1932 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1933 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1935 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1936 | // CXSourceLocation and CXSourceRange Operations. |
| 1937 | //===----------------------------------------------------------------------===// |
| 1938 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1939 | extern "C" { |
| 1940 | CXSourceLocation clang_getNullLocation() { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1941 | CXSourceLocation Result = { { 0, 0 }, 0 }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1942 | return Result; |
| 1943 | } |
| 1944 | |
| 1945 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { |
Daniel Dunbar | 90a6b9e | 2010-01-30 23:58:27 +0000 | [diff] [blame] | 1946 | return (loc1.ptr_data[0] == loc2.ptr_data[0] && |
| 1947 | loc1.ptr_data[1] == loc2.ptr_data[1] && |
| 1948 | loc1.int_data == loc2.int_data); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, |
| 1952 | CXFile file, |
| 1953 | unsigned line, |
| 1954 | unsigned column) { |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 1955 | if (!tu || !file) |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1956 | return clang_getNullLocation(); |
Douglas Gregor | 42748ec | 2010-04-30 19:45:53 +0000 | [diff] [blame] | 1957 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1958 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); |
| 1959 | SourceLocation SLoc |
| 1960 | = CXXUnit->getSourceManager().getLocation( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1961 | static_cast<const FileEntry *>(file), |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1962 | line, column); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1963 | |
Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 1964 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1967 | CXSourceRange clang_getNullRange() { |
| 1968 | CXSourceRange Result = { { 0, 0 }, 0, 0 }; |
| 1969 | return Result; |
| 1970 | } |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1972 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { |
| 1973 | if (begin.ptr_data[0] != end.ptr_data[0] || |
| 1974 | begin.ptr_data[1] != end.ptr_data[1]) |
| 1975 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1976 | |
| 1977 | CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1978 | begin.int_data, end.int_data }; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1979 | return Result; |
| 1980 | } |
| 1981 | |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 1982 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 1983 | CXFile *file, |
| 1984 | unsigned *line, |
| 1985 | unsigned *column, |
| 1986 | unsigned *offset) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1987 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1988 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 1989 | if (!location.ptr_data[0] || Loc.isInvalid()) { |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 1990 | if (file) |
| 1991 | *file = 0; |
| 1992 | if (line) |
| 1993 | *line = 0; |
| 1994 | if (column) |
| 1995 | *column = 0; |
| 1996 | if (offset) |
| 1997 | *offset = 0; |
| 1998 | return; |
| 1999 | } |
| 2000 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2001 | const SourceManager &SM = |
| 2002 | *static_cast<const SourceManager*>(location.ptr_data[0]); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2003 | SourceLocation InstLoc = SM.getInstantiationLoc(Loc); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2004 | |
| 2005 | if (file) |
| 2006 | *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); |
| 2007 | if (line) |
| 2008 | *line = SM.getInstantiationLineNumber(InstLoc); |
| 2009 | if (column) |
| 2010 | *column = SM.getInstantiationColumnNumber(InstLoc); |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2011 | if (offset) |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 2012 | *offset = SM.getDecomposedLoc(InstLoc).second; |
Douglas Gregor | e69517c | 2010-01-26 03:07:15 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2015 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2016 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2017 | range.begin_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2018 | return Result; |
| 2019 | } |
| 2020 | |
| 2021 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 2022 | CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2023 | range.end_int_data }; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2024 | return Result; |
| 2025 | } |
| 2026 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2027 | } // end: extern "C" |
| 2028 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 2029 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2030 | // CXFile Operations. |
| 2031 | //===----------------------------------------------------------------------===// |
| 2032 | |
| 2033 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2034 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2035 | if (!SFile) |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2036 | return createCXString(NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2037 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2038 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2039 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2043 | if (!SFile) |
| 2044 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2045 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2046 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2047 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2048 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2049 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2050 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2051 | if (!tu) |
| 2052 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2054 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2056 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 2057 | const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name)); |
| 2058 | return const_cast<FileEntry *>(File); |
| 2059 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2060 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2061 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2062 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2063 | //===----------------------------------------------------------------------===// |
| 2064 | // CXCursor Operations. |
| 2065 | //===----------------------------------------------------------------------===// |
| 2066 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2067 | static Decl *getDeclFromExpr(Stmt *E) { |
| 2068 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2069 | return RefExpr->getDecl(); |
| 2070 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2071 | return ME->getMemberDecl(); |
| 2072 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2073 | return RE->getDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2074 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2075 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2076 | return getDeclFromExpr(CE->getCallee()); |
| 2077 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 2078 | return getDeclFromExpr(CE->getSubExpr()); |
| 2079 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2080 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2081 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2082 | return 0; |
| 2083 | } |
| 2084 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2085 | static SourceLocation getLocationFromExpr(Expr *E) { |
| 2086 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2087 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2088 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2089 | return DRE->getLocation(); |
| 2090 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2091 | return Member->getMemberLoc(); |
| 2092 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2093 | return Ivar->getLocation(); |
| 2094 | return E->getLocStart(); |
| 2095 | } |
| 2096 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2097 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2098 | |
| 2099 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2100 | CXCursorVisitor visitor, |
| 2101 | CXClientData client_data) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2102 | ASTUnit *CXXUnit = getCursorASTUnit(parent); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2103 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 2104 | CursorVisitor CursorVis(CXXUnit, visitor, client_data, |
| 2105 | CXXUnit->getMaxPCHLevel()); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2106 | return CursorVis.VisitChildren(parent); |
| 2107 | } |
| 2108 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2109 | static CXString getDeclSpelling(Decl *D) { |
| 2110 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
| 2111 | if (!ND) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2112 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2114 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2115 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2117 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 2118 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 2119 | // and returns different names. NamedDecl returns the class name and |
| 2120 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2121 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2123 | if (isa<UsingDirectiveDecl>(D)) |
| 2124 | return createCXString(""); |
| 2125 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 2126 | llvm::SmallString<1024> S; |
| 2127 | llvm::raw_svector_ostream os(S); |
| 2128 | ND->printName(os); |
| 2129 | |
| 2130 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2131 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2132 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2133 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2134 | if (clang_isTranslationUnit(C.kind)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2135 | return clang_getTranslationUnitSpelling(C.data[2]); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2136 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2137 | if (clang_isReference(C.kind)) { |
| 2138 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2139 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 2140 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2141 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2142 | } |
| 2143 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 2144 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2145 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2146 | } |
| 2147 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 2148 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2149 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2150 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2151 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2152 | case CXCursor_CXXBaseSpecifier: { |
| 2153 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 2154 | return createCXString(B->getType().getAsString()); |
| 2155 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2156 | case CXCursor_TypeRef: { |
| 2157 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 2158 | assert(Type && "Missing type decl"); |
| 2159 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2160 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 2161 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2162 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2163 | case CXCursor_TemplateRef: { |
| 2164 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2165 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2166 | |
| 2167 | return createCXString(Template->getNameAsString()); |
| 2168 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2169 | |
| 2170 | case CXCursor_NamespaceRef: { |
| 2171 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 2172 | assert(NS && "Missing namespace decl"); |
| 2173 | |
| 2174 | return createCXString(NS->getNameAsString()); |
| 2175 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2176 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2177 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2178 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2181 | |
| 2182 | if (clang_isExpression(C.kind)) { |
| 2183 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 2184 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2185 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2186 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 2189 | if (C.kind == CXCursor_MacroInstantiation) |
| 2190 | return createCXString(getCursorMacroInstantiation(C)->getName() |
| 2191 | ->getNameStart()); |
| 2192 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2193 | if (C.kind == CXCursor_MacroDefinition) |
| 2194 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 2195 | ->getNameStart()); |
| 2196 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 2197 | if (clang_isDeclaration(C.kind)) |
| 2198 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2199 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2200 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2203 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 2204 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2205 | case CXCursor_FunctionDecl: |
| 2206 | return createCXString("FunctionDecl"); |
| 2207 | case CXCursor_TypedefDecl: |
| 2208 | return createCXString("TypedefDecl"); |
| 2209 | case CXCursor_EnumDecl: |
| 2210 | return createCXString("EnumDecl"); |
| 2211 | case CXCursor_EnumConstantDecl: |
| 2212 | return createCXString("EnumConstantDecl"); |
| 2213 | case CXCursor_StructDecl: |
| 2214 | return createCXString("StructDecl"); |
| 2215 | case CXCursor_UnionDecl: |
| 2216 | return createCXString("UnionDecl"); |
| 2217 | case CXCursor_ClassDecl: |
| 2218 | return createCXString("ClassDecl"); |
| 2219 | case CXCursor_FieldDecl: |
| 2220 | return createCXString("FieldDecl"); |
| 2221 | case CXCursor_VarDecl: |
| 2222 | return createCXString("VarDecl"); |
| 2223 | case CXCursor_ParmDecl: |
| 2224 | return createCXString("ParmDecl"); |
| 2225 | case CXCursor_ObjCInterfaceDecl: |
| 2226 | return createCXString("ObjCInterfaceDecl"); |
| 2227 | case CXCursor_ObjCCategoryDecl: |
| 2228 | return createCXString("ObjCCategoryDecl"); |
| 2229 | case CXCursor_ObjCProtocolDecl: |
| 2230 | return createCXString("ObjCProtocolDecl"); |
| 2231 | case CXCursor_ObjCPropertyDecl: |
| 2232 | return createCXString("ObjCPropertyDecl"); |
| 2233 | case CXCursor_ObjCIvarDecl: |
| 2234 | return createCXString("ObjCIvarDecl"); |
| 2235 | case CXCursor_ObjCInstanceMethodDecl: |
| 2236 | return createCXString("ObjCInstanceMethodDecl"); |
| 2237 | case CXCursor_ObjCClassMethodDecl: |
| 2238 | return createCXString("ObjCClassMethodDecl"); |
| 2239 | case CXCursor_ObjCImplementationDecl: |
| 2240 | return createCXString("ObjCImplementationDecl"); |
| 2241 | case CXCursor_ObjCCategoryImplDecl: |
| 2242 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 2243 | case CXCursor_CXXMethod: |
| 2244 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2245 | case CXCursor_UnexposedDecl: |
| 2246 | return createCXString("UnexposedDecl"); |
| 2247 | case CXCursor_ObjCSuperClassRef: |
| 2248 | return createCXString("ObjCSuperClassRef"); |
| 2249 | case CXCursor_ObjCProtocolRef: |
| 2250 | return createCXString("ObjCProtocolRef"); |
| 2251 | case CXCursor_ObjCClassRef: |
| 2252 | return createCXString("ObjCClassRef"); |
| 2253 | case CXCursor_TypeRef: |
| 2254 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2255 | case CXCursor_TemplateRef: |
| 2256 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2257 | case CXCursor_NamespaceRef: |
| 2258 | return createCXString("NamespaceRef"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2259 | case CXCursor_UnexposedExpr: |
| 2260 | return createCXString("UnexposedExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 2261 | case CXCursor_BlockExpr: |
| 2262 | return createCXString("BlockExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2263 | case CXCursor_DeclRefExpr: |
| 2264 | return createCXString("DeclRefExpr"); |
| 2265 | case CXCursor_MemberRefExpr: |
| 2266 | return createCXString("MemberRefExpr"); |
| 2267 | case CXCursor_CallExpr: |
| 2268 | return createCXString("CallExpr"); |
| 2269 | case CXCursor_ObjCMessageExpr: |
| 2270 | return createCXString("ObjCMessageExpr"); |
| 2271 | case CXCursor_UnexposedStmt: |
| 2272 | return createCXString("UnexposedStmt"); |
| 2273 | case CXCursor_InvalidFile: |
| 2274 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 2275 | case CXCursor_InvalidCode: |
| 2276 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2277 | case CXCursor_NoDeclFound: |
| 2278 | return createCXString("NoDeclFound"); |
| 2279 | case CXCursor_NotImplemented: |
| 2280 | return createCXString("NotImplemented"); |
| 2281 | case CXCursor_TranslationUnit: |
| 2282 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 2283 | case CXCursor_UnexposedAttr: |
| 2284 | return createCXString("UnexposedAttr"); |
| 2285 | case CXCursor_IBActionAttr: |
| 2286 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2287 | case CXCursor_IBOutletAttr: |
| 2288 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2289 | case CXCursor_IBOutletCollectionAttr: |
| 2290 | return createCXString("attribute(iboutletcollection)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2291 | case CXCursor_PreprocessingDirective: |
| 2292 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2293 | case CXCursor_MacroDefinition: |
| 2294 | return createCXString("macro definition"); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 2295 | case CXCursor_MacroInstantiation: |
| 2296 | return createCXString("macro instantiation"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 2297 | case CXCursor_Namespace: |
| 2298 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 2299 | case CXCursor_LinkageSpec: |
| 2300 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2301 | case CXCursor_CXXBaseSpecifier: |
| 2302 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 2303 | case CXCursor_Constructor: |
| 2304 | return createCXString("CXXConstructor"); |
| 2305 | case CXCursor_Destructor: |
| 2306 | return createCXString("CXXDestructor"); |
| 2307 | case CXCursor_ConversionFunction: |
| 2308 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 2309 | case CXCursor_TemplateTypeParameter: |
| 2310 | return createCXString("TemplateTypeParameter"); |
| 2311 | case CXCursor_NonTypeTemplateParameter: |
| 2312 | return createCXString("NonTypeTemplateParameter"); |
| 2313 | case CXCursor_TemplateTemplateParameter: |
| 2314 | return createCXString("TemplateTemplateParameter"); |
| 2315 | case CXCursor_FunctionTemplate: |
| 2316 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 2317 | case CXCursor_ClassTemplate: |
| 2318 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 2319 | case CXCursor_ClassTemplatePartialSpecialization: |
| 2320 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2321 | case CXCursor_NamespaceAlias: |
| 2322 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2323 | case CXCursor_UsingDirective: |
| 2324 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 2325 | case CXCursor_UsingDeclaration: |
| 2326 | return createCXString("UsingDeclaration"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 2327 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2328 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 2329 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2330 | return createCXString(NULL); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2331 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 2332 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2333 | enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 2334 | CXCursor parent, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 2335 | CXClientData client_data) { |
| 2336 | CXCursor *BestCursor = static_cast<CXCursor *>(client_data); |
| 2337 | *BestCursor = cursor; |
| 2338 | return CXChildVisit_Recurse; |
| 2339 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2341 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 2342 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 2343 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2344 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2345 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 2346 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 2347 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2348 | // Translate the given source location to make it point at the beginning of |
| 2349 | // the token under the cursor. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2350 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 2351 | |
| 2352 | // Guard against an invalid SourceLocation, or we may assert in one |
| 2353 | // of the following calls. |
| 2354 | if (SLoc.isInvalid()) |
| 2355 | return clang_getNullCursor(); |
| 2356 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2357 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 2358 | CXXUnit->getASTContext().getLangOptions()); |
| 2359 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 2360 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 2361 | if (SLoc.isValid()) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 2362 | // FIXME: Would be great to have a "hint" cursor, then walk from that |
| 2363 | // hint cursor upward until we find a cursor whose source range encloses |
| 2364 | // the region of interest, rather than starting from the translation unit. |
| 2365 | CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2366 | CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2367 | Decl::MaxPCHLevel, SourceLocation(SLoc)); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 2368 | CursorVis.VisitChildren(Parent); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 2369 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2370 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 2373 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 2374 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 2378 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 2379 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2380 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2381 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 2382 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 2383 | } |
| 2384 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2385 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 2386 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 2387 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 2388 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2389 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2390 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 2391 | } |
| 2392 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2393 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 2394 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 2395 | } |
| 2396 | |
| 2397 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 2398 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 2399 | } |
| 2400 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2401 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 2402 | return K == CXCursor_TranslationUnit; |
| 2403 | } |
| 2404 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2405 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 2406 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 2407 | } |
| 2408 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 2409 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 2410 | switch (K) { |
| 2411 | case CXCursor_UnexposedDecl: |
| 2412 | case CXCursor_UnexposedExpr: |
| 2413 | case CXCursor_UnexposedStmt: |
| 2414 | case CXCursor_UnexposedAttr: |
| 2415 | return true; |
| 2416 | default: |
| 2417 | return false; |
| 2418 | } |
| 2419 | } |
| 2420 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2421 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 2422 | return C.kind; |
| 2423 | } |
| 2424 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2425 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 2426 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2427 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2428 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2429 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 2430 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2431 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2434 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2435 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 2436 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2437 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2440 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2441 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 2442 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2443 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2444 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2445 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2446 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2447 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2448 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2449 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2450 | |
| 2451 | case CXCursor_TemplateRef: { |
| 2452 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 2453 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 2454 | } |
| 2455 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2456 | case CXCursor_NamespaceRef: { |
| 2457 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 2458 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 2459 | } |
| 2460 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2461 | case CXCursor_CXXBaseSpecifier: { |
| 2462 | // FIXME: Figure out what location to return for a CXXBaseSpecifier. |
| 2463 | return clang_getNullLocation(); |
| 2464 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2466 | default: |
| 2467 | // FIXME: Need a way to enumerate all non-reference cases. |
| 2468 | llvm_unreachable("Missed a reference kind"); |
| 2469 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2470 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2471 | |
| 2472 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2473 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2474 | getLocationFromExpr(getCursorExpr(C))); |
| 2475 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2476 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 2477 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 2478 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 2479 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 2480 | |
| 2481 | if (C.kind == CXCursor_MacroInstantiation) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 2482 | SourceLocation L |
| 2483 | = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 2484 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 2485 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2486 | |
| 2487 | if (C.kind == CXCursor_MacroDefinition) { |
| 2488 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 2489 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 2490 | } |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2491 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 2492 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2493 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2495 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2496 | SourceLocation Loc = D->getLocation(); |
| 2497 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 2498 | Loc = Class->getClassLoc(); |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 2499 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2500 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 2501 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2502 | } // end extern "C" |
| 2503 | |
| 2504 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 2505 | if (clang_isReference(C.kind)) { |
| 2506 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2507 | case CXCursor_ObjCSuperClassRef: |
| 2508 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2509 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2510 | case CXCursor_ObjCProtocolRef: |
| 2511 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2513 | case CXCursor_ObjCClassRef: |
| 2514 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2515 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2516 | case CXCursor_TypeRef: |
| 2517 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2518 | |
| 2519 | case CXCursor_TemplateRef: |
| 2520 | return getCursorTemplateRef(C).second; |
| 2521 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2522 | case CXCursor_NamespaceRef: |
| 2523 | return getCursorNamespaceRef(C).second; |
| 2524 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2525 | case CXCursor_CXXBaseSpecifier: |
| 2526 | // FIXME: Figure out what source range to use for a CXBaseSpecifier. |
| 2527 | return SourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2529 | default: |
| 2530 | // FIXME: Need a way to enumerate all non-reference cases. |
| 2531 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 2532 | } |
| 2533 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2536 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 2537 | |
| 2538 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2539 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2541 | if (C.kind == CXCursor_PreprocessingDirective) |
| 2542 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 2543 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2544 | if (C.kind == CXCursor_MacroInstantiation) |
| 2545 | return cxcursor::getCursorMacroInstantiation(C)->getSourceRange(); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2547 | if (C.kind == CXCursor_MacroDefinition) |
| 2548 | return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2550 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) |
| 2551 | return getCursorDecl(C)->getSourceRange(); |
| 2552 | |
| 2553 | return SourceRange(); |
| 2554 | } |
| 2555 | |
| 2556 | extern "C" { |
| 2557 | |
| 2558 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 2559 | SourceRange R = getRawCursorExtent(C); |
| 2560 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2561 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 2563 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 2564 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2565 | |
| 2566 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2567 | if (clang_isInvalid(C.kind)) |
| 2568 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2569 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2570 | ASTUnit *CXXUnit = getCursorASTUnit(C); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2571 | if (clang_isDeclaration(C.kind)) |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2572 | return C; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2573 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2574 | if (clang_isExpression(C.kind)) { |
| 2575 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 2576 | if (D) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2577 | return MakeCXCursor(D, CXXUnit); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2578 | return clang_getNullCursor(); |
| 2579 | } |
| 2580 | |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 2581 | if (C.kind == CXCursor_MacroInstantiation) { |
| 2582 | if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition()) |
| 2583 | return MakeMacroDefinitionCursor(Def, CXXUnit); |
| 2584 | } |
| 2585 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2586 | if (!clang_isReference(C.kind)) |
| 2587 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2588 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2589 | switch (C.kind) { |
| 2590 | case CXCursor_ObjCSuperClassRef: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2591 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2592 | |
| 2593 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2594 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2595 | |
| 2596 | case CXCursor_ObjCClassRef: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2597 | return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2598 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2599 | case CXCursor_TypeRef: |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2600 | return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2601 | |
| 2602 | case CXCursor_TemplateRef: |
| 2603 | return MakeCXCursor(getCursorTemplateRef(C).first, CXXUnit); |
| 2604 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2605 | case CXCursor_NamespaceRef: |
| 2606 | return MakeCXCursor(getCursorNamespaceRef(C).first, CXXUnit); |
| 2607 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2608 | case CXCursor_CXXBaseSpecifier: { |
| 2609 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 2610 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 2611 | CXXUnit)); |
| 2612 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2614 | default: |
| 2615 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 2616 | llvm_unreachable("Unhandled reference cursor kind"); |
| 2617 | break; |
| 2618 | } |
| 2619 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 2621 | return clang_getNullCursor(); |
| 2622 | } |
| 2623 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2624 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2625 | if (clang_isInvalid(C.kind)) |
| 2626 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2628 | ASTUnit *CXXUnit = getCursorASTUnit(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2629 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2630 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2631 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2632 | C = clang_getCursorReferenced(C); |
| 2633 | WasReference = true; |
| 2634 | } |
| 2635 | |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 2636 | if (C.kind == CXCursor_MacroInstantiation) |
| 2637 | return clang_getCursorReferenced(C); |
| 2638 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2639 | if (!clang_isDeclaration(C.kind)) |
| 2640 | return clang_getNullCursor(); |
| 2641 | |
| 2642 | Decl *D = getCursorDecl(C); |
| 2643 | if (!D) |
| 2644 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2645 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2646 | switch (D->getKind()) { |
| 2647 | // Declaration kinds that don't really separate the notions of |
| 2648 | // declaration and definition. |
| 2649 | case Decl::Namespace: |
| 2650 | case Decl::Typedef: |
| 2651 | case Decl::TemplateTypeParm: |
| 2652 | case Decl::EnumConstant: |
| 2653 | case Decl::Field: |
| 2654 | case Decl::ObjCIvar: |
| 2655 | case Decl::ObjCAtDefsField: |
| 2656 | case Decl::ImplicitParam: |
| 2657 | case Decl::ParmVar: |
| 2658 | case Decl::NonTypeTemplateParm: |
| 2659 | case Decl::TemplateTemplateParm: |
| 2660 | case Decl::ObjCCategoryImpl: |
| 2661 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 2662 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2663 | case Decl::LinkageSpec: |
| 2664 | case Decl::ObjCPropertyImpl: |
| 2665 | case Decl::FileScopeAsm: |
| 2666 | case Decl::StaticAssert: |
| 2667 | case Decl::Block: |
| 2668 | return C; |
| 2669 | |
| 2670 | // Declaration kinds that don't make any sense here, but are |
| 2671 | // nonetheless harmless. |
| 2672 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2673 | break; |
| 2674 | |
| 2675 | // Declaration kinds for which the definition is not resolvable. |
| 2676 | case Decl::UnresolvedUsingTypename: |
| 2677 | case Decl::UnresolvedUsingValue: |
| 2678 | break; |
| 2679 | |
| 2680 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2681 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 2682 | CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2683 | |
| 2684 | case Decl::NamespaceAlias: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2685 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2686 | |
| 2687 | case Decl::Enum: |
| 2688 | case Decl::Record: |
| 2689 | case Decl::CXXRecord: |
| 2690 | case Decl::ClassTemplateSpecialization: |
| 2691 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2692 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2693 | return MakeCXCursor(Def, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2694 | return clang_getNullCursor(); |
| 2695 | |
| 2696 | case Decl::Function: |
| 2697 | case Decl::CXXMethod: |
| 2698 | case Decl::CXXConstructor: |
| 2699 | case Decl::CXXDestructor: |
| 2700 | case Decl::CXXConversion: { |
| 2701 | const FunctionDecl *Def = 0; |
| 2702 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2703 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2704 | return clang_getNullCursor(); |
| 2705 | } |
| 2706 | |
| 2707 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2708 | // Ask the variable if it has a definition. |
| 2709 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
| 2710 | return MakeCXCursor(Def, CXXUnit); |
| 2711 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2712 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2714 | case Decl::FunctionTemplate: { |
| 2715 | const FunctionDecl *Def = 0; |
| 2716 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2717 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2718 | return clang_getNullCursor(); |
| 2719 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2720 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2721 | case Decl::ClassTemplate: { |
| 2722 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2723 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2724 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2725 | CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2726 | return clang_getNullCursor(); |
| 2727 | } |
| 2728 | |
| 2729 | case Decl::Using: { |
| 2730 | UsingDecl *Using = cast<UsingDecl>(D); |
| 2731 | CXCursor Def = clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2732 | for (UsingDecl::shadow_iterator S = Using->shadow_begin(), |
| 2733 | SEnd = Using->shadow_end(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2734 | S != SEnd; ++S) { |
| 2735 | if (Def != clang_getNullCursor()) { |
| 2736 | // FIXME: We have no way to return multiple results. |
| 2737 | return clang_getNullCursor(); |
| 2738 | } |
| 2739 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2740 | Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2741 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | return Def; |
| 2745 | } |
| 2746 | |
| 2747 | case Decl::UsingShadow: |
| 2748 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2749 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2750 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2751 | |
| 2752 | case Decl::ObjCMethod: { |
| 2753 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 2754 | if (Method->isThisDeclarationADefinition()) |
| 2755 | return C; |
| 2756 | |
| 2757 | // Dig out the method definition in the associated |
| 2758 | // @implementation, if we have it. |
| 2759 | // FIXME: The ASTs should make finding the definition easier. |
| 2760 | if (ObjCInterfaceDecl *Class |
| 2761 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 2762 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 2763 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 2764 | Method->isInstanceMethod())) |
| 2765 | if (Def->isThisDeclarationADefinition()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2766 | return MakeCXCursor(Def, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2767 | |
| 2768 | return clang_getNullCursor(); |
| 2769 | } |
| 2770 | |
| 2771 | case Decl::ObjCCategory: |
| 2772 | if (ObjCCategoryImplDecl *Impl |
| 2773 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2774 | return MakeCXCursor(Impl, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2775 | return clang_getNullCursor(); |
| 2776 | |
| 2777 | case Decl::ObjCProtocol: |
| 2778 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 2779 | return C; |
| 2780 | return clang_getNullCursor(); |
| 2781 | |
| 2782 | case Decl::ObjCInterface: |
| 2783 | // There are two notions of a "definition" for an Objective-C |
| 2784 | // class: the interface and its implementation. When we resolved a |
| 2785 | // reference to an Objective-C class, produce the @interface as |
| 2786 | // the definition; when we were provided with the interface, |
| 2787 | // produce the @implementation as the definition. |
| 2788 | if (WasReference) { |
| 2789 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 2790 | return C; |
| 2791 | } else if (ObjCImplementationDecl *Impl |
| 2792 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2793 | return MakeCXCursor(Impl, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2794 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2796 | case Decl::ObjCProperty: |
| 2797 | // FIXME: We don't really know where to find the |
| 2798 | // ObjCPropertyImplDecls that implement this property. |
| 2799 | return clang_getNullCursor(); |
| 2800 | |
| 2801 | case Decl::ObjCCompatibleAlias: |
| 2802 | if (ObjCInterfaceDecl *Class |
| 2803 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 2804 | if (!Class->isForwardDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2805 | return MakeCXCursor(Class, CXXUnit); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2807 | return clang_getNullCursor(); |
| 2808 | |
| 2809 | case Decl::ObjCForwardProtocol: { |
| 2810 | ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D); |
| 2811 | if (Forward->protocol_size() == 1) |
| 2812 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2813 | MakeCXCursor(*Forward->protocol_begin(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2814 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2815 | |
| 2816 | // FIXME: Cannot return multiple definitions. |
| 2817 | return clang_getNullCursor(); |
| 2818 | } |
| 2819 | |
| 2820 | case Decl::ObjCClass: { |
| 2821 | ObjCClassDecl *Class = cast<ObjCClassDecl>(D); |
| 2822 | if (Class->size() == 1) { |
| 2823 | ObjCInterfaceDecl *IFace = Class->begin()->getInterface(); |
| 2824 | if (!IFace->isForwardDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2825 | return MakeCXCursor(IFace, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2826 | return clang_getNullCursor(); |
| 2827 | } |
| 2828 | |
| 2829 | // FIXME: Cannot return multiple definitions. |
| 2830 | return clang_getNullCursor(); |
| 2831 | } |
| 2832 | |
| 2833 | case Decl::Friend: |
| 2834 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2835 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2836 | return clang_getNullCursor(); |
| 2837 | |
| 2838 | case Decl::FriendTemplate: |
| 2839 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 2840 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 2841 | return clang_getNullCursor(); |
| 2842 | } |
| 2843 | |
| 2844 | return clang_getNullCursor(); |
| 2845 | } |
| 2846 | |
| 2847 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 2848 | if (!clang_isDeclaration(C.kind)) |
| 2849 | return 0; |
| 2850 | |
| 2851 | return clang_getCursorDefinition(C) == C; |
| 2852 | } |
| 2853 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2854 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 2855 | const char **startBuf, |
| 2856 | const char **endBuf, |
| 2857 | unsigned *startLine, |
| 2858 | unsigned *startColumn, |
| 2859 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2860 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 2861 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 2862 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 2863 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 2864 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2865 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 2866 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 2867 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 2868 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 2869 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 2870 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 2871 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 2872 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 2873 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2874 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2875 | void clang_enableStackTraces(void) { |
| 2876 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 2877 | } |
| 2878 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2879 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 2880 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2881 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2882 | // Token-based Operations. |
| 2883 | //===----------------------------------------------------------------------===// |
| 2884 | |
| 2885 | /* CXToken layout: |
| 2886 | * int_data[0]: a CXTokenKind |
| 2887 | * int_data[1]: starting token location |
| 2888 | * int_data[2]: token length |
| 2889 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2890 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2891 | * otherwise unused. |
| 2892 | */ |
| 2893 | extern "C" { |
| 2894 | |
| 2895 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 2896 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 2897 | } |
| 2898 | |
| 2899 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 2900 | switch (clang_getTokenKind(CXTok)) { |
| 2901 | case CXToken_Identifier: |
| 2902 | case CXToken_Keyword: |
| 2903 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2904 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 2905 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2906 | |
| 2907 | case CXToken_Literal: { |
| 2908 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 2909 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2910 | return createCXString(llvm::StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2911 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2913 | case CXToken_Punctuation: |
| 2914 | case CXToken_Comment: |
| 2915 | break; |
| 2916 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2917 | |
| 2918 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2919 | // deconstructing the source location. |
| 2920 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
| 2921 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2922 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2924 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 2925 | std::pair<FileID, unsigned> LocInfo |
| 2926 | = CXXUnit->getSourceManager().getDecomposedLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 2927 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 2928 | llvm::StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 2929 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 2930 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 2931 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2932 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 2933 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2934 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2936 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
| 2937 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
| 2938 | if (!CXXUnit) |
| 2939 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2940 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2941 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 2942 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 2943 | } |
| 2944 | |
| 2945 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
| 2946 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2947 | if (!CXXUnit) |
| 2948 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2949 | |
| 2950 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2951 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 2952 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2954 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 2955 | CXToken **Tokens, unsigned *NumTokens) { |
| 2956 | if (Tokens) |
| 2957 | *Tokens = 0; |
| 2958 | if (NumTokens) |
| 2959 | *NumTokens = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2961 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
| 2962 | if (!CXXUnit || !Tokens || !NumTokens) |
| 2963 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 2965 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 2966 | |
Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 2967 | SourceRange R = cxloc::translateCXSourceRange(Range); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2968 | if (R.isInvalid()) |
| 2969 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2971 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 2972 | std::pair<FileID, unsigned> BeginLocInfo |
| 2973 | = SourceMgr.getDecomposedLoc(R.getBegin()); |
| 2974 | std::pair<FileID, unsigned> EndLocInfo |
| 2975 | = SourceMgr.getDecomposedLoc(R.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2976 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2977 | // Cannot tokenize across files. |
| 2978 | if (BeginLocInfo.first != EndLocInfo.first) |
| 2979 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2980 | |
| 2981 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 2982 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 2983 | llvm::StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 2984 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 2985 | if (Invalid) |
| 2986 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2988 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 2989 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 2990 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2991 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2993 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 2994 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2995 | llvm::SmallVector<CXToken, 32> CXTokens; |
| 2996 | Token Tok; |
| 2997 | do { |
| 2998 | // Lex the next token |
| 2999 | Lex.LexFromRawLexer(Tok); |
| 3000 | if (Tok.is(tok::eof)) |
| 3001 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3003 | // Initialize the CXToken. |
| 3004 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3006 | // - Common fields |
| 3007 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 3008 | CXTok.int_data[2] = Tok.getLength(); |
| 3009 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3010 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3011 | // - Kind-specific fields |
| 3012 | if (Tok.isLiteral()) { |
| 3013 | CXTok.int_data[0] = CXToken_Literal; |
| 3014 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
| 3015 | } else if (Tok.is(tok::identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3016 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3017 | std::pair<FileID, unsigned> LocInfo |
| 3018 | = SourceMgr.getDecomposedLoc(Tok.getLocation()); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3019 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3020 | llvm::StringRef Buf |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 3021 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 3022 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 3023 | return; |
| 3024 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 3025 | const char *StartPos = Buf.data() + LocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3026 | IdentifierInfo *II |
| 3027 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 3028 | |
| 3029 | if (II->getObjCKeywordID() != tok::objc_not_keyword) { |
| 3030 | CXTok.int_data[0] = CXToken_Keyword; |
| 3031 | } |
| 3032 | else { |
| 3033 | CXTok.int_data[0] = II->getTokenID() == tok::identifier? |
| 3034 | CXToken_Identifier |
| 3035 | : CXToken_Keyword; |
| 3036 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3037 | CXTok.ptr_data = II; |
| 3038 | } else if (Tok.is(tok::comment)) { |
| 3039 | CXTok.int_data[0] = CXToken_Comment; |
| 3040 | CXTok.ptr_data = 0; |
| 3041 | } else { |
| 3042 | CXTok.int_data[0] = CXToken_Punctuation; |
| 3043 | CXTok.ptr_data = 0; |
| 3044 | } |
| 3045 | CXTokens.push_back(CXTok); |
| 3046 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3048 | if (CXTokens.empty()) |
| 3049 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3050 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3051 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 3052 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 3053 | *NumTokens = CXTokens.size(); |
| 3054 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3055 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3056 | void clang_disposeTokens(CXTranslationUnit TU, |
| 3057 | CXToken *Tokens, unsigned NumTokens) { |
| 3058 | free(Tokens); |
| 3059 | } |
| 3060 | |
| 3061 | } // end: extern "C" |
| 3062 | |
| 3063 | //===----------------------------------------------------------------------===// |
| 3064 | // Token annotation APIs. |
| 3065 | //===----------------------------------------------------------------------===// |
| 3066 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3067 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3068 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 3069 | CXCursor parent, |
| 3070 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3071 | namespace { |
| 3072 | class AnnotateTokensWorker { |
| 3073 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3074 | CXToken *Tokens; |
| 3075 | CXCursor *Cursors; |
| 3076 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3077 | unsigned TokIdx; |
| 3078 | CursorVisitor AnnotateVis; |
| 3079 | SourceManager &SrcMgr; |
| 3080 | |
| 3081 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 3082 | unsigned NextToken() const { return TokIdx; } |
| 3083 | void AdvanceToken() { ++TokIdx; } |
| 3084 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 3085 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 3086 | } |
| 3087 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3088 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3089 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3090 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
| 3091 | ASTUnit *CXXUnit, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3092 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3093 | NumTokens(numTokens), TokIdx(0), |
| 3094 | AnnotateVis(CXXUnit, AnnotateTokensVisitor, this, |
| 3095 | Decl::MaxPCHLevel, RegionOfInterest), |
| 3096 | SrcMgr(CXXUnit->getSourceManager()) {} |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3097 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3098 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3099 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3100 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3101 | }; |
| 3102 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3103 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3104 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 3105 | // Walk the AST within the region of interest, annotating tokens |
| 3106 | // along the way. |
| 3107 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3108 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3109 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 3110 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 3111 | if (Pos != Annotated.end()) |
| 3112 | Cursors[I] = Pos->second; |
| 3113 | } |
| 3114 | |
| 3115 | // Finish up annotating any tokens left. |
| 3116 | if (!MoreTokens()) |
| 3117 | return; |
| 3118 | |
| 3119 | const CXCursor &C = clang_getNullCursor(); |
| 3120 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 3121 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 3122 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3126 | enum CXChildVisitResult |
| 3127 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3128 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
| 3129 | // We can always annotate a preprocessing directive/macro instantiation. |
| 3130 | if (clang_isPreprocessing(cursor.kind)) { |
| 3131 | Annotated[Loc.int_data] = cursor; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3132 | return CXChildVisit_Recurse; |
| 3133 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3134 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3135 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3136 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3137 | if (cursorRange.isInvalid()) |
| 3138 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3139 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3140 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 3141 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3142 | // Adjust the annotated range based specific declarations. |
| 3143 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 3144 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 3145 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 3146 | // Don't visit synthesized ObjC methods, since they have no syntatic |
| 3147 | // representation in the source. |
| 3148 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 3149 | if (MD->isSynthesized()) |
| 3150 | return CXChildVisit_Continue; |
| 3151 | } |
| 3152 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3153 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) { |
| 3154 | TypeLoc TL = TI->getTypeLoc(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3155 | SourceLocation TLoc = TL.getSourceRange().getBegin(); |
Ted Kremenek | 6bfd533 | 2010-05-13 15:38:38 +0000 | [diff] [blame] | 3156 | if (TLoc.isValid() && |
| 3157 | SrcMgr.isBeforeInTranslationUnit(TLoc, L)) |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3158 | cursorRange.setBegin(TLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 3163 | // If the location of the cursor occurs within a macro instantiation, record |
| 3164 | // the spelling location of the cursor in our annotation map. We can then |
| 3165 | // paper over the token labelings during a post-processing step to try and |
| 3166 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 3167 | // instantiation. |
| 3168 | if (L.isMacroID()) { |
| 3169 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 3170 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 3171 | // directive. Here we assume that the default construction of CXCursor |
| 3172 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 3173 | // this isn't the case, we can fix by doing lookup + insertion. |
| 3174 | |
| 3175 | CXCursor &oldC = Annotated[rawEncoding]; |
| 3176 | if (!clang_isPreprocessing(oldC.kind)) |
| 3177 | oldC = cursor; |
| 3178 | } |
| 3179 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3180 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 3181 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 3182 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 3183 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3184 | |
| 3185 | while (MoreTokens()) { |
| 3186 | const unsigned I = NextToken(); |
| 3187 | SourceLocation TokLoc = GetTokenLoc(I); |
| 3188 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 3189 | case RangeBefore: |
| 3190 | Cursors[I] = updateC; |
| 3191 | AdvanceToken(); |
| 3192 | continue; |
| 3193 | case RangeAfter: |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3194 | case RangeOverlap: |
| 3195 | break; |
| 3196 | } |
| 3197 | break; |
| 3198 | } |
| 3199 | |
| 3200 | // Visit children to get their cursor information. |
| 3201 | const unsigned BeforeChildren = NextToken(); |
| 3202 | VisitChildren(cursor); |
| 3203 | const unsigned AfterChildren = NextToken(); |
| 3204 | |
| 3205 | // Adjust 'Last' to the last token within the extent of the cursor. |
| 3206 | while (MoreTokens()) { |
| 3207 | const unsigned I = NextToken(); |
| 3208 | SourceLocation TokLoc = GetTokenLoc(I); |
| 3209 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 3210 | case RangeBefore: |
| 3211 | assert(0 && "Infeasible"); |
| 3212 | case RangeAfter: |
| 3213 | break; |
| 3214 | case RangeOverlap: |
| 3215 | Cursors[I] = updateC; |
| 3216 | AdvanceToken(); |
| 3217 | continue; |
| 3218 | } |
| 3219 | break; |
| 3220 | } |
| 3221 | const unsigned Last = NextToken(); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3222 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3223 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 3224 | // capture by the child cursors. |
| 3225 | |
| 3226 | // For AST elements within macros, rely on a post-annotate pass to |
| 3227 | // to correctly annotate the tokens with cursors. Otherwise we can |
| 3228 | // get confusing results of having tokens that map to cursors that really |
| 3229 | // are expanded by an instantiation. |
| 3230 | if (L.isMacroID()) |
| 3231 | cursor = clang_getNullCursor(); |
| 3232 | |
| 3233 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 3234 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 3235 | break; |
| 3236 | Cursors[I] = cursor; |
| 3237 | } |
| 3238 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 3239 | // but the child cursors. |
| 3240 | for (unsigned I = AfterChildren; I != Last; ++I) |
| 3241 | Cursors[I] = cursor; |
| 3242 | |
| 3243 | TokIdx = Last; |
| 3244 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3247 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 3248 | CXCursor parent, |
| 3249 | CXClientData client_data) { |
| 3250 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 3251 | } |
| 3252 | |
| 3253 | extern "C" { |
| 3254 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3255 | void clang_annotateTokens(CXTranslationUnit TU, |
| 3256 | CXToken *Tokens, unsigned NumTokens, |
| 3257 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3258 | |
| 3259 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3260 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3261 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3262 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3263 | if (!CXXUnit) { |
| 3264 | // Any token we don't specifically annotate will have a NULL cursor. |
| 3265 | const CXCursor &C = clang_getNullCursor(); |
| 3266 | for (unsigned I = 0; I != NumTokens; ++I) |
| 3267 | Cursors[I] = C; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3268 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3271 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3273 | // Determine the region of interest, which contains all of the tokens. |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3274 | SourceRange RegionOfInterest; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3275 | RegionOfInterest.setBegin(cxloc::translateSourceLocation( |
| 3276 | clang_getTokenLocation(TU, Tokens[0]))); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3277 | RegionOfInterest.setEnd(cxloc::translateSourceLocation( |
| 3278 | clang_getTokenLocation(TU, |
| 3279 | Tokens[NumTokens - 1]))); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3281 | // A mapping from the source locations found when re-lexing or traversing the |
| 3282 | // region of interest to the corresponding cursors. |
| 3283 | AnnotateTokensData Annotated; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3284 | |
| 3285 | // Relex the tokens within the source range to look for preprocessing |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3286 | // directives. |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3287 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 3288 | std::pair<FileID, unsigned> BeginLocInfo |
| 3289 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 3290 | std::pair<FileID, unsigned> EndLocInfo |
| 3291 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3292 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3293 | llvm::StringRef Buffer; |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3294 | bool Invalid = false; |
| 3295 | if (BeginLocInfo.first == EndLocInfo.first && |
| 3296 | ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && |
| 3297 | !Invalid) { |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3298 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 3299 | CXXUnit->getASTContext().getLangOptions(), |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3300 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3301 | Buffer.end()); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3302 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3303 | |
| 3304 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3305 | // entering #includes or expanding macros. |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3306 | while (true) { |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3307 | Token Tok; |
| 3308 | Lex.LexFromRawLexer(Tok); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3309 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3310 | reprocess: |
| 3311 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 3312 | // We have found a preprocessing directive. Gobble it up so that we |
| 3313 | // don't see it while preprocessing these tokens later, but keep track of |
| 3314 | // all of the token locations inside this preprocessing directive so that |
| 3315 | // we can annotate them appropriately. |
| 3316 | // |
| 3317 | // FIXME: Some simple tests here could identify macro definitions and |
| 3318 | // #undefs, to provide specific cursor kinds for those. |
| 3319 | std::vector<SourceLocation> Locations; |
| 3320 | do { |
| 3321 | Locations.push_back(Tok.getLocation()); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3322 | Lex.LexFromRawLexer(Tok); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3323 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3324 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3325 | using namespace cxcursor; |
| 3326 | CXCursor Cursor |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3327 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 3328 | Locations.back()), |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 3329 | CXXUnit); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3330 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 3331 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 3332 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3334 | if (Tok.isAtStartOfLine()) |
| 3335 | goto reprocess; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3337 | continue; |
| 3338 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3340 | if (Tok.is(tok::eof)) |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3341 | break; |
| 3342 | } |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3343 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3344 | |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3345 | // Annotate all of the source locations in the region of interest that map to |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 3346 | // a specific cursor. |
| 3347 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 3348 | CXXUnit, RegionOfInterest); |
| 3349 | W.AnnotateTokens(clang_getTranslationUnitCursor(CXXUnit)); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3350 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3351 | } // end: extern "C" |
| 3352 | |
| 3353 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 3354 | // Operations for querying linkage of a cursor. |
| 3355 | //===----------------------------------------------------------------------===// |
| 3356 | |
| 3357 | extern "C" { |
| 3358 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 3359 | if (!clang_isDeclaration(cursor.kind)) |
| 3360 | return CXLinkage_Invalid; |
| 3361 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 3362 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 3363 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 3364 | switch (ND->getLinkage()) { |
| 3365 | case NoLinkage: return CXLinkage_NoLinkage; |
| 3366 | case InternalLinkage: return CXLinkage_Internal; |
| 3367 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 3368 | case ExternalLinkage: return CXLinkage_External; |
| 3369 | }; |
| 3370 | |
| 3371 | return CXLinkage_Invalid; |
| 3372 | } |
| 3373 | } // end: extern "C" |
| 3374 | |
| 3375 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 3376 | // Operations for querying language of a cursor. |
| 3377 | //===----------------------------------------------------------------------===// |
| 3378 | |
| 3379 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 3380 | switch (D->getKind()) { |
| 3381 | default: |
| 3382 | break; |
| 3383 | case Decl::ImplicitParam: |
| 3384 | case Decl::ObjCAtDefsField: |
| 3385 | case Decl::ObjCCategory: |
| 3386 | case Decl::ObjCCategoryImpl: |
| 3387 | case Decl::ObjCClass: |
| 3388 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 3389 | case Decl::ObjCForwardProtocol: |
| 3390 | case Decl::ObjCImplementation: |
| 3391 | case Decl::ObjCInterface: |
| 3392 | case Decl::ObjCIvar: |
| 3393 | case Decl::ObjCMethod: |
| 3394 | case Decl::ObjCProperty: |
| 3395 | case Decl::ObjCPropertyImpl: |
| 3396 | case Decl::ObjCProtocol: |
| 3397 | return CXLanguage_ObjC; |
| 3398 | case Decl::CXXConstructor: |
| 3399 | case Decl::CXXConversion: |
| 3400 | case Decl::CXXDestructor: |
| 3401 | case Decl::CXXMethod: |
| 3402 | case Decl::CXXRecord: |
| 3403 | case Decl::ClassTemplate: |
| 3404 | case Decl::ClassTemplatePartialSpecialization: |
| 3405 | case Decl::ClassTemplateSpecialization: |
| 3406 | case Decl::Friend: |
| 3407 | case Decl::FriendTemplate: |
| 3408 | case Decl::FunctionTemplate: |
| 3409 | case Decl::LinkageSpec: |
| 3410 | case Decl::Namespace: |
| 3411 | case Decl::NamespaceAlias: |
| 3412 | case Decl::NonTypeTemplateParm: |
| 3413 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 3414 | case Decl::TemplateTemplateParm: |
| 3415 | case Decl::TemplateTypeParm: |
| 3416 | case Decl::UnresolvedUsingTypename: |
| 3417 | case Decl::UnresolvedUsingValue: |
| 3418 | case Decl::Using: |
| 3419 | case Decl::UsingDirective: |
| 3420 | case Decl::UsingShadow: |
| 3421 | return CXLanguage_CPlusPlus; |
| 3422 | } |
| 3423 | |
| 3424 | return CXLanguage_C; |
| 3425 | } |
| 3426 | |
| 3427 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 3428 | |
| 3429 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 3430 | if (clang_isDeclaration(cursor.kind)) |
| 3431 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
| 3432 | if (D->hasAttr<UnavailableAttr>() || |
| 3433 | (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())) |
| 3434 | return CXAvailability_Available; |
| 3435 | |
| 3436 | if (D->hasAttr<DeprecatedAttr>()) |
| 3437 | return CXAvailability_Deprecated; |
| 3438 | } |
| 3439 | |
| 3440 | return CXAvailability_Available; |
| 3441 | } |
| 3442 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 3443 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 3444 | if (clang_isDeclaration(cursor.kind)) |
| 3445 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 3446 | |
| 3447 | return CXLanguage_Invalid; |
| 3448 | } |
| 3449 | } // end: extern "C" |
| 3450 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 3451 | |
| 3452 | //===----------------------------------------------------------------------===// |
| 3453 | // C++ AST instrospection. |
| 3454 | //===----------------------------------------------------------------------===// |
| 3455 | |
| 3456 | extern "C" { |
| 3457 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 3458 | if (!clang_isDeclaration(C.kind)) |
| 3459 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 3460 | |
| 3461 | CXXMethodDecl *Method = 0; |
| 3462 | Decl *D = cxcursor::getCursorDecl(C); |
| 3463 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 3464 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 3465 | else |
| 3466 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 3467 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 3468 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 3469 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 3470 | } // end: extern "C" |
| 3471 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 3472 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 3473 | // Attribute introspection. |
| 3474 | //===----------------------------------------------------------------------===// |
| 3475 | |
| 3476 | extern "C" { |
| 3477 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 3478 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 3479 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorASTUnit(C)); |
| 3480 | |
| 3481 | IBOutletCollectionAttr *A = |
| 3482 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 3483 | |
| 3484 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorASTUnit(C)); |
| 3485 | } |
| 3486 | } // end: extern "C" |
| 3487 | |
| 3488 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3489 | // CXString Operations. |
| 3490 | //===----------------------------------------------------------------------===// |
| 3491 | |
| 3492 | extern "C" { |
| 3493 | const char *clang_getCString(CXString string) { |
| 3494 | return string.Spelling; |
| 3495 | } |
| 3496 | |
| 3497 | void clang_disposeString(CXString string) { |
| 3498 | if (string.MustFreeString && string.Spelling) |
| 3499 | free((void*)string.Spelling); |
| 3500 | } |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 3501 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 3502 | } // end: extern "C" |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 3503 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3504 | namespace clang { namespace cxstring { |
| 3505 | CXString createCXString(const char *String, bool DupString){ |
| 3506 | CXString Str; |
| 3507 | if (DupString) { |
| 3508 | Str.Spelling = strdup(String); |
| 3509 | Str.MustFreeString = 1; |
| 3510 | } else { |
| 3511 | Str.Spelling = String; |
| 3512 | Str.MustFreeString = 0; |
| 3513 | } |
| 3514 | return Str; |
| 3515 | } |
| 3516 | |
| 3517 | CXString createCXString(llvm::StringRef String, bool DupString) { |
| 3518 | CXString Result; |
| 3519 | if (DupString || (!String.empty() && String.data()[String.size()] != 0)) { |
| 3520 | char *Spelling = (char *)malloc(String.size() + 1); |
| 3521 | memmove(Spelling, String.data(), String.size()); |
| 3522 | Spelling[String.size()] = 0; |
| 3523 | Result.Spelling = Spelling; |
| 3524 | Result.MustFreeString = 1; |
| 3525 | } else { |
| 3526 | Result.Spelling = String.data(); |
| 3527 | Result.MustFreeString = 0; |
| 3528 | } |
| 3529 | return Result; |
| 3530 | } |
| 3531 | }} |
| 3532 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 3533 | //===----------------------------------------------------------------------===// |
| 3534 | // Misc. utility functions. |
| 3535 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3536 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 3537 | extern "C" { |
| 3538 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 3539 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3540 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | } // end: extern "C" |