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