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