Skip to content

Commit 19c129a

Browse files
committed
Add '-W/--warnings-as-errors' flag to GNATcheck
1 parent 688bc47 commit 19c129a

File tree

9 files changed

+93
-36
lines changed

9 files changed

+93
-36
lines changed

lkql_checker/doc/gnatcheck_rm/using_gnatcheck.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ The following switches control the general ``gnatcheck`` behavior
270270
Verbose mode; ``gnatcheck`` generates version information and then
271271
a trace of sources being processed.
272272

273+
.. index:: -W
274+
275+
``-W, --warnings-as-errors``
276+
Treat warnings raised by GNATcheck as errors, ensuring an erroneous return
277+
code.
278+
273279
.. index:: -o
274280

275281
``-o report_file``

lkql_checker/src/gnatcheck-options.ads

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ package Gnatcheck.Options is
466466
Help => "emit a 'rules.lkql' file containing the rules "
467467
& "configuration");
468468

469+
package Warnings_As_Errors is new Parse_Flag
470+
(Parser => Parser,
471+
Long => "--warnings-as-errors",
472+
Short => "-W",
473+
Help => "Treat warning messages as errors");
474+
469475
function Quiet_Mode return Boolean is (Quiet.Get or else Brief.Get);
470476

471477
function Short_Report return Boolean is (Brief.Get or else Short.Get);

lkql_checker/src/gnatcheck-output.adb

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,14 @@ package body Gnatcheck.Output is
543543

544544
procedure Warning (Message : String) is
545545
begin
546-
if Warning_Mode /= Quiet then
546+
if Arg.Warnings_As_Errors.Get or else Warning_Mode /= Quiet then
547547
Error (Message);
548548
end if;
549+
550+
-- Force a non-zero return code when "warnings as errors" is enabled
551+
if Arg.Warnings_As_Errors.Get then
552+
Error_From_Warning := True;
553+
end if;
549554
end Warning;
550555

551556
----------------
@@ -593,11 +598,12 @@ package body Gnatcheck.Output is
593598
Put_Line (" --target=targetname - specify a target for cross platforms");
594599
Put_Line (" --RTS=<runtime> - use runtime <runtime>");
595600
Put_Line ("");
596-
Put_Line (" -h - print out the list of the available kp detectors");
597-
Put_Line (" -jn - n is the maximal number of processes");
598-
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
599-
Put_Line (" -v - verbose mode");
600-
Put_Line (" -l - full pathname for file locations");
601+
Put_Line (" -h - print out the list of the available kp detectors");
602+
Put_Line (" -jn - n is the maximal number of processes");
603+
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
604+
Put_Line (" -v - verbose mode");
605+
Put_Line (" -W, --warnings-as-errors - treat warning messages as errors");
606+
Put_Line (" -l - full pathname for file locations");
601607
Put_Line ("");
602608
Put_Line (" --brief - brief mode, only report detections in Stderr");
603609
Put_Line (" --check-semantic - check semantic validity of the source files");
@@ -636,18 +642,19 @@ package body Gnatcheck.Output is
636642
Put_Line (" --RTS=<runtime> - use runtime <runtime>");
637643
Put_Line (" --config=<cgpr> - use configuration project <cgpr>");
638644
Put_Line ("");
639-
Put_Line (" -h - print out the list of the currently implemented rules");
640-
Put_Line (" -mn - n is the maximal number of diagnoses in Stderr");
641-
Put_Line (" (n in 0 .. 1000, 0 means no limit); default is 0");
642-
Put_Line (" -jn - n is the maximal number of processes");
643-
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
644-
Put_Line (" -t - report execution time in Stderr");
645-
Put_Line (" -v - verbose mode");
646-
Put_Line (" -l - full pathname for file locations");
647-
Put_Line (" -log - duplicate all the messages sent to Stderr in gnatcheck.log");
648-
Put_Line (" -s - short form of the report file");
649-
Put_Line (" -xml - generate report in XML format");
650-
Put_Line (" -nt - do not generate text report (enforces '-xml')");
645+
Put_Line (" -h - print out the list of the currently implemented rules");
646+
Put_Line (" -mn - n is the maximal number of diagnoses in Stderr");
647+
Put_Line (" (n in 0 .. 1000, 0 means no limit); default is 0");
648+
Put_Line (" -jn - n is the maximal number of processes");
649+
Put_Line (" -q - quiet mode (do not report detections in Stderr)");
650+
Put_Line (" -t - report execution time in Stderr");
651+
Put_Line (" -v - verbose mode");
652+
Put_Line (" -W, --warnings-as-errors - treat warning messages as errors");
653+
Put_Line (" -l - full pathname for file locations");
654+
Put_Line (" -log - duplicate all the messages sent to Stderr in gnatcheck.log");
655+
Put_Line (" -s - short form of the report file");
656+
Put_Line (" -xml - generate report in XML format");
657+
Put_Line (" -nt - do not generate text report (enforces '-xml')");
651658
Put_Line ("");
652659
Put_Line (" --show-rule - append rule names to diagnoses generated");
653660
Put_Line (" --show-instantiation-chain - show instantiation chain for reported generic construct");
@@ -668,9 +675,9 @@ package body Gnatcheck.Output is
668675
Put_Line (" -ox filename - specify the name of the XML report file (enforces '-xml')");
669676
Put_Line ("");
670677
Put_Line (" filename - the name of the Ada source file to be analyzed.");
671-
Put_Line (" Wildcards are allowed");
678+
Put_Line (" Wildcards are allowed");
672679
Put_Line (" -files=filename - the name of the text file containing a list of Ada");
673-
Put_Line (" source files to analyze");
680+
Put_Line (" source files to analyze");
674681
Put_Line (" --ignore=filename - do not process sources listed in filename");
675682
Put_Line (" --rule-file=filename - read rule configuration from the given LKQL file");
676683
Put_Line (" -r, --rule [rule_name] - enable the given rule during the GNATcheck run (this option is cumulative)");

