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