7

I read about tool called "Include What You Use" which can help clean superfluous includes from source code. I understood that there is a version for compiler LLVM (Clang) and version for GCC.

My questions are:

  1. Why is this tool compiler-dependent and not "cross-platform" for compilers? Why didn't the creators of the tool make it compiler-independent from the beginning? Is it related to the special implementation it has or something like that?

  2. If I want to take a version of the tool compatible for LLVM and I want to make it compatible with GCC (since I'm working with GCC), what do I have to do for that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Or Shemesh
  • 71
  • 1
  • 2
  • 2
    The tool relies on an API only provided by clang. Most compilers aren't developed with tooling in mind and don't expose the proper information to make such a tool. Your code should compile just fine with clang, so just use that to run your tools if you must continue to use gcc. Porting the tool to gcc would either be impossible or at least a massive undertaking. – xaxxon Aug 06 '17 at 04:32

3 Answers3

1

For the most part, Include-What-You-Use should be able to handle any valid C++ codebase, regardless of whether that codebase was written with GCC or Clang in mind. I recently had the occasion to run Include-What-You-Use on a very large codebase that was usually compiled with GCC and it worked fine. So in that sense it is already compatible.

That said, it might not work perfectly. It's likely that some of the information it provides will be wrong, even if it's a Clang codebase. So always verify the output manually.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flight Odyssey
  • 2,267
  • 18
  • 25
1
  1. Why is this tool compiler-dependent and not "cross-platform" for compilers? Why didn't the creators of the tool make it compiler-independent from the beginning? Is it related to the special implementation it has or something like that?

Reason is simple. Clang is more modern, fresh and has a better modular architecture. As a result, it is much easier to create tools using Clang modules.

That is why Clang was the first which had an address sanitizer and have more cool sanitizers. And this is why when someone creates a new tool for C++, it starts from Clang.

If I want to take a version of the tool compatible for LLVM and I want to make it compatible with GCC (since I'm working with GCC), what do I have to do for that?

Clang was created since Apple was not happy with GCC. So when it was written, it was supposed to be as much compatible with GCC as possible, since there was lots of code which was verified with GCC.

Since Clang is mature now and provides new own features, there might be small differences with GCC (for example, different bugs in implementations of new C++ standards), but in most common code there shouldn't be any problem.

Include What You Use should work without problems on both compilers. My coworker used it on a large project build with three compilers (Clang, GCC, and Visual Studio) and it worked like a charm.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marek R
  • 32,568
  • 6
  • 55
  • 140
0

The tool itself needs parts of the compiler! It is sitting somewhere between reading the source and parsing it. LLVM provides an API which is used for the tool. The tool itself is not standalone, but a plugin to the Clang/LLVM. As this, it needs the Clang/LLVM.

The modifications which will be done by the tool are fully compatible to every C++ compiler. And also the plugin in combination with Clang/LLVM should be able to parse more or less every code base independent of used other compilers. There might be some strange macros which are supported by other toolchains, which LLVM might be struggle with. But that should be a rare case at all.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Klaus
  • 24,205
  • 7
  • 58
  • 113