Skip to content

Commit e8b450f

Browse files
committed
initial commit
1 parent 2bf3cca commit e8b450f

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

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

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

8282
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
8383
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
84+
using Warning = std::variant<LanguageFeature, UsageWarning>;
85+
using WarningAndEnabled = std::pair<Warning, bool>;
8486

8587
class LanguageFeatureControl {
8688
public:
@@ -94,6 +96,13 @@ class LanguageFeatureControl {
9496
void EnableWarning(UsageWarning w, bool yes = true) {
9597
warnUsage_.set(w, yes);
9698
}
99+
void EnableWarning(Warning warning, bool yes = true) {
100+
if (std::holds_alternative<LanguageFeature>(warning)) {
101+
EnableWarning(std::get<LanguageFeature>(warning), yes);
102+
} else {
103+
EnableWarning(std::get<UsageWarning>(warning), yes);
104+
}
105+
}
97106
void WarnOnAllNonstandard(bool yes = true);
98107
bool IsWarnOnAllNonstandard() const { return warnAllLanguage_; }
99108
void WarnOnAllUsage(bool yes = true);
@@ -116,9 +125,11 @@ class LanguageFeatureControl {
116125
bool ShouldWarn(LanguageFeature f) const { return warnLanguage_.test(f); }
117126
bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
118127
// Cli options
128+
// Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
129+
std::optional<WarningAndEnabled> FindWarning(std::string input);
119130
// Take a string from the Cli and apply it to the LanguageFeatureControl.
120131
// Return true if the option was recognized (and hence applied).
121-
bool ApplyCliOption(std::string input);
132+
bool EnableWarning(std::string input);
122133
// The add and replace functions are not currently used but are provided
123134
// to allow a flexible many-to-one mapping from Cli spellings to enum values.
124135
// 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: 11 additions & 10 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<std::pair<Warning, bool>> LanguageFeatureControl::FindWarning(
155+
std::string input) {
156156
bool negated{false};
157157
if (input.size() > 3 && input.substr(0, 3) == "no-") {
158158
negated = true;
159159
input = input.substr(3);
160160
}
161161
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-
}
162+
return std::make_pair(it->second, !negated);
163+
}
164+
return std::nullopt;
165+
}
166+
167+
bool LanguageFeatureControl::EnableWarning(std::string 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)