From f3d16fdeb404c17b77f3bef00259b3b9b54623b0 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Sun, 19 May 2019 02:54:47 -0400
Subject: [PATCH 56/67] FIXME: add options for each warning

---
 gcc/analyzer/hardcoded-state-machine-file.cc      |  5 +--
 gcc/analyzer/hardcoded-state-machine-malloc.cc    | 19 +++++++----
 gcc/analyzer/hardcoded-state-machine-sensitive.cc | 26 +++++++--------
 gcc/analyzer/hardcoded-state-machine-taint.cc     |  2 +-
 gcc/analyzer/hardcoded-state-machine.h            |  8 ++---
 gcc/common.opt                                    | 40 +++++++++++++++++++++++
 gcc/diagnostic-core.h                             |  7 ++--
 gcc/diagnostic.c                                  |  4 +--
 8 files changed, 79 insertions(+), 32 deletions(-)

diff --git a/gcc/analyzer/hardcoded-state-machine-file.cc b/gcc/analyzer/hardcoded-state-machine-file.cc
index 66698ab..745eb83 100644
--- a/gcc/analyzer/hardcoded-state-machine-file.cc
+++ b/gcc/analyzer/hardcoded-state-machine-file.cc
@@ -53,7 +53,7 @@ public:
 
   void emit (rich_location *rich_loc) FINAL OVERRIDE
   {
-    warning_at (rich_loc, 0,
+    warning_at (rich_loc, OPT_Wanalyzer_double_fclose,
 		"double %<fclose%> of FILE %qE",
 		m_arg);
   }
@@ -77,7 +77,8 @@ public:
   {
     metadata m;
     m.add_cwe (775);
-    warning_at (rich_loc, 0,
+    // FIXME: use m
+    warning_at (rich_loc, OPT_Wanalyzer_file_leak,
 		"leak of FILE %qE",
 		m_arg);
   }
diff --git a/gcc/analyzer/hardcoded-state-machine-malloc.cc b/gcc/analyzer/hardcoded-state-machine-malloc.cc
index 4ce5106..cb612b9 100644
--- a/gcc/analyzer/hardcoded-state-machine-malloc.cc
+++ b/gcc/analyzer/hardcoded-state-machine-malloc.cc
@@ -71,7 +71,8 @@ public:
     auto_diagnostic_group d;
     metadata m;
     m.add_cwe (415); /* CWE-415: Double Free.  */
-    warning_at (rich_loc, m, "double-free of %qE", m_arg);
+    warning_at (rich_loc, m, OPT_Wanalyzer_double_free,
+		"double-free of %qE", m_arg);
 
 #if 0
     if (!rich_loc->get_path ())
@@ -140,7 +141,8 @@ public:
     /* CWE-690: Unchecked Return Value to NULL Pointer Dereference.  */
     metadata m;
     m.add_cwe (690);
-    warning_at (rich_loc, m, "use of possibly-NULL %qE", m_arg);
+    warning_at (rich_loc, m, OPT_Wanalyzer_possible_null_dereference,
+		"use of possibly-NULL %qE", m_arg);
   }
 
   label_text describe_critical_state_change (tree expr,
@@ -178,7 +180,8 @@ public:
     /* CWE-690: Unchecked Return Value to NULL Pointer Dereference.  */
     metadata m;
     m.add_cwe (690);
-    warning_at (rich_loc, m, "use of NULL %qE", m_arg);
+    warning_at (rich_loc, m, OPT_Wanalyzer_null_dereference,
+		"use of NULL %qE", m_arg);
   }
 };
 
@@ -196,7 +199,8 @@ public:
     /* CWE-416: Use After Free.  */
     metadata m;
     m.add_cwe (416);
