[PATCH] D99262: [analyzer] Fix dead store checker false positive
Valeriy Savchenko via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 29 02:25:48 PDT 2021
vsavchenko updated this revision to Diff 333793.
vsavchenko added a comment.
Add another test
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D99262/new/
https://reviews.llvm.org/D99262
Files:
clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
clang/test/Analysis/dead-stores.c
Index: clang/test/Analysis/dead-stores.c
===================================================================
--- clang/test/Analysis/dead-stores.c
+++ clang/test/Analysis/dead-stores.c
@@ -635,3 +635,27 @@
volatile int v;
v = 0; // no warning
}
+
+struct Foo {
+ int x;
+ int y;
+};
+
+struct Foo rdar34122265_getFoo(void);
+
+int rdar34122265_test(int input) {
+ // This is allowed for defensive programming.
+ struct Foo foo = {0, 0};
+ if (input > 0) {
+ foo = rdar34122265_getFoo();
+ } else {
+ return 0;
+ }
+ return foo.x + foo.y;
+}
+
+void rdar34122265_test_cast() {
+ // This is allowed for defensive programming.
+ struct Foo foo = {0, 0};
+ (void)foo;
+}
Index: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
@@ -11,17 +11,18 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/Lex/Lexer.h"
-#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Analysis/Analyses/LiveVariables.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "llvm/ADT/BitVector.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/SaveAndRestore.h"
@@ -415,8 +416,23 @@
if (E->isEvaluatable(Ctx))
return;
+ // We should also allow defensive initialization of structs.
+ if (const auto *ILE =
+ dyn_cast<InitListExpr>(E->IgnoreParenCasts())) {
+ // We can use exactly the same logic here.
+ // If all fields of the struct are initialized with constants,
+ //
+ // e.g. : Foo x = { 0, 1 };
+ //
+ // we shouldn't complain about the fact that it is not used.
+ if (llvm::all_of(ILE->inits(), [this](const Expr *Init) {
+ return Init->isEvaluatable(Ctx);
+ }))
+ return;
+ }
+
if (const DeclRefExpr *DRE =
- dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
+ dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// Special case: check for initialization from constant
// variables.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99262.333793.patch
Type: text/x-patch
Size: 2918 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210329/e4cab9bd/attachment.bin>
More information about the cfe-commits
mailing list