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