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