cfgexpand: Expand comment on when non-var clobbers can show up
[official-gcc.git] / gcc / ada / libgnat / g-diopit.adb
blob57cca861f9eeb4edaf0dad7cfe376d13b9bbfeaf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S . I T E R A T I O N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2024, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 with Ada.Characters.Handling;
33 with Ada.Strings.Fixed;
34 with Ada.Strings.Maps;
36 with GNAT.OS_Lib;
37 with GNAT.Regexp;
39 package body GNAT.Directory_Operations.Iteration is
41 use Ada;
43 ----------
44 -- Find --
45 ----------
47 procedure Find
48 (Root_Directory : Dir_Name_Str;
49 File_Pattern : String)
51 File_Regexp : constant Regexp.Regexp := Regexp.Compile (File_Pattern);
52 Index : Natural := 0;
53 Quit : Boolean := False;
55 procedure Read_Directory (Directory : Dir_Name_Str);
56 -- Open Directory and read all entries. This routine is called
57 -- recursively for each sub-directories.
59 function Make_Pathname (Dir, File : String) return String;
60 -- Returns the pathname for File by adding Dir as prefix
62 -------------------
63 -- Make_Pathname --
64 -------------------
66 function Make_Pathname (Dir, File : String) return String is
67 begin
68 if Dir (Dir'Last) = '/' or else Dir (Dir'Last) = '\' then
69 return Dir & File;
70 else
71 return Dir & Dir_Separator & File;
72 end if;
73 end Make_Pathname;
75 --------------------
76 -- Read_Directory --
77 --------------------
79 procedure Read_Directory (Directory : Dir_Name_Str) is
80 Buffer : String (1 .. 2_048);
81 Last : Natural;
83 Dir : Dir_Type;
84 pragma Warnings (Off, Dir);
86 begin
87 Open (Dir, Directory);
89 loop
90 Read (Dir, Buffer, Last);
91 exit when Last = 0;
93 declare
94 Dir_Entry : constant String := Buffer (1 .. Last);
95 Pathname : constant String :=
96 Make_Pathname (Directory, Dir_Entry);
98 begin
99 if Regexp.Match (Dir_Entry, File_Regexp) then
100 Index := Index + 1;
102 begin
103 Action (Pathname, Index, Quit);
104 exception
105 when others =>
106 Close (Dir);
107 raise;
108 end;
110 exit when Quit;
111 end if;
113 -- Recursively call for sub-directories, except for . and ..
115 if not (Dir_Entry = "." or else Dir_Entry = "..")
116 and then OS_Lib.Is_Directory (Pathname)
117 and then not OS_Lib.Is_Symbolic_Link (Pathname)
118 then
119 Read_Directory (Pathname);
120 exit when Quit;
121 end if;
122 end;
123 end loop;
125 Close (Dir);
126 end Read_Directory;
128 begin
129 Read_Directory (Root_Directory);
130 end Find;
132 -----------------------
133 -- Wildcard_Iterator --
134 -----------------------
136 procedure Wildcard_Iterator (Path : Path_Name) is
138 Index : Natural := 0;
140 procedure Read
141 (Directory : String;
142 File_Pattern : String;
143 Suffix_Pattern : String);
144 -- Read entries in Directory and call user's callback if the entry match
145 -- File_Pattern and Suffix_Pattern is empty; otherwise go down one more
146 -- directory level by calling Next_Level routine below.
148 procedure Next_Level
149 (Current_Path : String;
150 Suffix_Path : String);
151 -- Extract next File_Pattern from Suffix_Path and call Read routine
152 -- above.
154 ----------------
155 -- Next_Level --
156 ----------------
158 procedure Next_Level
159 (Current_Path : String;
160 Suffix_Path : String)
162 DS : Natural;
163 SP : String renames Suffix_Path;
165 begin
166 if SP'Length > 2
167 and then SP (SP'First) = '.'
168 and then Strings.Maps.Is_In (SP (SP'First + 1), Dir_Seps)
169 then
170 -- Starting with "./"
172 DS := Strings.Fixed.Index
173 (SP (SP'First + 2 .. SP'Last),
174 Dir_Seps);
176 if DS = 0 then
178 -- We have "./"
180 Read (Current_Path & ".", "*", "");
182 else
183 -- We have "./dir"
185 Read (Current_Path & ".",
186 SP (SP'First + 2 .. DS - 1),
187 SP (DS .. SP'Last));
188 end if;
190 elsif SP'Length > 3
191 and then SP (SP'First .. SP'First + 1) = ".."
192 and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
193 then
194 -- Starting with "../"
196 DS := Strings.Fixed.Index
197 (SP (SP'First + 3 .. SP'Last), Dir_Seps);
199 if DS = 0 then
201 -- We have "../"
203 Read (Current_Path & "..", "*", "");
205 else
206 -- We have "../dir"
208 Read (Current_Path & "..",
209 SP (SP'First + 3 .. DS - 1),
210 SP (DS .. SP'Last));
211 end if;
213 elsif Current_Path = ""
214 and then SP'Length > 1
215 and then Characters.Handling.Is_Letter (SP (SP'First))
216 and then SP (SP'First + 1) = ':'
217 then
218 -- Starting with "<drive>:"
220 if SP'Length > 2
221 and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
222 then
223 -- Starting with "<drive>:\"
225 DS := Strings.Fixed.Index
226 (SP (SP'First + 3 .. SP'Last), Dir_Seps);
228 if DS = 0 then
230 -- We have "<drive>:\dir"
232 Read (SP (SP'First .. SP'First + 2),
233 SP (SP'First + 3 .. SP'Last),
234 "");
236 else
237 -- We have "<drive>:\dir\kkk"
239 Read (SP (SP'First .. SP'First + 2),
240 SP (SP'First + 3 .. DS - 1),
241 SP (DS .. SP'Last));
242 end if;
244 else
245 -- Starting with "<drive>:" and the drive letter not followed
246 -- by a directory separator. The proper semantic on Windows is
247 -- to read the content of the current selected directory on
248 -- this drive. For example, if drive C current selected
249 -- directory is c:\temp the suffix pattern "c:m*" is
250 -- equivalent to c:\temp\m*.
252 DS := Strings.Fixed.Index
253 (SP (SP'First + 2 .. SP'Last), Dir_Seps);
255 if DS = 0 then
257 -- We have "<drive>:dir"
259 Read (SP, "", "");
261 else
262 -- We have "<drive>:dir/kkk"
264 Read (SP (SP'First .. DS - 1), "", SP (DS .. SP'Last));
265 end if;
266 end if;
268 elsif Strings.Maps.Is_In (SP (SP'First), Dir_Seps) then
270 -- Starting with a /
272 DS := Strings.Fixed.Index
273 (SP (SP'First + 1 .. SP'Last), Dir_Seps);
275 if DS = 0 then
277 -- We have "/dir"
279 Read (Current_Path, SP (SP'First + 1 .. SP'Last), "");
280 else
281 -- We have "/dir/kkk"
283 Read (Current_Path,
284 SP (SP'First + 1 .. DS - 1),
285 SP (DS .. SP'Last));
286 end if;
288 else
289 -- Starting with a name
291 DS := Strings.Fixed.Index (SP, Dir_Seps);
293 if DS = 0 then
295 -- We have "dir"
297 Read (Current_Path & '.', SP, "");
298 else
299 -- We have "dir/kkk"
301 Read (Current_Path & '.',
302 SP (SP'First .. DS - 1),
303 SP (DS .. SP'Last));
304 end if;
306 end if;
307 end Next_Level;
309 ----------
310 -- Read --
311 ----------
313 Quit : Boolean := False;
314 -- Global state to be able to exit all recursive calls
316 procedure Read
317 (Directory : String;
318 File_Pattern : String;
319 Suffix_Pattern : String)
321 File_Regexp : constant Regexp.Regexp :=
322 Regexp.Compile (File_Pattern, Glob => True);
324 Dir : Dir_Type;
325 pragma Warnings (Off, Dir);
327 Buffer : String (1 .. 2_048);
328 Last : Natural;
330 begin
331 if OS_Lib.Is_Directory (Directory & Dir_Separator) then
332 Open (Dir, Directory & Dir_Separator);
334 Dir_Iterator : loop
335 Read (Dir, Buffer, Last);
336 exit Dir_Iterator when Last = 0;
338 declare
339 Dir_Entry : constant String := Buffer (1 .. Last);
340 Pathname : constant String :=
341 Directory & Dir_Separator & Dir_Entry;
342 begin
343 -- Handle "." and ".." only if explicit use in the
344 -- File_Pattern.
346 if not
347 ((Dir_Entry = "." and then File_Pattern /= ".")
348 or else
349 (Dir_Entry = ".." and then File_Pattern /= ".."))
350 then
351 if Regexp.Match (Dir_Entry, File_Regexp) then
352 if Suffix_Pattern = "" then
354 -- No more matching needed, call user's callback
356 Index := Index + 1;
358 begin
359 Action (Pathname, Index, Quit);
360 exception
361 when others =>
362 Close (Dir);
363 raise;
364 end;
366 else
367 -- Down one level
369 Next_Level
370 (Directory & Dir_Separator & Dir_Entry,
371 Suffix_Pattern);
372 end if;
373 end if;
374 end if;
375 end;
377 -- Exit if Quit set by call to Action, either at this level
378 -- or at some lower recursive call to Next_Level.
380 exit Dir_Iterator when Quit;
381 end loop Dir_Iterator;
383 Close (Dir);
384 end if;
385 end Read;
387 -- Start of processing for Wildcard_Iterator
389 begin
390 if Path = "" then
391 return;
392 end if;
394 Next_Level ("", Path);
395 end Wildcard_Iterator;
397 end GNAT.Directory_Operations.Iteration;