2 * Copyright 2011 Sven Verdoolaege. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
34 #include "isl_config.h"
38 #include <llvm/ADT/OwningPtr.h>
39 #include <llvm/Support/raw_ostream.h>
40 #include <llvm/Support/CommandLine.h>
41 #include <llvm/Support/Host.h>
42 #include <llvm/Support/ManagedStatic.h>
43 #include <clang/AST/ASTContext.h>
44 #include <clang/AST/ASTConsumer.h>
45 #include <clang/Basic/FileSystemOptions.h>
46 #include <clang/Basic/FileManager.h>
47 #include <clang/Basic/TargetOptions.h>
48 #include <clang/Basic/TargetInfo.h>
49 #include <clang/Basic/Version.h>
50 #include <clang/Driver/Compilation.h>
51 #include <clang/Driver/Driver.h>
52 #include <clang/Driver/Tool.h>
53 #include <clang/Frontend/CompilerInstance.h>
54 #include <clang/Frontend/CompilerInvocation.h>
55 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
56 #include <clang/Basic/DiagnosticOptions.h>
58 #include <clang/Frontend/DiagnosticOptions.h>
60 #include <clang/Frontend/TextDiagnosticPrinter.h>
61 #include <clang/Frontend/Utils.h>
62 #include <clang/Lex/HeaderSearch.h>
63 #include <clang/Lex/Preprocessor.h>
64 #include <clang/Parse/ParseAST.h>
65 #include <clang/Sema/Sema.h>
67 #include "extract_interface.h"
71 using namespace clang
;
72 using namespace clang::driver
;
74 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
75 llvm::cl::Required
, llvm::cl::desc("<input file>"));
76 static llvm::cl::list
<string
> Includes("I",
77 llvm::cl::desc("Header search path"),
78 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
80 static const char *ResourceDir
=
81 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
83 /* Does decl have an attribute of the following form?
85 * __attribute__((annotate("name")))
87 bool has_annotation(Decl
*decl
, const char *name
)
89 if (!decl
->hasAttrs())
92 AttrVec attrs
= decl
->getAttrs();
93 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
94 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
97 if (ann
->getAnnotation().str() == name
)
104 /* Is decl marked as exported?
106 static bool is_exported(Decl
*decl
)
108 return has_annotation(decl
, "isl_export");
111 /* Collect all types and functions that are annotated "isl_export"
112 * in "types" and "function".
114 * We currently only consider single declarations.
116 struct MyASTConsumer
: public ASTConsumer
{
117 set
<RecordDecl
*> types
;
118 set
<FunctionDecl
*> functions
;
120 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
123 if (!D
.isSingleDecl())
124 return HandleTopLevelDeclContinue
;
125 decl
= D
.getSingleDecl();
126 if (!is_exported(decl
))
127 return HandleTopLevelDeclContinue
;
128 switch (decl
->getKind()) {
130 types
.insert(cast
<RecordDecl
>(decl
));
133 functions
.insert(cast
<FunctionDecl
>(decl
));
138 return HandleTopLevelDeclContinue
;
144 #ifdef HAVE_CXXISPRODUCTION
145 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
147 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
148 "", false, false, Diags
);
150 #elif defined(HAVE_ISPRODUCTION)
151 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
153 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
157 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
159 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
164 /* Create a CompilerInvocation object that stores the command line
165 * arguments constructed by the driver.
166 * The arguments are mainly useful for setting up the system include
167 * paths on newer clangs and on some platforms.
169 static CompilerInvocation
*construct_invocation(const char *filename
,
170 DiagnosticsEngine
&Diags
)
172 const char *binary
= CLANG_PREFIX
"/bin/clang";
173 const llvm::OwningPtr
<Driver
> driver(construct_driver(binary
, Diags
));
174 std::vector
<const char *> Argv
;
175 Argv
.push_back(binary
);
176 Argv
.push_back(filename
);
177 const llvm::OwningPtr
<Compilation
> compilation(
178 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
179 JobList
&Jobs
= compilation
->getJobs();
181 Command
*cmd
= cast
<Command
>(*Jobs
.begin());
182 if (strcmp(cmd
->getCreator().getName(), "clang"))
185 const ArgStringList
*args
= &cmd
->getArguments();
187 CompilerInvocation
*invocation
= new CompilerInvocation
;
188 CompilerInvocation::CreateFromArgs(*invocation
, args
->data() + 1,
189 args
->data() + args
->size(),
196 static CompilerInvocation
*construct_invocation(const char *filename
,
197 DiagnosticsEngine
&Diags
)
204 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
206 static TextDiagnosticPrinter
*construct_printer(void)
208 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
213 static TextDiagnosticPrinter
*construct_printer(void)
215 DiagnosticOptions DO
;
216 return new TextDiagnosticPrinter(llvm::errs(), DO
);
221 #ifdef CREATETARGETINFO_TAKES_POINTER
223 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
224 DiagnosticsEngine
&Diags
)
226 TargetOptions
&TO
= Clang
->getTargetOpts();
227 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
228 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
233 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
234 DiagnosticsEngine
&Diags
)
236 TargetOptions
&TO
= Clang
->getTargetOpts();
237 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
238 return TargetInfo::CreateTargetInfo(Diags
, TO
);
243 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
245 static void create_diagnostics(CompilerInstance
*Clang
)
247 Clang
->createDiagnostics(0, NULL
, construct_printer());
252 static void create_diagnostics(CompilerInstance
*Clang
)
254 Clang
->createDiagnostics(construct_printer());
259 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
261 static void create_preprocessor(CompilerInstance
*Clang
)
263 Clang
->createPreprocessor(TU_Complete
);
268 static void create_preprocessor(CompilerInstance
*Clang
)
270 Clang
->createPreprocessor();
275 #ifdef ADDPATH_TAKES_4_ARGUMENTS
277 void add_path(HeaderSearchOptions
&HSO
, string Path
)
279 HSO
.AddPath(Path
, frontend::Angled
, false, false);
284 void add_path(HeaderSearchOptions
&HSO
, string Path
)
286 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
291 int main(int argc
, char *argv
[])
293 llvm::cl::ParseCommandLineOptions(argc
, argv
);
295 CompilerInstance
*Clang
= new CompilerInstance();
296 create_diagnostics(Clang
);
297 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
298 Diags
.setSuppressSystemWarnings(true);
299 CompilerInvocation
*invocation
=
300 construct_invocation(InputFilename
.c_str(), Diags
);
302 Clang
->setInvocation(invocation
);
303 Clang
->createFileManager();
304 Clang
->createSourceManager(Clang
->getFileManager());
305 TargetInfo
*target
= create_target_info(Clang
, Diags
);
306 Clang
->setTarget(target
);
307 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
308 LangStandard::lang_unspecified
);
309 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
310 LangOptions
&LO
= Clang
->getLangOpts();
311 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
312 HSO
.ResourceDir
= ResourceDir
;
314 for (int i
= 0; i
< Includes
.size(); ++i
)
315 add_path(HSO
, Includes
[i
]);
317 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
318 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
319 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
320 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
321 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
322 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
324 create_preprocessor(Clang
);
325 Preprocessor
&PP
= Clang
->getPreprocessor();
327 PP
.getBuiltinInfo().InitializeBuiltins(PP
.getIdentifierTable(), LO
);
329 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
331 Clang
->getSourceManager().createMainFileID(file
);
333 Clang
->createASTContext();
334 MyASTConsumer consumer
;
335 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
337 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
339 Diags
.getClient()->EndSourceFile();
341 generate_python(consumer
.types
, consumer
.functions
);
345 llvm::llvm_shutdown();