LibTooling CMake Error

Hi,
   I am trying to setup LibTooling for clang. I have gone through the
procedure given at </a>
<http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html> . I can
succesfully run clang-check for any source file. I am now trying to compile
the example at </a> <http://clang.llvm.org/docs/RAVFrontendAction.html> .
When using cmake, I get the following error:

Unknown CMake command : add_clang_executable

  When using cmake to build clang, I used -DEXPORT_CMAKE_COMPILE_COMMANDS=ON
variable as suggested. Then why isn't this command working?

Regards,
Adil

add_clang_executable is defined in the clang cmake files, so to use it
you need to build the example as part of clang. If you want to follow
this route, then a convenient place in the tree to put it is in the
extra tools repository. Have a look at
http://clang.llvm.org/docs/ClangTools.html

Alternatively, to build standalone, you can try copying the cmake
configuration from
https://github.com/philipc/clang-ast/blob/master/CMakeLists.standalone
and https://github.com/philipc/clang-ast/blob/master/cmake/modules/FindLibClang.cmake

I’ve checked out the extra tools repository at the correct place and I have built clang again, with the CLANG_BUILD_EXAMPLES variable set to true in cmake. I am still getting the same error. What can possibly be wrong? Can you please provide all the steps required to make this command work.
The two cmake files you mentioned are both related to libclang. I am using Libtooling. How do I adapt them to build the FrontEndAction example given in the docs.

I've checked out the extra tools repository at the correct place and I have
built clang again, with the CLANG_BUILD_EXAMPLES variable set to true in
cmake. I am still getting the same error. What can possibly be wrong? Can
you please provide all the steps required to make this command work.

Did you place your example code in the extra tools repository as well?
There isn't any documentation for this, but it is straight forward to
copy how the existing tools in the repository are built.

     The two cmake files you mentioned are both related to libclang. I am
using Libtooling. How do I adapt them to build the FrontEndAction example
given in the docs.

Tooling is part of libclang. I've pushed a version of the
FrontEndAction example that compiles for me to
https://github.com/philipc/find-class-decls

Thnx a lot. I didn’t realize that I had to put my code in the extra tools repository. But with your CMakeLists , it’s working independently. I have a few small issues though.
I need to pass in a cpp file rather than the code in the command line. How do I do that? I have tried modifying the parameters of runtooloncode but they are not working. (I need to rewrite in that file)
I need to find all the instances of a variable usage in a file. e.g. Every place where ‘x’ was used. How can I do that?
I am very grateful for your help.

Regards,
Adil

Also, I’ve trying to build the simple syntax checker tool and run it. I create a .json file in my build directory and build the tool. The tool builds fine. But when I call the tool using

./simple-tool test.cpp

or any other arguments, it gives an error saying

skipping …
Command Line not found

What is the problem here? How can it be rectified?

Regards,
Adil

Hi,
   I am trying to setup LibTooling for clang. I have gone through the
procedure given at </a>
<http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html> . I can
succesfully run clang-check for any source file. I am now trying to compile
the example at </a> <http://clang.llvm.org/docs/RAVFrontendAction.html> .
When using cmake, I get the following error:

Unknown CMake command : add_clang_executable

  When using cmake to build clang, I used -DEXPORT_CMAKE_COMPILE_COMMANDS=ON
variable as suggested. Then why isn't this command working?

My best guess would be that you don't have a version of cmake that
supports this command, but I don't really know. This functionality was
only added (to both Clang an CMake) in the last 6 months or so.

That problem is resolved using a different CMakeLists. I am having a slightly different problem now. The tool works now but when it is giving an error:

#include <stddef.h> not found

I checked the directories using -v and two directories namely “…/lib/clang/3.2/include” and “/include” were not found. As you can see, these paths are wrong. They should be “/usr/local/lib/clang…” and “/usr/include”. How can I fix this?

The second problem is that I want to run this tool on a source file rather than code given through command line. How do I do that? I also want to retain the original locations in the source file so that I can rewrite to it.

Regards.
Adil

