From b0de47733bf9ee5690fb2bcad919d8d92431b026 Mon Sep 17 00:00:00 2001
From: Tristan Gingold <tgingold@free.fr>
Date: Mon, 1 Jun 2020 22:05:01 +0200
Subject: grt: add and document option --asserts

---
 doc/using/Simulation.rst | 16 +++++++++++-----
 src/grt/grt-lib.adb      | 26 ++++++++++++++------------
 src/grt/grt-options.adb  | 33 +++++++++++++++++++++++----------
 src/grt/grt-options.ads  |  3 +++
 4 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/doc/using/Simulation.rst b/doc/using/Simulation.rst
index e90d061b3..607227042 100644
--- a/doc/using/Simulation.rst
+++ b/doc/using/Simulation.rst
@@ -77,15 +77,21 @@ Here is the list of the most useful options. For further info, see :ref:`DEV:Deb
   useful.
 
 .. option:: --ieee-asserts=<POLICY>
+.. option:: --asserts=<POLICY>
 
-  Select how the assertions from ``ieee`` units are
-  handled. `POLICY` can be ``enable`` (the default),
-  ``disable`` which disables all assertions from ``ieee`` packages
-  and ``disable-at-0`` which disables only at the start of simulation.
+  Select how assertions are handled.  `POLICY` can be ``enable`` (the
+  default), ``disable`` which disables all assertions and
+  ``disable-at-0`` which disables only at the start of simulation.
 
-  This option can be useful to avoid assertion messages from
+  The ``--ieee-asserts`` applies only to assertions from ``ieee``
+  package.  This option can be useful to avoid assertion messages from
   ``ieee.numeric_std`` (and other ``ieee`` packages).
 
+  The ``--asserts`` option applies to all assertions, including those
+  from the ``ieee`` units.  The behaviour of the latter can be
+  overridden by using the ``--ieee-asserts`` option after the
+  ``--asserts`` option.
+
 .. option:: --stop-time=<TIME>
 
   Stop the simulation after ``TIME``. ``TIME`` is expressed as a time
diff --git a/src/grt/grt-lib.adb b/src/grt/grt-lib.adb
index 3bd3440ec..9908d581c 100644
--- a/src/grt/grt-lib.adb
+++ b/src/grt/grt-lib.adb
@@ -27,7 +27,7 @@ with Interfaces;
 with Grt.Errors; use Grt.Errors;
 with Grt.Errors_Exec; use Grt.Errors_Exec;
 with Grt.Severity;
-with Grt.Options;
+with Grt.Options; use Grt.Options;
 with Grt.Fcvt;
 with Grt.Backtraces;
 
@@ -96,25 +96,28 @@ package body Grt.Lib is
       end if;
    end Do_Report;
 
+   function Is_Assert_Disabled (Policy : Assert_Handling) return Boolean is
+   begin
+      return Policy = Disable_Asserts
+        or else (Policy = Disable_Asserts_At_Time_0 and Current_Time = 0);
+   end Is_Assert_Disabled;
+
    procedure Ghdl_Assert_Failed
-     (Str : Std_String_Ptr; Severity : Integer; Loc : Ghdl_Location_Ptr)
-   is
+     (Str : Std_String_Ptr; Severity : Integer; Loc : Ghdl_Location_Ptr) is
    begin
+      if Is_Assert_Disabled (Asserts_Policy) then
+         return;
+      end if;
       Do_Report ("assertion", Str, "Assertion violation", Severity, Loc);
    end Ghdl_Assert_Failed;
 
    procedure Ghdl_Ieee_Assert_Failed
-     (Str : Std_String_Ptr; Severity : Integer; Loc : Ghdl_Location_Ptr)
-   is
-      use Grt.Options;
+     (Str : Std_String_Ptr; Severity : Integer; Loc : Ghdl_Location_Ptr) is
    begin
-      if Ieee_Asserts = Disable_Asserts
-        or else (Ieee_Asserts = Disable_Asserts_At_Time_0 and Current_Time = 0)
-      then
+      if Is_Assert_Disabled (Ieee_Asserts) then
          return;
-      else
-         Do_Report ("assertion", Str, "Assertion violation", Severity, Loc);
       end if;
+      Do_Report ("assertion", Str, "Assertion violation", Severity, Loc);
    end Ghdl_Ieee_Assert_Failed;
 
    procedure Ghdl_Psl_Assert_Failed
@@ -290,7 +293,6 @@ package body Grt.Lib is
 
    procedure Ghdl_Check_Stack_Allocation (Size : Ghdl_Index_Type)
    is
-      use Options;
       Bt : Backtrace_Addrs;
    begin
       if Max_Stack_Allocation = 0 then
diff --git a/src/grt/grt-options.adb b/src/grt/grt-options.adb
index 097a9d6da..eadca0068 100644
--- a/src/grt/grt-options.adb
+++ b/src/grt/grt-options.adb
@@ -73,7 +73,8 @@ package body Grt.Options is
       P ("       LEVEL is note,warning,error,failure,none");
       P (" --backtrace-severity=LEVEL  display a backtrace for assertions");
       P (" --ieee-asserts=POLICY  enable or disable asserts from IEEE");
-      P ("       POLICY is enable,disable,disable-at-0");
+      P ("       POLICY is enable, disable, disable-at-0");
+      P (" --asserts=POLICY  enable or disable asserts");
       P (" --stop-time=X     stop the simulation at time X");
       P ("       X is expressed as a time value, without spaces: 1ns, ps...");
       P (" --stop-delta=X    stop the simulation cycle after X delta");
@@ -212,6 +213,23 @@ package body Grt.Options is
       end if;
    end Parse_Severity;
 
+   function Parse_Policy (Opt_Name : String; Arg : String)
+                          return Assert_Handling is
+   begin
+      if Arg = "disable" then
+         return Disable_Asserts;
+      elsif Arg = "enable" then
+         return Enable_Asserts;
+      elsif Arg = "disable-at-0" then
+         return Disable_Asserts_At_Time_0;
+      else
+         Error_S ("bad argument for ");
+         Diag_C (Opt_Name);
+         Error_E (" option, try --help");
+         return Enable_Asserts;
+      end if;
+   end Parse_Policy;
+
    procedure Decode_Option
      (Option : String; Status : out Decode_Option_Status)
    is
@@ -305,15 +323,10 @@ package body Grt.Options is
             end if;
          end;
       elsif Len > 15 and then Option (1 .. 15) = "--ieee-asserts=" then
-         if Option (16 .. Len) = "disable" then
-            Ieee_Asserts := Disable_Asserts;
-         elsif Option (16 .. Len) = "enable" then
-            Ieee_Asserts := Enable_Asserts;
-         elsif Option (16 .. Len) = "disable-at-0" then
-            Ieee_Asserts := Disable_Asserts_At_Time_0;
-         else
-            Error ("bad argument for --ieee-asserts option, try --help");
-         end if;
+         Ieee_Asserts := Parse_Policy ("--ieee-asserts", Option (16 .. Len));
+      elsif Len > 10 and then Option (1 .. 10) = "--asserts=" then
+         Asserts_Policy := Parse_Policy ("--asserts", Option (11 .. Len));
+         Ieee_Asserts := Asserts_Policy;
       elsif Option = "--expect-failure" then
          Expect_Failure := True;
       elsif Len >= 13 and then Option (1 .. 13) = "--stack-size=" then
diff --git a/src/grt/grt-options.ads b/src/grt/grt-options.ads
index 495391e43..a0fb57177 100644
--- a/src/grt/grt-options.ads
+++ b/src/grt/grt-options.ads
@@ -135,6 +135,9 @@ package Grt.Options is
    --  Handling of assertions from IEEE library.
    Ieee_Asserts : Assert_Handling := Enable_Asserts;
 
+   --  Handling of assertions (except from IEEE library).
+   Asserts_Policy : Assert_Handling := Enable_Asserts;
+
    --  Set by --stop-delta=XXX to stop the simulation after XXX delta cycles.
    Stop_Delta : Natural := 5000;
 
-- 
cgit v1.2.3