Skip to content

Commit

Permalink
Merge branch 'topic/warnings_as_errors' into 'master'
Browse files Browse the repository at this point in the history
Add the "-W/--warnings-as-errors" flag to GNATcheck

Closes #232

See merge request eng/libadalang/langkit-query-language!328
  • Loading branch information
HugoGGuerrier committed Dec 16, 2024
2 parents ca66efd + e2b082b commit 44f8b40
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 305 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
16 changes: 6 additions & 10 deletions lkql_checker/src/gnatcheck-options.ads
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ package Gnatcheck.Options is
KP_Version : GNAT.OS_Lib.String_Access;
-- If set, the relevant GNAT version to check when running gnatkp.

type Warning_Modes is
(Quiet, -- all warnings are suppressed
Short,
Normal,
Full);

Warning_Mode : Warning_Modes := Normal;
-- Specifies the warning message level
-- '-w(q|s|n|f)

Log_Mode : Boolean := False;
-- Create the log file and duplicate in this file all the messages
-- generated by a tool.
Expand Down Expand Up @@ -466,6 +456,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
371 changes: 137 additions & 234 deletions lkql_checker/src/gnatcheck-output.adb

Large diffs are not rendered by default.

46 changes: 15 additions & 31 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 All @@ -43,39 +47,19 @@ package Gnatcheck.Output is
procedure Error_No_Tool_Name (Message : String);
-- Sends into Stderr the error message with no tool name prefix

procedure Error_In_Tty (Message : String);
-- Same as ``Error`` but send the message only if Stderr is a TTY. Also,
-- ``Message`` is not added to the stderr log file.
procedure Warning (Message : String);
-- Same as ``Error``

procedure Info (Message : String);
-- Sends Message into Stderr (with no tool name prefix).

procedure SLOC_Error
(Message : String;
SLOC : String);
-- Sends to Stderr the error message in the following format:
-- 'SLOC:Tool_Name:Message', where SLOC is the GNAT-style source location.
procedure Info_No_EOL (Message : String);
-- The same as ``Info``, but does not output a (platform-specific) EOL
-- character(s) after ``Message``.

procedure Warning (Message : String);
-- Same as Error, but do nothing if Warning_Mode = Quiet.

procedure Info
(Message : String;
Line_Len : Natural := 0;
Spacing : Natural := 0);
-- Sends Message as a separate line(s) into Stderr (with no tool name
-- prefix). If Line_Len is set to some positive value, it is treated as a
-- maximal length of the text to be placed into one output line, and if the
-- length of Message exceeds Line_Len, this procedure tries to split
-- Message treating spaces as word separators and prints the rest of the
-- Message on the next line(s). Each continuation line starts from Spacing
-- number of space characters. Message can be split only on borders of
-- words.

procedure Info_No_EOL
(Message : String;
Line_Len : Natural := 0;
Spacing : Natural := 0);
-- The same as Info, but does not "close" the last line being printed out,
-- that is, the last line does not contain a (platform-specific) EOL
-- character(s).
procedure Info_In_Tty (Message : String);
-- Same as ``Info`` but send the message only if Stderr is a TTY. Also,
-- ``Message`` is not added to the current ``Log_File``.

Indent_String : constant String := " ";
-- Used as indentation element in various output
Expand Down
5 changes: 3 additions & 2 deletions lkql_checker/src/gnatcheck-projects.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1212,13 +1212,14 @@ package body Gnatcheck.Projects is
Individual_Rules_Set := True;
end case;
if not Rules_Depreciation_Emitted then
Error_In_Tty
Info_In_Tty
("The '-rules' section is now deprecated. You should only " &
"use the '--rule' and '--rule-file' command-line options.");
Error_In_Tty
Info_In_Tty
("You can use the '--emit-lkql-rule-file' flag to " &
"automatically translate your rule configuration to the " &
"new LKQL format.");
Rules_Depreciation_Emitted := True;
end if;
end loop;
end Process_Sections;
Expand Down
4 changes: 2 additions & 2 deletions lkql_checker/src/gnatcheck-rules-rule_table.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,9 @@ package body Gnatcheck.Rules.Rule_Table is
then To_String (Instance.Defined_At)
else "command line") & Diag_Defined_At);
if not Instance_Help_Emitted then
Warning
Info
("if you want to pass multiple parameters to a rule you " &
"should use the comma separated notation: e.g. " &
"should use the comma separated notation: e.g. " &
"+RMy_Rule:Param1,Param2");
Instance_Help_Emitted := True;
end if;
Expand Down
8 changes: 3 additions & 5 deletions lkql_checker/src/gnatcheck-rules.adb
Original file line number Diff line number Diff line change
Expand Up @@ -2804,11 +2804,9 @@ package body Gnatcheck.Rules is
procedure Print_Rule_Help (Rule : Rule_Info) is
begin
Info
(Message =>
" " & Rule_Name (Rule) & " - " &
To_String (Rule.Help_Info) & " - " &
Rule.Remediation_Level'Img,
Line_Len => 0, Spacing => 0);
(" " & Rule_Name (Rule) & " - " &
To_String (Rule.Help_Info) & " - " &
Rule.Remediation_Level'Img);
end Print_Rule_Help;

---------------------------------------
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
4 changes: 2 additions & 2 deletions testsuite/tests/gnatcheck/rules_depreciation_message/test.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
gnatcheck: The '-rules' section is now deprecated. You should only use the '--rule' and '--rule-file' command-line options.
gnatcheck: You can use the '--emit-lkql-rule-file' flag to automatically translate your rule configuration to the new LKQL format.
The '-rules' section is now deprecated. You should only use the '--rule' and '--rule-file' command-line options.
You can use the '--emit-lkql-rule-file' flag to automatically translate your rule configuration to the new LKQL format.
main.adb:3:04: goto statement
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
6 changes: 3 additions & 3 deletions testsuite/tests/gnatcheck_errors/same_name_instances/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ In rule options
===============

gnatcheck: rule instance with the same name already exists: "same_alias" previously instantiated at command line
gnatcheck: if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
gnatcheck: rule instance with the same name already exists: "comp_check" previously instantiated at command line
gnatcheck: rule instance with the same name already exists: "comp_check" previously instantiated at command line
>>>program returned status code 5
Expand All @@ -11,12 +11,12 @@ In command-line
===============

gnatcheck: rule instance with the same name already exists: "goto_statements" previously instantiated at command line
gnatcheck: if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
>>>program returned status code 5

In command-line and rule options
================================

gnatcheck: rule instance with the same name already exists: "goto_statements" previously instantiated at command line (rules.txt:1:1)
gnatcheck: if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
if you want to pass multiple parameters to a rule you should use the comma separated notation: e.g. +RMy_Rule:Param1,Param2
>>>program returned status code 5

0 comments on commit 44f8b40

Please sign in to comment.