Skip to content

[InterfaceFile] Improve flag extraction from interface file #72093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/Frontend/ModuleInterfaceSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define SWIFT_COMPILER_VERSION_KEY "swift-compiler-version"
#define SWIFT_MODULE_FLAGS_KEY "swift-module-flags"
#define SWIFT_MODULE_FLAGS_IGNORABLE_KEY "swift-module-flags-ignorable"
#define SWIFT_MODULE_FLAGS_IGNORABLE_PRIVATE_KEY \
"swift-module-flags-ignorable-private"

namespace swift {

Expand Down
53 changes: 32 additions & 21 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/CommandLine.h"
#include <optional>
#include <system_error>

using namespace swift;
Expand Down Expand Up @@ -1389,17 +1390,33 @@ static bool tripleNeedsSubarchitectureAdjustment(const llvm::Triple &lhs, const
lhs.getEnvironment() == rhs.getEnvironment());
}

static std::optional<StringRef> getFlagsFromInterfaceFile(StringRef &file,
StringRef prefix) {
StringRef line, buffer = file;
while (!buffer.empty()) {
std::tie(line, buffer) = buffer.split('\n');
// If the line is no longer comments, return not found.
if (!line.consume_front("// "))
return std::nullopt;

if (line.consume_front(prefix) && line.consume_front(":")) {
file = buffer;
return line;
}
}

return std::nullopt;
}

bool swift::extractCompilerFlagsFromInterface(
StringRef interfacePath, StringRef buffer, llvm::StringSaver &ArgSaver,
SmallVectorImpl<const char *> &SubArgs,
std::optional<llvm::Triple> PreferredTarget,
std::optional<llvm::Triple> PreferredTargetVariant) {
SmallVector<StringRef, 1> FlagMatches;
auto FlagRe = llvm::Regex("^// swift-module-flags:(.*)$", llvm::Regex::Newline);
if (!FlagRe.match(buffer, &FlagMatches))
auto FlagMatch = getFlagsFromInterfaceFile(buffer, SWIFT_MODULE_FLAGS_KEY);
if (!FlagMatch)
return true;
assert(FlagMatches.size() == 2);
llvm::cl::TokenizeGNUCommandLine(FlagMatches[1], ArgSaver, SubArgs);
llvm::cl::TokenizeGNUCommandLine(*FlagMatch, ArgSaver, SubArgs);

// If the target triple parsed from the Swift interface file differs
// only in subarchitecture from the compatible target triple, then
Expand All @@ -1421,28 +1438,22 @@ bool swift::extractCompilerFlagsFromInterface(
}
}

SmallVector<StringRef, 1> IgnFlagMatches;
// Cherry-pick supported options from the ignorable list.
auto IgnFlagRe = llvm::Regex("^// swift-module-flags-ignorable:(.*)$",
llvm::Regex::Newline);
auto hasIgnorableFlags = IgnFlagRe.match(buffer, &IgnFlagMatches);

// Check for ignorable-private flags
SmallVector<StringRef, 1> IgnPrivateFlagMatches;
auto IgnPrivateFlagRe = llvm::Regex("^// swift-module-flags-ignorable-private:(.*)$",
llvm::Regex::Newline);
auto hasIgnorablePrivateFlags = IgnPrivateFlagRe.match(buffer, &IgnPrivateFlagMatches);
auto IgnFlagMatch =
getFlagsFromInterfaceFile(buffer, SWIFT_MODULE_FLAGS_IGNORABLE_KEY);
auto IgnPrivateFlagMatch = getFlagsFromInterfaceFile(
buffer, SWIFT_MODULE_FLAGS_IGNORABLE_PRIVATE_KEY);

// It's OK the interface doesn't have the ignorable list (private or not), we just
// ignore them all.
if (!hasIgnorableFlags && !hasIgnorablePrivateFlags)
if (!IgnFlagMatch && !IgnPrivateFlagMatch)
return false;

SmallVector<const char *, 8> IgnSubArgs;
if (hasIgnorableFlags)
llvm::cl::TokenizeGNUCommandLine(IgnFlagMatches[1], ArgSaver, IgnSubArgs);
if (hasIgnorablePrivateFlags)
llvm::cl::TokenizeGNUCommandLine(IgnPrivateFlagMatches[1], ArgSaver, IgnSubArgs);
if (IgnFlagMatch)
llvm::cl::TokenizeGNUCommandLine(*IgnFlagMatch, ArgSaver, IgnSubArgs);
if (IgnPrivateFlagMatch)
llvm::cl::TokenizeGNUCommandLine(*IgnPrivateFlagMatch, ArgSaver,
IgnSubArgs);

std::unique_ptr<llvm::opt::OptTable> table = swift::createSwiftOptTable();
unsigned missingArgIdx = 0;
Expand Down
1 change: 0 additions & 1 deletion test/ModuleInterface/SmokeTest.swiftinterface
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// The "flags" line in this test deliberately has no flags and no space after
// the colon, just to make sure that works (even though it'll never be printed
// that way).

// swift-module-flags:
// swift-interface-format-version: 1.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// RUN: %target-swift-typecheck-module-from-interface(%t/bug.swiftinterface) -I %t

//--- bug.swiftinterface

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)
// swift-module-flags: -enable-objc-interop -enable-library-evolution -module-name bug
Expand Down
1 change: 0 additions & 1 deletion test/SPI/spi_only_import_flag_check.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
@_spiOnly import Empty // expected-error {{'@_spiOnly' requires setting the frontend flag '-experimental-spi-only-imports'}}

//--- Client.private.swiftinterface

// swift-interface-format-version: 1.0
// swift-compiler-version: Swift version 5.8-dev effective-4.1.50
// swift-module-flags: -swift-version 4 -module-name Client
Expand Down