From fb0cbec3c13ca03e4f96a0f9bde9b89e7438c392 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Fri, 29 Aug 2025 11:37:34 -0700 Subject: [PATCH] [flang] Support #if defined when no definitions The "defined" operator isn't working in #if/#elif expressions when there are no defined macros. Fix. --- flang/lib/Parser/preprocessor.cpp | 2 +- flang/test/Preprocessing/no-pp-if.f90 | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 flang/test/Preprocessing/no-pp-if.f90 diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp index ae14e2d46020f..9176b4db3408a 100644 --- a/flang/lib/Parser/preprocessor.cpp +++ b/flang/lib/Parser/preprocessor.cpp @@ -414,7 +414,7 @@ std::optional Preprocessor::MacroReplacement( const TokenSequence &input, Prescanner &prescanner, std::optional *partialFunctionLikeMacro, bool inIfExpression) { // Do quick scan for any use of a defined name. - if (definitions_.empty()) { + if (!inIfExpression && definitions_.empty()) { return std::nullopt; } std::size_t tokens{input.SizeInTokens()}; diff --git a/flang/test/Preprocessing/no-pp-if.f90 b/flang/test/Preprocessing/no-pp-if.f90 new file mode 100644 index 0000000000000..3e49df3deb251 --- /dev/null +++ b/flang/test/Preprocessing/no-pp-if.f90 @@ -0,0 +1,10 @@ +!RUN: %flang -fc1 -fdebug-unparse %s 2>&1 | FileCheck %s +!CHECK-NOT: ERROR STOP +!CHECK: CONTINUE +#if defined UNDEFINED +error stop +#endif +#if !defined UNDEFINED +continue +#endif +end