[clang] [clang][analyzer] Less redundant warnings from FixedAddressChecker (PR #110458)

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 2 01:33:03 PDT 2024


https://github.com/balazske updated https://github.com/llvm/llvm-project/pull/110458

>From eb03076eca550ea53143bc753639f22bbb7caa35 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Mon, 30 Sep 2024 09:19:52 +0200
Subject: [PATCH 1/2] [clang][analyzer] Less redundant warnings from
 FixedAddressChecker

If a fixed value is assigned to a pointer variable, the checker
did emit a warning. If the pointer variable is assigned to another
pointer variable, this resulted in another warning.
The checker now emits warning only if a value with non-pointer type is assigned
(to a pointer variable).
---
 clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp | 6 ++++++
 clang/test/Analysis/ptr-arith.c                           | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
index e7fde3edc7f9ee..87ba8e070b328a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
@@ -43,6 +43,12 @@ void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
   if (!T->isPointerType())
     return;
 
+  // Omit warning if the RHS has already pointer type.
+  // The value may come from a variable and is candidate for a previous warning
+  // from the checker.
+  if (B->getRHS()->IgnoreParenCasts()->getType()->isPointerType())
+    return;
+
   SVal RV = C.getSVal(B->getRHS());
 
   if (!RV.isConstant() || RV.isZeroConstant())
diff --git a/clang/test/Analysis/ptr-arith.c b/clang/test/Analysis/ptr-arith.c
index 020a5006292306..b5ccd2c207ead1 100644
--- a/clang/test/Analysis/ptr-arith.c
+++ b/clang/test/Analysis/ptr-arith.c
@@ -42,6 +42,10 @@ domain_port (const char *domain_b, const char *domain_e,
 void f4(void) {
   int *p;
   p = (int*) 0x10000; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
+
+  int *p1;
+  p1 = p; // no warning
+
   long x = 0x10100;
   x += 10;
   p = (int*) x; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}

>From 5ea33cfb1651a65b82dd21925c2e47325e090fd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Wed, 2 Oct 2024 10:32:56 +0200
Subject: [PATCH 2/2] commit suggestion: change comment
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Donát Nagy <donat.nagy at ericsson.com>
---
 clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
index 87ba8e070b328a..f7fd92db7757e5 100644
--- a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
@@ -43,9 +43,9 @@ void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
   if (!T->isPointerType())
     return;
 
-  // Omit warning if the RHS has already pointer type.
-  // The value may come from a variable and is candidate for a previous warning
-  // from the checker.
+  // Omit warning if the RHS has already pointer type. Without this passing
+  // around one fixed value in several pointer variables would produce several
+  // redundant warnings.
   if (B->getRHS()->IgnoreParenCasts()->getType()->isPointerType())
     return;
 



More information about the cfe-commits mailing list