Skip to content

Commit 21bbda0

Browse files
committed
Remove usage of GNAT.Command_Line to parse arguments
It's last usage was for explicit source files provided through the CLI. Also remove the now useless 'Store' parameter from 'Store_Main_Unit' and 'Store_Sources_To_Process' procedures.
1 parent 1d73380 commit 21bbda0

File tree

4 files changed

+59
-81
lines changed

4 files changed

+59
-81
lines changed

lkql_checker/src/gnatcheck-options.adb

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,12 @@ package body Gnatcheck.Options is
101101
(First_Pass : Boolean := False; Args : Argument_List_Access := null)
102102
is
103103
Unknown_Opt_Parse_Args : XString_Vector;
104-
Args_After_Opt_Parse : Argument_List_Access;
105-
Parser : Opt_Parser;
104+
Exp_It : Expansion_Iterator;
105+
Explicit_Sources : String_Vector;
106106

107-
function To_Arg_List (Args : XString_Vector) return Argument_List_Access;
108107
function To_XString_Array
109108
(Args : Argument_List_Access) return XString_Array;
110109

111-
function To_Arg_List (Args : XString_Vector) return Argument_List_Access
112-
is
113-
Ret : constant Argument_List_Access :=
114-
new String_List (1 .. Args.Last_Index);
115-
begin
116-
for I in Ret'Range loop
117-
Ret (I) := new String'(Args (I).To_String);
118-
end loop;
119-
return Ret;
120-
end To_Arg_List;
121-
122110
function To_XString_Array
123111
(Args : Argument_List_Access) return XString_Array
124112
is
@@ -138,8 +126,6 @@ package body Gnatcheck.Options is
138126
Compose (Compose (Prefix, "share"), "lkql");
139127

140128
Args_From_Project : constant Boolean := Args /= null;
141-
Initial_Char : Character;
142-
Success : Boolean;
143129

144130
-- Start of processing for Scan_Arguments
145131

@@ -183,14 +169,12 @@ package body Gnatcheck.Options is
183169
end;
184170
end if;
185171

186-
if Arg.Parser.Parse
187-
((if Args /= null then To_XString_Array (Args) else No_Arguments),
188-
Unknown_Arguments => Unknown_Opt_Parse_Args)
172+
if not Arg.Parser.Parse
173+
((if Args /= null
174+
then To_XString_Array (Args)
175+
else No_Arguments),
176+
Unknown_Arguments => Unknown_Opt_Parse_Args)
189177
then
190-
Args_After_Opt_Parse := To_Arg_List (Unknown_Opt_Parse_Args);
191-
Initialize_Option_Scan
192-
(Parser, Args_After_Opt_Parse, Section_Delimiters => "cargs rules");
193-
else
194178
raise Parameter_Error;
195179
end if;
196180

@@ -213,54 +197,56 @@ package body Gnatcheck.Options is
213197
Allow (Arg.List_Rules.This);
214198
end if;
215199

