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