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