Skip to content

Commit 17f5b8b

Browse files
authored
[flang][driver] add ability to look up feature flags without setting them (#144559)
This just adds some convenience methods to feature control and rewrites old code in terms of those methods. Also cleans up some names that I just realize were overloads of another method.
1 parent 3f3526f commit 17f5b8b

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

flang/include/flang/Support/Fortran-features.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
8181

8282
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
8383
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
84+
using LanguageFeatureOrWarning = std::variant<LanguageFeature, UsageWarning>;
85+
using LanguageControlFlag =
86+
std::pair<LanguageFeatureOrWarning, /*shouldEnable=*/bool>;
8487

8588
class LanguageFeatureControl {
8689
public:
@@ -94,6 +97,13 @@ class LanguageFeatureControl {
9497
void EnableWarning(UsageWarning w, bool yes = true) {
9598
warnUsage_.set(w, yes);
9699
}
100+
void EnableWarning(LanguageFeatureOrWarning flag, bool yes = true) {
101+
if (std::holds_alternative<LanguageFeature>(flag)) {
102+
EnableWarning(std::get<LanguageFeature>(flag), yes);
103+
} else {
104+
EnableWarning(std::get<UsageWarning>(flag), yes);
105+
}
106+
}
97107
void WarnOnAllNonstandard(bool yes = true);
98108
bool IsWarnOnAllNonstandard() const { return warnAllLanguage_; }
99109
void WarnOnAllUsage(bool yes = true);
@@ -116,9 +126,11 @@ class LanguageFeatureControl {
116126
bool ShouldWarn(LanguageFeature f) const { return warnLanguage_.test(f); }
117127
bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
118128
// Cli options
129+
// Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
130+
std::optional<LanguageControlFlag> FindWarning(std::string_view input);
119131
// Take a string from the Cli and apply it to the LanguageFeatureControl.
120132
// Return true if the option was recognized (and hence applied).
121-
bool ApplyCliOption(std::string input);
133+
bool EnableWarning(std::string_view input);
122134
// The add and replace functions are not currently used but are provided
123135
// to allow a flexible many-to-one mapping from Cli spellings to enum values.
124136
// Taking a string by value because the functions own this string after the

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
10111011
if (wArg == "error") {
10121012
res.setWarnAsErr(true);
10131013
// -W(no-)<feature>
1014-
} else if (!features.ApplyCliOption(wArg)) {
1014+
} else if (!features.EnableWarning(wArg)) {
10151015
const unsigned diagID = diags.getCustomDiagID(
10161016
clang::DiagnosticsEngine::Error, "Unknown diagnostic option: -W%0");
10171017
diags.Report(diagID) << wArg;

flang/lib/Support/Fortran-features.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,23 @@ LanguageFeatureControl::LanguageFeatureControl() {
151151
warnLanguage_.set(LanguageFeature::NullActualForAllocatable);
152152
}
153153

154-
// Take a string from the Cli and apply it to the LanguageFeatureControl.
155-
bool LanguageFeatureControl::ApplyCliOption(std::string input) {
154+
std::optional<LanguageControlFlag> LanguageFeatureControl::FindWarning(
155+
std::string_view input) {
156156
bool negated{false};
157157
if (input.size() > 3 && input.substr(0, 3) == "no-") {
158158
negated = true;
159159
input = input.substr(3);
160160
}
161-
if (auto it{cliOptions_.find(input)}; it != cliOptions_.end()) {
162-
if (std::holds_alternative<LanguageFeature>(it->second)) {
163-
EnableWarning(std::get<LanguageFeature>(it->second), !negated);
164-
return true;
165-
}
166-
if (std::holds_alternative<UsageWarning>(it->second)) {
167-
EnableWarning(std::get<UsageWarning>(it->second), !negated);
168-
return true;
169-
}
161+
if (auto it{cliOptions_.find(std::string{input})}; it != cliOptions_.end()) {
162+
return std::make_pair(it->second, !negated);
163+
}
164+
return std::nullopt;
165+
}
166+
167+
bool LanguageFeatureControl::EnableWarning(std::string_view input) {
168+
if (auto warningAndEnabled{FindWarning(input)}) {
169+
EnableWarning(warningAndEnabled->first, warningAndEnabled->second);
170+
return true;
170171
}
171172
return false;
172173
}

0 commit comments

Comments
 (0)