简体   繁体   English

如何使用 static 分析工具或编译器标志检测 C++ 代码中的“显式类型转换中的潜在精度损失”

[英]How to detect “potential loss of precision in explicit type conversion” in C++ code by using static analysis tool or compiler flag

For example:例如:

    long long a = ....;
    int b;
    b = (int) a;

Is there a gcc flag or static analysis tools like Sonarqube can detect this case?是否有像 Sonarqube 这样的分析工具可以检测到这种情况?

Searching for explicit type castings is a strange task.搜索显式类型转换是一项奇怪的任务。 The developer obviously meant that he wanted to "cut off" high bits using explicit type casting.开发人员显然意味着他想使用显式类型转换“切断”高位。 Nevertheless, I would venture to say that the task was actually different: one had to port a 32-bit application to the 64-bit platform.不过,我敢说任务实际上是不同的:必须将 32 位应用程序移植到 64 位平台。 In this case, yes, it is a very relevant task to detect dangerous explicit types castings.在这种情况下,是的,检测危险的显式类型转换是一项非常相关的任务。 If it is so, there is a solution.如果是这样,有一个解决方案。 There is a special set of diagnostics related to porting in the PVS-Studio analyzer.在 PVS-Studio 分析器中有一组与移植相关的特殊诊断。 Examples:例子:

  • V202 . V202 Explicit conversion from memsize type to 32-bit integer type.从 memsize 类型到 32 位 integer 类型的显式转换。
  • V220 . V220 Suspicious sequence of types castings: memsize -> 32-bit integer -> memsize.类型铸件的可疑序列:memsize -> 32 位 integer -> memsize。
  • V221 . V221 Suspicious sequence of types castings: pointer -> memsize -> 32-bit integer.类型转换的可疑序列:指针 -> memsize -> 32 位 integer。
  • and others .其他人

You can also try CppDepend which uses Clang as a C/C++ parser, and Clang detects such issues.您也可以尝试使用Clang作为 C/C++ 解析器的 CppDepend,并且 Clang 会检测到此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 静态分析工具,用于检测C ++中的ABI中断 - Static analysis tool to detect ABI breaks in C++ 如何在不运行静态代码分析的情况下使用 Visual C++ 在我自己的代码中引起 SAL 编译器警告 - How to cause SAL compiler warnings in my own code using Visual C++ without running static code analysis 检测纯C ++中的精度损失 - detect the loss of precision in pure C++ 在C ++中使用pow时的精度损失 - Loss of precision when using pow in C++ 在C ++中转换为显式的本机类型 - Make conversion to a native type explicit in C++ 如何知道我的编译器的默认C ++版本(无显式标志)? - How to tell default c++ version (without explicit flag) for my compiler? 显式/隐式类型转换c ++ - explicit/implicit type conversion c++ C ++强制转换显式类型转换(C样式转换)和static_cast的多种解释 - C++ cast notation of explicit type conversion (C-style cast) and multiple interpretations of static_cast 用于旧版C / C ++的代码分析工具 - Code Analysis tool for legacy C/C++ 静态代码分析工具,用于在编译之前检测C ++代码中的未捕获异常? - Static code analysis tool for detecting uncaught exceptions in a C++ code before compilation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM