-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[flang] Implemented a warning about contiguity of compile time constant values #161084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
|
flang/lib/Evaluate/fold-logical.cpp
Outdated
} | ||
} else if (name == "is_contiguous") { | ||
if (args.at(0)) { | ||
auto warnContiguous = [&]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please always use braced initialization in everything before lowering or in the runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
flang/lib/Evaluate/fold-logical.cpp
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text of this message isn't really actionable; what should the programmer do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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::ConstantIsContiguous
to warn about thefollowing case:
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". Ifarr
wasn't a constant, the result at runtimewould have been "false".