[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 00:07:39 PST 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/81578
Fixes #81569.
>From 2292ccb78839a90a15215b11c473fcbfc47aa00c 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 insert a space between #if and l_paren
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..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;
More information about the cfe-commits
mailing list