标签: clang-tidy

处理"抛出的异常类型不是无法复制构造"警告

经过12年的中断,回到C++开发.我正在使用JetBrains的CLion软件,因为它提供了很多关于我的课程设计可能出现的问题的输入.我在我的类'constructor throw语句中得到的警告之一是:Thrown exception type is not nothrow copy constructible.以下是生成此警告的代码示例:

#include <exception>
#include <iostream>

using std::invalid_argument;
using std::string;

class MyClass {
    public:
        explicit MyClass(string value) throw (invalid_argument);
    private:
        string value;
};

MyClass::MyClass(string value) throw (invalid_argument) {
    if (value.length() == 0) {
        throw invalid_argument("YOLO!"); // Warning is here.
    }

    this->value = value;
} 
Run Code Online (Sandbox Code Playgroud)

这段代码编译,我能够对它进行单元测试.但是我非常希望摆脱这个警告(为了理解我做错了什么,即使它编译了).

谢谢

c++ exception std c++11 clang-tidy

12
推荐指数
1
解决办法
3436
查看次数

how to use clang tidy in CMake

I would like to use CMake and clang-tidy in my project, however I see that build times are quite a bit higher when I use this in all the main cmake file:

set(CMAKE_CXX_CLANG_TIDY
  clang-tidy-11;
  -format-style='file';
  -header-filter=${CMAKE_CURRENT_SOURCE_DIR};
)
Run Code Online (Sandbox Code Playgroud)

It is working well, but I don't want to have this build-time penalty every time I build the project during development. Therefore I thought I would make a separate target that builds all, but uses clang-tidy. And when I do a regular debug …

cmake clang-tidy

12
推荐指数
1
解决办法
2万
查看次数

变得铿锵有力地修复头文件

我正在将当前使用gcc编译的项目移动到clang,并且有一堆gcc没有生成的警告(-Winconsistent-missing-override).clang-tidy用于修复*.cpp文件中的这些错误,但它不会触及hpp文件,因为在数据库中找不到编译命令(正如我所料).

我正在使用ninja构建项目并ninja -t compdb cc cxx > .build/compile_commands.json生成编译数据库.我试过跑:

clang-tidy-3.6 -p .build/      \
      $(find src/ -name *.cpp) \
      $(find src/ -name *.hpp) \
      --checks=misc-use-override --fix
Run Code Online (Sandbox Code Playgroud)

修复错误.它拒绝触摸头文件抱怨:

Skipping .../src/header/file.hpp. Compile command not found.
Run Code Online (Sandbox Code Playgroud)

c++ automated-refactoring llvm-clang clang++ clang-tidy

11
推荐指数
1
解决办法
1万
查看次数

Clang-Tidy 找不到我的头文件

这里是 clang 和 clang-tidy 的新手。

我有一个具有这种结构的项目: project/ - build/ - cmake/ - component1/ - src/ - someFile.cpp - someFile2.cpp - someFile.hpp - someFile2.hpp - component2/ - etc... -

