forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* commit 'FETCH_HEAD': [X86] combineAndLoadToBZHI - don't do an return early return if we fail to match a load [X86] replace-load-and-with-bzhi.ll - add commuted test cases to show failure to fold [X86] replace-load-and-with-bzhi.ll - cleanup check-prefixes to use X86/X64 for 32/64-bit targets [ExecutionEngine] Avoid repeated hash lookups (NFC) (llvm#111275) [ByteCode] Avoid repeated hash lookups (NFC) (llvm#111273) [StaticAnalyzer] Avoid repeated hash lookups (NFC) (llvm#111272) [CodeGen] Avoid repeated hash lookups (NFC) (llvm#111274) [RISCV] Simplify fixed-vector-fp.ll run lines. NFC [libc++][format][1/3] Adds more benchmarks. (llvm#101803) [X86] combineOrXorWithSETCC - avoid duplicate SDLoc/operands code. NFC. [X86] convertIntLogicToFPLogic - avoid duplicate SDLoc/operands code. NFC. [libc] Clean up some include in `libc`. (llvm#110980) [X86] combineBitOpWithPACK - avoid duplicate SDLoc/operands code. NFC. [X86] combineBitOpWithMOVMSK - avoid duplicate SDLoc/operands code. NFC. [X86] combineBitOpWithShift - avoid duplicate SDLoc/operands code. NFC. [x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat. [X86] combineSubABS - avoid duplicate SDLoc. NFC. [ValueTypes][RISCV] Add v1bf16 type (llvm#111112) [VPlan] Add additional FOR hoisting test. [clang-tidy] Create bugprone-bitwise-pointer-cast check (llvm#108083) [InstCombine] Canonicalize more geps with constant gep bases and constant offsets. (llvm#110033) [LV] Honor uniform-after-vectorization in setVectorizedCallDecision. [ELF] Pass Ctx & to Arch/ [ELF] Pass Ctx & to Arch/ [libc++] Fix a typo (llvm#111239) [X86] For minsize memset/memcpy, use byte or double-word accesses (llvm#87003) [RISCV] Unify RVBShift_ri and RVBShiftW_ri with Shift_ri and ShiftW_ri. NFC (llvm#111263) Revert "Reapply "[AMDGPU][GlobalISel] Fix load/store of pointer vectors, buffer.*.pN (llvm#110714)" (llvm#111059)" [libc] Add missing include to __support/StringUtil/tables/stdc_errors.h. (llvm#111271) [libc] remove errno.h includes (llvm#110934) [NFC][rtsan] Update docs to include [[clang::blocking]] (llvm#111249) [RISCV] Give ZEXT_H_RV32 and ZEXT_H_RV64 R-type format to match PACK. NFC [mlir][SPIRV] Fix build (2) (llvm#111265) [mlir][SPIRV] Fix build error (llvm#111264) [mlir][NFC] Mark type converter in `populate...` functions as `const` (llvm#111250) [Basic] Avoid repeated hash lookups (NFC) (llvm#111228) [RISCV] Use THShift_ri class instead of RVBShift_ri for TH_TST instruction. NFC [VPlan] Only generate first lane for VPPredInstPHI if no others used. [ELF] Don't call getPPC64TargetInfo outside Driver. NFC [GISel] Don't preserve NSW flag when converting G_MUL of INT_MIN to G_SHL. (llvm#111230) [APInt] Slightly simplify APInt::ashrSlowCase. NFC (llvm#111220) [Sema] Avoid repeated hash lookups (NFC) (llvm#111227) [Affine] Avoid repeated hash lookups (NFC) (llvm#111226) [Driver] Avoid repeated hash lookups (NFC) (llvm#111225) [clang][test] Remove a broken bytecode test [ELF] Pass Ctx & [ELF] Pass Ctx & to Relocations Signed-off-by: kyvangka1610 <[email protected]>
- Loading branch information
Showing
361 changed files
with
5,755 additions
and
7,113 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===--- BitwisePointerCastCheck.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 "BitwisePointerCastCheck.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) { | ||
if (getLangOpts().CPlusPlus20) { | ||
auto IsPointerType = refersToType(qualType(isAnyPointer())); | ||
Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf( | ||
hasName("::std::bit_cast"), | ||
hasTemplateArgument(0, IsPointerType), | ||
hasTemplateArgument(1, IsPointerType))))) | ||
.bind("bit_cast"), | ||
this); | ||
} | ||
|
||
auto IsDoublePointerType = | ||
hasType(qualType(pointsTo(qualType(isAnyPointer())))); | ||
Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType), | ||
hasArgument(1, IsDoublePointerType), | ||
hasDeclaration(functionDecl(hasName("::memcpy")))) | ||
.bind("memcpy"), | ||
this); | ||
} | ||
|
||
void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) { | ||
if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("bit_cast")) | ||
diag(Call->getBeginLoc(), | ||
"do not use 'std::bit_cast' to cast between pointers") | ||
<< Call->getSourceRange(); | ||
else if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("memcpy")) | ||
diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers") | ||
<< Call->getSourceRange(); | ||
} | ||
|
||
} // namespace clang::tidy::bugprone |
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- BitwisePointerCastCheck.h - clang-tidy -----------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
/// Warns about code that tries to cast between pointers by means of | ||
/// ``std::bit_cast`` or ``memcpy``. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html | ||
class BitwisePointerCastCheck : public ClangTidyCheck { | ||
public: | ||
BitwisePointerCastCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::bugprone | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
clang-tools-extra/docs/clang-tidy/checks/bugprone/bitwise-pointer-cast.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. title:: clang-tidy - bugprone-bitwise-pointer-cast | ||
|
||
bugprone-bitwise-pointer-cast | ||
============================= | ||
|
||
Warns about code that tries to cast between pointers by means of | ||
``std::bit_cast`` or ``memcpy``. | ||
|
||
The motivation is that ``std::bit_cast`` is advertised as the safe alternative | ||
to type punning via ``reinterpret_cast`` in modern C++. However, one should not | ||
blindly replace ``reinterpret_cast`` with ``std::bit_cast``, as follows: | ||
|
||
.. code-block:: c++ | ||
|
||
int x{}; | ||
-float y = *reinterpret_cast<float*>(&x); | ||
+float y = *std::bit_cast<float*>(&x); | ||
|
||
The drop-in replacement behaves exactly the same as ``reinterpret_cast``, and | ||
Undefined Behavior is still invoked. ``std::bit_cast`` is copying the bytes of | ||
the input pointer, not the pointee, into an output pointer of a different type, | ||
which may violate the strict aliasing rules. However, simply looking at the | ||
code, it looks "safe", because it uses ``std::bit_cast`` which is advertised as | ||
safe. | ||
|
||
The solution to safe type punning is to apply ``std::bit_cast`` on value types, | ||
not on pointer types: | ||
|
||
.. code-block:: c++ | ||
|
||
int x{}; | ||
float y = std::bit_cast<float>(x); | ||
|
||
This way, the bytes of the input object are copied into the output object, which | ||
is much safer. Do note that Undefined Behavior can still occur, if there is no | ||
value of type ``To`` corresponding to the value representation produced. | ||
Compilers may be able to optimize this copy and generate identical assembly to | ||
the original ``reinterpret_cast`` version. | ||
|
||
Code before C++20 may backport ``std::bit_cast`` by means of ``memcpy``, or | ||
simply call ``memcpy`` directly, which is equally problematic. This is also | ||
detected by this check: | ||
|
||
.. code-block:: c++ | ||
|
||
int* x{}; | ||
float* y{}; | ||
std::memcpy(&y, &x, sizeof(x)); | ||
|
||
Alternatively, if a cast between pointers is truly wanted, ``reinterpret_cast`` | ||
should be used, to clearly convey the intent and enable warnings from compilers | ||
and linters, which should be addressed accordingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast-cxx20.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// RUN: %check_clang_tidy -std=c++20 %s bugprone-bitwise-pointer-cast %t | ||
|
||
void memcpy(void* to, void* dst, unsigned long long size) | ||
{ | ||
// Dummy implementation for the purpose of the test | ||
} | ||
|
||
namespace std | ||
{ | ||
template <typename To, typename From> | ||
To bit_cast(From from) | ||
{ | ||
// Dummy implementation for the purpose of the test | ||
To to{}; | ||
return to; | ||
} | ||
|
||
using ::memcpy; | ||
} | ||
|
||
void pointer2pointer() | ||
{ | ||
int x{}; | ||
float bad = *std::bit_cast<float*>(&x); // UB, but looks safe due to std::bit_cast | ||
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::bit_cast' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
float good = std::bit_cast<float>(x); // Well-defined | ||
|
||
using IntPtr = int*; | ||
using FloatPtr = float*; | ||
IntPtr x2{}; | ||
float bad2 = *std::bit_cast<FloatPtr>(x2); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use 'std::bit_cast' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
} | ||
|
||
void pointer2pointer_memcpy() | ||
{ | ||
int x{}; | ||
int* px{}; | ||
float y{}; | ||
float* py{}; | ||
|
||
memcpy(&py, &px, sizeof(px)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'memcpy' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
std::memcpy(&py, &px, sizeof(px)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'memcpy' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
|
||
std::memcpy(&y, &x, sizeof(x)); | ||
} | ||
|
||
// Pointer-integer conversions are allowed by this check | ||
void int2pointer() | ||
{ | ||
unsigned long long addr{}; | ||
float* p = std::bit_cast<float*>(addr); | ||
std::memcpy(&p, &addr, sizeof(addr)); | ||
} | ||
|
||
void pointer2int() | ||
{ | ||
float* p{}; | ||
auto addr = std::bit_cast<unsigned long long>(p); | ||
std::memcpy(&addr, &p, sizeof(p)); | ||
} |
41 changes: 41 additions & 0 deletions
41
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// RUN: %check_clang_tidy %s bugprone-bitwise-pointer-cast %t | ||
|
||
void memcpy(void* to, void* dst, unsigned long long size) | ||
{ | ||
// Dummy implementation for the purpose of the test | ||
} | ||
|
||
namespace std | ||
{ | ||
using ::memcpy; | ||
} | ||
|
||
void pointer2pointer() | ||
{ | ||
int x{}; | ||
int* px{}; | ||
float y{}; | ||
float* py{}; | ||
|
||
memcpy(&py, &px, sizeof(px)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'memcpy' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
std::memcpy(&py, &px, sizeof(px)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'memcpy' to cast between pointers [bugprone-bitwise-pointer-cast] | ||
|
||
std::memcpy(&y, &x, sizeof(x)); | ||
} | ||
|
||
// Pointer-integer conversions are allowed by this check | ||
void int2pointer() | ||
{ | ||
unsigned long long addr{}; | ||
float* p{}; | ||
std::memcpy(&p, &addr, sizeof(addr)); | ||
} | ||
|
||
void pointer2int() | ||
{ | ||
unsigned long long addr{}; | ||
float* p{}; | ||
std::memcpy(&addr, &p, sizeof(p)); | ||
} |
Oops, something went wrong.