I checked the directories using -v and two directories namely
"../lib/clang/3.2/include" and "/include" were not found. As you can see,
these paths are wrong. They should be "/usr/local/lib/clang.." and
"/usr/include". How can I fix this?

This is the issue that I warned you about in the other thread, and it
has no solution to my knowledge except for hardcoding the correct
path.

-- Sean Silva

Thnx a lot for your help. Is there any chance this will be resolved soon? So, basically, there is absolutely no way in which proper paths for C++ can be added without hardcoding right now? I cloned the standard clang repository right now. Is there a development branch that might be more up-to-date?

Thnx a lot for your help. Is there any chance this will be resolved soon?
So, basically, there is absolutely no way in which proper paths for C++ can
be added without hardcoding right now? I cloned the standard clang
repository right now. Is there a development branch that might be more
up-to-date?

clang-tool behave the same way clang does here - they need to find their
builtin includes; by default, the tools find those in
../lib/clang/<version>/include.

There are two ways to solve this problem:
1. copy the tool you wrote next to the clang binary you built from the same
source tree
2. create an "installation" of your tool, where
../lib/clang/<version>/include points to the builtin headers

I'm not sure what else could be done here. Ideas welcome :slight_smile:

Cheers,
/Manuel

I'm not sure what else could be done here. Ideas welcome :slight_smile:

Maybe we could bake those header files directly into libFrontend?

It's a bit extreme, but I think that it is probably the right thing to
do here, since to be honest, it is simply unacceptable for a library
to force its clients to run their tool from directories with
particular directory layouts, or to install in a particular place.
Imagine if, say, zlib would not function correctly without a
configuration file "../share/zlib/zlib.conf", or something like
that---there would be outrage.

-- Sean Silva

> I'm not sure what else could be done here. Ideas welcome :slight_smile:

Maybe we could bake those header files directly into libFrontend?

It's a bit extreme, but I think that it is probably the right thing to
do here, since to be honest, it is simply unacceptable for a library
to force its clients to run their tool from directories with
particular directory layouts, or to install in a particular place.
Imagine if, say, zlib would not function correctly without a
configuration file "../share/zlib/zlib.conf", or something like
that---there would be outrage.

Feel free to bring this up with Chandler / Doug on IRC. I've argued that
many times, and so far I've always lost (I don't remember all the points in
the arguments, but I think one big point is: you wan to have headers with
paths people can actually look at in their text editors).

Cheers,
/Manuel

Hi,
I wish to share my humble opinion in this regard. I believe that clang is a great program and the Clang API is by far, the most self-explanatory API I have seen. Nevertheless, If clang is to ever replace gcc, it should offer a very smooth development. By forcing the user to place all tools in the clang directory, the project management is seriously hindered. Currently the paths in the json file are “…/lib/clang/3.2/include”. Why not simply replace them with the complete path like the rest of standard paths. Please consider my humble suggestion. It will be a lot of help for future development.
As for now, Can you please tell me exactly where to place my tool? I’ve tried a few locations, but none of them seems to completely resolve the problem. I am very grateful for all of your help.

Regards,
Adil

Hi,
   I wish to share my humble opinion in this regard. I believe that clang
is a great program and the Clang API is by far, the most self-explanatory
API I have seen. Nevertheless, If clang is to ever replace gcc, it should
offer a very smooth development. By forcing the user to place all tools in
the clang directory, the project management is seriously hindered.
Currently the paths in the json file are "../lib/clang/3.2/include". Why
not simply replace them with the complete path like the rest of standard
paths. Please consider my humble suggestion. It will be a lot of help for
future development.

I'm not sure what the json files have to do with it. The json files just
describe compile options - the builtin header files really belong to the
tool executable.

   As for now, Can you please tell me exactly where to place my tool? I've
tried a few locations, but none of them seems to completely resolve the
problem. I am very grateful for all of your help.

If you have a working <somepath>/clang binary of the same version as your
tool, with which you can compile code (and find the builtin headers :wink:
place your tool into <somepath>/.

The easiest way to get your tool to work is to integrate it with the normal
clang build, the same way the extra-tools repository does.

Cheers,
/Manuel