Skip to content

Commit 6a6dda8

Browse files
Merge branch 'mr/273-add-mechanism-to-expose-the-hash-function-used-to-index-serialized-json-tests' into 'master'
Add the --dump-subp-hash switch See merge request eng/ide/libadalang-tools!321
2 parents d234f23 + 910d85b commit 6a6dda8

File tree

10 files changed

+141
-2
lines changed

10 files changed

+141
-2
lines changed

src/test-actions.adb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ with Ada.Strings.Fixed;
7070

7171
with TGen.Libgen;
7272

73+
with Libadalang.Common;
74+
7375
package body Test.Actions is
7476

7577
SPT : GNATCOLL.Projects.Project_Tree
@@ -237,6 +239,7 @@ package body Test.Actions is
237239
return Result;
238240
end Process_Comma_Separated_String;
239241

242+
use Ada.Strings.Fixed;
240243
begin
241244
GNATCOLL.Traces.Parse_Config_File;
242245
Test.Common.Verbose := Arg (Cmd, Verbose);
@@ -250,6 +253,34 @@ package body Test.Actions is
250253
return;
251254
end if;
252255

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;
277+
Utils.Command_Lines.Append_File_Name
278+
(Cmd, Test.Common.Subp_File_Name.all);
279+
Test.Common.Quiet := True;
280+
return;
281+
end if;
282+
end;
283+
253284
Test.Common.Instrument := Arg (Cmd, Dump_Test_Inputs);
254285

255286
Test.Common.Lang_Version :=
@@ -1135,10 +1166,18 @@ package body Test.Actions is
11351166
-----------
11361167

11371168
procedure Final (Tool : in out Test_Tool; Cmd : Command_Line) is
1169+
use Ada.Strings.Unbounded;
11381170
Src_Prj : constant String :=
11391171
Tool.Project_Tree.Root_Project.Project_Path.Display_Full_Name;
11401172
begin
11411173

1174+
-- Abort here if we the switch --dump-subp-hash is on. This return
1175+
-- should not be moved further down.
1176+
1177+
if Test.Common.Subp_File_Name /= null then
1178+
return;
1179+
end if;
1180+
11421181
-- If the tool project is an aggregate one, exit early and do nothing.
11431182
-- The aggregated projects will be processed in sequence in subprocess
11441183
-- calls made by the driver.
@@ -1238,6 +1277,8 @@ package body Test.Actions is
12381277
BOM_Seen : Boolean;
12391278
Unit : Analysis_Unit)
12401279
is
1280+
use Libadalang.Common;
1281+
use Ada.Strings.Unbounded;
12411282
pragma Unreferenced (Tool, Input, BOM_Seen); -- ????
12421283
begin
12431284
if Debug_Flag_V then
@@ -1246,6 +1287,40 @@ package body Test.Actions is
12461287
PP_Trivia (Unit);
12471288
end if;
12481289