lkql_checker/src/gnatcheck-output.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ package Gnatcheck.Output is
1818
Custom_XML_Report_File : Boolean := False;
1919
-- Undicate if custom name is specified for text or XML output file
2020

21+
Error_From_Warning : Boolean;
22+
-- Whether a warning message has been emitted while "warnings as errors"
23+
-- mode is enabled. This ensure the return code of GNATcheck is not 0.
24+
2125
procedure Print_Version_Info (Released_At : Positive);
2226
-- Prints into Stderr the tool version information in the following format:
2327
--

lkql_checker/src/gnatcheck_main.adb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -604,22 +604,25 @@ begin
604604
Gnatcheck.Rules.Rule_Table.Clean_Up;
605605
Close_Log_File;
606606

607-
OS_Exit (if Tool_Failures /= 0 or else Detected_Internal_Error /= 0
608-
then E_Error
609-
elsif Missing_Rule_File_Detected then E_Missing_Rule_File
610-
elsif Bad_Rule_Detected then E_Missing_Rule
611-
elsif Rule_Option_Problem_Detected then E_Bad_Rules
612-
elsif Missing_File_Detected then E_Missing_Source
613-
614-
-- If we are here, no problem with gnatcheck execution or rule
615-
-- option or missing file definition is detected, so we can trust
616-
-- gnatcheck results.
617-
618-
elsif (Detected_Non_Exempted_Violations > 0
619-
or else Detected_Compiler_Error > 0)
620-
and then not Arg.Brief_Mode
621-
then E_Violation
622-
else E_Success);
607+
OS_Exit
608+
(if Tool_Failures /= 0
609+
or else Detected_Internal_Error /= 0
610+
or else Error_From_Warning
611+
then E_Error
612+
elsif Missing_Rule_File_Detected then E_Missing_Rule_File
613+
elsif Bad_Rule_Detected then E_Missing_Rule
614+
elsif Rule_Option_Problem_Detected then E_Bad_Rules
615+
elsif Missing_File_Detected then E_Missing_Source
616+
617+
-- If we are here, no problem with gnatcheck execution or rule
618+
-- option or missing file definition is detected, so we can trust
619+
-- gnatcheck results.
620+
621+
elsif (Detected_Non_Exempted_Violations > 0
622+
or else Detected_Compiler_Error > 0)
623+
and then not Arg.Brief_Mode
624+
then E_Violation
625+
else E_Success);
623626

624627
exception
625628
when Parameter_Error =>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
procedure Main is
2+
begin
3+
goto lbl; -- FLAG (2)
4+
<<lbl>>
5+
end Main;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
val rules = @{
2+
goto_statements,
3+
restrictions: {arg: ["No_Recursion"]}
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Without "warnings as errors"
2+
============================
3+
4+
gnatcheck: restriction No_Recursion ignored (cannot be checked statically), use rule Recursive_Subprograms instead
5+
main.adb:3:04: goto statement
6+
7+
With "warnings as errors"
8+
=========================
9+
10+
gnatcheck: restriction No_Recursion ignored (cannot be checked statically), use rule Recursive_Subprograms instead
11+
main.adb:3:04: goto statement
12+
>>>program returned status code 2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
driver: gnatcheck
2+
format: brief
3+
input_sources:
4+
- main.adb
5+
lkql_rule_file: rules.lkql
6+
tests:
7+
- label: Without "warnings as errors"
8+
- label: With "warnings as errors"
9+
extra_args:
10+
- -W

0 commit comments

Comments
 (0)