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