From 24686ae727c5b1e304bec167a6ce41879e456bed Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 14 Mar 2019 17:42:01 -0400
Subject: [PATCH 114/169] FIXME: various cleanups

---
 gcc/analyzer/constraints.cc                        | 116 +++++++++------------
 .../facts/infinite-recursion/stdout.txt            |   0
 gcc/testsuite/gcc.dg/TODO-analyzer/json/input.c    |  27 -----
 gcc/testsuite/gcc.dg/TODO-analyzer/json/script.py  | 104 ------------------
 gcc/testsuite/gcc.dg/TODO-analyzer/json/stdout.txt |   2 -
 5 files changed, 50 insertions(+), 199 deletions(-)
 delete mode 100644 gcc/testsuite/gcc.dg/TODO-analyzer/facts/infinite-recursion/stdout.txt
 delete mode 100644 gcc/testsuite/gcc.dg/TODO-analyzer/json/input.c
 delete mode 100644 gcc/testsuite/gcc.dg/TODO-analyzer/json/script.py
 delete mode 100644 gcc/testsuite/gcc.dg/TODO-analyzer/json/stdout.txt

diff --git a/gcc/analyzer/constraints.cc b/gcc/analyzer/constraints.cc
index e4867a0..7d7566e 100644
--- a/gcc/analyzer/constraints.cc
+++ b/gcc/analyzer/constraints.cc
@@ -659,6 +659,24 @@ debug (const path_state &ps)
 
 namespace selftest {
 
+/* Attempt to add the constraint (LHS OP RHS) to PS.
+   Verify that PS remains satisfiable.  */
+
+#define ADD_SAT_CONSTRAINT(PS, LHS, OP, RHS)				\
+  SELFTEST_BEGIN_STMT							\
+    bool sat = (PS).add_constraint (LHS, OP, RHS);			\
+    ASSERT_TRUE (sat);							\
+  SELFTEST_END_STMT
+
+/* Attempt to add the constraint (LHS OP RHS) to PS.
+   Verify that the result is not satisfiable.  */
+
+#define ADD_UNSAT_CONSTRAINT(PS, LHS, OP, RHS)				\
+  SELFTEST_BEGIN_STMT							\
+    bool sat = (PS).add_constraint (LHS, OP, RHS);			\
+    ASSERT_FALSE (sat);							\
+  SELFTEST_END_STMT
+
 static void
 assert_condition (const location &loc,
 		  path_state &ps,
@@ -723,8 +741,7 @@ test_constraint_conditions ()
     path_state ps;
     ASSERT_CONDITION_UNKNOWN (ps, x, EQ_EXPR, y);
 
-    bool sat = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, y);
 
     ASSERT_CONDITION_TRUE (ps, x, EQ_EXPR, y);
     ASSERT_CONDITION_TRUE (ps, x, LE_EXPR, y);
@@ -755,11 +772,8 @@ test_constraint_conditions ()
     path_state ps;
     ASSERT_CONDITION_UNKNOWN (ps, x, EQ_EXPR, y);
 
-    bool sat_0 = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_TRUE (sat_0);
-
-    bool sat_1 = ps.add_constraint (y, EQ_EXPR, z);
-    ASSERT_TRUE (sat_1);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, y);
+    ADD_SAT_CONSTRAINT (ps, y, EQ_EXPR, z);
 
     ASSERT_CONDITION_TRUE (ps, x, EQ_EXPR, z);
     ASSERT_CONDITION_TRUE (ps, x, LE_EXPR, z);
@@ -773,8 +787,7 @@ test_constraint_conditions ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, NE_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, NE_EXPR, y);
 
     ASSERT_CONDITION_TRUE (ps, x, NE_EXPR, y);
     ASSERT_CONDITION_FALSE (ps, x, EQ_EXPR, y);
@@ -804,8 +817,7 @@ test_constraint_conditions ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, LT_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, LT_EXPR, y);
 
     ASSERT_CONDITION_TRUE (ps, x, LT_EXPR, y);
     ASSERT_CONDITION_TRUE (ps, x, LE_EXPR, y);
@@ -827,8 +839,7 @@ test_constraint_conditions ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, LE_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, LE_EXPR, y);
 
     ASSERT_CONDITION_UNKNOWN (ps, x, LT_EXPR, y);
     ASSERT_CONDITION_TRUE (ps, x, LE_EXPR, y);
@@ -850,8 +861,7 @@ test_constraint_conditions ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, GT_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, GT_EXPR, y);
 
     ASSERT_CONDITION_TRUE (ps, x, GT_EXPR, y);
     ASSERT_CONDITION_TRUE (ps, x, GE_EXPR, y);
@@ -873,8 +883,7 @@ test_constraint_conditions ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, GE_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, GE_EXPR, y);
 
     ASSERT_CONDITION_UNKNOWN (ps, x, GT_EXPR, y);
     ASSERT_CONDITION_TRUE (ps, x, GE_EXPR, y);
@@ -908,10 +917,8 @@ test_constraint_conditions ()
   /* x == 0, y == 42.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, EQ_EXPR, int_0);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (y, EQ_EXPR, int_42);
-    ASSERT_TRUE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, int_0);
+    ADD_SAT_CONSTRAINT (ps, y, EQ_EXPR, int_42);
 
     ASSERT_CONDITION_TRUE (ps, x, NE_EXPR, y);
     ASSERT_CONDITION_FALSE (ps, x, EQ_EXPR, y);
@@ -926,55 +933,43 @@ test_constraint_conditions ()
   /* x == y && x != y.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, NE_EXPR, y);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, y);
+    ADD_UNSAT_CONSTRAINT (ps, x, NE_EXPR, y);
   }
 
   /* x == 0 then x == 42.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, EQ_EXPR, int_0);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, EQ_EXPR, int_42);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, int_0);
+    ADD_UNSAT_CONSTRAINT (ps, x, EQ_EXPR, int_42);
   }
 
   /* x == 0 then x != 0.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, EQ_EXPR, int_0);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, NE_EXPR, int_0);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, int_0);
+    ADD_UNSAT_CONSTRAINT (ps, x, NE_EXPR, int_0);
   }
 
   /* x == 0 then x > 0.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, EQ_EXPR, int_0);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, GT_EXPR, int_0);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, int_0);
+    ADD_UNSAT_CONSTRAINT (ps, x, GT_EXPR, int_0);
   }
 
   /* x != y && x == y.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, NE_EXPR, y);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, NE_EXPR, y);
+    ADD_UNSAT_CONSTRAINT (ps, x, EQ_EXPR, y);
   }
 
   /* x <= y && x > y.  */
   {
     path_state ps;
-    bool sat_a = ps.add_constraint (x, LE_EXPR, y);
-    ASSERT_TRUE (sat_a);
-    bool sat_b = ps.add_constraint (x, GT_EXPR, y);
-    ASSERT_FALSE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, LE_EXPR, y);
+    ADD_UNSAT_CONSTRAINT (ps, x, GT_EXPR, y);
   }
 
   // etc
@@ -1009,17 +1004,14 @@ test_constraint_conditions_2 ()
     ASSERT_CONDITION_UNKNOWN (ps, c, EQ_EXPR, d);
     ASSERT_CONDITION_UNKNOWN (ps, a, EQ_EXPR, d);
 
-    bool sat_0 = ps.add_constraint (a, EQ_EXPR, b);
-    ASSERT_TRUE (sat_0);
+    ADD_SAT_CONSTRAINT (ps, a, EQ_EXPR, b);
     ASSERT_CONDITION_TRUE (ps, a, EQ_EXPR, b);
 
-    bool sat_1 = ps.add_constraint (c, EQ_EXPR, d);
-    ASSERT_TRUE (sat_1);
+    ADD_SAT_CONSTRAINT (ps, c, EQ_EXPR, d);
     ASSERT_CONDITION_TRUE (ps, c, EQ_EXPR, d);
     ASSERT_CONDITION_UNKNOWN (ps, a, EQ_EXPR, d);
 
-    bool sat_2 = ps.add_constraint (c, EQ_EXPR, b);
-    ASSERT_TRUE (sat_2);
+    ADD_SAT_CONSTRAINT (ps, c, EQ_EXPR, b);
     ASSERT_CONDITION_TRUE (ps, c, EQ_EXPR, b);
     ASSERT_CONDITION_TRUE (ps, a, EQ_EXPR, d);
   }
