@@ -121,18 +121,19 @@ std::string getDefaultPreamble(const Opts& opts, std::vector<Directive>& directi
121
121
const File& file, bool & manualExport) {
122
122
const GetIncludeCtx ctx{opts.inDir , opts.includePaths };
123
123
std::string preamble;
124
+ std::string imports;
124
125
IncludeHandleResult res;
125
126
StdImportLvl lvl{StdImportLvl::Unused};
126
127
127
128
// Only convert include to import for source with a main(), paired or unpaired
128
129
if (file.type == FileType::SrcWithMain) {
129
- MinimizeCondCtx mcCtx ;
130
+ MinimizeCondCtx NoImportCtx ;
130
131
for (Directive& directive : directives) switch (directive.type ) {
131
132
case DirectiveType::IfCond:
132
133
case DirectiveType::ElCond:
133
134
case DirectiveType::Else:
134
135
case DirectiveType::EndIf:
135
- mcCtx .emplace_back (std::move (directive));
136
+ NoImportCtx .emplace_back (std::move (directive));
136
137
break ;
137
138
case DirectiveType::Include:
138
139
res = handleInclude (std::get<IncludeInfo>(directive.extraInfo ), ctx, file, opts,
@@ -141,55 +142,51 @@ std::string getDefaultPreamble(const Opts& opts, std::vector<Directive>& directi
141
142
// We're in default mode, no need to handle switch case 3 or 4
142
143
switch (res.index ()) {
143
144
case 2 : // ConvertToImport
144
- mcCtx. emplace_back ( std::get<ConvertToImport>(std::move ( res)) );
145
+ imports += std::get<ConvertToImport>(res);
145
146
continue ;
146
147
case 0 : // Skip
147
148
continue ;
148
149
case 1 :; // KeepAsInclude
149
150
}
150
151
[[fallthrough]];
151
152
default :
152
- mcCtx .emplace_back (std::move (directive.str ));
153
+ NoImportCtx .emplace_back (std::move (directive.str ));
153
154
}
154
- preamble = minimizeCondToStr (mcCtx );
155
+ preamble = minimizeCondToStr (NoImportCtx );
155
156
}
156
157
157
158
// Convert to module interface/implementation
158
159
else {
159
- MinimizeCondCtx includeCtx;
160
- MinimizeCondCtx importCtx;
160
+ MinimizeCondCtx GMFCtx;
161
161
for (Directive& directive : directives) switch (directive.type ) {
162
162
case DirectiveType::IfCond:
163
163
case DirectiveType::Else:
164
164
case DirectiveType::ElCond:
165
165
case DirectiveType::EndIf:
166
- includeCtx.emplace_back (directive);
167
- importCtx.emplace_back (std::move (directive));
166
+ GMFCtx.emplace_back ((std::move (directive)));
168
167
break ;
169
168
case DirectiveType::Include:
170
169
res = handleInclude (std::get<IncludeInfo>(directive.extraInfo ), ctx, file, opts,
171
170
lvl);
172
171
173
172
// We're in default mode, no need to handle switch case 3 or 4
174
173
switch (res.index ()) {
175
- case 0 :
176
- break ;
177
- case 2 :
178
- importCtx.emplace_back (std::get<ConvertToImport>(std::move (res)));
174
+ case 0 : // Skip
179
175
break ;
180
- case 1 :
181
- includeCtx. emplace_back (std::move (directive. str ));
176
+ case 2 : // ConvertToImport
177
+ imports += std::get<ConvertToImport> (std::move (res ));
182
178
break ;
179
+ case 1 : // KeepAsInclude
180
+ GMFCtx.emplace_back (std::move (directive.str ));
183
181
}
184
182
break ;
185
183
default :
186
- includeCtx.emplace_back (directive.str );
187
- importCtx.emplace_back (std::move (directive.str ));
184
+ GMFCtx.emplace_back (directive.str );
188
185
}
189
186
fmt::format_to (std::back_inserter (preamble),
190
187
" module;\n "
191
188
" {}" ,
192
- minimizeCondToStr (includeCtx ));
189
+ minimizeCondToStr (GMFCtx ));
193
190
194
191
// Convert header and unpaired source into module interface unit. Without
195
192
// the "export " the file is a module implementation unit
@@ -198,10 +195,10 @@ std::string getDefaultPreamble(const Opts& opts, std::vector<Directive>& directi
198
195
preamble += " export " ;
199
196
}
200
197
fmt::format_to (std::back_inserter (preamble),
201
- " module {};\n "
202
- " {}" ,
203
- path2ModuleName (file.relPath ), minimizeCondToStr (importCtx));
198
+ " module {};\n " ,
199
+ path2ModuleName (file.relPath ));
204
200
}
201
+ preamble += imports;
205
202
if (lvl == StdImportLvl::StdCompat) preamble += " import std.compat;\n " ;
206
203
else if (lvl == StdImportLvl::Std) preamble += " import std;\n " ;
207
204
return preamble;
@@ -214,14 +211,15 @@ std::string getTransitionalPreamble(const Opts& opts,
214
211
MinimizeCondCtx includeCtx;
215
212
StdImportLvl lvl{StdImportLvl::Unused};
216
213
if (file.type == FileType::SrcWithMain) {
217
- MinimizeCondCtx moduleCtx;
214
+ MinimizeCondCtx NoImportCtx;
215
+ std::string imports;
218
216
for (Directive& directive : directives) switch (directive.type ) {
219
217
case DirectiveType::IfCond:
220
218
case DirectiveType::ElCond:
221
219
case DirectiveType::Else:
222
220
case DirectiveType::EndIf:
223
221
includeCtx.emplace_back (directive);
224
- moduleCtx .emplace_back (std::move (directive));
222
+ NoImportCtx .emplace_back (std::move (directive));
225
223
break ;
226
224
case DirectiveType::Include:
227
225
res = handleInclude (std::get<IncludeInfo>(directive.extraInfo ), ctx, file, opts,
@@ -233,7 +231,7 @@ std::string getTransitionalPreamble(const Opts& opts,
233
231
case 2 : // ConvertToImport
234
232
includeCtx.emplace_back (
235
233
replaceIncludeExt (std::move (directive), opts.moduleInterfaceExt ));
236
- moduleCtx. emplace_back ( std::get<ConvertToImport>(std::move ( res)) );
234
+ imports += std::get<ConvertToImport>(res);
237
235
continue ;
238
236
case 3 : // KeepForHdr
239
237
includeCtx.emplace_back (std::move (directive.str ));
@@ -243,18 +241,18 @@ std::string getTransitionalPreamble(const Opts& opts,
243
241
[[fallthrough]];
244
242
default :
245
243
includeCtx.emplace_back (directive.str );
246
- moduleCtx .emplace_back (std::move (directive.str ));
244
+ NoImportCtx .emplace_back (std::move (directive.str ));
247
245
}
248
246
fmt::format_to (std::back_inserter (preamble),
249
247
" #ifdef {}\n "
250
- " {}" ,
251
- opts.transitionalOpts ->mi_control , minimizeCondToStr (moduleCtx) );
248
+ " {}{} " ,
249
+ opts.transitionalOpts ->mi_control , minimizeCondToStr (NoImportCtx), imports );
252
250
}
253
251
254
252
// Convert to module interface/implementation
255
253
else {
256
254
MinimizeCondCtx GMFCtx;
257
- MinimizeCondCtx importCtx ;
255
+ std::string imports ;
258
256
for (Directive& directive : directives) switch (directive.type ) {
259
257
case DirectiveType::IfCond:
260
258
if (std::holds_alternative<IncludeGuard>(directive.extraInfo )) {
@@ -267,7 +265,6 @@ std::string getTransitionalPreamble(const Opts& opts,
267
265
case DirectiveType::EndIf:
268
266
includeCtx.emplace_back (directive);
269
267
GMFCtx.emplace_back (directive);
270
- importCtx.emplace_back (std::move (directive));
271
268
break ;
272
269
case DirectiveType::Include:
273
270
res = handleInclude (std::get<IncludeInfo>(directive.extraInfo ), ctx, file, opts, lvl);
@@ -283,7 +280,7 @@ std::string getTransitionalPreamble(const Opts& opts,
283
280
case 2 : // ConvertToImport
284
281
includeCtx.emplace_back (
285
282
replaceIncludeExt (std::move (directive), opts.moduleInterfaceExt ));
286
- importCtx. emplace_back ( std::get<ConvertToImport>(std::move ( res)) );
283
+ imports += std::get<ConvertToImport>(res);
287
284
break ;
288
285
case 4 : // ReplaceExtForPair
289
286
includeCtx.emplace_back (
@@ -301,8 +298,7 @@ std::string getTransitionalPreamble(const Opts& opts,
301
298
[[fallthrough]];
302
299
default :
303
300
includeCtx.emplace_back (directive.str );
304
- GMFCtx.emplace_back (directive.str );
305
- importCtx.emplace_back (std::move (directive.str ));
301
+ GMFCtx.emplace_back (std::move (directive.str ));
306
302
}
307
303
308
304
// generic_string() to convert '\' to '/'
@@ -325,16 +321,16 @@ std::string getTransitionalPreamble(const Opts& opts,
325
321
fmt::format_to (std::back_inserter (preamble),
326
322
" module {};\n "
327
323
" {}" ,
328
- path2ModuleName (file.relPath ), minimizeCondToStr (importCtx) );
324
+ path2ModuleName (file.relPath ), imports );
329
325
}
330
326
331
327
// From here import section should be added, but not include section
332
328
if (lvl == StdImportLvl::StdCompat) preamble += " import std.compat;\n " ;
333
329
else if (lvl == StdImportLvl::Std) preamble += " import std;\n " ;
334
330
fmt::format_to (std::back_inserter (preamble),
335
331
" #else\n {}"
336
- " #endif\n "
337
- , minimizeCondToStr (includeCtx));
332
+ " #endif\n " ,
333
+ minimizeCondToStr (includeCtx));
338
334
return preamble;
339
335
}
340
336
0 commit comments