[flang] Implemented a warning about contiguity of compile time constant values#161084
Conversation
…nt values Implemented common::UsageWarning::ConstantIsContiguous to warn about the following case: ``` integer, parameter :: num = 3 integer, parameter :: arr(num)=[(i, i=1,num)] logical, parameter :: result=is_contiguous(arr(num:1:-1)) end ``` Here, while array section is discontiguous, `arr` is a compile time constant, so array section created at compile time will end up being contiguous and `result` will be "true". If `arr` wasn't a constant, the result at runtime would have been "false".
|
@llvm/pr-subscribers-flang-semantics Author: Eugene Epshteyn (eugeneepshteyn) ChangesImplemented Here, while array section is discontiguous, Full diff: https://github.com/llvm/llvm-project/pull/161084.diff 3 Files Affected:
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 2bbc2385777da..51364d552be64 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
- CompatibleDeclarationsFromDistinctModules,
+ CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
RealConstantWidening, VolatileOrAsynchronousTemporary)
diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp
index c64f79e06a8ac..4bb1dc99ebec4 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -799,12 +799,20 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
}
} else if (name == "is_contiguous") {
if (args.at(0)) {
+ auto warnContiguous = [&]() {
+ if (auto source{args[0]->sourceLocation()}) {
+ context.Warn(common::UsageWarning::ConstantIsContiguous, *source,
+ "constant values constructed at compile time are likely to be contiguous"_warn_en_US);
+ }
+ };
if (auto *expr{args[0]->UnwrapExpr()}) {
if (auto contiguous{IsContiguous(*expr, context)}) {
+ warnContiguous();
return Expr<T>{*contiguous};
}
} else if (auto *assumedType{args[0]->GetAssumedTypeDummy()}) {
if (auto contiguous{IsContiguous(*assumedType, context)}) {
+ warnContiguous();
return Expr<T>{*contiguous};
}
}
diff --git a/flang/test/Semantics/contiguous-warn.f90 b/flang/test/Semantics/contiguous-warn.f90
new file mode 100644
index 0000000000000..5d87089a36778
--- /dev/null
+++ b/flang/test/Semantics/contiguous-warn.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+integer, parameter :: num = 3
+integer, parameter :: arr(num)=[(i, i=1,num)]
+!WARNING: constant values constructed at compile time are likely to be contiguous [-Wconstant-is-contiguous]
+logical, parameter :: result=is_contiguous(arr(num:1:-1))
+end
|
| } | ||
| } else if (name == "is_contiguous") { | ||
| if (args.at(0)) { | ||
| auto warnContiguous = [&]() { |
There was a problem hiding this comment.
Please always use braced initialization in everything before lowering or in the runtime.
| auto warnContiguous = [&]() { | ||
| if (auto source{args[0]->sourceLocation()}) { | ||
| context.Warn(common::UsageWarning::ConstantIsContiguous, *source, | ||
| "constant values constructed at compile time are likely to be contiguous"_warn_en_US); |
There was a problem hiding this comment.
The text of this message isn't really actionable; what should the programmer do?
There was a problem hiding this comment.
Message updated.
…nt values (llvm#161084) Implemented `common::UsageWarning::ConstantIsContiguous` to warn about the following case: ``` integer, parameter :: num = 3 integer, parameter :: arr(num)=[(i, i=1,num)] logical, parameter :: result=is_contiguous(arr(num:1:-1)) end ``` Here, while array section is discontiguous, `arr` is a compile time constant, so array section created at compile time will end up being contiguous and `result` will be "true". If `arr` wasn't a constant, the result at runtime would have been "false".
Implemented
common::UsageWarning::ConstantIsContiguousto warn about thefollowing case:
Here, while array section is discontiguous,
arris a compile time constant,so array section created at compile time will end up being contiguous and
resultwill be "true". Ifarrwasn't a constant, the result at runtimewould have been "false".