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