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 "types" and "function".
122 * We currently only consider single declarations.
124 struct MyASTConsumer
: public ASTConsumer
{
125 set
<RecordDecl
*> types
;
126 set
<FunctionDecl
*> functions
;
128 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef D
) {
131 if (!D
.isSingleDecl())
132 return HandleTopLevelDeclContinue
;
133 decl
= D
.getSingleDecl();
134 if (!is_exported(decl
))
135 return HandleTopLevelDeclContinue
;
136 switch (decl
->getKind()) {
138 types
.insert(cast
<RecordDecl
>(decl
));
141 functions
.insert(cast
<FunctionDecl
>(decl
));
146 return HandleTopLevelDeclContinue
;
152 #ifdef HAVE_CXXISPRODUCTION
153 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
155 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
156 "", false, false, Diags
);
158 #elif defined(HAVE_ISPRODUCTION)
159 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
161 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
164 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
165 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
167 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
171 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
173 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
177 /* Create a CompilerInvocation object that stores the command line
178 * arguments constructed by the driver.
179 * The arguments are mainly useful for setting up the system include
180 * paths on newer clangs and on some platforms.
182 static CompilerInvocation
*construct_invocation(const char *filename
,
183 DiagnosticsEngine
&Diags
)
185 const char *binary
= CLANG_PREFIX
"/bin/clang";
186 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
187 std::vector
<const char *> Argv
;
188 Argv
.push_back(binary
);
189 Argv
.push_back(filename
);
190 const unique_ptr
<Compilation
> compilation(
191 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
192 JobList
&Jobs
= compilation
->getJobs();
194 Command
*cmd
= cast
<Command
>(*Jobs
.begin());
195 if (strcmp(cmd
->getCreator().getName(), "clang"))
198 const ArgStringList
*args
= &cmd
->getArguments();
200 CompilerInvocation
*invocation
= new CompilerInvocation
;
201 CompilerInvocation::CreateFromArgs(*invocation
, args
->data() + 1,
202 args
->data() + args
->size(),
209 static CompilerInvocation
*construct_invocation(const char *filename
,
210 DiagnosticsEngine
&Diags
)
217 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
219 static TextDiagnosticPrinter
*construct_printer(void)
221 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
226 static TextDiagnosticPrinter
*construct_printer(void)
228 DiagnosticOptions DO
;
229 return new TextDiagnosticPrinter(llvm::errs(), DO
);
234 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
236 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
237 DiagnosticsEngine
&Diags
)
239 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
240 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
241 return TargetInfo::CreateTargetInfo(Diags
, TO
);
244 #elif defined(CREATETARGETINFO_TAKES_POINTER)
246 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
247 DiagnosticsEngine
&Diags
)
249 TargetOptions
&TO
= Clang
->getTargetOpts();
250 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
251 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
256 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
257 DiagnosticsEngine
&Diags
)
259 TargetOptions
&TO
= Clang
->getTargetOpts();
260 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
261 return TargetInfo::CreateTargetInfo(Diags
, TO
);
266 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
268 static void create_diagnostics(CompilerInstance
*Clang
)
270 Clang
->createDiagnostics(0, NULL
, construct_printer());
275 static void create_diagnostics(CompilerInstance
*Clang
)
277 Clang
->createDiagnostics(construct_printer());
282 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
284 static void create_preprocessor(CompilerInstance
*Clang
)
286 Clang
->createPreprocessor(TU_Complete
);
291 static void create_preprocessor(CompilerInstance
*Clang
)
293 Clang
->createPreprocessor();
298 #ifdef ADDPATH_TAKES_4_ARGUMENTS
300 void add_path(HeaderSearchOptions
&HSO
, string Path
)
302 HSO
.AddPath(Path
, frontend::Angled
, false, false);
307 void add_path(HeaderSearchOptions
&HSO
, string Path
)
309 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
314 #ifdef HAVE_SETMAINFILEID
316 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
318 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
324 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
326 SM
.createMainFileID(file
);
331 int main(int argc
, char *argv
[])
333 llvm::cl::ParseCommandLineOptions(argc
, argv
);
335 CompilerInstance
*Clang
= new CompilerInstance();
336 create_diagnostics(Clang
);
337 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
338 Diags
.setSuppressSystemWarnings(true);
339 CompilerInvocation
*invocation
=
340 construct_invocation(InputFilename
.c_str(), Diags
);
342 Clang
->setInvocation(invocation
);
343 Clang
->createFileManager();
344 Clang
->createSourceManager(Clang
->getFileManager());
345 TargetInfo
*target
= create_target_info(Clang
, Diags
);
346 Clang
->setTarget(target
);
347 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
348 LangStandard::lang_unspecified
);
349 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
350 LangOptions
&LO
= Clang
->getLangOpts();
351 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
352 HSO
.ResourceDir
= ResourceDir
;
354 for (int i
= 0; i
< Includes
.size(); ++i
)
355 add_path(HSO
, Includes
[i
]);
357 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
358 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
359 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
360 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
361 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
362 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
364 create_preprocessor(Clang
);
365 Preprocessor
&PP
= Clang
->getPreprocessor();
367 PP
.getBuiltinInfo().InitializeBuiltins(PP
.getIdentifierTable(), LO
);
369 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
371 create_main_file_id(Clang
->getSourceManager(), file
);
373 Clang
->createASTContext();
374 MyASTConsumer consumer
;
375 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
377 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
379 Diags
.getClient()->EndSourceFile();
381 generate_python(consumer
.types
, consumer
.functions
);
385 llvm::llvm_shutdown();