[clang] [analyzer][NFC] Clean up `MemRegion.cpp` and align with llvm coding style (PR #218860)
Shengxin Pei via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 26 03:24:38 PDT 2026
https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/218860
>From d354e8c9cfb97bcb48e5a5c24d759418ff4e5675 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Wed, 26 Aug 2026 16:25:35 +0800
Subject: [PATCH 1/2] [analyzer][NFC] Clean up `MemRegion.cpp` and align with
llvm coding style
---
clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 44 +++++++++------------
1 file changed, 19 insertions(+), 25 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
index 36a71d510b902..188db163525a7 100644
--- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
+++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
@@ -207,22 +207,23 @@ QualType ParamVarRegion::getValueType() const {
const ParmVarDecl *ParamVarRegion::getDecl() const {
const Decl *D = getStackFrame()->getDecl();
-
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
assert(Index < FD->param_size());
return FD->parameters()[Index];
- } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
+ }
+ if (const auto *BD = dyn_cast<BlockDecl>(D)) {
assert(Index < BD->param_size());
return BD->parameters()[Index];
- } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
+ }
+ if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
assert(Index < MD->param_size());
return MD->parameters()[Index];
- } else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) {
+ }
+ if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) {
assert(Index < CD->param_size());
return CD->parameters()[Index];
- } else {
- llvm_unreachable("Unexpected Decl kind!");
}
+ llvm_unreachable("Unexpected Decl kind!");
}
//===----------------------------------------------------------------------===//
@@ -622,11 +623,10 @@ void ParamVarRegion::dumpToStream(raw_ostream &os) const {
assert(PVD &&
"`ParamVarRegion` support functions without `Decl` not implemented"
" yet.");
- if (const IdentifierInfo *ID = PVD->getIdentifier()) {
+ if (const IdentifierInfo *ID = PVD->getIdentifier())
os << ID->getName();
- } else {
+ else
os << "ParamVarRegion{P" << PVD->getID() << '}';
- }
}
bool MemRegion::canPrintPretty() const {
@@ -808,13 +808,11 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes,
SourceRange MemRegion::sourceRange() const {
// Check for more specific regions first.
- if (auto *FR = dyn_cast<FieldRegion>(this)) {
+ if (auto *FR = dyn_cast<FieldRegion>(this))
return FR->getDecl()->getSourceRange();
- }
- if (auto *VR = dyn_cast<VarRegion>(this->getBaseRegion())) {
+ if (auto *VR = dyn_cast<VarRegion>(this->getBaseRegion()))
return VR->getDecl()->getSourceRange();
- }
// Return invalid source range (can be checked by client).
return {};
@@ -925,18 +923,16 @@ DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
template <typename REG>
const REG *MemRegionManager::LazyAllocate(REG*& region) {
- if (!region) {
+ if (!region)
region = new (A) REG(*this);
- }
return region;
}
template <typename REG, typename ARG>
const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
- if (!region) {
+ if (!region)
region = new (A) REG(this, a);
- }
return region;
}
@@ -1065,13 +1061,12 @@ const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
if (CallSite) {
const Decl *CalleeDecl = SF->getDecl();
bool CurrentParam = true;
- if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) {
+ if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
CurrentParam =
(Index < FD->param_size() && FD->getParamDecl(Index) == PVD);
- } else if (const auto *BD = dyn_cast<BlockDecl>(CalleeDecl)) {
+ else if (const auto *BD = dyn_cast<BlockDecl>(CalleeDecl))
CurrentParam =
(Index < BD->param_size() && BD->getParamDecl(Index) == PVD);
- }
if (CurrentParam) {
// If this is a parameter of the *current* stack frame, we can
@@ -1111,11 +1106,10 @@ const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
// so they are placed in the global internal space, which is not
// invalidated by calls to functions declared in system headers.
if (Ctx.getSourceManager().isInSystemHeader(D->getLocation()) &&
- !isStdStreamVar(D)) {
+ !isStdStreamVar(D))
sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
- } else {
+ else
sReg = getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind);
- }
}
// Finally handle static locals.
@@ -1230,9 +1224,9 @@ MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
const StackFrame *SF) {
const MemSpaceRegion *sReg = nullptr;
- if (CL->isFileScope())
+ if (CL->isFileScope()) {
sReg = getGlobalsRegion();
- else {
+ } else {
assert(SF);
sReg = getStackLocalsRegion(SF);
}
>From de6b6d64c21fc449a3474504efe92ea168456625 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Wed, 26 Aug 2026 18:24:20 +0800
Subject: [PATCH 2/2] update
---
clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
index 188db163525a7..f565bafc7479d 100644
--- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
+++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
@@ -808,11 +808,13 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes,
SourceRange MemRegion::sourceRange() const {
// Check for more specific regions first.
- if (auto *FR = dyn_cast<FieldRegion>(this))
+ if (auto *FR = dyn_cast<FieldRegion>(this)) {
return FR->getDecl()->getSourceRange();
+ }
- if (auto *VR = dyn_cast<VarRegion>(this->getBaseRegion()))
+ if (auto *VR = dyn_cast<VarRegion>(this->getBaseRegion())) {
return VR->getDecl()->getSourceRange();
+ }
// Return invalid source range (can be checked by client).
return {};
@@ -1061,12 +1063,13 @@ const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
if (CallSite) {
const Decl *CalleeDecl = SF->getDecl();
bool CurrentParam = true;
- if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
+ if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) {
CurrentParam =
(Index < FD->param_size() && FD->getParamDecl(Index) == PVD);
- else if (const auto *BD = dyn_cast<BlockDecl>(CalleeDecl))
+ } else if (const auto *BD = dyn_cast<BlockDecl>(CalleeDecl)) {
CurrentParam =
(Index < BD->param_size() && BD->getParamDecl(Index) == PVD);
+ }
if (CurrentParam) {
// If this is a parameter of the *current* stack frame, we can
More information about the cfe-commits
mailing list