Skip to content

Commit 46e2848

Browse files
committed
Move instance creation outside of parameter processing functions
This ensure processed instance already have a declaration location. Also this is a good simplification of code flow.
1 parent 24b31a3 commit 46e2848

File tree

3 files changed

+103
-254
lines changed

3 files changed

+103
-254
lines changed

lkql_checker/src/gnatcheck-rules-rule_table.adb

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ package body Gnatcheck.Rules.Rule_Table is
3737
-- Local subprograms --
3838
-----------------------
3939

40+
function Get_Or_Create_Instance
41+
(Id : Rule_Id; Instance_Name, Defined_At : String)
42+
return Rule_Instance_Access;
43+
-- Helper function to create an instance with the given name and return a
44+
-- pointer to it. This function adds the created instance to the global
45+
-- instance map.
46+
4047
Fatal_Error : exception;
4148

4249
type Rule_File_Record is record
@@ -128,6 +135,40 @@ package body Gnatcheck.Rules.Rule_Table is
128135
-- This function populates the `All_Rules` table according to the given
129136
-- rule object.
130137

138+
----------------------------
139+
-- Get_Or_Create_Instance --
140+
----------------------------
141+
142+
function Get_Or_Create_Instance
143+
(Id : Rule_Id; Instance_Name, Defined_At : String)
144+
return Rule_Instance_Access
145+
is
146+
Rule : constant Rule_Info := All_Rules (Id);
147+
Normalized_Rule_Name : constant String :=
148+
To_Lower (To_String (Rule.Name));
149+
Normalized_Instance_Name : constant String := To_Lower (Instance_Name);
150+
Instance : Rule_Instance_Access := null;
151+
begin
152+
-- If the instance name is already registered
153+
if All_Rule_Instances.Contains (Normalized_Instance_Name) then
154+
return All_Rule_Instances (Normalized_Instance_Name);
155+
end if;
156+
157+
-- Else, create a new instance and return it
158+
if Normalized_Instance_Name = Normalized_Rule_Name then
159+
Instance := Rule.Create_Instance (Is_Alias => False);
160+
else
161+
Instance := Rule.Create_Instance (Is_Alias => True);
162+
Instance.Alias_Name := To_Unbounded_String (Instance_Name);
163+
end if;
164+
Instance.Rule := Id;
165+
Instance.Source_Mode := General;
166+
Instance.Defined_At :=
167+
Ada.Strings.Unbounded.To_Unbounded_String (Defined_At);
168+
Turn_Instance_On (Instance);
169+
return Instance;
170+
end Get_Or_Create_Instance;
171+
131172
-----------------------
132173
-- Check_For_Looping --
133174
-----------------------
@@ -1213,21 +1254,20 @@ package body Gnatcheck.Rules.Rule_Table is
12131254
-- If the rule is not compiler-based, we process it normally
12141255

12151256
else
1257+
Instance :=
1258+
Get_Or_Create_Instance
1259+
(Id => Rule,
1260+
Instance_Name => To_String (Instance_Name),
1261+
Defined_At => Defined_At);
12161262
if Word_Start = 0 then
12171263
All_Rules (Rule).Process_Rule_Parameter
1218-
(Rule => Rule,
1219-
Instance_Name => To_String (Instance_Name),
1220-
Param => "",
1221-
Enable => Enable,
1222-
Defined_At => Defined_At);
1264+
(Instance => Instance, Param => "", Enable => Enable);
12231265
else
12241266
while Word_Start /= 0 loop
12251267
All_Rules (Rule).Process_Rule_Parameter
1226-
(Rule => Rule,
1227-
Instance_Name => To_String (Instance_Name),
1228-
Param => Option (Word_Start .. Word_End),
1229-
Enable => Enable,
1230-
Defined_At => Defined_At);
1268+
(Instance => Instance,
1269+
Param => Option (Word_Start .. Word_End),
1270+
Enable => Enable);
12311271
Set_Parameter;
12321272
end loop;
12331273
end if;

0 commit comments

Comments
 (0)