Skip to content

Commit

Permalink
[flang] Don't emit debugging output to module file
Browse files Browse the repository at this point in the history
When a constant array value has a non-default lower bound, the current
expression formatting code uses a non-Fortran syntax to dump the lower
bounds. (There's no way in Fortran to explicitly specify such a constant value,
but they can be created through the use of named constants.)  But we
don't want this lower bounds syntax from expression dumping to show up
in module files, since it can't be parsed back in.  So disable that
part of expression formatting by default.

Fixes #64391.

Differential Revision: https://reviews.llvm.org/D157330
  • Loading branch information
klausler committed Aug 8, 2023
1 parent 7593f9b commit c465158
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions flang/lib/Evaluate/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

namespace Fortran::evaluate {

// Constant arrays can have non-default lower bounds, but this can't be
// expressed in Fortran syntax directly, only implied through the use of
// named constant (PARAMETER) definitions. For debugging, setting this flag
// enables a non-standard %LBOUND=[...] argument to the RESHAPE intrinsic
// calls used to dumy constants. It's off by default so that this syntax
// doesn't show up in module files.
static const bool printLbounds{false};

static void ShapeAsFortran(llvm::raw_ostream &o,
const ConstantSubscripts &shape, const ConstantSubscripts &lbounds,
bool hasNonDefaultLowerBound) {
Expand Down Expand Up @@ -46,7 +54,7 @@ static void ShapeAsFortran(llvm::raw_ostream &o,
template <typename RESULT, typename VALUE>
llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
llvm::raw_ostream &o) const {
bool hasNonDefaultLowerBound{HasNonDefaultLowerBound()};
bool hasNonDefaultLowerBound{printLbounds && HasNonDefaultLowerBound()};
if (Rank() > 1 || hasNonDefaultLowerBound) {
o << "reshape(";
}
Expand Down Expand Up @@ -90,7 +98,7 @@ llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
template <int KIND>
llvm::raw_ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
llvm::raw_ostream &o) const {
bool hasNonDefaultLowerBound{HasNonDefaultLowerBound()};
bool hasNonDefaultLowerBound{printLbounds && HasNonDefaultLowerBound()};
if (Rank() > 1 || hasNonDefaultLowerBound) {
o << "reshape(";
}
Expand Down
10 changes: 10 additions & 0 deletions flang/test/Semantics/modfile56.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
! RUN: %python %S/test_modfile.py %s %flang_fc1
! Named constant array with non-default lower bound
module m
real, parameter :: x(0:0) = [0.]
end

!Expect: m.mod
!module m
!real(4),parameter::x(0_8:0_8)=[REAL(4)::0._4]
!end

0 comments on commit c465158

Please sign in to comment.