Skip to content

Commit 9f88d9d

Browse files
committed
Change the way provided LKQL rule files are resolved
When provide through the CLI, an LKQL rule file is resolved relatively to the current working dir. When provided through a GPR file, an LKQL rule file is resolved relatively to this project file.
1 parent ed9aa8e commit 9f88d9d

File tree

16 files changed

+99
-30
lines changed

16 files changed

+99
-30
lines changed

lkql_checker/doc/gnatcheck_rm/using_gnatcheck.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ The following switches control the general ``gnatcheck`` behavior
184184

185185
``--rule-file=filename``
186186
Load the given file as an LKQL rule options file (see :ref:`LKQL_options_file`
187-
for more information).
187+
for more information). If not absolute, the provided path is relative to the
188+
current working directory.
188189

189190
.. index:: -r, --rule
190191

@@ -352,9 +353,9 @@ GNATcheck:
352353
defining this attribute, then, values are concatenated.
353354

354355
``Rule_File``
355-
Value is a path to a LKQL rule file. The path is relative to the project
356-
file that defines this attribute. See :ref:`LKQL_options_file` for more
357-
information.
356+
Value is a path to a LKQL rule file. If not absolute, the path is relative
357+
to the project file that defines this attribute.
358+
See :ref:`LKQL_options_file` for more information.
358359

359360
If the ``--rule-file`` switch is set when calling ``gnatcheck`` on a project
360361
file defining this attribute, then, an error is emitted and ``gnatcheck``

lkql_checker/src/gnatcheck-options.ads

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ package Gnatcheck.Options is
214214
-- processed, keeps the name of this file, otherwise is null.
215215

216216
LKQL_Rule_File_Name : Unbounded_String := Null_Unbounded_String;
217-
-- Name of the LKQL file to process as a rule file.
217+
-- Name of the LKQL file to process as a rule file. We assume that the
218+
-- stored value is an absolute path to the LKQL rule file.
218219

219220
---------------------
220221
-- Project support --

lkql_checker/src/gnatcheck-projects.adb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,12 @@ package body Gnatcheck.Projects is
261261
end if;
262262

263263
-- Process the LKQL rule file
264-
-- Process the rule list
265264
if Proj.Has_Attribute (Rule_File_Attr) then
266265
declare
267266
Rule_File : constant String :=
268267
Load_Single_Attribute (Rule_File_Attr);
269268
begin
270-
Set_LKQL_Rule_File
271-
(My_Project.Get_Project_Relative_File (Rule_File));
269+
Set_LKQL_Rule_File (Rule_File, True);
272270
end;
273271
end if;
274272

@@ -1111,11 +1109,19 @@ package body Gnatcheck.Projects is
11111109
-- Set_LKQL_Rule_File --
11121110
------------------------
11131111

1114-
procedure Set_LKQL_Rule_File (File : String) is
1112+
procedure Set_LKQL_Rule_File (File : String; Project_Relative : Boolean) is
11151113
use Ada.Strings.Unbounded;
1114+
1115+
Resolved_File : constant String :=
1116+
(if Is_Absolute_Path (File)
1117+
then File
1118+
else
1119+
(if Project_Relative
1120+
then Gnatcheck_Prj.Get_Project_Relative_File (File)
1121+
else Normalize_Pathname (File)));
11161122
begin
11171123
if LKQL_Rule_File_Name = Null_Unbounded_String then
1118-
LKQL_Rule_File_Name := To_Unbounded_String (File);
1124+
LKQL_Rule_File_Name := To_Unbounded_String (Resolved_File);
11191125
else
11201126
Error ("only one LKQL configuration file is allowed");
11211127
Rule_Option_Problem_Detected := True;

lkql_checker/src/gnatcheck-projects.ads

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,15 @@ package Gnatcheck.Projects is
255255
-- Use ``Add_Rule_Option`` to forge a new rule option enabling the given
256256
-- rule without any parameter.
257257

