interface/extract_interface.cc: add space between literal and identifier
[isl.git] / interface / extract_interface.cc
bloba0cf5479d6ec64fa3f354807b2b3ff2ffe39d5dd
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 =
80 CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
82 /* Does decl have an attribute of the following form?
84 * __attribute__((annotate("name")))
86 bool has_annotation(Decl *decl, const char *name)
88 if (!decl->hasAttrs())
89 return false;
91 AttrVec attrs = decl->getAttrs();
92 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
93 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
94 if (!ann)
95 continue;
96 if (ann->getAnnotation().str() == name)
97 return true;
100 return false;
103 /* Is decl marked as exported?
105 static bool is_exported(Decl *decl)
107 return has_annotation(decl, "isl_export");
110 /* Collect all types and functions that are annotated "isl_export"
111 * in "types" and "function".
113 * We currently only consider single declarations.
115 struct MyASTConsumer : public ASTConsumer {
116 set<RecordDecl *> types;
117 set<FunctionDecl *> functions;
119 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) {
120 Decl *decl;
122 if (!D.isSingleDecl())
123 return HandleTopLevelDeclContinue;
124 decl = D.getSingleDecl();
125 if (!is_exported(decl))
126 return HandleTopLevelDeclContinue;
127 switch (decl->getKind()) {
128 case Decl::Record:
129 types.insert(cast<RecordDecl>(decl));
130 break;
131 case Decl::Function:
132 functions.insert(cast<FunctionDecl>(decl));
133 break;
134 default:
135 break;
137 return HandleTopLevelDeclContinue;
141 #ifdef USE_ARRAYREF
143 #ifdef HAVE_CXXISPRODUCTION
144 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
146 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
147 "", false, false, Diags);
149 #elif defined(HAVE_ISPRODUCTION)
150 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
152 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
153 "", false, Diags);
155 #else
156 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
158 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
159 "", Diags);
161 #endif
163 /* Create a CompilerInvocation object that stores the command line
164 * arguments constructed by the driver.
165 * The arguments are mainly useful for setting up the system include
166 * paths on newer clangs and on some platforms.
168 static CompilerInvocation *construct_invocation(const char *filename,
169 DiagnosticsEngine &Diags)
171 const char *binary = CLANG_PREFIX"/bin/clang";
172 const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags));
173 std::vector<const char *> Argv;
174 Argv.push_back(binary);
175 Argv.push_back(filename);
176 const llvm::OwningPtr<Compilation> compilation(
177 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
178 JobList &Jobs = compilation->getJobs();
180 Command *cmd = cast<Command>(*Jobs.begin());
181 if (strcmp(cmd->getCreator().getName(), "clang"))
182 return NULL;
184 const ArgStringList *args = &cmd->getArguments();
186 CompilerInvocation *invocation = new CompilerInvocation;
187 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
188 args->data() + args->size(),
189 Diags);
190 return invocation;
193 #else
195 static CompilerInvocation *construct_invocation(const char *filename,
196 DiagnosticsEngine &Diags)
198 return NULL;
201 #endif
203 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
205 static TextDiagnosticPrinter *construct_printer(void)
207 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
210 #else
212 static TextDiagnosticPrinter *construct_printer(void)
214 DiagnosticOptions DO;
215 return new TextDiagnosticPrinter(llvm::errs(), DO);
218 #endif
220 #ifdef CREATETARGETINFO_TAKES_POINTER
222 static TargetInfo *create_target_info(CompilerInstance *Clang,
223 DiagnosticsEngine &Diags)
225 TargetOptions &TO = Clang->getTargetOpts();
226 TO.Triple = llvm::sys::getDefaultTargetTriple();
227 return TargetInfo::CreateTargetInfo(Diags, &TO);
230 #else
232 static TargetInfo *create_target_info(CompilerInstance *Clang,
233 DiagnosticsEngine &Diags)
235 TargetOptions &TO = Clang->getTargetOpts();
236 TO.Triple = llvm::sys::getDefaultTargetTriple();
237 return TargetInfo::CreateTargetInfo(Diags, TO);
240 #endif
242 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
244 static void create_diagnostics(CompilerInstance *Clang)
246 Clang->createDiagnostics(0, NULL, construct_printer());
249 #else
251 static void create_diagnostics(CompilerInstance *Clang)
253 Clang->createDiagnostics(construct_printer());
256 #endif
258 #ifdef ADDPATH_TAKES_4_ARGUMENTS
260 void add_path(HeaderSearchOptions &HSO, string Path)
262 HSO.AddPath(Path, frontend::Angled, false, false);
265 #else
267 void add_path(HeaderSearchOptions &HSO, string Path)
269 HSO.AddPath(Path, frontend::Angled, true, false, false);
272 #endif
274 int main(int argc, char *argv[])
276 llvm::cl::ParseCommandLineOptions(argc, argv);
278 CompilerInstance *Clang = new CompilerInstance();
279 create_diagnostics(Clang);
280 DiagnosticsEngine &Diags = Clang->getDiagnostics();
281 Diags.setSuppressSystemWarnings(true);
282 CompilerInvocation *invocation =
283 construct_invocation(InputFilename.c_str(), Diags);
284 if (invocation)
285 Clang->setInvocation(invocation);
286 Clang->createFileManager();
287 Clang->createSourceManager(Clang->getFileManager());
288 TargetInfo *target = create_target_info(Clang, Diags);
289 Clang->setTarget(target);
290 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
291 LangStandard::lang_unspecified);
292 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
293 LangOptions &LO = Clang->getLangOpts();
294 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
295 HSO.ResourceDir = ResourceDir;
297 for (int i = 0; i < Includes.size(); ++i)
298 add_path(HSO, Includes[i]);
300 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
301 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
302 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
303 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
304 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
305 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
307 Clang->createPreprocessor();
308 Preprocessor &PP = Clang->getPreprocessor();
310 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO);
312 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
313 assert(file);
314 Clang->getSourceManager().createMainFileID(file);
316 Clang->createASTContext();
317 MyASTConsumer consumer;
318 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
320 Diags.getClient()->BeginSourceFile(LO, &PP);
321 ParseAST(*sema);
322 Diags.getClient()->EndSourceFile();
324 generate_python(consumer.types, consumer.functions);
326 delete sema;
327 delete Clang;
328 llvm::llvm_shutdown();
330 return 0;