我无法让 clang-tidy 读取我的编译数据库。如果我尝试:
clang-tidy --config-file ./.clang-tidy -checks=* -p ./target
Run Code Online (Sandbox Code Playgroud)
或者
clang-tidy --config-file ./.clang-tidy -checks=* -p ./target/compile-commands.json
Run Code Online (Sandbox Code Playgroud)
我明白了
Error: no input files specified.
USAGE: clang-tidy [options] <source0> [... <sourceN>]
...
Run Code Online (Sandbox Code Playgroud)
但根据 --help -p 的方式来指定它的路径。它似乎只有在我指定单个文件时才有效,如下所示:
clang-tidy --config-file ./.clang-tidy -checks=* path/to/file.cpp
Run Code Online (Sandbox Code Playgroud) 我能够通过 epel 存储库安装 llvm 和 clang。但是,它们不包括 clang-tidy 二进制文件。我一直在搜索 llvm 网站,但他们没有任何可用于 CentOS 7 的软件包。 ius 存储库也没有 clang-tidy。我想在从源代码构建之前仔细检查。谢谢你。
我已经为开源 Traffic Server 项目创建了一个 PR。作为 CI 的一部分,他们运行得井井有条。我的更改向 clang-tidy 公开了一个新文件,该文件现在被标记为移动后使用警告。这是测试代码,固定在我的更改所基于的提交上:
https://github.com/apache/trafficserver/blob/415cd8f/tests/tools/plugins/test_cppapi.cc#L115
这是该代码的副本,添加了一条注释,显示了它抱怨的地方:
void
f()
{
TestCont::Mutex m(TSMutexCreate());
TestCont c(m);
ALWAYS_ASSERT(!!c)
ALWAYS_ASSERT(c.asTSCont() != nullptr)
ALWAYS_ASSERT(c.mutex() == m)
TestCont c2(std::move(c));
ALWAYS_ASSERT(!!c2)
ALWAYS_ASSERT(c2.asTSCont() != nullptr) // <--- Complains here
ALWAYS_ASSERT(c2.mutex() == m)
ALWAYS_ASSERT(!c)
ALWAYS_ASSERT(c.asTSCont() == nullptr)
ALWAYS_ASSERT(c.mutex() == nullptr)
Run Code Online (Sandbox Code Playgroud)
所以抱怨是有道理的,c2是在走棋之后使用的。但在这种情况下,TestCont通过设计明确支持移动后使用,并且测试有意执行此操作以确保其状态符合预期。
NOLINT因此,和就是为这种情况而NOLINTNEXTLINE创建的。所以我应用了这样的评论(显然,出于绝望,我添加了比我应该需要的更多的评论):
// NOLINTNEXTLINE
ALWAYS_ASSERT(!c) // NOLINT
// We turn off the clang-tidy warning about this being a use after move
// because that is the intention of the test. …Run Code Online (Sandbox Code Playgroud) 我正在使用 clang-tidy 分析代码库并看到一个我不明白的警告。警告由以下代码行调用:
void fun(const QString& bar) {
const char* c_format = bar.toStdString().c_str();
expand_some_macro(c_format);
}
Run Code Online (Sandbox Code Playgroud)
c_format 在扩展宏中传递,其内容如下:
#define expand_some_macro(c_format)\
char buffer[MAX_SIZE];\
va_list args;\
va_start(args, c_format);\
vsnprintf(buffer, MAX_SIZE, _TRUNCATE, c_format, args);\
va_end(args);
Run Code Online (Sandbox Code Playgroud)
其中包括来自shlobj标题的函数调用,我目前不明白。将clang-tidy生成以下警告:
warning: object backing the pointer will be destroyed at the end of the full-expression [clang-diagnostic-dangling-gsl]
Run Code Online (Sandbox Code Playgroud)
我浏览了网络,特别是c++ 核心指南,试图让自己了解这个警告,但找不到合适的参考。这让我想到了两组问题:
delete[] c_format在范围结束时调用吗?我libffi在 C 中使用并收到一条警告,clang-tidy说我不太明白。
我在堆上为类型的元素数组分配内存ffi_type:
ffi_type **arg_types = malloc(num_total_args * sizeof(ffi_type));
Run Code Online (Sandbox Code Playgroud)
并收到以下警告:
Result of 'malloc' is converted to a pointer of type 'ffi_type *', which is incompatible with sizeof operand type 'ffi_type'
Run Code Online (Sandbox Code Playgroud)
有人可以给我提示,如何修复它吗?的实际定义ffi_type是
ffi_type **arg_types = malloc(num_total_args * sizeof(ffi_type));
Run Code Online (Sandbox Code Playgroud)
实际上我不清楚为什么它与 sizeof 运算符不兼容。
更新:@IanAbbot、@Toby Speight、@dbush 的有用评论帮助我意识到了这个(实际上很愚蠢)问题。一定是
ffi_type **arg_types = malloc(num_total_args * sizeof(ffi_type *));
Run Code Online (Sandbox Code Playgroud)
(注意最后一个星号),因为数组的元素类型为ffi_type *。
我尝试clang-tidy在Windows 上使用-到目前为止没有成功。
作为前提,我已经安装了LLVM 3.9.1。
我的常规编译器是MSVC 2015,我的构建系统是QBS 1.7.2。使用QBS我已经生成了compile_commands.json。当然,该文件包含MSVC 2015 cl.exe标志/参数。以下摘录显示了一个文件的条目:
{
"arguments": [
"C:/Programs/MVS14/VC/bin/amd64/cl.exe",
"/nologo",
"/c",
"/EHsc",
"/Zi",
"/MDd",
"/IC:\\some_ci_job_folder\\src\\some_component\\include",
"/IC:\\some_ci_job_folder\\src\\some_needed_component\\include",
"/IC:\\Programs\\Qt\\Online\\5.6\\msvc2015_64\\include",
"/IC:\\Programs\\Qt\\Online\\5.6\\msvc2015_64\\include\\QtCore",
"/IC:\\Programs\\Qt\\Online\\5.6\\msvc2015_64\\mkspecs\\win32-msvc2015",
"/IC:\\some_ci_job_folder\\target\\build\\win64-vc14-qt56x-debug\\some_component.win64-vc14-qt56x.ce20db24\\qt.headers",
"/D_MBCS",
"/DWIN32",
"/DQT_CORE_LIB",
"/DNOMINMAX",
"/DQT_DISABLE_DEPRECATED_BEFORE=0x000000",
"/DVERSION_ID=\"MAJOR_UNDEF.MINOR_UNDEF-REVISION_UNDEF\"",
"/DVERSION_MAJOR=0",
"/DVERSION_MINOR=0",
"/DVERSION_REVISION=0",
"/DUSES_QT",
"/D_DEBUG",
"/D__mswin__",
"/DUSE_RVALUE_REFS=1",
"/D_CRT_SECURE_NO_DEPRECATE",
"/D_SCL_SECURE_NO_WARNINGS",
"/DWINVER=0x0502",
"/D_WIN32_WINNT=0x0502",
"/D_WIN32_WINDOWS=0x0502",
"/TP",
"/FS",
"/Zm200",
"/W4",
"/FS",
"/GR",
"/wd4018",
"/wd4063",
"/wd4100",
"/wd4121",
"/wd4127",
"/wd4150",
"/wd4189",
"/wd4238",
"/wd4239",
"/wd4244",
"/wd4245",
"/wd4267",
"/wd4290",
"/wd4389",
"/wd4510",
"/wd4512",
"/wd4610",
"/wd4718",
"/wd4481",
"/FoC:\\some_ci_job_folder\\target\\build\\win64-vc14-qt56x-debug\\some_component.win64-vc14-qt56x.ce20db24\\.obj\\f27fede2220bcd32\\foo.cpp.obj",
"/FdC:\\some_ci_job_folder\\target\\build\\win64-vc14-qt56x-debug\\some_component.win64-vc14-qt56x.ce20db24\\.obj\\f27fede2220bcd32\\", …Run Code Online (Sandbox Code Playgroud) 我从 Clang-Tidy 收到以下消息:
Clang-Tidy: Calling a base constructor other than the copy constructor
Run Code Online (Sandbox Code Playgroud)
代码是这样的:
#include <memory>
#include <set>
using namespace std;
class Node : enable_shared_from_this<Node>
{
public:
explicit Node(Node *parent = nullptr) : parent(parent) { parent->children.insert(shared_from_this()); }
Node(const Node &that) : parent(that.parent), children(that.children) {}
private:
Node *parent; // C++14 "weak pointer"
set<shared_ptr<Node>> children;
};
Run Code Online (Sandbox Code Playgroud)
我在任何地方都找不到来自 Clang-Tidy 的此消息的文档。
它是什么意思以及为什么要给予它?
执行检查时:
std::string st = "hello";
bool is_empty = st.size() > 0;
Run Code Online (Sandbox Code Playgroud)
我收到了上面提到的 Clang-Tidy 警告。
为什么最好使用该empty()方法?
该from_base函数将内存地址从基址返回到程序中选定的值。我想检索该值并将其返回到函数中,但是,我收到一条警告,提示integer to pointer cast pessimism optimization opportunities.
DWORD chat::client() {
return *reinterpret_cast<DWORD*>(core::from_base(offsets::chat::client));
}
Run Code Online (Sandbox Code Playgroud)
从程序中转换函数时我也收到此警告:
auto og_print = reinterpret_cast<chat::fn_print_chat>(core::from_base(offsets::chat::print));
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到 clang-tidy 的警告integer to pointer cast pessimism optimization opportunities
我查了一下,但我无法弄清楚。该代码有效,并获得正确的值。我只是担心这个警告。
clang-tidy ×9
c++ ×6
clang ×2
c ×1
c++14 ×1
centos7 ×1
containers ×1
llvm-clang ×1
pointers ×1
std ×1
stdvector ×1
warnings ×1
windows ×1