Skip to content

[analyzer][NFC] Combine similar methods of StreamChecker #70170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged

[analyzer][NFC] Combine similar methods of StreamChecker #70170

merged 1 commit into from
Oct 25, 2023

Conversation

benshi001
Copy link
Member

Methods StreamChecker::preFread and StreamChecker::preFwrite are quite similar, so they can be combined to StreamChecker::preFreadFwrite.

@benshi001 benshi001 requested a review from balazske October 25, 2023 07:03
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Oct 25, 2023
@benshi001 benshi001 requested a review from steakhal October 25, 2023 07:04
@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ben Shi (benshi001)

Changes

Methods StreamChecker::preFread and StreamChecker::preFwrite are quite similar, so they can be combined to StreamChecker::preFreadFwrite.


Full diff: https://github.com/llvm/llvm-project/pull/70170.diff

1 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp (+12-27)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
index ef209f64f0c372c..843748c832ff97f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
@@ -245,10 +245,10 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
       {{{"fclose"}, 1},
        {&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
       {{{"fread"}, 4},
-       {&StreamChecker::preFread,
+       {std::bind(&StreamChecker::preFreadFwrite, _1, _2, _3, _4, true),
         std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, true), 3}},
       {{{"fwrite"}, 4},
-       {&StreamChecker::preFwrite,
+       {std::bind(&StreamChecker::preFreadFwrite, _1, _2, _3, _4, false),
         std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, false), 3}},
       {{{"fseek"}, 3},
        {&StreamChecker::preFseek, &StreamChecker::evalFseek, 0}},
@@ -305,11 +305,8 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
   void evalFclose(const FnDescription *Desc, const CallEvent &Call,
                   CheckerContext &C) const;
 
-  void preFread(const FnDescription *Desc, const CallEvent &Call,
-                CheckerContext &C) const;
-
-  void preFwrite(const FnDescription *Desc, const CallEvent &Call,
-                 CheckerContext &C) const;
+  void preFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
+                      CheckerContext &C, bool IsFread) const;
 
   void evalFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
                        CheckerContext &C, bool IsFread) const;
@@ -637,8 +634,9 @@ void StreamChecker::evalFclose(const FnDescription *Desc, const CallEvent &Call,
   C.addTransition(StateFailure);
 }
 
-void StreamChecker::preFread(const FnDescription *Desc, const CallEvent &Call,
-                             CheckerContext &C) const {
+void StreamChecker::preFreadFwrite(const FnDescription *Desc,
+                                   const CallEvent &Call, CheckerContext &C,
+                                   bool IsFread) const {
   ProgramStateRef State = C.getState();
   SVal StreamVal = getStreamArg(Desc, Call);
   State = ensureStreamNonNull(StreamVal, Call.getArgExpr(Desc->StreamArgNo), C,
@@ -652,6 +650,11 @@ void StreamChecker::preFread(const FnDescription *Desc, const CallEvent &Call,
   if (!State)
     return;
 
+  if (!IsFread) {
+    C.addTransition(State);
+    return;
+  }
+
   SymbolRef Sym = StreamVal.getAsSymbol();
   if (Sym && State->get<StreamMap>(Sym)) {
     const StreamState *SS = State->get<StreamMap>(Sym);
@@ -662,24 +665,6 @@ void StreamChecker::preFread(const FnDescription *Desc, const CallEvent &Call,
   }
 }
 
-void StreamChecker::preFwrite(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)
-    return;
-  State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
-  if (!State)
-    return;
-
-  C.addTransition(State);
-}
-
 void StreamChecker::evalFreadFwrite(const FnDescription *Desc,
                                     const CallEvent &Call, CheckerContext &C,
                                     bool IsFread) const {

@benshi001 benshi001 changed the title [analyzer][NFC] Combine similar methods in StreamChecker [analyzer][NFC] Combine similar methods of StreamChecker Oct 25, 2023
@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Oct 25, 2023
Copy link
Collaborator

@balazske balazske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably add [clang] tag to the commit message, and remove the change in DiagnosticParseKinds.td, otherwise the change looks good.

Methods StreamChecker::preFread and StreamChecker::preFwrite are quite
similar, so they can be combined to StreamChecker::preFreadFwrite.
@benshi001 benshi001 requested a review from balazske October 25, 2023 13:04
@benshi001 benshi001 merged commit 00b7979 into llvm:main Oct 25, 2023
@benshi001 benshi001 deleted the csa-stream branch October 25, 2023 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants