Skip to content

Commit 80d920a

Browse files
committed
Get KPs' impacts from impact-db fetched data
Data is formatted as JSON in kp.json in the KP rules directory.
1 parent 1d456eb commit 80d920a

File tree

4 files changed

+173
-28
lines changed

4 files changed

+173
-28
lines changed

lkql_checker/share/lkql/kp/kp.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"impacts": {
3+
"kp_19423": "22.*,23.*,24.*",
4+
"kp_19341": "24.*",
5+
"kp_19237": "23.*,24.1",
6+
"kp_19447": "24.*",
7+
"kp_19501": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.*,19.*,20.*,21.*,22.*,23.*,24.*",
8+
"kp_19142": "24.*",
9+
"kp_19312": "24.*",
10+
"kp_19529": "24.*",
11+
"kp_19134": "23.*,24.*",
12+
"kp_19279": "23.*,24.*",
13+
"kp_19038": "24.1",
14+
"kp_19159": "23.*,24.*",
15+
"kp_19198": "19.1,19.2,19.3,19.4,19.5,19.6,19.7,19.8,20.*,21.*,22.*,23.*,24.1",
16+
"kp_va03_035": "22.*",
17+
"kp_u923_018": "21.*",
18+
"kp_v329_046": "22.1",
19+
"kp_18701": "23.*",
20+
"kp_ua12_036": "21.*,22.*",
21+
"kp_vb20_002": "22.*",
22+
"kp_18637": "23.*",
23+
"kp_u727_033": "7.4.*,17.*,18.*,19.*,20.*,21.*",
24+
"kp_u812_034": "21.*",
25+
"kp_w330_020": "20.*,21.*,22.*,23.1",
26+
"kp_v907_005": "22.*",
27+
"kp_v924_001": "22.*",
28+
"kp_va06_034": "22.*",
29+
"kp_ub15_001": "18.*,19.*,20.*,21.*",
30+
"kp_18718": "23.*",
31+
"kp_ub22_022": "19.*,20.*,21.*",
32+
"kp_18614": "20.*,21.*,22.*,23.*",
33+
"kp_v216_031": "21.*",
34+
"kp_v120_015": "7.4.1,7.4.2,7.4.3,7.4.4,7.4.5,7.4.6,7.4.7,17.*",
35+
"kp_v127_049": "21.*,22.1",
36+
"kp_v302_021": "21.*,22.1",
37+
"kp_u630_012": "21.*",
38+
"kp_v719_024": "22.*",
39+
"kp_18636": "22.*,23.*",
40+
"kp_18801": "23.*",
41+
"kp_u629_002": "21.*",
42+
"kp_v505_020": "22.*",
43+
"kp_v114_047": "18.*,19.*,20.*,21.*,22.1",
44+
"kp_vb24_008": "23.1",
45+
"kp_v616_018": "22.*",
46+
"kp_uc22_009": "22.1",
47+
"kp_u928_018": "22.*,23.1",
48+
"kp_v206_001": "22.1",
49+
"kp_va18_003": "21.*,22.*",
50+
"kp_v214_016": "21.*,22.*,23.1",
51+
"kp_v407_011": "20.*,21.*,22.1",
52+
"kp_v624_025": "22.*",
53+
"kp_v720_003": "22.*",
54+
"kp_v708_029": "22.*",
55+
"kp_u702_038": "21.*",
56+
"kp_18926": "23.*,24.1",
57+
"kp_v404_040": "19.1,19.2,19.3,19.4,19.5,19.6,20.1,20.2,21.1,21.2,21.3,21.4,22.1",
58+
"kp_v712_039": "22.*",
59+
"kp_u803_030": "21.*",
60+
"kp_u701_015": "21.*,22.*",
61+
"kp_q309_014": "7.1.*,7.2.*,7.3.*,7.4.*,17.*",
62+
"kp_r517_023": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.1,18.2",
63+
"kp_qc02_001": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.1,18.2",
64+
"kp_r511_017": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.*",
65+
"kp_r410_028": "7.1.*,7.2.*,7.3.*",
66+
"kp_r316_001": "18.*,19.1,19.2,19.3,19.4,20.1,20.2",
67+
"kp_q609_033": "7.1.*,7.2.*,7.3.*,7.4.*",
68+
"kp_p226_024": "7.1.*,7.2.*,7.3.*,7.4.1,7.4.2,7.4.3",
69+
"kp_ob03_009": "19.*",
70+
"kp_ta30_032": "20.1,20.2",
71+
"kp_s724_042": "19.*,20.1",
72+
"kp_s521_014": "19.*",
73+
"kp_tb09_038": "20.1,20.2",
74+
"kp_s114_015": "18.*,19.*",
75+
"kp_s814_034": "17.*,18.*,19.*",
76+
"kp_ta10_002": "21.*,22.*,23.1",
77+
"kp_u310_012": "22.*",
78+
"kp_s722_023": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.1,18.2,19.1,19.2,19.3",
79+
"kp_u225_018": "20.1,20.2,21.1,21.2,21.3",
80+
"kp_t708_064": "20.1,20.2",
81+
"kp_tc01_050": "19.1,19.2,19.3,19.4,20.1,20.2,21.1",
82+
"kp_ta15_022": "18.1,18.2,18.3,19.1,19.2,19.3,19.4,19.5,19.6,20.1,20.2",
83+
"kp_t602_028": "20.*",
84+
"kp_u126_006": "20.1,20.2,21.1",
85+
"kp_t528_043": "18.*,19.1,19.2,19.3,19.4,19.5,20.1,20.2",
86+
"kp_s124_035": "7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.*,19.*",
87+
"kp_t420_033": "18.*,19.*,20.1",
88+
"kp_tb23_035": "20.1,20.2",
89+
"kp_t805_037": "20.1,20.2",
90+
"kp_tb25_028": "20.*",
91+
"kp_s621_051": "19.*",
92+
"kp_u316_028": "20.1,20.2,21.1"
93+
},
94+
"gnat": [
95+
"7.1",
96+
"7.2",
97+
"7.3",
98+
"7.4",
99+
"17",
100+
"18",
101+
"19",
102+
"20",
103+
"21",
104+
"22",
105+
"23",
106+
"24",
107+
"25",
108+
"26"
109+
]
110+
}

lkql_checker/src/rule_commands.adb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ package body Rule_Commands is
9393
function Create_Rule_Command
9494
(Lkql_File_Path : String;
9595
Ctx : L.Analysis_Context;
96+
Impacts : JSON_Value;
9697
Rc : out Rule_Command) return Boolean
9798
is
9899
Root : constant L.Lkql_Node :=
@@ -132,8 +133,6 @@ package body Rule_Commands is
132133
Remediation_Arg : constant L.Arg :=
133134
Check_Annotation.P_Arg_With_Name
134135
(To_Unbounded_Text ("remediation"));
135-
Impact_Arg : constant L.Arg :=
136-
Check_Annotation.P_Arg_With_Name (To_Unbounded_Text ("impact"));
137136
Target_Arg : constant L.Arg :=
138137
Check_Annotation.P_Arg_With_Name (To_Unbounded_Text ("target"));
139138
Msg : Unbounded_Text_Type;
@@ -218,24 +217,25 @@ package body Rule_Commands is
218217
Get_Text (Category_Arg, To_Unbounded_Text ("Misc"), Category);
219218
Get_Text (Subcategory_Arg, To_Unbounded_Text (""), Subcategory);
220219

221-
if not Impact_Arg.Is_Null then
222-
Check_String (Impact_Arg);
223-
220+
if Impacts /= JSON_Null then
224221
declare
225-
Str : constant String :=
226-
To_String (Impact_Arg.P_Expr.As_String_Literal.Text);
222+
Impact_Value : constant JSON_Value :=
223+
Impacts.Get (To_UTF8 (To_Lower (Name)));
227224
begin
228-
Impact :=
229-
new Regexp'(Compile ("{" &
230-
Str (Str'First + 1 .. Str'Last - 1) &
231-
"}",
232-
Glob => True, Case_Sensitive => False));
233-
225+
if Impact_Value /= JSON_Null then
226+
Impact :=
227+
new Regexp'
228+
(Compile
229+
("{" & Impact_Value.Get & "}",
230+
Glob => True,
231+
Case_Sensitive => False));
232+
end if;
234233
exception
235234
when others =>
236-
raise Rule_Error with
237-
"invalid argument for @" &
238-
To_String (Check_Annotation.F_Name.Text);
235+
raise Rule_Error
236+
with
237+
"invalid impact entry for "
238+
& To_String (Check_Annotation.F_Name.Text);
239239
end;
240240
end if;
241241

lkql_checker/src/rule_commands.ads

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ with Ada.Containers.Vectors;
77

88
with GNAT.Regexp;
99

10-
with Langkit_Support.Diagnostics; use Langkit_Support.Diagnostics;
10+
with GNATCOLL.JSON; use GNATCOLL.JSON;
11+
12+
with Langkit_Support.Diagnostics; use Langkit_Support.Diagnostics;
1113
with Langkit_Support.Generic_API.Analysis;
1214
use Langkit_Support.Generic_API.Analysis;
13-
with Langkit_Support.Text; use Langkit_Support.Text;
15+
with Langkit_Support.Text; use Langkit_Support.Text;
1416

1517
with Libadalang.Analysis;
1618

@@ -122,6 +124,7 @@ package Rule_Commands is
122124
function Create_Rule_Command
123125
(Lkql_File_Path : String;
124126
Ctx : L.Analysis_Context;
127+
Impacts : JSON_Value;
125128
Rc : out Rule_Command) return Boolean;
126129
-- Create a Rule_Command value with the given name and arguments and
127130
-- store it in ``Rc``. Return ``True`` if this succeeded, ie. the file