当我使用 clang-tidy 通过project/component1/以下命令查看所有文件时:clang-tidy project/component1/src/* -checks=-*,clang-analyzer-*,-clang-analyzer-alpha*

它最终抛出这样的错误: $HOME/project/component1/src/someFile.cpp:18:10: error: 'project/component1/someFile.hpp' file not found [clang-diagnostic-error] \#include "component1/someFile.hpp"

c++ clang clang-tidy

11
推荐指数
2
解决办法
1万
查看次数

如何使用视觉工作室进行clang-tidy现代化

我正在尝试从visual studio或命令行中对文件应用clang现代化,利用我的visual studio 2015 C++项目中的包含路径和设置.

我创建了一个铿锵有力的设置文件,如下所示:

clang-tidy -dump-config=.clang-tidy 
  -checks="modernize-loop-convert,modernize-deprecated-headers"
Run Code Online (Sandbox Code Playgroud)

并验证它可以在单个文件上运行,来自(cygwin)命令行:clang-tidy.exe -explain-config列表(以及其他内容)

'modernize-deprecated-headers' is enabled in the C:\abc\.clang-tidy.
Run Code Online (Sandbox Code Playgroud)

可以通过手动设置包括在单个文件上运行它: clang-tidy.exe someFile.cpp -- -Ic:/abc -I. -IIncludes

我安装了视觉工作室铛格式VS插件,和创建的示例.clang格式文件,{ BasedOnStyle: "LLVM", IndentWidth: 20 }由VS插件拾取.然后我天真地尝试用clang-tidy配置来提供clang-format,但不出所料,这根本不起作用.

总结一下:如何在文件和visual studio项目上运行clang-tidy?

也许解决方法是从visual studio中转储每个文件的包含?任何前进的方式都可以.

以供参考:

如何启用clang-tidy的"现代化"检查?

如何使用clang格式文件提供Visual Studio Clang-Format插件?

c++ clang visual-studio clang-format clang-tidy

11
推荐指数
1
解决办法
4590
查看次数

什么是具有 ID 相关向后分支的循环?

我试图理解这个铿锵有力的警告:altera-id-dependent-backward-branch这似乎是由这个循环触发的。

        for(; first != current; ++first)
Run Code Online (Sandbox Code Playgroud)

我的例子是这段代码,它看起来几乎完全是std::uninitialized_fill_n.

静态分析器抱怨说:

error: backward branch (for loop) is ID-dependent due to variable reference to 'current' and may cause performance degradation [altera-id-dependent-backward-branch,-warnings-as-errors]
                        for(; current != first; ++first) {
Run Code Online (Sandbox Code Playgroud)
uninitialized_fill_n(ForwardIt first, Size n, T const& v) {
    ForwardIt current = first;
    try {
        for(; n > 0; ++current, --n) {
            new (std::addressof(*current)) T(v);
        }
        return current;
    } catch(...) {
        for(; first != current; ++first) {  // clang-tidy error here
            std::addressof(*first)->~T();
        }
        throw; …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop clang-tidy

11
推荐指数
1
解决办法
1302
查看次数

是否可以使用 clang-tidy 忽略标头

我想包含来自外部项目的标头,但 clang-tidy 对此非常不满意,并生成了大量警告列表。为了解决这个问题,我尝试禁用来自此标头的所有诊断。

我试过:

// NOLINTBEGIN
// NOLINTNEXTLINE
#include <bad.hpp> // NOLINT
// NOLINTEND
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这不起作用。

电子邮件线程建议使用-header-filter(HeaderFilterRegex) 选项。

HeaderFilterRegex: '^((?!bad.hpp).)*$'
Run Code Online (Sandbox Code Playgroud)

但这会导致所有标头被忽略,因为 clang tidy使用POSIX 正则表达式语法。这不支持负面展望。

正如这个答案所建议的那样,我还考虑使用线路过滤器,但配置文件没有这样的选项。

有可能吗?

clang-tidy

11
推荐指数
1
解决办法
7969
查看次数

静态成员变量的 C++ 核心指南

我的类中有一个私有静态向量,它保存一个指向所有从它创建的对象的指针。这是必要的,因为每个对象都需要访问来自所有其他对象的信息来执行一些计算:

// Header file:
class Example {
public:
    Example();
private:
    static std::vector<const Example*> examples_;
};
// Cpp file:
std::vector<const Example *> Example::examples_ = {};
Example::Example() {
    // intialization
    examples_.emplace_back(this);
}
void Example::DoCalc() {
    for (auto example : examples_) {
        // do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

clang-tidy指出我违反了 C++ 核心指南,即:“变量 'examples_' 是非常量且全局可访问的,考虑将其设置为常量(cppcoreguidelines-avoid-non-const-global-variables)”。

就我个人而言,我没有看到我的代码与核心指南中的示例代码之间的相似之处,尤其是因为该变量在一个类中并且是私有的。实现此功能的“正确”方式是什么?如果可以避免,我不想从 clang-tidy 禁用此检查。

c++ static-members cpp-core-guidelines clang-tidy

10
推荐指数
2
解决办法
747
查看次数

std::accumulate 溢出警告

init当arg 的类型与您正在累积的包含类型不匹配时,是否有任何方法可以收到 std::accumulate 警告?例如,在此示例中,我们在迭代 64 位整数向量时不应累积 32 位值。但是很容易只传入 0 而忘记传递 0LL。有什么办法可以得到警告吗?-Wall -Wextra -Wconversion似乎没有帮助。我还尝试寻找可能有效的 clang tidy 检查,但也没有找到任何东西。

std::vector<long long> a = {10000000000, 10000000000};
cout << std::accumulate(a.begin(), a.end(), 0) << "\n"; // overflows
cout << std::accumulate(a.begin(), a.end(), 0LL) << "\n"; // prints 20000000000
Run Code Online (Sandbox Code Playgroud)

c++ warnings clang-tidy

10
推荐指数
1
解决办法
231
查看次数

Ubuntu 上 Studio 3.5.2 的 NDK 问题

我是 NDK 的新手。刚刚升级到Android Studio 3.5.2,在Ubuntu 16.04上安装了LLDB 3.1.4508709,NDK(并排)21.0.6113669,CMake 3.6.4111459(顺便说一句,界面很糟糕)。
从模板创建了一个新的 Native C++ 项目。
每次打开cpp文件,窗口顶部都会出现一条粉红色的消息:

无法执行 Clang-Tidy:无法为 JavaBean 创建属性=诊断=com.jetbrains.cidr.lang.daemon.clang.tidy.ClangTidyYamlLoader...

消息太长,无法适应窗口。它仅在 Ubuntu 上发生,在 Mac 上,相同的 IDE 版本工作正常。其他一切似乎都正常 - 在模拟器上构建、运行/调试。只是这个消息很烦人。有任何想法吗?

android-ndk android-studio ubuntu-16.04 clang-tidy

9
推荐指数
0
解决办法
1734
查看次数