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