[flang-commits] [flang] [flang] Pass llvm::StringRef directly to message formatting (PR #228449)

via flang-commits flang-commits at lists.llvm.org
Fri Oct 2 07:18:46 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: Krzysztof Parzyszek (kparzysz)

<details>
<summary>Changes</summary>

Added an overload of MessageFormattedText::Convert for llvm::StringRef. This removes the need to explicitly convert llvm::StringRef arguments to std::string in calls to diagnostic functions (Say/Warn/Attach).

Only direct message arguments are changed; uses that feed ToUpperCaseLetters (which takes std::string_view), llvm::Twine concatenations, and variable initializers are left as-is. No behavioral change.

---
Full diff: https://github.com/llvm/llvm-project/pull/228449.diff


8 Files Affected:

- (modified) flang/include/flang/Parser/message.h (+2) 
- (modified) flang/lib/Parser/message.cpp (+5) 
- (modified) flang/lib/Semantics/check-acc-structure.cpp (+2-2) 
- (modified) flang/lib/Semantics/check-directive-structure.h (+1-2) 
- (modified) flang/lib/Semantics/check-omp-loop.cpp (+4-4) 
- (modified) flang/lib/Semantics/check-omp-structure.cpp (+11-12) 
- (modified) flang/lib/Semantics/check-omp-syntax.cpp (+8-8) 
- (modified) flang/lib/Semantics/resolve-directives.cpp (+3-3) 


``````````diff
diff --git a/flang/include/flang/Parser/message.h b/flang/include/flang/Parser/message.h
index c70c335133ba8..ae4a28b846ba1 100644
--- a/flang/include/flang/Parser/message.h
+++ b/flang/include/flang/Parser/message.h
@@ -19,6 +19,7 @@
 #include "flang/Common/reference-counted.h"
 #include "flang/Common/restorer.h"
 #include "flang/Support/Fortran-features.h"
+#include "llvm/ADT/StringRef.h"
 #include <cstddef>
 #include <cstring>
 #include <forward_list>
@@ -159,6 +160,7 @@ class MessageFormattedText {
   const char *Convert(const std::string_view &);
   const char *Convert(std::string_view &&);
   const char *Convert(CharBlock);
+  const char *Convert(llvm::StringRef);
   std::intmax_t Convert(std::int64_t x) { return x; }
   std::uintmax_t Convert(std::uint64_t x) { return x; }
 
diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp
index 8189f1800f2eb..1df368fa4a750 100644
--- a/flang/lib/Parser/message.cpp
+++ b/flang/lib/Parser/message.cpp
@@ -9,6 +9,7 @@
 #include "flang/Parser/message.h"
 #include "flang/Common/idioms.h"
 #include "flang/Parser/char-set.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cstdarg>
@@ -92,6 +93,10 @@ const char *MessageFormattedText::Convert(CharBlock x) {
   return Convert(x.ToString());
 }
 
+const char *MessageFormattedText::Convert(llvm::StringRef x) {
+  return Convert(x.str());
+}
+
 std::string MessageExpectedText::ToString() const {
   return common::visit(
       common::visitors{
diff --git a/flang/lib/Semantics/check-acc-structure.cpp b/flang/lib/Semantics/check-acc-structure.cpp
index b4ec2a494b1d1..de59796e13401 100644
--- a/flang/lib/Semantics/check-acc-structure.cpp
+++ b/flang/lib/Semantics/check-acc-structure.cpp
@@ -318,7 +318,7 @@ void AccStructureChecker::CheckLoopLevelClauseValue(
           &context_.FindScope(GetContext().clauseSource))) {
     context_.Say(GetContext().clauseSource,
         "'%s(value)' not allowed in subprogram compiled with ROUTINE directive"_err_en_US,
-        clauseName.str());
+        clauseName);
     return;
   }
 
@@ -333,7 +333,7 @@ void AccStructureChecker::CheckLoopLevelClauseValue(
     }
   }
   context_.Say(GetContext().clauseSource,
-      "'%s(value)' not allowed in %s directive"_err_en_US, clauseName.str(),
+      "'%s(value)' not allowed in %s directive"_err_en_US, clauseName,
       parser::ToUpperCaseLetters(getDirectiveName(dir).str()));
 }
 
diff --git a/flang/lib/Semantics/check-directive-structure.h b/flang/lib/Semantics/check-directive-structure.h
index 3061a5a94c2df..f750bb283b2c4 100644
--- a/flang/lib/Semantics/check-directive-structure.h
+++ b/flang/lib/Semantics/check-directive-structure.h
@@ -699,8 +699,7 @@ void DirectiveStructureChecker<D, C, PC,
       context_.Say(GetContext().clauseSource,
           "The %s of the %s clause must be "
           "a positive integer expression"_err_en_US,
-          paramName.str(),
-          parser::ToUpperCaseLetters(getClauseName(clause).str()));
+          paramName, parser::ToUpperCaseLetters(getClauseName(clause).str()));
     }
   }
 }
diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index b6fb414854637..d2466cdf219cb 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -795,7 +795,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
       auto &desc{llvm::omp::getDescriptor(llvm::omp::Modifier::LinearModifier)};
       context_.Say(source,
           "The list item '%s' specified without the REF '%s' must be of INTEGER type"_err_en_US,
-          symbol->name(), desc.getName().str());
+          symbol->name(), desc.getName());
     }
   }};
 
@@ -820,7 +820,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
         if (dir != llvm::omp::Directive::OMPD_declare_simd) {
           context_.Say(modSource,
               "A REF or UVAL '%s' may not be specified in a LINEAR clause on the %s directive"_err_en_US,
-              desc.getName().str(), parser::omp::GetUpperName(dir, version));
+              desc.getName(), parser::omp::GetUpperName(dir, version));
           valid = false;
         }
       }
@@ -840,7 +840,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
               !IsPolymorphic(*symbol)) {
             context_.Say(source,
                 "The list item `%s` specified with the REF '%s' must be polymorphic variable, assumed-shape array, or a variable with the `ALLOCATABLE` attribute"_err_en_US,
-                symbol->name(), desc.getName().str());
+                symbol->name(), desc.getName());
           }
         }
         if (linearMod->v == parser::OmpLinearModifier::Value::Ref ||
@@ -848,7 +848,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
           if (!IsDummy(*symbol) || IsValue(*symbol)) {
             context_.Say(source,
                 "If the `%s` is REF or UVAL, the list item '%s' must be a dummy argument without the VALUE attribute"_err_en_US,
-                desc.getName().str(), symbol->name());
+                desc.getName(), symbol->name());
           }
         }
       } // for (symbol, source)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index c45ad61f73aba..041b80ccffad6 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -2270,20 +2270,19 @@ void OmpStructureChecker::CheckInitOnDepobj(
           llvm::omp::getDescriptor(llvm::omp::Modifier::DepinfoModifier)};
       context_.Say(OmpGetModifierSource(modifiers, depInfo),
           "'%s' is not an allowed value of the '%s' modifier"_err_en_US,
-          parser::ToUpperCaseLetters(EnumToString(depKind)),
-          desc.getName().str());
+          parser::ToUpperCaseLetters(EnumToString(depKind)), desc.getName());
     }
   } else {
     auto &desc{llvm::omp::getDescriptor(llvm::omp::Modifier::DepinfoModifier)};
     context_.Say(initClause.source,
         "The '%s' modifier is required on a DEPOBJ construct"_err_en_US,
-        desc.getName().str());
+        desc.getName());
   }
   if (auto *prefType{OmpGetUniqueModifier<parser::OmpPreferType>(modifiers)}) {
     auto &desc{llvm::omp::getDescriptor(llvm::omp::Modifier::PreferType)};
     context_.Say(OmpGetModifierSource(modifiers, prefType),
         "The '%s' modifier is not allowed on a DEPOBJ construct"_err_en_US,
-        desc.getName().str());
+        desc.getName());
   }
 }
 
@@ -4609,10 +4608,10 @@ void OmpStructureChecker::CheckVarIsNotPartOfAnotherVar(
       if (clause.empty() &&
           llvm::omp::nonPartialVarSet.test(GetContext().directive)) {
         context_.Say(source, "%s cannot appear on the %s directive"_err_en_US,
-            kind.str(), ContextDirectiveAsFortran());
+            kind, ContextDirectiveAsFortran());
       } else {
-        context_.Say(source, "%s cannot appear in a %s clause"_err_en_US,
-            kind.str(), clause.str());
+        context_.Say(
+            source, "%s cannot appear in a %s clause"_err_en_US, kind, clause);
       }
     }
   }
@@ -5028,7 +5027,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) {
           llvm::omp::getDescriptor(llvm::omp::Modifier::AttachModifier)};
       context_.Say(OmpGetModifierSource(modifiers, attach),
           "The '%s' modifier can only appear on a map-entering construct or on a DECLARE_MAPPER directive"_err_en_US,
-          desc.getName().str());
+          desc.getName());
     }
 
     auto hasBasePointer{[&](const SomeExpr &item) {
@@ -5182,7 +5181,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Device &x) {
               .getName()};
       context_.Say(OmpGetModifierSource(modifiers, deviceMod),
           "The ANCESTOR %s must not appear on the DEVICE clause on any directive other than the TARGET construct. Found on %s construct."_err_en_US,
-          name.str(), parser::omp::GetUpperName(dir, version));
+          name, parser::omp::GetUpperName(dir, version));
     }
   }
 }
@@ -5835,7 +5834,7 @@ void OmpStructureChecker::CheckUsesAllocatorsSpec(
           llvm::omp::getDescriptor(llvm::omp::Modifier::MemSpace).getName()};
       context_.Say(memSpaceSource,
           "The '%s' modifier must name a predefined memory space"_err_en_US,
-          name.str());
+          name);
     }
   }
 
@@ -6246,7 +6245,7 @@ void OmpStructureChecker::CheckCrayPointee(
             semantics::GetCrayPointer(*symbol).name().ToString() + "' instead";
       context_.Say(source,
           "Cray Pointee '%s' may not appear in %s clause%s"_err_en_US,
-          symbol->name(), clause.str(), suggestionMsg);
+          symbol->name(), clause, suggestionMsg);
     }
   }
 }
