[flang-commits] [PATCH] D143820: [flang] Diagnose REPEAT with negative NCOPIES=
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Sat Feb 11 10:54:33 PST 2023
klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.
Emit an error when the NCOPIES= argument to the intrinsic function
REPEAT has a negative value. (The current implementation crashes, which
isn't informative.)
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.496700.patch
Type: text/x-patch
Size: 1919 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230211/1138274c/attachment-0001.bin>
More information about the flang-commits
mailing list