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