经过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)
这段代码编译,我能够对它进行单元测试.但是我非常希望摆脱这个警告(为了理解我做错了什么,即使它编译了).
谢谢
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 …
我正在将当前使用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) 这里是 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"
我正在尝试从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中转储每个文件的包含?任何前进的方式都可以.
以供参考:
我试图理解这个铿锵有力的警告: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) 我想包含来自外部项目的标头,但 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 正则表达式语法。这不支持负面展望。
正如这个答案所建议的那样,我还考虑使用线路过滤器,但配置文件没有这样的选项。
有可能吗?
我的类中有一个私有静态向量,它保存一个指向所有从它创建的对象的指针。这是必要的,因为每个对象都需要访问来自所有其他对象的信息来执行一些计算:
// 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 禁用此检查。
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) 我是 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 版本工作正常。其他一切似乎都正常 - 在模拟器上构建、运行/调试。只是这个消息很烦人。有任何想法吗?
clang-tidy ×10
c++ ×7
clang ×2
android-ndk ×1
c++11 ×1
clang++ ×1
clang-format ×1
cmake ×1
exception ×1
for-loop ×1
llvm-clang ×1
std ×1
ubuntu-16.04 ×1
warnings ×1