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"
40 #ifdef HAVE_ADT_OWNINGPTR_H
41 #include <llvm/ADT/OwningPtr.h>
45 #ifdef HAVE_LLVM_OPTION_ARG_H
46 #include <llvm/Option/Arg.h>
48 #include <llvm/Support/raw_ostream.h>
49 #include <llvm/Support/CommandLine.h>
50 #include <llvm/Support/Host.h>
51 #include <llvm/Support/ManagedStatic.h>
52 #include <clang/AST/ASTContext.h>
53 #include <clang/AST/ASTConsumer.h>
54 #include <clang/Basic/Builtins.h>
55 #include <clang/Basic/FileSystemOptions.h>
56 #include <clang/Basic/FileManager.h>
57 #include <clang/Basic/TargetOptions.h>
58 #include <clang/Basic/TargetInfo.h>
59 #include <clang/Basic/Version.h>
60 #include <clang/Driver/Compilation.h>
61 #include <clang/Driver/Driver.h>
62 #include <clang/Driver/Tool.h>
63 #include <clang/Frontend/CompilerInstance.h>
64 #include <clang/Frontend/CompilerInvocation.h>
65 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
66 #include <clang/Basic/DiagnosticOptions.h>
68 #include <clang/Frontend/DiagnosticOptions.h>
70 #include <clang/Frontend/TextDiagnosticPrinter.h>
71 #include <clang/Frontend/Utils.h>
72 #include <clang/Lex/HeaderSearch.h>
73 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
74 #include <clang/Lex/PreprocessorOptions.h>
76 #include <clang/Frontend/PreprocessorOptions.h>
78 #include <clang/Lex/Preprocessor.h>
79 #include <clang/Parse/ParseAST.h>
80 #include <clang/Sema/Sema.h>
82 #include "extract_interface.h"
83 #include "generator.h"
85 #include "plain_cpp.h"
86 #include "cpp_conversion.h"
87 #include "template_cpp.h"
90 using namespace clang
;
91 using namespace clang::driver
;
92 #ifdef HAVE_LLVM_OPTION_ARG_H
93 using namespace llvm::opt
;
96 #ifdef HAVE_ADT_OWNINGPTR_H
97 #define unique_ptr llvm::OwningPtr
100 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
101 llvm::cl::Required
, llvm::cl::desc("<input file>"));
102 static llvm::cl::list
<string
> Includes("I",
103 llvm::cl::desc("Header search path"),
104 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
106 static llvm::cl::opt
<string
> OutputLanguage(llvm::cl::Required
,
107 llvm::cl::ValueRequired
, "language",
108 llvm::cl::desc("Bindings to generate"),
109 llvm::cl::value_desc("name"));
111 static const char *ResourceDir
=
112 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
114 /* Does decl have an attribute of the following form?
116 * __attribute__((annotate("name")))
118 bool has_annotation(Decl
*decl
, const char *name
)
120 if (!decl
->hasAttrs())
123 AttrVec attrs
= decl
->getAttrs();
124 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
125 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
128 if (ann
->getAnnotation().str() == name
)
135 /* Is decl marked as exported?
137 static bool is_exported(Decl
*decl
)
139 return has_annotation(decl
, "isl_export");
142 /* Collect all types and functions that are annotated "isl_export"
143 * in "exported_types" and "exported_function". Collect all function
144 * declarations in "functions".
146 * We currently only consider single declarations.
148 struct MyASTConsumer
: public ASTConsumer
{
149 set
<RecordDecl
*> exported_types
;
150 set
<FunctionDecl
*> exported_functions
;
151 set
<FunctionDecl
*> functions
;
153 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
156 if (!D
.isSingleDecl())
157 return HandleTopLevelDeclContinue
;
158 decl
= D
.getSingleDecl();
159 if (isa
<FunctionDecl
>(decl
))
160 functions
.insert(cast
<FunctionDecl
>(decl
));
161 if (!is_exported(decl
))
162 return HandleTopLevelDeclContinue
;
163 switch (decl
->getKind()) {
165 exported_types
.insert(cast
<RecordDecl
>(decl
));
168 exported_functions
.insert(cast
<FunctionDecl
>(decl
));
173 return HandleTopLevelDeclContinue
;
179 #ifdef HAVE_CXXISPRODUCTION
180 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
182 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
183 "", false, false, Diags
);
185 #elif defined(HAVE_ISPRODUCTION)
186 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
188 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
191 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
192 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
194 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
198 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
200 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
204 namespace clang
{ namespace driver
{ class Job
; } }
206 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
207 * We fix this with a simple overloaded function here.
210 static Job
*command(Job
*J
) { return J
; }
211 static Job
*command(Job
&J
) { return &J
; }
212 static Command
*command(Command
&C
) { return &C
; }
215 #ifdef CREATE_FROM_ARGS_TAKES_ARRAYREF
217 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
218 * In this case, an ArrayRef<const char *>.
220 static void create_from_args(CompilerInvocation
&invocation
,
221 const ArgStringList
*args
, DiagnosticsEngine
&Diags
)
223 CompilerInvocation::CreateFromArgs(invocation
, *args
, Diags
);
228 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
229 * In this case, two "const char *" pointers.
231 static void create_from_args(CompilerInvocation
&invocation
,
232 const ArgStringList
*args
, DiagnosticsEngine
&Diags
)
234 CompilerInvocation::CreateFromArgs(invocation
, args
->data() + 1,
235 args
->data() + args
->size(),
242 /* Set sysroot if required.
244 * If CLANG_SYSROOT is defined, then set it to this value.
246 static void set_sysroot(ArgStringList
&args
)
248 args
.push_back("-isysroot");
249 args
.push_back(CLANG_SYSROOT
);
252 /* Set sysroot if required.
254 * If CLANG_SYSROOT is not defined, then it does not need to be set.
256 static void set_sysroot(ArgStringList
&args
)
261 /* Create a CompilerInvocation object that stores the command line
262 * arguments constructed by the driver.
263 * The arguments are mainly useful for setting up the system include
264 * paths on newer clangs and on some platforms.
266 static CompilerInvocation
*construct_invocation(const char *filename
,
267 DiagnosticsEngine
&Diags
)
269 const char *binary
= CLANG_PREFIX
"/bin/clang";
270 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
271 std::vector
<const char *> Argv
;
272 Argv
.push_back(binary
);
273 Argv
.push_back(filename
);
274 const unique_ptr
<Compilation
> compilation(
275 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
276 JobList
&Jobs
= compilation
->getJobs();
278 Command
*cmd
= cast
<Command
>(ClangAPI::command(*Jobs
.begin()));
279 if (strcmp(cmd
->getCreator().getName(), "clang"))
282 ArgStringList args
= cmd
->getArguments();
285 CompilerInvocation
*invocation
= new CompilerInvocation
;
286 create_from_args(*invocation
, &args
, Diags
);
292 static CompilerInvocation
*construct_invocation(const char *filename
,
293 DiagnosticsEngine
&Diags
)
300 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
302 static TextDiagnosticPrinter
*construct_printer(void)
304 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
309 static TextDiagnosticPrinter
*construct_printer(void)
311 DiagnosticOptions DO
;
312 return new TextDiagnosticPrinter(llvm::errs(), DO
);
317 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
319 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
320 DiagnosticsEngine
&Diags
)
322 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
323 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
324 return TargetInfo::CreateTargetInfo(Diags
, TO
);
327 #elif defined(CREATETARGETINFO_TAKES_POINTER)
329 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
330 DiagnosticsEngine
&Diags
)
332 TargetOptions
&TO
= Clang
->getTargetOpts();
333 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
334 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
339 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
340 DiagnosticsEngine
&Diags
)
342 TargetOptions
&TO
= Clang
->getTargetOpts();
343 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
344 return TargetInfo::CreateTargetInfo(Diags
, TO
);
349 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
351 static void create_diagnostics(CompilerInstance
*Clang
)
353 Clang
->createDiagnostics(0, NULL
, construct_printer());
358 static void create_diagnostics(CompilerInstance
*Clang
)
360 Clang
->createDiagnostics(construct_printer());
365 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
367 static void create_preprocessor(CompilerInstance
*Clang
)
369 Clang
->createPreprocessor(TU_Complete
);
374 static void create_preprocessor(CompilerInstance
*Clang
)
376 Clang
->createPreprocessor();
381 #ifdef ADDPATH_TAKES_4_ARGUMENTS
383 /* Add "Path" to the header search options.
385 * Do not take into account sysroot, i.e., set ignoreSysRoot to true.
387 void add_path(HeaderSearchOptions
&HSO
, string Path
)
389 HSO
.AddPath(Path
, frontend::Angled
, false, true);
394 /* Add "Path" to the header search options.
396 * Do not take into account sysroot, i.e., set IsSysRootRelative to false.
398 void add_path(HeaderSearchOptions
&HSO
, string Path
)
400 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
405 #ifdef HAVE_SETMAINFILEID
407 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
409 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
415 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
417 SM
.createMainFileID(file
);
422 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
424 #include "set_lang_defaults_arg4.h"
426 static void set_lang_defaults(CompilerInstance
*Clang
)
428 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
429 TargetOptions
&TO
= Clang
->getTargetOpts();
430 llvm::Triple
T(TO
.Triple
);
431 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
, T
,
432 setLangDefaultsArg4(PO
),
433 LangStandard::lang_unspecified
);
438 static void set_lang_defaults(CompilerInstance
*Clang
)
440 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
441 LangStandard::lang_unspecified
);
446 #ifdef SETINVOCATION_TAKES_SHARED_PTR
448 static void set_invocation(CompilerInstance
*Clang
,
449 CompilerInvocation
*invocation
)
451 Clang
->setInvocation(std::make_shared
<CompilerInvocation
>(*invocation
));
456 static void set_invocation(CompilerInstance
*Clang
,
457 CompilerInvocation
*invocation
)
459 Clang
->setInvocation(invocation
);
464 /* Helper function for ignore_error that only gets enabled if T
465 * (which is either const FileEntry * or llvm::ErrorOr<const FileEntry *>)
466 * has getError method, i.e., if it is llvm::ErrorOr<const FileEntry *>.
469 static const FileEntry
*ignore_error_helper(const T obj
, int,
470 int[1][sizeof(obj
.getError())])
475 /* Helper function for ignore_error that is always enabled,
476 * but that only gets selected if the variant above is not enabled,
477 * i.e., if T is const FileEntry *.
480 static const FileEntry
*ignore_error_helper(const T obj
, long, void *)
485 /* Given either a const FileEntry * or a llvm::ErrorOr<const FileEntry *>,
486 * extract out the const FileEntry *.
489 static const FileEntry
*ignore_error(const T obj
)
491 return ignore_error_helper(obj
, 0, NULL
);
494 /* Return the FileEntry corresponding to the given file name
495 * in the given compiler instances, ignoring any error.
497 static const FileEntry
*getFile(CompilerInstance
*Clang
, std::string Filename
)
499 return ignore_error(Clang
->getFileManager().getFile(Filename
));
502 /* Create an interface generator for the selected language and
503 * then use it to generate the interface.
505 static void generate(MyASTConsumer
&consumer
, SourceManager
&SM
)
509 if (OutputLanguage
.compare("python") == 0) {
510 gen
= new python_generator(SM
, consumer
.exported_types
,
511 consumer
.exported_functions
, consumer
.functions
);
512 } else if (OutputLanguage
.compare("cpp") == 0) {
513 gen
= new plain_cpp_generator(SM
, consumer
.exported_types
,
514 consumer
.exported_functions
, consumer
.functions
);
515 } else if (OutputLanguage
.compare("cpp-checked") == 0) {
516 gen
= new plain_cpp_generator(SM
, consumer
.exported_types
,
517 consumer
.exported_functions
, consumer
.functions
, true);
518 } else if (OutputLanguage
.compare("cpp-checked-conversion") == 0) {
519 gen
= new cpp_conversion_generator(SM
, consumer
.exported_types
,
520 consumer
.exported_functions
, consumer
.functions
);
521 } else if (OutputLanguage
.compare("template-cpp") == 0) {
522 gen
= new template_cpp_generator(SM
, consumer
.exported_types
,
523 consumer
.exported_functions
, consumer
.functions
);
525 cerr
<< "Language '" << OutputLanguage
526 << "' not recognized." << endl
527 << "Not generating bindings." << endl
;
534 int main(int argc
, char *argv
[])
536 llvm::cl::ParseCommandLineOptions(argc
, argv
);
538 CompilerInstance
*Clang
= new CompilerInstance();
539 create_diagnostics(Clang
);
540 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
541 Diags
.setSuppressSystemWarnings(true);
542 TargetInfo
*target
= create_target_info(Clang
, Diags
);
543 Clang
->setTarget(target
);
544 set_lang_defaults(Clang
);
545 CompilerInvocation
*invocation
=
546 construct_invocation(InputFilename
.c_str(), Diags
);
548 set_invocation(Clang
, invocation
);
549 Clang
->createFileManager();
550 Clang
->createSourceManager(Clang
->getFileManager());
551 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
552 LangOptions
&LO
= Clang
->getLangOpts();
553 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
554 HSO
.ResourceDir
= ResourceDir
;
556 for (llvm::cl::list
<string
>::size_type i
= 0; i
< Includes
.size(); ++i
)
557 add_path(HSO
, Includes
[i
]);
559 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
560 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
561 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
562 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
563 PO
.addMacroDef("__isl_overload="
564 "__attribute__((annotate(\"isl_overload\"))) "
565 "__attribute__((annotate(\"isl_export\")))");
566 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
567 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
569 create_preprocessor(Clang
);
570 Preprocessor
&PP
= Clang
->getPreprocessor();
572 PP
.getBuiltinInfo().initializeBuiltins(PP
.getIdentifierTable(), LO
);
574 const FileEntry
*file
= getFile(Clang
, InputFilename
);
576 create_main_file_id(Clang
->getSourceManager(), file
);
578 Clang
->createASTContext();
579 MyASTConsumer consumer
;
580 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
582 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
584 Diags
.getClient()->EndSourceFile();
586 generate(consumer
, Clang
->getSourceManager());
590 llvm::llvm_shutdown();
592 if (Diags
.hasErrorOccurred())