<div dir="ltr"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div bgcolor="#FFFFFF"><p>I just got back around to trying the example program. I had to
      make a few changes to get the code to compile.<br></p></div></blockquote><div>You are right, after I have sent you the code I realized it was not correct. The CommandLine you pass to Driver::BuildCompilation must be a ArrayRef not a string. Sorry for the confusion. </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div bgcolor="#FFFFFF"><p>When I try the
      program it says it is unable to find clang++ which is definitely
      on my system. Do I need to hard code the path to clang++?<br></p></div></blockquote><div>Yes, the path for clang/llvm tools must be absolute paths. Thats why I find it easier to re-use the command line from the compilation database. In order to generate the compile_commands.json (a.k.a. compilation database), you just need to pass -DCMAKE_EXPORT_COMPILE_COMMANDS to cmake while configuring the build of your project. After that, you can retrieve the compile command for a specific file using the <a href="https://clang.llvm.org/doxygen/classclang_1_1tooling_1_1CompilationDatabase.html">tooling::CompilationDatabase::getCompileCommmands()</a> method.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div bgcolor="#FFFFFF"><p>
    </p>
    <div class="gmail-m_5225735098880413098moz-cite-prefix">On 11/11/18 2:33 PM, João Paulo
      Labegalini de Carvalho wrote:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div dir="ltr">
                  <div dir="ltr">
                    <div dir="ltr">
                      <div dir="ltr">
                        <div dir="ltr">
                          <div dir="ltr">
                            <div dir="ltr">
                              <div dir="ltr">Michael,
                                <div><br>
                                </div>
                                <div>The easiest way to use Clang API to
                                  perform "standard" compiler tasks is
                                  through the <a href="https://clang.llvm.org/doxygen/classclang_1_1driver_1_1Driver.html" target="_blank">Driver class</a>.</div>
                                <div>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.</div>
                                <div><br>
                                </div>
                                <div>For instance:</div>
                                <div><font face="monospace, monospace"><br>
                                  </font></div>
                                <div><font face="monospace, monospace">std::string
                                    CommandLine = "clang++ -Wall -O3
                                    -emit-llvm -o test.bc test.cpp";</font></div>
                                <div><font face="monospace, monospace">clang::DiagnosticOptions*
                                    DiagOptions = new
                                    DiagnosticOptions();<br>
                                  </font></div>
                                <div><font face="monospace, monospace">clang::DiagnosticConsumer
                                    *DiagClient = new
                                    TextDiagnosticPrinter(llvm::errs(),
                                    DiagOptions);<br>
                                  </font></div>
                                <div><font face="monospace, monospace">llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>
                                    DiagID(new clang::DiagnosticIDs());<br>
                                  </font></div>
                                <div><font face="monospace, monospace">clang::DiagnosticsEngine
                                    Diags(DiagID, DiagOptions,
                                    DiagClient);<br>
                                  </font></div>
                                <div><font face="monospace, monospace">clang::driver::Driver
                                    TheDriver("clang++",
                                    llvm::sys::getDefaultTargetTriple(),
                                    Diags);</font></div>
                                <div><font face="monospace, monospace">const
std::unique_ptr<driver::Compilation>
                                    compilation(TheDriver.BuildCompilation(CommandLine));<br>
                                  </font></div>
                                <div><font face="monospace, monospace">if
                                    (!compilation) { llvm::errs()
                                    << "failed to build
                                    compilation object!\n"; }<br>
                                  </font></div>
                                <div><font face="monospace, monospace">llvm::SmallVector<std::pair<int,
                                    const driver::Command*>,10>
                                    failingCommands;<br>
                                  </font></div>
                                <div><font face="monospace, monospace">int
                                    res =
                                    TheDriver.ExecuteCompilation(*compilation,
                                    failingCommands);<br>
                                  </font></div>
                                <div>
                                  <div><font face="monospace, monospace">if
                                      (res < 0) {</font></div>
                                  <div><font face="monospace, monospace"> 
                                      ProcessingFailed = true;</font></div>
                                  <div><font face="monospace, monospace"> 
                                      for (const std::pair<int, const
                                      driver::Command*>& p :
                                      failingCommands) {</font></div>
                                  <div><font face="monospace, monospace"> 
                                        if (p.first) {</font></div>
                                  <div><font face="monospace, monospace"> 
                                         
                                      TheDriver.generateCompilationDiagnostics(*compilation,
                                      *p.second);</font></div>
                                  <div><font face="monospace, monospace"> 
                                        }</font></div>
                                  <div><font face="monospace, monospace"> 
                                      }</font></div>
                                  <div><font face="monospace, monospace">}</font></div>
                                </div>
                                <div><br>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr">On Sun, Nov 11, 2018 at 6:53 PM Michael Collison
          via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I have a
          requirement to parse a .cpp file to a .ast file and a .bc
          file. <br>
          I have the parsing, with filtering of the AST working using
          the the <br>
          tooling tempalte on this page: <br>
          <a href="https://clang.llvm.org/docs/RAVFrontendAction.html" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/RAVFrontendAction.html</a>.
          I also need to <br>
          generate LLVM code to write out to a .bc file. I tried using :<br>
          <br>
          tool.run
          (newFrontEndActionFactory<EmitBCAction>().get())<br>
          <br>
          where tool is an instance of ClangTool but received an error
          saying "not <br>
          enough position command line arguments specified". Any ideas?
          Are there <br>
          any examples I can look at?<br>
          <br>
          Regards,<br>
          <br>
          Michael Collison<br>
          <br>
          <br>
          _______________________________________________<br>
          cfe-dev mailing list<br>
          <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
          <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
        </blockquote>
      </div>
      <br clear="all">
      <div><br>
      </div>
      -- <br>
      <div dir="ltr" class="gmail-m_5225735098880413098gmail_signature">
        <div dir="ltr">
          <div>
            <div dir="ltr">
              <div>João Paulo L. de Carvalho<br>
                Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div>
              <div><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div>
              <div><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">joao.carvalho@ic.unicamp.br</a><br>
                <a href="mailto:j160924@dac.unicamp.br" target="_blank">j160924@dac.unicamp.br</a></div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
  </div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>João Paulo L. de Carvalho<br>Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div><div><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">joao.carvalho@ic.unicamp.br</a><br><a href="mailto:j160924@dac.unicamp.br" target="_blank">j160924@dac.unicamp.br</a></div></div></div></div></div></div></div>