Search code examples
cmakeg++iccclang-tidy

clang-tidy report error unknown argument when contain other compiler options


I have a project, I built it with intel compiler. I want use the clang-tidy to help detect code problems.

I am using CMake to generate compile_commands.json and I'm getting the follow error when I using clang-tidy:

$ run-clang-tidy
# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 warning and 1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]
warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [clang-diagnostic-unknown-warning-option]

Actually, there is a very simliar question exist: clang-tidy reporting unknown warnings

However, when I try to using the method refered above, there is no help. The warning can be suppressed but error still exist.

$ run-clang-tidy -extra-arg=-Wno-unknown-warning-option

# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -extra-arg=-Wno-unknown-warning-option -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]

How can I deal with the error?

-w2 options is used to control warning in intel compiler.

Although the problem occurs to me because of the intel compiler, but may be other compiler's options can also leads to the problem.

Appendix

The follow code snippets can help reproduce the problem.

// CMakeLists.txt
SET(CMAKE_CXX_COMPILER "icc")
SET(CMAKE_CXX_COMPILER "icpc")

project(test)

# leads to warning, can be settled by refer link
add_compile_options("-Wno-maybe-uninitialized")

# leads to error, can not be settled by refer link
add_compile_options("-w2")

add_executable(a.out main.cpp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
// main.cpp
#include <iostream>
int main()
{
    std::cout << "hello!" << std::endl;
    return 0;
}

the above code can generate compile_commands.json like follows:

[
{
  "directory": "/home/xuhui/temp/build",
  "command": "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc      -Wno-maybe-uninitialized -w2 -o CMakeFiles/a.out.dir/main.o -c /home/xuhui/temp/main.cpp",
  "file": "/home/xuhui/temp/main.cpp"
}
]

Thanks for your time.


Solution

  • This is not a clang-tidy error per se. Clang-diagnostic-error is basically a compiler error. Clang has made unknown arguments a hard error some time ago and it cannot be degraded to a warning. There used to be -Qunused-arguments but that doesn't work in Clang 11 AFAIK.

    You will have to remove the argument before passing the compile commands to clang-tidy, I suggest CMake - remove a compile flag for a single translation unit.