@@ -62,53 +62,56 @@ struct ModuleGroup
6262 */
6363 void sortCtors ()
6464 {
65- import core.bitop : bts, btr, bt;
66- debug (printModuleDependencies)
65+ import core.bitop : bts, btr, bt;
66+
67+ debug (printModuleDependencies)
6768 {
6869 import core.stdc.stdio : printf;
69- foreach (_m; _modules)
70+
71+ foreach (_m; _modules)
7072 {
71- printf(" %s%s%s:" , _m.name.ptr, (_m.flags & MIstandalone) ? " +" .ptr : " " .ptr, (_m.flags & (MIctor | MIdtor)) ? " *" .ptr : " " .ptr);
72- foreach (_i; _m.importedModules)
73+ printf(" %s%s%s:" , _m.name.ptr, (_m.flags & MIstandalone)
74+ ? " +" .ptr : " " .ptr, (_m.flags & (MIctor | MIdtor)) ? " *" .ptr : " " .ptr);
75+ foreach (_i; _m.importedModules)
7376 printf(" %s" , _i.name.ptr);
7477 printf(" \n " );
7578 }
7679 }
7780
78- immutable uint len = cast (uint )_modules.length;
79- if (! len)
81+ immutable uint len = cast (uint ) _modules.length;
82+ if (! len)
8083 return ; // nothing to do.
8184
8285 immutable (ModuleInfo )* minMod = _modules[0 ];
8386 immutable (ModuleInfo )* maxMod = _modules[0 ];
84- foreach (m; _modules)
87+ foreach (m; _modules)
8588 {
86- if (m < minMod)
89+ if (m < minMod)
8790 minMod = m;
88- if (m > maxMod)
91+ if (m > maxMod)
8992 maxMod = m;
9093 }
9194
9295 int findModule (in ModuleInfo * mi)
9396 {
9497 // short circuit linear search if possible.
95- if (mi < minMod || mi > maxMod)
98+ if (mi < minMod || mi > maxMod)
9699 return - 1 ;
97100 // binary search would be nice...
98101 foreach (i, m; _modules)
99- if (m is mi) return cast (int )i;
102+ if (m is mi)
103+ return cast (int ) i;
100104 return - 1 ;
101105 }
102106
103-
104107 // allocate some stack arrays that will be used throughout the process.
105108 immutable nwords = (len + 8 * size_t .sizeof - 1 ) / (8 * size_t .sizeof);
106109 immutable flagbytes = nwords * size_t .sizeof;
107- auto ctorstart = cast (size_t * )alloca(flagbytes);// ctor/dtor seen
108- auto ctordone = cast (size_t * )alloca(flagbytes);// ctor/dtor processed
109- auto relevant = cast (size_t * )alloca(flagbytes);// has ctors/dtors
110+ auto ctorstart = cast (size_t * ) alloca(flagbytes); // ctor/dtor seen
111+ auto ctordone = cast (size_t * ) alloca(flagbytes); // ctor/dtor processed
112+ auto relevant = cast (size_t * ) alloca(flagbytes); // has ctors/dtors
110113
111- void clearFlags (size_t * flags)
114+ void clearFlags (size_t * flags)
112115 {
113116 memset(flags, 0 , flagbytes);
114117 }
@@ -130,7 +133,7 @@ struct ModuleGroup
130133 void println (string [] msgs... )
131134 {
132135 print(msgs);
133- version (Windows )
136+ version (Windows )
134137 print(" \r\n " );
135138 else
136139 print(" \n " );
@@ -153,36 +156,36 @@ struct ModuleGroup
153156 distance[] = int .max;
154157 int curdist = 0 ;
155158 distance[start] = 0 ;
156- while (true )
159+ while (true )
157160 {
158161 bool done = true ;
159- foreach (i, x; distance)
162+ foreach (i, x; distance)
160163 {
161- if (x == curdist)
164+ if (x == curdist)
162165 {
163- if (i == target)
166+ if (i == target)
164167 {
165168 done = true ;
166169 break ;
167170 }
168- foreach (n; edges[i])
171+ foreach (n; edges[i])
169172 {
170- if (distance[n] == int .max)
173+ if (distance[n] == int .max)
171174 {
172175 distance[n] = curdist + 1 ;
173176 done = false ;
174177 }
175178 }
176179 }
177180 }
178- if (done)
181+ if (done)
179182 break ;
180183 ++ curdist;
181184 }
182185 // it should be impossible to not get to target, this is just a
183186 // sanity check. Not an assert, because druntime is compiled in
184187 // release mode.
185- if (distance[target] != curdist)
188+ if (distance[target] != curdist)
186189 {
187190 throw new Error(" internal error printing module cycle" );
188191 }
@@ -191,23 +194,23 @@ struct ModuleGroup
191194 // follow the edges in reverse to get back to the original. We
192195 // don't have a reverse mapping, so it takes a bit of looping.
193196 cyclePath.length += curdist;
194- auto subpath = cyclePath[$- curdist .. $];
195- while (true )
197+ auto subpath = cyclePath[$ - curdist .. $];
198+ while (true )
196199 {
197200 -- curdist;
198201 subpath[curdist] = target;
199- if (curdist == 0 )
202+ if (curdist == 0 )
200203 break ;
201- distloop:
204+ distloop:
202205 // search for next (previous) module in cycle.
203- foreach (int m, d; distance)
206+ foreach (int m, d; distance)
204207 {
205- if (d == curdist)
208+ if (d == curdist)
206209 {
207210 // determine if m can reach target
208- foreach (e; edges[m])
211+ foreach (e; edges[m])
209212 {
210- if (e == target)
213+ if (e == target)
211214 {
212215 // recurse
213216 target = m;
@@ -231,16 +234,16 @@ distloop:
231234 distance.length = len;
232235 edges.length = len;
233236 auto reachable = (new size_t [](nwords)).ptr;
234- foreach (i, m; _modules)
237+ foreach (i, m; _modules)
235238 {
236239 // use bit array to prevent duplicates
237240 // https://issues.dlang.org/show_bug.cgi?id=16208
238241 clearFlags(reachable);
239- foreach (e; m.importedModules)
242+ foreach (e; m.importedModules)
240243 {
241244 auto impidx = findModule(e);
242- if (impidx != - 1 && impidx != i)
243- if (! bts(reachable, impidx))
245+ if (impidx != - 1 && impidx != i)
246+ if (! bts(reachable, impidx))
244247 edges[i] ~= impidx;
245248 }
246249 }
@@ -258,7 +261,7 @@ distloop:
258261 // trivial modules to get at the non-trivial ones.
259262 //
260263 // If a cycle is detected, returns the index of the module that completes the cycle.
261- int findDeps (int idx, size_t * reachable)
264+ int findDeps (int idx, size_t * reachable)
262265 {
263266 static struct stackFrame
264267 {
@@ -267,7 +270,7 @@ distloop:
267270 }
268271
269272 // initialize "stack"
270- auto stack = cast (stackFrame * ) alloca(stackFrame.sizeof * len);
273+ auto stack = cast (stackFrame* ) alloca(stackFrame.sizeof * len);
271274 auto stacktop = stack + len;
272275 auto sp = stack;
273276 sp.curMod = idx;
@@ -277,39 +280,39 @@ distloop:
277280 clearFlags(reachable);
278281 bts(reachable, idx);
279282
280- for (;;)
283+ for (;;)
281284 {
282285 auto m = _modules[sp.curMod];
283- if (sp.curDep >= m.importedModules.length)
286+ if (sp.curDep >= m.importedModules.length)
284287 {
285288 // return
286- if (sp == stack)
287- // finished the algorithm
289+ if (sp == stack) // finished the algorithm
288290 break ;
289291 -- sp;
290292 }
291293 else
292294 {
293295 auto midx = findModule(m.importedModules[sp.curDep]);
294296 // if midx is -1, then this isn't part of this DSO.
295- if (midx != - 1 && ! bts(reachable, midx))
297+ if (midx != - 1 && ! bts(reachable, midx))
296298 {
297- if (bt(relevant, midx))
299+ if (bt(relevant, midx))
298300 {
299301 // need to process this node, don't recurse.
300- if (bt(ctorstart, midx))
302+ if (bt(ctorstart, midx))
301303 {
302304 // was already started, this is a cycle.
303305 return midx;
304306 }
305307 }
306- else if (! bt(ctordone, midx))
308+ else if (! bt(ctordone, midx))
307309 {
308310 // non-relevant, and hasn't been exhaustively processed, recurse.
309- if (++ sp >= stacktop)
311+ if (++ sp >= stacktop)
310312 {
311313 // stack overflow, this shouldn't happen.
312314 import core.internal.abort : abort;
315+
313316 abort(" stack overflow on dependency search" );
314317 }
315318 sp.curMod = midx;
@@ -332,7 +335,6 @@ distloop:
332335 // current element being inserted into ctors list.
333336 size_t ctoridx = 0 ;
334337
335-
336338 // This function will determine the order of construction/destruction and
337339 // check for cycles. If a cycle is found, the cycle path is transformed
338340 // into a string and thrown as an error.
@@ -346,16 +348,16 @@ distloop:
346348 immutable ModuleInfo * current = _modules[curidx];
347349
348350 // First, determine what modules are reachable.
349- auto reachable = cast (size_t * ) alloca(flagbytes);
351+ auto reachable = cast (size_t * ) alloca(flagbytes);
350352 auto cycleIdx = findDeps(curidx, reachable);
351- if (cycleIdx != - 1 )
353+ if (cycleIdx != - 1 )
352354 {
353355 auto cycleMod = _modules[cycleIdx];
354356 // found a cycle
355357 println(" Cyclic dependency between module " , cycleMod.name, " and " , current.name);
356358 genPath(cycleIdx, curidx);
357359
358- foreach (midx; cyclePath[0 .. $- 1 ])
360+ foreach (midx; cyclePath[0 .. $ - 1 ])
359361 {
360362 println(_modules[midx].name, bt (relevant, midx) ? " * ->" : " ->" );
361363 }
@@ -367,8 +369,7 @@ distloop:
367369 bts(ctorstart, curidx);
368370 foreach (int i; 0 .. len)
369371 {
370- if (i != curidx && bt(reachable, i) &&
371- bt(relevant, i) && ! bt(ctordone, i))
372+ if (i != curidx && bt(reachable, i) && bt(relevant, i) && ! bt(ctordone, i))
372373 {
373374 assert (! bt(ctorstart, i)); // sanity check, this should have been flagged a cycle earlier
374375 processMod(i);
@@ -380,7 +381,7 @@ distloop:
380381 btr(ctorstart, curidx);
381382 foreach (int i; 0 .. len)
382383 {
383- if (bt(reachable, i))
384+ if (bt(reachable, i))
384385 {
385386 // Since relevant dependencies are already marked as done
386387 // from recursion above, no reason to check for relevance,
@@ -393,8 +394,7 @@ distloop:
393394 ctors[ctoridx++ ] = current;
394395 }
395396
396- immutable (ModuleInfo )* []
397- doSort(int function (immutable ModuleInfo * ) getf)
397+ immutable (ModuleInfo )* [] doSort (int function (immutable ModuleInfo * ) getf)
398398 {
399399 clearFlags(relevant);
400400 clearFlags(ctorstart);
@@ -406,11 +406,12 @@ distloop:
406406 foreach (int idx, m; _modules)
407407 {
408408 // TODO: Should null ModuleInfo be allowed?
409- if (m is null ) continue ;
409+ if (m is null )
410+ continue ;
410411 auto flag = getf(m);
411- if (flag & MIctor)
412+ if (flag & MIctor)
412413 {
413- if (flag & MIstandalone)
414+ if (flag & MIstandalone)
414415 {
415416 // can run at any time. Just run it first.
416417 ctors[ctoridx++ ] = m;
@@ -425,9 +426,9 @@ distloop:
425426 // now run the algorithm in the relevant ones
426427 foreach (int idx, m; _modules)
427428 {
428- if (bt(relevant, idx))
429+ if (bt(relevant, idx))
429430 {
430- if (! bt(ctordone, idx))
431+ if (! bt(ctordone, idx))
431432 processMod(idx);
432433 }
433434 }
@@ -436,8 +437,10 @@ distloop:
436437 }
437438
438439 // finally, do the sorting for both shared and tls ctors.
439- _ctors = doSort((immutable ModuleInfo * m) => (m.flags & MIstandalone) | ((m.dtor || m.ctor)? MIctor : 0 ));
440- _tlsctors = doSort((immutable ModuleInfo * m) => (m.flags & MIstandalone) | ((m.tlsdtor || m.tlsctor)? MIctor : 0 ));
440+ _ctors = doSort((immutable ModuleInfo * m) => (m.flags & MIstandalone) | ((m.dtor
441+ || m.ctor) ? MIctor : 0 ));
442+ _tlsctors = doSort((immutable ModuleInfo * m) => (m.flags & MIstandalone) | ((m.tlsdtor
443+ || m.tlsctor) ? MIctor : 0 ));
441444 }
442445
443446 void runCtors ()
0 commit comments