lkql_checker/src/rules_factory.adb

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
with Ada.Environment_Variables;
77

88
with GNAT.OS_Lib;
9+
with GNAT.Strings;
910

11+
with GNATCOLL.JSON; use GNATCOLL.JSON;
1012
with GNATCOLL.Opt_Parse; use GNATCOLL.Opt_Parse;
1113
with GNATCOLL.Strings; use GNATCOLL.Strings;
1214
with GNATCOLL.Utils;
@@ -20,17 +22,23 @@ package body Rules_Factory is
2022
(Dirs : Path_Array) return Virtual_File_Array;
2123
-- Return the absolute path of the directory containing the LKQL programs
2224

25+
function Get_Impacts (Rules_Dirs : Virtual_File_Array) return JSON_Value;
26+
-- Return all KPs' impacts. Impacts are stored in a JSON file named
27+
-- 'kp.json'. This file should be located in one of the directories from
28+
-- "Rules_Dirs".
29+
2330
---------------
2431
-- All_Rules --
2532
---------------
2633

2734
function All_Rules
28-
(Ctx : L.Analysis_Context;
29-
Dirs : Path_Array := No_Paths) return Rule_Vector
35+
(Ctx : L.Analysis_Context; Dirs : Path_Array := No_Paths)
36+
return Rule_Vector
3037
is
3138
Rules_Dirs : constant Virtual_File_Array := Get_Rules_Directories (Dirs);
3239
Rules : Rule_Vector := Rule_Vectors.Empty_Vector;
3340
Seen : String_Sets.Set := String_Sets.Empty_Set;
41+
Impacts : constant JSON_Value := Get_Impacts (Rules_Dirs);
3442

3543
begin
3644
-- We search (non recursively) for all .lkql files in the Rules_Dir.
@@ -45,13 +53,14 @@ package body Rules_Factory is
4553
begin
4654
for File of Dir.all loop
4755
if File.File_Extension = +".lkql"
48-
and then not Seen.Contains (+File.Full_Name)
56+
and then not Seen.Contains (+File.Full_Name)
4957
then
5058
declare
5159
Rc : Rule_Command;
5260

53-
Has_Rule : constant Boolean := Create_Rule_Command
54-
(+File.Full_Name, Ctx, Rc);
61+
Has_Rule : constant Boolean :=
62+
Create_Rule_Command
63+
(+File.Full_Name, Ctx, Impacts, Rc);
5564
begin
5665
if Has_Rule then
5766
Rules.Append (Rc);
@@ -73,8 +82,7 @@ package body Rules_Factory is
7382
-- Get_Rules_Directories --
7483
---------------------------
7584

76-
function Get_Rules_Directories
77-
(Dirs : Path_Array) return Virtual_File_Array
85+
function Get_Rules_Directories (Dirs : Path_Array) return Virtual_File_Array
7886
is
7987
function Add_Rules_Path (Path : String) return Boolean;
8088

@@ -94,10 +102,11 @@ package body Rules_Factory is
94102
end if;
95103

96104
declare
97-
Custom_Checkers_Dirs : Virtual_File_Array
98-
(1 .. Integer (Dirs'Length) + Integer (Lkql_Rules_Paths.Length));
105+
Custom_Checkers_Dirs :
106+
Virtual_File_Array
107+
(1 .. Integer (Dirs'Length) + Integer (Lkql_Rules_Paths.Length));
99108

100-
Index : Positive := 1;
109+
Index : Positive := 1;
101110
begin
102111
for Dir of Dirs loop
103112
Custom_Checkers_Dirs (Index) := Create (+To_String (Dir));
@@ -113,4 +122,27 @@ package body Rules_Factory is
113122
end;
114123
end Get_Rules_Directories;
115124

125+
-----------------
126+
-- Get_Impacts --
127+
-----------------
128+
129+
function Get_Impacts (Rules_Dirs : Virtual_File_Array) return JSON_Value is
130+
begin
131+
for Rules_Dir of Rules_Dirs loop
132+
declare
133+
JSON_Filename : constant Virtual_File := Rules_Dir / "kp.json";
134+
File_Content : GNAT.Strings.String_Access;
135+
Result : JSON_Value;
136+
begin
137+
if Is_Regular_File (JSON_Filename) then
138+
File_Content := Read_File (JSON_Filename);
139+
Result := Read (File_Content.all).Get ("impacts");
140+
GNAT.Strings.Free (File_Content);
141+
return Result;
142+
end if;
143+
end;
144+
end loop;
145+
return JSON_Null;
146+
end Get_Impacts;
147+
116148
end Rules_Factory;

0 commit comments

Comments
 (0)