@@ -1047,8 +1039,7 @@ test_constraint_impl ()
   {
     path_state ps;
 
-    bool sat = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_TRUE (sat);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, y);
 
     /* Assert various things about the insides of ps.  */
     ASSERT_EQ (ps.m_constraints.length (), 0);
@@ -1061,13 +1052,11 @@ test_constraint_impl ()
     ASSERT_CONDITION_UNKNOWN (ps, x, EQ_EXPR, y);
     ASSERT_CONDITION_UNKNOWN (ps, x, GE_EXPR, z);
 
-    bool sat_a = ps.add_constraint (y, GE_EXPR, z);
-    ASSERT_TRUE (sat_a);
+    ADD_SAT_CONSTRAINT (ps, y, GE_EXPR, z);
     ASSERT_CONDITION_TRUE (ps, y, GE_EXPR, z);
     ASSERT_CONDITION_UNKNOWN (ps, x, GE_EXPR, z);
 
-    bool sat_b = ps.add_constraint (x, EQ_EXPR, y);
-    ASSERT_TRUE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, y);
 
     /* Assert various things about the insides of ps.  */
     ASSERT_EQ (ps.m_constraints.length (), 1);
@@ -1083,13 +1072,11 @@ test_constraint_impl ()
     ASSERT_CONDITION_UNKNOWN (ps, x, EQ_EXPR, y);
     ASSERT_CONDITION_UNKNOWN (ps, x, GE_EXPR, z);
 
-    bool sat_a = ps.add_constraint (y, GE_EXPR, z);
-    ASSERT_TRUE (sat_a);
+    ADD_SAT_CONSTRAINT (ps, y, GE_EXPR, z);
     ASSERT_CONDITION_TRUE (ps, y, GE_EXPR, z);
     ASSERT_CONDITION_UNKNOWN (ps, x, GE_EXPR, z);
 
-    bool sat_b = ps.add_constraint (y, EQ_EXPR, x);
-    ASSERT_TRUE (sat_b);
+    ADD_SAT_CONSTRAINT (ps, y, EQ_EXPR, x);
 
     /* Assert various things about the insides of ps.  */
     ASSERT_EQ (ps.m_constraints.length (), 1);
