[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

Erich Reitz via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 8 13:17:35 PDT 2024


================
@@ -796,6 +796,44 @@ TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
             format("namespace {}\n   /* Test */    #define A"));
 }
 
+TEST_F(FormatTestComments, DeIdentsCommentBeforeIfdefAfterBracelessIf) {
+  verifyFormat("void f() {\n"
+               "  if (true)\n"
+               "    int i;\n"
+               "  /* comment */\n"
+               "#ifdef A\n"
+               "  int j;\n"
+               "#endif\n"
+               "}",
+               "void f() {\n"
+               "  if (true)\n"
+               "    int i;\n"
+               "    /* comment */\n"
+               "#ifdef A\n"
+               "  int j;\n"
+               "#endif\n"
+               "}");
+
+  verifyFormat("void f() {\n"
+               "  if (true)\n"
+               "    int i;\n"
+               "  /* comment */\n"
----------------
Erich-Reitz wrote:

Okay, I don't think that is the existing behavior. Using `clang-format-18`
```
void f() {
  if (foo) 
    a = 3;
    /* comment */

  int b = 4; 
}
```
is formatted to
```
void f() {
  if (foo)
    a = 3;
  /* comment */

  int b = 4;
}
```

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


More information about the cfe-commits mailing list