Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang-Tidy can't find my header files

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"

like image 947
Maggie S. Avatar asked Sep 12 '16 16:09

Maggie S.


2 Answers

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.

like image 129
Knitschi Avatar answered Sep 20 '22 14:09

Knitschi


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 --

like image 37
eugenioperez Avatar answered Sep 21 '22 14:09

eugenioperez