[lang-tidy报告错误,包含其他编译器选项时未知参数

问题描述 投票:1回答:2

我有一个项目,我使用intel编译器构建了它。我想使用clang-tidy来帮助检测代码问题。

我正在使用CMake生成compile_commands.json,并且在使用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]

实际上,存在一个非常相似的问题:clang-tidy reporting unknown warnings

但是,当我尝试使用上面提到的方法时,没有任何帮助。该警告可以抑制,但错误仍然存​​在。

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

我该如何处理错误?

-w2选项用于控制intel编译器中的警告。

虽然问题是由于intel编译器而发生的,但可能是其他编译器的选项也可能导致此问题。

附录

以下代码片段可以帮助重现该问题。

// 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;
}

上面的代码可以生成compile_commands.json,如下所示:

[
{
  "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"
}
]

感谢您的时间。

cmake g++ icc clang-tidy
2个回答
1
投票

这本身不是整洁的错误。 Clang-diagnostic-error本质上是编译器错误。不久前,Clang已将未知参数设为硬错误,并且不能将其降级为警告。曾经有-Qunused-arguments,但是在Clang 11 AFAIK中不起作用。

[您必须在将编译命令传递给clang-tidy之前删除参数,我建议CMake - remove a compile flag for a single translation unit


1
投票

@@ pablo285已经给出了完美的答案。他说:

必须在将编译命令传递给clang-tidy

他已经提供了一个链接来演示如何修改CMakeLists.txt以删除参数。

此外,我们可以直接在compile_commands.json上进行一些修改以删除参数。

可以使代码整洁的脚本编写如下:

# tidy_code.sh

cd build
cmake ..

# do modification on compile_commands.json to remove argument which clang can not recognized
# replace '-w2' to ' '
sed -i 's/-w2/ /g' compile_commands.json

# using clang tidy 
run-clang-tidy -checks='*' -extra-arg=-Wno-unknown-warning-option
© www.soinside.com 2019 - 2024. All rights reserved.