[cfe-dev] Compile to ast and llvm bytecode using Clang API

Michael Collison via cfe-dev cfe-dev at lists.llvm.org
Sun Nov 11 13:47:57 PST 2018


João,

Thank you for your reply. I t appears from your response that I need to 
make "two passes" over the input file: one to generate my modified ast 
output file using FrontEndAction and a second pass using the code 
template your provided to generate the .bc file.

On 11/11/2018 2:33 PM, João Paulo Labegalini de Carvalho wrote:
> Michael,
>
> The easiest way to use Clang API to perform "standard" compiler tasks 
> is through the Driver class 
> <https://clang.llvm.org/doxygen/classclang_1_1driver_1_1Driver.html>.
> Basically what you need to do is setup a command line invocation, 
> using the flags from a compilation database or adding your own, build 
> a Compilation object and execute it through the driver.
>
> For instance:
>
> std::string CommandLine = "clang++ -Wall -O3 -emit-llvm -o test.bc 
> test.cpp";
> clang::DiagnosticOptions* DiagOptions = new DiagnosticOptions();
> clang::DiagnosticConsumer *DiagClient = new 
> TextDiagnosticPrinter(llvm::errs(), DiagOptions);
> llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new 
> clang::DiagnosticIDs());
> clang::DiagnosticsEngine Diags(DiagID, DiagOptions, DiagClient);
> clang::driver::Driver TheDriver("clang++", 
> llvm::sys::getDefaultTargetTriple(), Diags);
> const std::unique_ptr<driver::Compilation> 
> compilation(TheDriver.BuildCompilation(CommandLine));
> if (!compilation) { llvm::errs() << "failed to build compilation 
> object!\n"; }
> llvm::SmallVector<std::pair<int, const driver::Command*>,10> 
> failingCommands;
> int res = TheDriver.ExecuteCompilation(*compilation, failingCommands);
> if (res < 0) {
> ProcessingFailed = true;
> for (const std::pair<int, const driver::Command*>& p : failingCommands) {
>   if (p.first) {
> TheDriver.generateCompilationDiagnostics(*compilation, *p.second);
>   }
> }
> }
>
>
> On Sun, Nov 11, 2018 at 6:53 PM Michael Collison via cfe-dev 
> <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
>
>     I have a requirement to parse a .cpp file to a .ast file and a .bc
>     file.
>     I have the parsing, with filtering of the AST working using the the
>     tooling tempalte on this page:
>     https://clang.llvm.org/docs/RAVFrontendAction.html. I also need to
>     generate LLVM code to write out to a .bc file. I tried using :
>
>     tool.run (newFrontEndActionFactory<EmitBCAction>().get())
>
>     where tool is an instance of ClangTool but received an error
>     saying "not
>     enough position command line arguments specified". Any ideas? Are
>     there
>     any examples I can look at?
>
>     Regards,
>
>     Michael Collison
>
>
>     _______________________________________________
>     cfe-dev mailing list
>     cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>     http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
>
> -- 
> João Paulo L. de Carvalho
> Computer Science |  IC-UNICAMP | Campinas , SP - Brazil
> jaopaulolc at gmail.com <mailto:jaopaulolc at gmail.com>
> joao.carvalho at ic.unicamp.br <mailto:joao.carvalho at ic.unicamp.br>
> j160924 at dac.unicamp.br <mailto:j160924 at dac.unicamp.br>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181111/0d50f6a2/attachment.html>


More information about the cfe-dev mailing list