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