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