Issue 242214
Summary [clang-format] Output alternates between two layouts on every run when a trailing block comment contains tabs
Labels clang-format
Assignees
Reporter tieo
    With a trailing block comment that contains tab characters, clang-format never reaches a fixed point: every run switches one line between two layouts, so run 3 gives the output of run 1 again.

Create the input and the configuration (the input uses tabs for indentation and two tabs before the last `*/`):

```sh
printf 'int x(struct x *xx)\n{\n\tif (1) {\n\t\tif ((xx == NULL)\n\t\t\t|| (xx->xxxxx.xxxxxx_xxxxx_x\n\t\t\t\t== NULL) /*13 xxxxxxxx xxxxxxxx xxxxxxxxx xxxxx x (1= xxxxxxxx, 2= xxxxx xxxxxxxx) */\n\t\t\t|| (xx->xxxxx.xxxxxx_xxxxx_x\n\t\t\t\t== NULL) /*14 xxxxxxxx xxxxxxxx xxxxxxxxx xxxxx x (1= xxxxxxxx, 2= xxxxx xxxxxxxx) */\n\t\t\t|| (xx->xxxxx.xxxxx_xxxxx_x\n\t\t\t\t== NULL) /*17 xxxxxxxx xxxxxxxx xxxxxxxx xxxxx x (1= xxxxxxxx, 2= xxxxx xxxxxxxx) */\n\t\t\t|| (xx->xxxxx.xxxxx_xxxxx_x\n\t\t\t\t== NULL) /*18 xxxxxxxx xxxxxxxx xxxxxxxx xxxxx x (1= xxxxxxxx, 2= xxxxx xxxxxxxx) */\n\t\t\t|| (xx->xxxxx.xxxx_xxxx\n\t\t\t\t== NULL) /*35 xxxxxxxxxx xxxxxxxxxxxxxxxxx: xxxxxxxxxxxxxx 1=xxxxx, 2=xxxxxxx, 0/3=xxxxxxxxx */\n\t\t\t|| (xx->xx_xxxxxxx.xxx.xxxxxxxxxx_xxx_xxx\n\t\t\t\t== NULL) /* 16 xxxxxxxxxx xxxxxxxxxxx (xxxxxxxxx xxxxxxxxx)\t\t*/\n\t\t) {\n\t\t\treturn 1;\n\t\t}\n\t}\n\treturn 0;\n}\n' > repro.c
printf 'ColumnLimit: 120\nIndentWidth: 4\nTabWidth: 4\nBreakBeforeBinaryOperators: NonAssignment\nReflowComments: Never\n' > .clang-format
```

Then format three times:

```sh
clang-format repro.c > run1.c
clang-format run1.c > run2.c
clang-format run2.c > run3.c
cmp run1.c run3.c   # identical
diff -u run1.c run2.c
```

Run 1 joins the last operand onto one line, run 2 breaks it before `== NULL` again, run 3 joins it again:

```diff
--- after-run-1.c
+++ after-run-2.c
@@ -11,7 +11,8 @@
                 == NULL) /*18 xxxxxxxx xxxxxxxx xxxxxxxx xxxxx x (1= xxxxxxxx, 2= xxxxx xxxxxxxx) */
             || (xx->xxxxx.xxxx_xxxx
 == NULL) /*35 xxxxxxxxxx xxxxxxxxxxxxxxxxx: xxxxxxxxxxxxxx 1=xxxxx, 2=xxxxxxx, 0/3=xxxxxxxxx */
-            || (xx->xx_xxxxxxx.xxx.xxxxxxxxxx_xxx_xxx == NULL) /* 16 xxxxxxxxxx xxxxxxxxxxx (xxxxxxxxx xxxxxxxxx)		*/
+            || (xx->xx_xxxxxxx.xxx.xxxxxxxxxx_xxx_xxx
+                == NULL) /* 16 xxxxxxxxxx xxxxxxxxxxx (xxxxxxxxx xxxxxxxxx)		*/
         ) {
 return 1;
         }
```

The joined line is 120 columns wide when the two tabs inside the comment are counted with `TabWidth: 4` from their position in the line. The line ends exactly at the `ColumnLimit` in one count and beyond it in the other, so the two runs probably disagree on the width of those tabs.

Replacing the two tabs before `*/` with two spaces makes the output stable after one run. `UseTab` does not matter: the input above is indented with tabs, but the output (with the default `UseTab: Never`) still alternates.

Seen with clang-format 21.1.8, 22.1.8, 23.1.0 and 23.1.3 (the PyPI wheels, Linux x86_64).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to