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 | |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | // Visitors. |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 120 | namespace { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 121 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 122 | NamedDecl *D = DRE->getDecl(); |
| 123 | if (isa<VarDecl>(D)) |
| 124 | return CXCursor_VarRef; |
| 125 | else if (isa<FunctionDecl>(D)) |
| 126 | return CXCursor_FunctionRef; |
| 127 | else if (isa<EnumConstantDecl>(D)) |
| 128 | return CXCursor_EnumConstantRef; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 129 | else |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 130 | return CXCursor_NotImplemented; |
| 131 | } |
| 132 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 133 | // Translation Unit Visitor. |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 134 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 135 | class TUVisitor : public DeclVisitor<TUVisitor> { |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 136 | public: |
| 137 | typedef void (*Iterator)(void *, CXCursor, CXClientData); |
| 138 | private: |
| 139 | void *Root; // CXDecl or CXTranslationUnit |
| 140 | Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator. |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 141 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 143 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 144 | // to the visitor. Declarations with a PCH level greater than this value will |
| 145 | // be suppressed. |
| 146 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 148 | void Call(const CXCursor &C) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 149 | if (clang_isInvalid(C.kind)) |
| 150 | return; |
| 151 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 152 | if (const Decl *D = getCursorDecl(C)) { |
| 153 | // Filter any declarations that have a PCH level greater than what |
| 154 | // we allow. |
| 155 | if (D->getPCHLevel() > MaxPCHLevel) |
| 156 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 158 | // Filter any implicit declarations (since the source info will be bogus). |
| 159 | if (D->isImplicit()) |
| 160 | return; |
| 161 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 163 | Callback(Root, C, CData); |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 164 | } |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 165 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 166 | public: |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 167 | TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) : |
| 168 | Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 170 | void VisitDecl(Decl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 171 | void VisitDeclContext(DeclContext *DC); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 172 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 173 | }; |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 175 | void TUVisitor::VisitDecl(Decl *D) { |
| 176 | Call(MakeCXCursor(D)); |
| 177 | } |
| 178 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 179 | void TUVisitor::VisitDeclContext(DeclContext *DC) { |
| 180 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 181 | I != E; ++I) |
| 182 | Visit(*I); |
| 183 | } |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 185 | void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 186 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 187 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 188 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 189 | // Declaration visitor. |
| 190 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 191 | CXDecl CDecl; |
| 192 | CXDeclIterator Callback; |
| 193 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 194 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 195 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 196 | // to the visitor. Declarations with a PCH level greater than this value will |
| 197 | // be suppressed. |
| 198 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 199 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 200 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 201 | // Disable the callback when the context is equal to the visiting decl. |
| 202 | if (CDecl == ND && !clang_isReference(CK)) |
| 203 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 204 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 205 | // Filter any declarations that have a PCH level greater than what we allow. |
| 206 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 207 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 209 | CXCursor C = { CK, { ND, 0, 0 } }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 210 | Callback(CDecl, C, CData); |
| 211 | } |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 212 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 213 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 214 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 215 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 216 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 218 | void VisitDeclContext(DeclContext *DC); |
| 219 | void VisitEnumConstantDecl(EnumConstantDecl *ND); |
| 220 | void VisitFieldDecl(FieldDecl *ND); |
| 221 | void VisitFunctionDecl(FunctionDecl *ND); |
| 222 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 223 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 224 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 225 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 226 | void VisitObjCIvarDecl(ObjCIvarDecl *ND); |
| 227 | void VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 228 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND); |
| 229 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 230 | void VisitParmVarDecl(ParmVarDecl *ND); |
| 231 | void VisitTagDecl(TagDecl *D); |
| 232 | void VisitVarDecl(VarDecl *ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 233 | }; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 234 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 236 | void CDeclVisitor::VisitDeclContext(DeclContext *DC) { |
| 237 | for (DeclContext::decl_iterator |
| 238 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 239 | Visit(*I); |
| 240 | } |
| 241 | |
| 242 | void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) { |
| 243 | Call(CXCursor_EnumConstantDecl, ND); |
| 244 | } |
| 245 | |
| 246 | void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) { |
| 247 | Call(CXCursor_FieldDecl, ND); |
| 248 | } |
| 249 | |
| 250 | void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
| 251 | if (ND->isThisDeclarationADefinition()) { |
| 252 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 253 | #if 0 |
| 254 | // Not currently needed. |
| 255 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
| 256 | CRefVisitor RVisit(CDecl, Callback, CData); |
| 257 | RVisit.Visit(Body); |
| 258 | #endif |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 263 | // Issue callbacks for the containing class. |
| 264 | Call(CXCursor_ObjCClassRef, ND); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 265 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 266 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 267 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 268 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 269 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 270 | } |
| 271 | |
| 272 | void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 273 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 274 | } |
| 275 | |
| 276 | void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 277 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 278 | } |
| 279 | |
| 280 | void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 281 | // Issue callbacks for super class. |
| 282 | if (D->getSuperClass()) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 283 | Callback(CDecl, |
| 284 | MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 285 | D->getSuperClassLoc()), |
| 286 | CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 288 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 289 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 290 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 291 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 292 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 293 | } |
| 294 | |
| 295 | void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
| 296 | Call(CXCursor_ObjCIvarDecl, ND); |
| 297 | } |
| 298 | |
| 299 | void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 300 | if (ND->getBody()) { |
| 301 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 302 | : CXCursor_ObjCClassMethodDefn, ND); |
| 303 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 304 | } else |
| 305 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 306 | : CXCursor_ObjCClassMethodDecl, ND); |
| 307 | } |
| 308 | |
| 309 | void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 310 | Call(CXCursor_ObjCPropertyDecl, ND); |
| 311 | } |
| 312 | |
| 313 | void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 314 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 315 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 316 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 317 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 318 | |
| 319 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 320 | } |
| 321 | |
| 322 | void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) { |
| 323 | Call(CXCursor_ParmDecl, ND); |
| 324 | } |
| 325 | |
| 326 | void CDeclVisitor::VisitTagDecl(TagDecl *D) { |
| 327 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 328 | } |
| 329 | |
| 330 | void CDeclVisitor::VisitVarDecl(VarDecl *ND) { |
| 331 | Call(CXCursor_VarDecl, ND); |
| 332 | } |
| 333 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 334 | static SourceLocation getLocationFromCursor(CXCursor C, |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 335 | SourceManager &SourceMgr, |
| 336 | NamedDecl *ND) { |
| 337 | if (clang_isReference(C.kind)) { |
Ted Kremenek | 8b456e8 | 2010-01-15 18:24:18 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 339 | if (Decl *D = getCursorReferringDecl(C)) |
Ted Kremenek | 8b456e8 | 2010-01-15 18:24:18 +0000 | [diff] [blame] | 340 | return D->getLocation(); |
| 341 | |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 342 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 343 | case CXCursor_ObjCClassRef: { |
| 344 | if (isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 345 | NamedDecl *parentDecl = getCursorInterfaceParent(C); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 346 | return parentDecl->getLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 347 | } |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 348 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 349 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 350 | return OID->getClassInterface()->getLocation(); |
| 351 | } |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 352 | case CXCursor_ObjCSuperClassRef: |
| 353 | return getCursorObjCSuperClassRef(C).second; |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 354 | case CXCursor_ObjCProtocolRef: |
| 355 | return getCursorObjCProtocolRef(C).second; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 356 | case CXCursor_ObjCSelectorRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 357 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C)); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 358 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 359 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 360 | } |
| 361 | case CXCursor_VarRef: |
| 362 | case CXCursor_FunctionRef: |
| 363 | case CXCursor_EnumConstantRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 364 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C)); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 365 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 366 | return DRE->getLocation(); |
| 367 | } |
| 368 | default: |
| 369 | return SourceLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 370 | } |
| 371 | } else { // We have a declaration or a definition. |
| 372 | SourceLocation SLoc; |
| 373 | switch (ND->getKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 374 | case Decl::ObjCInterface: { |
| 375 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 376 | break; |
| 377 | } |
| 378 | case Decl::ObjCProtocol: { |
| 379 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 380 | break; |
| 381 | } |
| 382 | default: { |
| 383 | SLoc = ND->getLocation(); |
| 384 | break; |
| 385 | } |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 386 | } |
| 387 | if (SLoc.isInvalid()) |
| 388 | return SourceLocation(); |
| 389 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
| 390 | } |
| 391 | } |
| 392 | |
Daniel Dunbar | 140fce2 | 2010-01-12 02:34:07 +0000 | [diff] [blame] | 393 | CXString CIndexer::createCXString(const char *String, bool DupString){ |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 394 | CXString Str; |
| 395 | if (DupString) { |
| 396 | Str.Spelling = strdup(String); |
| 397 | Str.MustFreeString = 1; |
| 398 | } else { |
| 399 | Str.Spelling = String; |
| 400 | Str.MustFreeString = 0; |
| 401 | } |
| 402 | return Str; |
| 403 | } |
| 404 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 405 | extern "C" { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 406 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 407 | int displayDiagnostics) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 408 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 409 | if (excludeDeclarationsFromPCH) |
| 410 | CIdxr->setOnlyLocalDecls(); |
| 411 | if (displayDiagnostics) |
| 412 | CIdxr->setDisplayDiagnostics(); |
| 413 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 416 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 417 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 418 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 421 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
| 422 | assert(CIdx && "Passed null CXIndex"); |
| 423 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 424 | CXXIdx->setUseExternalASTGeneration(value); |
| 425 | } |
| 426 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 427 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 428 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 429 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 430 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 431 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 432 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 433 | return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), |
| 434 | CXXIdx->getOnlyLocalDecls(), |
| 435 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 438 | CXTranslationUnit |
| 439 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 440 | const char *source_filename, |
| 441 | int num_command_line_args, |
| 442 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 443 | assert(CIdx && "Passed null CXIndex"); |
| 444 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 445 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 446 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 447 | llvm::SmallVector<const char *, 16> Args; |
| 448 | |
| 449 | // The 'source_filename' argument is optional. If the caller does not |
| 450 | // specify it then it is assumed that the source file is specified |
| 451 | // in the actual argument list. |
| 452 | if (source_filename) |
| 453 | Args.push_back(source_filename); |
| 454 | Args.insert(Args.end(), command_line_args, |
| 455 | command_line_args + num_command_line_args); |
| 456 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 457 | unsigned NumErrors = CXXIdx->getDiags().getNumErrors(); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 459 | #ifdef USE_CRASHTRACER |
| 460 | ArgsCrashTracerInfo ACTI(Args); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 461 | #endif |
| 462 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 463 | llvm::OwningPtr<ASTUnit> Unit( |
| 464 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 465 | CXXIdx->getDiags(), |
| 466 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 467 | CXXIdx->getOnlyLocalDecls(), |
| 468 | /* UseBumpAllocator = */ true)); |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 469 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 470 | // FIXME: Until we have broader testing, just drop the entire AST if we |
| 471 | // encountered an error. |
| 472 | if (NumErrors != CXXIdx->getDiags().getNumErrors()) |
| 473 | return 0; |
| 474 | |
| 475 | return Unit.take(); |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 478 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 479 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 481 | // First add the complete path to the 'clang' executable. |
| 482 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 483 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 484 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 485 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 486 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 488 | // The 'source_filename' argument is optional. If the caller does not |
| 489 | // specify it then it is assumed that the source file is specified |
| 490 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 491 | if (source_filename) |
| 492 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 493 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 494 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 495 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 496 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 497 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 498 | |
| 499 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 500 | for (int i = 0; i < num_command_line_args; ++i) |
| 501 | if (const char *arg = command_line_args[i]) { |
| 502 | if (strcmp(arg, "-o") == 0) { |
| 503 | ++i; // Also skip the matching argument. |
| 504 | continue; |
| 505 | } |
| 506 | if (strcmp(arg, "-emit-ast") == 0 || |
| 507 | strcmp(arg, "-c") == 0 || |
| 508 | strcmp(arg, "-fsyntax-only") == 0) { |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | // Keep the argument. |
| 513 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 514 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 516 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 517 | argv.push_back(NULL); |
| 518 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 519 | // Invoke 'clang'. |
| 520 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 521 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 522 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 523 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 524 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 525 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 526 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 528 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 529 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 530 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 531 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 532 | I!=E; ++I) { |
| 533 | if (*I) |
| 534 | llvm::errs() << ' ' << *I << '\n'; |
| 535 | } |
| 536 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 537 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 538 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 539 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 540 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 541 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 542 | if (ATU) |
| 543 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 544 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 547 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 548 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 549 | delete static_cast<ASTUnit *>(CTUnit); |
| 550 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 551 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 552 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 553 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 554 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 555 | return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(), |
| 556 | true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 557 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 559 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 560 | CXTranslationUnitIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 561 | CXClientData CData) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 562 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 563 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 564 | ASTContext &Ctx = CXXUnit->getASTContext(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 565 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 566 | unsigned PCHLevel = Decl::MaxPCHLevel; |
| 567 | |
| 568 | // Set the PCHLevel to filter out unwanted decls if requested. |
| 569 | if (CXXUnit->getOnlyLocalDecls()) { |
| 570 | PCHLevel = 0; |
| 571 | |
| 572 | // If the main input was an AST, bump the level. |
| 573 | if (CXXUnit->isMainFileAST()) |
| 574 | ++PCHLevel; |
| 575 | } |
| 576 | |
| 577 | TUVisitor DVisit(CTUnit, callback, CData, PCHLevel); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 578 | |
| 579 | // If using a non-AST based ASTUnit, iterate over the stored list of top-level |
| 580 | // decls. |
| 581 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) { |
| 582 | const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); |
| 583 | for (std::vector<Decl*>::const_iterator it = TLDs.begin(), |
| 584 | ie = TLDs.end(); it != ie; ++it) { |
| 585 | DVisit.Visit(*it); |
| 586 | } |
| 587 | } else |
| 588 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 591 | void clang_loadDeclaration(CXDecl Dcl, |
| 592 | CXDeclIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 593 | CXClientData CData) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 594 | assert(Dcl && "Passed null CXDecl"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 596 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 597 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 598 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 599 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 600 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 601 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 602 | //===----------------------------------------------------------------------===// |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 603 | // CXDecl Operations. |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 604 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 605 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 606 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
| 607 | SourceLocation SLoc) { |
| 608 | FileID FID; |
| 609 | if (SLoc.isFileID()) |
| 610 | FID = SMgr.getFileID(SLoc); |
| 611 | else |
| 612 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 613 | return SMgr.getFileEntryForID(FID); |
| 614 | } |
| 615 | |
| 616 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 617 | CXString clang_getDeclSpelling(CXDecl AnonDecl) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 618 | assert(AnonDecl && "Passed null CXDecl"); |
| 619 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 620 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 621 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 622 | return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(), |
| 623 | true); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 624 | |
| 625 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 626 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 627 | // and returns different names. NamedDecl returns the class name and |
| 628 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 629 | return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 630 | |
| 631 | if (ND->getIdentifier()) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 632 | return CIndexer::createCXString(ND->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 633 | |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 634 | return CIndexer::createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 635 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 636 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 637 | unsigned clang_getDeclLine(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 638 | assert(AnonDecl && "Passed null CXDecl"); |
| 639 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 640 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 641 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 642 | } |
| 643 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 644 | unsigned clang_getDeclColumn(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 645 | assert(AnonDecl && "Passed null CXDecl"); |
| 646 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 647 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 648 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 649 | } |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 650 | |
| 651 | CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) { |
| 652 | assert(AnonDecl && "Passed null CXDecl"); |
| 653 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 654 | SourceManager &SM = ND->getASTContext().getSourceManager(); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 655 | SourceRange R = ND->getSourceRange(); |
| 656 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 657 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 658 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 659 | |
| 660 | if (!Begin.isValid()) { |
| 661 | CXDeclExtent extent = { { 0, 0 }, { 0, 0 } }; |
| 662 | return extent; |
| 663 | } |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 664 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 665 | // FIXME: This is largely copy-paste from |
| 666 | ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is |
| 667 | // what we want the two routines should be refactored. |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 668 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 669 | // If the End location and the start location are the same and are a macro |
| 670 | // location, then the range was something that came from a macro expansion |
| 671 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 672 | // get the range. If this is a function-like macro, we'd also like to |
| 673 | // get the arguments. |
| 674 | if (Begin == End && R.getEnd().isMacroID()) |
| 675 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 676 | |
| 677 | assert(SM.getFileID(Begin) == SM.getFileID(End)); |
| 678 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
| 679 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 680 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 681 | // Compute the column number of the start. Keep the column based at 1. |
| 682 | unsigned StartColNo = SM.getInstantiationColumnNumber(Begin); |
| 683 | |
| 684 | // Compute the column number of the end. |
| 685 | unsigned EndColNo = SM.getInstantiationColumnNumber(End); |
| 686 | if (EndColNo) { |
| 687 | // Offset the end column by 1 so that we point to the last character |
| 688 | // in the last token. |
| 689 | --EndColNo; |
| 690 | |
| 691 | // Add in the length of the token, so that we cover multi-char tokens. |
| 692 | ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext(); |
| 693 | const LangOptions &LOpts = Ctx.getLangOptions(); |
| 694 | |
| 695 | EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts); |
| 696 | } |
| 697 | |
| 698 | // Package up the line/column data and return to the caller. |
| 699 | CXDeclExtent extent = { { StartLineNo, StartColNo }, |
| 700 | { EndLineNo, EndColNo } }; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 701 | return extent; |
| 702 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 703 | |
Ted Kremenek | 6ab9db1 | 2010-01-08 17:11:32 +0000 | [diff] [blame] | 704 | const char *clang_getDeclSource(CXDecl AnonDecl) { |
| 705 | assert(AnonDecl && "Passed null CXDecl"); |
| 706 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 707 | assert(FEnt && "Cannot find FileEntry for Decl"); |
| 708 | return clang_getFileName(FEnt); |
| 709 | } |
| 710 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 711 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 712 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 713 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 714 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 715 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 716 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 717 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 718 | } // end: extern "C" |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 720 | //===----------------------------------------------------------------------===// |
| 721 | // CXFile Operations. |
| 722 | //===----------------------------------------------------------------------===// |
| 723 | |
| 724 | extern "C" { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 725 | const char *clang_getFileName(CXFile SFile) { |
| 726 | assert(SFile && "Passed null CXFile"); |
| 727 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 728 | return FEnt->getName(); |
| 729 | } |
| 730 | |
| 731 | time_t clang_getFileTime(CXFile SFile) { |
| 732 | assert(SFile && "Passed null CXFile"); |
| 733 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 734 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 735 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 736 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 738 | //===----------------------------------------------------------------------===// |
| 739 | // CXCursor Operations. |
| 740 | //===----------------------------------------------------------------------===// |
| 741 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 742 | static Decl *getDeclFromExpr(Stmt *E) { |
| 743 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 744 | return RefExpr->getDecl(); |
| 745 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 746 | return ME->getMemberDecl(); |
| 747 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 748 | return RE->getDecl(); |
| 749 | |
| 750 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 751 | return getDeclFromExpr(CE->getCallee()); |
| 752 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 753 | return getDeclFromExpr(CE->getSubExpr()); |
| 754 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 755 | return OME->getMethodDecl(); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 761 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 762 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
| 763 | assert(ND && "CXCursor has null decl"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 764 | if (clang_isReference(C.kind)) { |
| 765 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 766 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 767 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
| 768 | return CIndexer::createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 769 | } |
| 770 | case CXCursor_ObjCClassRef: { |
| 771 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 772 | return CIndexer::createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 773 | |
| 774 | ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND); |
| 775 | assert(OCD && "clang_getCursorLine(): Missing category decl"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 776 | return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier() |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 777 | ->getNameStart()); |
| 778 | } |
| 779 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 780 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 781 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 782 | return CIndexer::createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 783 | } |
| 784 | case CXCursor_ObjCSelectorRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 785 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C)); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 786 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 787 | return CIndexer::createCXString(OME->getSelector().getAsString().c_str(), |
| 788 | true); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 789 | } |
| 790 | case CXCursor_VarRef: |
| 791 | case CXCursor_FunctionRef: |
| 792 | case CXCursor_EnumConstantRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 793 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C)); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 794 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 795 | return CIndexer::createCXString(DRE->getDecl()->getIdentifier() |
| 796 | ->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 797 | } |
| 798 | default: |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 799 | return CIndexer::createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 802 | return clang_getDeclSpelling(getCursorDecl(C)); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 805 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 806 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 807 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 808 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 809 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 810 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 811 | case CXCursor_StructDecl: return "StructDecl"; |
| 812 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 813 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 814 | case CXCursor_FieldDecl: return "FieldDecl"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 815 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 816 | case CXCursor_VarDecl: return "VarDecl"; |
| 817 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 818 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 819 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 820 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 821 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 822 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 823 | case CXCursor_ObjCIvarRef: return "ObjCIvarRef"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 824 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 825 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 826 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 827 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 828 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 829 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 830 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 831 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 832 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
| 833 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 835 | case CXCursor_VarRef: return "VarRef"; |
| 836 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 837 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 838 | case CXCursor_MemberRef: return "MemberRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 839 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 840 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 841 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 842 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 843 | } |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 844 | |
| 845 | llvm_unreachable("Unhandled CXCursorKind"); |
| 846 | return NULL; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 847 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 848 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 849 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 850 | unsigned line, unsigned column) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 851 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 852 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 853 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 854 | FileManager &FMgr = CXXUnit->getFileManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 855 | const FileEntry *File = FMgr.getFile(source_name, |
| 856 | source_name+strlen(source_name)); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 857 | if (!File) |
| 858 | return clang_getNullCursor(); |
| 859 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 860 | SourceLocation SLoc = |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 861 | CXXUnit->getSourceManager().getLocation(File, line, column); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 862 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 863 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 864 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 865 | &LastLoc); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 866 | |
| 867 | // FIXME: This doesn't look thread-safe. |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 868 | if (ALoc.isValid()) |
| 869 | CXXUnit->setLastASTLocation(ALoc); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 870 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 871 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 872 | if (ALoc.isNamedRef()) |
| 873 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 874 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 875 | if (Dcl) { |
| 876 | if (Stm) { |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 877 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) |
| 878 | return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm); |
| 879 | else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) |
| 880 | return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 881 | // Fall through...treat as a decl, not a ref. |
| 882 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 883 | if (ALoc.isNamedRef()) { |
| 884 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 885 | CXCursor C = { CXCursor_ObjCClassRef, { Dcl, ALoc.getParentDecl(), 0 }}; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 886 | return C; |
| 887 | } |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 888 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl)) |
| 889 | return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 890 | } |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 891 | return MakeCXCursor(Dcl); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 892 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 893 | return MakeCXCursor(CXCursor_NoDeclFound, 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 896 | CXCursor clang_getNullCursor(void) { |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 897 | return MakeCXCursor(CXCursor_InvalidFile, 0); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 901 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 902 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 903 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 904 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 905 | assert(AnonDecl && "Passed null CXDecl"); |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 906 | return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl)); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 909 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 910 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 911 | } |
| 912 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 913 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 914 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 915 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 916 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 917 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 918 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 919 | } |
| 920 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 921 | unsigned clang_isDefinition(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 922 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 923 | } |
| 924 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 925 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 926 | return C.kind; |
| 927 | } |
| 928 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 929 | CXDecl clang_getCursorDecl(CXCursor C) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 930 | if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind)) |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 931 | return getCursorDecl(C); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 932 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 933 | if (clang_isReference(C.kind)) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 934 | if (getCursorStmt(C)) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame^] | 935 | if (C.kind == CXCursor_ObjCClassRef) |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 936 | return getCursorStmt(C); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 937 | else |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 938 | return getDeclFromExpr(getCursorStmt(C)); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 939 | } else |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 940 | return getCursorDecl(C); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 941 | } |
| 942 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 945 | unsigned clang_getCursorLine(CXCursor C) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 946 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 947 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 948 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 949 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 950 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 951 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 952 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 953 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 954 | unsigned clang_getCursorColumn(CXCursor C) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 955 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 956 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 957 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 958 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 959 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 960 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 961 | } |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 962 | |
| 963 | const char *clang_getCursorSource(CXCursor C) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 964 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 965 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 966 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 967 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 968 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 970 | if (SLoc.isFileID()) { |
| 971 | const char *bufferName = SourceMgr.getBufferName(SLoc); |
| 972 | return bufferName[0] == '<' ? NULL : bufferName; |
| 973 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 975 | // Retrieve the file in which the macro was instantiated, then provide that |
| 976 | // buffer name. |
| 977 | // FIXME: Do we want to give specific macro-instantiation information? |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 978 | const llvm::MemoryBuffer *Buffer |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 979 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 980 | if (!Buffer) |
| 981 | return 0; |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 983 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 986 | CXFile clang_getCursorSourceFile(CXCursor C) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 987 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 988 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 989 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 990 | |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 991 | return (void *) |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 992 | getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr, |
| 993 | ND)); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 996 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 997 | const char **startBuf, |
| 998 | const char **endBuf, |
| 999 | unsigned *startLine, |
| 1000 | unsigned *startColumn, |
| 1001 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1002 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 1003 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 1004 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1005 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1006 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1008 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1009 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1010 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1011 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1012 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1013 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1014 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1015 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1016 | |
| 1017 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1018 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1019 | //===----------------------------------------------------------------------===// |
| 1020 | // CXString Operations. |
| 1021 | //===----------------------------------------------------------------------===// |
| 1022 | |
| 1023 | extern "C" { |
| 1024 | const char *clang_getCString(CXString string) { |
| 1025 | return string.Spelling; |
| 1026 | } |
| 1027 | |
| 1028 | void clang_disposeString(CXString string) { |
| 1029 | if (string.MustFreeString && string.Spelling) |
| 1030 | free((void*)string.Spelling); |
| 1031 | } |
| 1032 | } // end: extern "C" |