================
@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy
----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+ auto IsPointerType = refersToType(qualType(isAnyPointer()));
+ Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+ hasName("::std::bit_cast"),
+ hasTemplateArgument(0, IsPointerType),
+ hasTemplateArgument(1, IsPointerType)))))
+ .bind("x"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+ if (const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("x"))
+ diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast on pointers; use it on values instead");
----------------
5chmidti wrote:
Steaming (`<<`) a source range into the diagnostic will underline the whole
range:
```
18:16: warning: do not use std::bit_cast on pointers; use it on values instead
[bugprone-bit-cast-pointers]
18 | float bad = *std::bit_cast<float*>(&x); // UB, but looks safe due to
std::bit_cast
| ^
```
vs
```
18:16: warning: do not use std::bit_cast on pointers; use it on values instead
[bugprone-bit-cast-pointers]
18 | float bad = *std::bit_cast<float*>(&x); // UB, but looks safe due to
std::bit_cast
| ^~~~~~~~~~~~~~~~~~~~~~~~~
```
```c++
diag(MatchedDecl->getBeginLoc(),
"do not use std::bit_cast on pointers; use it on values instead")
<< MatchedDecl.getSourceRange();
```
In case of FixItHint's, their replacement/deletion ranges are also highlighted
in this way.
https://github.com/llvm/llvm-project/pull/108083
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits