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