Skip to content

[flang][preprocessor] Finesse disabling of function-like macros #71589

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

Merged
merged 1 commit into from
Nov 13, 2023
Merged
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
4 changes: 2 additions & 2 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
(n + 1 == argStart.size() ? k : argStart[n + 1] - 1) - at};
args.emplace_back(TokenSequence(input, at, count));
}
TokenSequence applied{def->Apply(args, prescanner)};
def->set_isDisabled(true);
TokenSequence replaced{
ReplaceMacros(def->Apply(args, prescanner), prescanner)};
TokenSequence replaced{ReplaceMacros(std::move(applied), prescanner)};
def->set_isDisabled(false);
if (!replaced.empty()) {
ProvenanceRange from{def->replacement().GetProvenanceRange()};
Expand Down
14 changes: 9 additions & 5 deletions flang/lib/Semantics/check-call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,11 +1274,15 @@ static void CheckAssociated(evaluate::ActualArguments &arguments,
return;
}
if (const auto &targetArg{arguments[1]}) {
// The standard requires that the POINTER= argument be a valid LHS for
// a pointer assignment when the TARGET= argument is present. This,
// perhaps unintentionally, excludes function results, including NULL(),
// from being used there, as well as INTENT(IN) dummy pointers.
// Allow this usage as a benign extension with a portability warning.
// The standard requires that the TARGET= argument, when present,
// be a valid RHS for a pointer assignment that has the POINTER=
// argument as its LHS. Some popular compilers misinterpret this
// requirement more strongly than necessary, and actually validate
// the POINTER= argument as if it were serving as the LHS of a pointer
// assignment. This, perhaps unintentionally, excludes function
// results, including NULL(), from being used there, as well as
// INTENT(IN) dummy pointers. Detect these conditions and emit
// portability warnings.
if (!evaluate::ExtractDataRef(*pointerExpr) &&
!evaluate::IsProcedurePointer(*pointerExpr)) {
context.messages().Say(pointerArg->sourceLocation(),
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Preprocessing/disable-expansion.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
! RUN: %flang -E %s | FileCheck %s
#define KWM a
#define FLM(x) b FLM2(x) KWM c
#define FLM2(x) d FLM(x) e
! CHECK: a
KWM
! CHECK: b d FLM(y) e a c
FLM(y)
! CHECK: b d FLM(a) e a c
FLM(KWM)
! CHECK: b d FLM(b d FLM(y) e a c) e a c
FLM(FLM(y))
! CHECK: b d FLM(b d FLM(a) e a c) e a c
FLM(FLM(KWM))