Skip to content

[flang] Don't create bogus tokens from token pasting (##) #147596

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flang/include/flang/Parser/char-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CharBlock {
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_);
Expand Down
43 changes: 35 additions & 8 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,50 @@ static TokenSequence TokenPasting(TokenSequence &&text) {
}
TokenSequence result;
std::size_t tokens{text.SizeInTokens()};
bool pasting{false};
std::optional<CharBlock> 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 /=
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there tests in the test suite that exercise these sanity checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, all of the token pasting in the test suite that I know of applies to identifiers.

if (doPaste) {
result.ReopenLastToken();
}
} else if (pasting && text.TokenAt(j).IsBlank()) {
} else {
result.AppendRange(text, j, 1);
pasting = false;
before.reset();
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ class UnparseVisitor {
}
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, ":");
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(nameLen)};
CHECK(name == "len" || name == "kind");
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Semantics/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ std::forward_list<std::string> GetOperatorNames(
std::forward_list<std::string> 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})};
Expand Down
20 changes: 20 additions & 0 deletions flang/test/Preprocessing/bug1126.F90
Original file line number Diff line number Diff line change
@@ -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)