Skip to content

Conversation

@klausler
Copy link
Contributor

The "defined" operator isn't working in #if/#elif expressions when there are no defined macros. Fix.

The "defined" operator isn't working in #if/#elif expressions
when there are no defined macros.  Fix.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Aug 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2025

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

The "defined" operator isn't working in #if/#elif expressions when there are no defined macros. Fix.


Full diff: https://github.com/llvm/llvm-project/pull/156080.diff

2 Files Affected:

  • (modified) flang/lib/Parser/preprocessor.cpp (+1-1)
  • (added) flang/test/Preprocessing/no-pp-if.f90 (+10)
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<TokenSequence> Preprocessor::MacroReplacement(
     const TokenSequence &input, Prescanner &prescanner,
     std::optional<std::size_t> *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

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@klausler klausler merged commit 943c704 into llvm:main Aug 29, 2025
12 checks passed
@klausler klausler deleted the ifdef branch August 29, 2025 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:parser flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants