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 | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame^] | 28 | #ifndef _MSC_VER |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 29 | #include <dlfcn.h> |
Ted Kremenek | 8c4195e | 2009-10-15 22:10:56 +0000 | [diff] [blame] | 30 | #include <sys/wait.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: |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 271 | explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {} |
| 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; } |
| 280 | |
| 281 | private: |
| 282 | bool OnlyLocalDecls; |
| 283 | }; |
| 284 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 287 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 288 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 289 | static const char *clangPath; |
| 290 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 291 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 292 | { |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame^] | 293 | // FIXME: This is a hack to unbreak the MSVC build. |
| 294 | #ifdef _MSC_VER |
| 295 | llvm::sys::Path CIndexPath(""); |
| 296 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 297 | // Find the location where this library lives (libCIndex.dylib). |
| 298 | // We do the lookup here to avoid poking dladdr too many times. |
| 299 | // This silly cast below avoids a C++ warning. |
| 300 | Dl_info info; |
| 301 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 302 | assert(0 && "Call to dladdr() failed"); |
| 303 | |
| 304 | llvm::sys::Path CIndexPath(info.dli_fname); |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame^] | 305 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 306 | std::string CIndexDir = CIndexPath.getDirname(); |
| 307 | |
| 308 | // We now have the CIndex directory, locate clang relative to it. |
| 309 | std::string ClangPath = CIndexDir + "/../bin/clang"; |
| 310 | |
| 311 | clangPath = ClangPath.c_str(); |
| 312 | |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 313 | return new CIndexer(new Program()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 316 | void clang_disposeIndex(CXIndex CIdx) |
| 317 | { |
| 318 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 319 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 322 | // FIXME: need to pass back error info. |
| 323 | CXTranslationUnit clang_createTranslationUnit( |
| 324 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 325 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 326 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 327 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 328 | std::string astName(ast_filename); |
| 329 | std::string ErrMsg; |
| 330 | |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 331 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(), |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 332 | CXXIdx->getFileManager(), &ErrMsg, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 333 | CXXIdx->getOnlyLocalDecls(), |
| 334 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 337 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 338 | CXIndex CIdx, |
| 339 | const char *source_filename, |
| 340 | int num_command_line_args, const char **command_line_args) |
| 341 | { |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame^] | 342 | // FIXME: This is a hack to unbreak the build. |
| 343 | #ifdef _MSC_VER |
| 344 | return 0; |
| 345 | #else |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 346 | // Build up the arguments for involing clang. |
| 347 | std::vector<const char *> argv; |
| 348 | argv.push_back(clangPath); |
| 349 | argv.push_back("-emit-ast"); |
| 350 | argv.push_back(source_filename); |
| 351 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 352 | // Generate a temporary name for the AST file. |
| 353 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 354 | argv.push_back(tmpnam(astTmpFile)); |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 355 | 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] | 356 | argv.push_back(command_line_args[i]); |
| 357 | argv.push_back(NULL); |
| 358 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 359 | // Generate the AST file in a separate process. |
| 360 | pid_t child_pid = fork(); |
| 361 | if (child_pid == 0) { // Child process |
| 362 | |
| 363 | // Execute the command, passing the appropriate arguments. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 364 | execv(argv[0], (char *const *)&argv[0]); |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 365 | |
| 366 | // If execv returns, it failed. |
| 367 | assert(0 && "execv() failed"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 368 | } |
| 369 | // This is run by the parent. |
| 370 | int child_status; |
| 371 | pid_t tpid; |
| 372 | do { // Wait for the child to terminate. |
| 373 | tpid = wait(&child_status); |
| 374 | } while (tpid != child_pid); |
| 375 | |
| 376 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 377 | ASTUnit *ATU = static_cast<ASTUnit *>( |
| 378 | clang_createTranslationUnit(CIdx, astTmpFile)); |
| 379 | ATU->unlinkTemporaryFile(); |
| 380 | return ATU; |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame^] | 381 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 384 | void clang_disposeTranslationUnit( |
| 385 | CXTranslationUnit CTUnit) |
| 386 | { |
| 387 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 388 | delete static_cast<ASTUnit *>(CTUnit); |
| 389 | } |
| 390 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 391 | void clang_wantOnlyLocalDeclarations(CXIndex CIdx) { |
| 392 | static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true); |
| 393 | } |
| 394 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 395 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 396 | { |
| 397 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 398 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 399 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 400 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 401 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 402 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 403 | CXTranslationUnitIterator callback, |
| 404 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 405 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 406 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 407 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 408 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 409 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 410 | TUVisitor DVisit(CTUnit, callback, CData, |
| 411 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 412 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 415 | void clang_loadDeclaration(CXDecl Dcl, |
| 416 | CXDeclIterator callback, |
| 417 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 418 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 419 | assert(Dcl && "Passed null CXDecl"); |
| 420 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 421 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 422 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 423 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 426 | // Some notes on CXEntity: |
| 427 | // |
| 428 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 429 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 430 | // entity for 2 different types). For example: |
| 431 | // |
| 432 | // module1.m: @interface Foo @end Foo *x; |
| 433 | // module2.m: void Foo(int); |
| 434 | // |
| 435 | // - Since the unique name spans translation units, static data/functions |
| 436 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 437 | // As a result, there will be no entity for the following: |
| 438 | // |
| 439 | // module.m: static void Foo() { } |
| 440 | // |
| 441 | |
| 442 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 443 | const char *clang_getDeclarationName(CXEntity) |
| 444 | { |
| 445 | return ""; |
| 446 | } |
| 447 | const char *clang_getURI(CXEntity) |
| 448 | { |
| 449 | return ""; |
| 450 | } |
| 451 | |
| 452 | CXEntity clang_getEntity(const char *URI) |
| 453 | { |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | // |
| 458 | // CXDecl Operations. |
| 459 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 460 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 461 | { |
| 462 | return 0; |
| 463 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 464 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 465 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 466 | assert(AnonDecl && "Passed null CXDecl"); |
| 467 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 468 | |
| 469 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 470 | return OMD->getSelector().getAsString().c_str(); |
| 471 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 472 | if (ND->getIdentifier()) |
| 473 | return ND->getIdentifier()->getName(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 474 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 475 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 476 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 477 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 478 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 479 | { |
| 480 | assert(AnonDecl && "Passed null CXDecl"); |
| 481 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 482 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 483 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 484 | } |
| 485 | |
| 486 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 487 | { |
| 488 | assert(AnonDecl && "Passed null CXDecl"); |
| 489 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 490 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 491 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 494 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 495 | { |
| 496 | assert(AnonDecl && "Passed null CXDecl"); |
| 497 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 498 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 499 | return SourceMgr.getBufferName(ND->getLocation()); |
| 500 | } |
| 501 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 502 | const char *clang_getCursorSpelling(CXCursor C) |
| 503 | { |
| 504 | assert(C.decl && "CXCursor has null decl"); |
| 505 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 506 | |
| 507 | if (clang_isReference(C.kind)) { |
| 508 | switch (C.kind) { |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 509 | case CXCursor_ObjCSuperClassRef: |
| 510 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 511 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 512 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 513 | return OID->getSuperClass()->getIdentifier()->getName(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 514 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 515 | case CXCursor_ObjCClassRef: |
| 516 | { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 517 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 518 | return OID->getIdentifier()->getName(); |
| 519 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 520 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 521 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 522 | return OID->getClassInterface()->getIdentifier()->getName(); |
| 523 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 524 | case CXCursor_ObjCProtocolRef: |
| 525 | { |
| 526 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 527 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 528 | return OID->getIdentifier()->getName(); |
| 529 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 530 | case CXCursor_ObjCSelectorRef: |
| 531 | { |
| 532 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 533 | static_cast<Stmt *>(C.stmt)); |
| 534 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 535 | return OME->getSelector().getAsString().c_str(); |
| 536 | } |
| 537 | case CXCursor_VarRef: |
| 538 | case CXCursor_FunctionRef: |
| 539 | case CXCursor_EnumConstantRef: |
| 540 | { |
| 541 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 542 | static_cast<Stmt *>(C.stmt)); |
| 543 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 544 | return DRE->getDecl()->getIdentifier()->getName(); |
| 545 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 546 | default: |
| 547 | return "<not implemented>"; |
| 548 | } |
| 549 | } |
| 550 | return clang_getDeclSpelling(C.decl); |
| 551 | } |
| 552 | |
| 553 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 554 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 555 | switch (Kind) { |
| 556 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 557 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 558 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 559 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 560 | case CXCursor_StructDecl: return "StructDecl"; |
| 561 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 562 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 563 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 564 | case CXCursor_VarDecl: return "VarDecl"; |
| 565 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 566 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 567 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 568 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 569 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 570 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 571 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 572 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 573 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 574 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 575 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 576 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 577 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 578 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 579 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 580 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 581 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 582 | |
| 583 | case CXCursor_VarRef: return "VarRef"; |
| 584 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 585 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 586 | case CXCursor_MemberRef: return "MemberRef"; |
| 587 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 588 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 589 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 590 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 591 | default: return "<not implemented>"; |
| 592 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 593 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 594 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 595 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 596 | switch (D->getKind()) { |
| 597 | case Decl::Function: return CXCursor_FunctionDecl; |
| 598 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 599 | case Decl::Enum: return CXCursor_EnumDecl; |
| 600 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 601 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 602 | case Decl::Field: return CXCursor_FieldDecl; |
| 603 | case Decl::Var: return CXCursor_VarDecl; |
| 604 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 605 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 606 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 607 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 608 | case Decl::ObjCMethod: { |
| 609 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 610 | if (MD->isInstanceMethod()) |
| 611 | return CXCursor_ObjCInstanceMethodDecl; |
| 612 | return CXCursor_ObjCClassMethodDecl; |
| 613 | } |
| 614 | default: break; |
| 615 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 616 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 617 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 618 | // |
| 619 | // CXCursor Operations. |
| 620 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 621 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 622 | unsigned line, unsigned column) |
| 623 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 624 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 625 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 626 | |
| 627 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 628 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 629 | source_name+strlen(source_name)); |
| 630 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 631 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 632 | return C; |
| 633 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 634 | SourceLocation SLoc = |
| 635 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 636 | |
| 637 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); |
| 638 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 639 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 640 | if (ALoc.isNamedRef()) |
| 641 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 642 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 643 | if (Dcl) { |
| 644 | if (Stm) { |
| 645 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 646 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 647 | return C; |
| 648 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 649 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 650 | return C; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 651 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 652 | // Fall through...treat as a decl, not a ref. |
| 653 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 654 | if (ALoc.isNamedRef()) { |
| 655 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 656 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 657 | return C; |
| 658 | } |
| 659 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 660 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 661 | return C; |
| 662 | } |
| 663 | } |
| 664 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 665 | return C; |
| 666 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 667 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 668 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 671 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 672 | { |
| 673 | assert(AnonDecl && "Passed null CXDecl"); |
| 674 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 675 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 676 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 677 | return C; |
| 678 | } |
| 679 | |
| 680 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 681 | { |
| 682 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 683 | } |
| 684 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 685 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 686 | { |
| 687 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 688 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 689 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 690 | unsigned clang_isReference(enum CXCursorKind K) |
| 691 | { |
| 692 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 693 | } |
| 694 | |
| 695 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 696 | { |
| 697 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 698 | } |
| 699 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 700 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 701 | { |
| 702 | return C.kind; |
| 703 | } |
| 704 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 705 | static Decl *getDeclFromExpr(Stmt *E) { |
| 706 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 707 | return RefExpr->getDecl(); |
| 708 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 709 | return ME->getMemberDecl(); |
| 710 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 711 | return RE->getDecl(); |
| 712 | |
| 713 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 714 | return getDeclFromExpr(CE->getCallee()); |
| 715 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 716 | return getDeclFromExpr(CE->getSubExpr()); |
| 717 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 718 | return OME->getMethodDecl(); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 723 | CXDecl clang_getCursorDecl(CXCursor C) |
| 724 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 725 | if (clang_isDeclaration(C.kind)) |
| 726 | return C.decl; |
| 727 | |
| 728 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 729 | if (C.stmt) { |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 730 | if (C.kind == CXCursor_ObjCClassRef || |
| 731 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 732 | return static_cast<Stmt *>(C.stmt); |
| 733 | else |
| 734 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 735 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 736 | return C.decl; |
| 737 | } |
| 738 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 741 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 742 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 743 | SourceManager &SourceMgr, |
| 744 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 745 | if (clang_isReference(C.kind)) { |
| 746 | switch (C.kind) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 747 | case CXCursor_ObjCClassRef: |
| 748 | { |
| 749 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 750 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 751 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 752 | return parentDecl->getLocation(); |
| 753 | } |
| 754 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 755 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 756 | return OID->getClassInterface()->getLocation(); |
| 757 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 758 | case CXCursor_ObjCSuperClassRef: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 759 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 760 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 761 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 762 | return OID->getSuperClassLoc(); |
| 763 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 764 | case CXCursor_ObjCProtocolRef: |
| 765 | { |
| 766 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 767 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 768 | return OID->getLocation(); |
| 769 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 770 | case CXCursor_ObjCSelectorRef: |
| 771 | { |
| 772 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 773 | static_cast<Stmt *>(C.stmt)); |
| 774 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 775 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 776 | } |
| 777 | case CXCursor_VarRef: |
| 778 | case CXCursor_FunctionRef: |
| 779 | case CXCursor_EnumConstantRef: |
| 780 | { |
| 781 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 782 | static_cast<Stmt *>(C.stmt)); |
| 783 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 784 | return DRE->getLocation(); |
| 785 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 786 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 787 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 788 | } |
| 789 | } else { // We have a declaration or a definition. |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 790 | SourceLocation SLoc; |
| 791 | switch (ND->getKind()) { |
| 792 | case Decl::ObjCInterface: |
| 793 | { |
| 794 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 795 | break; |
| 796 | } |
| 797 | case Decl::ObjCProtocol: |
| 798 | { |
| 799 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 800 | break; |
| 801 | } |
| 802 | default: |
| 803 | { |
| 804 | SLoc = ND->getLocation(); |
| 805 | break; |
| 806 | } |
| 807 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 808 | if (SLoc.isInvalid()) |
| 809 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 810 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 811 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 814 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 815 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 816 | assert(C.decl && "CXCursor has null decl"); |
| 817 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 818 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 819 | |
| 820 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 821 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 822 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 823 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 824 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 825 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 826 | assert(C.decl && "CXCursor has null decl"); |
| 827 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 828 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 829 | |
| 830 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 831 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 832 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 833 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 834 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 835 | assert(C.decl && "CXCursor has null decl"); |
| 836 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 837 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 838 | |
| 839 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 840 | if (SLoc.isFileID()) |
| 841 | return SourceMgr.getBufferName(SLoc); |
| 842 | |
| 843 | // Retrieve the file in which the macro was instantiated, then provide that |
| 844 | // buffer name. |
| 845 | // FIXME: Do we want to give specific macro-instantiation information? |
| 846 | const llvm::MemoryBuffer *Buffer |
| 847 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 848 | if (!Buffer) |
| 849 | return 0; |
| 850 | |
| 851 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 854 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 855 | const char **startBuf, |
| 856 | const char **endBuf, |
| 857 | unsigned *startLine, |
| 858 | unsigned *startColumn, |
| 859 | unsigned *endLine, |
| 860 | unsigned *endColumn) |
| 861 | { |
| 862 | assert(C.decl && "CXCursor has null decl"); |
| 863 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 864 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 865 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 866 | |
| 867 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 868 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 869 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 870 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 871 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 872 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 873 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 874 | } |
| 875 | |
| 876 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 877 | } // end extern "C" |