[flang-commits] [flang] [flang] Add warning when BOZ literal is too large for assignment (PR #210749)

John Otken via flang-commits flang-commits at lists.llvm.org
Mon Jul 20 09:05:38 PDT 2026


https://github.com/jotken created https://github.com/llvm/llvm-project/pull/210749

Generate a warning when a BOZ literal assignment does not fit into the left-hand side variable.

AI use disclaimer: Github CoPilot assisted with this PR. I manually reviewed and tested the code.

Co-authored-by: John Otken john.otken at hpe.com

>From 0b8fb1bafb8720c9433453a096591f958a31a575 Mon Sep 17 00:00:00 2001
From: John Otken <john at otken.com>
Date: Mon, 20 Jul 2026 10:52:30 -0500
Subject: [PATCH] [flang] Add warning when BOZ literal is too large for
 assignment

Generate a warning when a BOZ literal assignment does not fit into the lhs variable.

AI use disclaimer: Github CoPilot assisted with this PR. I manually reviewed and tested the code.

Co-authored-by: John Otken john.otken at hpe.com
---
 .../include/flang/Support/Fortran-features.h  |  3 ++-
 flang/lib/Semantics/expression.cpp            | 11 ++++++++
 flang/lib/Support/Fortran-features.cpp        |  1 +
 flang/test/Semantics/boz-truncation.f90       | 25 +++++++++++++++++++
 4 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/boz-truncation.f90

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 9bbbb7dd5f899..4ee956a0b4a4f 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -87,7 +87,8 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     RealConstantWidening, VolatileOrAsynchronousTemporary, UnusedVariable,
     UsedUndefinedVariable, BadValueInDeadCode, AssumedTypeSizeDummy,
     MisplacedIgnoreTKR, NamelistParameter, ImpureFinalInPure,
-    IgnoredNoReallocateLHS, ExperimentalOption, IoImpliedDoIndexConflict)
+    IgnoredNoReallocateLHS, ExperimentalOption, IoImpliedDoIndexConflict,
+    BOZLiteralTruncation)
 
 using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
 using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index fc57cc43e981c..d185b94eaf9da 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -5875,6 +5875,17 @@ void ArgumentAnalyzer::ConvertBOZAssignmentRHS(const DynamicType &lhsType) {
       lhsType.category() == TypeCategory::Unsigned ||
       lhsType.category() == TypeCategory::Real) {
     Expr<SomeType> rhs{MoveExpr(1)};
+    if (lhsType.category() == TypeCategory::Integer ||
+        lhsType.category() == TypeCategory::Unsigned) {
+      if (const auto *boz{std::get_if<BOZLiteralConstant>(&rhs.u)};
+          boz && boz->bits - boz->LEADZ() > lhsType.kind() * 8) {
+        context_.Warn(common::UsageWarning::BOZLiteralTruncation,
+            "BOZ literal constant is too large for %s(KIND=%d) assignment target"_warn_en_US,
+            lhsType.category() == TypeCategory::Unsigned ? "UNSIGNED"
+                                                         : "INTEGER",
+            lhsType.kind());
+      }
+    }
     if (MaybeExpr converted{ConvertToType(lhsType, std::move(rhs))}) {
       actuals_[1] = std::move(*converted);
     }
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index d94729200df06..0af3ff61d18e1 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -222,6 +222,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
   warnUsage_.set(UsageWarning::ImpureFinalInPure);
   warnUsage_.set(UsageWarning::IgnoredNoReallocateLHS);
   warnUsage_.set(UsageWarning::IoImpliedDoIndexConflict);
+  warnUsage_.set(UsageWarning::BOZLiteralTruncation);
   warnLanguage_.set(LanguageFeature::OpenMPThreadprivateEquivalence);
   warnLanguage_.set(LanguageFeature::OpenAccDefaultNoneScalarsStrict);
   warnLanguage_.set(LanguageFeature::OpenACCMultipleNamesInRoutine);
diff --git a/flang/test/Semantics/boz-truncation.f90 b/flang/test/Semantics/boz-truncation.f90
new file mode 100644
index 0000000000000..8a337ac92849e
--- /dev/null
+++ b/flang/test/Semantics/boz-truncation.f90
@@ -0,0 +1,25 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -funsigned -Werror
+! Test the warning for a BOZ literal constant that is too large for the
+! INTEGER or UNSIGNED type of an assignment target.
+program boztest
+  integer(4) :: i4
+  integer(8) :: i8
+  unsigned(2) :: u2
+  unsigned(4) :: u4
+
+  ! INTEGER targets
+  i4 = z'FFFFFFFF' ! fits in 32 bits, no warning
+  !WARNING: BOZ literal constant is too large for INTEGER(KIND=4) assignment target [-Wboz-literal-truncation]
+  i4 = z'1FFFFFFFF' ! 33 bits, too large for INTEGER(4)
+  i8 = z'FFFFFFFFFFFFFFFF' ! fits in 64 bits, no warning
+  !WARNING: BOZ literal constant is too large for INTEGER(KIND=8) assignment target [-Wboz-literal-truncation]
+  i8 = z'1FFFFFFFFFFFFFFFF' ! 65 bits, too large for INTEGER(8)
+
+  ! UNSIGNED targets
+  u2 = z'FFFF' ! fits in 16 bits, no warning
+  !WARNING: BOZ literal constant is too large for UNSIGNED(KIND=2) assignment target [-Wboz-literal-truncation]
+  u2 = z'1FFFF' ! 17 bits, too large for UNSIGNED(2)
+  u4 = z'FFFFFFFF' ! fits in 32 bits, no warning
+  !WARNING: BOZ literal constant is too large for UNSIGNED(KIND=4) assignment target [-Wboz-literal-truncation]
+  u4 = z'1FFFFFFFF' ! 33 bits, too large for UNSIGNED(4)
+end program



More information about the flang-commits mailing list