Skip to content

Commit

Permalink
Add '-W/--warnings-as-errors' flag to GNATcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoGGuerrier committed Dec 16, 2024
1 parent 688bc47 commit 19c129a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 36 deletions.
6 changes: 6 additions & 0 deletions lkql_checker/doc/gnatcheck_rm/using_gnatcheck.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ The following switches control the general ``gnatcheck`` behavior
Verbose mode; ``gnatcheck`` generates version information and then
a trace of sources being processed.

.. index:: -W

``-W, --warnings-as-errors``
Treat warnings raised by GNATcheck as errors, ensuring an erroneous return
code.

.. index:: -o

``-o report_file``
Expand Down
6 changes: 6 additions & 0 deletions lkql_checker/src/gnatcheck-options.ads
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ package Gnatcheck.Options is
Help => "emit a 'rules.lkql' file containing the rules "
& "configuration");

package Warnings_As_Errors is new Parse_Flag
(Parser => Parser,
Long => "--warnings-as-errors",
Short => "-W",
Help => "Treat warning messages as errors");

function Quiet_Mode return Boolean is (Quiet.Get or else Brief.Get);

function Short_Report return Boolean is (Brief.Get or else Short.Get);
Expand Down
47 changes: 27 additions & 20 deletions lkql_checker/src/gnatcheck-output.adb
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,14 @@ package body Gnatcheck.Output is

procedure Warning (Message : String) is
begin
if Warning_Mode /= Quiet then
if Arg.Warnings_As_Errors.Get or else Warning_Mode /= Quiet then
Error (Message);
end if;

-- Force a non-zero return code when "warnings as errors" is enabled
if Arg.Warnings_As_Errors.Get then
Error_From_Warning := True;
end if;
end Warning;

----------------
Expand Down Expand Up @@ -593,11 +598,12 @@ package body Gnatcheck.Output is
Put_Line (" --target=targetname - specify a target for cross platforms");
Put_Line (" --RTS=<runtime> - use runtime <runtime>");
Put_Line ("");
Put_Line (" -h - print out the list of the available kp detectors");
Put_Line (" -jn - n is the maximal number of processes");
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
Put_Line (" -v - verbose mode");
Put_Line (" -l - full pathname for file locations");
Put_Line (" -h - print out the list of the available kp detectors");
Put_Line (" -jn - n is the maximal number of processes");
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
Put_Line (" -v - verbose mode");
Put_Line (" -W, --warnings-as-errors - treat warning messages as errors");
Put_Line (" -l - full pathname for file locations");
Put_Line ("");
Put_Line (" --brief - brief mode, only report detections in Stderr");
Put_Line (" --check-semantic - check semantic validity of the source files");
Expand Down Expand Up @@ -636,18 +642,19 @@ package body Gnatcheck.Output is
Put_Line (" --RTS=<runtime> - use runtime <runtime>");
Put_Line (" --config=<cgpr> - use configuration project <cgpr>");
Put_Line ("");
Put_Line (" -h - print out the list of the currently implemented rules");
Put_Line (" -mn - n is the maximal number of diagnoses in Stderr");
Put_Line (" (n in 0 .. 1000, 0 means no limit); default is 0");
Put_Line (" -jn - n is the maximal number of processes");
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
Put_Line (" -t - report execution time in Stderr");
Put_Line (" -v - verbose mode");
Put_Line (" -l - full pathname for file locations");
Put_Line (" -log - duplicate all the messages sent to Stderr in gnatcheck.log");
Put_Line (" -s - short form of the report file");
Put_Line (" -xml - generate report in XML format");
Put_Line (" -nt - do not generate text report (enforces '-xml')");
Put_Line (" -h - print out the list of the currently implemented rules");
Put_Line (" -mn - n is the maximal number of diagnoses in Stderr");
Put_Line (" (n in 0 .. 1000, 0 means no limit); default is 0");
Put_Line (" -jn - n is the maximal number of processes");
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
Put_Line (" -t - report execution time in Stderr");
Put_Line (" -v - verbose mode");
Put_Line (" -W, --warnings-as-errors - treat warning messages as errors");
Put_Line (" -l - full pathname for file locations");
Put_Line (" -log - duplicate all the messages sent to Stderr in gnatcheck.log");
Put_Line (" -s - short form of the report file");
Put_Line (" -xml - generate report in XML format");
Put_Line (" -nt - do not generate text report (enforces '-xml')");
Put_Line ("");
Put_Line (" --show-rule - append rule names to diagnoses generated");
Put_Line (" --show-instantiation-chain - show instantiation chain for reported generic construct");
Expand All @@ -668,9 +675,9 @@ package body Gnatcheck.Output is
Put_Line (" -ox filename - specify the name of the XML report file (enforces '-xml')");
Put_Line ("");
Put_Line (" filename - the name of the Ada source file to be analyzed.");
Put_Line (" Wildcards are allowed");
Put_Line (" Wildcards are allowed");
Put_Line (" -files=filename - the name of the text file containing a list of Ada");
Put_Line (" source files to analyze");
Put_Line (" source files to analyze");
Put_Line (" --ignore=filename - do not process sources listed in filename");
Put_Line (" --rule-file=filename - read rule configuration from the given LKQL file");
Put_Line (" -r, --rule [rule_name] - enable the given rule during the GNATcheck run (this option is cumulative)");
Expand Down
4 changes: 4 additions & 0 deletions lkql_checker/src/gnatcheck-output.ads
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package Gnatcheck.Output is
Custom_XML_Report_File : Boolean := False;
-- Undicate if custom name is specified for text or XML output file

Error_From_Warning : Boolean;
-- Whether a warning message has been emitted while "warnings as errors"
-- mode is enabled. This ensure the return code of GNATcheck is not 0.

procedure Print_Version_Info (Released_At : Positive);
-- Prints into Stderr the tool version information in the following format:
--
Expand Down
35 changes: 19 additions & 16 deletions lkql_checker/src/gnatcheck_main.adb
Original file line number Diff line number Diff line change
Expand Up @@ -604,22 +604,25 @@ begin
Gnatcheck.Rules.Rule_Table.Clean_Up;
Close_Log_File;

OS_Exit (if Tool_Failures /= 0 or else Detected_Internal_Error /= 0
then E_Error
elsif Missing_Rule_File_Detected then E_Missing_Rule_File
elsif Bad_Rule_Detected then E_Missing_Rule
elsif Rule_Option_Problem_Detected then E_Bad_Rules
elsif Missing_File_Detected then E_Missing_Source

-- If we are here, no problem with gnatcheck execution or rule
-- option or missing file definition is detected, so we can trust
-- gnatcheck results.

elsif (Detected_Non_Exempted_Violations > 0
or else Detected_Compiler_Error > 0)
and then not Arg.Brief_Mode
then E_Violation
else E_Success);
OS_Exit
(if Tool_Failures /= 0
or else Detected_Internal_Error /= 0
or else Error_From_Warning
then E_Error
elsif Missing_Rule_File_Detected then E_Missing_Rule_File
elsif Bad_Rule_Detected then E_Missing_Rule
elsif Rule_Option_Problem_Detected then E_Bad_Rules
elsif Missing_File_Detected then E_Missing_Source

-- If we are here, no problem with gnatcheck execution or rule
-- option or missing file definition is detected, so we can trust
-- gnatcheck results.

elsif (Detected_Non_Exempted_Violations > 0
or else Detected_Compiler_Error > 0)
and then not Arg.Brief_Mode
then E_Violation
else E_Success);

exception
when Parameter_Error =>
Expand Down
5 changes: 5 additions & 0 deletions testsuite/tests/gnatcheck/warnings_as_errors/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
procedure Main is
begin
goto lbl; -- FLAG (2)
<<lbl>>
end Main;
4 changes: 4 additions & 0 deletions testsuite/tests/gnatcheck/warnings_as_errors/rules.lkql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
val rules = @{
goto_statements,
restrictions: {arg: ["No_Recursion"]}
}
12 changes: 12 additions & 0 deletions testsuite/tests/gnatcheck/warnings_as_errors/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Without "warnings as errors"
============================

gnatcheck: restriction No_Recursion ignored (cannot be checked statically), use rule Recursive_Subprograms instead
main.adb:3:04: goto statement

With "warnings as errors"
=========================

gnatcheck: restriction No_Recursion ignored (cannot be checked statically), use rule Recursive_Subprograms instead
main.adb:3:04: goto statement
>>>program returned status code 2
10 changes: 10 additions & 0 deletions testsuite/tests/gnatcheck/warnings_as_errors/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
driver: gnatcheck
format: brief
input_sources:
- main.adb
lkql_rule_file: rules.lkql
tests:
- label: Without "warnings as errors"
- label: With "warnings as errors"
extra_args:
- -W

0 comments on commit 19c129a

Please sign in to comment.