标签: clang-tidy

"使用带有二进制位运算符的带符号整数操作数" - 使用无符号短整型时

在下面的C片段中,检查是否设置了16位序列的前两位:

bool is_pointer(unsigned short int sequence) {
  return (sequence >> 14) == 3;
}
Run Code Online (Sandbox Code Playgroud)

CLion的Clang-Tidy给我一个"使用带有二进制位运算符的有符号整数操作数"警告,我无法理解为什么.是unsigned short不是没有签名?

c clion clang-tidy

34
推荐指数
3
解决办法
8721
查看次数

在clang-tidy中忽略系统头

tldr;>如何在clang-tidy中隐藏系统头文件中的警告?

我有以下最小示例源文件,它会在系统标头中触发一个铿锵有力的警告:

#include <future>

int main() {
  std::promise<int> p;
  p.set_value(3);
}
Run Code Online (Sandbox Code Playgroud)

在Ubuntu 17.04上使用clang-tidy 4.0.0使用libstdc ++ 7.0.1调用它:

$ clang-tidy main.cpp -extra-arg=-std=c++14
Run Code Online (Sandbox Code Playgroud)

产量

Running without flags.
1 warning generated.
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:693:5: warning: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller.  This will be a dangling reference [clang-analyzer-core.StackAddressEscape]
    }
    ^
/home/user/main.cpp:5:3: note: Calling 'promise::set_value'
  p.set_value(3);
  ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:1094:9: note: Calling '_State_baseV2::_M_set_result'
      { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); }
        ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:401:2: note: Calling 'call_once' …
Run Code Online (Sandbox Code Playgroud)

c++ clang clang-static-analyzer libtooling clang-tidy

19
推荐指数
2
解决办法
5460
查看次数

内联方式禁用铿锵的检查

我正试图为一个项目设置铿锵声.我希望能够提供干净的输出,并鼓励尽可能使用-fix模式.但是,在某些情况下需要例外.

很有可能使用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
// Code that is being specially exempted
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

对于想要在本地禁用编译器警告的等效情况,是否可以通过clang-tidy执行类似的操作?

我试过了

#pragma clang diagnostic push
#pragma clang diagnostic ignored "readability-identifier-naming"
// Code that is being specially exempted
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

并且还clang替换为clang-tidy.不幸的是,当clang用作pragma目标并使用常规clang进行编译时,我得到了编译警告

warning: pragma diagnostic expected option name (e.g. "-Wundef") [-Wunknown-pragmas]
Run Code Online (Sandbox Code Playgroud)

warning: unknown pragma ignored [clang-diagnostic-unknown-pragmas]
Run Code Online (Sandbox Code Playgroud)

编译时,如果我用来clang-tidy代替clang.clang-tidy在源上运行时,都不会对自身的输出产生影响.

这与x86_64 Linux上的3.8 clangclang-tidy.

c++ clang++ clang-tidy

17
推荐指数
2
解决办法
7557
查看次数

如何建立最新的铿锵声?

我试图从源代码构建clang-tidy,但它抱怨一个未定义的CMake命令:

CMake Error at clang-apply-replacements/CMakeLists.txt:5 (add_clang_library):
  Unknown CMake command "add_clang_library".


CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.9)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

我怎样才能构建铿锵有力的,或者如何在macOS上安装最新版本?

c++ llvm clang clang-tidy

16
推荐指数
4
解决办法
5312
查看次数

Clang-tidy 文件:如何在多行中列出检查

现在我有一个.clang-tidy包含大量检查列表的文件,它们都在一行中,如下所示:

Checks: '-*,bugprone-*,-bugprone-narrowing-conversions, cert-*, -cert-err58-cpp, clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-narrowing-conversions...'
Run Code Online (Sandbox Code Playgroud)

有没有办法在多行中列出每个检查(启用或禁用)以便于版本控制?

现在我切换自动换行,这有助于编辑,但在代码审查中很难区分。

我正在寻找这样的东西:

