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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 07:53:42 PST 2024


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

>From 02d2d44046a4cf6ca8c5bb1897b6677c01d81974 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 13 Feb 2024 00:03:19 -0800
Subject: [PATCH] [clang-format] Always add a space after #if and #elif

Fixes #81569.
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 ++
 clang/unittests/Format/FormatTest.cpp | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b9a000faae7cf7..08a49bc17f13f3 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->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;



More information about the cfe-commits mailing list