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