configure.ac: check if TargetInfo::CreateTargetInfo takes TargetOptions pointer
[isl.git] / interface / extract_interface.cc
blobdb5934204b8fdb5b4040a9e8af7e759b124eb033
1 /*
2 * Copyright 2011 Sven Verdoolaege. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Sven Verdoolaege.
32 */
34 #include "isl_config.h"
36 #include <assert.h>
37 #include <iostream>
38 #include <llvm/Support/raw_ostream.h>
39 #include <llvm/Support/CommandLine.h>
40 #include <llvm/Support/Host.h>
41 #include <llvm/Support/ManagedStatic.h>
42 #include <clang/AST/ASTContext.h>
43 #include <clang/AST/ASTConsumer.h>
44 #include <clang/Basic/FileSystemOptions.h>
45 #include <clang/Basic/FileManager.h>
46 #include <clang/Basic/TargetOptions.h>
47 #include <clang/Basic/TargetInfo.h>
48 #include <clang/Basic/Version.h>
49 #include <clang/Driver/Compilation.h>
50 #include <clang/Driver/Driver.h>
51 #include <clang/Driver/Tool.h>
52 #include <clang/Frontend/CompilerInstance.h>
53 #include <clang/Frontend/CompilerInvocation.h>
54 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
55 #include <clang/Basic/DiagnosticOptions.h>
56 #else
57 #include <clang/Frontend/DiagnosticOptions.h>
58 #endif
59 #include <clang/Frontend/TextDiagnosticPrinter.h>
60 #include <clang/Frontend/Utils.h>
61 #include <clang/Lex/HeaderSearch.h>
62 #include <clang/Lex/Preprocessor.h>
63 #include <clang/Parse/ParseAST.h>
64 #include <clang/Sema/Sema.h>
66 #include "extract_interface.h"
67 #include "python.h"
69 using namespace std;
70 using namespace clang;
71 using namespace clang::driver;
73 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
74 llvm::cl::Required, llvm::cl::desc("<input file>"));
75 static llvm::cl::list<string> Includes("I",
76 llvm::cl::desc("Header search path"),
77 llvm::cl::value_desc("path"), llvm::cl::Prefix);
79 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
81 /* Does decl have an attribute of the following form?
83 * __attribute__((annotate("name")))
85 bool has_annotation(Decl *decl, const char *name)
87 if (!decl->hasAttrs())
88 return false;
90 AttrVec attrs = decl->getAttrs();
91 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
92 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
93 if (!ann)
94 continue;
95 if (ann->getAnnotation().str() == name)
96 return true;
99 return false;
102 /* Is decl marked as exported?
104 static bool is_exported(Decl *decl)
106 return has_annotation(decl, "isl_export");
109 /* Collect all types and functions that are annotated "isl_export"
110 * in "types" and "function".
112 * We currently only consider single declarations.
114 struct MyASTConsumer : public ASTConsumer {
115 set<RecordDecl *> types;
116 set<FunctionDecl *> functions;
118 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) {
119 Decl *decl;
121 if (!D.isSingleDecl())
122 return HandleTopLevelDeclContinue;
123 decl = D.getSingleDecl();
124 if (!is_exported(decl))
125 return HandleTopLevelDeclContinue;
126 switch (decl->getKind()) {
127 case Decl::Record:
128 types.insert(cast<RecordDecl>(decl));
129 break;
130 case Decl::Function:
131 functions.insert(cast<FunctionDecl>(decl));
132 break;
133 default:
134 break;
136 return HandleTopLevelDeclContinue;
140 #ifdef USE_ARRAYREF
142 #ifdef HAVE_CXXISPRODUCTION
143 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
145 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
146 "", false, false, Diags);
148 #else
149 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
151 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
152 "", false, Diags);
154 #endif
156 /* Create a CompilerInvocation object that stores the command line
157 * arguments constructed by the driver.
158 * The arguments are mainly useful for setting up the system include
159 * paths on newer clangs and on some platforms.
161 static CompilerInvocation *construct_invocation(const char *filename,
162 DiagnosticsEngine &Diags)
164 const char *binary = CLANG_PREFIX"/bin/clang";
165 const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags));
166 std::vector<const char *> Argv;
167 Argv.push_back(binary);
168 Argv.push_back(filename);
169 const llvm::OwningPtr<Compilation> compilation(
170 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
171 JobList &Jobs = compilation->getJobs();
173 Command *cmd = cast<Command>(*Jobs.begin());
174 if (strcmp(cmd->getCreator().getName(), "clang"))
175 return NULL;
177 const ArgStringList *args = &cmd->getArguments();
179 CompilerInvocation *invocation = new CompilerInvocation;
180 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
181 args->data() + args->size(),
182 Diags);
183 return invocation;
186 #else
188 static CompilerInvocation *construct_invocation(const char *filename,
189 DiagnosticsEngine &Diags)
191 return NULL;
194 #endif
196 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
198 static TextDiagnosticPrinter *construct_printer(void)
200 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
203 #else
205 static TextDiagnosticPrinter *construct_printer(void)
207 DiagnosticOptions DO;
208 return new TextDiagnosticPrinter(llvm::errs(), DO);
211 #endif
213 #ifdef CREATETARGETINFO_TAKES_POINTER
215 static TargetInfo *create_target_info(CompilerInstance *Clang,
216 DiagnosticsEngine &Diags)
218 TargetOptions &TO = Clang->getTargetOpts();
219 TO.Triple = llvm::sys::getDefaultTargetTriple();
220 return TargetInfo::CreateTargetInfo(Diags, &TO);
223 #else
225 static TargetInfo *create_target_info(CompilerInstance *Clang,
226 DiagnosticsEngine &Diags)
228 TargetOptions &TO = Clang->getTargetOpts();
229 TO.Triple = llvm::sys::getDefaultTargetTriple();
230 return TargetInfo::CreateTargetInfo(Diags, TO);
233 #endif
235 int main(int argc, char *argv[])
237 llvm::cl::ParseCommandLineOptions(argc, argv);
239 CompilerInstance *Clang = new CompilerInstance();
240 Clang->createDiagnostics(0, NULL, construct_printer());
241 DiagnosticsEngine &Diags = Clang->getDiagnostics();
242 Diags.setSuppressSystemWarnings(true);
243 CompilerInvocation *invocation =
244 construct_invocation(InputFilename.c_str(), Diags);
245 if (invocation)
246 Clang->setInvocation(invocation);
247 Clang->createFileManager();
248 Clang->createSourceManager(Clang->getFileManager());
249 TargetInfo *target = create_target_info(Clang, Diags);
250 Clang->setTarget(target);
251 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
252 LangStandard::lang_unspecified);
253 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
254 LangOptions &LO = Clang->getLangOpts();
255 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
256 HSO.ResourceDir = ResourceDir;
258 for (int i = 0; i < Includes.size(); ++i)
259 HSO.AddPath(Includes[i], frontend::Angled, true, false, false);
261 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
262 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
263 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
264 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
265 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
266 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
268 Clang->createPreprocessor();
269 Preprocessor &PP = Clang->getPreprocessor();
271 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO);
273 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
274 assert(file);
275 Clang->getSourceManager().createMainFileID(file);
277 Clang->createASTContext();
278 MyASTConsumer consumer;
279 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
281 Diags.getClient()->BeginSourceFile(LO, &PP);
282 ParseAST(*sema);
283 Diags.getClient()->EndSourceFile();
285 generate_python(consumer.types, consumer.functions);
287 delete sema;
288 delete Clang;
289 llvm::llvm_shutdown();
291 return 0;