From 850d42fb145c636a3b56a7616c3e3c5c188c1916 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Tue, 5 Nov 2024 13:18:27 -0800 Subject: [flang] Handle "defined" in macro expansions (#114844) The preprocessor implements "defined(X)" and "defined X" in if/elif directive expressions in such a way that they only work at the top level, not when they appear in macro expansions. Fix that, which is a little tricky due to the need to detect the "defined" keyword before applying any macro expansion to its argument, and add a bunch of tests. Fixes https://github.com/llvm/llvm-project/issues/114064. --- flang/lib/Parser/token-sequence.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'flang/lib/Parser/token-sequence.cpp') diff --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp index c73bca1..cdbe89b 100644 --- a/flang/lib/Parser/token-sequence.cpp +++ b/flang/lib/Parser/token-sequence.cpp @@ -61,6 +61,16 @@ std::size_t TokenSequence::SkipBlanks(std::size_t at) const { return tokens; // even if at > tokens } +std::optional TokenSequence::SkipBlanksBackwards( + std::size_t at) const { + while (at-- > 0) { + if (!TokenAt(at).IsBlank()) { + return at; + } + } + return std::nullopt; +} + // C-style /*comments*/ are removed from preprocessing directive // token sequences by the prescanner, but not C++ or Fortran // free-form line-ending comments (//... and !...) because -- cgit v1.1