1290+
if Test.Common.Subp_File_Name /= null
1291+
and then Test.Common.Subp_File_Name.all = File_Name
1292+
then
1293+
declare
1294+
Found_Hash : Boolean := False;
1295+
1296+
function Visit (Node : Ada_Node'Class) return Visit_Status;
1297+
1298+
function Visit (Node : Ada_Node'Class) return Visit_Status is
1299+
begin
1300+
if Kind (Node) in Ada_Basic_Subp_Decl
1301+
and then Natural (Node.Sloc_Range.Start_Line)
1302+
= Test.Common.Subp_Line_Nbr
1303+
then
1304+
Ada.Text_IO.Put (Test.Common.Mangle_Hash_16 (Node));
1305+
Found_Hash := True;
1306+
return Stop;
1307+
end if;
1308+
return Into;
1309+
end Visit;
1310+
begin
1311+
Traverse (Root (Unit), Visit'Access);
1312+
if not Found_Hash then
1313+
Ada.Text_IO.Put
1314+
("Subprogram in "
1315+
& Test.Common.Subp_File_Name.all
1316+
& " at line "
1317+
& Natural'Image (Test.Common.Subp_Line_Nbr)
1318+
& " could not be found.");
1319+
end if;
1320+
return;
1321+
end;
1322+
end if;
1323+
12491324
if Test.Common.Harness_Only then
12501325
Test.Harness.Process_Source (Unit);
12511326
else
@@ -1374,6 +1449,8 @@ package body Test.Actions is
13741449
(" --gen-test-num=n - Specify the number of test inputs to be generated (experimental, defaults to 5)\n");
13751450
Put
13761451
(" --gen-test-subprograms=file:line - Specify a comma separated list of subprograms declared at file:line to generate test cases for\n");
1452+
Put
1453+
(" --dump-subp-hash=file:line - Print the hash of the subprogram at file:line in the standard output and bypass all other swicthes.\n");
13771454
Put
13781455
(" --serialized-test-dir=dir - Specify in which directory test inputs should be generated (experimental)\n");
13791456
Put

src/test-command_lines.ads

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ package Test.Command_Lines is
9595
Gen_Test_Subprograms,
9696
Serialized_Test_Dir,
9797
Cov_Level,
98-
Minimization_Filter);
98+
Minimization_Filter,
99+
Dump_Subp_Hash);
99100

100101
package Test_String_Switches is new
101102
String_Switches (Descriptor, Test_Strings);
@@ -117,7 +118,8 @@ package Test.Command_Lines is
117118
Gen_Test_Subprograms => '=',
118119
Serialized_Test_Dir => '=',
119120
Cov_Level => '=',
120-
Minimization_Filter => '=']);
121+
Minimization_Filter => '=',
122+
Dump_Subp_Hash => '=']);
121123

122124
type Test_String_Seqs is (Exclude_From_Stubbing);
123125

src/test-common.ads

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ 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;
302+
-- Name of the file from which we want to extract the hash in case of the
303+
-- --dump-subp-hash option
304+
305+
Subp_Line_Nbr : Natural;
306+
-- Line in Subp_File_Name from which we want to extract the hash in case of
307+
-- the --dump-subp-hash option
308+
301309
Test_Dir_Name : GNAT.OS_Lib.String_Access :=
302310
new String'("gnattest" & GNAT.OS_Lib.Directory_Separator & "tests");
303311
-- Name of default directory to place test files
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
success
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
cd test/
4+
gnattest -Psimple --dump-subp-hash=src/simple.ads:5 > my_hash 2> /dev/null
5+
gnattest -Psimple src/simple.ads --gen-test-vectors
6+
grep -o -E "^..([a-z]|[0-9]){16}" obj/gnattest/tests/JSON_Tests/simple.json | cut -c 3- > hash_ref
7+
8+
if cmp -s my_hash hash_ref; then
9+
printf 'success\n'
10+
else
11+
printf 'error: The two hashes do not match. expected:\n'
12+
cat hash_ref
13+
printf 'but got:\n'
14+
cat my_hash
15+
printf '\n'
16+
fi;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
description: Test that asserts that --dump-subp-hash=<filename>:<line>
2+
indeed prints the correct hash.
3+
4+
driver: shell_script
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project Simple is
2+
for Source_Dirs use ("src");
3+
for Object_Dir use "obj";
4+
for Main use ("main.adb");
5+
end Simple;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
with Simple; use Simple;
2+
3+
procedure Main is
4+
X : constant T := 8;
5+
Y : constant T := 7;
6+
Z : T;
7+
begin
8+
Z := Foo (X, Y);
9+
null;
10+
end Main;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package body Simple is
2+
3+
function Foo (X, Y : T) return T is
4+
begin
5+
return X + Y;
6+
end Foo;
7+
8+
end Simple;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Simple is
2+
3+
type T is range 0 .. 10;
4+
5+
function Foo (X, Y : T) return T
6+
with Post => Foo'Result < 15;
7+
8+
end Simple;

0 commit comments

Comments
 (0)