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