nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Copyright 2011 Sven Verdoolaege. All rights reserved. |
||
3 | * |
||
4 | * Redistribution and use in source and binary forms, with or without |
||
5 | * modification, are permitted provided that the following conditions |
||
6 | * are met: |
||
7 | * |
||
8 | * 1. Redistributions of source code must retain the above copyright |
||
9 | * notice, this list of conditions and the following disclaimer. |
||
10 | * |
||
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. |
||
15 | * |
||
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. |
||
27 | * |
||
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 |
||
31 | * Sven Verdoolaege. |
||
32 | */ |
||
33 | |||
34 | #include <assert.h> |
||
35 | #include <iostream> |
||
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/Driver/Compilation.h> |
||
48 | #include <clang/Driver/Driver.h> |
||
49 | #include <clang/Driver/Tool.h> |
||
50 | #include <clang/Frontend/CompilerInstance.h> |
||
51 | #include <clang/Frontend/CompilerInvocation.h> |
||
52 | #include <clang/Frontend/DiagnosticOptions.h> |
||
53 | #include <clang/Frontend/TextDiagnosticPrinter.h> |
||
54 | #include <clang/Frontend/Utils.h> |
||
55 | #include <clang/Lex/HeaderSearch.h> |
||
56 | #include <clang/Lex/Preprocessor.h> |
||
57 | #include <clang/Parse/ParseAST.h> |
||
58 | #include <clang/Sema/Sema.h> |
||
59 | |||
60 | #include "isl_config.h" |
||
61 | #include "extract_interface.h" |
||
62 | #include "python.h" |
||
63 | |||
64 | using namespace std; |
||
65 | using namespace clang; |
||
66 | using namespace clang::driver; |
||
67 | |||
68 | static llvm::cl::opt<string> InputFilename(llvm::cl::Positional, |
||
69 | llvm::cl::Required, llvm::cl::desc("<input file>")); |
||
70 | static llvm::cl::list<string> Includes("I", |
||
71 | llvm::cl::desc("Header search path"), |
||
72 | llvm::cl::value_desc("path"), llvm::cl::Prefix); |
||
73 | |||
74 | static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING; |
||
75 | |||
76 | /* Does decl have an attribute of the following form? |
||
77 | * |
||
78 | * __attribute__((annotate("name"))) |
||
79 | */ |
||
80 | bool has_annotation(Decl *decl, const char *name) |
||
81 | { |
||
82 | if (!decl->hasAttrs()) |
||
83 | return false; |
||
84 | |||
85 | AttrVec attrs = decl->getAttrs(); |
||
86 | for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) { |
||
87 | const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i); |
||
88 | if (!ann) |
||
89 | continue; |
||
90 | if (ann->getAnnotation().str() == name) |
||
91 | return true; |
||
92 | } |
||
93 | |||
94 | return false; |
||
95 | } |
||
96 | |||
97 | /* Is decl marked as exported? |
||
98 | */ |
||
99 | static bool is_exported(Decl *decl) |
||
100 | { |
||
101 | return has_annotation(decl, "isl_export"); |
||
102 | } |
||
103 | |||
104 | /* Collect all types and functions that are annotated "isl_export" |
||
105 | * in "types" and "function". |
||
106 | * |
||
107 | * We currently only consider single declarations. |
||
108 | */ |
||
109 | struct MyASTConsumer : public ASTConsumer { |
||
110 | set<RecordDecl *> types; |
||
111 | set<FunctionDecl *> functions; |
||
112 | |||
113 | virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) { |
||
114 | Decl *decl; |
||
115 | |||
116 | if (!D.isSingleDecl()) |
||
117 | return HandleTopLevelDeclContinue; |
||
118 | decl = D.getSingleDecl(); |
||
119 | if (!is_exported(decl)) |
||
120 | return HandleTopLevelDeclContinue; |
||
121 | switch (decl->getKind()) { |
||
122 | case Decl::Record: |
||
123 | types.insert(cast<RecordDecl>(decl)); |
||
124 | break; |
||
125 | case Decl::Function: |
||
126 | functions.insert(cast<FunctionDecl>(decl)); |
||
127 | break; |
||
128 | default: |
||
129 | break; |
||
130 | } |
||
131 | return HandleTopLevelDeclContinue; |
||
132 | } |
||
133 | }; |
||
134 | |||
135 | #ifdef USE_ARRAYREF |
||
136 | |||
137 | #ifdef HAVE_CXXISPRODUCTION |
||
138 | static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags) |
||
139 | { |
||
140 | return new Driver(binary, llvm::sys::getDefaultTargetTriple(), |
||
141 | "", false, false, Diags); |
||
142 | } |
||
143 | #else |
||
144 | static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags) |
||
145 | { |
||
146 | return new Driver(binary, llvm::sys::getDefaultTargetTriple(), |
||
147 | "", false, Diags); |
||
148 | } |
||
149 | #endif |
||
150 | |||
151 | /* Create a CompilerInvocation object that stores the command line |
||
152 | * arguments constructed by the driver. |
||
153 | * The arguments are mainly useful for setting up the system include |
||
154 | * paths on newer clangs and on some platforms. |
||
155 | */ |
||
156 | static CompilerInvocation *construct_invocation(const char *filename, |
||
157 | DiagnosticsEngine &Diags) |
||
158 | { |
||
159 | const char *binary = CLANG_PREFIX"/bin/clang"; |
||
160 | const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags)); |
||
161 | std::vector<const char *> Argv; |
||
162 | Argv.push_back(binary); |
||
163 | Argv.push_back(filename); |
||
164 | const llvm::OwningPtr<Compilation> compilation( |
||
165 | driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv))); |
||
166 | JobList &Jobs = compilation->getJobs(); |
||
167 | |||
168 | Command *cmd = cast<Command>(*Jobs.begin()); |
||
169 | if (strcmp(cmd->getCreator().getName(), "clang")) |
||
170 | return NULL; |
||
171 | |||
172 | const ArgStringList *args = &cmd->getArguments(); |
||
173 | |||
174 | CompilerInvocation *invocation = new CompilerInvocation; |
||
175 | CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1, |
||
176 | args->data() + args->size(), |
||
177 | Diags); |
||
178 | return invocation; |
||
179 | } |
||
180 | |||
181 | #else |
||
182 | |||
183 | static CompilerInvocation *construct_invocation(const char *filename, |
||
184 | DiagnosticsEngine &Diags) |
||
185 | { |
||
186 | return NULL; |
||
187 | } |
||
188 | |||
189 | #endif |
||
190 | |||
191 | int main(int argc, char *argv[]) |
||
192 | { |
||
193 | llvm::cl::ParseCommandLineOptions(argc, argv); |
||
194 | |||
195 | CompilerInstance *Clang = new CompilerInstance(); |
||
196 | DiagnosticOptions DO; |
||
197 | Clang->createDiagnostics(0, NULL, |
||
198 | new TextDiagnosticPrinter(llvm::errs(), DO)); |
||
199 | DiagnosticsEngine &Diags = Clang->getDiagnostics(); |
||
200 | Diags.setSuppressSystemWarnings(true); |
||
201 | CompilerInvocation *invocation = |
||
202 | construct_invocation(InputFilename.c_str(), Diags); |
||
203 | if (invocation) |
||
204 | Clang->setInvocation(invocation); |
||
205 | Clang->createFileManager(); |
||
206 | Clang->createSourceManager(Clang->getFileManager()); |
||
207 | TargetOptions TO; |
||
208 | TO.Triple = llvm::sys::getDefaultTargetTriple(); |
||
209 | TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO); |
||
210 | Clang->setTarget(target); |
||
211 | CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C, |
||
212 | LangStandard::lang_unspecified); |
||
213 | HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts(); |
||
214 | LangOptions &LO = Clang->getLangOpts(); |
||
215 | PreprocessorOptions &PO = Clang->getPreprocessorOpts(); |
||
216 | HSO.ResourceDir = ResourceDir; |
||
217 | |||
218 | for (int i = 0; i < Includes.size(); ++i) |
||
219 | HSO.AddPath(Includes[i], frontend::Angled, true, false, false); |
||
220 | |||
221 | PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))"); |
||
222 | PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))"); |
||
223 | PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))"); |
||
224 | PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))"); |
||
225 | PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))"); |
||
226 | PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))"); |
||
227 | |||
228 | Clang->createPreprocessor(); |
||
229 | Preprocessor &PP = Clang->getPreprocessor(); |
||
230 | |||
231 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO); |
||
232 | |||
233 | const FileEntry *file = Clang->getFileManager().getFile(InputFilename); |
||
234 | assert(file); |
||
235 | Clang->getSourceManager().createMainFileID(file); |
||
236 | |||
237 | Clang->createASTContext(); |
||
238 | MyASTConsumer consumer; |
||
239 | Sema *sema = new Sema(PP, Clang->getASTContext(), consumer); |
||
240 | |||
241 | Diags.getClient()->BeginSourceFile(LO, &PP); |
||
242 | ParseAST(*sema); |
||
243 | Diags.getClient()->EndSourceFile(); |
||
244 | |||
245 | generate_python(consumer.types, consumer.functions); |
||
246 | |||
247 | delete sema; |
||
248 | delete Clang; |
||
249 | llvm::llvm_shutdown(); |
||
250 | |||
251 | return 0; |
||
252 | } |