Tool programming. Where can I get default platform configuration ?

Hello everyone,

I’m trying to develop a tool based on cmake and I have couple of problems. I’ll be really happy if anyone would help me. J

In our project we have a custom c++ reflection library and we are using it pretty much everywhere. Our developer uses a simple tool to generate type information from header files. It is a very simple tool which requires ugly syntax (I’m not fond of it) in source code. Therefore we have decided to replaced it with an source to source transformation tool based on cmake. We have developed the tool simply by using CompilerInstance and ASTConsumer which is pretty neat and simple. :smiley: It works perfectly in our test inputs.

The tool works perfectly and is going to improve our code quality massively. However the problem is when we have tried to use our new tool in our actual project code. It produces tremendous amounts of error output in system headers. (crtlib, ostream, string) Our main development platform is Windows and Visual Studio 2010. When I tried to compile the actual project code in clang.exe it compiled the files perfectly fine.

So I have looked out for a way to make our tool behave more like clang.exe;

· I have tried to set CompilerInstance options manually. I have compiled target code with clang.exe –v and tried to match the clangs.exe’s default options in the tool’s source code. I have achieved some progress but not fully get rid of the errors.

· I have tried tooling library. But I could not get it working.

· I have tried to reverse engineer frondend, driver and clang code. But I got stuck.

So, my question is how can I automatically configure my CompilerInstance for my platform ? (Include paths, compiler definitions, compiler options, preprocessor options, language options) Is there a way, function, class or mechanism that automatically configures CompilerInstance ?

Here is my actual code;

int main_compiler_instance(int argc, const char** argv)

{

CompilerInstance Compiler;

Compiler.createDiagnostics(0, NULL);

TargetOptions to;

to.Triple = llvm::sys::getDefaultTargetTriple();

TargetInfo *pti = TargetInfo::CreateTargetInfo(Compiler.getDiagnostics(), to);

Compiler.setTarget(pti);

Compiler.getPreprocessorOpts().UsePredefines = false;

Compiler.getLangOpts().CPlusPlus = 1;

Compiler.getLangOpts().Bool = 1;

Compiler.getLangOpts().MSCVersion = 1300;

Compiler.getLangOpts().MicrosoftExt = 1;

Compiler.getLangOpts().MicrosoftMode = 1;

Compiler.getLangOpts().CPlusPlus0x = 1;

Compiler.getLangOpts().ShortWChar = 1;

Compiler.getLangOpts().Exceptions = 1;

Compiler.getLangOpts().CXXExceptions = 1;

Compiler.getLangOpts().DelayedTemplateParsing = 1;

Compiler.getLangOpts().MathErrno = 1;

Compiler.getHeaderSearchOpts().ResourceDir = “c:/llvm/bin\…\lib\clang\3.2”;

Compiler.getHeaderSearchOpts().AddPath(“c:/llvm/bin/…/lib/clang/3.2/include”, frontend::System, false, false, true, true);

Compiler.getHeaderSearchOpts().AddPath(“C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include”, frontend::System, false, false, true, true);

Compiler.getHeaderSearchOpts().AddPath(“C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include”, frontend::System, false, false, true, true);

Compiler.getHeaderSearchOpts().AddPath(“C:\Users\Orcun\Desktop\ZEPrototypes\Source”, frontend::Quoted, false, false, true);

Compiler.getHeaderSearchOpts().AddPath(“C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation”, frontend::Quoted, false, false, true);

Compiler.getHeaderSearchOpts().AddPath(“C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZECodeGenerator”, frontend::Quoted, false, false, true);

Compiler.getPreprocessorOpts().addMacroDef(“_WIN32”);

Compiler.getPreprocessorOpts().addMacroDef(“_WINDOWS”);

Compiler.getPreprocessorOpts().addMacroDef(“_WCHAR_T_DEFINED”);

Compiler.createFileManager();

Compiler.createSourceManager(Compiler.getFileManager());

Compiler.createPreprocessor();

Compiler.setASTConsumer(new ZECodeGeneratorASTConsumer());

Compiler.createASTContext();

const FileEntry *pFile = Compiler.getFileManager().getFile(argv[1]);

Compiler.getSourceManager().createMainFileID(pFile);

Compiler.getDiagnosticClient().BeginSourceFile(Compiler.getLangOpts(), &Compiler.getPreprocessor());

ParseAST(Compiler.getPreprocessor(), &Compiler.getASTConsumer(), Compiler.getASTContext(), false, clang::TU_Complete, NULL, true);

Compiler.getDiagnosticClient().EndSourceFile();

return 1;

}

