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