[flang-commits] [flang] 943c704 - [flang] Support #if defined when no definitions (#156080)

via flang-commits flang-commits at lists.llvm.org
Fri Aug 29 11:58:02 PDT 2025


Author: Peter Klausler
Date: 2025-08-29T11:57:59-07:00
New Revision: 943c7042c622c331784f7eadc35e732ae62aef52

URL: https://github.com/llvm/llvm-project/commit/943c7042c622c331784f7eadc35e732ae62aef52
DIFF: https://github.com/llvm/llvm-project/commit/943c7042c622c331784f7eadc35e732ae62aef52.diff

LOG: [flang] Support #if defined when no definitions (#156080)

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

Added: 
    flang/test/Preprocessing/no-pp-if.f90

Modified: 
    flang/lib/Parser/preprocessor.cpp

Removed: 
    


################################################################################
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


        


More information about the flang-commits mailing list