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