From 32e1e006041d9cb894763b93ecfffbf662b3a692 Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Thu, 18 Apr 2019 18:29:12 +0200
Subject: [PATCH] =?UTF-8?q?[typing]=20cleaning=20up=20unused=20locals,=20a?=
 =?UTF-8?q?gain=20=F0=9F=9A=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/kernel_internals/typing/cabs2cil.ml       | 19 ++++++++++++++++---
 tests/syntax/local-variable.i                 |  3 +++
 tests/syntax/oracle/local-variable.res.oracle |  8 ++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/kernel_internals/typing/cabs2cil.ml b/src/kernel_internals/typing/cabs2cil.ml
index 7f5b53a7cd8..b66c18cb4e1 100644
--- a/src/kernel_internals/typing/cabs2cil.ml
+++ b/src/kernel_internals/typing/cabs2cil.ml
@@ -1628,13 +1628,26 @@ struct
       (fun (stmt, _, _, _, _) -> ignore (Cil.visitCilStmt vis stmt))
       c.stmts
 
-  (* if we're about to drop a chunk, clean up locals of current func. *)
-  let clean_up_chunk_locals c =
+  let remove_locals l =
     !currentFunctionFDEC.slocals <-
       List.filter
-        (fun x -> not (List.exists (Cil_datatype.Varinfo.equal x) c.locals))
+        (fun x -> not (List.exists (Cil_datatype.Varinfo.equal x) l))
         !currentFunctionFDEC.slocals
 
+  let clean_up_block_locals (s, _, _, _, _) =
+    let vis =
+      object
+        inherit Cil.nopCilVisitor
+        method! vblock b = remove_locals b.blocals; DoChildren
+      end
+    in
+    ignore (Cil.visitCilStmt vis s)
+
+  (* if we're about to drop a chunk, clean up locals of current func. *)
+  let clean_up_chunk_locals c =
+    remove_locals c.locals;
+    List.iter clean_up_block_locals c.stmts
+
   (* Gathers locals of blocks. *)
   class locals_visitor () = object
     inherit Cil.nopCilVisitor
diff --git a/tests/syntax/local-variable.i b/tests/syntax/local-variable.i
index 52067d1dcec..6e6d1b453d0 100644
--- a/tests/syntax/local-variable.i
+++ b/tests/syntax/local-variable.i
@@ -10,3 +10,6 @@ void f() {
     int b;
   }
 }
+
+int c;
+int g() { return 1 || (-1L || g(), c); }
diff --git a/tests/syntax/oracle/local-variable.res.oracle b/tests/syntax/oracle/local-variable.res.oracle
index e7cfa00d9cd..13981c13468 100644
--- a/tests/syntax/oracle/local-variable.res.oracle
+++ b/tests/syntax/oracle/local-variable.res.oracle
@@ -16,4 +16,12 @@ void f(void)
   return;
 }
 
+int c;
+int g(void)
+{
+  int __retres;
+  __retres = 1;
+  return __retres;
+}
+
 
-- 
GitLab