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 #ifdef HAVE_ADT_OWNINGPTR_H
39 #include <llvm/ADT/OwningPtr.h>
43 #include <llvm/Support/raw_ostream.h>
44 #include <llvm/Support/CommandLine.h>
45 #include <llvm/Support/Host.h>
46 #include <llvm/Support/ManagedStatic.h>
47 #include <clang/AST/ASTContext.h>
48 #include <clang/AST/ASTConsumer.h>
49 #include <clang/Basic/FileSystemOptions.h>
50 #include <clang/Basic/FileManager.h>
51 #include <clang/Basic/TargetOptions.h>
52 #include <clang/Basic/TargetInfo.h>
53 #include <clang/Basic/Version.h>
54 #include <clang/Driver/Compilation.h>
55 #include <clang/Driver/Driver.h>
56 #include <clang/Driver/Tool.h>
57 #include <clang/Frontend/CompilerInstance.h>
58 #include <clang/Frontend/CompilerInvocation.h>
59 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
60 #include <clang/Basic/DiagnosticOptions.h>
62 #include <clang/Frontend/DiagnosticOptions.h>
64 #include <clang/Frontend/TextDiagnosticPrinter.h>
65 #include <clang/Frontend/Utils.h>
66 #include <clang/Lex/HeaderSearch.h>
67 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
68 #include <clang/Lex/PreprocessorOptions.h>
70 #include <clang/Frontend/PreprocessorOptions.h>
72 #include <clang/Lex/Preprocessor.h>
73 #include <clang/Parse/ParseAST.h>
74 #include <clang/Sema/Sema.h>
76 #include "extract_interface.h"
80 using namespace clang
;
81 using namespace clang::driver
;
83 #ifdef HAVE_ADT_OWNINGPTR_H
84 #define unique_ptr llvm::OwningPtr
87 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
88 llvm::cl::Required
, llvm::cl::desc("<input file>"));
89 static llvm::cl::list
<string
> Includes("I",
90 llvm::cl::desc("Header search path"),
91 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
93 static const char *ResourceDir
=
94 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
96 /* Does decl have an attribute of the following form?
98 * __attribute__((annotate("name")))
100 bool has_annotation(Decl
*decl
, const char *name
)
102 if (!decl
->hasAttrs())
105 AttrVec attrs
= decl
->getAttrs();
106 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
107 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
110 if (ann
->getAnnotation().str() == name
)
117 /* Is decl marked as exported?
119 static bool is_exported(Decl
*decl
)
121 return has_annotation(decl
, "isl_export");
124 /* Collect all types and functions that are annotated "isl_export"
125 * in "exported_types" and "exported_function". Collect all function
126 * declarations in "functions".
128 * We currently only consider single declarations.
130 struct MyASTConsumer
: public ASTConsumer
{
131 set
<RecordDecl
*> exported_types
;
132 set
<FunctionDecl
*> exported_functions
;
133 set
<FunctionDecl
*> functions
;
135 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
138 if (!D
.isSingleDecl())
139 return HandleTopLevelDeclContinue
;
140 decl
= D
.getSingleDecl();
141 if (isa
<FunctionDecl
>(decl
))
142 functions
.insert(cast
<FunctionDecl
>(decl
));
143 if (!is_exported(decl
))
144 return HandleTopLevelDeclContinue
;
145 switch (decl
->getKind()) {
147 exported_types
.insert(cast
<RecordDecl
>(decl
));
150 exported_functions
.insert(cast
<FunctionDecl
>(decl
));
155 return HandleTopLevelDeclContinue
;
161 #ifdef HAVE_CXXISPRODUCTION
162 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
164 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
165 "", false, false, Diags
);
167 #elif defined(HAVE_ISPRODUCTION)
168 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
170 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
173 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
174 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
176 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
180 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
182 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
186 namespace clang
{ namespace driver
{ class Job
; } }
188 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
189 * We fix this with a simple overloaded function here.
192 static Job
*command(Job
*J
) { return J
; }
193 static Job
*command(Job
&J
) { return &J
; }
194 static Command
*command(Command
&C
) { return &C
; }
197 /* Create a CompilerInvocation object that stores the command line
198 * arguments constructed by the driver.
199 * The arguments are mainly useful for setting up the system include
200 * paths on newer clangs and on some platforms.
202 static CompilerInvocation
*construct_invocation(const char *filename
,
203 DiagnosticsEngine
&Diags
)
205 const char *binary
= CLANG_PREFIX
"/bin/clang";
206 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
207 std::vector
<const char *> Argv
;
208 Argv
.push_back(binary
);
209 Argv
.push_back(filename
);
210 const unique_ptr
<Compilation
> compilation(
211 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
212 JobList
&Jobs
= compilation
->getJobs();
214 Command
*cmd
= cast
<Command
>(ClangAPI::command(*Jobs
.begin()));
215 if (strcmp(cmd
->getCreator().getName(), "clang"))
218 const ArgStringList
*args
= &cmd
->getArguments();
220 CompilerInvocation
*invocation
= new CompilerInvocation
;
221 CompilerInvocation::CreateFromArgs(*invocation
, args
->data() + 1,
222 args
->data() + args
->size(),
229 static CompilerInvocation
*construct_invocation(const char *filename
,
230 DiagnosticsEngine
&Diags
)
237 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
239 static TextDiagnosticPrinter
*construct_printer(void)
241 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
246 static TextDiagnosticPrinter
*construct_printer(void)
248 DiagnosticOptions DO
;
249 return new TextDiagnosticPrinter(llvm::errs(), DO
);
254 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
256 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
257 DiagnosticsEngine
&Diags
)
259 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
260 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
261 return TargetInfo::CreateTargetInfo(Diags
, TO
);
264 #elif defined(CREATETARGETINFO_TAKES_POINTER)
266 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
267 DiagnosticsEngine
&Diags
)
269 TargetOptions
&TO
= Clang
->getTargetOpts();
270 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
271 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
276 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
277 DiagnosticsEngine
&Diags
)
279 TargetOptions
&TO
= Clang
->getTargetOpts();
280 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
281 return TargetInfo::CreateTargetInfo(Diags
, TO
);
286 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
288 static void create_diagnostics(CompilerInstance
*Clang
)
290 Clang
->createDiagnostics(0, NULL
, construct_printer());
295 static void create_diagnostics(CompilerInstance
*Clang
)
297 Clang
->createDiagnostics(construct_printer());
302 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
304 static void create_preprocessor(CompilerInstance
*Clang
)
306 Clang
->createPreprocessor(TU_Complete
);
311 static void create_preprocessor(CompilerInstance
*Clang
)
313 Clang
->createPreprocessor();
318 #ifdef ADDPATH_TAKES_4_ARGUMENTS
320 void add_path(HeaderSearchOptions
&HSO
, string Path
)
322 HSO
.AddPath(Path
, frontend::Angled
, false, false);
327 void add_path(HeaderSearchOptions
&HSO
, string Path
)
329 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
334 #ifdef HAVE_SETMAINFILEID
336 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
338 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
344 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
346 SM
.createMainFileID(file
);
351 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
353 static void set_lang_defaults(CompilerInstance
*Clang
)
355 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
356 TargetOptions
&TO
= Clang
->getTargetOpts();
357 llvm::Triple
T(TO
.Triple
);
358 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
, T
, PO
,
359 LangStandard::lang_unspecified
);
364 static void set_lang_defaults(CompilerInstance
*Clang
)
366 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
367 LangStandard::lang_unspecified
);
372 int main(int argc
, char *argv
[])
374 llvm::cl::ParseCommandLineOptions(argc
, argv
);
376 CompilerInstance
*Clang
= new CompilerInstance();
377 create_diagnostics(Clang
);
378 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
379 Diags
.setSuppressSystemWarnings(true);
380 CompilerInvocation
*invocation
=
381 construct_invocation(InputFilename
.c_str(), Diags
);
383 Clang
->setInvocation(invocation
);
384 Clang
->createFileManager();
385 Clang
->createSourceManager(Clang
->getFileManager());
386 TargetInfo
*target
= create_target_info(Clang
, Diags
);
387 Clang
->setTarget(target
);
388 set_lang_defaults(Clang
);
389 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
390 LangOptions
&LO
= Clang
->getLangOpts();
391 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
392 HSO
.ResourceDir
= ResourceDir
;
394 for (llvm::cl::list
<string
>::size_type i
= 0; i
< Includes
.size(); ++i
)
395 add_path(HSO
, Includes
[i
]);
397 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
398 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
399 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
400 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
401 PO
.addMacroDef("__isl_overload="
402 "__attribute__((annotate(\"isl_overload\"))) "
403 "__attribute__((annotate(\"isl_export\")))");
404 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
405 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
406 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
408 create_preprocessor(Clang
);
409 Preprocessor
&PP
= Clang
->getPreprocessor();
411 PP
.getBuiltinInfo().initializeBuiltins(PP
.getIdentifierTable(), LO
);
413 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
415 create_main_file_id(Clang
->getSourceManager(), file
);
417 Clang
->createASTContext();
418 MyASTConsumer consumer
;
419 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
421 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
423 Diags
.getClient()->EndSourceFile();
425 generate_python(consumer
.exported_types
, consumer
.exported_functions
,
430 llvm::llvm_shutdown();