4

Thanks to this answer, I learned I could run include-what-you-use on my C++ project by using the following in CMake:

set_property(TARGET ${target_name}
             PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path})

This outputs suggestions on what to include when I run cmake --build on my project:

Warning: include-what-you-use reported diagnostics:

/home/user/hello/main.cc should add these lines:

/home/user/hello/main.cc should remove these lines:
- #include <vector>  // lines 2-2

The full include-list for /home/user/hello/main.cc:
#include <iostream>  // for operator<<, basic_ostream, cout, endl, ostream

The include-what-you-use documentation explains how to automatically apply these suggestions, so that I can always use the "full include-list" specified by include-what-you-use:

make -k CXX=/path/to/llvm/Debug+Asserts/bin/include-what-you-use > /tmp/iwyu.out
python fix_includes.py < /tmp/iwyu.out

Is there a way to automatically apply suggestions when using the CMake CXX_INCLUDE_WHAT_YOU_USE property? If not, is there a way I can apply suggestions as part of my CMake build?

Community
  • 1
  • 1
Brian Gesiak
  • 6,648
  • 4
  • 35
  • 50
  • 1
    The first thing that comes to my mind is to make a custom target ... but I will lookup the doc on this `CXX_INCLUDE_WHAT_YOU_USE`. – fedepad Jan 19 '17 at 19:23
  • 1
    As far as I see `CXX_INCLUDE_WHAT_YOU_USE` is set to the path of the program iwyu (and eventually some options).My understanding is that you still need some kind of custom target in which you execute those two lines you mention.You could make it part of the `all` target or not. In this case you will do something like `make -k CXX=${CXX_INCLUDE_WHAT_YOU_USE}`. Probably you can achieve the same in another way, but I think it's cleaner using custom target then other ways...maybe!You can then make your standard target depend on this target; so you will "fix" the files for example before the build. – fedepad Jan 19 '17 at 20:08

0 Answers0