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