<div dir="ltr"><div>I'm trying to use LLVM/Clang API to compile the source code to LLVM IR.</div><div><br></div><div>clang_ir.cpp:</div><div><br></div><div> #include <iostream></div><div> </div><div> #include <clang/Driver/Compilation.h></div>
<div> #include <clang/Driver/Driver.h></div><div> #include <clang/Frontend/TextDiagnosticPrinter.h></div><div> #include <llvm/Support/Host.h></div><div> #include <llvm/Support/Program.h></div>
<div> #include <llvm/Support/raw_ostream.h></div><div> </div><div> using namespace std;</div><div> using namespace clang;</div><div> using namespace clang::driver;</div><div> </div><div> int main(int argc, char** argv)</div>
<div> {</div><div> cout << "Starting ----" << std::endl;</div><div> </div><div> <span class="" style="white-space:pre"> </span>// [clang -S -emit-llvm ./test/hello_world.cpp]</div><div>
</div><div> // Arguments to pass to the clang driver:</div><div> <span class="" style="white-space:pre"> </span>// clang getinmemory.c -lcurl -v</div><div> <span class="" style="white-space:pre"> </span>// Path to the C file</div>
<div> <span class="" style="white-space:pre"> </span>string clangPath = "clang";</div><div> </div><div> <span class="" style="white-space:pre"> </span>string inputPath = "./test/hello_world.cpp";</div>
<div> <span class="" style="white-space:pre"> </span>string outputPath = "hello_world.ll";</div><div> </div><div> <span class="" style="white-space:pre"> </span>vector<const char *> args;</div><div>
<span class="" style="white-space:pre"> </span>args.push_back(clangPath.c_str());</div><div> <span class="" style="white-space:pre"> </span>args.push_back("-S");</div><div> <span class="" style="white-space:pre"> </span>args.push_back("-emit-llvm");</div>
<div> <span class="" style="white-space:pre"> </span>args.push_back(inputPath.c_str());</div><div> </div><div> <span class="" style="white-space:pre"> </span>// The clang driver needs a DiagnosticsEngine so it can report problems</div>
<div> <span class="" style="white-space:pre"> </span>clang::DiagnosticOptions *Options = new clang::DiagnosticOptions();</div><div> <span class="" style="white-space:pre"> </span>//clang::TextDiagnosticPrinter *DiagClient = new clang::TextDiagnosticPrinter(llvm::errs(), Options);</div>
<div> <span class="" style="white-space:pre"> </span>clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());</div><div> <span class="" style="white-space:pre"> </span>clang::DiagnosticsEngine Diags(DiagID, Options);</div>
<div> <span class="" style="white-space:pre"> </span></div><div> <span class="" style="white-space:pre"> </span>cout << "making driver" << endl;</div><div> </div><div> <span class="" style="white-space:pre"> </span>// Create the clang driver</div>
<div> <span class="" style="white-space:pre"> </span>clang::driver::Driver TheDriver(args[0], llvm::sys::getDefaultTargetTriple(), outputPath, Diags);</div><div> </div><div> <span class="" style="white-space:pre"> </span>// C++ code</div>
<div> <span class="" style="white-space:pre"> </span>//TheDriver.CCCIsCXX = true;</div><div> </div><div> <span class="" style="white-space:pre"> </span>cout << "making compilation" << endl;</div>
<div> </div><div> <span class="" style="white-space:pre"> </span>// Create the set of actions to perform</div><div> <span class="" style="white-space:pre"> </span>clang::OwningPtr<clang::driver::Compilation> Compilation(TheDriver.BuildCompilation(args));</div>
<div> <span class="" style="white-space:pre"> </span></div><div> <span class="" style="white-space:pre"> </span>cout << "printing actions:" << endl;</div><div> </div><div> <span class="" style="white-space:pre"> </span>// Print the set of actions</div>
<div> <span class="" style="white-space:pre"> </span>TheDriver.PrintActions(*Compilation);</div><div> </div><div> std::cout << "Done ----" << std::endl;</div><div> </div><div> // Carry out the actions</div>
<div> <span class="" style="white-space:pre"> </span>int Res = 0;</div><div> <span class="" style="white-space:pre"> </span>SmallVector<std::pair<int, const Command *>, 4> FailingCommands;</div><div> <span class="" style="white-space:pre"> </span>if (Compilation)</div>
<div> <span class="" style="white-space:pre"> </span>Res = TheDriver.ExecuteCompilation(*Compilation, FailingCommands);</div><div> <span class="" style="white-space:pre"> </span></div><div> <span class="" style="white-space:pre"> </span>// Report problems</div>
<div> <span class="" style="white-space:pre"> </span>/*</div><div> <span class="" style="white-space:pre"> </span>if (Res < 0)</div><div> <span class="" style="white-space:pre"> </span>TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommands);</div>
<div> <span class="" style="white-space:pre"> </span>*/</div><div> <span class="" style="white-space:pre"> </span>for (SmallVectorImpl< std::pair<int,</div><div> <span class="" style="white-space:pre"> </span>const Command *> >::iterator it = FailingCommands.begin(),</div>
<div> <span class="" style="white-space:pre"> </span>ie = FailingCommands.end(); it != ie; ++it) {</div><div> <span class="" style="white-space:pre"> </span> int CommandRes = it->first;</div><div> <span class="" style="white-space:pre"> </span> const Command *FailingCommand = it->second;</div>
<div> <span class="" style="white-space:pre"> </span> if (!Res)</div><div> <span class="" style="white-space:pre"> </span> Res = CommandRes;</div><div> </div><div> <span class="" style="white-space:pre"> </span> // If result status is < 0, then the driver command signalled an error.</div>
<div> <span class="" style="white-space:pre"> </span> // If result status is 70, then the driver command reported a fatal error.</div><div> <span class="" style="white-space:pre"> </span> // In these cases, generate additional diagnostic information if possible.</div>
<div> <span class="" style="white-space:pre"> </span> if (CommandRes < 0 || CommandRes == 70) {</div><div> <span class="" style="white-space:pre"> </span> TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommand);</div>
<div> <span class="" style="white-space:pre"> </span> break;</div><div> <span class="" style="white-space:pre"> </span> }</div><div> <span class="" style="white-space:pre"> </span> }</div><div> </div>
<div> return Res;</div><div> }</div><div><br></div><div>successfully built with command-line:</div><div><br></div><div> MBA-Anton:build asmirnov$ clang++ `llvm-config --cxxflags` `llvm-config --ldflags` `llvm-config --libs all` -lclang -lclangAST -lclangASTMatchers -lclangAnalysis -lclangApplyReplacements -lclangBasic -lclangCodeGen -lclangDriver -lclangDynamicASTMatchers -lclangEdit -lclangFormat -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex -lclangParse -lclangQuery -lclangRewriteCore -lclangRewriteFrontend -lclangSema -lclangSerialization -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangStaticAnalyzerFrontend -lclangTidy -lclangTidyLLVMModule -lclangTooling ../clang_ir.cpp -o clang_ir</div>
<div> MBA-Anton:build asmirnov$ </div><div><br></div><div>while running i'm getting assertion error:</div><div><br></div><div> MBA-Anton:build asmirnov$ ./clang_ir</div><div> Starting ----</div><div> making driver</div>
<div> making compilation</div><div> Assertion failed: (getClient() && "DiagnosticClient not set!"), function EmitCurrentDiagnostic, file /Users/asmirnov/Documents/dev/src/llvm_34/tools/clang/lib/Basic/Diagnostic.cpp, line 391.</div>
<div> Abort trap: 6</div><div> MBA-Anton:build asmirnov$ </div></div>