tooling helper

Hi,

wrote a small program which might help to use the tools, which are using the tooling library. My understanding was that these tools need some compilation database. And one format is the JSON compilation database, which comes for free in a cmake project. Other projects could have difficulties to generate such file. And that’s where a tool could help.

https://github.com/rizsotto/Bear

It runs the build command and intercept the ‘exec’ calls, filters the compiler invocations and generate the output file. (It is using LD_PRELOAD from GNU Libc, so it is not really portable.)

I know these kind of tools are out of scope here. But wanted to mention, because it could simplify to solve some cases. At least it did to me. :wink:

Regards,
Laszlo

This is very interesting and useful. Even more, something like this
could help Clang static analyzer to work with build systems that don't
respect CC and CXX environment variables.

Dmitri

I don't know what the current state of this is, but waaaay back when
the tooling stuff was going in directly, there was a flag to Clang
itself that would cause Clang to write out the compilation database
just so that it could be used with any build system which could use
the Clang compiler. The hope was that this would serve the needs of
the static analyzer and other tools in interoperating with arbitrary
build systems. Does that not work for some folks?

(We are aware that not everyone is using Clang as their compiler, but
honestly using Clang for tools but not for the compilation isn't going
to work terribly well given that Clang can't transform code it can't
parse....)

-Chandler

It runs the build command and intercept the 'exec' calls, filters the
compiler invocations and generate the output file. (It is using LD_PRELOAD
from GNU Libc, so it is not really portable.)

FYI, this can be done ridiculously easily (it's basically a one-liner)
on platforms that support DTrace, which should be Mac and FreeBSD (and
of course Solaris and descendants, although there hasn't been a big
clang investment there). Let me know if you would like to integrate
this into the project and I can give you more information.

-- Sean Silva

There's already a premade script for this, actually. It's
DTTk/execsnoop in
<http://opensource.apple.com/tarballs/dtrace/dtrace-96.tar.gz>. It's a
bit more sophisticated than the one-liner I was thinking of but it
should work "out of the box" for your use case. The "meat" of it
though is basically

dtrace -n 'syscall::execve:return'

which should basically just indicate when processes exec. The script
just adds the usual DTrace contortions to dig out the relevant info. I
think you could also possibly do this with the proc provider on
proc:::create, but I don't currently have a machine with DTrace to
mess around on.

>> wrote a small program which might help to use the tools, which are
using the
>> tooling library. My understanding was that these tools need some
compilation
>> database. And one format is the JSON compilation database, which comes
for
>> free in a cmake project. Other projects could have difficulties to
generate
>> such file. And that's where a tool could help.
>>
>> https://github.com/rizsotto/Bear
>>
>> It runs the build command and intercept the 'exec' calls, filters the
>> compiler invocations and generate the output file. (It is using
LD_PRELOAD
>> from GNU Libc, so it is not really portable.)
>>
>> I know these kind of tools are out of scope here. But wanted to mention,
>> because it could simplify to solve some cases. At least it did to me. :wink:
>
> This is very interesting and useful. Even more, something like this
> could help Clang static analyzer to work with build systems that don't
> respect CC and CXX environment variables.

I don't know what the current state of this is, but waaaay back when
the tooling stuff was going in directly, there was a flag to Clang
itself that would cause Clang to write out the compilation database

You forgot the word "plan for" somewhere in that sentence? :slight_smile:

Hi,

I need some help to clarify the JSON compilation database syntax… This document <http://clang.llvm.org/docs/JSONCompilationDatabase.html> gives an example with space separated definition. I tried to reproduce the desired output with cmake, but failed.

add_definitions(-DQUOTED_DEF=“With spaces and quotes.”)
add_definitions(-DESCAPED_DEF=With\ spaces\ and\ without\ quotes.)

These extra lines causes this command on the terminal

gcc -DQUOTED_DEF="“With spaces and quotes.”" -DESCAPED_DEF=“With spaces and without quotes.” -o …

And the compile_commands.json file has entry like this

“command”: “/usr/lib64/ccache/gcc -DQUOTED_DEF=”\“With spaces and quotes.\”" -DESCAPED_DEF=“With spaces and without quotes.” -o …"

(The cmake version is 2.8.9) My understanding is that cmake does produce valid JSON here. Only the document was wanted more elegant escaping by single quotes. (-DSOMEDEF=’“With spaces and quotes.”’) Is that correct?

Regards,
Laszlo

Hi,

I need some help to clarify the JSON compilation database syntax... This
document <http://clang.llvm.org/docs/JSONCompilationDatabase.html> gives
an example with space separated definition. I tried to reproduce the
desired output with cmake, but failed.

  add_definitions(-DQUOTED_DEF="With spaces and quotes.")
  add_definitions(-DESCAPED_DEF=With\ spaces\ and\ without\ quotes.)

These extra lines causes this command on the terminal

  gcc -DQUOTED_DEF="\"With spaces and quotes.\"" -DESCAPED_DEF="With
spaces and without quotes." -o ...

And the compile_commands.json file has entry like this

  "command": "/usr/lib64/ccache/gcc -DQUOTED_DEF=\"\\\"With spaces and
quotes.\\\"\" -DESCAPED_DEF=\"With spaces and without quotes.\" -o ..."

(The cmake version is 2.8.9) My understanding is that cmake does produce
valid JSON here. Only the document was wanted more elegant escaping by
single quotes. (-DSOMEDEF='\"With spaces and quotes.\"') Is that correct?

Yes, the document is incorrect. Sorry for that, no idea what I smoked that
day...

Thx for reporting,
/Manuel