Re: [cfe-users] using Clang for runtime compilation of a class

2015-10-04 Thread Joel Andersson via cfe-users
Hi Georg (and others interested in Clang+JIT),

We just implemented this (Clang+JIT) in our open-source project CasADi (
http://casadi.org). Feel free to have a look and/or reuse components.

We used a plugin-design for using Clang, which is a bit different from the
examples we could find for using LLVM/Clang. We added a generic "Compiler"
class and made Clang/JIT a plugin for this Compiler class. Here is the
implementation of the plugin:
https://github.com/casadi/casadi/blob/develop/casadi/interfaces/clang/clang_compiler.cpp.
We also made a second plugin (only for Linux/Mac) which simply calls the
system compiler using shell commands. Mainly for benchmarking and when
Clang/JIT isn't available:
https://github.com/casadi/casadi/blob/develop/casadi/solvers/shell_compiler.cpp
.

The trickiest was to set up the build system (we use CMake) and in
particular redistribute the C and C++ standard libraries needed to compile
any useful C/C++ code. And do it in a way that makes the compiled code
portable and not relying on e.g. setting environmental variables.

This all works now, also on Windows, although it is not yet completely
mature (especially when trying to JIT compile C++ code). The solution used
in the LLVM examples was to use the arguments passed to "main", e.g. to
locate the "resource", but this didn't work for us since or tool is itself
a library and we don't have a "main". So the design we chose instead was to
place the compilation resources (including C/C++ standard library headers)
in a relative location to the plugin (which is a shared library) since the
path of the plugin can be retrieved at runtime by "dladdr" in Linux/Mac and
GetModuleHandleEx+GetModuleFileName on Windows.

Best regards,
Joel


2015-10-03 20:37 GMT+02:00 David Blaikie via cfe-users <
cfe-users@lists.llvm.org>:

>
>
> On Sat, Oct 3, 2015 at 12:30 AM, Georg Bartels via cfe-users <
> cfe-users@lists.llvm.org> wrote:
>
>> Dear Clang Users,
>>
>> I have a particular programming problem, and several discussion forums
>> suggested Clang as a possible solution. Being totally new to Clang and its
>> community, is this the right place to ask questions about how to use Clang
>> for a particular programming task? If not, where should I turn to?
>>
>> In case this is the right forum for such questions, I continue describing
>> my problem. Basically, I want to compile a class which inherits from a
>> virtual interface class at runtime of my program. The program should then
>> create an instance of this new class and use it through its interface.
>>
>> Let me quickly sketch an example. The static version of my program would
>> look like this:
>>
>> class Base
>> {
>>   public:
>> virtual int calc() const;
>> };
>>
>> class Derived: public Base
>> {
>>   public:
>> virtual int calc() const
>> {
>>   return 42;
>> }
>> };
>>
>> int main()
>> {
>> std::cout << Derived().calc() << std::endl;
>> return 0;
>> }
>>
>>
>> The dynamic version (with two fuzzy lines of code) shall look like this:
>>
>> class Base
>> {
>>   public:
>> virtual void calc() const;
>> };
>>
>> int main()
>> {
>>   std::string code = read_derived_code();
>>
>>   // two fuzzy lines for which I am looking for a solution
>>   SomeCompiledObjectClass o = compile(code);
>>   Base* d = SomeFactoryClass(o).createInstance();
>>
>>   std::cout << d->calc() << std::endl;
>>   return 0;
>> }
>>
>> Is Clang the right tool for solving my two fuzzy lines of code? If yes,
>> which class should have a look at? If not, do you maybe know about another
>> library/compile which can achieve this task?
>>
>
> This migth be the sort of thing that would escalate to cfe-dev and/or
> llvm-dev.
>
> What you're looking to do is JIT (Just In Time) compile some code. That
> involves using clang to turn the source code into LLVM IR and then LLVM to
> make machine code from that, put it in memory, make it executable, and call
> some part of it.
>
> Your two fuzzy lines of code would look slightly different (chances are
> what you'd do is generate source code that included a private function that
> return an instance, then you would JIT compile that function, indirectly
> code generating anything in the class that was needed, etc).
>
> I'm not sure where the best examples of the Clang half of this problem
> are. But for the second have, LLVM has the Kaleidoscope tutorials
> (especially the recently added ORC examples) that show how to use LLVM as a
> JIT compiler. You shuold be able to wrap up all these APIs into a fairly
> simple API for your one specific use case.
>
> I think /maybe/ out there is an existing C++ rapid development environment
> using Clang (essentially a C++ interpreter/interactive command prompt like
> Python) that you might be able to grab the basics of "feed code to
> compiler, get IR" from.
>
> - David
>
>
>>
>> Thanks a lot for your help!
>>
>> Cheers,
>> Georg.
>>
>> ___
>> cfe-users mailing list
>> cfe-users@list

[cfe-users] clang-format bracewrapping

2015-10-04 Thread Philipp Kraus via cfe-users

Hello,

I'm new with clang-format and I try under OSX 10.9 with clang-format 
version 3.8.0 (tags/google/testing/2015-07-31) to use the BraceWrapping 
definition on my .clang-format file. My current style definition the 
following structure but I cannot create the correct definition of the 
BraceWrapping item. How can I set this correctly?


Thanks

Phil



Language:  Cpp
BasedOnStyle:  LLVM
Standard:  Cpp11
TabWidth:  4
UseTab:Never

#BraceWrapping: custom
#AfterFunction:   true
#IndentBraces:true
#AfterClass:  true

NamespaceIndentation:  Inner
AccessModifierOffset:  -2
AlignAfterOpenBracket: true
AlignConsecutiveAssignments:   false
AlignEscapedNewlinesLeft:  false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine:  None
AllowShortIfStatementsOnASingleLine:   false
AllowShortLoopsOnASingleLine:  false
AlwaysBreakAfterDefinitionReturnType:  None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations:   false
BinPackArguments:  true
BinPackParameters: true
BreakBeforeBinaryOperators:None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators:   true
BreakConstructorInitializersBeforeComma:   false
ColumnLimit:   120
CommentPragmas:'^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine:false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth:   4
Cpp11BracedListStyle:  true
DerivePointerAlignment:false
DisableFormat: false
ExperimentalAutoDetectBinPacking:  false
ForEachMacros: [ foreach, 
Q_FOREACH, BOOST_FOREACH ]

IndentCaseLabels:  true
IndentWidth:   2
IndentWrappedFunctionNames:false
KeepEmptyLinesAtTheStartOfBlocks:  true
MacroBlockBegin:   ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep:   1
NamespaceIndentation:  None
ObjCBlockIndentWidth:  2
ObjCSpaceAfterProperty:false
ObjCSpaceBeforeProtocolList:   true
PenaltyBreakBeforeFirstCallParameter:  19
PenaltyBreakComment:   300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString:1000
PenaltyExcessCharacter:100
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment:  Left
SpaceAfterCStyleCast:  false
SpaceBeforeAssignmentOperators:true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses:   false
SpacesBeforeTrailingComments:  1
SpacesInAngles:true
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses:   true
SpacesInSquareBrackets:false___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users