-    warning_at (rich_loc, m, "use after free of %qE", m_arg);
+    warning_at (rich_loc, m, OPT_Wanalyzer_use_after_free,
+		"use after free of %qE", m_arg);
   }
 
   label_text describe_critical_state_change (tree expr,
@@ -223,7 +227,8 @@ public:
   {
     metadata m;
     m.add_cwe (401);
-    warning_at (rich_loc, m, "leak of %qE", m_arg);
+    warning_at (rich_loc, m, OPT_Wanalyzer_malloc_leak,
+		"leak of %qE", m_arg);
   }
 
   label_text describe_critical_state_change (tree expr,
@@ -275,12 +280,12 @@ public:
       default:
 	gcc_unreachable ();
       case KIND_UNKNOWN:
-	warning_at (rich_loc, m,
+	warning_at (rich_loc, m, OPT_Wanalyzer_free_of_non_heap,
 		    "free of %qE which points to memory not on the heap",
 		    m_arg);
 	break;
       case KIND_ALLOCA:
-	warning_at (rich_loc, m,
+	warning_at (rich_loc, m, OPT_Wanalyzer_free_of_non_heap,
 		    "free of memory allocated on the stack by %qs (%qE)"
 		    " will corrupt the heap",
 		    "alloca", m_arg);
diff --git a/gcc/analyzer/hardcoded-state-machine-sensitive.cc b/gcc/analyzer/hardcoded-state-machine-sensitive.cc
index 49dce94..5c52431 100644
--- a/gcc/analyzer/hardcoded-state-machine-sensitive.cc
+++ b/gcc/analyzer/hardcoded-state-machine-sensitive.cc
@@ -40,15 +40,15 @@ static const bool DEBUG = false;
 
 ////////////////////////////////////////////////////////////////////////////
 
-class leakage_through_output_file
-  : public pending_diagnostic_subclass<leakage_through_output_file>
+class exposure_through_output_file
+  : public pending_diagnostic_subclass<exposure_through_output_file>
 {
 public:
-  leakage_through_output_file (tree arg) : m_arg (arg) {}
+  exposure_through_output_file (tree arg) : m_arg (arg) {}
 
-  const char *get_kind () const FINAL OVERRIDE { return "leakage_through_output_file"; }
+  const char *get_kind () const FINAL OVERRIDE { return "exposure_through_output_file"; }
 
-  bool operator== (const leakage_through_output_file &other) const
+  bool operator== (const exposure_through_output_file &other) const
   {
     return m_arg == other.m_arg;
   }
@@ -58,7 +58,7 @@ public:
     metadata m;
     /* CWE-532: Information Exposure Through Log Files */
     m.add_cwe (532);
-    warning_at (rich_loc, m,
+    warning_at (rich_loc, m, OPT_Wanalyzer_exposure_through_output_file,
 		"sensitive value %qE written to output file",
 		m_arg);
   }
@@ -79,13 +79,13 @@ sensitive_state_machine::sensitive_state_machine (logger *logger)
 }
 
 void
-sensitive_state_machine::warn_for_any_leakage (sm_context *sm_ctxt,
-					       const supernode *node,
-					       const gimple *stmt,
-					       tree arg) const
+sensitive_state_machine::warn_for_any_exposure (sm_context *sm_ctxt,
+						const supernode *node,
+						const gimple *stmt,
+						tree arg) const
 {
   sm_ctxt->warn_for_state (node, stmt, arg, m_sensitive,
-			   new leakage_through_output_file (arg));
+			   new exposure_through_output_file (arg));
 }
 
 bool
@@ -112,14 +112,14 @@ sensitive_state_machine::on_stmt (sm_context *sm_ctxt,
 	  for (int idx = 1; idx < gimple_call_num_args (call); idx++)
 	    {
 	      tree arg = gimple_call_arg (call, idx);
-	      warn_for_any_leakage (sm_ctxt, node, stmt, arg);
+	      warn_for_any_exposure (sm_ctxt, node, stmt, arg);
 	    }
 	  return true;
 	}
       else if (is_named_call_p (call, "fwrite", 4))
 	{
 	  tree arg = gimple_call_arg (call, 0);
-	  warn_for_any_leakage (sm_ctxt, node, stmt, arg);
+	  warn_for_any_exposure (sm_ctxt, node, stmt, arg);
 	  return true;
 	}
     }
diff --git a/gcc/analyzer/hardcoded-state-machine-taint.cc b/gcc/analyzer/hardcoded-state-machine-taint.cc
index 19731bc..d6c2d6c 100644
--- a/gcc/analyzer/hardcoded-state-machine-taint.cc
+++ b/gcc/analyzer/hardcoded-state-machine-taint.cc
@@ -56,7 +56,7 @@ public:
   {
     metadata m;
     m.add_cwe (129);
-    warning_at (rich_loc, m,
+    warning_at (rich_loc, m, OPT_Wanalyzer_tainted_array_index,
 		"use of tainted value %qE in array lookup without bounds checking",
 		m_arg);
   }
diff --git a/gcc/analyzer/hardcoded-state-machine.h b/gcc/analyzer/hardcoded-state-machine.h
index e4b359f..ff9065f 100644
--- a/gcc/analyzer/hardcoded-state-machine.h
+++ b/gcc/analyzer/hardcoded-state-machine.h
@@ -387,10 +387,10 @@ public:
   bool can_purge_p (state_t s) const FINAL OVERRIDE;
 
 private:
-  void warn_for_any_leakage (sm_context *sm_ctxt,
-			     const supernode *node,
-			     const gimple *stmt,
-			     tree arg) const;
+  void warn_for_any_exposure (sm_context *sm_ctxt,
+			      const supernode *node,
+			      const gimple *stmt,
+			      tree arg) const;
 
   state_t m_start;
   state_t m_sensitive;
diff --git a/gcc/common.opt b/gcc/common.opt
index 0ff88fe9..432e631 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -541,6 +541,46 @@ Waggressive-loop-optimizations
 Common Var(warn_aggressive_loop_optimizations) Init(1) Warning
 Warn if a loop with constant number of iterations triggers undefined behavior.
 
+Wanalyzer-double-fclose
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-double-free
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-exposure-through-output-file
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-file-leak
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-free-of-non-heap
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-malloc-leak
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-null-dereference
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-possible-null-dereference
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-tainted-array-index
+Common Init(1) Warning
+FIXME
+
+Wanalyzer-use-after-free
+Common Init(1) Warning
+FIXME
+
 Warray-bounds
 Common Var(warn_array_bounds) Warning
 Warn if an array is accessed out of bounds.
diff --git a/gcc/diagnostic-core.h b/gcc/diagnostic-core.h
index 0770ac7..2e7f120 100644
--- a/gcc/diagnostic-core.h
+++ b/gcc/diagnostic-core.h
@@ -81,8 +81,9 @@ extern bool warning_at (location_t, int, const char *, ...)
     ATTRIBUTE_GCC_DIAG(3,4);
 extern bool warning_at (rich_location *, int, const char *, ...)
     ATTRIBUTE_GCC_DIAG(3,4);
-extern bool warning_at (rich_location *, const diagnostic_metadata &, const char *, ...)
-    ATTRIBUTE_GCC_DIAG(3,4);
+extern bool warning_at (rich_location *, const diagnostic_metadata &, int,
+			const char *, ...)
+    ATTRIBUTE_GCC_DIAG(4,5);
 extern void error (const char *, ...) ATTRIBUTE_GCC_DIAG(1,2);
 extern void error_n (location_t, unsigned HOST_WIDE_INT, const char *,
 		     const char *, ...)
@@ -114,7 +115,7 @@ extern bool emit_diagnostic (diagnostic_t, rich_location *, int,
 			     const char *, ...) ATTRIBUTE_GCC_DIAG(4,5);
 extern bool emit_diagnostic_valist (diagnostic_t, location_t, int, const char *,
 				    va_list *) ATTRIBUTE_GCC_DIAG (4,0);
-extern bool emit_diagnostic_valist (diagnostic_t, rich_location *, 
+extern bool emit_diagnostic_valist (diagnostic_t, rich_location *,
 				    const diagnostic_metadata *metadata,
 				    int, const char *, va_list *)
   ATTRIBUTE_GCC_DIAG (5,0);
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index 4a5f1b44..0468325 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -1438,7 +1438,7 @@ warning_at (rich_location *richloc, int opt, const char *gmsgid, ...)
 
 bool
 warning_at (rich_location *richloc, const diagnostic_metadata &metadata,
-	    const char *gmsgid, ...)
+	    int opt, const char *gmsgid, ...)
 {
   gcc_assert (richloc);
 
@@ -1446,7 +1446,7 @@ warning_at (rich_location *richloc, const diagnostic_metadata &metadata,
   va_list ap;
   va_start (ap, gmsgid);
   bool ret
-    = diagnostic_impl (richloc, &metadata, 0, gmsgid, &ap,
+    = diagnostic_impl (richloc, &metadata, opt, gmsgid, &ap,
 		       DK_WARNING);
   va_end (ap);
   return ret;
-- 
1.8.5.3

