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"
39 #ifdef HAVE_ADT_OWNINGPTR_H
40 #include <llvm/ADT/OwningPtr.h>
44 #include <llvm/Support/raw_ostream.h>
45 #include <llvm/Support/CommandLine.h>
46 #include <llvm/Support/Host.h>
47 #include <llvm/Support/ManagedStatic.h>
48 #include <clang/AST/ASTContext.h>
49 #include <clang/AST/ASTConsumer.h>
50 #include <clang/Basic/FileSystemOptions.h>
51 #include <clang/Basic/FileManager.h>
52 #include <clang/Basic/TargetOptions.h>
53 #include <clang/Basic/TargetInfo.h>
54 #include <clang/Basic/Version.h>
55 #include <clang/Driver/Compilation.h>
56 #include <clang/Driver/Driver.h>
57 #include <clang/Driver/Tool.h>
58 #include <clang/Frontend/CompilerInstance.h>
59 #include <clang/Frontend/CompilerInvocation.h>
60 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
61 #include <clang/Basic/DiagnosticOptions.h>
63 #include <clang/Frontend/DiagnosticOptions.h>
65 #include <clang/Frontend/TextDiagnosticPrinter.h>
66 #include <clang/Frontend/Utils.h>
67 #include <clang/Lex/HeaderSearch.h>
68 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
69 #include <clang/Lex/PreprocessorOptions.h>
71 #include <clang/Frontend/PreprocessorOptions.h>
73 #include <clang/Lex/Preprocessor.h>
74 #include <clang/Parse/ParseAST.h>
75 #include <clang/Sema/Sema.h>
77 #include "extract_interface.h"
78 #include "generator.h"
81 #include "cpp_conversion.h"
84 using namespace clang
;
85 using namespace clang::driver
;
87 #ifdef HAVE_ADT_OWNINGPTR_H
88 #define unique_ptr llvm::OwningPtr
91 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
92 llvm::cl::Required
, llvm::cl::desc("<input file>"));
93 static llvm::cl::list
<string
> Includes("I",
94 llvm::cl::desc("Header search path"),
95 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
97 static llvm::cl::opt
<string
> Language(llvm::cl::Required
,
98 llvm::cl::ValueRequired
, "language",
99 llvm::cl::desc("Bindings to generate"),
100 llvm::cl::value_desc("name"));
102 static const char *ResourceDir
=
103 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
105 /* Does decl have an attribute of the following form?
107 * __attribute__((annotate("name")))
109 bool has_annotation(Decl
*decl
, const char *name
)
111 if (!decl
->hasAttrs())
114 AttrVec attrs
= decl
->getAttrs();
115 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
116 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
119 if (ann
->getAnnotation().str() == name
)
126 /* Is decl marked as exported?
128 static bool is_exported(Decl
*decl
)
130 return has_annotation(decl
, "isl_export");
133 /* Collect all types and functions that are annotated "isl_export"
134 * in "exported_types" and "exported_function". Collect all function
135 * declarations in "functions".
137 * We currently only consider single declarations.
139 struct MyASTConsumer
: public ASTConsumer
{
140 set
<RecordDecl
*> exported_types
;
141 set
<FunctionDecl
*> exported_functions
;
142 set
<FunctionDecl
*> functions
;
144 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
147 if (!D
.isSingleDecl())
148 return HandleTopLevelDeclContinue
;
149 decl
= D
.getSingleDecl();
150 if (isa
<FunctionDecl
>(decl
))
151 functions
.insert(cast
<FunctionDecl
>(decl
));
152 if (!is_exported(decl
))
153 return HandleTopLevelDeclContinue
;
154 switch (decl
->getKind()) {
156 exported_types
.insert(cast
<RecordDecl
>(decl
));
159 exported_functions
.insert(cast
<FunctionDecl
>(decl
));
164 return HandleTopLevelDeclContinue
;
170 #ifdef HAVE_CXXISPRODUCTION
171 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
173 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
174 "", false, false, Diags
);
176 #elif defined(HAVE_ISPRODUCTION)
177 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
179 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
182 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
183 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
185 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
189 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
191 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
195 namespace clang
{ namespace driver
{ class Job
; } }
197 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
198 * We fix this with a simple overloaded function here.
201 static Job
*command(Job
*J
) { return J
; }
202 static Job
*command(Job
&J
) { return &J
; }
203 static Command
*command(Command
&C
) { return &C
; }
206 /* Create a CompilerInvocation object that stores the command line
207 * arguments constructed by the driver.
208 * The arguments are mainly useful for setting up the system include
209 * paths on newer clangs and on some platforms.
211 static CompilerInvocation
*construct_invocation(const char *filename
,
212 DiagnosticsEngine
&Diags
)
214 const char *binary
= CLANG_PREFIX
"/bin/clang";
215 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
216 std::vector
<const char *> Argv
;
217 Argv
.push_back(binary
);
218 Argv
.push_back(filename
);
219 const unique_ptr
<Compilation
> compilation(
220 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
221 JobList
&Jobs
= compilation
->getJobs();
223 Command
*cmd
= cast
<Command
>(ClangAPI::command(*Jobs
.begin()));
224 if (strcmp(cmd
->getCreator().getName(), "clang"))
227 const ArgStringList
*args
= &cmd
->getArguments();
229 CompilerInvocation
*invocation
= new CompilerInvocation
;
230 CompilerInvocation::CreateFromArgs(*invocation
, args
->data() + 1,
231 args
->data() + args
->size(),
238 static CompilerInvocation
*construct_invocation(const char *filename
,
239 DiagnosticsEngine
&Diags
)
246 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
248 static TextDiagnosticPrinter
*construct_printer(void)
250 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
255 static TextDiagnosticPrinter
*construct_printer(void)
257 DiagnosticOptions DO
;
258 return new TextDiagnosticPrinter(llvm::errs(), DO
);
263 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
265 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
266 DiagnosticsEngine
&Diags
)
268 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
269 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
270 return TargetInfo::CreateTargetInfo(Diags
, TO
);
273 #elif defined(CREATETARGETINFO_TAKES_POINTER)
275 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
276 DiagnosticsEngine
&Diags
)
278 TargetOptions
&TO
= Clang
->getTargetOpts();
279 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
280 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
285 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
286 DiagnosticsEngine
&Diags
)
288 TargetOptions
&TO
= Clang
->getTargetOpts();
289 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
290 return TargetInfo::CreateTargetInfo(Diags
, TO
);
295 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
297 static void create_diagnostics(CompilerInstance
*Clang
)
299 Clang
->createDiagnostics(0, NULL
, construct_printer());
304 static void create_diagnostics(CompilerInstance
*Clang
)
306 Clang
->createDiagnostics(construct_printer());
311 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
313 static void create_preprocessor(CompilerInstance
*Clang
)
315 Clang
->createPreprocessor(TU_Complete
);
320 static void create_preprocessor(CompilerInstance
*Clang
)
322 Clang
->createPreprocessor();
327 #ifdef ADDPATH_TAKES_4_ARGUMENTS
329 void add_path(HeaderSearchOptions
&HSO
, string Path
)
331 HSO
.AddPath(Path
, frontend::Angled
, false, false);
336 void add_path(HeaderSearchOptions
&HSO
, string Path
)
338 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
343 #ifdef HAVE_SETMAINFILEID
345 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
347 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
353 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
355 SM
.createMainFileID(file
);
360 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
362 static void set_lang_defaults(CompilerInstance
*Clang
)
364 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
365 TargetOptions
&TO
= Clang
->getTargetOpts();
366 llvm::Triple
T(TO
.Triple
);
367 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
, T
, PO
,
368 LangStandard::lang_unspecified
);
373 static void set_lang_defaults(CompilerInstance
*Clang
)
375 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
376 LangStandard::lang_unspecified
);
381 #ifdef SETINVOCATION_TAKES_SHARED_PTR
383 static void set_invocation(CompilerInstance
*Clang
,
384 CompilerInvocation
*invocation
)
386 Clang
->setInvocation(std::make_shared
<CompilerInvocation
>(*invocation
));
391 static void set_invocation(CompilerInstance
*Clang
,
392 CompilerInvocation
*invocation
)
394 Clang
->setInvocation(invocation
);
399 /* Create an interface generator for the selected language and
400 * then use it to generate the interface.
402 static void generate(MyASTConsumer
&consumer
, SourceManager
&SM
)
406 if (Language
.compare("python") == 0) {
407 gen
= new python_generator(SM
, consumer
.exported_types
,
408 consumer
.exported_functions
, consumer
.functions
);
409 } else if (Language
.compare("cpp") == 0) {
410 gen
= new cpp_generator(SM
, consumer
.exported_types
,
411 consumer
.exported_functions
, consumer
.functions
);
412 } else if (Language
.compare("cpp-checked") == 0) {
413 gen
= new cpp_generator(SM
, consumer
.exported_types
,
414 consumer
.exported_functions
, consumer
.functions
, true);
415 } else if (Language
.compare("cpp-checked-conversion") == 0) {
416 gen
= new cpp_conversion_generator(SM
, consumer
.exported_types
,
417 consumer
.exported_functions
, consumer
.functions
);
419 cerr
<< "Language '" << Language
<< "' not recognized." << endl
420 << "Not generating bindings." << endl
;
427 int main(int argc
, char *argv
[])
429 llvm::cl::ParseCommandLineOptions(argc
, argv
);
431 CompilerInstance
*Clang
= new CompilerInstance();
432 create_diagnostics(Clang
);
433 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
434 Diags
.setSuppressSystemWarnings(true);
435 CompilerInvocation
*invocation
=
436 construct_invocation(InputFilename
.c_str(), Diags
);
438 set_invocation(Clang
, invocation
);
439 Clang
->createFileManager();
440 Clang
->createSourceManager(Clang
->getFileManager());
441 TargetInfo
*target
= create_target_info(Clang
, Diags
);
442 Clang
->setTarget(target
);
443 set_lang_defaults(Clang
);
444 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
445 LangOptions
&LO
= Clang
->getLangOpts();
446 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
447 HSO
.ResourceDir
= ResourceDir
;
449 for (llvm::cl::list
<string
>::size_type i
= 0; i
< Includes
.size(); ++i
)
450 add_path(HSO
, Includes
[i
]);
452 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
453 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
454 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
455 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
456 PO
.addMacroDef("__isl_overload="
457 "__attribute__((annotate(\"isl_overload\"))) "
458 "__attribute__((annotate(\"isl_export\")))");
459 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
460 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
462 create_preprocessor(Clang
);
463 Preprocessor
&PP
= Clang
->getPreprocessor();
465 PP
.getBuiltinInfo().initializeBuiltins(PP
.getIdentifierTable(), LO
);
467 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
469 create_main_file_id(Clang
->getSourceManager(), file
);
471 Clang
->createASTContext();
472 MyASTConsumer consumer
;
473 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
475 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
477 Diags
.getClient()->EndSourceFile();
479 generate(consumer
, Clang
->getSourceManager());
483 llvm::llvm_shutdown();