#include “clang/Tooling/Tooling.h”

#include “llvm/Support/CommandLine.h”

#include “clang/Tooling/CompilationDatabase.h”

using namespace clang::tooling;

using namespace llvm;

int main_tooling(int argc, const char **argv)

{

FixedCompilationDatabase* Database = FixedCompilationDatabase::loadFromCommandLine(argc, argv); //Actually I’m not pretty sure what it does.

ClangTool Tool(*Database, Database->getAllFiles());

return Tool.run(newFrontendActionFactory());

}

int main(int argc, const char** argv)

{

/* Select one of them */

//main_compiler_instance(argc, argv);

//main_tooling(argc, argv);

}

And here is the command line options that I use: ZEMeta.cpp -I …\ZEFoundation -I …

“main_tooling” function does not work because “FixedCompilationDatabase::loadFromCommandLine(argc, argv);” function return NULL.

I have appended output log of “main_compiler_instance” function at the end of this email. Output log is very big (256+ KB) so I have removed some of the errors. When compiling the ZEMeta.cpp with clang.exe It does not produces these errors.

Thanks to anyone has read this long email and have a nice day,

Orcun.

Note: My native language is not English. Therefore, I’m sorry if I could not express myself clearly or correctly.

— Output Log —

In file included from ZEMeta.h:2:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEDS/ZEArray.h:66:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEError.h:41:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZELog.h:5:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZEDS/ZEString.h:40:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\istream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ios:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocnum:10:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\streambuf:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xiosbase:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocale:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdexcept:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception:38:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\eh.h:58:53: error: unknown type name ‘type_info’

_CRTIMP int __cdecl _is_exception_typeof(In const type_info &_Type, In struct _EXCEPTION_POINTERS * _ExceptionPtr);

In file included from ZEMeta.h:2:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEDS/ZEArray.h:66:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEError.h:41:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZELog.h:5:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZEDS/ZEString.h:40:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\istream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ios:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocnum:10:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\streambuf:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xiosbase:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocale:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdexcept:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception:39:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\malloc.h:187:40: error: use of undeclared identifier ‘_ALLOCA_S_MARKER_SIZE’

_STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE);

^

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\malloc.h:43:63: note: expanded from macro ‘_STATIC_ASSERT’

#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]

In file included from ZEMeta.h:2:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEDS/ZEArray.h:66:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEError.h:41:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZELog.h:5:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZEDS/ZEString.h:40:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\istream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ios:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocnum:10:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\streambuf:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xiosbase:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocale:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdexcept:7:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility:9:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\type_traits:6:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\limits:182:22: error: use of undeclared identifier ‘FLT_RADIX

_STCONS(int, radix, FLT_RADIX);

In file included from ZEMeta.h:2:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEDS/ZEArray.h:66:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation\ZEError.h:41:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZELog.h:5:

In file included from C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZEDS/ZEString.h:40:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\istream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ios:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocnum:10:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\streambuf:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xiosbase:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xlocale:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdexcept:7:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring:6:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility:8:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility:9:

In file included from C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\type_traits:6:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\limits:27:61: note: expanded from macro ‘_STCONS’

#define _STCONS(ty, name, val) static const ty name = (ty)(val)

^

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\limits:240:23: error: use of undeclared identifier ‘CHAR_BIT

_STCONS(int, digits, CHAR_BIT - (CHAR_MIN != 0 ? 1 : 0));

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\limits:1467:15: error: default initialization of an object of const type ‘const int’

_STCONS(int, digits, CHAR_BIT);

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\limits:1490:15: error: no member named ‘digits10’ in ‘std::numeric_limits’

