37
37
#include " llvm/Support/MemoryBuffer.h"
38
38
#include " llvm/Support/Path.h"
39
39
#include " llvm/Support/CommandLine.h"
40
+ #include < optional>
40
41
#include < system_error>
41
42
42
43
using namespace swift ;
@@ -1259,16 +1260,32 @@ void swift::serialization::diagnoseSerializedASTLoadFailureTransitive(
1259
1260
}
1260
1261
}
1261
1262
1263
+ static std::optional<StringRef> getFlagsFromInterfaceFile (StringRef &file,
1264
+ StringRef prefix) {
1265
+ StringRef line, buffer = file;
1266
+ while (!buffer.empty ()) {
1267
+ std::tie (line, buffer) = buffer.split (' \n ' );
1268
+ // If the line is no longer comments, return not found.
1269
+ if (!line.consume_front (" // " ))
1270
+ return std::nullopt;
1271
+
1272
+ if (line.consume_front (prefix) && line.consume_front (" :" )) {
1273
+ file = buffer;
1274
+ return line;
1275
+ }
1276
+ }
1277
+
1278
+ return std::nullopt;
1279
+ }
1280
+
1262
1281
bool swift::extractCompilerFlagsFromInterface (
1263
1282
StringRef interfacePath, StringRef buffer, llvm::StringSaver &ArgSaver,
1264
1283
SmallVectorImpl<const char *> &SubArgs,
1265
1284
std::optional<llvm::Triple> PreferredTarget) {
1266
- SmallVector<StringRef, 1 > FlagMatches;
1267
- auto FlagRe = llvm::Regex (" ^// swift-module-flags:(.*)$" , llvm::Regex::Newline);
1268
- if (!FlagRe.match (buffer, &FlagMatches))
1285
+ auto FlagMatch = getFlagsFromInterfaceFile (buffer, SWIFT_MODULE_FLAGS_KEY);
1286
+ if (!FlagMatch)
1269
1287
return true ;
1270
- assert (FlagMatches.size () == 2 );
1271
- llvm::cl::TokenizeGNUCommandLine (FlagMatches[1 ], ArgSaver, SubArgs);
1288
+ llvm::cl::TokenizeGNUCommandLine (*FlagMatch, ArgSaver, SubArgs);
1272
1289
1273
1290
// If the target triple parsed from the Swift interface file differs
1274
1291
// only in subarchitecture from the compatible target triple, then
@@ -1289,28 +1306,22 @@ bool swift::extractCompilerFlagsFromInterface(
1289
1306
SubArgs[I] = ArgSaver.save (target.str ()).data ();
1290
1307
}
1291
1308
1292
- SmallVector<StringRef, 1 > IgnFlagMatches;
1293
- // Cherry-pick supported options from the ignorable list.
1294
- auto IgnFlagRe = llvm::Regex (" ^// swift-module-flags-ignorable:(.*)$" ,
1295
- llvm::Regex::Newline);
1296
- auto hasIgnorableFlags = IgnFlagRe.match (buffer, &IgnFlagMatches);
1297
-
1298
- // Check for ignorable-private flags
1299
- SmallVector<StringRef, 1 > IgnPrivateFlagMatches;
1300
- auto IgnPrivateFlagRe = llvm::Regex (" ^// swift-module-flags-ignorable-private:(.*)$" ,
1301
- llvm::Regex::Newline);
1302
- auto hasIgnorablePrivateFlags = IgnPrivateFlagRe.match (buffer, &IgnPrivateFlagMatches);
1309
+ auto IgnFlagMatch =
1310
+ getFlagsFromInterfaceFile (buffer, SWIFT_MODULE_FLAGS_IGNORABLE_KEY);
1311
+ auto IgnPrivateFlagMatch = getFlagsFromInterfaceFile (
1312
+ buffer, SWIFT_MODULE_FLAGS_IGNORABLE_PRIVATE_KEY);
1303
1313
1304
1314
// It's OK the interface doesn't have the ignorable list (private or not), we just
1305
1315
// ignore them all.
1306
- if (!hasIgnorableFlags && !hasIgnorablePrivateFlags )
1316
+ if (!IgnFlagMatch && !IgnPrivateFlagMatch )
1307
1317
return false ;
1308
1318
1309
1319
SmallVector<const char *, 8 > IgnSubArgs;
1310
- if (hasIgnorableFlags)
1311
- llvm::cl::TokenizeGNUCommandLine (IgnFlagMatches[1 ], ArgSaver, IgnSubArgs);
1312
- if (hasIgnorablePrivateFlags)
1313
- llvm::cl::TokenizeGNUCommandLine (IgnPrivateFlagMatches[1 ], ArgSaver, IgnSubArgs);
1320
+ if (IgnFlagMatch)
1321
+ llvm::cl::TokenizeGNUCommandLine (*IgnFlagMatch, ArgSaver, IgnSubArgs);
1322
+ if (IgnPrivateFlagMatch)
1323
+ llvm::cl::TokenizeGNUCommandLine (*IgnPrivateFlagMatch, ArgSaver,
1324
+ IgnSubArgs);
1314
1325
1315
1326
std::unique_ptr<llvm::opt::OptTable> table = swift::createSwiftOptTable ();
1316
1327
unsigned missingArgIdx = 0 ;
0 commit comments