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