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