Clang extra tools with compile_commands.json on windows

Hello everyone.

I would like to talk about an attempt to use clang-modernize and clang-tidy,
and clang-analyzer on windows.
I'm using CMake 3 and Visual studio 2013.

I would like to use the various great tools of clang to scan over my code
base, but these seems to take a compile_commands.json file as input (this
file should be in the build path provided by the "-p" option of those
tools).

But sadly the CMAKE_EXPORT_COMPILE_COMMAND=ON option does not work with the
visual generator (which I use)

Some great people of Clang community are talking about here
<http://clang-developers.42468.n3.nabble.com/compile-commands-json-for-Windows-MSVC-tt4033848.html#none&gt;

Their point is that it could be in the future added to CMake, but that they
would prefer to not rely on it. Anyway, because of this *"Richard" decided
to use the Log output of MSBuild to generate the compile_commands.json file*
which he says should work.

Therefore as I don't see any of this implemented yet in a public repo, I
decided to implement myself a little script to transform the output of the
MSBuild log (that i get using "/fl
/fileLoggerParameters:LogFile=MSBuild.log;Append;Verbosity=normal;Encoding=UTF-8"
as options to MSBuild, and parse it to generate a compile_commands.json

See here an example of a simple project which has only 1 cpp file:
compile_commands.json
<http://clang-developers.42468.n3.nabble.com/file/n4043700/compile_commands.json&gt;

Then I used
*clang-modernize -p=<build-path> -summary -include=<src_dir>*
*clang-tidy -p=<build-path> src/main.cpp*
Which leads to a lot of warnings and an error because "<stdlib.h> could not
be found.

See the console log attached. log.log
<http://clang-developers.42468.n3.nabble.com/file/n4043700/log.log&gt;

Do you have any idea of what is wrong?

Should I get as many errors, it looks like the tools don't understand the
flags?
Should this fail because of stdlib.h?
what could be a solution?
Is my use of the tools (command line parameters) right?

thank you

But as I yet do not know how to

Hello everyone.

I would like to talk about an attempt to use clang-modernize and clang-tidy,
and clang-analyzer on windows.
I’m using CMake 3 and Visual studio 2013.

I would like to use the various great tools of clang to scan over my code
base, but these seems to take a compile_commands.json file as input (this
file should be in the build path provided by the “-p” option of those
tools).

But sadly the CMAKE_EXPORT_COMPILE_COMMAND=ON option does not work with the
visual generator (which I use)

Some great people of Clang community are talking about here
<http://clang-developers.42468.n3.nabble.com/compile-commands-json-for-Windows-MSVC-tt4033848.html#none>

Their point is that it could be in the future added to CMake, but that they
would prefer to not rely on it. Anyway, because of this “Richard” decided
to use the Log output of MSBuild to generate the compile_commands.json file

which he says should work.

Therefore as I don’t see any of this implemented yet in a public repo, I
decided to implement myself a little script to transform the output of the
MSBuild log (that i get using “/fl
/fileLoggerParameters:LogFile=MSBuild.log;Append;Verbosity=normal;Encoding=UTF-8”
as options to MSBuild, and parse it to generate a compile_commands.json

See here an example of a simple project which has only 1 cpp file:
compile_commands.json
<http://clang-developers.42468.n3.nabble.com/file/n4043700/compile_commands.json>

Then I used
clang-modernize -p= -summary -include=<src_dir>
clang-tidy -p= src/main.cpp
Which leads to a lot of warnings and an error because "<stdlib.h> could not
be found.

See the console log attached. log.log
<http://clang-developers.42468.n3.nabble.com/file/n4043700/log.log>

Do you have any idea of what is wrong?

Should I get as many errors, it looks like the tools don’t understand the
flags?
Should this fail because of stdlib.h?
what could be a solution?
Is my use of the tools (command line parameters) right?

There are 2 problems here:

  1. you need to quote anything containing spaces, which includes the original command (you have spaces in the path)
  2. clang needs to know it’s in cl- mode; Hans might have an idea on how to solve this…

(Re-sending from the correct address; apologies if you receive this twice.)

Hello everyone.

I would like to talk about an attempt to use clang-modernize and
clang-tidy,
and clang-analyzer on windows.
I'm using CMake 3 and Visual studio 2013.

I would like to use the various great tools of clang to scan over my code
base, but these seems to take a compile_commands.json file as input (this
file should be in the build path provided by the "-p" option of those
tools).

But sadly the CMAKE_EXPORT_COMPILE_COMMAND=ON option does not work with
the
visual generator (which I use)

Some great people of Clang community are talking about here

<http://clang-developers.42468.n3.nabble.com/compile-commands-json-for-Windows-MSVC-tt4033848.html#none&gt;

Their point is that it could be in the future added to CMake, but that
they
would prefer to not rely on it. Anyway, because of this *"Richard" decided
to use the Log output of MSBuild to generate the compile_commands.json
file*
which he says should work.

Therefore as I don't see any of this implemented yet in a public repo, I
decided to implement myself a little script to transform the output of the
MSBuild log (that i get using "/fl

/fileLoggerParameters:LogFile=MSBuild.log;Append;Verbosity=normal;Encoding=UTF-8"
as options to MSBuild, and parse it to generate a compile_commands.json

See here an example of a simple project which has only 1 cpp file:
compile_commands.json

<http://clang-developers.42468.n3.nabble.com/file/n4043700/compile_commands.json&gt;

Then I used
*clang-modernize -p=<build-path> -summary -include=<src_dir>*
*clang-tidy -p=<build-path> src/main.cpp*
Which leads to a lot of warnings and an error because "<stdlib.h> could
not
be found.

See the console log attached. log.log
<http://clang-developers.42468.n3.nabble.com/file/n4043700/log.log&gt;

Do you have any idea of what is wrong?

Should I get as many errors, it looks like the tools don't understand the
flags?
Should this fail because of stdlib.h?
what could be a solution?
Is my use of the tools (command line parameters) right?

There are 2 problems here:
1. you need to quote anything containing spaces, which includes the original
command (you have spaces in the path)
2. clang needs to know it's in cl- mode; Hans might have an idea on how to
solve this...

Right, I don't think anyone has been working on using these tools with
cl-style command-lines.

Getting the options parser to accept cl-style arguments is just a
matter of flipping a flag. The question is how to design it. Should
clang-tidy have an option for this? Should there be a clang-tidy-cl?
Should there be something in the json file?

The more difficult problem is that IIUC these tools don't go through
the Driver, in which clang-cl does stuff like figuring out include
paths, enabling -fms-extensions etc. Or maybe it does - how does
clang-tidy et al find system include files?

In any case, as it is today these tools are not ready to be used with
cl-style command-lines.

- Hans

(Re-sending from the correct address; apologies if you receive this twice.)

Hello everyone.

I would like to talk about an attempt to use clang-modernize and
clang-tidy,
and clang-analyzer on windows.
I’m using CMake 3 and Visual studio 2013.

I would like to use the various great tools of clang to scan over my code
base, but these seems to take a compile_commands.json file as input (this
file should be in the build path provided by the “-p” option of those
tools).

But sadly the CMAKE_EXPORT_COMPILE_COMMAND=ON option does not work with
the
visual generator (which I use)

Some great people of Clang community are talking about here

<http://clang-developers.42468.n3.nabble.com/compile-commands-json-for-Windows-MSVC-tt4033848.html#none>

Their point is that it could be in the future added to CMake, but that
they
would prefer to not rely on it. Anyway, because of this “Richard” decided
to use the Log output of MSBuild to generate the compile_commands.json
file

which he says should work.

Therefore as I don’t see any of this implemented yet in a public repo, I
decided to implement myself a little script to transform the output of the
MSBuild log (that i get using "/fl

/fileLoggerParameters:LogFile=MSBuild.log;Append;Verbosity=normal;Encoding=UTF-8"
as options to MSBuild, and parse it to generate a compile_commands.json

See here an example of a simple project which has only 1 cpp file:
compile_commands.json

<http://clang-developers.42468.n3.nabble.com/file/n4043700/compile_commands.json>

Then I used
clang-modernize -p= -summary -include=<src_dir>
clang-tidy -p= src/main.cpp
Which leads to a lot of warnings and an error because "<stdlib.h> could
not
be found.

See the console log attached. log.log
<http://clang-developers.42468.n3.nabble.com/file/n4043700/log.log>

Do you have any idea of what is wrong?

Should I get as many errors, it looks like the tools don’t understand the
flags?
Should this fail because of stdlib.h?
what could be a solution?
Is my use of the tools (command line parameters) right?

There are 2 problems here:

  1. you need to quote anything containing spaces, which includes the original
    command (you have spaces in the path)
  2. clang needs to know it’s in cl- mode; Hans might have an idea on how to
    solve this…

Right, I don’t think anyone has been working on using these tools with
cl-style command-lines.

Getting the options parser to accept cl-style arguments is just a
matter of flipping a flag. The question is how to design it. Should
clang-tidy have an option for this? Should there be a clang-tidy-cl?
Should there be something in the json file?

The more difficult problem is that IIUC these tools don’t go through
the Driver, in which clang-cl does stuff like figuring out include
paths, enabling -fms-extensions etc. Or maybe it does - how does
clang-tidy et al find system include files?

Well, libTooling does go through the clang::driver::Driver. The problem is that if we need the binary name to follow a certain pattern, the tool would need to know that… It’s certainly doable

We don't need that. We can thread through --driver-mode=gcc/cl to get the
desired behavior.

Here are some other potential pitfalls for making this code work:
1. The builtin argument adjusters won't work, clang-cl doesn't recognize
-fsyntax-only or use -o for output
2. The json file will need to capture the %INCLUDE% environment variable,
as this is how clang-cl finds standard headers (<vector>, <stdio.h>)

Well, libTooling does go through the clang::driver::Driver. The problem
is that if we need the binary name to follow a certain pattern, the tool
would need to know that... It's certainly doable

We don't need that. We can thread through --driver-mode=gcc/cl to get the
desired behavior.

Here are some other potential pitfalls for making this code work:
1. The builtin argument adjusters won't work, clang-cl doesn't recognize
-fsyntax-only or use -o for output

Is it possible to have one universal syntax-only arguments adjuster (e.g.
handle both -o and /o, etc.?) or do we need to recognize the clang-cl mode?

There are arguments relevant to tooling with conflicting meanings, for
example -MD, so I think that yes, we will have to know the mode.

One way to do this would be to write a translator from clang-cl driver
options to clang driver options. The clang-cl driver logic does *not* have
support for this, it actually translates to -cc1 options. So there would be
some duplicated logic. =/