[clang] [clang-format] Always insert a space between #if and l_paren (PR #81578)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 00:08:08 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

<details>
<summary>Changes</summary>

Fixes #<!-- -->81569.

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


2 Files Affected:

- (modified) clang/lib/Format/TokenAnnotator.cpp (+2) 
- (modified) clang/unittests/Format/FormatTest.cpp (+4) 


``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b9a000faae7cf7..981147da18ca29 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1856,6 +1856,8 @@ class AnnotatingParser {
     case tok::pp_elif:
       Contexts.back().IsExpression = true;
       next();
+      if (CurrentToken && CurrentToken->is(tok::l_paren))
+        CurrentToken->SpacesRequiredBefore = true;
       parseLine();
       break;
     default:
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 13937a15fdaee2..79cd521b6a9998 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16491,6 +16491,10 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("T A::operator()();", NoSpace);
   verifyFormat("X A::operator++(T);", NoSpace);
   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
+  verifyFormat("#if (foo || bar) && baz\n"
+               "#elif ((a || b) && c) || d\n"
+               "#endif",
+               NoSpace);
 
   FormatStyle Space = getLLVMStyle();
   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;

``````````

</details>


https://github.com/llvm/llvm-project/pull/81578


More information about the cfe-commits mailing list