Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Always add a space after #if and #elif #81578

Merged
merged 1 commit into from
Feb 14, 2024
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Feb 13, 2024

Fixes #81569.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 13, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

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

@owenca owenca changed the title [clang-format] Always insert a space between #if and l_paren [clang-format] Always add a space after #if and #elif Feb 13, 2024
@owenca owenca merged commit eafe98f into llvm:main Feb 14, 2024
5 checks passed
@owenca owenca deleted the 81569 branch February 14, 2024 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format produces #if(abc && def) || ghi
3 participants