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