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