[flang-commits] [PATCH] D143820: [flang] Diagnose REPEAT with negative NCOPIES=

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Feb 13 14:49:07 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG8151d6f80458: [flang] Diagnose REPEAT with negative NCOPIES= (authored by klausler).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143820/new/

https://reviews.llvm.org/D143820

Files:
  flang/lib/Evaluate/character.h
  flang/lib/Evaluate/fold-character.cpp
  flang/test/Evaluate/errors01.f90


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -148,6 +148,8 @@
   subroutine s13
     !CHECK: portability: Result of REPEAT() is too large to compute at compilation time (1.1259e+15 characters)
     print *, repeat(repeat(' ', 2**20), 2**30)
+    !CHECK: error: NCOPIES= argument to REPEAT() should be nonnegative, but is -666
+    print *, repeat(' ', -666)
   end subroutine
   subroutine warnings
     real, parameter :: ok1 = scale(0.0, 99999) ! 0.0
Index: flang/lib/Evaluate/fold-character.cpp
===================================================================
--- flang/lib/Evaluate/fold-character.cpp
+++ flang/lib/Evaluate/fold-character.cpp
@@ -99,7 +99,11 @@
             context, funcRef.arguments())}) {
       auto str{std::get<Scalar<T>>(*scalars)};
       auto n{std::get<Scalar<SubscriptInteger>>(*scalars).ToInt64()};
-      if (static_cast<double>(n) * str.size() >
+      if (n < 0) {
+        context.messages().Say(
+            "NCOPIES= argument to REPEAT() should be nonnegative, but is %jd"_err_en_US,
+            static_cast<std::intmax_t>(n));
+      } else if (static_cast<double>(n) * str.size() >
           (1 << 20)) { // sanity limit of 1MiB
         context.messages().Say(
             "Result of REPEAT() is too large to compute at compilation time (%g characters)"_port_en_US,
Index: flang/lib/Evaluate/character.h
===================================================================
--- flang/lib/Evaluate/character.h
+++ flang/lib/Evaluate/character.h
@@ -105,7 +105,7 @@
 
   static Character REPEAT(const Character &str, ConstantSubscript ncopies) {
     Character result;
-    if (!str.empty()) {
+    if (!str.empty() && ncopies > 0) {
       result.reserve(ncopies * str.size());
       while (ncopies-- > 0) {
         result += str;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143820.497120.patch
Type: text/x-patch
Size: 1919 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230213/63dfd0c9/attachment.bin>


More information about the flang-commits mailing list