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