<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{mso-style-priority:99;
mso-style-link:"Plain Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.5pt;
font-family:Consolas;}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.PlainTextChar
{mso-style-name:"Plain Text Char";
mso-style-priority:99;
mso-style-link:"Plain Text";
font-family:Consolas;}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:"Courier New";}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>Hi,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>I'm not sure if this is a libclang, llvm::cl or clang-interpreter issue so I'll try posting here for a response.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>I am using libclang as a frontend to the LLVM JIT (3.3 release). I started from the clang-interpreter example and have everything working (given a C/C++ source file I can have it JIT'd to memory and executed) for a single run. When I try to compile a second source file I get an infinite loop reporting the following:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>rdsuvideodrome:~/ciEx> ./ciEx.out<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>Result = 55<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'verify-loop-info' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'verify-scev' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'scalar-evolution-max-iterations' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'struct-path-tbaa' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'enable-tbaa' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'enable-bc-uselist-preserve' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><premain>: CommandLine Error: Argument 'agg-antidep-debugmod' defined more than once!<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>--------------- end snip, but this continues and continues! -------------------<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>The "Result = 55" is the correct return value from compiling the first source file (i.e fib(10)). Processing the second causes the repeating <premain> complaints. I can execute the JIT'd code multiple times, just can't compile a second file.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>I went back to the original clang-interpreter example and found that the issue exists there as well (modifying the example to run twice on different input). </span><span style='font-family:"Times New Roman","serif"'> I've included my slightly modified version of the example code below (all you need to do is create two test files "test1.c" and "test2.c" in the same dir --- I've included my simple test files also below).<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>As I mentioned I'm not sure if this is:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> * a libclang issue (can libclang be called into multiple times as this example does?)<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> * a limitation of the simple clang-interpreter example<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> * or is state being held in llvm:cl across compilation requests? <o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>From looking at the code and debugging, it appears that the call to Clang.ExecuteAction(*Act) the second time around causes llvm::cl::RegisteredOptionList (from lib/Support/CommandLine.cpp) </span><span style='font-family:"Times New Roman","serif"'>to contain a huge (unbounded?) number of nodes. This becomes an issue when the list is processed in GetOptionInfo().<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>Any ideas?<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>Thanks for your help,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>Jay<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//=============================================================================<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>/******* test1.c *********/<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int fib( int x )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>{<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if ( x == 0 )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 0;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if ( x == 1 )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return ( fib( x-1 ) + fib( x-2) );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int main( int argc, char** argv )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>{<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return fib( 10 );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//=============================================================================<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>/******* test2.c *********/<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int fib2( int x )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>{<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if ( x == 0 )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 0;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if ( x == 1 )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return ( fib2( x-1 ) + fib2( x-2) );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int main( int argc, char** argv )<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>{<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return fib2( 12 );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//=============================================================================<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>/****** Modified clang-interpreter example. *********/<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//===-- examples/clang-interpreter/main.cpp - Clang C Interpreter Example -===//<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// The LLVM Compiler Infrastructure<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// This file is distributed under the University of Illinois Open Source<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// License. See LICENSE.TXT for details.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//===----------------------------------------------------------------------===//<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/CodeGen/CodeGenAction.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Basic/DiagnosticOptions.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Driver/Compilation.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Driver/Driver.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Driver/Tool.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Frontend/CompilerInstance.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Frontend/CompilerInvocation.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Frontend/FrontendDiagnostic.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "clang/Frontend/TextDiagnosticPrinter.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/ADT/OwningPtr.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/ADT/SmallString.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/ExecutionEngine/ExecutionEngine.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/ExecutionEngine/JIT.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/IR/Module.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/Support/Host.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/Support/ManagedStatic.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/Support/Path.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/Support/TargetSelect.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>#include "llvm/Support/raw_ostream.h"<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>using namespace clang;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>using namespace clang::driver;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// This function isn't referenced outside its translation unit, but it<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// can't use the "static" keyword because its address is used for<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// GetMainExecutable (since some platforms don't support taking the<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// address of main, and some platforms can't implement GetMainExecutable<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>// without being given the address of a function in the main executable).<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>llvm::sys::Path GetExecutablePath(const char *Argv0) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // This just needs to be some symbol in the binary; C++ doesn't<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // allow taking the address of ::main however.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> void *MainAddr = (void*) (intptr_t) GetExecutablePath;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>static int Execute(llvm::Module *Mod, char * const *envp) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::InitializeNativeTarget();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> std::string Error;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> OwningPtr<llvm::ExecutionEngine> EE(<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::ExecutionEngine::createJIT(Mod, &Error));<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (!EE) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::errs() << "unable to make execution engine: " << Error << "\n";<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 255;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::Function *EntryFn = Mod->getFunction("main");<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (!EntryFn) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::errs() << "'main' function not found in module.\n";<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 255;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // FIXME: Support passing arguments.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> std::vector<std::string> Args;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Args.push_back(Mod->getModuleIdentifier());<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return EE->runFunctionAsMain(EntryFn, Args, envp);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//int main(int argc, const char **argv, char * const *envp) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int interpretFile (int argc, const char *argv, char * const *envp) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> void *MainAddr = (void*) (intptr_t) GetExecutablePath;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // llvm::sys::Path Path = GetExecutablePath(argv[0]);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::sys::Path Path = GetExecutablePath(argv);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> TextDiagnosticPrinter *DiagClient =<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Driver TheDriver(Path.str(), llvm::sys::getProcessTriple(), "a.out", Diags);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> TheDriver.setTitle("clang interpreter");<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // FIXME: This is a hack to try to force the driver to do something we can<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // recognize. We need to extend the driver library to support this use model<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // (basically, exactly one input, and the operation mode is hard wired).<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // SmallVector<const char *, 16> Args(argv, argv + argc);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> SmallVector<const char *, 16> Args(2, argv);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Args.push_back("-fsyntax-only");<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> OwningPtr<Compilation> C(TheDriver.BuildCompilation(Args));<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (!C)<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 0;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // FIXME: This is copied from ASTUnit.cpp; simplify and eliminate.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // We expect to get back exactly one command job, if we didn't something<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // failed. Extract that job from the compilation.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> const driver::JobList &Jobs = C->getJobs();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> SmallString<256> Msg;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::raw_svector_ostream OS(Msg);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> C->PrintJob(OS, C->getJobs(), "; ", true);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Diags.Report(diag::err_fe_expected_clang_command);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Initialize a compiler invocation object from the clang (-cc1) arguments.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> const driver::ArgStringList &CCArgs = Cmd->getArguments();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> OwningPtr<CompilerInvocation> CI(new CompilerInvocation);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> CompilerInvocation::CreateFromArgs(*CI,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> const_cast<const char **>(CCArgs.data()),<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> const_cast<const char **>(CCArgs.data()) +<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> CCArgs.size(),<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Diags);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Show the invocation, with -v.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (CI->getHeaderSearchOpts().Verbose) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::errs() << "clang invocation:\n";<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::errs() << "\n";<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // FIXME: This is copied from cc1_main.cpp; simplify and eliminate.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Create a compiler instance to handle the actual work.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> CompilerInstance Clang;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Clang.setInvocation(CI.take());<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Create the compilers actual diagnostics engine.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Clang.createDiagnostics();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (!Clang.hasDiagnostics())<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Infer the builtin include path if unspecified.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (Clang.getHeaderSearchOpts().UseBuiltinIncludes &&<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Clang.getHeaderSearchOpts().ResourceDir.empty())<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Clang.getHeaderSearchOpts().ResourceDir =<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> CompilerInvocation::GetResourcesPath(argv, MainAddr);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // CompilerInvocation::GetResourcesPath(argv[0], MainAddr);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Create and execute the frontend to generate an LLVM bitcode module.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> OwningPtr<CodeGenAction> Act(new EmitLLVMOnlyAction());<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (!Clang.ExecuteAction(*Act))<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return 1;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> int Res = 255;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> if (llvm::Module *Module = Act->takeModule())<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> Res = Execute(Module, envp);<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::errs() << "Result = " << Res << "\n"; //Not an error, but just report Res to see what happened.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> // Shutdown.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> llvm::llvm_shutdown();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> return Res;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>//int main(int argc, const char **argv, char * const *envp) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>int main(int argc, const char **argv) {<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> char* const* envp;<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> interpretFile( 1, "test1.c", envp );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'> interpretFile( 1, "test2.c", envp );<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Times New Roman","serif"'><o:p> </o:p></span></p></div></body></html>