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