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