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