[flang-commits] [flang] [flang][driver] add ability to look up feature flags without setting them (PR #144559)

Andre Kuhlenschmidt via flang-commits flang-commits at lists.llvm.org
Tue Jun 17 09:22:35 PDT 2025


https://github.com/akuhlens created https://github.com/llvm/llvm-project/pull/144559

This just adds some convenience methods to feature control and rewrites old code in terms of those methods.

>From e8b450fd62f8b8890dc958b122298af4a0760662 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Mon, 16 Jun 2025 18:02:13 -0700
Subject: [PATCH 1/2] initial commit

---
 .../include/flang/Support/Fortran-features.h  | 13 +++++++++++-
 flang/lib/Frontend/CompilerInvocation.cpp     |  2 +-
 flang/lib/Support/Fortran-features.cpp        | 21 ++++++++++---------
 3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index ea0845b7d605f..ffd2e21070f6f 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -81,6 +81,8 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
 
 using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
 using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
+using Warning = std::variant<LanguageFeature, UsageWarning>;
+using WarningAndEnabled = std::pair<Warning, bool>;
 
 class LanguageFeatureControl {
 public:
@@ -94,6 +96,13 @@ class LanguageFeatureControl {
   void EnableWarning(UsageWarning w, bool yes = true) {
     warnUsage_.set(w, yes);
   }
+  void EnableWarning(Warning warning, bool yes = true) {
+    if (std::holds_alternative<LanguageFeature>(warning)) {
+      EnableWarning(std::get<LanguageFeature>(warning), yes);
+    } else {
+      EnableWarning(std::get<UsageWarning>(warning), yes);
+    }
+  }
   void WarnOnAllNonstandard(bool yes = true);
   bool IsWarnOnAllNonstandard() const { return warnAllLanguage_; }
   void WarnOnAllUsage(bool yes = true);
@@ -116,9 +125,11 @@ class LanguageFeatureControl {
   bool ShouldWarn(LanguageFeature f) const { return warnLanguage_.test(f); }
   bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
   // Cli options
+  // Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
+  std::optional<WarningAndEnabled> FindWarning(std::string input);
   // Take a string from the Cli and apply it to the LanguageFeatureControl.
   // Return true if the option was recognized (and hence applied).
-  bool ApplyCliOption(std::string input);
+  bool EnableWarning(std::string input);
   // The add and replace functions are not currently used but are provided
   // to allow a flexible many-to-one mapping from Cli spellings to enum values.
   // Taking a string by value because the functions own this string after the
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 147849b0b7d2a..2603a3f6dc643 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1011,7 +1011,7 @@ static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
       if (wArg == "error") {
         res.setWarnAsErr(true);
         // -W(no-)<feature>
-      } else if (!features.ApplyCliOption(wArg)) {
+      } else if (!features.EnableWarning(wArg)) {
         const unsigned diagID = diags.getCustomDiagID(
             clang::DiagnosticsEngine::Error, "Unknown diagnostic option: -W%0");
         diags.Report(diagID) << wArg;
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index 08ded173de513..28df4af501534 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -151,22 +151,23 @@ LanguageFeatureControl::LanguageFeatureControl() {
   warnLanguage_.set(LanguageFeature::NullActualForAllocatable);
 }
 
-// Take a string from the Cli and apply it to the LanguageFeatureControl.
-bool LanguageFeatureControl::ApplyCliOption(std::string input) {
+std::optional<std::pair<Warning, bool>> LanguageFeatureControl::FindWarning(
+    std::string input) {
   bool negated{false};
   if (input.size() > 3 && input.substr(0, 3) == "no-") {
     negated = true;
     input = input.substr(3);
   }
   if (auto it{cliOptions_.find(input)}; it != cliOptions_.end()) {
-    if (std::holds_alternative<LanguageFeature>(it->second)) {
-      EnableWarning(std::get<LanguageFeature>(it->second), !negated);
-      return true;
-    }
-    if (std::holds_alternative<UsageWarning>(it->second)) {
-      EnableWarning(std::get<UsageWarning>(it->second), !negated);
-      return true;
-    }
+    return std::make_pair(it->second, !negated);
+  }
+  return std::nullopt;
+}
+
+bool LanguageFeatureControl::EnableWarning(std::string input) {
+  if (auto warningAndEnabled{FindWarning(input)}) {
+    EnableWarning(warningAndEnabled->first, warningAndEnabled->second);
+    return true;
   }
   return false;
 }

>From f4f69cd1dc68fda1763e315124cb3583b60afa43 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Tue, 17 Jun 2025 09:21:35 -0700
Subject: [PATCH 2/2] minor tweeks

---
 flang/include/flang/Support/Fortran-features.h | 4 ++--
 flang/lib/Support/Fortran-features.cpp         | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index ffd2e21070f6f..3019319a153a6 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -126,10 +126,10 @@ class LanguageFeatureControl {
   bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
   // Cli options
   // Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
-  std::optional<WarningAndEnabled> FindWarning(std::string input);
+  std::optional<WarningAndEnabled> FindWarning(std::string_view input);
   // Take a string from the Cli and apply it to the LanguageFeatureControl.
   // Return true if the option was recognized (and hence applied).
-  bool EnableWarning(std::string input);
+  bool EnableWarning(std::string_view input);
   // The add and replace functions are not currently used but are provided
   // to allow a flexible many-to-one mapping from Cli spellings to enum values.
   // Taking a string by value because the functions own this string after the
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index 28df4af501534..6b1ddfd91cd39 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -152,19 +152,19 @@ LanguageFeatureControl::LanguageFeatureControl() {
 }
 
 std::optional<std::pair<Warning, bool>> LanguageFeatureControl::FindWarning(
-    std::string input) {
+    std::string_view input) {
   bool negated{false};
   if (input.size() > 3 && input.substr(0, 3) == "no-") {
     negated = true;
     input = input.substr(3);
   }
-  if (auto it{cliOptions_.find(input)}; it != cliOptions_.end()) {
+  if (auto it{cliOptions_.find(std::string{input})}; it != cliOptions_.end()) {
     return std::make_pair(it->second, !negated);
   }
   return std::nullopt;
 }
 
-bool LanguageFeatureControl::EnableWarning(std::string input) {
+bool LanguageFeatureControl::EnableWarning(std::string_view input) {
   if (auto warningAndEnabled{FindWarning(input)}) {
     EnableWarning(warningAndEnabled->first, warningAndEnabled->second);
     return true;



More information about the flang-commits mailing list