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