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