From 4dceb25dd1b96c93a7bd302c5d81bb1aacf75f4d Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Mon, 14 Jul 2025 11:11:43 -0700 Subject: [PATCH] [flang] Don't create bogus tokens from token pasting (##) (#147596) When blank tokens arise from macro replacement in token sequences with token pasting (##), the preprocessor is producing some bogus tokens (e.g., "name(") that can lead to subtle bugs later when macro names are not recognized as such. The fix is to not paste tokens together when the result would not be a valid Fortran or C token in the preprocessing context. --- flang/include/flang/Parser/char-block.h | 2 ++ flang/lib/Parser/preprocessor.cpp | 43 ++++++++++++++++++++----- flang/lib/Parser/unparse.cpp | 2 +- flang/lib/Semantics/expression.cpp | 2 +- flang/lib/Semantics/resolve-names.cpp | 2 +- flang/lib/Semantics/tools.cpp | 2 +- flang/test/Preprocessing/bug1126.F90 | 20 ++++++++++++ 7 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 flang/test/Preprocessing/bug1126.F90 diff --git a/flang/include/flang/Parser/char-block.h b/flang/include/flang/Parser/char-block.h index b3b1f04034d3..93a7e744d8d0 100644 --- a/flang/include/flang/Parser/char-block.h +++ b/flang/include/flang/Parser/char-block.h @@ -46,6 +46,8 @@ public: constexpr const char &operator[](std::size_t j) const { return interval_.start()[j]; } + constexpr const char &front() const { return (*this)[0]; } + constexpr const char &back() const { return (*this)[size() - 1]; } bool Contains(const CharBlock &that) const { return interval_.Contains(that.interval_); diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp index 8ef9810463d5..0aadc41934f3 100644 --- a/flang/lib/Parser/preprocessor.cpp +++ b/flang/lib/Parser/preprocessor.cpp @@ -156,23 +156,50 @@ static TokenSequence TokenPasting(TokenSequence &&text) { } TokenSequence result; std::size_t tokens{text.SizeInTokens()}; - bool pasting{false}; + std::optional before; // last non-blank token before ## for (std::size_t j{0}; j < tokens; ++j) { - if (IsTokenPasting(text.TokenAt(j))) { - if (!pasting) { + CharBlock after{text.TokenAt(j)}; + if (!before) { + if (IsTokenPasting(after)) { while (!result.empty() && result.TokenAt(result.SizeInTokens() - 1).IsBlank()) { result.pop_back(); } if (!result.empty()) { - result.ReopenLastToken(); - pasting = true; + before = result.TokenAt(result.SizeInTokens() - 1); + } + } else { + result.AppendRange(text, j, 1); + } + } else if (after.IsBlank() || IsTokenPasting(after)) { + // drop it + } else { // pasting before ## after + bool doPaste{false}; + char last{before->back()}; + char first{after.front()}; + // Apply basic sanity checking to pasting so avoid constructing a bogus + // token that might cause macro replacement to fail, like "macro(". + if (IsLegalInIdentifier(last) && IsLegalInIdentifier(first)) { + doPaste = true; + } else if (IsDecimalDigit(first) && + (last == '.' || last == '+' || last == '-')) { + doPaste = true; // 1. ## 0, - ## 1 + } else if (before->size() == 1 && after.size() == 1) { + if (first == last && + (last == '<' || last == '>' || last == '*' || last == '/' || + last == '=' || last == '&' || last == '|' || last == ':')) { + // Fortran **, //, ==, :: + // C <<, >>, &&, || for use in #if expressions + doPaste = true; + } else if (first == '=' && (last == '!' || last == '/')) { + doPaste = true; // != and /= } } - } else if (pasting && text.TokenAt(j).IsBlank()) { - } else { + if (doPaste) { + result.ReopenLastToken(); + } result.AppendRange(text, j, 1); - pasting = false; + before.reset(); } } return result; diff --git a/flang/lib/Parser/unparse.cpp b/flang/lib/Parser/unparse.cpp index b66d756bdbf2..fbe89c668fc1 100644 --- a/flang/lib/Parser/unparse.cpp +++ b/flang/lib/Parser/unparse.cpp @@ -778,7 +778,7 @@ public: } void Unparse(const SubstringInquiry &x) { Walk(x.v); - Put(x.source.end()[-1] == 'n' ? "%LEN" : "%KIND"); + Put(x.source.back() == 'n' ? "%LEN" : "%KIND"); } void Unparse(const SubstringRange &x) { // R910 Walk(x.t, ":"); diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp index f4af738284ed..53ec3827893d 100644 --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -1269,7 +1269,7 @@ MaybeExpr ExpressionAnalyzer::Analyze( MaybeExpr ExpressionAnalyzer::Analyze(const parser::SubstringInquiry &x) { if (MaybeExpr substring{Analyze(x.v)}) { CHECK(x.source.size() >= 8); - int nameLen{x.source.end()[-1] == 'n' ? 3 /*LEN*/ : 4 /*KIND*/}; + int nameLen{x.source.back() == 'n' ? 3 /*LEN*/ : 4 /*KIND*/}; parser::CharBlock name{ x.source.end() - nameLen, static_cast(nameLen)}; CHECK(name == "len" || name == "kind"); diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp index d0336c9cb661..96faa5fd954c 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -2139,7 +2139,7 @@ bool ImplicitRules::isImplicitNoneExternal() const { const DeclTypeSpec *ImplicitRules::GetType( SourceName name, bool respectImplicitNoneType) const { - char ch{name.begin()[0]}; + char ch{name.front()}; if (isImplicitNoneType_ && respectImplicitNoneType) { return nullptr; } else if (auto it{map_.find(ch)}; it != map_.end()) { diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp index d27d250b3f11..5e5b43f26c79 100644 --- a/flang/lib/Semantics/tools.cpp +++ b/flang/lib/Semantics/tools.cpp @@ -1740,7 +1740,7 @@ std::forward_list GetOperatorNames( std::forward_list GetAllNames( const SemanticsContext &context, const SourceName &name) { std::string str{name.ToString()}; - if (!name.empty() && name.end()[-1] == ')' && + if (!name.empty() && name.back() == ')' && name.ToString().rfind("operator(", 0) == 0) { for (int i{0}; i != common::LogicalOperator_enumSize; ++i) { auto names{GetOperatorNames(context, common::LogicalOperator{i})}; diff --git a/flang/test/Preprocessing/bug1126.F90 b/flang/test/Preprocessing/bug1126.F90 new file mode 100644 index 000000000000..ae5bb5633581 --- /dev/null +++ b/flang/test/Preprocessing/bug1126.F90 @@ -0,0 +1,20 @@ +! RUN: %flang -E %s 2>&1 | FileCheck %s +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#define PREFIX(x) prefix ## x +#define NAME(x) PREFIX(foo ## x) +#define AUGMENT(x) NAME(x ## suffix) + +! CHECK: subroutine prefixfoosuffix() +! CHECK: print *, "prefixfoosuffix" +! CHECK: end subroutine prefixfoosuffix +subroutine AUGMENT()() + print *, TOSTRING(AUGMENT()) +end subroutine AUGMENT() + +! CHECK: subroutine prefixfoobarsuffix() +! CHECK: print *, "prefixfoobarsuffix" +! CHECK: end subroutine prefixfoobarsuffix +subroutine AUGMENT(bar)() + print *, TOSTRING(AUGMENT(bar)) +end subroutine AUGMENT(bar)