Skip to content

Commit dfae3ce

Browse files
committed
Merge branch 'topic/machu/274-gen-test-subprograms-accept-filename' into 'master'
Allow file paths with the `--gen-test-subprograms` option Closes #274 See merge request eng/ide/libadalang-tools!323
2 parents 1196719 + 9ce9fe3 commit dfae3ce

File tree

15 files changed

+167
-27
lines changed

15 files changed

+167
-27
lines changed

src/test-actions.adb

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use Utils;
4646
use Utils.Command_Lines.Common;
4747
pragma Unreferenced (Utils.Command_Lines.Common); -- ????
4848
with Utils.Formatted_Output;
49+
with Utils.String_Utilities;
4950

5051
with Utils_Debug; use Utils_Debug;
5152

@@ -239,7 +240,6 @@ package body Test.Actions is
239240
return Result;
240241
end Process_Comma_Separated_String;
241242

242-
use Ada.Strings.Fixed;
243243
begin
244244
GNATCOLL.Traces.Parse_Config_File;
245245
Test.Common.Verbose := Arg (Cmd, Verbose);
@@ -253,33 +253,22 @@ package body Test.Actions is
253253
return;
254254
end if;
255255

256-
declare
257-
Subp_Hash : constant String_Access := Arg (Cmd, Dump_Subp_Hash);
258-
Separator_Index : constant Natural :=
259-
(if Subp_Hash /= null then Index (Subp_Hash.all, ":", 1) else 0);
260-
begin
261-
if Subp_Hash /= null then
262-
if Separator_Index = 0 then
263-
Cmd_Error_No_Help
264-
("Unexpected format for --dump-subp-hash, expected <filename>"
265-
& ":<line>");
266-
end if;
267-
Test.Common.Subp_File_Name :=
268-
new String'(Subp_Hash.all (1 .. Separator_Index - 1));
269-
begin
270-
Test.Common.Subp_Line_Nbr :=
271-
Natural'Value
272-
((Subp_Hash.all (Separator_Index + 1 .. Subp_Hash'Length)));
273-
exception
274-
when others =>
275-
Cmd_Error_No_Help ("<line> must be a Natural.");
276-
end;
256+
if Arg (Cmd, Dump_Subp_Hash) /= null then
257+
declare
258+
Subp_File_Name : constant String :=
259+
Test.Common.Parse_File_And_Number
260+
("--dump-subp-hash",
261+
Arg (Cmd, Dump_Subp_Hash).all,
262+
Test.Common.Subp_Line_Nbr);
263+
begin
264+
Test.Common.Subp_File_Name := new String'(Subp_File_Name);
277265
Utils.Command_Lines.Append_File_Name
278266
(Cmd, Test.Common.Subp_File_Name.all);
279267
Test.Common.Quiet := True;
280-
return;
281-
end if;
282-
end;
268+
end;
269+
270+
return;
271+
end if;
283272

284273
Test.Common.Instrument := Arg (Cmd, Dump_Test_Inputs);
285274

@@ -1051,7 +1040,20 @@ package body Test.Actions is
10511040
(Arg (Cmd, Gen_Test_Subprograms).all);
10521041
begin
10531042
for E of Subp_List loop
1054-
Test.Common.Add_Allowed_Subprograms (E.To_String);
1043+
declare
1044+
Line_Number : Natural;
1045+
File_Path : constant String :=
1046+
Test.Common.Parse_File_And_Number
1047+
("--gen-test-subprograms",
1048+
E.To_String,
1049+
Line_Number,
1050+
Extract_File_Name => True);
1051+
begin
1052+
Test.Common.Add_Allowed_Subprograms
1053+
(File_Path
1054+
& ":"
1055+
& Utils.String_Utilities.Image (Line_Number));
1056+
end;
10551057
end loop;
10561058
end;
10571059
end if;

src/test-common.adb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,4 +1445,44 @@ package body Test.Common is
14451445
or else Allowed_Subprograms.Contains (Decl_String);
14461446
end Is_Subprogram_Allowed;
14471447

1448+
---------------------------
1449+
-- Parse_File_And_Number --
1450+
---------------------------
1451+
1452+
function Parse_File_And_Number
1453+
(Arg_Name, Arg_Val : String;
1454+
Line_Number : out Natural;
1455+
Extract_File_Name : Boolean := False) return String
1456+
is
1457+
Split_Index : constant Natural := Ada.Strings.Fixed.Index (Arg_Val, ":");
1458+
begin
1459+
if Split_Index = 0 then
1460+
Cmd_Error_No_Help
1461+
("Unexpected format for "
1462+
& Arg_Name
1463+
& ", expected <filename>"
1464+
& ":<line>");
1465+
end if;
1466+
1467+
declare
1468+
User_File_Path : constant String :=
1469+
Arg_Val (Arg_Val'First .. Split_Index - 1);
1470+
User_Line_Number : constant String :=
1471+
Arg_Val (Split_Index + 1 .. Arg_Val'Last);
1472+
begin
1473+
begin
1474+
Line_Number := Natural'Value (User_Line_Number);
1475+
exception
1476+
when Constraint_Error =>
1477+
Cmd_Error_No_Help
1478+
(User_Line_Number & " must be a positive number");
1479+
end;
1480+
1481+
return
1482+
(if Extract_File_Name
1483+
then Ada.Directories.Simple_Name (User_File_Path)
1484+
else User_File_Path);
1485+
end;
1486+
end Parse_File_And_Number;
1487+
14481488
end Test.Common;

src/test-common.ads

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ package Test.Common is
298298
-- The root directory to place the test file hierarchy in case of
299299
-- --separate-root option.
300300

301-
Subp_File_Name : String_Access;
301+
Subp_File_Name : String_Access := null;
302302
-- Name of the file from which we want to extract the hash in case of the
303303
-- --dump-subp-hash option
304304

@@ -468,12 +468,26 @@ package Test.Common is
468468
-- not explicitly allowed will be ignored during test case generation.
469469
-- The `Subp_Decl` parameter should have the following format
470470
-- `<subp_decl_filename>:<line_number>`.
471+
-- Note: `subp_decl_filename` can be an absolute or relative path to the
472+
-- source file. The filename will be extracted from the given path.
471473

472474
function Is_Subprogram_Allowed (Subp : Basic_Decl'Class) return Boolean;
473475
-- Return if `Subp_Name` test case generation is allowed. If no subprograms
474476
-- have been allowed before (the list of allowed subprograms is empty) all
475477
-- subprograms are considered to be allowed.
476478

479+
function Parse_File_And_Number
480+
(Arg_Name, Arg_Val : String;
481+
Line_Number : out Natural;
482+
Extract_File_Name : Boolean := False) return String;
483+
-- Parse a file name and a line number separated by a single colon for
484+
-- an argument `Arg_Name` and its associated value `Arg_Val`.
485+
-- The parsed file name will be returned. If `Extract_File_Name` is set,
486+
-- only the file name is returned.
487+
--
488+
-- CAUTION: **This function will call `Cmd_Error_No_Help`** if the input is
489+
-- ill formated. This function is intended to handle user inputs.
490+
477491
Preprocessor_Config : Libadalang.Preprocessing.Preprocessor_Data;
478492
-- Preprocessor config for the loaded user project.
479493
-- Might be null if the project isn't using preprocessing. The
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package Pkg is
2+
function Identity (X : Integer) return Integer is (X);
3+
end Pkg;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gnattest: Unexpected format for --gen-test-subprograms, expected <filename>:<line>
2+
gnattest: abc must be a positive number
3+
>>>program returned status code 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
gnattest \
4+
-P user_project.gpr \
5+
--gen-test-vectors \
6+
--gen-test-subprograms=./src/foo.ads
7+
gnattest \
8+
-P user_project.gpr \
9+
--gen-test-vectors \
10+
--gen-test-subprograms=./src/foo.ads:abc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: |
2+
Check that gnattest rejects erroneous inputs that are not formatted following
3+
this `<filename>:<number>` pattern.
4+
5+
driver: shell_script
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project User_Project is
2+
for Source_Dirs use ("src");
3+
for Create_Missing_Dirs use "True";
4+
for Object_Dir use "obj";
5+
package Compiler is
6+
for Switches ("Ada") use
7+
("-gnatwI", "-g", "-gnat2022", "-gnatf", "-gnatwa");
8+
end Compiler;
9+
end User_Project;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project Prj is
2+
for Source_Dirs use ("src", "src/nested");
3+
end Prj;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Foo is
2+
function Plus_Two (X : Integer) return Integer is (X + 2);
3+
function Double (X : Integer) return Integer is (X * 2);
4+
-- Ignored
5+
end Foo;

0 commit comments

Comments
 (0)