Skip to content

Commit

Permalink
[flang][runtime] Trim FORMATs echoed to error messages
Browse files Browse the repository at this point in the history
Since dynamic FORMAT strings usually come from blank-padded fixed-length
CHARACTER variables, trim leading and trailing blanks from them when they
are echoed to error messages for better readability.

Differential Revision: https://reviews.llvm.org/D129024
  • Loading branch information
klausler committed Jul 7, 2022
1 parent ac77649 commit 7c708ad
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions flang/runtime/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,24 @@ template <typename CONTEXT> class FormatControl {

void ReportBadFormat(Context &context, const char *msg, int offset) const {
if constexpr (std::is_same_v<CharType, char>) {
context.SignalError(IostatErrorInFormat,
"%s; at offset %d in format '%s'", msg, offset, format_);
} else {
context.SignalError(IostatErrorInFormat, "%s; at offset %d", msg, offset);
// Echo the bad format in the error message, but trim any leading or
// trailing spaces.
int firstNonBlank{0};
while (firstNonBlank < formatLength_ && format_[firstNonBlank] == ' ') {
++firstNonBlank;
}
int lastNonBlank{formatLength_ - 1};
while (lastNonBlank > firstNonBlank && format_[lastNonBlank] == ' ') {
--lastNonBlank;
}
if (firstNonBlank <= lastNonBlank) {
context.SignalError(IostatErrorInFormat,
"%s; at offset %d in format '%.*s'", msg, offset,
lastNonBlank - firstNonBlank + 1, format_ + firstNonBlank);
return;
}
}
context.SignalError(IostatErrorInFormat, "%s; at offset %d", msg, offset);
}

// Data members are arranged and typed so as to reduce size.
Expand Down

0 comments on commit 7c708ad

Please sign in to comment.