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 | // |
| 10 | // This file implements the Clang-C Source Indexing library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang-c/Index.h" |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Version.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 23 | #include "clang/Index/ASTLocation.h" |
| 24 | #include "clang/Index/Indexer.h" |
| 25 | #include "clang/Index/Program.h" |
| 26 | #include "clang/Index/Utils.h" |
| 27 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 29 | #include "llvm/Config/config.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 33 | #include "llvm/System/Path.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 34 | #include "llvm/System/Program.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 35 | |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 36 | #include <cstdio> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 37 | #include <vector> |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 38 | #include <sstream> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 39 | |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 40 | #ifdef LLVM_ON_WIN32 |
| 41 | #define WIN32_LEAN_AND_MEAN |
| 42 | #include <windows.h> |
| 43 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 44 | #include <dlfcn.h> |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 45 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 46 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 47 | using namespace clang; |
| 48 | using namespace idx; |
| 49 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 50 | namespace { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 51 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 52 | { |
| 53 | NamedDecl *D = DRE->getDecl(); |
| 54 | if (isa<VarDecl>(D)) |
| 55 | return CXCursor_VarRef; |
| 56 | else if (isa<FunctionDecl>(D)) |
| 57 | return CXCursor_FunctionRef; |
| 58 | else if (isa<EnumConstantDecl>(D)) |
| 59 | return CXCursor_EnumConstantRef; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 60 | else |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 61 | return CXCursor_NotImplemented; |
| 62 | } |
| 63 | |
| 64 | #if 0 |
| 65 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 66 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 67 | CXDecl CDecl; |
| 68 | CXDeclIterator Callback; |
| 69 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 70 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 71 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 72 | CXCursor C = { CK, CDecl, SRef }; |
| 73 | Callback(CDecl, C, CData); |
| 74 | } |
| 75 | |
| 76 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 77 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 78 | CDecl(C), Callback(cback), CData(D) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 79 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 80 | void VisitStmt(Stmt *S) { |
| 81 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 82 | C != CEnd; ++C) |
| 83 | Visit(*C); |
| 84 | } |
| 85 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 86 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 87 | } |
| 88 | void VisitMemberExpr(MemberExpr *Node) { |
| 89 | Call(CXCursor_MemberRef, Node); |
| 90 | } |
| 91 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 92 | Call(CXCursor_ObjCSelectorRef, Node); |
| 93 | } |
| 94 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 95 | Call(CXCursor_ObjCIvarRef, Node); |
| 96 | } |
| 97 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 98 | #endif |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 100 | /// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted |
| 101 | /// warnings and errors. |
| 102 | class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient { |
| 103 | public: |
| 104 | virtual ~IgnoreDiagnosticsClient() {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 105 | virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {} |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 106 | }; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 107 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 108 | // Translation Unit Visitor. |
| 109 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 110 | CXTranslationUnit TUnit; |
| 111 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 112 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 114 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 115 | // to the visitor. Declarations with a PCH level greater than this value will |
| 116 | // be suppressed. |
| 117 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 118 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 119 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 120 | // Filter any declarations that have a PCH level greater than what we allow. |
| 121 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 122 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 123 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 124 | // Filter any implicit declarations (since the source info will be bogus). |
| 125 | if (ND->isImplicit()) |
| 126 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 127 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 128 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 129 | Callback(TUnit, C, CData); |
| 130 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 131 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 132 | TUVisitor(CXTranslationUnit CTU, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 133 | CXTranslationUnitIterator cback, CXClientData D, |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 134 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 135 | TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 136 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 137 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 138 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 139 | } |
| 140 | void VisitDeclContext(DeclContext *DC) { |
| 141 | for (DeclContext::decl_iterator |
| 142 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 143 | Visit(*I); |
| 144 | } |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 145 | |
| 146 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 147 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 148 | : CXCursor_FunctionDecl, ND); |
| 149 | } |
| 150 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 151 | Call(CXCursor_ObjCCategoryDecl, ND); |
| 152 | } |
| 153 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 154 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 155 | } |
| 156 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 157 | Call(CXCursor_ObjCClassDefn, ND); |
| 158 | } |
| 159 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
| 160 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 161 | } |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 162 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
| 163 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 164 | } |
| 165 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 166 | switch (ND->getTagKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 167 | case TagDecl::TK_struct: |
| 168 | Call(CXCursor_StructDecl, ND); |
| 169 | break; |
| 170 | case TagDecl::TK_class: |
| 171 | Call(CXCursor_ClassDecl, ND); |
| 172 | break; |
| 173 | case TagDecl::TK_union: |
| 174 | Call(CXCursor_UnionDecl, ND); |
| 175 | break; |
| 176 | case TagDecl::TK_enum: |
| 177 | Call(CXCursor_EnumDecl, ND); |
| 178 | break; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 179 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 180 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 181 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 182 | Call(CXCursor_TypedefDecl, ND); |
| 183 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 184 | void VisitVarDecl(VarDecl *ND) { |
| 185 | Call(CXCursor_VarDecl, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 186 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 187 | }; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 188 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 189 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 190 | // Declaration visitor. |
| 191 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 192 | CXDecl CDecl; |
| 193 | CXDeclIterator Callback; |
| 194 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 196 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 197 | // to the visitor. Declarations with a PCH level greater than this value will |
| 198 | // be suppressed. |
| 199 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 200 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 201 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 202 | // Disable the callback when the context is equal to the visiting decl. |
| 203 | if (CDecl == ND && !clang_isReference(CK)) |
| 204 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 206 | // Filter any declarations that have a PCH level greater than what we allow. |
| 207 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 208 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 209 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 210 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 211 | Callback(CDecl, C, CData); |
| 212 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 213 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 214 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 215 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 216 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 217 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 218 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 219 | // Issue callbacks for the containing class. |
| 220 | Call(CXCursor_ObjCClassRef, ND); |
| 221 | // FIXME: Issue callbacks for protocol refs. |
| 222 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 223 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 224 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 225 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 226 | if (D->getSuperClass()) |
| 227 | Call(CXCursor_ObjCSuperClassRef, D); |
| 228 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 229 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 230 | E = D->protocol_end(); I != E; ++I) |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 231 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 232 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 233 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 234 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 235 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 236 | E = PID->protocol_end(); I != E; ++I) |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 237 | Call(CXCursor_ObjCProtocolRef, *I); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 238 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 239 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 240 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 241 | void VisitTagDecl(TagDecl *D) { |
| 242 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 243 | } |
| 244 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 245 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 246 | } |
| 247 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 248 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 249 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 250 | void VisitDeclContext(DeclContext *DC) { |
| 251 | for (DeclContext::decl_iterator |
| 252 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 253 | Visit(*I); |
| 254 | } |
| 255 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 256 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 257 | } |
| 258 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 259 | Call(CXCursor_FieldDecl, ND); |
| 260 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 261 | void VisitVarDecl(VarDecl *ND) { |
| 262 | Call(CXCursor_VarDecl, ND); |
| 263 | } |
| 264 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 265 | Call(CXCursor_ParmDecl, ND); |
| 266 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 267 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 268 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 269 | } |
| 270 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 271 | Call(CXCursor_ObjCIvarDecl, ND); |
| 272 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 273 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 274 | if (ND->isThisDeclarationADefinition()) { |
| 275 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 276 | #if 0 |
| 277 | // Not currently needed. |
| 278 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 279 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 280 | RVisit.Visit(Body); |
| 281 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 284 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 285 | if (ND->getBody()) { |
| 286 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 287 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 288 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 289 | } else |
| 290 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 291 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 292 | } |
| 293 | }; |
| 294 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 295 | class CIndexer : public Indexer { |
Daniel Dunbar | bb3503a | 2009-12-06 09:56:30 +0000 | [diff] [blame] | 296 | DiagnosticOptions DiagOpts; |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 297 | IgnoreDiagnosticsClient IgnoreDiagClient; |
| 298 | llvm::OwningPtr<Diagnostic> TextDiags; |
| 299 | Diagnostic IgnoreDiags; |
| 300 | bool UseExternalASTGeneration; |
| 301 | bool OnlyLocalDecls; |
| 302 | bool DisplayDiagnostics; |
| 303 | |
| 304 | llvm::sys::Path ClangPath; |
| 305 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 306 | public: |
| 307 | explicit CIndexer(Program *prog) : Indexer(*prog), |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 308 | IgnoreDiags(&IgnoreDiagClient), |
| 309 | UseExternalASTGeneration(false), |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 310 | OnlyLocalDecls(false), |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 311 | DisplayDiagnostics(false) { |
| 312 | TextDiags.reset( |
Daniel Dunbar | bb3503a | 2009-12-06 09:56:30 +0000 | [diff] [blame] | 313 | CompilerInstance::createDiagnostics(DiagOpts, 0, 0)); |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 314 | } |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 315 | |
| 316 | virtual ~CIndexer() { delete &getProgram(); } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 317 | |
| 318 | /// \brief Whether we only want to see "local" declarations (that did not |
| 319 | /// come from a previous precompiled header). If false, we want to see all |
| 320 | /// declarations. |
| 321 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 322 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
Benjamin Kramer | 9670762 | 2009-10-18 11:10:55 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 324 | bool getDisplayDiagnostics() const { return DisplayDiagnostics; } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 325 | void setDisplayDiagnostics(bool Display = true) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 326 | DisplayDiagnostics = Display; |
| 327 | } |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 328 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 329 | bool getUseExternalASTGeneration() const { return UseExternalASTGeneration; } |
| 330 | void setUseExternalASTGeneration(bool Value) { |
| 331 | UseExternalASTGeneration = Value; |
| 332 | } |
| 333 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 334 | Diagnostic &getDiags() { |
| 335 | return DisplayDiagnostics ? *TextDiags : IgnoreDiags; |
| 336 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 337 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 338 | /// \brief Get the path of the clang binary. |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 339 | const llvm::sys::Path& getClangPath(); |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 340 | |
| 341 | /// \brief Get the path of the clang resource files. |
| 342 | std::string getClangResourcesPath(); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 343 | }; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 344 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 345 | const llvm::sys::Path& CIndexer::getClangPath() { |
| 346 | // Did we already compute the path? |
| 347 | if (!ClangPath.empty()) |
| 348 | return ClangPath; |
| 349 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 350 | // Find the location where this library lives (libCIndex.dylib). |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 351 | #ifdef LLVM_ON_WIN32 |
| 352 | MEMORY_BASIC_INFORMATION mbi; |
| 353 | char path[MAX_PATH]; |
Benjamin Kramer | edcd828 | 2009-10-18 16:20:58 +0000 | [diff] [blame] | 354 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 355 | sizeof(mbi)); |
| 356 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
| 357 | |
| 358 | llvm::sys::Path CIndexPath(path); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 359 | |
| 360 | CIndexPath.eraseComponent(); |
| 361 | CIndexPath.appendComponent("clang"); |
| 362 | CIndexPath.appendSuffix("exe"); |
| 363 | CIndexPath.makeAbsolute(); |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 364 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 365 | // This silly cast below avoids a C++ warning. |
| 366 | Dl_info info; |
| 367 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 368 | assert(0 && "Call to dladdr() failed"); |
| 369 | |
| 370 | llvm::sys::Path CIndexPath(info.dli_fname); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 371 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 372 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 373 | CIndexPath.eraseComponent(); |
| 374 | CIndexPath.eraseComponent(); |
| 375 | CIndexPath.appendComponent("bin"); |
| 376 | CIndexPath.appendComponent("clang"); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 377 | #endif |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 378 | |
| 379 | // Cache our result. |
| 380 | ClangPath = CIndexPath; |
| 381 | return ClangPath; |
| 382 | } |
| 383 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 384 | std::string CIndexer::getClangResourcesPath() { |
| 385 | llvm::sys::Path P = getClangPath(); |
| 386 | |
| 387 | if (!P.empty()) { |
| 388 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 389 | P.eraseComponent(); // Remove /bin from foo/bin |
| 390 | |
| 391 | // Get foo/lib/clang/<version>/include |
| 392 | P.appendComponent("lib"); |
| 393 | P.appendComponent("clang"); |
| 394 | P.appendComponent(CLANG_VERSION_STRING); |
| 395 | } |
| 396 | |
| 397 | return P.str(); |
| 398 | } |
| 399 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 402 | static SourceLocation getLocationFromCursor(CXCursor C, |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 403 | SourceManager &SourceMgr, |
| 404 | NamedDecl *ND) { |
| 405 | if (clang_isReference(C.kind)) { |
| 406 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 407 | case CXCursor_ObjCClassRef: { |
| 408 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 409 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 410 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 411 | return parentDecl->getLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 412 | } |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 413 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 414 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 415 | return OID->getClassInterface()->getLocation(); |
| 416 | } |
| 417 | case CXCursor_ObjCSuperClassRef: { |
| 418 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 419 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 420 | return OID->getSuperClassLoc(); |
| 421 | } |
| 422 | case CXCursor_ObjCProtocolRef: { |
| 423 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 424 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 425 | return OID->getLocation(); |
| 426 | } |
| 427 | case CXCursor_ObjCSelectorRef: { |
| 428 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 429 | static_cast<Stmt *>(C.stmt)); |
| 430 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 431 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 432 | } |
| 433 | case CXCursor_VarRef: |
| 434 | case CXCursor_FunctionRef: |
| 435 | case CXCursor_EnumConstantRef: { |
| 436 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 437 | static_cast<Stmt *>(C.stmt)); |
| 438 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 439 | return DRE->getLocation(); |
| 440 | } |
| 441 | default: |
| 442 | return SourceLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 443 | } |
| 444 | } else { // We have a declaration or a definition. |
| 445 | SourceLocation SLoc; |
| 446 | switch (ND->getKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 447 | case Decl::ObjCInterface: { |
| 448 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 449 | break; |
| 450 | } |
| 451 | case Decl::ObjCProtocol: { |
| 452 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 453 | break; |
| 454 | } |
| 455 | default: { |
| 456 | SLoc = ND->getLocation(); |
| 457 | break; |
| 458 | } |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 459 | } |
| 460 | if (SLoc.isInvalid()) |
| 461 | return SourceLocation(); |
| 462 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
| 463 | } |
| 464 | } |
| 465 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 466 | static CXString createCXString(const char *String, bool DupString = false) { |
| 467 | CXString Str; |
| 468 | if (DupString) { |
| 469 | Str.Spelling = strdup(String); |
| 470 | Str.MustFreeString = 1; |
| 471 | } else { |
| 472 | Str.Spelling = String; |
| 473 | Str.MustFreeString = 0; |
| 474 | } |
| 475 | return Str; |
| 476 | } |
| 477 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 478 | extern "C" { |
| 479 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 480 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 481 | int displayDiagnostics) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 482 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 483 | if (excludeDeclarationsFromPCH) |
| 484 | CIdxr->setOnlyLocalDecls(); |
| 485 | if (displayDiagnostics) |
| 486 | CIdxr->setDisplayDiagnostics(); |
| 487 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 490 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 491 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 492 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 495 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
| 496 | assert(CIdx && "Passed null CXIndex"); |
| 497 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 498 | CXXIdx->setUseExternalASTGeneration(value); |
| 499 | } |
| 500 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 501 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 502 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 503 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 504 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 505 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 506 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 507 | return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), |
| 508 | CXXIdx->getOnlyLocalDecls(), |
| 509 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 512 | CXTranslationUnit |
| 513 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 514 | const char *source_filename, |
| 515 | int num_command_line_args, |
| 516 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 517 | assert(CIdx && "Passed null CXIndex"); |
| 518 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 519 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 520 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 521 | llvm::SmallVector<const char *, 16> Args; |
| 522 | |
| 523 | // The 'source_filename' argument is optional. If the caller does not |
| 524 | // specify it then it is assumed that the source file is specified |
| 525 | // in the actual argument list. |
| 526 | if (source_filename) |
| 527 | Args.push_back(source_filename); |
| 528 | Args.insert(Args.end(), command_line_args, |
| 529 | command_line_args + num_command_line_args); |
| 530 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 531 | unsigned NumErrors = CXXIdx->getDiags().getNumErrors(); |
| 532 | llvm::OwningPtr<ASTUnit> Unit( |
| 533 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 534 | CXXIdx->getDiags(), |
| 535 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 536 | CXXIdx->getOnlyLocalDecls(), |
| 537 | /* UseBumpAllocator = */ true)); |
| 538 | |
| 539 | // FIXME: Until we have broader testing, just drop the entire AST if we |
| 540 | // encountered an error. |
| 541 | if (NumErrors != CXXIdx->getDiags().getNumErrors()) |
| 542 | return 0; |
| 543 | |
| 544 | return Unit.take(); |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 547 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 548 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 549 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 550 | // First add the complete path to the 'clang' executable. |
| 551 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 552 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 553 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 554 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 555 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 556 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 557 | // The 'source_filename' argument is optional. If the caller does not |
| 558 | // specify it then it is assumed that the source file is specified |
| 559 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 560 | if (source_filename) |
| 561 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 562 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 563 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 564 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 565 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 566 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 567 | |
| 568 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 569 | for (int i = 0; i < num_command_line_args; ++i) |
| 570 | if (const char *arg = command_line_args[i]) { |
| 571 | if (strcmp(arg, "-o") == 0) { |
| 572 | ++i; // Also skip the matching argument. |
| 573 | continue; |
| 574 | } |
| 575 | if (strcmp(arg, "-emit-ast") == 0 || |
| 576 | strcmp(arg, "-c") == 0 || |
| 577 | strcmp(arg, "-fsyntax-only") == 0) { |
| 578 | continue; |
| 579 | } |
| 580 | |
| 581 | // Keep the argument. |
| 582 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 583 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 584 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 585 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 586 | argv.push_back(NULL); |
| 587 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 588 | // Invoke 'clang'. |
| 589 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 590 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 591 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 592 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 593 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 594 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 595 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 597 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 598 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 599 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 600 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 601 | I!=E; ++I) { |
| 602 | if (*I) |
| 603 | llvm::errs() << ' ' << *I << '\n'; |
| 604 | } |
| 605 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 606 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 607 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 608 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 609 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 610 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 611 | if (ATU) |
| 612 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 613 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 616 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 617 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 618 | delete static_cast<ASTUnit *>(CTUnit); |
| 619 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 620 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 621 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 622 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 623 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 624 | return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 625 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 626 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 627 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 628 | CXTranslationUnitIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 629 | CXClientData CData) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 630 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 631 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 632 | ASTContext &Ctx = CXXUnit->getASTContext(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 633 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 634 | unsigned PCHLevel = Decl::MaxPCHLevel; |
| 635 | |
| 636 | // Set the PCHLevel to filter out unwanted decls if requested. |
| 637 | if (CXXUnit->getOnlyLocalDecls()) { |
| 638 | PCHLevel = 0; |
| 639 | |
| 640 | // If the main input was an AST, bump the level. |
| 641 | if (CXXUnit->isMainFileAST()) |
| 642 | ++PCHLevel; |
| 643 | } |
| 644 | |
| 645 | TUVisitor DVisit(CTUnit, callback, CData, PCHLevel); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 646 | |
| 647 | // If using a non-AST based ASTUnit, iterate over the stored list of top-level |
| 648 | // decls. |
| 649 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) { |
| 650 | const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); |
| 651 | for (std::vector<Decl*>::const_iterator it = TLDs.begin(), |
| 652 | ie = TLDs.end(); it != ie; ++it) { |
| 653 | DVisit.Visit(*it); |
| 654 | } |
| 655 | } else |
| 656 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 659 | void clang_loadDeclaration(CXDecl Dcl, |
| 660 | CXDeclIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 661 | CXClientData CData) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 662 | assert(Dcl && "Passed null CXDecl"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 664 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 665 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 666 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 669 | // Some notes on CXEntity: |
| 670 | // |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 671 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 672 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 673 | // entity for 2 different types). For example: |
| 674 | // |
| 675 | // module1.m: @interface Foo @end Foo *x; |
| 676 | // module2.m: void Foo(int); |
| 677 | // |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 678 | // - Since the unique name spans translation units, static data/functions |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 679 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 680 | // As a result, there will be no entity for the following: |
| 681 | // |
| 682 | // module.m: static void Foo() { } |
| 683 | // |
| 684 | |
| 685 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 686 | const char *clang_getDeclarationName(CXEntity) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 687 | return ""; |
| 688 | } |
| 689 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 690 | const char *clang_getURI(CXEntity) { |
| 691 | return ""; |
| 692 | } |
| 693 | |
| 694 | CXEntity clang_getEntity(const char *URI) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | // |
| 699 | // CXDecl Operations. |
| 700 | // |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 701 | |
| 702 | CXEntity clang_getEntityFromDecl(CXDecl) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 703 | return 0; |
| 704 | } |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 705 | |
| 706 | CXString clang_getDeclSpelling(CXDecl AnonDecl) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 707 | assert(AnonDecl && "Passed null CXDecl"); |
| 708 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 709 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 710 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 711 | return createCXString(OMD->getSelector().getAsString().c_str(), true); |
| 712 | |
| 713 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 714 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 715 | // and returns different names. NamedDecl returns the class name and |
| 716 | // ObjCCategoryImplDecl returns the category name. |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 717 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
| 718 | |
| 719 | if (ND->getIdentifier()) |
| 720 | return createCXString(ND->getIdentifier()->getNameStart()); |
| 721 | |
| 722 | return createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 723 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 724 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 725 | unsigned clang_getDeclLine(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 726 | assert(AnonDecl && "Passed null CXDecl"); |
| 727 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 728 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 729 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 730 | } |
| 731 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 732 | unsigned clang_getDeclColumn(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 733 | assert(AnonDecl && "Passed null CXDecl"); |
| 734 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 735 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 736 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 739 | const char *clang_getDeclSource(CXDecl AnonDecl) { |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 740 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 741 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 742 | assert (FEnt && "Cannot find FileEntry for Decl"); |
| 743 | return clang_getFileName(FEnt); |
| 744 | } |
| 745 | |
| 746 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 747 | SourceLocation SLoc) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 748 | FileID FID; |
| 749 | if (SLoc.isFileID()) |
| 750 | FID = SMgr.getFileID(SLoc); |
| 751 | else |
| 752 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 753 | return SMgr.getFileEntryForID(FID); |
| 754 | } |
| 755 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 756 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 757 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 758 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 759 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 760 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 761 | } |
| 762 | |
| 763 | const char *clang_getFileName(CXFile SFile) { |
| 764 | assert(SFile && "Passed null CXFile"); |
| 765 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 766 | return FEnt->getName(); |
| 767 | } |
| 768 | |
| 769 | time_t clang_getFileTime(CXFile SFile) { |
| 770 | assert(SFile && "Passed null CXFile"); |
| 771 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 772 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 775 | CXString clang_getCursorSpelling(CXCursor C) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 776 | assert(C.decl && "CXCursor has null decl"); |
| 777 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 778 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 779 | if (clang_isReference(C.kind)) { |
| 780 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 781 | case CXCursor_ObjCSuperClassRef: { |
| 782 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 783 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 784 | return createCXString(OID->getSuperClass()->getIdentifier() |
| 785 | ->getNameStart()); |
| 786 | } |
| 787 | case CXCursor_ObjCClassRef: { |
| 788 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 789 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 790 | |
| 791 | ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND); |
| 792 | assert(OCD && "clang_getCursorLine(): Missing category decl"); |
| 793 | return createCXString(OCD->getClassInterface()->getIdentifier() |
| 794 | ->getNameStart()); |
| 795 | } |
| 796 | case CXCursor_ObjCProtocolRef: { |
| 797 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 798 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 799 | return createCXString(OID->getIdentifier()->getNameStart()); |
| 800 | } |
| 801 | case CXCursor_ObjCSelectorRef: { |
| 802 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 803 | static_cast<Stmt *>(C.stmt)); |
| 804 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 805 | return createCXString(OME->getSelector().getAsString().c_str(), true); |
| 806 | } |
| 807 | case CXCursor_VarRef: |
| 808 | case CXCursor_FunctionRef: |
| 809 | case CXCursor_EnumConstantRef: { |
| 810 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 811 | static_cast<Stmt *>(C.stmt)); |
| 812 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 813 | return createCXString(DRE->getDecl()->getIdentifier()->getNameStart()); |
| 814 | } |
| 815 | default: |
| 816 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | return clang_getDeclSpelling(C.decl); |
| 820 | } |
| 821 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 822 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 823 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 824 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 825 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 826 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 827 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 828 | case CXCursor_StructDecl: return "StructDecl"; |
| 829 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 830 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 831 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 832 | case CXCursor_VarDecl: return "VarDecl"; |
| 833 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 834 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 835 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 836 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 837 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 838 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
| 839 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 840 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 841 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 842 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 843 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 844 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 845 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
| 846 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 847 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 848 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
| 849 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 850 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 851 | case CXCursor_VarRef: return "VarRef"; |
| 852 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 853 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 854 | case CXCursor_MemberRef: return "MemberRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 856 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 857 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 858 | case CXCursor_NotImplemented: return "NotImplemented"; |
| 859 | default: return "<not implemented>"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 860 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 861 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 862 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 863 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 864 | switch (D->getKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 865 | case Decl::Function: return CXCursor_FunctionDecl; |
| 866 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 867 | case Decl::Enum: return CXCursor_EnumDecl; |
| 868 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 869 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 870 | case Decl::Field: return CXCursor_FieldDecl; |
| 871 | case Decl::Var: return CXCursor_VarDecl; |
| 872 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 873 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 874 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 875 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 876 | case Decl::ObjCMethod: { |
| 877 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 878 | if (MD->isInstanceMethod()) |
| 879 | return CXCursor_ObjCInstanceMethodDecl; |
| 880 | return CXCursor_ObjCClassMethodDecl; |
| 881 | } |
| 882 | default: break; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 883 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 884 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 885 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 886 | // |
| 887 | // CXCursor Operations. |
| 888 | // |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 889 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 890 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 891 | unsigned line, unsigned column) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 892 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 893 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 894 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 895 | FileManager &FMgr = CXXUnit->getFileManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 896 | const FileEntry *File = FMgr.getFile(source_name, |
| 897 | source_name+strlen(source_name)); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 898 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 899 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 900 | return C; |
| 901 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 902 | SourceLocation SLoc = |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 903 | CXXUnit->getSourceManager().getLocation(File, line, column); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 904 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 905 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
| 906 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 907 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 908 | &LastLoc); |
| 909 | if (ALoc.isValid()) |
| 910 | CXXUnit->setLastASTLocation(ALoc); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 911 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 912 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 913 | if (ALoc.isNamedRef()) |
| 914 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 915 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 916 | if (Dcl) { |
| 917 | if (Stm) { |
| 918 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 919 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 920 | return C; |
| 921 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 922 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 923 | return C; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 924 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 925 | // Fall through...treat as a decl, not a ref. |
| 926 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 927 | if (ALoc.isNamedRef()) { |
| 928 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 929 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 930 | return C; |
| 931 | } |
| 932 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 933 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 934 | return C; |
| 935 | } |
| 936 | } |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 937 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 938 | return C; |
| 939 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 940 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 941 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 944 | CXCursor clang_getNullCursor(void) { |
| 945 | CXCursor C; |
| 946 | C.kind = CXCursor_InvalidFile; |
| 947 | C.decl = NULL; |
| 948 | C.stmt = NULL; |
| 949 | return C; |
| 950 | } |
| 951 | |
| 952 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
| 953 | return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt; |
| 954 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 956 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 957 | assert(AnonDecl && "Passed null CXDecl"); |
| 958 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 959 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 960 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 961 | return C; |
| 962 | } |
| 963 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 964 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 965 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 966 | } |
| 967 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 968 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 969 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 970 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 971 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 972 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 973 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 974 | } |
| 975 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 976 | unsigned clang_isDefinition(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 977 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 978 | } |
| 979 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 980 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 981 | return C.kind; |
| 982 | } |
| 983 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 984 | static Decl *getDeclFromExpr(Stmt *E) { |
| 985 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 986 | return RefExpr->getDecl(); |
| 987 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 988 | return ME->getMemberDecl(); |
| 989 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 990 | return RE->getDecl(); |
| 991 | |
| 992 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 993 | return getDeclFromExpr(CE->getCallee()); |
| 994 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 995 | return getDeclFromExpr(CE->getSubExpr()); |
| 996 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 997 | return OME->getMethodDecl(); |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1002 | CXDecl clang_getCursorDecl(CXCursor C) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 1003 | if (clang_isDeclaration(C.kind)) |
| 1004 | return C.decl; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1005 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 1006 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 1007 | if (C.stmt) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1008 | if (C.kind == CXCursor_ObjCClassRef || |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 1009 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 1010 | return static_cast<Stmt *>(C.stmt); |
| 1011 | else |
| 1012 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 1013 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 1014 | return C.decl; |
| 1015 | } |
| 1016 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1019 | unsigned clang_getCursorLine(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1020 | assert(C.decl && "CXCursor has null decl"); |
| 1021 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1022 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1023 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1024 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1025 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1026 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1027 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 1028 | const char *clang_getCString(CXString string) { |
| 1029 | return string.Spelling; |
| 1030 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1031 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 1032 | void clang_disposeString(CXString string) { |
Benjamin Kramer | 858e5de | 2009-11-09 18:24:53 +0000 | [diff] [blame] | 1033 | if (string.MustFreeString) |
| 1034 | free((void*)string.Spelling); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1037 | unsigned clang_getCursorColumn(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1038 | assert(C.decl && "CXCursor has null decl"); |
| 1039 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1040 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1041 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1042 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1043 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1044 | } |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1045 | |
| 1046 | const char *clang_getCursorSource(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1047 | assert(C.decl && "CXCursor has null decl"); |
| 1048 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1049 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1050 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1051 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 1053 | if (SLoc.isFileID()) { |
| 1054 | const char *bufferName = SourceMgr.getBufferName(SLoc); |
| 1055 | return bufferName[0] == '<' ? NULL : bufferName; |
| 1056 | } |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Retrieve the file in which the macro was instantiated, then provide that |
| 1059 | // buffer name. |
| 1060 | // FIXME: Do we want to give specific macro-instantiation information? |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1061 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 1062 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 1063 | if (!Buffer) |
| 1064 | return 0; |
| 1065 | |
| 1066 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1069 | CXFile clang_getCursorSourceFile(CXCursor C) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1070 | assert(C.decl && "CXCursor has null decl"); |
| 1071 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 1072 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1073 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1074 | return (void *)getFileEntryFromSourceLocation(SourceMgr, |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1075 | getLocationFromCursor(C,SourceMgr, ND)); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1078 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1079 | const char **startBuf, |
| 1080 | const char **endBuf, |
| 1081 | unsigned *startLine, |
| 1082 | unsigned *startColumn, |
| 1083 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1084 | unsigned *endColumn) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1085 | assert(C.decl && "CXCursor has null decl"); |
| 1086 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 1087 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1088 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1089 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1090 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1091 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1092 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1093 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1094 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1095 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1096 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1097 | } |
| 1098 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1099 | enum CXCompletionChunkKind |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1100 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 1101 | unsigned chunk_number) { |
| 1102 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1103 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1104 | return CXCompletionChunk_Text; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1106 | switch ((*CCStr)[chunk_number].Kind) { |
| 1107 | case CodeCompletionString::CK_TypedText: |
| 1108 | return CXCompletionChunk_TypedText; |
| 1109 | case CodeCompletionString::CK_Text: |
| 1110 | return CXCompletionChunk_Text; |
| 1111 | case CodeCompletionString::CK_Optional: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1112 | return CXCompletionChunk_Optional; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1113 | case CodeCompletionString::CK_Placeholder: |
| 1114 | return CXCompletionChunk_Placeholder; |
| 1115 | case CodeCompletionString::CK_Informative: |
| 1116 | return CXCompletionChunk_Informative; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1117 | case CodeCompletionString::CK_ResultType: |
| 1118 | return CXCompletionChunk_ResultType; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1119 | case CodeCompletionString::CK_CurrentParameter: |
| 1120 | return CXCompletionChunk_CurrentParameter; |
| 1121 | case CodeCompletionString::CK_LeftParen: |
| 1122 | return CXCompletionChunk_LeftParen; |
| 1123 | case CodeCompletionString::CK_RightParen: |
| 1124 | return CXCompletionChunk_RightParen; |
| 1125 | case CodeCompletionString::CK_LeftBracket: |
| 1126 | return CXCompletionChunk_LeftBracket; |
| 1127 | case CodeCompletionString::CK_RightBracket: |
| 1128 | return CXCompletionChunk_RightBracket; |
| 1129 | case CodeCompletionString::CK_LeftBrace: |
| 1130 | return CXCompletionChunk_LeftBrace; |
| 1131 | case CodeCompletionString::CK_RightBrace: |
| 1132 | return CXCompletionChunk_RightBrace; |
| 1133 | case CodeCompletionString::CK_LeftAngle: |
| 1134 | return CXCompletionChunk_LeftAngle; |
| 1135 | case CodeCompletionString::CK_RightAngle: |
| 1136 | return CXCompletionChunk_RightAngle; |
| 1137 | case CodeCompletionString::CK_Comma: |
| 1138 | return CXCompletionChunk_Comma; |
| 1139 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1141 | // Should be unreachable, but let's be careful. |
| 1142 | return CXCompletionChunk_Text; |
| 1143 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1145 | const char *clang_getCompletionChunkText(CXCompletionString completion_string, |
| 1146 | unsigned chunk_number) { |
| 1147 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1148 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1149 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1151 | switch ((*CCStr)[chunk_number].Kind) { |
| 1152 | case CodeCompletionString::CK_TypedText: |
| 1153 | case CodeCompletionString::CK_Text: |
| 1154 | case CodeCompletionString::CK_Placeholder: |
| 1155 | case CodeCompletionString::CK_CurrentParameter: |
| 1156 | case CodeCompletionString::CK_Informative: |
| 1157 | case CodeCompletionString::CK_LeftParen: |
| 1158 | case CodeCompletionString::CK_RightParen: |
| 1159 | case CodeCompletionString::CK_LeftBracket: |
| 1160 | case CodeCompletionString::CK_RightBracket: |
| 1161 | case CodeCompletionString::CK_LeftBrace: |
| 1162 | case CodeCompletionString::CK_RightBrace: |
| 1163 | case CodeCompletionString::CK_LeftAngle: |
| 1164 | case CodeCompletionString::CK_RightAngle: |
| 1165 | case CodeCompletionString::CK_Comma: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1166 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1167 | return (*CCStr)[chunk_number].Text; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1169 | case CodeCompletionString::CK_Optional: |
| 1170 | // Note: treated as an empty text block. |
Douglas Gregor | 90742c1 | 2009-12-10 22:46:19 +0000 | [diff] [blame] | 1171 | return ""; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1172 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1174 | // Should be unreachable, but let's be careful. |
| 1175 | return 0; |
| 1176 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1178 | CXCompletionString |
| 1179 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 1180 | unsigned chunk_number) { |
| 1181 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1182 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1183 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1185 | switch ((*CCStr)[chunk_number].Kind) { |
| 1186 | case CodeCompletionString::CK_TypedText: |
| 1187 | case CodeCompletionString::CK_Text: |
| 1188 | case CodeCompletionString::CK_Placeholder: |
| 1189 | case CodeCompletionString::CK_CurrentParameter: |
| 1190 | case CodeCompletionString::CK_Informative: |
| 1191 | case CodeCompletionString::CK_LeftParen: |
| 1192 | case CodeCompletionString::CK_RightParen: |
| 1193 | case CodeCompletionString::CK_LeftBracket: |
| 1194 | case CodeCompletionString::CK_RightBracket: |
| 1195 | case CodeCompletionString::CK_LeftBrace: |
| 1196 | case CodeCompletionString::CK_RightBrace: |
| 1197 | case CodeCompletionString::CK_LeftAngle: |
| 1198 | case CodeCompletionString::CK_RightAngle: |
| 1199 | case CodeCompletionString::CK_Comma: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1200 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1201 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1202 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1203 | case CodeCompletionString::CK_Optional: |
| 1204 | // Note: treated as an empty text block. |
| 1205 | return (*CCStr)[chunk_number].Optional; |
| 1206 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1207 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1208 | // Should be unreachable, but let's be careful. |
| 1209 | return 0; |
| 1210 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1212 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
| 1213 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1214 | return CCStr? CCStr->size() : 0; |
| 1215 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1217 | static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, |
| 1218 | unsigned &Value) { |
| 1219 | if (Memory + sizeof(unsigned) > MemoryEnd) |
| 1220 | return true; |
| 1221 | |
| 1222 | memmove(&Value, Memory, sizeof(unsigned)); |
| 1223 | Memory += sizeof(unsigned); |
| 1224 | return false; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1225 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1226 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1227 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 1228 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 1229 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
| 1230 | /// \brief The memory buffer from which we parsed the results. We |
| 1231 | /// retain this buffer because the completion strings point into it. |
| 1232 | llvm::MemoryBuffer *Buffer; |
| 1233 | }; |
| 1234 | |
| 1235 | CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, |
| 1236 | const char *source_filename, |
| 1237 | int num_command_line_args, |
| 1238 | const char **command_line_args, |
| 1239 | unsigned num_unsaved_files, |
| 1240 | struct CXUnsavedFile *unsaved_files, |
| 1241 | const char *complete_filename, |
| 1242 | unsigned complete_line, |
| 1243 | unsigned complete_column) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1244 | // The indexer, which is mainly used to determine where diagnostics go. |
| 1245 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1247 | // The set of temporary files that we've built. |
| 1248 | std::vector<llvm::sys::Path> TemporaryFiles; |
| 1249 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1250 | // Build up the arguments for invoking 'clang'. |
| 1251 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1253 | // First add the complete path to the 'clang' executable. |
| 1254 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 1255 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Add the '-fsyntax-only' argument so that we only perform a basic |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1258 | // syntax check of the code. |
| 1259 | argv.push_back("-fsyntax-only"); |
| 1260 | |
| 1261 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 1262 | // to perform code completion, with an "-Xclang" preceding it. |
| 1263 | std::string code_complete_at; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1264 | code_complete_at += complete_filename; |
| 1265 | code_complete_at += ":"; |
| 1266 | code_complete_at += llvm::utostr(complete_line); |
| 1267 | code_complete_at += ":"; |
| 1268 | code_complete_at += llvm::utostr(complete_column); |
| 1269 | argv.push_back("-Xclang"); |
Daniel Dunbar | 7bd7165 | 2009-12-03 05:32:40 +0000 | [diff] [blame] | 1270 | argv.push_back("-code-completion-at"); |
| 1271 | argv.push_back("-Xclang"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1272 | argv.push_back(code_complete_at.c_str()); |
| 1273 | argv.push_back("-Xclang"); |
Daniel Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 1274 | argv.push_back("-no-code-completion-debug-printer"); |
Douglas Gregor | 1733c35 | 2010-01-04 19:45:17 +0000 | [diff] [blame] | 1275 | argv.push_back("-Xclang"); |
| 1276 | argv.push_back("-code-completion-macros"); |
| 1277 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1278 | std::vector<std::string> RemapArgs; |
| 1279 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 1280 | char tmpFile[L_tmpnam]; |
| 1281 | char *tmpFileName = tmpnam(tmpFile); |
| 1282 | |
| 1283 | // Write the contents of this unsaved file into the temporary file. |
| 1284 | llvm::sys::Path SavedFile(tmpFileName); |
| 1285 | std::string ErrorInfo; |
| 1286 | llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo); |
| 1287 | if (!ErrorInfo.empty()) |
| 1288 | continue; |
| 1289 | |
| 1290 | OS.write(unsaved_files[i].Contents, unsaved_files[i].Length); |
| 1291 | OS.close(); |
| 1292 | if (OS.has_error()) { |
| 1293 | SavedFile.eraseFromDisk(); |
| 1294 | continue; |
| 1295 | } |
| 1296 | |
| 1297 | // Remap the file. |
Daniel Dunbar | 7bd7165 | 2009-12-03 05:32:40 +0000 | [diff] [blame] | 1298 | std::string RemapArg = unsaved_files[i].Filename; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1299 | RemapArg += ';'; |
| 1300 | RemapArg += tmpFileName; |
| 1301 | RemapArgs.push_back("-Xclang"); |
Daniel Dunbar | 7bd7165 | 2009-12-03 05:32:40 +0000 | [diff] [blame] | 1302 | RemapArgs.push_back("-remap-file"); |
| 1303 | RemapArgs.push_back("-Xclang"); |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1304 | RemapArgs.push_back(RemapArg); |
| 1305 | TemporaryFiles.push_back(SavedFile); |
| 1306 | } |
| 1307 | |
| 1308 | // The pointers into the elements of RemapArgs are stable because we |
| 1309 | // won't be adding anything to RemapArgs after this point. |
| 1310 | for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) |
| 1311 | argv.push_back(RemapArgs[i].c_str()); |
| 1312 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1313 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 1314 | // file from the buffer, or just feed the source text via standard input). |
Ted Kremenek | 4633d1b | 2009-11-17 18:18:02 +0000 | [diff] [blame] | 1315 | if (source_filename) |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1316 | argv.push_back(source_filename); |
| 1317 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1318 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 1319 | for (int i = 0; i < num_command_line_args; ++i) |
| 1320 | if (const char *arg = command_line_args[i]) { |
| 1321 | if (strcmp(arg, "-o") == 0) { |
| 1322 | ++i; // Also skip the matching argument. |
| 1323 | continue; |
| 1324 | } |
| 1325 | if (strcmp(arg, "-emit-ast") == 0 || |
| 1326 | strcmp(arg, "-c") == 0 || |
| 1327 | strcmp(arg, "-fsyntax-only") == 0) { |
| 1328 | continue; |
| 1329 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1331 | // Keep the argument. |
| 1332 | argv.push_back(arg); |
| 1333 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1335 | // Add the null terminator. |
| 1336 | argv.push_back(NULL); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1338 | // Generate a temporary name for the AST file. |
| 1339 | char tmpFile[L_tmpnam]; |
| 1340 | char *tmpFileName = tmpnam(tmpFile); |
| 1341 | llvm::sys::Path ResultsFile(tmpFileName); |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1342 | TemporaryFiles.push_back(ResultsFile); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1344 | // Invoke 'clang'. |
| 1345 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 1346 | // on Unix or NUL (Windows). |
| 1347 | std::string ErrMsg; |
| 1348 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, &DevNull, 0 }; |
| 1349 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 1350 | /* redirects */ &Redirects[0], |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1351 | /* secondsToWait */ 0, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1352 | /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1353 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 1354 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1355 | llvm::errs() << "clang_codeComplete: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1356 | << '\n' << "Arguments: \n"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1357 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 1358 | I!=E; ++I) { |
| 1359 | if (*I) |
| 1360 | llvm::errs() << ' ' << *I << '\n'; |
| 1361 | } |
| 1362 | llvm::errs() << '\n'; |
| 1363 | } |
| 1364 | |
| 1365 | // Parse the resulting source file to find code-completion results. |
| 1366 | using llvm::MemoryBuffer; |
| 1367 | using llvm::StringRef; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1368 | AllocatedCXCodeCompleteResults *Results = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1369 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1370 | llvm::SmallVector<CXCompletionResult, 4> CompletionResults; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1371 | StringRef Buffer = F->getBuffer(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1372 | for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); |
| 1373 | Str < StrEnd;) { |
| 1374 | unsigned KindValue; |
| 1375 | if (ReadUnsigned(Str, StrEnd, KindValue)) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1376 | break; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1378 | CodeCompletionString *CCStr |
| 1379 | = CodeCompletionString::Deserialize(Str, StrEnd); |
| 1380 | if (!CCStr) |
| 1381 | continue; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1383 | if (!CCStr->empty()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1384 | // Vend the code-completion result to the caller. |
| 1385 | CXCompletionResult Result; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1386 | Result.CursorKind = (CXCursorKind)KindValue; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1387 | Result.CompletionString = CCStr; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1388 | CompletionResults.push_back(Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1389 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1390 | }; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Allocate the results. |
| 1393 | Results = new AllocatedCXCodeCompleteResults; |
| 1394 | Results->Results = new CXCompletionResult [CompletionResults.size()]; |
| 1395 | Results->NumResults = CompletionResults.size(); |
| 1396 | memcpy(Results->Results, CompletionResults.data(), |
| 1397 | CompletionResults.size() * sizeof(CXCompletionResult)); |
| 1398 | Results->Buffer = F; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1401 | for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) |
| 1402 | TemporaryFiles[i].eraseFromDisk(); |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1403 | |
| 1404 | return Results; |
| 1405 | } |
| 1406 | |
| 1407 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 1408 | if (!ResultsIn) |
| 1409 | return; |
| 1410 | |
| 1411 | AllocatedCXCodeCompleteResults *Results |
| 1412 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 1413 | |
| 1414 | for (unsigned I = 0, N = Results->NumResults; I != N; ++I) |
| 1415 | delete (CXCompletionString *)Results->Results[I].CompletionString; |
| 1416 | delete [] Results->Results; |
| 1417 | |
| 1418 | Results->Results = 0; |
| 1419 | Results->NumResults = 0; |
| 1420 | delete Results->Buffer; |
| 1421 | Results->Buffer = 0; |
| 1422 | delete Results; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1423 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1424 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1425 | } // end extern "C" |