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