25

new to clang and clang-tidy here.

I have a project with this type of structure: project/ - build/ - cmake/ - component1/ - src/ - someFile.cpp - someFile2.cpp - someFile.hpp - someFile2.hpp - component2/ - etc... -

When I use clang-tidy to go through all the files in project/component1/ with this command: clang-tidy project/component1/src/* -checks=-*,clang-analyzer-*,-clang-analyzer-alpha*

It ends up throwing an error like this: $HOME/project/component1/src/someFile.cpp:18:10: error: 'project/component1/someFile.hpp' file not found [clang-diagnostic-error] \#include "component1/someFile.hpp"

valiano
  • 16,433
  • 7
  • 64
  • 79
Maggie S.
  • 1,076
  • 4
  • 20
  • 30
  • Could it be that they are not in the same level ('someFile.hpp' is not in /src)? – Griffin Sep 12 '16 at 17:24
  • @Grif-fin I've thought of that, but I'm not allowed to mess with the file structure. Is there are command options that I can set to let it know where the *.hpp files are? The descriptions of each option are not always high-level enough for me to get what's going on. *:-/ – Maggie S. Sep 12 '16 at 18:00
  • I just tried running the clang-tidy command on the `project/component1/` directory but I'm getting the same error or those files under `src/`. – Maggie S. Sep 12 '16 at 18:21
  • 1
    You could try with flag --header-filter= or include the hpp files in cpp with '../'. e.g. #include "../someFile.hpp". The later option is not recommended. – Griffin Sep 13 '16 at 10:02

3 Answers3

29

I tell clang-tidy to search for them using plain compiler includes, but they have to be introduced after a double dash (--). It also took me a while to discover it, since it is not included in the --help:

clang-tidy -checks='...' <source0> ... -- -Iblabla/ ...

Reading again the options, you coult try -extra-arg= parameter, but I use the double dash approax since it allows me to put all the options to give clang and clang-tidy in a single file, with no more processing than a $(cat $file) for both.


From: https://clang.llvm.org/extra/clang-tidy/#using-clang-tidy

clang-tidy is a LibTooling-based tool . You can also specify compilation options on the command line after --

eugenioperez
  • 627
  • 7
  • 15
27

This answer will only help you if you use CMake to manage your project.

CMake has an option to create a .json file that contains all the compiler calls with command line options. This file can be given to clang-tidy with the option:

-p <build-path> is used to read a compile command database.

    For example, it can be a CMake build directory in which a file named
    compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    CMake option to get this output). When no build path is specified,
    a search for compile_commands.json will be attempted through all
    parent paths of the first input file . See:
    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
    example of setting up Clang Tooling on a source tree.

As the documentation states, you have to set the CMAKE_EXPORT_COMPILE_COMMANDS variable to generate the .json file with CMake and then pass the CMake output directory to clang-tidy. Clang-tidy will then get the include paths from the commands in the .json file.

Knitschi
  • 2,822
  • 3
  • 32
  • 51
  • This is unnecessary if you are using CMake. You can just use `set(CMAKE_CXX_CLANG_TIDY clang-tidy)` and it [will add the full command line to `clang-tidy` itself.](https://cmake.org/cmake/help/latest/prop_tgt/LANG_CLANG_TIDY.html#prop_tgt:%3CLANG%3E_CLANG_TIDY). – Timmmm Feb 09 '23 at 14:20
0

Had the same problem in CMake. The problem in my case was that Clang-Tidy could not handle include directories via response files – at least in version 10.

Deactivating them solved it:

# Disable response files
set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES Off)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Oliver
  • 1