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