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
36 #include <llvm/Support/raw_ostream.h>
37 #include <llvm/Support/CommandLine.h>
38 #include <llvm/Support/Host.h>
39 #include <llvm/Support/ManagedStatic.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTConsumer.h>
42 #include <clang/Basic/FileSystemOptions.h>
43 #include <clang/Basic/FileManager.h>
44 #include <clang/Basic/TargetOptions.h>
45 #include <clang/Basic/TargetInfo.h>
46 #include <clang/Basic/Version.h>
47 #include <clang/Frontend/CompilerInstance.h>
48 #include <clang/Frontend/CompilerInvocation.h>
49 #include <clang/Frontend/DiagnosticOptions.h>
50 #include <clang/Frontend/TextDiagnosticPrinter.h>
51 #include <clang/Frontend/Utils.h>
52 #include <clang/Lex/HeaderSearch.h>
53 #include <clang/Lex/Preprocessor.h>
54 #include <clang/Parse/ParseAST.h>
55 #include <clang/Sema/Sema.h>
57 #include "isl_config.h"
58 #include "extract_interface.h"
62 using namespace clang
;
64 static llvm::cl::opt
<string
> InputFilename(llvm::cl::Positional
,
65 llvm::cl::Required
, llvm::cl::desc("<input file>"));
66 static llvm::cl::list
<string
> Includes("I",
67 llvm::cl::desc("Header search path"),
68 llvm::cl::value_desc("path"), llvm::cl::Prefix
);
70 static const char *ResourceDir
= CLANG_PREFIX
"/lib/clang/"CLANG_VERSION_STRING
;
72 /* Does decl have an attribute of the following form?
74 * __attribute__((annotate("name")))
76 bool has_annotation(Decl
*decl
, const char *name
)
78 if (!decl
->hasAttrs())
81 AttrVec attrs
= decl
->getAttrs();
82 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
83 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
86 if (ann
->getAnnotation().str() == name
)
93 /* Is decl marked as exported?
95 static bool is_exported(Decl
*decl
)
97 return has_annotation(decl
, "isl_export");
100 /* Collect all types and functions that are annotated "isl_export"
101 * in "types" and "function".
103 * We currently only consider single declarations.
105 struct MyASTConsumer
: public ASTConsumer
{
106 set
<RecordDecl
*> types
;
107 set
<FunctionDecl
*> functions
;
109 virtual void HandleTopLevelDecl(DeclGroupRef D
) {
112 if (!D
.isSingleDecl())
114 decl
= D
.getSingleDecl();
115 if (!is_exported(decl
))
117 switch (decl
->getKind()) {
119 types
.insert(cast
<RecordDecl
>(decl
));
122 functions
.insert(cast
<FunctionDecl
>(decl
));
130 int main(int argc
, char *argv
[])
132 llvm::cl::ParseCommandLineOptions(argc
, argv
);
134 CompilerInstance
*Clang
= new CompilerInstance();
135 DiagnosticOptions DO
;
136 Clang
->createDiagnostics(0, NULL
,
137 new TextDiagnosticPrinter(llvm::errs(), DO
));
138 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
139 Diags
.setSuppressSystemWarnings(true);
140 Clang
->createFileManager();
141 Clang
->createSourceManager(Clang
->getFileManager());
143 TO
.Triple
= llvm::sys::getHostTriple();
144 TargetInfo
*target
= TargetInfo::CreateTargetInfo(Diags
, TO
);
145 Clang
->setTarget(target
);
146 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
147 LangStandard::lang_unspecified
);
148 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
149 LangOptions
&LO
= Clang
->getLangOpts();
150 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
151 HSO
.ResourceDir
= ResourceDir
;
153 for (int i
= 0; i
< Includes
.size(); ++i
)
154 HSO
.AddPath(Includes
[i
], frontend::Angled
, true, false, false);
156 PO
.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
157 PO
.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
158 PO
.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
159 PO
.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
160 PO
.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
161 PO
.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
163 Clang
->createPreprocessor();
164 Preprocessor
&PP
= Clang
->getPreprocessor();
166 PP
.getBuiltinInfo().InitializeBuiltins(PP
.getIdentifierTable(), LO
);
168 const FileEntry
*file
= Clang
->getFileManager().getFile(InputFilename
);
170 Clang
->getSourceManager().createMainFileID(file
);
172 Clang
->createASTContext();
173 MyASTConsumer consumer
;
174 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
176 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
178 Diags
.getClient()->EndSourceFile();
180 generate_python(consumer
.types
, consumer
.functions
);
184 llvm::llvm_shutdown();