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