标签: clang-tidy

使用 clang-tidy 分析嵌入式 GCC 项目

我正在尝试使用 clang-tidy 来解析由 arm-none-eabi-g++ 编译的项目。

不幸的是,clang-tidy 无法找到编译器头,即使给出了它们的包含路径。

我的compile_commands.json是

  [   
    {
    "directory": "C:/LMA/repo/clang-tidy",
    "arguments": [
      "arm-none-eabi-c++",
      "-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
      "-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
      "test.cpp"
    ],
    "file": "./test.cpp"   } ]
Run Code Online (Sandbox Code Playgroud)

示例 test.cpp 文件是:

#include <cstdint>
#include <cstdlib>

int test()
{
  int temp;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Clang-tidy 显示错误:

C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1\cstdlib:75:15: error: 'stdlib.h' file not found [clang-diagnostic-error]
#include_next <stdlib.h>
Run Code Online (Sandbox Code Playgroud)

因此,它正确地找到并包含 cstdlib,但是无法找到位于完全相同的文件夹中的 stdint.h。更令人恼火的是,它不包括 stdlib.h,即使我添加

-include C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/stdlib.h 到编译器参数以强制预包含。

非常感谢任何如何解决此问题的建议。

c++ llvm clang-tidy

7
推荐指数
1
解决办法
2460
查看次数

clang-tidy:从 clang-tidy 分析中忽略特定文件夹

我有以下目录结构

\n

\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .clang 格式

\n

\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .clang-tidy

\n

\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 外部

\n

\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 脚本

\n

\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 源

\n

为了我的 clang 整洁执行,我只想包含source文件夹中的标头并排除external. 以下设置不适用于任一匹配 正则表达式HeaderFilterRegex: \'^(source)+(?!.*external).*\'的格式正确

\n

在文件中.clang-tidy如果我只包含HeaderFilterRegex: \'^(?!.*external).*\'它会排除该external文件夹但不包含source头文件

\n

同样,如果我只包含此检查,HeaderFilterRegex: \'(src/)\'它不会排除外部文件。

\n

更新

\n

好吧,这似乎与 clang 使用 Posix ERE 正则表达式风格有关(感谢 @pablo285),并且这不支持负向前瞻。我本以为这是使用 clang-tidy 的相当标准的要求,从分析中排除某些文件夹。

\n

c++ regex clang-tidy

7
推荐指数
0
解决办法
4649
查看次数

使用配置文件禁用或启用 cppcheck 警告

.clang-tidy使用 clang-tidy 静态分析器,我可以在项目的根目录中 保留一个文件 ( ),其中包含我想要激活或停用的警告。clang-tidy将查找此文件(据我所知)并使用其中定义的选项。这使我不必在 CMake 或 Makefile 中对长命令行进行硬编码。

cppcheck静态分析器可以做同样的事情吗?

目前我有这个很长的命令行硬编码:

cppcheck --max-ctu-depth=3 --enable=all --inline-suppr --suppress=*:*thrust/complex* --suppress=missingInclude --suppress=syntaxError --suppress=unmatchedSuppression --suppress=preprocessorErrorDirective --language=c++ --std=c++14 --error-exitcode=666
Run Code Online (Sandbox Code Playgroud)

.clang-tidy这是我保存在项目根目录下的配置文件示例:

---
Checks: '
    *,
    -readability-magic-numbers,
    -modernize-use-nodiscard,
    -altera-struct-pack-align,
    -cert-err58-cpp,
    -cppcoreguidelines-avoid-non-const-global-variables,
    -cppcoreguidelines-macro-usage,
    -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
    -cppcoreguidelines-pro-type-vararg,
    -cppcoreguidelines-avoid-magic-numbers,
    -fuchsia-default-arguments-calls,
    -fuchsia-trailing-return,
    -fuchsia-statically-constructed-objects,
    -fuchsia-overloaded-operator,
    -hicpp-vararg,
    -hicpp-no-array-decay,
    -llvm-header-guard,
    -llvmlibc-restrict-system-libc-headers,
    -llvmlibc-implementation-in-namespace,
    -llvmlibc-callee-namespace
'
WarningsAsErrors: '*'
HeaderFilterRegex: '.'
AnalyzeTemporaryDtors: false
FormatStyle: file
...
Run Code Online (Sandbox Code Playgroud)

c++ configuration-files cppcheck clang-tidy

7
推荐指数
1
解决办法
4692
查看次数

Clang Tidy 配置格式

目前我正在我的项目中使用 Clang Format 实用程序。为了在我的团队中共享其设置,我将 .clang 格式的配置文件放在项目文件夹的根目录中,现在 IDE 在处理项目时会自动加载它。同样,我想使用 Clang Tidy 实用程序。但是,与 Clang Format 不同,我找不到配置文件格式的描述或创建它的实用程序。我还需要 IDE 自动加载这些设置并在自动格式化时考虑它们,因此我不可能使用向其传递必要参数的脚本来运行该实用程序。有办法实现我所需要的吗?

c++ clang-tidy

7
推荐指数
1
解决办法
7202
查看次数

clang-tidy:从分析中排除特定目录

clang-tidy我正在一个具有以下三个文件夹的中型项目中使用:

srcA
srcB
external
Run Code Online (Sandbox Code Playgroud)

我试图external从分析中排除文件夹,但没有成功。我正在使用的命令是:

clang-tidy $SRC -p build/ --extra-arg=-ferror-limit=0'
Run Code Online (Sandbox Code Playgroud)

SRC=srcA/file.cpp srcA/fileN.cpp srcB/file.cpp srcB/fileN.cpp ...
Run Code Online (Sandbox Code Playgroud)

build/以及由生成的编译数据库cmake。请注意,SRC 不包含任何external文件,仅包含srcAsrcB(.cpp 和 .hpp)。此外,显然,srcAsrcB下的一些文件正在使用external.

80% 的错误来自clang-tidy文件external/,我无法修复这些错误,因为有第三方库。

下面是.clang-tidy我正在使用的文件:

Checks: '-*,readability-identifier-naming'
WarningsAsErrors: "*"
CheckOptions:
  - { key: readability-identifier-naming.ClassCase, value: CamelCase }
  - { key: readability-identifier-naming.ClassMethodCase, value: camelBack }
  - { key: readability-identifier-naming.VariableCase, value: camelBack }
  - { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ } …
Run Code Online (Sandbox Code Playgroud)

c++ cmake linter clang-tidy

7
推荐指数
2
解决办法
7430
查看次数

如何整合cakey与CMake(<LANG> _CLANG_TIDY)和MSVC?

如何传递clang旗帜,例如-fms-compatibility-version使用<LANG>_CLANG_TIDYCMake属性?在CLI上这很容易:

clang-tidy main.cpp -- -fms-compatibility-version=19.10
Run Code Online (Sandbox Code Playgroud)

但是使用CMake,这不能按预期工作:

-DCMAKE_CXX_CLANG_TIDY="clang-tidy;-checks=-*,readability-*;--;-fms-compatibility-version=19.10"
Run Code Online (Sandbox Code Playgroud)

该标志是clang使用现代版本的MSVC 所必需的.

如果这是不可能的; 有没有其他方法来集成CMake + MSVC + clang-tidy(除了创建自定义构建目标)?

cmake clang visual-c++ clang-tidy

6
推荐指数
1
解决办法
920
查看次数

Clion未初始化的记录类型:播放器

几周前我已经开始学习C ++。我现在看到类和所有东西,而且我想知道对象实例化。在此代码中:

class Player
{
    public:
        int x, y;
        int speed;
};

int main ()
{
    Player player {};

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果不加括号,我会从IDE收到警告Player player {}。我使用的是Clion 2017.2.3。警告说:“未初始化的记录类型:播放器”,似乎来自Clang-Tidy,尽管我不确定它的真正作用。

那么,这重要吗?是否必须在对象实例化中添加括号?

对不起,我的母语不是英语。

c++ curly-braces clion clang-tidy

6
推荐指数
1
解决办法
2933
查看次数

有没有办法避免在初始化字符串时从clang-tidy(紫红色默认参数)发出此警告?

考虑这段代码:

#include <iostream>

int main () { 
  std::string str = "not default";
  std::cout << str << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

运行clang-tidy -checks=* string.cpp给出以下内容:

7800 warnings generated.
/tmp/clang_tidy_bug/string.cpp:4:21: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
  std::string str = "not default";
                    ^
/../lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../include/c++/8.1.1/bits/basic_string.h:509:39: note: default parameter was declared here
      basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
                                      ^
Suppressed 7799 warnings (7799 in non-user code).
Run Code Online (Sandbox Code Playgroud)

还有其他可以通过的论点可以使此警告消失吗?我在这里实际上没有使用任何参数默认值。但是std :: string的实现确实可以。

编辑:更改代码以简化测试用例。

c++ clang-tidy

6
推荐指数
1
解决办法
1159
查看次数

静态std :: stringstream的clang-tidy警告

我有以下MCVE:

#include <sstream>

struct A {
    static std::stringstream s;
};

std::stringstream A::s;

int main() {}
Run Code Online (Sandbox Code Playgroud)

当我在这段代码上运行clang-tidy 6.0.1时,我得到以下警告:

static_sstream.cpp:7:22: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'out' [cppcoreguidelines-interfaces-global-init]
std::stringstream A::s;
                     ^
Run Code Online (Sandbox Code Playgroud)

似乎问题在于构造函数的std::stringstream参数具有默认值std::ios_base::out.我的问题是,这是一个真正的问题吗?如果是这样,std::stringstream在类中使用静态的正确方法是什么?

c++ stringstream clang-tidy

6
推荐指数
1
解决办法
197
查看次数

如何在不使用reinterpret_cast的情况下使用dlsym()加载函数?

我正在尝试使用clang-tidy来实施C ++核心准则。尽管它确实有很多有效的要点,但我无法真正解决一件事:dlsym返回a void*,我需要以某种方式将其转换为适当的函数指针。为此,我使用reinterpret_cast。由于准则禁止这样做,因此我对此有警告。当然,我可以在//NOLINT任何地方发表评论,但是我正在寻找一种不会使用的解决方案,reinterpret_cast因此警告会消失。

有没有解决此问题的方法?

c++ clang-tidy

6
推荐指数
1
解决办法
178
查看次数