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