Skip to content

[Flang][Preprocessor] Avoid creating an empty token when a kind suffix is torn by a pasting operator #139795

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
8 changes: 8 additions & 0 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
if (*at_ != '_') {
return false;
}
auto underscore = *at_;
TokenSequence withUnderscore, separate;
EmitChar(withUnderscore, '_');
EmitCharAndAdvance(separate, '_');
Expand All @@ -951,6 +952,13 @@ bool Prescanner::HandleKindSuffix(TokenSequence &tokens) {
}
withUnderscore.CloseToken();
separate.CloseToken();
// If we only saw "_" and nothing else, we have handled enough but we do not
// want to close the token here, or we will generate an extra token of length
// zero.
if (separate.SizeInTokens() == 1) {
EmitChar(tokens, underscore);
return true;
}
tokens.CloseToken();
if (separate.SizeInTokens() == 2 &&
preprocessor_.IsNameDefined(separate.TokenAt(1)) &&
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Preprocessing/torn-token-pasting-1.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: IF(10>HUGE(1_4).OR.10<-HUGE(1_4)) CALL foo()
#define CHECKSAFEINT(x,k) IF(x>HUGE(1_ ## k).OR.x<-HUGE(1_##k)) CALL foo()

program main
implicit none

CHECKSAFEINT(10, 4)
end program main
Loading