[clang] Clarify some code based on static analysis complaints; NFC (PR #145679)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 25 10:02:10 PDT 2025
https://github.com/AaronBallman updated https://github.com/llvm/llvm-project/pull/145679
>From 80e19f02a9e6dcec3f23d52a01416c6984103f27 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Wed, 25 Jun 2025 07:31:54 -0400
Subject: [PATCH 1/2] Clarify some code based on static analysis complaints;
NFC
In one case, we have a null pointer check that's unnecessary because
the only caller of the function already asserts the value is non-null.
In the other case, we've got an anti-pattern of `is` followed by `get`.
The logic was easier to repair by changing `get` to `cast`.
Neither case is a functional change.
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 3 +--
clang/lib/Sema/SemaHLSL.cpp | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 3103f1798e14e..85e602481e647 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -589,8 +589,7 @@ void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
auto *NonUniform = llvm::ConstantInt::get(Int1Ty, false);
auto *Index = llvm::ConstantInt::get(CGM.IntTy, 0);
auto *RangeSize = llvm::ConstantInt::get(CGM.IntTy, 1);
- auto *Space =
- llvm::ConstantInt::get(CGM.IntTy, RBA ? RBA->getSpaceNumber() : 0);
+ auto *Space = llvm::ConstantInt::get(CGM.IntTy, RBA->getSpaceNumber());
Value *Name = nullptr;
llvm::Intrinsic::ID IntrinsicID =
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 506da8a361edf..4d61ced9b0787 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2418,7 +2418,7 @@ static bool CheckFloatOrHalfRepresentation(Sema *S, SourceLocation Loc,
clang::QualType PassedType) {
clang::QualType BaseType =
PassedType->isVectorType()
- ? PassedType->getAs<clang::VectorType>()->getElementType()
+ ? PassedType->castAs<clang::VectorType>()->getElementType()
: PassedType;
if (!BaseType->isHalfType() && !BaseType->isFloat32Type())
return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
>From 7d3285369e80cc1291fba68636e615663e9dd8f8 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Wed, 25 Jun 2025 13:01:48 -0400
Subject: [PATCH 2/2] Add an assert
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 85e602481e647..7d5d2e9057f9e 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -585,6 +585,7 @@ static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV,
void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
llvm::GlobalVariable *GV,
HLSLResourceBindingAttr *RBA) {
+ assert(RBA && "expect a nonnull binding attribute");
llvm::Type *Int1Ty = llvm::Type::getInt1Ty(CGM.getLLVMContext());
auto *NonUniform = llvm::ConstantInt::get(Int1Ty, false);
auto *Index = llvm::ConstantInt::get(CGM.IntTy, 0);
More information about the cfe-commits
mailing list