isl_access_info: change interface for specifying restrictions
[isl.git] / interface / extract_interface.cc
blob1e631dcbf5283fb58c5223f59967d19ed003c4e1
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 <assert.h>
35 #include <iostream>
36 #include <llvm/Support/raw_ostream.h>
37 #include <llvm/Support/CommandLine.h>
38 #include <llvm/Support/Host.h>
39 #include <llvm/Support/ManagedStatic.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTConsumer.h>
42 #include <clang/Basic/FileSystemOptions.h>
43 #include <clang/Basic/FileManager.h>
44 #include <clang/Basic/TargetOptions.h>
45 #include <clang/Basic/TargetInfo.h>
46 #include <clang/Basic/Version.h>
47 #include <clang/Driver/Compilation.h>
48 #include <clang/Driver/Driver.h>
49 #include <clang/Driver/Tool.h>
50 #include <clang/Frontend/CompilerInstance.h>
51 #include <clang/Frontend/CompilerInvocation.h>
52 #include <clang/Frontend/DiagnosticOptions.h>
53 #include <clang/Frontend/TextDiagnosticPrinter.h>
54 #include <clang/Frontend/Utils.h>
55 #include <clang/Lex/HeaderSearch.h>
56 #include <clang/Lex/Preprocessor.h>
57 #include <clang/Parse/ParseAST.h>
58 #include <clang/Sema/Sema.h>
60 #include "isl_config.h"
61 #include "extract_interface.h"
62 #include "python.h"
64 using namespace std;
65 using namespace clang;
66 using namespace clang::driver;
68 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
69 llvm::cl::Required, llvm::cl::desc("<input file>"));
70 static llvm::cl::list<string> Includes("I",
71 llvm::cl::desc("Header search path"),
72 llvm::cl::value_desc("path"), llvm::cl::Prefix);
74 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
76 /* Does decl have an attribute of the following form?
78 * __attribute__((annotate("name")))
80 bool has_annotation(Decl *decl, const char *name)
82 if (!decl->hasAttrs())
83 return false;
85 AttrVec attrs = decl->getAttrs();
86 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
87 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
88 if (!ann)
89 continue;
90 if (ann->getAnnotation().str() == name)
91 return true;
94 return false;
97 /* Is decl marked as exported?
99 static bool is_exported(Decl *decl)
101 return has_annotation(decl, "isl_export");
104 /* Collect all types and functions that are annotated "isl_export"
105 * in "types" and "function".
107 * We currently only consider single declarations.
109 struct MyASTConsumer : public ASTConsumer {
110 set<RecordDecl *> types;
111 set<FunctionDecl *> functions;
113 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) {
114 Decl *decl;
116 if (!D.isSingleDecl())
117 return HandleTopLevelDeclContinue;
118 decl = D.getSingleDecl();
119 if (!is_exported(decl))
120 return HandleTopLevelDeclContinue;
121 switch (decl->getKind()) {
122 case Decl::Record:
123 types.insert(cast<RecordDecl>(decl));
124 break;
125 case Decl::Function:
126 functions.insert(cast<FunctionDecl>(decl));
127 break;
128 default:
129 break;
131 return HandleTopLevelDeclContinue;
135 #ifdef USE_ARRAYREF
137 #ifdef HAVE_CXXISPRODUCTION
138 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
140 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
141 "", false, false, Diags);
143 #else
144 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
146 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
147 "", false, Diags);
149 #endif
151 /* Create a CompilerInvocation object that stores the command line
152 * arguments constructed by the driver.
153 * The arguments are mainly useful for setting up the system include
154 * paths on newer clangs and on some platforms.
156 static CompilerInvocation *construct_invocation(const char *filename,
157 DiagnosticsEngine &Diags)
159 const char *binary = CLANG_PREFIX"/bin/clang";
160 const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags));
161 std::vector<const char *> Argv;
162 Argv.push_back(binary);
163 Argv.push_back(filename);
164 const llvm::OwningPtr<Compilation> compilation(
165 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
166 JobList &Jobs = compilation->getJobs();
168 Command *cmd = cast<Command>(*Jobs.begin());
169 if (strcmp(cmd->getCreator().getName(), "clang"))
170 return NULL;
172 const ArgStringList *args = &cmd->getArguments();
174 CompilerInvocation *invocation = new CompilerInvocation;
175 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
176 args->data() + args->size(),
177 Diags);
178 return invocation;
181 #else
183 static CompilerInvocation *construct_invocation(const char *filename,
184 DiagnosticsEngine &Diags)
186 return NULL;
189 #endif
191 int main(int argc, char *argv[])
193 llvm::cl::ParseCommandLineOptions(argc, argv);
195 CompilerInstance *Clang = new CompilerInstance();
196 DiagnosticOptions DO;
197 Clang->createDiagnostics(0, NULL,
198 new TextDiagnosticPrinter(llvm::errs(), DO));
199 DiagnosticsEngine &Diags = Clang->getDiagnostics();
200 Diags.setSuppressSystemWarnings(true);
201 CompilerInvocation *invocation =
202 construct_invocation(InputFilename.c_str(), Diags);
203 if (invocation)
204 Clang->setInvocation(invocation);
205 Clang->createFileManager();
206 Clang->createSourceManager(Clang->getFileManager());
207 TargetOptions TO;
208 TO.Triple = llvm::sys::getDefaultTargetTriple();
209 TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO);
210 Clang->setTarget(target);
211 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
212 LangStandard::lang_unspecified);
213 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
214 LangOptions &LO = Clang->getLangOpts();
215 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
216 HSO.ResourceDir = ResourceDir;
218 for (int i = 0; i < Includes.size(); ++i)
219 HSO.AddPath(Includes[i], frontend::Angled, true, false, false);
221 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
222 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
223 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
224 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
225 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
226 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
228 Clang->createPreprocessor();
229 Preprocessor &PP = Clang->getPreprocessor();
231 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO);
233 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
234 assert(file);
235 Clang->getSourceManager().createMainFileID(file);
237 Clang->createASTContext();
238 MyASTConsumer consumer;
239 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
241 Diags.getClient()->BeginSourceFile(LO, &PP);
242 ParseAST(*sema);
243 Diags.getClient()->EndSourceFile();
245 generate_python(consumer.types, consumer.functions);
247 delete sema;
248 delete Clang;
249 llvm::llvm_shutdown();
251 return 0;