258-
procedure Set_LKQL_Rule_File (File : String);
258+
procedure Set_LKQL_Rule_File (File : String; Project_Relative : Boolean);
259259
-- Set the given ``File`` as the LKQL rule file to process during the
260-
-- execution of ``Process_Rule_Options``.
260+
-- execution of ``Process_Rule_Options``. If a rule file has already been
261+
-- set, this function displays an error and set the
262+
-- ``Rule_Option_Problem_Detected`` flag to True.
263+
-- If the provided ``File`` isn't an absolute path, if ``Project_Relative``
264+
-- is set to ``True``, resolve the provided file relatively to
265+
-- the current project file (if any). Else, resolve ``File`` relatively to
266+
-- the current working directory.
261267

262268
function Is_Rule_Options_Empty return Boolean;
263269
-- Get whether the rule options are empty.

lkql_checker/src/gnatcheck-rules-rule_table.adb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,6 @@ package body Gnatcheck.Rules.Rule_Table is
804804

805805
procedure Process_LKQL_Rule_File (LKQL_RF_Name : String)
806806
is
807-
Rule_File_Absolute_Path : constant String :=
808-
Get_Rule_File_Name (LKQL_RF_Name);
809807
JSON_Config_File_Name : constant String :=
810808
Global_Report_Dir.all & "gnatcheck-rules.json.out";
811809
Parser_Pid : Process_Id;
@@ -823,21 +821,21 @@ package body Gnatcheck.Rules.Rule_Table is
823821
Instance_Object : JSON_Value) is
824822
begin
825823
Process_Rule_Object
826-
(Rule_File_Absolute_Path, String (Instance_Id), Instance_Object);
824+
(LKQL_RF_Name, String (Instance_Id), Instance_Object);
827825
end Rule_Object_Mapper;
828826

829827
begin
830828
-- Ensure that the provided rule file exists
831-
if not Is_Regular_File (Rule_File_Absolute_Path) then
832-
Error ("can not locate LKQL rule file " & Rule_File_Absolute_Path);
829+
if not Is_Regular_File (LKQL_RF_Name) then
830+
Error ("can not locate LKQL rule file " & LKQL_RF_Name);
833831
Missing_Rule_File_Detected := True;
834832
return;
835833
end if;
836834

837835
-- Call the LKQL rule config file parser and parse its result
838836
Parser_Pid :=
839837
Spawn_LKQL_Rule_File_Parser
840-
(Rule_File_Absolute_Path, JSON_Config_File_Name);
838+
(LKQL_RF_Name, JSON_Config_File_Name);
841839
Wait_Process (Waited_Pid, Success);
842840

843841
if Parser_Pid /= Waited_Pid or else not Success then

lkql_checker/src/gnatcheck_main.adb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ begin
503503

504504
-- Add the command-line LKQL rule file to the rule options
505505
if Arg.Rule_File.Get /= Null_Unbounded_String then
506-
Set_LKQL_Rule_File (To_String (Arg.Rule_File.Get));
506+
Set_LKQL_Rule_File (To_String (Arg.Rule_File.Get), False);
507507
end if;
508508

509509
-- Get the default LKQL rule file if none has been specified
@@ -512,7 +512,9 @@ begin
512512
Def_LKQL : constant String := Default_LKQL_Rule_Options_File;
513513
begin
514514
if Is_Regular_File (Def_LKQL) then
515-
Set_LKQL_Rule_File (Def_LKQL);
515+
-- The default rule file is already resolved relatively to the
516+
-- current project file.
517+
Set_LKQL_Rule_File (Def_LKQL, False);
516518
end if;
517519
end;
518520
end if;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
procedure Main is
2+
begin
3+
goto lbl; -- FLAG
4+
<<lbl>>
5+
6+
null;
7+
null; -- FLAG
8+
end Main;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val rules = @{
2+
redundant_null_statements
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project With_Invalid is
2+
package Check is
3+
for Rule_File use "rules.lkql";
4+
end Check;
5+
end With_Invalid;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project With_Valid is
2+
package Check is
3+
for Rule_File use "prj_rules.lkql";
4+
end Check;
5+
end With_Valid;

0 commit comments

Comments
 (0)