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 #include <clang/Lex/Preprocessor.h>
68 #include <clang/Parse/ParseAST.h>
69 #include <clang/Sema/Sema.h>
71 #include "extract_interface.h"
75 using namespace clang
;
76 using namespace clang::driver
;
78 #ifdef HAVE_ADT_OWNINGPTR_H
79 #define unique_ptr llvm::OwningPtr
82 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
83 llvm::cl::Required
, llvm::cl::desc("<input file>"));
84 static llvm::cl::list
<string
> Includes("I",
85 llvm::cl::desc("Header search path"),
86 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
88 static const char *ResourceDir
=
89 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
91 /* Does decl have an attribute of the following form?
93 * __attribute__((annotate("name")))
95 bool has_annotation(Decl
*decl
, const char *name
)
97 if (!decl
->hasAttrs())
100 AttrVec attrs
= decl
->getAttrs();
101 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
102 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
105 if (ann
->getAnnotation().str() == name
)
112 /* Is decl marked as exported?
114 static bool is_exported(Decl
*decl
)
116 return has_annotation(decl
, "isl_export");
119 /* Collect all types and functions that are annotated "isl_export"
120 * in "exported_types" and "exported_function". Collect all function
121 * declarations in "functions".
123 * We currently only consider single declarations.
125 struct MyASTConsumer
: public ASTConsumer
{
126 set
<RecordDecl
*> exported_types
;
127 set
<FunctionDecl
*> exported_functions
;
128 set
<FunctionDecl
*> functions
;
130 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
133 if (!D
.isSingleDecl())
134 return HandleTopLevelDeclContinue
;
135 decl
= D
.getSingleDecl();
136 if (isa
<FunctionDecl
>(decl
))
137 functions
.insert(cast
<FunctionDecl
>(decl
));
138 if (!is_exported(decl
))
139 return HandleTopLevelDeclContinue
;
140 switch (decl
->getKind()) {
142 exported_types
.insert(cast
<RecordDecl
>(decl
));
145 exported_functions
.insert(cast
<FunctionDecl
>(decl
));
150 return HandleTopLevelDeclContinue
;
156 #ifdef HAVE_CXXISPRODUCTION
157 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
159 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
160 "", false, false, Diags
);
162 #elif defined(HAVE_ISPRODUCTION)
163 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
165 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
168 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
169 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
171 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
175 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
177 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
181 namespace clang
{ namespace driver
{ class Job
; } }
183 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
184 * We fix this with a simple overloaded function here.
187 static Job
*command(Job
*J
) { return J
; }
188 static Job
*command(Job
&J
) { return &J
; }
189 static Command
*command(Command
&C
) { return &C
; }
192 /* Create a CompilerInvocation object that stores the command line
193 * arguments constructed by the driver.
194 * The arguments are mainly useful for setting up the system include
195 * paths on newer clangs and on some platforms.
197 static CompilerInvocation
*construct_invocation(const char *filename
,
198 DiagnosticsEngine
&Diags
)
200 const char *binary
= CLANG_PREFIX
"/bin/clang";
201 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
202 std::vector
<const char *> Argv
;
203 Argv
.push_back(binary
);
204 Argv
.push_back(filename
);
205 const unique_ptr
<Compilation
> compilation(
206 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
207 JobList
&Jobs
= compilation
->getJobs();
209 Command
*cmd
= cast
<Command
>(ClangAPI::command(*Jobs
.begin()));
210 if (strcmp(cmd
->getCreator().getName(), "clang"))
213 const ArgStringList
*args
= &cmd
->getArguments();
215 CompilerInvocation
*invocation
= new CompilerInvocation
;
216 CompilerInvocation::CreateFromArgs(*invocation
, args
->data() + 1,
217 args
->data() + args
->size(),
224 static CompilerInvocation
*construct_invocation(const char *filename
,
225 DiagnosticsEngine
&Diags
)
232 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
234 static TextDiagnosticPrinter
*construct_printer(void)
236 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
241 static TextDiagnosticPrinter
*construct_printer(void)
243 DiagnosticOptions DO
;
244 return new TextDiagnosticPrinter(llvm::errs(), DO
);
249 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
251 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
252 DiagnosticsEngine
&Diags
)
254 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
255 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
256 return TargetInfo::CreateTargetInfo(Diags
, TO
);
259 #elif defined(CREATETARGETINFO_TAKES_POINTER)
261 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
262 DiagnosticsEngine
&Diags
)
264 TargetOptions
&TO
= Clang
->getTargetOpts();
265 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
266 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
271 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
272 DiagnosticsEngine
&Diags
)
274 TargetOptions
&TO
= Clang
->getTargetOpts();
275 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
276 return TargetInfo::CreateTargetInfo(Diags
, TO
);
281 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
283 static void create_diagnostics(CompilerInstance
*Clang
)
285 Clang
->createDiagnostics(0, NULL
, construct_printer());
290 static void create_diagnostics(CompilerInstance
*Clang
)
292 Clang
->createDiagnostics(construct_printer());
297 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
299 static void create_preprocessor(CompilerInstance
*Clang
)
301 Clang
->createPreprocessor(TU_Complete
);
306 static void create_preprocessor(CompilerInstance
*Clang
)
308 Clang
->createPreprocessor();
313 #ifdef ADDPATH_TAKES_4_ARGUMENTS
315 void add_path(HeaderSearchOptions
&HSO
, string Path
)
317 HSO
.AddPath(Path
, frontend::Angled
, false, false);
322 void add_path(HeaderSearchOptions
&HSO
, string Path
)
324 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
329 #ifdef HAVE_SETMAINFILEID
331 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
333 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
339 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
341 SM
.createMainFileID(file
);
346 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
348 static void set_lang_defaults(CompilerInstance
*Clang
)
350 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
351 TargetOptions
&TO
= Clang
->getTargetOpts();
352 llvm::Triple
T(TO
.Triple
);
353 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
, T
, PO
,
354 LangStandard::lang_unspecified
);
359 static void set_lang_defaults(CompilerInstance
*Clang
)
361 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
362 LangStandard::lang_unspecified
);
367 int main(int argc
, char *argv
[])
369 llvm::cl::ParseCommandLineOptions(argc
, argv
);
371 CompilerInstance
*Clang
= new CompilerInstance();
372 create_diagnostics(Clang
);
373 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
374 Diags
.setSuppressSystemWarnings(true);
375 CompilerInvocation
*invocation
=
376 construct_invocation(InputFilename
.c_str(), Diags
);
378 Clang
->setInvocation(invocation
);
379 Clang
->createFileManager();
380 Clang
->createSourceManager(Clang
->getFileManager());
381 TargetInfo
*target
= create_target_info(Clang
, Diags
);
382 Clang
->setTarget(target
);
383 set_lang_defaults(Clang
);
384 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
385 LangOptions
&LO
= Clang
->getLangOpts();
386 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
387 HSO
.ResourceDir
= ResourceDir
;
389 for (llvm::cl::list
<string
>::size_type i
= 0; i
< Includes
.size(); ++i
)
390 add_path(HSO
, Includes
[i
]);
392 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
393 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
394 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
395 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
396 PO
.addMacroDef("__isl_overload="
397 "__attribute__((annotate(\"isl_overload\"))) "
398 "__attribute__((annotate(\"isl_export\")))");
399 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
400 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
401 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
403 create_preprocessor(Clang
);
404 Preprocessor
&PP
= Clang
->getPreprocessor();
406 PP
.getBuiltinInfo().initializeBuiltins(PP
.getIdentifierTable(), LO
);
408 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
410 create_main_file_id(Clang
->getSourceManager(), file
);
412 Clang
->createASTContext();
413 MyASTConsumer consumer
;
414 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
416 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
418 Diags
.getClient()->EndSourceFile();
420 generate_python(consumer
.exported_types
, consumer
.exported_functions
,
425 llvm::llvm_shutdown();