216-
loop
217-
Initial_Char := Getopt (" ", Parser => Parser);
218-
219-
case Initial_Char is
220-
when ASCII.NUL =>
221-
Success := False;
222-
223-
loop
224-
declare
225-
Arg : constant String :=
226-
Get_Argument (Do_Expansion => True, Parser => Parser);
227-
228-
begin
229-
exit when Arg = "";
230-
Success := True;
231-
232-
if Options.Arg.Transitive_Closure.Get then
233-
Store_Main_Unit (Arg, Args_From_Project or First_Pass);
234-
else
235-
Store_Sources_To_Process
236-
(Arg, Args_From_Project or First_Pass);
237-
238-
if not Args_From_Project then
239-
Argument_File_Specified := True;
240-
end if;
241-
end if;
242-
end;
243-
end loop;
244-
245-
exit when not Success;
246-
247-
when others =>
248-
Error
249-
("unrecognized switch: " & Full_Switch (Parser => Parser));
250-
raise Parameter_Error;
251-
end case;
200+
-- Now that we processed all switches, remaining arguments should be
201+
-- source files to analyze.
202+
-- We first have to check that there is no additional argument and
203+
-- expand possible glob patterns.
204+
for Unknown_Arg of Unknown_Opt_Parse_Args loop
205+
-- We consider that arguments starting by "-" are remaining unknown
206+
-- arguments.
207+
if Unknown_Arg.Starts_With ("-") then
208+
Error ("unrecognized switch: " & To_String (Unknown_Arg));
209+
raise Parameter_Error;
210+
211+
-- We now know that the current argument should be handled like an
212+
-- Ada source file name OR a glob pattern.
213+
-- We only handle explicit sources during the first pass to avoid
214+
-- duplication.
215+
elsif Args_From_Project or First_Pass then
216+
declare
217+
Is_Glob : constant Boolean :=
218+
Unknown_Arg.Find ('*') /= 0
219+
or else Unknown_Arg.Find ('?') /= 0
220+
or else Unknown_Arg.Find ('[') /= 0;
221+
begin
222+
if Is_Glob then
223+
Start_Expansion (Exp_It, To_String (Unknown_Arg));
224+
loop
225+
declare
226+
Expanded_Arg : constant String := Expansion (Exp_It);
227+
begin
228+
exit when Expanded_Arg = "";
229+
Explicit_Sources.Append (Expanded_Arg);
230+
end;
231+
end loop;
232+
else
233+
Explicit_Sources.Append (To_String (Unknown_Arg));
234+
end if;
235+
end;
236+
end if;
252237
end loop;
253238

254-
exception
255-
when Invalid_Switch =>
256-
Error ("invalid switch: " & Full_Switch (Parser => Parser));
257-
raise Parameter_Error;
258-
259-
when Invalid_Parameter =>
260-
Error
261-
("missing Parameter (Parser => Parser) for: -"
262-
& Full_Switch (Parser => Parser));
263-
raise Parameter_Error;
239+
-- We can now store sources to process
240+
for Arg of Explicit_Sources loop
241+
if Options.Arg.Transitive_Closure.Get then
242+
Store_Main_Unit (Arg);
243+
else
244+
Store_Sources_To_Process (Arg);
245+
if not Args_From_Project then
246+
Argument_File_Specified := True;
247+
end if;
248+
end if;
249+
end loop;
264250
end Scan_Arguments;
265251

266252
---------------------------------

lkql_checker/src/gnatcheck-projects.adb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,9 @@ package body Gnatcheck.Projects is
892892
-- Store_Main_Unit --
893893
---------------------
894894

895-
procedure Store_Main_Unit (Unit_Name : String; Store : Boolean := True) is
895+
procedure Store_Main_Unit (Unit_Name : String) is
896896
begin
897-
if Store then
898-
Gnatcheck.Projects.Main_Unit.Include (GPR2.Filename_Type (Unit_Name));
899-
end if;
897+
Gnatcheck.Projects.Main_Unit.Include (GPR2.Filename_Type (Unit_Name));
900898
end Store_Main_Unit;
901899

902900
--------------------------

lkql_checker/src/gnatcheck-projects.ads

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,9 @@ package Gnatcheck.Projects is
112112
-- If the tool is called with "... Pproj -U main_unit1 main_unit2 ...",
113113
-- main units are stored here.
114114

115-
procedure Store_Main_Unit (Unit_Name : String; Store : Boolean := True);
116-
-- Processes the result returned by GNAT.Comand_Line.Get_Argument provided
117-
-- that it is supposed to be the main unit name for '-U' project file
115+
procedure Store_Main_Unit (Unit_Name : String);
116+
-- Processes the provided name as the main unit name for '-U' project file
118117
-- option.
119-
--
120-
-- If Store is ON, stores the name of the unit. If Store is OFF,
121-
-- does not store anything.
122118

123119
-----------------------------------------------------------
124120
-- -Xvariable=value : set values of external variables --

lkql_checker/src/gnatcheck-source_table.ads

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ package Gnatcheck.Source_Table is
9595
-- Fname is stored in an internal database as the name of the file to be
9696
-- processed by the tool. No check is made if Fname denotes an existing
9797
-- file.
98-
--
99-
-- If Store is False then the procedure does not store anything.
10098

10199
procedure Read_Args_From_Temp_Storage
102100
(Duplication_Report : Boolean;

0 commit comments

Comments
 (0)