Checks:
'-*,'
'cert-*,etc-*,'
...
Run Code Online (Sandbox Code Playgroud)

c++ clang-tidy

15
推荐指数
1
解决办法
725
查看次数

clang-tidy - 忽略第三方标题代码

我正在为我的项目使用 CMake,我想为项目引入 clang-tidy 检查。

我使用这个目的CMAKE_CXX_CLANG_TIDY.clang-tidy文件检查设置。

我想使用警告作为错误在 CI 中有可靠的方法来检查提交是否引入了一些新的违规行为。不幸的是,由于 3rd 方库,我在启用检查时遇到了一些问题。例如,我使用Eigenwhich 是仅标头库。由于这个事实,我在我的代码中收到了一些警告,例如。“a_file.cpp”

/snap/cmake/301/bin/cmake -E __run_co_compile --tidy="clang-tidy;--extra-arg-before=--driver-mode=g++" --source=../../a_file.cpp -- /usr/bin/clang++ -DEIGEN_MPL2_ONLY -DEIGEN_STACK_ALLOCATION_LIMIT=16384 -I../../SomePath -I../../SomePath2 -isystem ../../path_to/include/Eigen -m64 -stdlib=libc++ -g -fPIC -std=c++11 -MD -MT a_file.cpp.o -MF a_file.cpp.o.d -o a_file.cpp.o -c a_file.cpp                                                                            
../../path_to/include/Eigen/Eigen/src/Core/Swap.h:89:36: error: Assigned value is garbage or undefined [clang-analyzer-core.uninitialized.Assign,-warnings-as-errors] 
a_file.cpp:279:5: note: Loop condition is true.  Entering loop body                            
    for( unsigned int i = 0; i < 100; ++i )                                                                                                                                                                                                                         
    ^                                                                                                                                                                                                                                                                 
a_file.cpp:282:13: note: Calling move assignment operator …
Run Code Online (Sandbox Code Playgroud)

c++ cmake clang clang-tidy

14
推荐指数
1
解决办法
2228
查看次数

cmake clang-tidy(或其他脚本)作为自定义目标

我正在尝试为clang-tidy创建一个自定义cmake目标,以便对我的项目进行lint.源文件夹看起来像这样:

src/scripts/run-clang-tidy.py
src/.clang-tidy
src/...
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的计划是使用自定义命令将这两个文件复制到构建目录:

add_custom_command(
    OUTPUT run-clang-tidy.py .clang-tidy
    COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy.py ${CMAKE_SOURCE_DIR}/.clang-tidy ${CMAKE_CURRENT_BINARY_DIR})
Run Code Online (Sandbox Code Playgroud)

我现在想要run-clang-tidy.py在构建目录(应该是工作目录)中调用自定义目标,以便我可以调用:

make lint
Run Code Online (Sandbox Code Playgroud)

哪个应该运行指定的检查.clang-tidy.

要使此脚本起作用,它还需要该CMAKE_EXPORT_COMPILE_COMMANDS选项.我尝试使用以下命令设置它,但它无法识别它:

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
Run Code Online (Sandbox Code Playgroud)

电话add_custom_target怎么样?

lint cmake clang target clang-tidy

13
推荐指数
3
解决办法
9402
查看次数

如何在MacOS上安装clang-tidy?

你如何在MacOS上安装clang-tidy

在MacOS上安装clang-format(使用brew)似乎很容易,但是如果没有安装并且从源代码构建所有clang和构建它似乎更难安装clang-tidy.有更好的选择吗?

macos clang-tidy

13
推荐指数
2
解决办法
4292
查看次数

使用 clang-tidy 时忽略某些文件

我正在尝试clang-tidy与集成cmake,但有些文件属于我想忽略的特定目标。

有没有办法让 clang-tidy 忽略特定目录下的文件或名称与特定模式匹配的文件?

static-analysis cmake clang-static-analyzer clang-tidy

13
推荐指数
1
解决办法
1158
查看次数

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

经过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
查看次数