@@ -6683,7 +6682,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPInteropConstruct &x) {
                     llvm::omp::Modifier::DepinfoModifier)};
                 context_.Say(OmpGetModifierSource(modifiers, depInfo),
                     "The '%s' is not allowed on INTEROP construct"_err_en_US,
-                    desc.getName().str());
+                    desc.getName());
               }
               // A prefer_type foreign-runtime-identifier must be a
               // constant expression of integer OpenMP type or a base
diff --git a/flang/lib/Semantics/check-omp-syntax.cpp b/flang/lib/Semantics/check-omp-syntax.cpp
index fed7767870254..ceda90edaf640 100644
--- a/flang/lib/Semantics/check-omp-syntax.cpp
+++ b/flang/lib/Semantics/check-omp-syntax.cpp
@@ -338,8 +338,8 @@ bool OmpStructureChecker::VerifyModifierRequired(
 
   for (llvm::omp::Modifier m : result.first) {
     auto &mdesc{llvm::omp::getDescriptor(m)};
-    context_.Say(clause.source, "'%s' modifier is required"_err_en_US,
-        mdesc.getName().str());
+    context_.Say(
+        clause.source, "'%s' modifier is required"_err_en_US, mdesc.getName());
   }
   for (llvm::omp::ModifierSet s : result.second) {
     auto &sdesc{llvm::omp::getDescriptor(s)};
@@ -348,7 +348,7 @@ bool OmpStructureChecker::VerifyModifierRequired(
     if (llvm::omp::isModifierGroup(s)) {
       context_.Say(clause.source,
           "modifier from '%s' modifier group is required"_err_en_US,
-          sdesc.getName().str());
+          sdesc.getName());
     } else {
       context_.Say(clause.source,
           "modifier from the modifier set on %s clause is required"_err_en_US,
@@ -369,7 +369,7 @@ bool OmpStructureChecker::VerifyModifierUnique(
     auto &mdesc{llvm::omp::getDescriptor(id)};
     context_
         .Say(where.first, "'%s' modifier cannot occur multiple times"_err_en_US,
-            mdesc.getName().str())
+            mdesc.getName())
         .Attach(where.second, "previous occurrence of this modifier"_en_US);
   }
 
@@ -387,9 +387,9 @@ bool OmpStructureChecker::VerifyModifierExclusive(
     context_
         .Say(source,
             "An exclusive '%s' modifier cannot be specified together with a modifier of a different type"_err_en_US,
-            llvm::omp::getDescriptor(id).getName().str())
+            llvm::omp::getDescriptor(id).getName())
         .Attach(otherSource, "'%s' provided here"_en_US,
-            llvm::omp::getDescriptor(otherId).getName().str());
+            llvm::omp::getDescriptor(otherId).getName());
   }
 
   auto resultMut = VerifyMutuallyExclusive(info, clause.value, version);
@@ -400,7 +400,7 @@ bool OmpStructureChecker::VerifyModifierExclusive(
     context_
         .Say(otherSource,
             "The '%s' and '%s' modifiers are mutually exclusive"_err_en_US,
-            llvm::omp::getDescriptor(otherId).getName().str(), thisName)
+            llvm::omp::getDescriptor(otherId).getName(), thisName)
         .Attach(source, "'%s' modifier specified here"_en_US, thisName);
   }
 
@@ -419,7 +419,7 @@ bool OmpStructureChecker::VerifyModifierUltimate(
 
   for (auto [id, where] : result) {
     context_.Say(where, "'%s' should be the %s modifier"_err_en_US,
-        llvm::omp::getDescriptor(id).getName().str(), expected);
+        llvm::omp::getDescriptor(id).getName(), expected);
   }
 
   return result.empty();
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 12373d9284673..b6a3db9375faf 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -3519,13 +3519,13 @@ void OmpAttributeVisitor::CheckObjectIsPrivatizable(
   if (SymbolOrEquivalentIsInNamelist(symbol)) {
     context_.Say(name.source,
         "Variable '%s' in NAMELIST cannot be in a %s clause"_err_en_US,
-        name.ToString(), clauseName.str());
+        name.ToString(), clauseName);
   }
 
   if (ultimateSymbol.has<AssocEntityDetails>()) {
     context_.Say(name.source,
         "Variable '%s' in ASSOCIATE cannot be in a %s clause"_err_en_US,
-        name.ToString(), clauseName.str());
+        name.ToString(), clauseName);
   }
 
   if (stmtFunctionExprSymbols_.find(ultimateSymbol) !=
@@ -3533,7 +3533,7 @@ void OmpAttributeVisitor::CheckObjectIsPrivatizable(
     context_.Say(name.source,
         "Variable '%s' in statement function expression cannot be in a "
         "%s clause"_err_en_US,
-        name.ToString(), clauseName.str());
+        name.ToString(), clauseName);
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/228449


More information about the flang-commits mailing list