@@ -1103,11 +1090,8 @@ test_constraint_impl ()
   {
     path_state ps;
 
-    bool sat_0 = ps.add_constraint (x, EQ_EXPR, int_0);
-    ASSERT_TRUE (sat_0);
-
-    bool sat_1 = ps.add_constraint (x, NE_EXPR, int_42);
-    ASSERT_TRUE (sat_1);
+    ADD_SAT_CONSTRAINT (ps, x, EQ_EXPR, int_0);
+    ADD_SAT_CONSTRAINT (ps, x, NE_EXPR, int_42);
 
     /* Assert various things about the insides of ps.  */
     ASSERT_EQ (ps.m_constraints.length (), 0);
diff --git a/gcc/testsuite/gcc.dg/TODO-analyzer/facts/infinite-recursion/stdout.txt b/gcc/testsuite/gcc.dg/TODO-analyzer/facts/infinite-recursion/stdout.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/gcc/testsuite/gcc.dg/TODO-analyzer/json/input.c b/gcc/testsuite/gcc.dg/TODO-analyzer/json/input.c
deleted file mode 100644
index 8234afa..0000000
--- a/gcc/testsuite/gcc.dg/TODO-analyzer/json/input.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
-   Copyright 2012 David Malcolm <dmalcolm@redhat.com>
-   Copyright 2012 Red Hat, Inc.
-
-   This is free software: you can redistribute it and/or modify it
-   under the terms of the GNU General Public License as published by
-   the Free Software Foundation, either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see
-   <http://www.gnu.org/licenses/>.
-*/
-
-#include <stdlib.h>
-
-int *test(int i)
-{
-  int *foo = (int*)malloc(sizeof(int*));
-  *foo = i; /* BUG: result of malloc could be NULL */
-  return foo;
-}
diff --git a/gcc/testsuite/gcc.dg/TODO-analyzer/json/script.py b/gcc/testsuite/gcc.dg/TODO-analyzer/json/script.py
deleted file mode 100644
index 281ccc9..0000000
--- a/gcc/testsuite/gcc.dg/TODO-analyzer/json/script.py
+++ /dev/null
@@ -1,104 +0,0 @@
-# -*- coding: utf-8 -*-
-#   Copyright 2012, 2013 David Malcolm <dmalcolm@redhat.com>
-#   Copyright 2012, 2013 Red Hat, Inc.
-#
-#   This is free software: you can redistribute it and/or modify it
-#   under the terms of the GNU General Public License as published by
-#   the Free Software Foundation, either version 3 of the License, or
-#   (at your option) any later version.
-#
-#   This program is distributed in the hope that it will be useful, but
-#   WITHOUT ANY WARRANTY; without even the implied warranty of
-#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#   General Public License for more details.
-#
-#   You should have received a copy of the GNU General Public License
-#   along with this program.  If not, see
-#   <http://www.gnu.org/licenses/>.
-
-###########################################################################
-# Selftest of JSON report dumping
-#
-# This is the same code and checker as:
-#    tests/sm/assignments/dereference-on-lhs
-# but with the output sent to JSON
-###########################################################################
-
-import glob
-import json
-
-from sm import main
-from sm.options import Options
-from sm.parser import parse_file
-
-def get_json_reports():
-    return glob.glob('tests/sm/json/input.c.*.sm.json')
-
-def delete_json_reports():
-    for jsonfile in get_json_reports():
-        os.unlink(jsonfile)
-
-def selftest(ctxt, solution):
-    if 0:
-        import sys
-        solution.dump(sys.stderr)
-
-    # Verify the JSON version of the report directly from the Error object:
-    assert len(ctxt._errors) == 1
-    err = ctxt._errors[0]
-    report = err.make_report(ctxt, solution)
-
-    jsonobj = report.as_json()
-    verify_json(jsonobj)
-    print('OK: verified in-memory JSON object')
-
-    # Verify that a JSON file was written and check its contents:
-    jsonfiles = get_json_reports()
-    assert len(jsonfiles) == 1
-    with open(jsonfiles[0], 'r') as f:
-        jsonobj = json.load(f)
-    verify_json(jsonobj)
-    print('OK: verified JSON object parsed from disk')
-
-    # On success, keep the directory clean:
-    if 1:
-        delete_json_reports()
-
-def verify_json(jsonobj):
-    if 0:
-        print(json.dumps(jsonobj,
-                         sort_keys=True,
-                         indent=4, separators=(',', ': ')))
-
-    # Verify jsonobj['cwe']:
-    assert jsonobj['cwe'] == 'CWE-690'
-
-    # Verify jsonobj['sm']:
-    assert jsonobj['sm']['name'] == 'malloc_checker'
-    assert jsonobj['sm']['filename'] == 'sm/checkers/malloc_checker.sm'
-    assert jsonobj['sm']['line'] == 23
-
-    # Verify jsonobj['loc']:
-    assert 'input.c' in jsonobj['loc']['actualfilename']
-    assert jsonobj['loc']['givenfilename'] == "tests/sm/json/input.c"
-    assert jsonobj['loc']['line'] == 25
-    assert jsonobj['loc']['column'] == 8
-
-    # Verify jsonobj['message']:
-    assert jsonobj['message'] == "dereference of possibly-NULL pointer foo"
-
-    # Verify jsonobj['notes']:
-    assert len(jsonobj['notes']) == 2
-    note = jsonobj['notes'][0]
-    assert jsonobj['loc']['givenfilename'] == "tests/sm/json/input.c"
-    assert note['loc']['line'] == 24
-    assert note['message'] == "foo assigned to the result of malloc()"
-
-# Preprocessing: remove any existing json error reports:
-delete_json_reports()
-
-checker = parse_file('sm/checkers/malloc_checker.sm')
-main([checker],
-     # Dump JSON files rather than writing to stderr:
-     options=Options(dump_json=True),
-     selftest=selftest, )
diff --git a/gcc/testsuite/gcc.dg/TODO-analyzer/json/stdout.txt b/gcc/testsuite/gcc.dg/TODO-analyzer/json/stdout.txt
deleted file mode 100644
index ad3aa19..0000000
--- a/gcc/testsuite/gcc.dg/TODO-analyzer/json/stdout.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-OK: verified in-memory JSON object
-OK: verified JSON object parsed from disk
-- 
1.8.5.3

