Skip to content

clang-format 18 idempotent regression with #if within struct #86539

Closed
@JohnC32

Description

@JohnC32

Running clang-format 18 twice produces different results when we have #if's within a struct.
Running with clang-format 17.0.5 shows no difference on repeated invocations.

Given idempotent_regression.cpp

extern const char** foo();
extern int bar();
const char** foo() {
    if (bar()) {
        static const char *a_long_named_identifier[] = {
            /* abc comment */ "abc",
#if FOO
            /* a comment describing what this is and so on */ "xyz",
#endif
            /* last */ "last"
        };
        return a_long_named_identifier;
    } else {
        return nullptr;
    }
}

using _clang-format:

---
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100
...

clang-format 18.1.1 yields the following differences on successive runs. Notice that the curly brace
moves up and down:

/path/to/llvm-18.1.1/bin/clang-format -style=file idempotent_regression.cpp >out.18.1.1.a.cpp 2>&1
/path/to/llvm-18.1.1/bin/clang-format -style=file out.18.1.1.a.cpp >out.18.1.1.b.cpp 2>&1
/path/to/llvm-18.1.1/bin/clang-format -style=file out.18.1.1.b.cpp >out.18.1.1.c.cpp 2>&1
diff -u out.18.1.1.a.cpp out.18.1.1.b.cpp
--- out.18.1.1.a.cpp	2024-03-25 13:18:52.544355938 -0400
+++ out.18.1.1.b.cpp	2024-03-25 13:18:52.565356089 -0400
@@ -7,7 +7,8 @@
 #if FOO
             /* a comment describing what this is and so on */ "xyz",
 #endif
-            /* last */ "last"};
+            /* last */ "last"
+        };
         return a_long_named_identifier;
     } else {
         return nullptr;

diff -u out.18.1.1.b.cpp out.18.1.1.c.cpp
--- out.18.1.1.b.cpp	2024-03-25 13:18:52.565356089 -0400
+++ out.18.1.1.c.cpp	2024-03-25 13:18:52.586356241 -0400
@@ -7,8 +7,7 @@
 #if FOO
             /* a comment describing what this is and so on */ "xyz",
 #endif
-            /* last */ "last"
-        };
+            /* last */ "last"};
         return a_long_named_identifier;
     } else {
         return nullptr;

diff -u out.18.1.1.a.cpp out.18.1.1.c.cpp

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions