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