[clang] [clang-format] Don't align comments over scopes (PR #68743)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 24 03:45:52 PDT 2023
================
@@ -3191,20 +3198,167 @@ TEST_F(FormatTestComments, DontAlignNamespaceComments) {
"}\n"
"// Comment";
-#if 0
- // FIXME: The following comment is aligned with the namespace comment.
verifyFormat("namespace A {\n"
" int Foo;\n"
" int Bar;\n"
"} // namespace A\n"
- " // Comment",
+ "// Comment",
Input, Style);
-#endif
Style.FixNamespaceComments = false;
verifyFormat(Input, Style);
}
+TEST_F(FormatTestComments, DontAlignOverScope) {
+ verifyFormat("if (foo) {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("if (foo) {\n"
+ " // something\n"
+ "} else {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("if (foo) {\n"
+ " // something\n"
+ "} else if (foo) {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("while (foo) {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("for (;;) {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("do {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} while (foo); // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("do\n"
+ " int aLongVariable; // with comment\n"
+ "while (foo); // not aigned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("do\n"
+ " int aLongVariable; // with comment\n"
+ "/**/ while (foo); // not aigned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("switch (foo) {\n"
+ "case 7: {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // case not aligned\n"
+ "} // switch also not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("switch (foo) {\n"
+ "default: {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "} // case not aligned\n"
+ "} // switch also not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("class C {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}; // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("struct S {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}; // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("union U {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}; // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("enum E {\n"
+ " aLongVariable, // with comment\n"
+ " f // aligned\n"
+ "}; // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group");
+
+ verifyFormat("void foo() {\n"
+ " {\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ " } // not aligned\n"
+ " int bar; // new align\n"
+ " int foobar; // group\n"
+ "}");
+
+ verifyFormat("auto longLambda = [] { // comment\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}; // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group\n"
+ "auto shortLambda = [] { return 5; }; // aligned");
+
+ verifyFormat("auto longLambdaResult = [] { // comment\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}(); // not aligned\n"
+ "int bar; // new align\n"
+ "int foobar; // group\n"
+ "auto shortLambda = [] { return 5; }(); // aligned");
+
+ verifyFormat(
+ "auto longLambdaResult = [](auto I, auto J) { // comment\n"
+ " int aLongVariable; // with comment\n"
+ " int f; // aligned\n"
+ "}(\"Input\", 5); // not aligned\n"
----------------
HazardyKnusperkeks wrote:
Do we want this?
I have to say, I was surprised this worked, because I did not think of the case. And I have no opinion on that.
https://github.com/llvm/llvm-project/pull/68743
More information about the cfe-commits
mailing list