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