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