[clang] [clang][Analyzer][NFC] Simplify preDefault/preFseek/preFreadFwrite of StreamChecker (PR #71394)
Ben Shi via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 6 05:52:49 PST 2023
https://github.com/benshi001 created https://github.com/llvm/llvm-project/pull/71394
None
>From 965c109cc19187329d5ab2ae324665dfbd7c17ee Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Mon, 6 Nov 2023 21:49:36 +0800
Subject: [PATCH] [clang][Analyzer][NFC] Simplify
preDefault/preFseek/preFreadFwrite of StreamChecker
---
.../StaticAnalyzer/Checkers/StreamChecker.cpp | 35 ++++++++++---------
1 file changed, 18 insertions(+), 17 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
index 898906977ba9bb6..e35929c4dcd28e4 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
@@ -342,6 +342,11 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
CheckerContext &C,
const StreamErrorState &ErrorKind) const;
+ /// Perform some common checks for all preXXX functions.
+ bool basicCheck(const FnDescription *Desc, const CallEvent &Call,
+ CheckerContext &C, ProgramStateRef &State,
+ SVal &StreamVal) const;
+
/// Check that the stream (in StreamVal) is not NULL.
/// If it can only be NULL a fatal error is emitted and nullptr returned.
/// Otherwise the return value is a new state where the stream is constrained
@@ -639,13 +644,9 @@ void StreamChecker::preFreadFwrite(const FnDescription *Desc,
bool IsFread) const {
ProgramStateRef State = C.getState();
SVal StreamVal = getStreamArg(Desc, Call);
- State = ensureStreamNonNull(StreamVal, Call.getArgExpr(Desc->StreamArgNo), C,
- State);
- if (!State)
- return;
- State = ensureStreamOpened(StreamVal, C, State);
- if (!State)
+ if (!basicCheck(Desc, Call, C, State, StreamVal))
return;
+
State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
if (!State)
return;
@@ -749,13 +750,9 @@ void StreamChecker::preFseek(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
SVal StreamVal = getStreamArg(Desc, Call);
- State = ensureStreamNonNull(StreamVal, Call.getArgExpr(Desc->StreamArgNo), C,
- State);
- if (!State)
- return;
- State = ensureStreamOpened(StreamVal, C, State);
- if (!State)
+ if (!basicCheck(Desc, Call, C, State, StreamVal))
return;
+
State = ensureFseekWhenceCorrect(Call.getArgSVal(2), C, State);
if (!State)
return;
@@ -1002,15 +999,19 @@ void StreamChecker::preDefault(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
SVal StreamVal = getStreamArg(Desc, Call);
+ if (basicCheck(Desc, Call, C, State, StreamVal))
+ C.addTransition(State);
+}
+
+bool StreamChecker::basicCheck(const FnDescription *Desc, const CallEvent &Call,
+ CheckerContext &C, ProgramStateRef &State,
+ SVal &StreamVal) const {
State = ensureStreamNonNull(StreamVal, Call.getArgExpr(Desc->StreamArgNo), C,
State);
if (!State)
- return;
+ return false;
State = ensureStreamOpened(StreamVal, C, State);
- if (!State)
- return;
-
- C.addTransition(State);
+ return State != nullptr;
}
void StreamChecker::evalSetFeofFerror(const FnDescription *Desc,
More information about the cfe-commits
mailing list