_STCONS(int, digits10, (CHAR_BIT * sizeof (unsigned short) - 1)

C:\Users\Orcun\Desktop\ZEPrototypes\Source\ZEFoundation/ZEDS/ZEString.h:233:74: error: expected expression

static ZEString FromUInt64(ZEUInt64 Value, const char* Format = NULL);

Hi. I’m also using Clang for a similiar tool (generate reflection data for C++), check out the attached source file ‘ParserInit.cpp’ to see how I initialize it.

You’re also gonna need the attached patch applied to Clang, it exposes the Windows SDK headers locations.

0001-Expose-function-to-get-Windows-system-include-paths.patch (4.63 KB)

ParserInit.cpp (1.98 KB)

There is CompilerInvocation::CreateFromArgs and
CompilerInvocation::setLangDefaults.

However, note that this is generally not a "supported" use case. You
can spend months trying to make this work, but at most you will get
something brittle that barely works (I speak directly from
experience). If put to it, I could probably make most of the things
work with the knowledge I have now, but there are some pretty
fundamental things that would need some hacking inside clang. For
example, the builtin header includes (e.g. clang's stddef.h) that are
found is a way that assumes that the program is being invoked from
within a certain filesystem layout, so that you would have to have the
tool sitting in a special directory in order to do it the "correct"
way.

The way to get around this and have all of the compiler configuration
stuff Just Work is to use a plugin
<http://clang.llvm.org/docs/ClangPlugins.html&gt;\. If you are already
building the project with Clang and all you need is CompilerInstance
and an ASTConsumer, then a plugin should suit your use case perfectly.

--Sean Silva

Hello everyone,

I’m trying to develop a tool based on cmake and I have couple of problems.
I’ll be really happy if anyone would help me. J

In our project we have a custom c++ reflection library and we are using it
pretty much everywhere. Our developer uses a simple tool to generate type
information from header files. It is a very simple tool which requires ugly
syntax (I’m not fond of it) in source code. Therefore we have decided to
replaced it with an source to source transformation tool based on cmake. We
have developed the tool simply by using CompilerInstance and ASTConsumer
which is pretty neat and simple. :smiley: It works perfectly in our test inputs.

The tool works perfectly and is going to improve our code quality massively.
However the problem is when we have tried to use our new tool in our actual
project code. It produces tremendous amounts of error output in system
headers. (crtlib, ostream, string) Our main development platform is Windows
and Visual Studio 2010. When I tried to compile the actual project code in
clang.exe it compiled the files perfectly fine.

So I have looked out for a way to make our tool behave more like clang.exe;

· I have tried to set CompilerInstance options manually. I have
compiled target code with clang.exe –v and tried to match the clangs.exe’s
default options in the tool’s source code. I have achieved some progress but
not fully get rid of the errors.

· I have tried tooling library. But I could not get it working.

· I have tried to reverse engineer frondend, driver and clang code.
But I got stuck.

So, my question is how can I automatically configure my CompilerInstance for
my platform ? (Include paths, compiler definitions, compiler options,
preprocessor options, language options) Is there a way, function, class or
mechanism that automatically configures CompilerInstance ?

Here is my actual code;

int main_compiler_instance(int argc, const char** argv)

{

       CompilerInstance Compiler;

       Compiler.createDiagnostics(0, NULL);

       TargetOptions to;

       to.Triple = llvm::sys::getDefaultTargetTriple();

       TargetInfo *pti =
TargetInfo::CreateTargetInfo(Compiler.getDiagnostics(), to);

       Compiler.setTarget(pti);

       Compiler.getPreprocessorOpts().UsePredefines = false;

       Compiler.getLangOpts().CPlusPlus = 1;

       Compiler.getLangOpts().Bool = 1;

       Compiler.getLangOpts().MSCVersion = 1300;

       Compiler.getLangOpts().MicrosoftExt = 1;

       Compiler.getLangOpts().MicrosoftMode = 1;

       Compiler.getLangOpts().CPlusPlus0x = 1;

       Compiler.getLangOpts().ShortWChar = 1;

       Compiler.getLangOpts().Exceptions = 1;

       Compiler.getLangOpts().CXXExceptions = 1;

       Compiler.getLangOpts().DelayedTemplateParsing = 1;

       Compiler.getLangOpts().MathErrno = 1;

       Compiler.getHeaderSearchOpts().ResourceDir =
"c:/llvm/bin\\..\\lib\\clang\\3.2";

Compiler.getHeaderSearchOpts().AddPath("c:/llvm/bin/../lib/clang/3.2/include",
frontend::System, false, false, true, true);

       Compiler.getHeaderSearchOpts().AddPath("C:\\Program Files
(x86)\\Microsoft SDKs\Windows\v7.0A\Include", frontend::System, false,
false, true, true);

       Compiler.getHeaderSearchOpts().AddPath("C:\\Program Files
(x86)\\Microsoft Visual Studio 10.0\\VC\\include", frontend::System, false,
false, true, true);

Compiler.getHeaderSearchOpts().AddPath("C:\\Users\\Orcun\\Desktop\\ZEPrototypes\\Source",
frontend::Quoted, false, false, true);

Compiler.getHeaderSearchOpts().AddPath("C:\\Users\\Orcun\\Desktop\\ZEPrototypes\\Source\\ZEFoundation",
frontend::Quoted, false, false, true);

Compiler.getHeaderSearchOpts().AddPath("C:\\Users\\Orcun\\Desktop\\ZEPrototypes\\Source\\ZECodeGenerator",
frontend::Quoted, false, false, true);

       Compiler.getPreprocessorOpts().addMacroDef("_WIN32");

       Compiler.getPreprocessorOpts().addMacroDef("_WINDOWS");

       Compiler.getPreprocessorOpts().addMacroDef("_WCHAR_T_DEFINED");

       Compiler.createFileManager();

       Compiler.createSourceManager(Compiler.getFileManager());

       Compiler.createPreprocessor();

       Compiler.setASTConsumer(new ZECodeGeneratorASTConsumer());

       Compiler.createASTContext();

       const FileEntry *pFile = Compiler.getFileManager().getFile(argv[1]);

       Compiler.getSourceManager().createMainFileID(pFile);

Compiler.getDiagnosticClient().BeginSourceFile(Compiler.getLangOpts(),
&Compiler.getPreprocessor());

       ParseAST(Compiler.getPreprocessor(), &Compiler.getASTConsumer(),
Compiler.getASTContext(), false, clang::TU_Complete, NULL, true);

       Compiler.getDiagnosticClient().EndSourceFile();

       return 1;

}

#include "clang/Tooling/Tooling.h"

#include "llvm/Support/CommandLine.h"

#include "clang/Tooling/CompilationDatabase.h"

using namespace clang::tooling;

using namespace llvm;

int main_tooling(int argc, const char **argv)

{

       FixedCompilationDatabase* Database =
FixedCompilationDatabase::loadFromCommandLine(argc, argv); //Actually I’m
not pretty sure what it does.

       ClangTool Tool(*Database, Database->getAllFiles());

       return
Tool.run(newFrontendActionFactory<ZECodeGeneratorFrontendAction>());

}

int main(int argc, const char** argv)

{

       /* Select one of them */

       //main_compiler_instance(argc, argv);

       //main_tooling(argc, argv);

}

And here is the command line options that I use: ZEMeta.cpp -I
..\ZEFoundation -I ..

You need to put '--' in between the ZEMeta.cpp and the command line
arguments. This is so that you can still add other command line
options you'd want for your tool.

Cheers,
/Manuel

Hi João,

Thank you so much for your reply and patch.

I have applied your patch and modified my driver according to your code. It really automated things like getting system paths but I still got errors in system headers (Visual Studio’s c library).

I think problem is clang can not parse visual studio c library headers becouse it could not found Microsoft specific type_info struct or CHAR_BIT.

I just can figure out the difference between a clang driver with my own code. They are doing the same thing. They are using compiler invocation so I do it too. I’m really stuck.

Thank you,

Orçun

The “type_info” problem is currently hacked around in InitializePredefinedMacros():InitPreprocessor.cpp, which should be called by CompilerInstance::createPreprocessor().

Also what version of the Visual C++ headers are you using? 2012 are not compatible yet, if you need it I can send you some patches.

Hello again João,

I’m using Visual Studio 2010 and I have managed to clear some of the produced errors.

But right now I’m struggling with three errors;

· C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(49,15) : error: function declared ‘cdecl’ here was previously declared without calling convention [Semantic Issue]

· C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\xstring(2067,21) : error: use of undeclared identifier ‘char16_t’ [Semantic Issue]

· C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\xlocale(1235,29) : error: use of undeclared identifier ‘char32_t’ [Semantic Issue]

In addition to these, after changing some parameters diagnostics started acting like crazy. I didn’t do anything about the diagnostics. Here is some diagnostic outputs;

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

In included file:

error: function declared ‘cdecl’ here was previously declared without calling convention

note: previous declaration is here

error: function declared ‘cdecl’ here was previously declared without calling convention

note: previous declaration is here

error: function declared ‘cdecl’ here was previously declared without calling convention

note: previous declaration is here

error: function declared ‘cdecl’ here was previously declared without calling convention

note: previous declaration is here

I think I’m getting closer :smiley:

Thank you and have a nice day,

Orçun

Here is the code so far;

int main_compiler_instance(int argc, const char** argv)

{

// Default Windows Platform Arguments

// These arguments has been taken from cmake -v

const char* Arguments =

{

“-cc1”,

“-I”, “.”,

“-I”, “…”,

“-I”, “…/ZEFoundation”,

“-v”,

“-fmath-errno”,

“-ferror-limit”, “19”,

“-fmessage-length”, “150”,

“-fms-extensions”,

“-fms-compatibility”,

“-fmsc-version=1300”,

“-fdelayed-template-parsing”,

“-fno-rtti”,

“-fcolor-diagnostics”,

“-x”, “c++”, // NOT WORKING !!! It is working in clang.exe command line but not here. I don’t know why…

“-std=c++0x”,

“-fsyntax-only”

};

size_t ArgumentCount = sizeof(Arguments) / sizeof(const char*);

CompilerInstance Compiler;

Compiler.createDiagnostics(0, NULL);

// Target Options

TargetOptions TO;

TO.Triple = llvm::sys::getDefaultTargetTriple();

TargetInfo* TI = TargetInfo::CreateTargetInfo(Compiler.getDiagnostics(), TO);

TI->setCXXABI(TargetCXXABI::CXXABI_Microsoft);

Compiler.setTarget(TI);

CompilerInvocation Invocation;

bool Result = CompilerInvocation::CreateFromArgs(Invocation, Arguments, Arguments + ArgumentCount, Compiler.getDiagnostics());

// -x c++ not working so; (I’m not sure whether it Works or not)

CompilerInvocation::setLangDefaults(*Invocation.getLangOpts(), InputKind::IK_CXX, LangStandard::Kind::lang_cxx11);

// Resource Directory

if (Invocation.getHeaderSearchOpts().UseBuiltinIncludes && Invocation.getHeaderSearchOpts().ResourceDir.empty())

{

// I’m going to distribute clang runtime include directory in tools binary directory.

// Like if the path of tools binary is c:\tool\codegen.exe then include files will be in c:\tool\include

llvm::sys::Path BinaryDir = llvm::sys::Path::GetMainExecutable(argv[0], main_compiler_instance);

BinaryDir.eraseComponent();

Invocation.getHeaderSearchOpts().ResourceDir = BinaryDir.str();

}

// Include Path

// Thanks for the patch João Matos.

std::vectorstd::string SystemDirs = clang::driver::GetWindowsSystemIncludeDirs();

for(size_t i = 0; i < SystemDirs.size(); ++i)

Invocation.getHeaderSearchOpts().AddPath(SystemDirs[i], frontend::System, false, false, true, true);

Invocation.getHeaderSearchOpts().AddPath(Invocation.getHeaderSearchOpts().ResourceDir + “/include”, clang::frontend::System, false, false, true, true);

// Compiler Definitions

//Invocation.getPreprocessorOpts().addMacroDef(“_WCHAR_T_DEFINED”);

Compiler.setInvocation(&Invocation);

Compiler.createFileManager();

Compiler.createSourceManager(Compiler.getFileManager());

Compiler.createPreprocessor();

Compiler.setASTConsumer(new ZECodeGeneratorASTConsumer());

Compiler.createASTContext();

const FileEntry *pFile = Compiler.getFileManager().getFile(argv[1]);

Compiler.getSourceManager().createMainFileID(pFile);

Compiler.getDiagnosticClient().BeginSourceFile(Compiler.getLangOpts(), &Compiler.getPreprocessor());

ParseAST(Compiler.getPreprocessor(), &Compiler.getASTConsumer(), Compiler.getASTContext(), false, clang::TU_Complete, NULL, true);

Compiler.getDiagnosticClient().EndSourceFile();

return 1;

}