[clang] [clang-format] Don't always break before << between string literals (PR #92214)

2024-05-24 Thread Owen Pan via cfe-commits


@@ -10539,6 +10539,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
   "  bbb);");
 }
 
+TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
+  verifyFormat("QStringList() << \"foo\" << \"bar\";");

owenca wrote:

No, because the single-argument version of `verifyFormat` is a more stringent 
test than `verifyNoChange` as the former also tests the input with all optional 
whitespace characters between tokens removed.

https://github.com/llvm/llvm-project/pull/92214
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-24 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,247 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");

owenca wrote:

Is the unquoted `a:` in assembly code invalid? If not, please add your test 
instead of editing the existing one.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-24 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,247 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("asm{\n"
+"\"a\":\n"
+": x\n"
+":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__{\n"
+"\"a\":\n"
+": x\n"
+":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm (\n"
+"\"a_label:\"\n"
+": x\n"
+":\n"
+": y);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm (\n"
+"\"a_label:\"\n"
+": x\n"
+":\n"
+ 

[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-24 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-24 Thread Owen Pan via cfe-commits

https://github.com/owenca commented:

It seems that you used 5 different assembly snippets and repeated each 3-6 
times. Instead, we can have something like the following:
```
asm{Snippet 1};

__asm(Snippet 2);

__asm__(Snippet 3);

asm volatile (Snippet 4);

__asm volatile (Snippet 5);
```
Also, no space between `tok::asm` (i.e. `asm`, `__asm`, or `__asm__`) and `(`.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,91 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a_label:\"\n"
+"\"a b c(%%x)\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[8], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"

owenca wrote:

```suggestion
  Tokens = annotate("__asm(\n"
```

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,91 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"

owenca wrote:

Ditto.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,91 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"

owenca wrote:

Remove `volatile` to have a little more coverage.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1426,12 +1428,9 @@ class AnnotatingParser {
 // the colon are passed as macro arguments.
 Tok->setType(TT_ObjCMethodExpr);
   } else if (Contexts.back().ContextKind == tok::l_paren &&
- !Line.InPragmaDirective) {
-if (Style.isTableGen() && Contexts.back().IsTableGenDAGArg) {
-  Tok->setType(TT_TableGenDAGArgListColon);
-  break;
-}
-Tok->setType(TT_InlineASMColon);
+ !Line.InPragmaDirective && Style.isTableGen() &&
+ Contexts.back().IsTableGenDAGArg) {
+Tok->setType(TT_TableGenDAGArgListColon);

owenca wrote:

I was thinking that this entire block could be deleted because 
`TT_TableGenDAGArgListColon` was already correctly set by now.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1488,12 +1488,91 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
- "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ "\"a\":\n"
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a\":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("asm volatile (\n"
+"\"a_label:\"\n"
+":\n"
+": x\n"
+":);");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
+
+  Tokens = annotate("__asm__ volatile (\n"

owenca wrote:

Use `__asm` instead.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1489,11 +1489,25 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
  "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ ": x\n"
+ ":};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
   EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
-  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+  Tokens = annotate("__asm__ volatile (\n"
+"a:\n"
+": x\n"
+":);");

owenca wrote:

Same question here. Can we use an example from 
https://en.cppreference.com/w/c/language/asm instead? Also, please add a test 
for `asm(...)`.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1489,11 +1489,25 @@ TEST_F(TokenAnnotatorTest, 
RequiresDoesNotChangeParsingOfTheRest) {
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   auto Tokens = annotate("__asm{\n"
  "a:\n"
- "};");
-  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ ": x\n"
+ ":};");

owenca wrote:

Is this real/valid assembly code?

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)

2024-05-23 Thread Owen Pan via cfe-commits


@@ -1358,6 +1358,8 @@ class AnnotatingParser {
   Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
   Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
 Tok->setType(TT_ModulePartitionColon);
+  } else if (Line.First->is(tok::kw_asm)) {
+Tok->setType(TT_InlineASMColon);

owenca wrote:

Does the `else if` block (lines 1430-1436) at the end of this `case tok::colon` 
become redundant now? It should be removed if yes.

https://github.com/llvm/llvm-project/pull/92617
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add space after a word token (PR #92741)

2024-05-21 Thread Owen Pan via cfe-commits

owenca wrote:

See #92880.

https://github.com/llvm/llvm-project/pull/92741
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Correctly annotate C++ alternative operators in C (PR #92880)

2024-05-21 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/92880
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Correctly annotate C++ alternative operators in C (PR #92880)

2024-05-21 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/92880

PR #90161 uncovered a bug that annotates C++ xor as UnaryOperator if followed 
by a binary operator. This patch fixes that and all other C++ alternative 
operator keywords when followed by a binary operator in C.

Fixes #92688.

>From 6d5fc044d8b221ea0643e1df0d511e693b500f07 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Tue, 21 May 2024 01:11:20 -0700
Subject: [PATCH] [clang-format] Correctly annotate C++ alternative operators
 in C

PR #90161 uncovered a bug that annotates C++ xor as UnaryOperator if
followed by a binary operator. This patch fixes that and all other C++
alternative operator keywords when followed by a binary operator in C.

Fixes #92688.
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 21 ++--
 clang/unittests/Format/FormatTest.cpp |  2 +
 clang/unittests/Format/TokenAnnotatorTest.cpp | 54 +++
 3 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 3dd10f6bd2b31..b6f7567adc140 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1410,6 +1410,13 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
   }
 }
 
+static bool isAltOperator(const FormatToken ) {
+  return isalpha(Tok.TokenText[0]) &&
+ Tok.isOneOf(tok::ampamp, tok::ampequal, tok::amp, tok::pipe,
+ tok::tilde, tok::exclaim, tok::exclaimequal, 
tok::pipepipe,
+ tok::pipeequal, tok::caret, tok::caretequal);
+}
+
 void UnwrappedLineParser::parseStructuralElement(
 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
@@ -1689,9 +1696,15 @@ void UnwrappedLineParser::parseStructuralElement(
 break;
   }
 
-  const bool InRequiresExpression =
-  OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
-  do {
+  for (const bool InRequiresExpression =
+   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
+   !eof();) {
+if (IsCpp && isAltOperator(*FormatTok)) {
+  if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
+  Next && Next->isBinaryOperator()) {
+FormatTok->Tok.setKind(tok::identifier);
+  }
+}
 const FormatToken *Previous = FormatTok->Previous;
 switch (FormatTok->Tok.getKind()) {
 case tok::at:
@@ -2122,7 +2135,7 @@ void UnwrappedLineParser::parseStructuralElement(
   nextToken();
   break;
 }
-  } while (!eof());
+  }
 }
 
 bool UnwrappedLineParser::tryToParsePropertyAccessor() {
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 2f0c0f0266774..a9df994189f00 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17340,12 +17340,14 @@ TEST_F(FormatTest, 
ConfigurableSpaceBeforeAssignmentOperators) {
   verifyFormat("int a = 5;");
   verifyFormat("a += 42;");
   verifyFormat("a or_eq 8;");
+  verifyFormat("xor = foo;");
 
   FormatStyle Spaces = getLLVMStyle();
   Spaces.SpaceBeforeAssignmentOperators = false;
   verifyFormat("int a= 5;", Spaces);
   verifyFormat("a+= 42;", Spaces);
   verifyFormat("a or_eq 8;", Spaces);
+  verifyFormat("xor= foo;", Spaces);
 }
 
 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 45c1554308c9b..6ea9c4a241dc5 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3015,6 +3015,60 @@ TEST_F(TokenAnnotatorTest, SwitchExpression) {
   EXPECT_TOKEN(Tokens[20], tok::arrow, TT_CaseLabelArrow);
 }
 
+TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
+  auto Tokens = annotate("a = b and c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
+
+  Tokens = annotate("a = b and_eq c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::ampequal, TT_BinaryOperator);
+
+  Tokens = annotate("a = b bitand c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator);
+
+  Tokens = annotate("a = b bitor c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::pipe, TT_BinaryOperator);
+
+  Tokens = annotate("a = b compl c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::tilde, TT_UnaryOperator);
+
+  Tokens = annotate("a = b not c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::exclaim, TT_UnaryOperator);
+
+  Tokens = annotate("a = b not_eq c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::exclaimequal, TT_BinaryOperator);
+
+  Tokens = annotate("a = b or c;");
+  ASSERT_EQ(Tokens.size(), 7u);
+  EXPECT_TOKEN(Tokens[3], tok::pipepipe, TT_BinaryOperator);
+
+  Tokens = annotate("a = b or_eq 

[clang] [clang-format] Fix a bug in formatting goto labels in macros (PR #92494)

2024-05-21 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/92494
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add space after a word token (PR #92741)

2024-05-21 Thread Owen Pan via cfe-commits


@@ -5280,7 +5280,8 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine ,
 // handled.
 if (Left.is(tok::amp) && Right.is(tok::r_square))
   return Style.SpacesInSquareBrackets;
-return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
+return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
+   Right.is(TT_BinaryOperator);

owenca wrote:

This line was deleted in #90161. Adding it back will hide the uncovered bug and 
only fix #92688 superficially IMO. For example, 
`SpaceBeforeAssignmentOperators` will have no effect.

https://github.com/llvm/llvm-project/pull/92741
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add space after a word token (PR #92741)

2024-05-21 Thread Owen Pan via cfe-commits

owenca wrote:

> Since I am by no means an expert on Clang, a few questions arose
> 
> * As Clang formats the code correctly (no code changes necessary) when 
> instead of `xor` a different function name e.g. `xooor` is used, I wonder why 
> `xor` is tokenized as Unary operator in the first place in C?
> * Is C and C++ using the same tokenizations?

clang-format formats all C code as C++. Since `xor` is a C++ keyword, it's 
lexed as `tok::caret`, which is then erroneously annotated as 
`TT_UnaryOperator`. This bug was uncovered by #90161.

> * How to properly distinguish between the C and C++ language in Clang Format?

clang-format can't do it properly. @mydeveloperday, @HazardyKnusperkeks, and 
@rymiel may know more about why we didn't add `LK_C` to the 
[`Language`](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#language) 
option.

https://github.com/llvm/llvm-project/pull/92741
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 1c58208 - [clang-format][NFC] Remove redundnat llvm::, clang::, etc.

2024-05-20 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-20T20:32:18-07:00
New Revision: 1c58208d899285318c89e069268145c85ec33368

URL: 
https://github.com/llvm/llvm-project/commit/1c58208d899285318c89e069268145c85ec33368
DIFF: 
https://github.com/llvm/llvm-project/commit/1c58208d899285318c89e069268145c85ec33368.diff

LOG: [clang-format][NFC] Remove redundnat llvm::, clang::, etc.

Added: 


Modified: 
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/FormatTokenSource.h
clang/lib/Format/MacroExpander.cpp
clang/lib/Format/Macros.h
clang/lib/Format/SortJavaScriptImports.cpp
clang/tools/clang-format/ClangFormat.cpp
clang/unittests/Format/CleanupTest.cpp
clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
clang/unittests/Format/FormatTestBase.h
clang/unittests/Format/FormatTestCSharp.cpp
clang/unittests/Format/FormatTestJS.cpp
clang/unittests/Format/FormatTestJson.cpp
clang/unittests/Format/FormatTestProto.cpp
clang/unittests/Format/FormatTestRawStrings.cpp
clang/unittests/Format/FormatTestSelective.cpp
clang/unittests/Format/FormatTestTableGen.cpp
clang/unittests/Format/FormatTestUtils.h
clang/unittests/Format/FormatTestVerilog.cpp
clang/unittests/Format/FormatTokenSourceTest.cpp
clang/unittests/Format/MacroCallReconstructorTest.cpp
clang/unittests/Format/MacroExpanderTest.cpp
clang/unittests/Format/MatchFilePathTest.cpp
clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
clang/unittests/Format/ObjCPropertyAttributeOrderFixerTest.cpp
clang/unittests/Format/QualifierFixerTest.cpp
clang/unittests/Format/SortImportsTestJS.cpp
clang/unittests/Format/SortImportsTestJava.cpp
clang/unittests/Format/SortIncludesTest.cpp
clang/unittests/Format/TestLexer.h
clang/unittests/Format/TokenAnnotatorTest.cpp
clang/unittests/Format/UsingDeclarationsSorterTest.cpp

Removed: 




diff  --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 74893f23210cd..274b45d1bc586 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -5239,7 +5239,7 @@ tooling::Replacements sortIncludes(const FormatStyle 
, StringRef Code,
 /// Returns the replacements corresponding to applying and formatting
 /// \p Replaces on success; otheriwse, return an llvm::Error carrying
 /// llvm::StringError.
-llvm::Expected
+Expected
 formatReplacements(StringRef Code, const tooling::Replacements ,
const FormatStyle );
 
@@ -5256,7 +5256,7 @@ formatReplacements(StringRef Code, const 
tooling::Replacements ,
 /// The include manipulation is done via ``tooling::HeaderInclude``, see its
 /// documentation for more details on how include insertion points are found 
and
 /// what edits are produced.
-llvm::Expected
+Expected
 cleanupAroundReplacements(StringRef Code, const tooling::Replacements 
,
   const FormatStyle );
 
@@ -5381,11 +5381,10 @@ extern const char *DefaultFallbackStyle;
 /// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
 /// "file" and no file is found, returns ``FallbackStyle``. If no style could 
be
 /// determined, returns an Error.
-llvm::Expected getStyle(StringRef StyleName, StringRef FileName,
- StringRef FallbackStyle,
- StringRef Code = "",
- llvm::vfs::FileSystem *FS = nullptr,
- bool AllowUnknownOptions = false);
+Expected getStyle(StringRef StyleName, StringRef FileName,
+   StringRef FallbackStyle, StringRef Code = "",
+   llvm::vfs::FileSystem *FS = nullptr,
+   bool AllowUnknownOptions = false);
 
 // Guesses the language from the ``FileName`` and ``Code`` to be formatted.
 // Defaults to FormatStyle::LK_Cpp.

diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 52005a6c881f3..9cba0c2614eef 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -28,7 +28,7 @@
 
 using clang::format::FormatStyle;
 
-LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::RawStringFormat)
+LLVM_YAML_IS_SEQUENCE_VECTOR(FormatStyle::RawStringFormat)
 
 namespace llvm {
 namespace yaml {
@@ -1235,7 +1235,7 @@ std::error_code make_error_code(ParseError e) {
   return std::error_code(static_cast(e), getParseCategory());
 }
 
-inline llvm::Error make_string_error(const llvm::Twine ) {
+inline llvm::Error make_string_error(const Twine ) {
   return llvm::make_error(Message,
  llvm::inconvertibleErrorCode());
 }
@@ -2361,7 +2361,7 @@ class JavaScriptRequoter : public TokenAnalyzer {
   // FIXME: handle error. For now, print error message and skip the
   // 

[clang] 2d5e488 - [clang-format][NFC] Clean up SortIncludesTest.cpp

2024-05-19 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-19T15:19:05-07:00
New Revision: 2d5e488c98225108aebfe4aa4acfe6ec1f234a37

URL: 
https://github.com/llvm/llvm-project/commit/2d5e488c98225108aebfe4aa4acfe6ec1f234a37
DIFF: 
https://github.com/llvm/llvm-project/commit/2d5e488c98225108aebfe4aa4acfe6ec1f234a37.diff

LOG: [clang-format][NFC] Clean up SortIncludesTest.cpp

Wherever applicable, replace EXPECT_EQ with verifyFormat and std::string
with StringRef. Also, change a raw string literal to a regular one.

Added: 


Modified: 
clang/unittests/Format/SortIncludesTest.cpp

Removed: 




diff  --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 824fa0078cd03..52ba19627182b 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -53,35 +53,35 @@ class SortIncludesTest : public test::FormatTestBase {
 };
 
 TEST_F(SortIncludesTest, BasicSorting) {
-  EXPECT_EQ("#include \"a.h\"\n"
-"#include \"b.h\"\n"
-"#include \"c.h\"",
-sort("#include \"a.h\"\n"
- "#include \"c.h\"\n"
- "#include \"b.h\""));
-
-  EXPECT_EQ("// comment\n"
-"#include \n"
-"#include ",
-sort("// comment\n"
- "#include \n"
- "#include ",
- {tooling::Range(25, 1)}));
+  verifyFormat("#include \"a.h\"\n"
+   "#include \"b.h\"\n"
+   "#include \"c.h\"",
+   sort("#include \"a.h\"\n"
+"#include \"c.h\"\n"
+"#include \"b.h\""));
+
+  verifyFormat("// comment\n"
+   "#include \n"
+   "#include ",
+   sort("// comment\n"
+"#include \n"
+"#include ",
+{tooling::Range(25, 1)}));
 }
 
 TEST_F(SortIncludesTest, TrailingComments) {
-  EXPECT_EQ("#include \"a.h\"\n"
-"#include \"b.h\" /* long\n"
-"  * long\n"
-"  * comment*/\n"
-"#include \"c.h\"\n"
-"#include \"d.h\"",
-sort("#include \"a.h\"\n"
- "#include \"c.h\"\n"
- "#include \"b.h\" /* long\n"
- "  * long\n"
- "  * comment*/\n"
- "#include \"d.h\""));
+  verifyFormat("#include \"a.h\"\n"
+   "#include \"b.h\" /* long\n"
+   "  * long\n"
+   "  * comment*/\n"
+   "#include \"c.h\"\n"
+   "#include \"d.h\"",
+   sort("#include \"a.h\"\n"
+"#include \"c.h\"\n"
+"#include \"b.h\" /* long\n"
+"  * long\n"
+"  * comment*/\n"
+"#include \"d.h\""));
 }
 
 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
@@ -100,531 +100,531 @@ TEST_F(SortIncludesTest, 
SortedIncludesUsingSortPriorityAttribute) {
   {"", 8, 10, false},
   {"^\".*\\.h\"", 10, 12, false}};
-  EXPECT_EQ("#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"\n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"\n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"#include \n"
-"\n"
-"#include \n"
-"\n"
-"#include \"pathnames.h\"",
-sort("#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include \"pathnames.h\"\n"
- "#include \n"
- "#include \n"
- "#include \n"
- "#include "));
+  verifyFormat("#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "\n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "\n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "#include \n"
+   "\n"
+   

[clang] [clang-format] Fix a bug in formatting goto labels in macros (PR #92494)

2024-05-16 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/92494
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating goto labels (PR #92494)

2024-05-16 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/92494

>From d9f113101028c68465e8befe7db1d4c206daa535 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 May 2024 20:49:17 -0700
Subject: [PATCH] [clang-format] Fix a bug in formatting goto labels in macros

Fixes #92300.
---
 clang/lib/Format/UnwrappedLineParser.cpp  |  9 ++---
 clang/unittests/Format/FormatTest.cpp |  8 
 clang/unittests/Format/TokenAnnotatorTest.cpp | 13 +
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..3dd10f6bd2b31 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1189,12 +1189,6 @@ void UnwrappedLineParser::parsePPDefine() {
 return;
   }
 
-  if (FormatTok->is(tok::identifier) &&
-  Tokens->peekNextToken()->is(tok::colon)) {
-nextToken();
-nextToken();
-  }
-
   // Errors during a preprocessor directive can only affect the layout of the
   // preprocessor directive, and thus we ignore them. An alternative approach
   // would be to use the same approach we use on the file level (no
@@ -1681,7 +1675,8 @@ void UnwrappedLineParser::parseStructuralElement(
 if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
   nextToken();
-  Line->Tokens.begin()->Tok->MustBreakBefore = true;
+  if (!Line->InMacroBody || CurrentLines->size() > 1)
+Line->Tokens.begin()->Tok->MustBreakBefore = true;
   FormatTok->setFinalizedType(TT_GotoLabelColon);
   parseLabel(!Style.IndentGotoLabels);
   if (HasLabel)
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 6f57f10e12e88..2f0c0f0266774 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3124,6 +3124,7 @@ TEST_F(FormatTest, FormatsLabels) {
"g();\n"
"  }\n"
"}");
+
   FormatStyle Style = getLLVMStyle();
   Style.IndentGotoLabels = false;
   verifyFormat("void f() {\n"
@@ -3163,6 +3164,13 @@ TEST_F(FormatTest, FormatsLabels) {
"  }\n"
"}",
Style);
+
+  Style.ColumnLimit = 15;
+  verifyFormat("#define FOO   \\\n"
+   "label:\\\n"
+   "  break;",
+   Style);
+
   // The opening brace may either be on the same unwrapped line as the colon or
   // on a separate one. The formatter should recognize both.
   Style = getLLVMStyle();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index aadfa6dc0165c..83d9cc766ae5f 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2581,15 +2581,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
   auto Tokens = annotate("{ x: break; }");
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
+
   Tokens = annotate("{ case x: break; }");
   ASSERT_EQ(Tokens.size(), 8u) << Tokens;
   EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
+
   Tokens = annotate("{ x: { break; } }");
   ASSERT_EQ(Tokens.size(), 9u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
+
   Tokens = annotate("{ case x: { break; } }");
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
+
+  Tokens = annotate("#define FOO label:");
+  ASSERT_EQ(Tokens.size(), 6u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
+
+  Tokens = annotate("#define FOO \\\n"
+"label: \\\n"
+"  break;");
+  ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating goto labels (PR #92494)

2024-05-16 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/92494

Fixes #92300.

>From 530b15d68c66b0f1414329a8e190b27c2e6bf106 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 May 2024 20:49:17 -0700
Subject: [PATCH] [clang-format] Fix a regression in annotating goto labels

Fixes #92300.
---
 clang/lib/Format/UnwrappedLineParser.cpp  |  9 ++---
 clang/unittests/Format/TokenAnnotatorTest.cpp | 13 +
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..3dd10f6bd2b31 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1189,12 +1189,6 @@ void UnwrappedLineParser::parsePPDefine() {
 return;
   }
 
-  if (FormatTok->is(tok::identifier) &&
-  Tokens->peekNextToken()->is(tok::colon)) {
-nextToken();
-nextToken();
-  }
-
   // Errors during a preprocessor directive can only affect the layout of the
   // preprocessor directive, and thus we ignore them. An alternative approach
   // would be to use the same approach we use on the file level (no
@@ -1681,7 +1675,8 @@ void UnwrappedLineParser::parseStructuralElement(
 if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
   nextToken();
-  Line->Tokens.begin()->Tok->MustBreakBefore = true;
+  if (!Line->InMacroBody || CurrentLines->size() > 1)
+Line->Tokens.begin()->Tok->MustBreakBefore = true;
   FormatTok->setFinalizedType(TT_GotoLabelColon);
   parseLabel(!Style.IndentGotoLabels);
   if (HasLabel)
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index aadfa6dc0165c..d9c6e8cc1c877 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2581,15 +2581,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
   auto Tokens = annotate("{ x: break; }");
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
+
   Tokens = annotate("{ case x: break; }");
   ASSERT_EQ(Tokens.size(), 8u) << Tokens;
   EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
+
   Tokens = annotate("{ x: { break; } }");
   ASSERT_EQ(Tokens.size(), 9u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
+
   Tokens = annotate("{ case x: { break; } }");
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
+
+  Tokens = annotate("#define FOO label:");
+  ASSERT_EQ(Tokens.size(), 6u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
+
+  Tokens = annotate("#define FOO \\\n"
+"  label:\\\n"
+"  break;");
+  ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating struct braces (PR #92352)

2024-05-16 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/92352
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't always break before << between string literals (PR #92214)

2024-05-16 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/92214
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating struct braces (PR #92352)

2024-05-15 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/92352

Fixes #92350.

>From d6f338d050bba640e90da983cac34111cdbf56c2 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Wed, 15 May 2024 22:38:29 -0700
Subject: [PATCH] [clang-format] Fix a regression in annotating struct braces

Fixes #92350.
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 12 +++-
 clang/unittests/Format/TokenAnnotatorTest.cpp |  5 +
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 2236a49e4b765..b15a87327240b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4008,8 +4008,6 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   };
 
   if (FormatTok->isOneOf(tok::colon, tok::less)) {
-if (FormatTok->is(tok::colon))
-  IsDerived = true;
 int AngleNestingLevel = 0;
 do {
   if (FormatTok->is(tok::less))
@@ -4017,9 +4015,13 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   else if (FormatTok->is(tok::greater))
 --AngleNestingLevel;
 
-  if (AngleNestingLevel == 0 && FormatTok->is(tok::l_paren) &&
-  IsNonMacroIdentifier(FormatTok->Previous)) {
-break;
+  if (AngleNestingLevel == 0) {
+if (FormatTok->is(tok::colon)) {
+  IsDerived = true;
+} else if (FormatTok->is(tok::l_paren) &&
+   IsNonMacroIdentifier(FormatTok->Previous)) {
+  break;
+}
   }
   if (FormatTok->is(tok::l_brace)) {
 if (AngleNestingLevel == 0 && IsListInitialization())
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 51b475d37977e..aadfa6dc0165c 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2903,6 +2903,11 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[5], BK_Block);
   EXPECT_BRACE_KIND(Tokens[6], BK_Block);
 
+  Tokens = annotate("struct Foo : Base {};");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[7], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[8], BK_Block);
+
   Tokens = annotate("struct Foo final {};");
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   EXPECT_BRACE_KIND(Tokens[3], BK_Block);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't always break before << between string literals (PR #92214)

2024-05-15 Thread Owen Pan via cfe-commits

owenca wrote:

> This is interesting.. I like it in that its a "Leave" ... but part of me 
> would like this choice of default behind an option. As with the previous 
> changes in this area I'm uncomfortable about us changing how it behaves, but 
> would like the extra capability to choose.. I'm going to say Approve, but I'm 
> interested in your opinion about if we SHOULD have an option or not?

I don't think it warrants an option. (If an option is to be added in the 
future, the default should be `Leave`.) This is because whether to break before 
a `<<` that is between two string literals depends on what the string literals 
are. If you have code like the following in the same directory, no option value 
(other than `Leave`) would help:
```
// Don't break:
QTest::newRow("test") << "" << "" << "" << "" << "" << 1 << 1 << 1 << 1 << 1;

// Break:
OS << I->Tok->Tok.getName() << "["
   << "T=" << (unsigned)I->Tok->getType()
   << ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
   << "\"] ";
```
Like some of the comments from  https://reviews.llvm.org/D80950, I'm still of 
the opinion that the (undocumented) behavior of "always breaking" is a bug, 
even though the fix should be "leave" instead of "always merging" as done in 
d68826dfbd98.

https://github.com/llvm/llvm-project/pull/92214
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] b11a660 - [clang-format][NFC] Reformat with 18.1.5

2024-05-15 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-15T21:04:46-07:00
New Revision: b11a6607cb6522c58dfbd5f54239e7daa281368e

URL: 
https://github.com/llvm/llvm-project/commit/b11a6607cb6522c58dfbd5f54239e7daa281368e
DIFF: 
https://github.com/llvm/llvm-project/commit/b11a6607cb6522c58dfbd5f54239e7daa281368e.diff

LOG: [clang-format][NFC] Reformat with 18.1.5

Added: 


Modified: 
clang/lib/Format/UnwrappedLineParser.cpp
clang/tools/clang-format/ClangFormat.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 4f1c2c5114e93..2236a49e4b765 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -47,7 +47,8 @@ void printLine(llvm::raw_ostream , const UnwrappedLine 
,
   OS << Prefix;
   NewLine = false;
 }
-OS << I->Tok->Tok.getName() << "[" << "T=" << (unsigned)I->Tok->getType()
+OS << I->Tok->Tok.getName() << "["
+   << "T=" << (unsigned)I->Tok->getType()
<< ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
<< "\"] ";
 for (SmallVectorImpl::const_iterator

diff  --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index 01f7c6047726e..3fa5f81a35768 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -336,7 +336,8 @@ static void outputReplacementXML(StringRef Text) {
 
 static void outputReplacementsXML(const Replacements ) {
   for (const auto  : Replaces) {
-outs() << "";
 outputReplacementXML(R.getReplacementText());
 outs() << "\n";



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't always break before << between string literals (PR #92214)

2024-05-14 Thread Owen Pan via cfe-commits

owenca wrote:

See also the discussion in https://reviews.llvm.org/D80950.

https://github.com/llvm/llvm-project/pull/92214
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't always break before << between string literals (PR #92214)

2024-05-14 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/92214

Instead, leave the line wrapping as is.

Fixes #43887.
Fixes #44363.

>From 40eeb958cef55465fdcee66ab385928c1f202e50 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Tue, 14 May 2024 19:14:15 -0700
Subject: [PATCH] [clang-format] Don't always break before << between string
 literals

Instead, leave the line wrapping as is.

Fixes #43887.
Fixes #44363.
---
 clang/lib/Format/TokenAnnotator.cpp   |  7 +--
 clang/unittests/Format/FormatTest.cpp | 11 +++
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index d0aa0838423e4..7c4c76a91f2c5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5601,10 +5601,13 @@ bool TokenAnnotator::mustBreakBefore(const 
AnnotatedLine ,
 return true;
   if (Left.IsUnterminatedLiteral)
 return true;
-  if (Right.is(tok::lessless) && AfterRight && Left.is(tok::string_literal) &&
+
+  if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
+  Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
   AfterRight->is(tok::string_literal)) {
-return true;
+return Right.NewlinesBefore > 0;
   }
+
   if (Right.is(TT_RequiresClause)) {
 switch (Style.RequiresClausePosition) {
 case FormatStyle::RCPS_OwnLine:
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index e6f8e4a06515e..6f57f10e12e88 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10539,6 +10539,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
   "  bbb);");
 }
 
+TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
+  verifyFormat("QStringList() << \"foo\" << \"bar\";");
+
+  verifyNoChange("QStringList() << \"foo\"\n"
+ "  << \"bar\";");
+
+  verifyFormat("log_error(log, \"foo\" << \"bar\");",
+   "log_error(log, \"foo\"\n"
+   "   << \"bar\");");
+}
+
 TEST_F(FormatTest, UnderstandsEquals) {
   verifyFormat(
   "a =\n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 72b2c37 - [clang-format][NFC] Clean up TokenAnnotator::mustBreakBefore()

2024-05-14 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-14T20:31:53-07:00
New Revision: 72b2c37de6a4bbc2b2d2cda49293684b7cc71508

URL: 
https://github.com/llvm/llvm-project/commit/72b2c37de6a4bbc2b2d2cda49293684b7cc71508
DIFF: 
https://github.com/llvm/llvm-project/commit/72b2c37de6a4bbc2b2d2cda49293684b7cc71508.diff

LOG: [clang-format][NFC] Clean up TokenAnnotator::mustBreakBefore()

Added: 


Modified: 
clang/lib/Format/TokenAnnotator.cpp

Removed: 




diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 478cae23d3c8c..d0aa0838423e4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5405,6 +5405,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
 return true;
   }
 
+  const auto *BeforeLeft = Left.Previous;
+  const auto *AfterRight = Right.Next;
+
   if (Style.isCSharp()) {
 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
 Style.BraceWrapping.AfterFunction) {
@@ -5416,7 +5419,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
 }
 if (Right.is(TT_CSharpGenericTypeConstraint))
   return true;
-if (Right.Next && Right.Next->is(TT_FatArrow) &&
+if (AfterRight && AfterRight->is(TT_FatArrow) &&
 (Right.is(tok::numeric_constant) ||
  (Right.is(tok::identifier) && Right.TokenText == "_"))) {
   return true;
@@ -5433,15 +5436,14 @@ bool TokenAnnotator::mustBreakBefore(const 
AnnotatedLine ,
 Left.is(tok::r_square) && Right.is(tok::l_square)) {
   return true;
 }
-
   } else if (Style.isJavaScript()) {
 // FIXME: This might apply to other languages and token kinds.
-if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
-Left.Previous->is(tok::string_literal)) {
+if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
+BeforeLeft->is(tok::string_literal)) {
   return true;
 }
 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
-Left.Previous && Left.Previous->is(tok::equal) &&
+BeforeLeft && BeforeLeft->is(tok::equal) &&
 Line.First->isOneOf(tok::identifier, Keywords.kw_import, 
tok::kw_export,
 tok::kw_const) &&
 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
@@ -5460,8 +5462,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
   // instead of bin-packing.
   return true;
 }
-if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
-Left.Previous->is(TT_FatArrow)) {
+if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
+BeforeLeft->is(TT_FatArrow)) {
   // JS arrow function (=> {...}).
   switch (Style.AllowShortLambdasOnASingleLine) {
   case FormatStyle::SLS_All:
@@ -5489,8 +5491,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
   FormatStyle::SFS_InlineOnly);
 }
   } else if (Style.Language == FormatStyle::LK_Java) {
-if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
-Right.Next->is(tok::string_literal)) {
+if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
+AfterRight->is(tok::string_literal)) {
   return true;
 }
   } else if (Style.isVerilog()) {
@@ -5543,8 +5545,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
   }
   return Style.BreakArrays;
 }
-  }
-  if (Style.isTableGen()) {
+  } else if (Style.isTableGen()) {
 // Break the comma in side cond operators.
 // !cond(case1:1,
 //   case2:0);
@@ -5600,8 +5601,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
 return true;
   if (Left.IsUnterminatedLiteral)
 return true;
-  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
-  Right.Next->is(tok::string_literal)) {
+  if (Right.is(tok::lessless) && AfterRight && Left.is(tok::string_literal) &&
+  AfterRight->is(tok::string_literal)) {
 return true;
   }
   if (Right.is(TT_RequiresClause)) {
@@ -5678,8 +5679,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
 // string literal accordingly. Thus, we try keep existing line breaks.
 return Right.IsMultiline && Right.NewlinesBefore > 0;
   }
-  if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
- Left.Previous->is(tok::equal))) &&
+  if ((Left.is(tok::l_brace) ||
+   (Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
   Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
 // Don't put enums or option definitions onto single lines in protocol
 // buffers.
@@ -5793,7 +5794,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
,
   //
   // We ensure elsewhere that extensions are always on 

[clang] 364f988 - Reland "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"

2024-05-13 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-13T21:54:23-07:00
New Revision: 364f988d3feb46ead8fdb657c9eab78d93425a28

URL: 
https://github.com/llvm/llvm-project/commit/364f988d3feb46ead8fdb657c9eab78d93425a28
DIFF: 
https://github.com/llvm/llvm-project/commit/364f988d3feb46ead8fdb657c9eab78d93425a28.diff

LOG: Reland "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"

Remove FormatToken::isSimpleTypeSpecifier() and call
Token::isSimpleTypeSpecifier(LangOpts) instead.

Added: 


Modified: 
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.cpp
clang/lib/Format/FormatToken.h
clang/lib/Format/FormatTokenLexer.cpp
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/lib/Format/QualifierAlignmentFixer.h
clang/lib/Format/TokenAnalyzer.cpp
clang/lib/Format/TokenAnalyzer.h
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/TokenAnnotator.h
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 8f027ffa20cca..52005a6c881f3 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3858,8 +3858,7 @@ LangOptions getFormattingLangOpts(const FormatStyle 
) {
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
 
   LangOpts.LineComment = 1;
-  bool AlternativeOperators = Style.isCpp();
-  LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
+  LangOpts.CXXOperatorNames = Style.isCpp();
   LangOpts.Bool = 1;
   LangOpts.ObjC = 1;
   LangOpts.MicrosoftExt = 1;// To get kw___try, kw___finally.

diff  --git a/clang/lib/Format/FormatToken.cpp 
b/clang/lib/Format/FormatToken.cpp
index 4fb70ffac706d..85bec71ffbbc8 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -34,43 +34,6 @@ const char *getTokenTypeName(TokenType Type) {
   return nullptr;
 }
 
-// FIXME: This is copy from Sema. Put it in a common place and remove
-// duplication.
-bool FormatToken::isSimpleTypeSpecifier() const {
-  switch (Tok.getKind()) {
-  case tok::kw_short:
-  case tok::kw_long:
-  case tok::kw___int64:
-  case tok::kw___int128:
-  case tok::kw_signed:
-  case tok::kw_unsigned:
-  case tok::kw_void:
-  case tok::kw_char:
-  case tok::kw_int:
-  case tok::kw_half:
-  case tok::kw_float:
-  case tok::kw_double:
-  case tok::kw___bf16:
-  case tok::kw__Float16:
-  case tok::kw___float128:
-  case tok::kw___ibm128:
-  case tok::kw_wchar_t:
-  case tok::kw_bool:
-#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
-#include "clang/Basic/TransformTypeTraits.def"
-  case tok::annot_typename:
-  case tok::kw_char8_t:
-  case tok::kw_char16_t:
-  case tok::kw_char32_t:
-  case tok::kw_typeof:
-  case tok::kw_decltype:
-  case tok::kw__Atomic:
-return true;
-  default:
-return false;
-  }
-}
-
 // Sorted common C++ non-keyword types.
 static SmallVector CppNonKeywordTypes = {
 "clock_t",  "int16_t",   "int32_t", "int64_t",   "int8_t",
@@ -78,15 +41,16 @@ static SmallVector CppNonKeywordTypes = {
 "uint32_t", "uint64_t",  "uint8_t", "uintptr_t",
 };
 
-bool FormatToken::isTypeName(bool IsCpp) const {
-  return is(TT_TypeName) || isSimpleTypeSpecifier() ||
+bool FormatToken::isTypeName(const LangOptions ) const {
+  const bool IsCpp = LangOpts.CXXOperatorNames;
+  return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) ||
  (IsCpp && is(tok::identifier) &&
   std::binary_search(CppNonKeywordTypes.begin(),
  CppNonKeywordTypes.end(), TokenText));
 }
 
-bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
-  return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
+bool FormatToken::isTypeOrIdentifier(const LangOptions ) const {
+  return isTypeName(LangOpts) || isOneOf(tok::kw_auto, tok::identifier);
 }
 
 bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle ) const {

diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 95f16fde5005f..8792f4c750748 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -684,12 +684,8 @@ struct FormatToken {
isAttribute();
   }
 
-  /// Determine whether the token is a simple-type-specifier.
-  [[nodiscard]] bool isSimpleTypeSpecifier() const;
-
-  [[nodiscard]] bool isTypeName(bool IsCpp) const;
-
-  [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
+  [[nodiscard]] bool isTypeName(const LangOptions ) const;
+  [[nodiscard]] bool isTypeOrIdentifier(const LangOptions ) const;
 
   bool isObjCAccessSpecifier() const {
 return is(tok::at) && Next &&

diff  --git a/clang/lib/Format/FormatTokenLexer.cpp 
b/clang/lib/Format/FormatTokenLexer.cpp
index f430d3764babe..e21b5a882b777 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ 

[clang] e20800c - [clang-format][NFC] Test IsQualifier only needs to call the lexer

2024-05-13 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-13T19:45:11-07:00
New Revision: e20800c16f0570562fea31e9a02d65ba56e6858a

URL: 
https://github.com/llvm/llvm-project/commit/e20800c16f0570562fea31e9a02d65ba56e6858a
DIFF: 
https://github.com/llvm/llvm-project/commit/e20800c16f0570562fea31e9a02d65ba56e6858a.diff

LOG: [clang-format][NFC] Test IsQualifier only needs to call the lexer

Added: 


Modified: 
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index 1e997bb06b867..4ddeef50f5f78 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1055,7 +1055,9 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   ConfiguredTokens.push_back(tok::kw_constexpr);
   ConfiguredTokens.push_back(tok::kw_friend);
 
-  auto Tokens = annotate(
+  TestLexer lexer{Allocator, Buffers};
+
+  auto Tokens = lexer.lex(
   "const static inline auto restrict int double long constexpr friend");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
 
@@ -1081,7 +1083,7 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   EXPECT_TRUE(isQualifierOrType(Tokens[8]));
   EXPECT_TRUE(isQualifierOrType(Tokens[9]));
 
-  auto NotTokens = annotate("for while do Foo Bar ");
+  auto NotTokens = lexer.lex("for while do Foo Bar ");
   ASSERT_EQ(NotTokens.size(), 6u) << Tokens;
 
   EXPECT_FALSE(isConfiguredQualifierOrType(NotTokens[0], ConfiguredTokens));



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Move LeftRightQualifierAlignmentFixer::is...() (PR #91930)

2024-05-13 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/91930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Move LeftRightQualifierAlignmentFixer::is...() (PR #91930)

2024-05-13 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/91930
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Move LeftRightQualifierAlignmentFixer::is...() (PR #91930)

2024-05-13 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/91930

Move static member functions LeftRightQualifierAlignmentFixer::is...() out the 
class so that #91712 can reland.

>From 6d346a4ec253110288a806fabde093678228184b Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Mon, 13 May 2024 00:07:50 -0700
Subject: [PATCH] [clang-format][NFC] Move
 LeftRightQualifierAlignmentFixer::is...()

Move static member functions LeftRightQualifierAlignmentFixer::is...() out
the class so that #91712 can reland.
---
 clang/lib/Format/QualifierAlignmentFixer.cpp  | 11 +--
 clang/lib/Format/QualifierAlignmentFixer.h| 19 ++--
 clang/unittests/Format/QualifierFixerTest.cpp | 98 +++
 3 files changed, 52 insertions(+), 76 deletions(-)

diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp 
b/clang/lib/Format/QualifierAlignmentFixer.cpp
index c263530456727..36d0639041c6c 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -614,22 +614,21 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
   }
 }
 
-bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken 
*Tok,
- bool IsCpp) {
+bool isQualifierOrType(const FormatToken *Tok, bool IsCpp) {
   return Tok &&
  (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
 }
 
-bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-const FormatToken *Tok, const std::vector ,
-bool IsCpp) {
+bool isConfiguredQualifierOrType(const FormatToken *Tok,
+ const std::vector ,
+ bool IsCpp) {
   return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) ||
  isConfiguredQualifier(Tok, Qualifiers));
 }
 
 // If a token is an identifier and it's upper case, it could
 // be a macro and hence we need to be able to ignore it.
-bool LeftRightQualifierAlignmentFixer::isPossibleMacro(const FormatToken *Tok) 
{
+bool isPossibleMacro(const FormatToken *Tok) {
   if (!Tok)
 return false;
   if (Tok->isNot(tok::identifier))
diff --git a/clang/lib/Format/QualifierAlignmentFixer.h 
b/clang/lib/Format/QualifierAlignmentFixer.h
index e1cc27e62b13a..e31d525da1640 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.h
+++ b/clang/lib/Format/QualifierAlignmentFixer.h
@@ -32,6 +32,15 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
 std::vector ,
 std::vector );
 
+// Is the Token a simple or qualifier type
+bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
+bool isConfiguredQualifierOrType(const FormatToken *Tok,
+ const std::vector ,
+ bool IsCpp = true);
+
+// Is the Token likely a Macro
+bool isPossibleMacro(const FormatToken *Tok);
+
 class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
   std::string Qualifier;
   bool RightAlign;
@@ -69,16 +78,6 @@ class LeftRightQualifierAlignmentFixer : public 
TokenAnalyzer {
  const FormatToken *Tok,
  const std::string ,
  tok::TokenKind QualifierType);
-
-  // Is the Token a simple or qualifier type
-  static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
-  static bool
-  isConfiguredQualifierOrType(const FormatToken *Tok,
-  const std::vector ,
-  bool IsCpp = true);
-
-  // Is the Token likely a Macro
-  static bool isPossibleMacro(const FormatToken *Tok);
 };
 
 } // end namespace format
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index 792d8f3c3a982..1e997bb06b867 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1059,66 +1059,44 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   "const static inline auto restrict int double long constexpr friend");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
 
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[0], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[1], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[2], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[3], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[4], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[5], ConfiguredTokens));
-  EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[6], ConfiguredTokens));
-  

[clang] [clang-format] Fix FormatToken::isSimpleTypeSpecifier() (PR #91712)

2024-05-13 Thread Owen Pan via cfe-commits

owenca wrote:

@dyung I've reverted all relevant commits in commit 
1fadb2b0c881ced247931f442fdee6c4ed96dccb.

https://github.com/llvm/llvm-project/pull/91712
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 1fadb2b - Revert "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"

2024-05-13 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-12T23:15:35-07:00
New Revision: 1fadb2b0c881ced247931f442fdee6c4ed96dccb

URL: 
https://github.com/llvm/llvm-project/commit/1fadb2b0c881ced247931f442fdee6c4ed96dccb
DIFF: 
https://github.com/llvm/llvm-project/commit/1fadb2b0c881ced247931f442fdee6c4ed96dccb.diff

LOG: Revert "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"

This reverts commits e62ce1f8842c, 5cd280433e8e, and de641e289269 due to
buildbot failures.

Added: 


Modified: 
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.cpp
clang/lib/Format/FormatToken.h
clang/lib/Format/FormatTokenLexer.cpp
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/lib/Format/QualifierAlignmentFixer.h
clang/lib/Format/TokenAnalyzer.cpp
clang/lib/Format/TokenAnalyzer.h
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/TokenAnnotator.h
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 52005a6c881f3..8f027ffa20cca 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3858,7 +3858,8 @@ LangOptions getFormattingLangOpts(const FormatStyle 
) {
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
 
   LangOpts.LineComment = 1;
-  LangOpts.CXXOperatorNames = Style.isCpp();
+  bool AlternativeOperators = Style.isCpp();
+  LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
   LangOpts.Bool = 1;
   LangOpts.ObjC = 1;
   LangOpts.MicrosoftExt = 1;// To get kw___try, kw___finally.

diff  --git a/clang/lib/Format/FormatToken.cpp 
b/clang/lib/Format/FormatToken.cpp
index 85bec71ffbbc8..4fb70ffac706d 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -34,6 +34,43 @@ const char *getTokenTypeName(TokenType Type) {
   return nullptr;
 }
 
+// FIXME: This is copy from Sema. Put it in a common place and remove
+// duplication.
+bool FormatToken::isSimpleTypeSpecifier() const {
+  switch (Tok.getKind()) {
+  case tok::kw_short:
+  case tok::kw_long:
+  case tok::kw___int64:
+  case tok::kw___int128:
+  case tok::kw_signed:
+  case tok::kw_unsigned:
+  case tok::kw_void:
+  case tok::kw_char:
+  case tok::kw_int:
+  case tok::kw_half:
+  case tok::kw_float:
+  case tok::kw_double:
+  case tok::kw___bf16:
+  case tok::kw__Float16:
+  case tok::kw___float128:
+  case tok::kw___ibm128:
+  case tok::kw_wchar_t:
+  case tok::kw_bool:
+#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
+#include "clang/Basic/TransformTypeTraits.def"
+  case tok::annot_typename:
+  case tok::kw_char8_t:
+  case tok::kw_char16_t:
+  case tok::kw_char32_t:
+  case tok::kw_typeof:
+  case tok::kw_decltype:
+  case tok::kw__Atomic:
+return true;
+  default:
+return false;
+  }
+}
+
 // Sorted common C++ non-keyword types.
 static SmallVector CppNonKeywordTypes = {
 "clock_t",  "int16_t",   "int32_t", "int64_t",   "int8_t",
@@ -41,16 +78,15 @@ static SmallVector CppNonKeywordTypes = {
 "uint32_t", "uint64_t",  "uint8_t", "uintptr_t",
 };
 
-bool FormatToken::isTypeName(const LangOptions ) const {
-  const bool IsCpp = LangOpts.CXXOperatorNames;
-  return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) ||
+bool FormatToken::isTypeName(bool IsCpp) const {
+  return is(TT_TypeName) || isSimpleTypeSpecifier() ||
  (IsCpp && is(tok::identifier) &&
   std::binary_search(CppNonKeywordTypes.begin(),
  CppNonKeywordTypes.end(), TokenText));
 }
 
-bool FormatToken::isTypeOrIdentifier(const LangOptions ) const {
-  return isTypeName(LangOpts) || isOneOf(tok::kw_auto, tok::identifier);
+bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
+  return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
 }
 
 bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle ) const {

diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 8792f4c750748..95f16fde5005f 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -684,8 +684,12 @@ struct FormatToken {
isAttribute();
   }
 
-  [[nodiscard]] bool isTypeName(const LangOptions ) const;
-  [[nodiscard]] bool isTypeOrIdentifier(const LangOptions ) const;
+  /// Determine whether the token is a simple-type-specifier.
+  [[nodiscard]] bool isSimpleTypeSpecifier() const;
+
+  [[nodiscard]] bool isTypeName(bool IsCpp) const;
+
+  [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
 
   bool isObjCAccessSpecifier() const {
 return is(tok::at) && Next &&

diff  --git a/clang/lib/Format/FormatTokenLexer.cpp 
b/clang/lib/Format/FormatTokenLexer.cpp
index e21b5a882b777..f430d3764babe 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ 

[clang] de641e2 - [clang-format] Fix buildbot failures

2024-05-12 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-12T18:56:18-07:00
New Revision: de641e289269061f8bdb138bb5c50e27ef4b354f

URL: 
https://github.com/llvm/llvm-project/commit/de641e289269061f8bdb138bb5c50e27ef4b354f
DIFF: 
https://github.com/llvm/llvm-project/commit/de641e289269061f8bdb138bb5c50e27ef4b354f.diff

LOG: [clang-format] Fix buildbot failures

Fix the following buildbot failures by making LangOpts in the unit test
static:
https://lab.llvm.org/buildbot/#/builders/236/builds/11223
https://lab.llvm.org/buildbot/#/builders/239/builds/6968

Added: 


Modified: 
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index 5463bfbb65ca6..fdc392e5d9481 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1055,7 +1055,7 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   ConfiguredTokens.push_back(tok::kw_constexpr);
   ConfiguredTokens.push_back(tok::kw_friend);
 
-  LangOptions LangOpts{getFormattingLangOpts()};
+  static const LangOptions LangOpts{getFormattingLangOpts()};
 
   auto Tokens = annotate(
   "const static inline auto restrict int double long constexpr friend");



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 626025a - Revert "[clang-format] Fix buildbot failures"

2024-05-12 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-12T18:35:35-07:00
New Revision: 626025ac7796b70cde9fc0fd4f688c3441949d04

URL: 
https://github.com/llvm/llvm-project/commit/626025ac7796b70cde9fc0fd4f688c3441949d04
DIFF: 
https://github.com/llvm/llvm-project/commit/626025ac7796b70cde9fc0fd4f688c3441949d04.diff

LOG: Revert "[clang-format] Fix buildbot failures"

This reverts commit 0869204cff22831d0bb19a82c99bf85e4deb4ae3, which caused a
buildbot failure:
https://lab.llvm.org/buildbot/#/builders/5/builds/43322

Added: 


Modified: 
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/lib/Format/QualifierAlignmentFixer.h
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/lib/Format/QualifierAlignmentFixer.cpp 
b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 077ce5e597a22..a904f0b773c6d 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -281,7 +281,7 @@ const FormatToken 
*LeftRightQualifierAlignmentFixer::analyzeRight(
 
 const FormatToken *LastSimpleTypeSpecifier = TypeToken;
 while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
- )) {
+ LangOpts)) {
   LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
 }
 
@@ -414,7 +414,7 @@ const FormatToken 
*LeftRightQualifierAlignmentFixer::analyzeLeft(
 const FormatToken *LastSimpleTypeSpecifier = TypeToken;
 while (isConfiguredQualifierOrType(
 LastSimpleTypeSpecifier->getPreviousNonComment(),
-ConfiguredQualifierTokens, )) {
+ConfiguredQualifierTokens, LangOpts)) {
   LastSimpleTypeSpecifier =
   LastSimpleTypeSpecifier->getPreviousNonComment();
 }
@@ -613,18 +613,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
 }
 
 bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
-const FormatToken *Tok, const LangOptions *LangOpts) {
-  return Tok &&
- (Tok->isTypeName(LangOpts ? *LangOpts : getFormattingLangOpts()) ||
-  Tok->is(tok::kw_auto) || isQualifier(Tok));
+const FormatToken *Tok, const LangOptions ) {
+  return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
+ isQualifier(Tok));
 }
 
 bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
 const FormatToken *Tok, const std::vector ,
-const LangOptions *LangOpts) {
-  return Tok &&
- (Tok->isTypeName(LangOpts ? *LangOpts : getFormattingLangOpts()) ||
-  Tok->is(tok::kw_auto) || isConfiguredQualifier(Tok, Qualifiers));
+const LangOptions ) {
+  return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
+ isConfiguredQualifier(Tok, Qualifiers));
 }
 
 // If a token is an identifier and it's upper case, it could

diff  --git a/clang/lib/Format/QualifierAlignmentFixer.h 
b/clang/lib/Format/QualifierAlignmentFixer.h
index 97fcb42e4b2e5..710fa2dc00309 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.h
+++ b/clang/lib/Format/QualifierAlignmentFixer.h
@@ -72,11 +72,11 @@ class LeftRightQualifierAlignmentFixer : public 
TokenAnalyzer {
 
   // Is the Token a simple or qualifier type
   static bool isQualifierOrType(const FormatToken *Tok,
-const LangOptions *LangOpts = nullptr);
+const LangOptions );
   static bool
   isConfiguredQualifierOrType(const FormatToken *Tok,
   const std::vector ,
-  const LangOptions *LangOpts = nullptr);
+  const LangOptions );
 
   // Is the Token likely a Macro
   static bool isPossibleMacro(const FormatToken *Tok);

diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index 792d8f3c3a982..5463bfbb65ca6 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1055,70 +1055,82 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   ConfiguredTokens.push_back(tok::kw_constexpr);
   ConfiguredTokens.push_back(tok::kw_friend);
 
+  LangOptions LangOpts{getFormattingLangOpts()};
+
   auto Tokens = annotate(
   "const static inline auto restrict int double long constexpr friend");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
 
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[0], ConfiguredTokens));
+  Tokens[0], ConfiguredTokens, LangOpts));
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[1], ConfiguredTokens));
+  Tokens[1], ConfiguredTokens, LangOpts));
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[2], ConfiguredTokens));
+  Tokens[2], ConfiguredTokens, LangOpts));
   

[clang] 0869204 - [clang-format] Fix buildbot failures

2024-05-11 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-11T15:23:35-07:00
New Revision: 0869204cff22831d0bb19a82c99bf85e4deb4ae3

URL: 
https://github.com/llvm/llvm-project/commit/0869204cff22831d0bb19a82c99bf85e4deb4ae3
DIFF: 
https://github.com/llvm/llvm-project/commit/0869204cff22831d0bb19a82c99bf85e4deb4ae3.diff

LOG: [clang-format] Fix buildbot failures

This effectively reverts 5cd280433e8e and changes to QualifierFixerTest.cpp
from e62ce1f8842c.

Failed buidbots:
https://lab.llvm.org/buildbot/#/builders/236/builds/11223
https://lab.llvm.org/buildbot/#/builders/239/builds/6968

Added: 


Modified: 
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/lib/Format/QualifierAlignmentFixer.h
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/lib/Format/QualifierAlignmentFixer.cpp 
b/clang/lib/Format/QualifierAlignmentFixer.cpp
index a904f0b773c6d..077ce5e597a22 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -281,7 +281,7 @@ const FormatToken 
*LeftRightQualifierAlignmentFixer::analyzeRight(
 
 const FormatToken *LastSimpleTypeSpecifier = TypeToken;
 while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
- LangOpts)) {
+ )) {
   LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
 }
 
@@ -414,7 +414,7 @@ const FormatToken 
*LeftRightQualifierAlignmentFixer::analyzeLeft(
 const FormatToken *LastSimpleTypeSpecifier = TypeToken;
 while (isConfiguredQualifierOrType(
 LastSimpleTypeSpecifier->getPreviousNonComment(),
-ConfiguredQualifierTokens, LangOpts)) {
+ConfiguredQualifierTokens, )) {
   LastSimpleTypeSpecifier =
   LastSimpleTypeSpecifier->getPreviousNonComment();
 }
@@ -613,16 +613,18 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
 }
 
 bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
-const FormatToken *Tok, const LangOptions ) {
-  return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
- isQualifier(Tok));
+const FormatToken *Tok, const LangOptions *LangOpts) {
+  return Tok &&
+ (Tok->isTypeName(LangOpts ? *LangOpts : getFormattingLangOpts()) ||
+  Tok->is(tok::kw_auto) || isQualifier(Tok));
 }
 
 bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
 const FormatToken *Tok, const std::vector ,
-const LangOptions ) {
-  return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
- isConfiguredQualifier(Tok, Qualifiers));
+const LangOptions *LangOpts) {
+  return Tok &&
+ (Tok->isTypeName(LangOpts ? *LangOpts : getFormattingLangOpts()) ||
+  Tok->is(tok::kw_auto) || isConfiguredQualifier(Tok, Qualifiers));
 }
 
 // If a token is an identifier and it's upper case, it could

diff  --git a/clang/lib/Format/QualifierAlignmentFixer.h 
b/clang/lib/Format/QualifierAlignmentFixer.h
index 710fa2dc00309..97fcb42e4b2e5 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.h
+++ b/clang/lib/Format/QualifierAlignmentFixer.h
@@ -72,11 +72,11 @@ class LeftRightQualifierAlignmentFixer : public 
TokenAnalyzer {
 
   // Is the Token a simple or qualifier type
   static bool isQualifierOrType(const FormatToken *Tok,
-const LangOptions );
+const LangOptions *LangOpts = nullptr);
   static bool
   isConfiguredQualifierOrType(const FormatToken *Tok,
   const std::vector ,
-  const LangOptions );
+  const LangOptions *LangOpts = nullptr);
 
   // Is the Token likely a Macro
   static bool isPossibleMacro(const FormatToken *Tok);

diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index 5463bfbb65ca6..792d8f3c3a982 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1055,82 +1055,70 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   ConfiguredTokens.push_back(tok::kw_constexpr);
   ConfiguredTokens.push_back(tok::kw_friend);
 
-  LangOptions LangOpts{getFormattingLangOpts()};
-
   auto Tokens = annotate(
   "const static inline auto restrict int double long constexpr friend");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
 
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[0], ConfiguredTokens, LangOpts));
+  Tokens[0], ConfiguredTokens));
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[1], ConfiguredTokens, LangOpts));
+  Tokens[1], ConfiguredTokens));
   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
-  Tokens[2], ConfiguredTokens, LangOpts));
+  Tokens[2], 

[clang] 5cd2804 - [clang-format] Move QualifierFixerTest::LangOpts to IsQualifierType test

2024-05-11 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-10T23:14:58-07:00
New Revision: 5cd280433e8e063bf3c2390eaf152e726db5b0fb

URL: 
https://github.com/llvm/llvm-project/commit/5cd280433e8e063bf3c2390eaf152e726db5b0fb
DIFF: 
https://github.com/llvm/llvm-project/commit/5cd280433e8e063bf3c2390eaf152e726db5b0fb.diff

LOG: [clang-format] Move QualifierFixerTest::LangOpts to IsQualifierType test

This should fix buildbot failures in:
https://lab.llvm.org/buildbot/#/builders/5/builds/43303
https://lab.llvm.org/buildbot/#/builders/168/builds/20347

Added: 


Modified: 
clang/unittests/Format/QualifierFixerTest.cpp

Removed: 




diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp 
b/clang/unittests/Format/QualifierFixerTest.cpp
index ada47549fd0b3..5463bfbb65ca6 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -27,15 +27,11 @@ namespace {
   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
 
 class QualifierFixerTest : public FormatTestBase {
-public:
-  QualifierFixerTest() : LangOpts(getFormattingLangOpts()) {}
-
 protected:
   TokenList annotate(llvm::StringRef Code,
  const FormatStyle  = getLLVMStyle()) {
 return TestLexer(Allocator, Buffers, Style).annotate(Code);
   }
-  LangOptions LangOpts;
   llvm::SpecificBumpPtrAllocator Allocator;
   std::vector> Buffers;
 };
@@ -1059,6 +1055,8 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
   ConfiguredTokens.push_back(tok::kw_constexpr);
   ConfiguredTokens.push_back(tok::kw_friend);
 
+  LangOptions LangOpts{getFormattingLangOpts()};
+
   auto Tokens = annotate(
   "const static inline auto restrict int double long constexpr friend");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix FormatToken::isSimpleTypeSpecifier() (PR #91712)

2024-05-10 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/91712
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix FormatToken::isSimpleTypeSpecifier() (PR #91712)

2024-05-10 Thread Owen Pan via cfe-commits

owenca wrote:

> I think this will stop people introducing extra arguments to function
> 
> ...isCpp,isThis,isThat)

I meant to fix the FIXME after I had langded a8279a8bc541, but my previous 
attempts didn't quite work. Now it seems this solution will stick.

https://github.com/llvm/llvm-project/pull/91712
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add option to remove leading blank lines (PR #91221)

2024-05-10 Thread Owen Pan via cfe-commits

https://github.com/owenca requested changes to this pull request.

See https://github.com/llvm/llvm-project/pull/91221#issuecomment-2105454428.

https://github.com/llvm/llvm-project/pull/91221
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add option to remove leading blank lines (PR #91221)

2024-05-10 Thread Owen Pan via cfe-commits

owenca wrote:

> Should I make dropping leading blank lines the default?

We should keep leading blank lines (up to `MaxEmptyLinesToKeep`) by default as 
before.

https://github.com/llvm/llvm-project/pull/91221
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add option to remove leading blank lines (PR #91221)

2024-05-10 Thread Owen Pan via cfe-commits

owenca wrote:

I think this is a good time to combine the `KeepEmptyLines...` options. For 
example:
```
KeepEmptyLines:
  AtStartOfBlock: true
  AtStartOfFile: false
  AtEndOfFile: true
```

https://github.com/llvm/llvm-project/pull/91221
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix FormatToken::isSimpleTypeSpecifier() (PR #91712)

2024-05-10 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/91712

Remove FormatToken::isSimpleTypeSpecifier() and call 
Token::isSimpleTypeSpecifier(LangOpts) instead.

>From 3e0b10020d15c102e050ad3785622bef01d1c075 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 10 May 2024 01:00:16 -0700
Subject: [PATCH] [clang-format] Fix FormatToken::isSimpleTypeSpecifier()

Remove FormatToken::isSimpleTypeSpecifier() and call
Token::isSimpleTypeSpecifier(LangOpts) instead.
---
 clang/lib/Format/Format.cpp   |  3 +-
 clang/lib/Format/FormatToken.cpp  | 46 +
 clang/lib/Format/FormatToken.h|  8 +-
 clang/lib/Format/FormatTokenLexer.cpp |  1 -
 clang/lib/Format/QualifierAlignmentFixer.cpp  | 24 +++--
 clang/lib/Format/QualifierAlignmentFixer.h|  5 +-
 clang/lib/Format/TokenAnalyzer.cpp|  4 +-
 clang/lib/Format/TokenAnalyzer.h  |  1 +
 clang/lib/Format/TokenAnnotator.cpp   | 43 +
 clang/lib/Format/TokenAnnotator.h |  6 +-
 clang/lib/Format/UnwrappedLineParser.cpp  | 19 ++--
 clang/lib/Format/UnwrappedLineParser.h|  1 +
 clang/unittests/Format/QualifierFixerTest.cpp | 94 +++
 13 files changed, 121 insertions(+), 134 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c4eac1c99a663..c5c79dd0f883e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3858,8 +3858,7 @@ LangOptions getFormattingLangOpts(const FormatStyle 
) {
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
 
   LangOpts.LineComment = 1;
-  bool AlternativeOperators = Style.isCpp();
-  LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
+  LangOpts.CXXOperatorNames = Style.isCpp();
   LangOpts.Bool = 1;
   LangOpts.ObjC = 1;
   LangOpts.MicrosoftExt = 1;// To get kw___try, kw___finally.
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 4fb70ffac706d..85bec71ffbbc8 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -34,43 +34,6 @@ const char *getTokenTypeName(TokenType Type) {
   return nullptr;
 }
 
-// FIXME: This is copy from Sema. Put it in a common place and remove
-// duplication.
-bool FormatToken::isSimpleTypeSpecifier() const {
-  switch (Tok.getKind()) {
-  case tok::kw_short:
-  case tok::kw_long:
-  case tok::kw___int64:
-  case tok::kw___int128:
-  case tok::kw_signed:
-  case tok::kw_unsigned:
-  case tok::kw_void:
-  case tok::kw_char:
-  case tok::kw_int:
-  case tok::kw_half:
-  case tok::kw_float:
-  case tok::kw_double:
-  case tok::kw___bf16:
-  case tok::kw__Float16:
-  case tok::kw___float128:
-  case tok::kw___ibm128:
-  case tok::kw_wchar_t:
-  case tok::kw_bool:
-#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
-#include "clang/Basic/TransformTypeTraits.def"
-  case tok::annot_typename:
-  case tok::kw_char8_t:
-  case tok::kw_char16_t:
-  case tok::kw_char32_t:
-  case tok::kw_typeof:
-  case tok::kw_decltype:
-  case tok::kw__Atomic:
-return true;
-  default:
-return false;
-  }
-}
-
 // Sorted common C++ non-keyword types.
 static SmallVector CppNonKeywordTypes = {
 "clock_t",  "int16_t",   "int32_t", "int64_t",   "int8_t",
@@ -78,15 +41,16 @@ static SmallVector CppNonKeywordTypes = {
 "uint32_t", "uint64_t",  "uint8_t", "uintptr_t",
 };
 
-bool FormatToken::isTypeName(bool IsCpp) const {
-  return is(TT_TypeName) || isSimpleTypeSpecifier() ||
+bool FormatToken::isTypeName(const LangOptions ) const {
+  const bool IsCpp = LangOpts.CXXOperatorNames;
+  return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) ||
  (IsCpp && is(tok::identifier) &&
   std::binary_search(CppNonKeywordTypes.begin(),
  CppNonKeywordTypes.end(), TokenText));
 }
 
-bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
-  return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
+bool FormatToken::isTypeOrIdentifier(const LangOptions ) const {
+  return isTypeName(LangOpts) || isOneOf(tok::kw_auto, tok::identifier);
 }
 
 bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle ) const {
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 95f16fde5005f..8792f4c750748 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -684,12 +684,8 @@ struct FormatToken {
isAttribute();
   }
 
-  /// Determine whether the token is a simple-type-specifier.
-  [[nodiscard]] bool isSimpleTypeSpecifier() const;
-
-  [[nodiscard]] bool isTypeName(bool IsCpp) const;
-
-  [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
+  [[nodiscard]] bool isTypeName(const LangOptions ) const;
+  [[nodiscard]] bool isTypeOrIdentifier(const LangOptions ) const;
 
   bool isObjCAccessSpecifier() const {
 return is(tok::at) && Next &&
diff --git a/clang/lib/Format/FormatTokenLexer.cpp 

[clang] [clang-format] Fix FormatToken::isSimpleTypeSpecifier() (PR #85564)

2024-05-10 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/85564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 6ce4c4c - [clang-format][NFC] Drop a redundant clang::format::

2024-05-10 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-10T00:05:16-07:00
New Revision: 6ce4c4ca2bb846c2b8d64dc2b3d3496785c9edff

URL: 
https://github.com/llvm/llvm-project/commit/6ce4c4ca2bb846c2b8d64dc2b3d3496785c9edff
DIFF: 
https://github.com/llvm/llvm-project/commit/6ce4c4ca2bb846c2b8d64dc2b3d3496785c9edff.diff

LOG: [clang-format][NFC] Drop a redundant clang::format::

Added: 


Modified: 
clang/lib/Format/UnwrappedLineParser.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 71557b127fb74..310b75485e089 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3385,7 +3385,7 @@ void UnwrappedLineParser::parseAccessSpecifier() {
 /// \brief Parses a requires, decides if it is a clause or an expression.
 /// \pre The current token has to be the requires keyword.
 /// \returns true if it parsed a clause.
-bool clang::format::UnwrappedLineParser::parseRequires() {
+bool UnwrappedLineParser::parseRequires() {
   assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
   auto RequiresToken = FormatTok;
 



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-06 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/91112
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)

2024-05-06 Thread Owen Pan via cfe-commits


@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle 
) {
   // the sequence "<::" will be unconditionally treated as "[:".
   // Cf. Lexer::LexTokenInternal.
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
+  LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11;

owenca wrote:

Can we set the default to 1 in `LangOpts.def`? That would take care of it.

https://github.com/llvm/llvm-project/pull/88265
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't remove parentheses of fold expressions (PR #91045)

2024-05-05 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick db0ed5533368

https://github.com/llvm/llvm-project/pull/91045
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't remove parentheses of fold expressions (PR #91045)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca milestoned 
https://github.com/llvm/llvm-project/pull/91045
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't remove parentheses of fold expressions (PR #91045)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/91045
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't allow comma in front of structural enum (PR #91056)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/91056
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From beab69244ce686a1d53342979d46f269a46c79de Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..6d092219877f91 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: java
+
+  true:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: java
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..74893f23210cd0 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code{.java}
+///   true:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -396,12 

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From 31a45ace7d828c63b31d0ba20ed07f2a0340c41d Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f0f1f44491251c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..7253f3b57b3003 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ -396,12 +413,14 

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/91112

>From be8569c3721337317635a2f0640237a2d5acd73a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH 1/2] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f320caf0d21998 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..d08db77c2ab063 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool AlignCaseArrows;
 /// Whether aligned case labels are aligned on the colon, or on the tokens
 /// after the colon.
 /// \code
@@ 

[clang] [clang-format] Handle Java switch expressions (PR #91112)

2024-05-05 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/91112

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows 
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.

>From be8569c3721337317635a2f0640237a2d5acd73a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 5 May 2024 00:21:55 -0700
Subject: [PATCH] [clang-format] Handle Java switch expressions

Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows
suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
---
 clang/docs/ClangFormatStyleOptions.rst|  36 +++-
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Format/Format.h   |  36 +++-
 clang/lib/Format/Format.cpp   |   4 +
 clang/lib/Format/FormatToken.h|   3 +
 clang/lib/Format/TokenAnnotator.cpp   |   2 +
 clang/lib/Format/UnwrappedLineFormatter.cpp   |   6 +
 clang/lib/Format/UnwrappedLineParser.cpp  |  46 -
 clang/lib/Format/UnwrappedLineParser.h|   2 +-
 clang/lib/Format/WhitespaceManager.cpp|  22 ++-
 clang/lib/Format/WhitespaceManager.h  |   2 +-
 clang/unittests/Format/ConfigParseTest.cpp|   2 +
 clang/unittests/Format/FormatTestJava.cpp | 171 ++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  18 ++
 14 files changed, 332 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index ce9035a2770eec..f320caf0d21998 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -861,7 +861,8 @@ the configuration (without a prefix: ``Auto``).
 
 **AlignConsecutiveShortCaseStatements** 
(``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`¶ 
`
   Style of aligning consecutive short case labels.
-  Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
+  Only applies if ``AllowShortCaseExpressionOnASingleLine`` or
+  ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
 
 
   .. code-block:: yaml
@@ -935,6 +936,24 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
+  * ``bool AlignCaseArrows`` Whether to align the case arrows when aligning 
short case expressions.
+
+.. code-block:: c++
+
+  true:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY  -> 9;
+default -> 0;
+  };
+
+  false:
+  i = switch (day) {\n"
+case THURSDAY, SATURDAY -> 8;
+case WEDNESDAY ->  9;
+default -> 0;
+  };
+
   * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
 after the colon.
 
@@ -1692,6 +1711,21 @@ the configuration (without a prefix: ``Auto``).
 
 
 
+.. _AllowShortCaseExpressionOnASingleLine:
+
+**AllowShortCaseExpressionOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 19` :ref:`¶ `
+  Whether to merge a short switch labeled rule into a single line.
+
+  .. code-block:: c++
+
+true:   false:
+switch (a) {   vs.  switch (a) {
+case 1 -> 1;case 1 ->
+default -> 0; 1;
+};  default ->
+  0;
+};
+
 .. _AllowShortCaseLabelsOnASingleLine:
 
 **AllowShortCaseLabelsOnASingleLine** (``Boolean``) 
:versionbadge:`clang-format 3.6` :ref:`¶ `
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b146a9b56884ad..a85095e424b64b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -834,6 +834,9 @@ clang-format
   ``BreakTemplateDeclarations``.
 - ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
   ``BreakAfterReturnType``.
+- Handles Java ``switch`` expressions.
+- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
+- Adds ``AlignCaseArrows`` suboption to 
``AlignConsecutiveShortCaseStatements``.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8ebdc86b98329c..d08db77c2ab063 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,6 +375,23 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
+/// Whether to align the case arrows when aligning short case expressions.
+/// \code
+///   true:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY  -> 9;
+/// default -> 0;
+///   };
+///
+///   false:
+///   i = switch (day) {\n"
+/// case THURSDAY, SATURDAY -> 8;
+/// case WEDNESDAY ->  9;
+/// default -> 0;
+///   };
+/// \endcode
+bool 

[clang] 9154a32 - [clang-format][doc] Fix typos

2024-05-05 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-05T00:18:55-07:00
New Revision: 9154a324bfce5dee27cb04708bd250b030d6cdd2

URL: 
https://github.com/llvm/llvm-project/commit/9154a324bfce5dee27cb04708bd250b030d6cdd2
DIFF: 
https://github.com/llvm/llvm-project/commit/9154a324bfce5dee27cb04708bd250b030d6cdd2.diff

LOG: [clang-format][doc] Fix typos

Added: 


Modified: 
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h

Removed: 




diff  --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index 39f7cded36edbf..ce9035a2770eec 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -935,8 +935,8 @@ the configuration (without a prefix: ``Auto``).
   default: return "";
   }
 
-  * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the
-, or on the tokens after the colon.
+  * ``bool AlignCaseColons`` Whether aligned case labels are aligned on the 
colon, or on the tokens
+after the colon.
 
 .. code-block:: c++
 

diff  --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 48f5fb44157570..8ebdc86b98329c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -375,8 +375,8 @@ struct FormatStyle {
 ///   }
 /// \endcode
 bool AcrossComments;
-/// Whether aligned case labels are aligned on the colon, or on the
-/// , or on the tokens after the colon.
+/// Whether aligned case labels are aligned on the colon, or on the tokens
+/// after the colon.
 /// \code
 ///   true:
 ///   switch (level) {



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't remove parentheses of fold expressions (PR #91045)

2024-05-05 Thread Owen Pan via cfe-commits

owenca wrote:

> I wouldn't worry too much about this, but, pedantically, you can be sure it's 
> a fold expression if the ellipsis is followed with or preceded by an operator
> 
> https://eel.is/c++draft/expr.prim.fold#nt:fold-operator

I had thought of that but decided not to bother. From 
https://en.cppreference.com/w/cpp/language/fold:
```
op - any of the following 32 binary operators: + - * / % ^ & | = < > << >> += 
-= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*
```
Because `<` and `>` are on the list, we still wouldn't be able to tell if 
`...>` is part of a fold expression. Also, I don't like the overhead of up to 
32 comparisons.

https://github.com/llvm/llvm-project/pull/91045
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a crash with AlignArrayOfStructures option (PR #86420)

2024-05-04 Thread Owen Pan via cfe-commits

https://github.com/owenca milestoned 
https://github.com/llvm/llvm-project/pull/86420
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a crash with AlignArrayOfStructures option (PR #86420)

2024-05-04 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick cceedc939a43

https://github.com/llvm/llvm-project/pull/86420
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)

2024-05-03 Thread Owen Pan via cfe-commits


@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle 
) {
   // the sequence "<::" will be unconditionally treated as "[:".
   // Cf. Lexer::LexTokenInternal.
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
+  LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11;

owenca wrote:

As clang-format formats all C code as C++, there's no need to set the new 
`RawStringLiterals` option here.

https://github.com/llvm/llvm-project/pull/88265
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Don't remove parentheses of fold expressions (PR #91045)

2024-05-03 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/91045

Fixes #90966.

>From 2cc5ef7cca02578262795a7f7ea840d0a1496f74 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 3 May 2024 22:15:33 -0700
Subject: [PATCH] [clang-format] Don't remove parentheses of fold expressions

Fixes #90966.
---
 clang/lib/Format/UnwrappedLineParser.cpp | 7 ++-
 clang/unittests/Format/FormatTest.cpp| 9 +
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index e8a8dd58d07eea..7d0278bce9a9c6 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2510,6 +2510,7 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
   assert(FormatTok->is(tok::l_paren) && "'(' expected.");
   auto *LeftParen = FormatTok;
   bool SeenEqual = false;
+  bool MightBeFoldExpr = false;
   const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
   nextToken();
   do {
@@ -2521,7 +2522,7 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
 parseChildBlock();
   break;
 case tok::r_paren:
-  if (!MightBeStmtExpr && !Line->InMacroBody &&
+  if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
   Style.RemoveParentheses > FormatStyle::RPS_Leave) {
 const auto *Prev = LeftParen->Previous;
 const auto *Next = Tokens->peekNextToken();
@@ -2564,6 +2565,10 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
 parseBracedList();
   }
   break;
+case tok::ellipsis:
+  MightBeFoldExpr = true;
+  nextToken();
+  break;
 case tok::equal:
   SeenEqual = true;
   if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 32ba6b6853c799..e6f8e4a06515ea 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27204,8 +27204,14 @@ TEST_F(FormatTest, RemoveParentheses) {
"if ((({ a; })))\n"
"  b;",
Style);
+  verifyFormat("static_assert((std::is_constructible_v && ...));",
+   "static_assert(((std::is_constructible_v && 
...)));",
+   Style);
   verifyFormat("return (0);", "return (((0)));", Style);
   verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
+  verifyFormat("return ((... && std::is_convertible_v));",
+   "return (((... && std::is_convertible_v)));",
+   Style);
 
   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
   verifyFormat("#define Return0 return (0);", Style);
@@ -27213,6 +27219,9 @@ TEST_F(FormatTest, RemoveParentheses) {
   verifyFormat("co_return 0;", "co_return ((0));", Style);
   verifyFormat("return 0;", "return (((0)));", Style);
   verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
+  verifyFormat("return (... && std::is_convertible_v);",
+   "return (((... && std::is_convertible_v)));",
+   Style);
   verifyFormat("inline decltype(auto) f() {\n"
"  if (a) {\n"
"return (a);\n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] d2af1ea - [clang-format][NFC] Minor improvement to parseLabel()

2024-05-02 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-05-02T19:42:01-07:00
New Revision: d2af1ea81dd0170133c53ebd67c9a9be125dfd31

URL: 
https://github.com/llvm/llvm-project/commit/d2af1ea81dd0170133c53ebd67c9a9be125dfd31
DIFF: 
https://github.com/llvm/llvm-project/commit/d2af1ea81dd0170133c53ebd67c9a9be125dfd31.diff

LOG: [clang-format][NFC] Minor improvement to parseLabel()

Added: 


Modified: 
clang/lib/Format/UnwrappedLineParser.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 854428389740d8..e8a8dd58d07eea 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3201,10 +3201,11 @@ void UnwrappedLineParser::parseDoWhile() {
 void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
   nextToken();
   unsigned OldLineLevel = Line->Level;
-  if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
---Line->Level;
+
   if (LeftAlignLabel)
 Line->Level = 0;
+  else if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
+--Line->Level;
 
   if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() &&
   FormatTok->is(tok::l_brace)) {



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [z/OS] treat text files as text files so auto-conversion is done (PR #90128)

2024-05-01 Thread Owen Pan via cfe-commits


@@ -413,8 +413,9 @@ static bool format(StringRef FileName, bool 
ErrorOnIncompleteFormat = false) {
   // On Windows, overwriting a file with an open file mapping doesn't work,
   // so read the whole file into memory when formatting in-place.
   ErrorOr> CodeOrErr =
-  !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName)
-: MemoryBuffer::getFileOrSTDIN(FileName);
+  !OutputXML && Inplace
+  ? MemoryBuffer::getFileAsStream(FileName)
+  : MemoryBuffer::getFileOrSTDIN(FileName, /*IsText=*/true);

owenca wrote:

I tested it on Windows and didn't have a problem. I used LF only, CRLF only, 
and mixed line endings for the following:
```
int i;
int j;
```
Running clang-format through all values of the `LineEnding` option, with and 
without in-place, seemed ok.

https://github.com/llvm/llvm-project/pull/90128
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a bug in annotating struct braces (PR #90555)

2024-04-30 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/90555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a bug in annotating struct braces (PR #90555)

2024-04-29 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/90555

Fixes #60040.

>From 77d807d47d47ca9916edc03182e1952c27300a8a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Mon, 29 Apr 2024 21:11:28 -0700
Subject: [PATCH] [clang-format] Fix a bug in annotating struct braces

Fixes #60040.
---
 clang/lib/Format/UnwrappedLineParser.cpp  |  5 -
 clang/unittests/Format/TokenAnnotatorTest.cpp | 13 +
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 3a263955a6a8fe..854428389740d8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3944,8 +3944,11 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
 switch (FormatTok->Tok.getKind()) {
 case tok::l_paren:
   // We can have macros in between 'class' and the class name.
-  if (!IsNonMacroIdentifier(Previous))
+  if (!IsNonMacroIdentifier(Previous) ||
+  // e.g. `struct macro(a) S { int i; };`
+  Previous->Previous == ) {
 parseParens();
+  }
   break;
 case tok::coloncolon:
   break;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index dff5251d2e9406..01daf8dee505bc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -438,6 +438,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
   EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
   EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_StructRBrace);
 
+  Tokens = annotate("struct macro(a) S {};");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+  EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_StructRBrace);
+
   Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
   ASSERT_EQ(Tokens.size(), 15u) << Tokens;
   EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
@@ -448,6 +453,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
   EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
   EXPECT_TOKEN(Tokens[16], tok::r_brace, TT_StructRBrace);
 
+  Tokens = annotate("struct macro(a) S {\n"
+"  void f(T );\n"
+"};");
+  ASSERT_EQ(Tokens.size(), 18u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+  EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
+
   Tokens = annotate("template  struct S {};");
   ASSERT_EQ(Tokens.size(), 18u) << Tokens;
   EXPECT_TOKEN(Tokens[7], tok::less, TT_TemplateOpener);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Set Change.TokenLength to ColumnWidth (PR #90378)

2024-04-28 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/90378
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Return early in isWordLike() for non-Verilog (PR #90363)

2024-04-28 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/90363
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Set Change.TokenLength to ColumnWidth (PR #90378)

2024-04-28 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/90378
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Set Change.TokenLength to ColumnWidth (PR #90378)

2024-04-28 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/90378

Fixes #47333.
Fixes #47624.
Fixes #75929.
Fixes #87885.
Fixes #89916.

>From d671f32e9bcc6e42e53ddd1799eb0fdef774c142 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sat, 27 Apr 2024 23:34:45 -0700
Subject: [PATCH] [clang-format] Set Change.TokenLength to ColumnWidth

Fixes #47333.
Fixes #47624.
Fixes #75929.
Fixes #87885.
Fixes #89916.
---
 clang/lib/Format/WhitespaceManager.cpp | 11 +---
 clang/unittests/Format/FormatTest.cpp  | 39 ++
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index cc9bcce6c414e0..44fd807ec27ea7 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -131,6 +131,7 @@ void WhitespaceManager::calculateLineBreakInformation() {
   for (unsigned I = 1, e = Changes.size(); I != e; ++I) {
 auto  = Changes[I];
 auto  = Changes[I - 1];
+auto  = P.TokenLength;
 SourceLocation OriginalWhitespaceStart =
 C.OriginalWhitespaceRange.getBegin();
 SourceLocation PreviousOriginalWhitespaceEnd =
@@ -169,21 +170,23 @@ void WhitespaceManager::calculateLineBreakInformation() {
 // line of the token.
 auto NewlinePos = Text.find_first_of('\n');
 if (NewlinePos == StringRef::npos) {
-  P.TokenLength = OriginalWhitespaceStartOffset -
+  PrevTokLength = OriginalWhitespaceStartOffset -
   PreviousOriginalWhitespaceEndOffset +
   C.PreviousLinePostfix.size() + 
P.CurrentLinePrefix.size();
+  if (!P.IsInsideToken)
+PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
 } else {
-  P.TokenLength = NewlinePos + P.CurrentLinePrefix.size();
+  PrevTokLength = NewlinePos + P.CurrentLinePrefix.size();
 }
 
 // If there are multiple changes in this token, sum up all the changes 
until
 // the end of the line.
 if (P.IsInsideToken && P.NewlinesBefore == 0)
-  LastOutsideTokenChange->TokenLength += P.TokenLength + P.Spaces;
+  LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
 else
   LastOutsideTokenChange = 
 
-C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + P.TokenLength;
+C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + PrevTokLength;
 
 P.IsTrailingComment =
 (C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 8ecc1188a127a5..32ba6b6853c799 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27363,6 +27363,45 @@ TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   verifyFormat(Code, Style);
 }
 
+TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
+  verifyFormat(
+  "int rus;  // А теперь комментарии, например, на русском, 2-байта\n"
+  "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
+  "  // уже отодвинут. Перенос, при этом, отрабатывает верно");
+
+  auto Style = getLLVMStyle();
+  Style.ColumnLimit = 15;
+  verifyNoChange("#define test  \\\n"
+ "  /* 测试 */  \\\n"
+ "  \"aa\"\\\n"
+ "  \"bb\"",
+ Style);
+
+  Style.ColumnLimit = 25;
+  verifyFormat("struct foo {\n"
+   "  int ii; ///< ii\n"
+   "  int b;  ///< ыыы\n"
+   "  int c;  ///< \n"
+   "};",
+   Style);
+
+  Style.ColumnLimit = 35;
+  verifyFormat("#define SENSOR_DESC_1 \\\n"
+   "  \"{\" \\\n"
+   "  \"unit_of_measurement: \\\"°C\\\",\"  \\\n"
+   "  \"}\"",
+   Style);
+
+  Style.ColumnLimit = 80;
+  Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
+  verifyFormat("Languages languages = {\n"
+   "Language{{'e', 'n'}, U\"Test English\" },\n"
+   "Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
+   "Language{{'r', 'u'}, U\"Test Русский\" },\n"
+   "};",
+   Style);
+}
+
 } // namespace
 } // namespace test
 } // namespace format

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Return early in isWordLike() for non-Verilog (PR #90363)

2024-04-27 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/90363

None

>From 70e5f7df8711ac22450a441efe91ed7fa6e339f0 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sat, 27 Apr 2024 15:16:43 -0700
Subject: [PATCH] [clang-format][NFC] Return early in isWordLike() for
 non-Verilog

---
 clang/lib/Format/FormatToken.h  |  8 ++--
 clang/lib/Format/TokenAnnotator.cpp | 13 -
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index f651e6228c206d..28b6488e54a422 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -1623,10 +1623,10 @@ struct AdditionalKeywords {
   IdentifierInfo *kw_then;
 
   /// Returns \c true if \p Tok is a keyword or an identifier.
-  bool isWordLike(const FormatToken ) const {
+  bool isWordLike(const FormatToken , bool IsVerilog = true) const {
 // getIdentifierinfo returns non-null for keywords as well as identifiers.
 return Tok.Tok.getIdentifierInfo() &&
-   !Tok.isOneOf(kw_verilogHash, kw_verilogHashHash, kw_apostrophe);
+   (!IsVerilog || !isVerilogKeywordSymbol(Tok));
   }
 
   /// Returns \c true if \p Tok is a true JavaScript identifier, returns
@@ -1755,6 +1755,10 @@ struct AdditionalKeywords {
 }
   }
 
+  bool isVerilogKeywordSymbol(const FormatToken ) const {
+return Tok.isOneOf(kw_verilogHash, kw_verilogHashHash, kw_apostrophe);
+  }
+
   bool isVerilogWordOperator(const FormatToken ) const {
 return Tok.isOneOf(kw_before, kw_intersect, kw_dist, kw_iff, kw_inside,
kw_with);
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 63629fa743184e..d366ae2080bc25 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4780,9 +4780,14 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine ,
   if (Left.Finalized)
 return Right.hasWhitespaceBefore();
 
+  const bool IsVerilog = Style.isVerilog();
+  assert(!IsVerilog || !IsCpp);
+
   // Never ever merge two words.
-  if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
+  if (Keywords.isWordLike(Right, IsVerilog) &&
+  Keywords.isWordLike(Left, IsVerilog)) {
 return true;
+  }
 
   // Leave a space between * and /* to avoid C4138 `comment end` found outside
   // of comment.
@@ -5063,12 +5068,10 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine ,
 Right.is(TT_TemplateOpener)) {
   return true;
 }
-  } else if (Style.isVerilog()) {
+  } else if (IsVerilog) {
 // An escaped identifier ends with whitespace.
-if (Style.isVerilog() && Left.is(tok::identifier) &&
-Left.TokenText[0] == '\\') {
+if (Left.is(tok::identifier) && Left.TokenText[0] == '\\')
   return true;
-}
 // Add space between things in a primitive's state table unless in a
 // transition like `(0?)`.
 if ((Left.is(TT_VerilogTableItem) &&

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] b4af01b - [clang-format][NFC] Don't repeat Changes[i]/Changes[i - 1]

2024-04-27 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2024-04-27T15:14:20-07:00
New Revision: b4af01bada0c945906d85c364e12aceaf98b0fae

URL: 
https://github.com/llvm/llvm-project/commit/b4af01bada0c945906d85c364e12aceaf98b0fae
DIFF: 
https://github.com/llvm/llvm-project/commit/b4af01bada0c945906d85c364e12aceaf98b0fae.diff

LOG: [clang-format][NFC] Don't repeat Changes[i]/Changes[i - 1]

Added: 


Modified: 
clang/lib/Format/WhitespaceManager.cpp

Removed: 




diff  --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 4f822807dd987d..cc9bcce6c414e0 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -128,11 +128,13 @@ const tooling::Replacements 
::generateReplacements() {
 void WhitespaceManager::calculateLineBreakInformation() {
   Changes[0].PreviousEndOfTokenColumn = 0;
   Change *LastOutsideTokenChange = [0];
-  for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
+  for (unsigned I = 1, e = Changes.size(); I != e; ++I) {
+auto  = Changes[I];
+auto  = Changes[I - 1];
 SourceLocation OriginalWhitespaceStart =
-Changes[i].OriginalWhitespaceRange.getBegin();
+C.OriginalWhitespaceRange.getBegin();
 SourceLocation PreviousOriginalWhitespaceEnd =
-Changes[i - 1].OriginalWhitespaceRange.getEnd();
+P.OriginalWhitespaceRange.getEnd();
 unsigned OriginalWhitespaceStartOffset =
 SourceMgr.getFileOffset(OriginalWhitespaceStart);
 unsigned PreviousOriginalWhitespaceEndOffset =
@@ -167,31 +169,26 @@ void WhitespaceManager::calculateLineBreakInformation() {
 // line of the token.
 auto NewlinePos = Text.find_first_of('\n');
 if (NewlinePos == StringRef::npos) {
-  Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
-   PreviousOriginalWhitespaceEndOffset +
-   Changes[i].PreviousLinePostfix.size() +
-   Changes[i - 1].CurrentLinePrefix.size();
+  P.TokenLength = OriginalWhitespaceStartOffset -
+  PreviousOriginalWhitespaceEndOffset +
+  C.PreviousLinePostfix.size() + 
P.CurrentLinePrefix.size();
 } else {
-  Changes[i - 1].TokenLength =
-  NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
+  P.TokenLength = NewlinePos + P.CurrentLinePrefix.size();
 }
 
 // If there are multiple changes in this token, sum up all the changes 
until
 // the end of the line.
-if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) {
-  LastOutsideTokenChange->TokenLength +=
-  Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
-} else {
-  LastOutsideTokenChange = [i - 1];
-}
+if (P.IsInsideToken && P.NewlinesBefore == 0)
+  LastOutsideTokenChange->TokenLength += P.TokenLength + P.Spaces;
+else
+  LastOutsideTokenChange = 
 
-Changes[i].PreviousEndOfTokenColumn =
-Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
+C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + P.TokenLength;
 
-Changes[i - 1].IsTrailingComment =
-(Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
- (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
-Changes[i - 1].Tok->is(tok::comment) &&
+P.IsTrailingComment =
+(C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
+ (C.IsInsideToken && C.Tok->is(tok::comment))) &&
+P.Tok->is(tok::comment) &&
 // FIXME: This is a dirty hack. The problem is that
 // BreakableLineCommentSection does comment reflow changes and here is
 // the aligning of trailing comments. Consider the case where we reflow



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add a space after a word token only if required (PR #90161)

2024-04-26 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/90161
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-25 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Add a space after a word token only if required (PR #90161)

2024-04-25 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/90161

Fixes #78166.

>From 3f59076142bd6a87f4875dc79e95e4570f4af7c2 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 25 Apr 2024 20:08:03 -0700
Subject: [PATCH] [clang-format] Add a space after a word token only if
 required

Fixes #78166.
---
 clang/lib/Format/TokenAnnotator.cpp   | 26 +++---
 clang/unittests/Format/FormatTest.cpp | 25 +
 2 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index cdfb4256e41d93..63629fa743184e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4834,10 +4834,8 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine ,
 Right.is(TT_TemplateOpener)) {
   return true;
 }
-if (Left.is(tok::identifier) && Right.is(tok::numeric_constant) &&
-Right.TokenText[0] == '.') {
-  return false;
-}
+if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
+  return Right.TokenText[0] != '.';
   } else if (Style.isProto()) {
 if (Right.is(tok::period) &&
 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
@@ -5266,21 +5264,11 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine ,
 return true;
   }
   if (Left.is(TT_UnaryOperator)) {
-if (Right.isNot(tok::l_paren)) {
-  // The alternative operators for ~ and ! are "compl" and "not".
-  // If they are used instead, we do not want to combine them with
-  // the token to the right, unless that is a left paren.
-  if (Left.is(tok::exclaim) && Left.TokenText == "not")
-return true;
-  if (Left.is(tok::tilde) && Left.TokenText == "compl")
-return true;
-  // Lambda captures allow for a lone &, so "&]" needs to be properly
-  // handled.
-  if (Left.is(tok::amp) && Right.is(tok::r_square))
-return Style.SpacesInSquareBrackets;
-}
-return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
-   Right.is(TT_BinaryOperator);
+// Lambda captures allow for a lone &, so "&]" needs to be properly
+// handled.
+if (Left.is(tok::amp) && Right.is(tok::r_square))
+  return Style.SpacesInSquareBrackets;
+return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
   }
 
   // If the next token is a binary operator or a selector name, we have
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index bc61b9c089e922..8ecc1188a127a5 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24507,16 +24507,25 @@ TEST_F(FormatTest, AlternativeOperators) {
   verifyFormat("int a compl(5);");
   verifyFormat("int a not(5);");
 
-  /* FIXME handle alternate tokens
-   * https://en.cppreference.com/w/cpp/language/operator_alternative
-  // alternative tokens
-  verifyFormat("compl foo();"); //  ~foo();
-  verifyFormat("foo() <%%>;");  // foo();
-  verifyFormat("void foo() <%%>;"); // void foo(){}
-  verifyFormat("int a <:1:>;"); // int a[1];[
+  verifyFormat("compl foo();"); // ~foo();
+  verifyFormat("foo() <%%>");   // foo() {}
+  verifyFormat("void foo() <%%>");  // void foo() {}
+  verifyFormat("int a<:1:>;");  // int a[1];
   verifyFormat("%:define ABC abc"); // #define ABC abc
   verifyFormat("%:%:"); // ##
-  */
+
+  verifyFormat("a = v(not;);\n"
+   "b = v(not+);\n"
+   "c = v(not x);\n"
+   "d = v(not 1);\n"
+   "e = v(not 123.f);");
+
+  verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V)  \\\n"
+ "  V(and)   \\\n"
+ "  V(not)   \\\n"
+ "  V(not!)  \\\n"
+ "  V(other)",
+ getLLVMStyleWithColumns(40));
 }
 
 TEST_F(FormatTest, STLWhileNotDefineChed) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] adds a space after not inside macros (PR #78176)

2024-04-25 Thread Owen Pan via cfe-commits

https://github.com/owenca unassigned 
https://github.com/llvm/llvm-project/pull/78176
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From 6c184f9714c94af94c7692e1264061b8dc14e912 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/3] [clang-format] Do not update cursor pos if no includes
 replacement

Signed-off-by: NorthBlue333 
---
 clang/lib/Format/Format.cpp |   3 +
 clang/unittests/Format/SortIncludesTest.cpp | 119 +++-
 2 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..e12ad2ced38285 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle ,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle ,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+*Cursor = OldCursor;
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 772eb53806b4b1..791ab7bb185ed9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ TEST_F(SortIncludesTest, 
CalculatesCorrectCursorPositionWithRegrouping) {
   EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
 }
 
+TEST_F(SortIncludesTest,
+   CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) 
{
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#include \"b\"\r\n" // Start of line: 14
+ "#include \"c\"\r\n" // Start of line: 28
+ "\r\n"   // Start of line: 42
+ "int i;";// Start of line: 44
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(
+  14u,
+  newCursor(Code, 14)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(14u, newCursor(Code, 16));
+  EXPECT_EQ(
+  30u,
+  newCursor(Code, 30)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(28u, newCursor(Code, 32));
+  EXPECT_EQ(42u, newCursor(Code, 46));
+  EXPECT_EQ(44u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From 6c184f9714c94af94c7692e1264061b8dc14e912 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/2] [clang-format] Do not update cursor pos if no includes
 replacement

Signed-off-by: NorthBlue333 
---
 clang/lib/Format/Format.cpp |   3 +
 clang/unittests/Format/SortIncludesTest.cpp | 119 +++-
 2 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..e12ad2ced38285 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle ,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle ,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+*Cursor = OldCursor;
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 772eb53806b4b1..791ab7bb185ed9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ TEST_F(SortIncludesTest, 
CalculatesCorrectCursorPositionWithRegrouping) {
   EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
 }
 
+TEST_F(SortIncludesTest,
+   CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) 
{
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#include \"b\"\r\n" // Start of line: 14
+ "#include \"c\"\r\n" // Start of line: 28
+ "\r\n"   // Start of line: 42
+ "int i;";// Start of line: 44
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(
+  14u,
+  newCursor(Code, 14)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(14u, newCursor(Code, 16));
+  EXPECT_EQ(
+  30u,
+  newCursor(Code, 30)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(28u, newCursor(Code, 32));
+  EXPECT_EQ(42u, newCursor(Code, 46));
+  EXPECT_EQ(44u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

owenca wrote:

> I have squashed the commits in only one. Note that I have left the failing 
> tests in the commit, I am not sure if I should remove them or not.

Unfortunately, this wiped out my updates that fixed a formatting error and 
added `#if 0` for the failing tests.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Annotate enum braces as BK_Block (PR #89871)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/89871
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Remove YAML hack to emit a BasedOnStyle comment (PR #89228)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/89228
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Annotate enum braces as BK_Block (PR #89871)

2024-04-23 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/89871

Fixes #89759.

>From dfd275f3d2f89e2cabffcc0d573f2b4571c0ead4 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Tue, 23 Apr 2024 21:59:43 -0700
Subject: [PATCH] [clang-format] Annotate enum braces as BK_Block

Fixes #89759.
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 24 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 17 +
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 6e4e6901e473f7..3a263955a6a8fe 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -534,11 +534,11 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 case tok::r_brace:
   if (LBraceStack.empty())
 break;
-  if (LBraceStack.back().Tok->is(BK_Unknown)) {
+  if (auto *LBrace = LBraceStack.back().Tok; LBrace->is(BK_Unknown)) {
 bool ProbablyBracedList = false;
 if (Style.Language == FormatStyle::LK_Proto) {
   ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
-} else {
+} else if (LBrace->isNot(TT_EnumLBrace)) {
   // Using OriginalColumn to distinguish between ObjC methods and
   // binary operators is a bit hacky.
   bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -552,7 +552,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 
   // If we already marked the opening brace as braced list, the closing
   // must also be part of it.
-  ProbablyBracedList = LBraceStack.back().Tok->is(TT_BracedListLBrace);
+  ProbablyBracedList = LBrace->is(TT_BracedListLBrace);
 
   ProbablyBracedList = ProbablyBracedList ||
(Style.isJavaScript() &&
@@ -608,13 +608,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 ProbablyBracedList = true;
   }
 }
-if (ProbablyBracedList) {
-  Tok->setBlockKind(BK_BracedInit);
-  LBraceStack.back().Tok->setBlockKind(BK_BracedInit);
-} else {
-  Tok->setBlockKind(BK_Block);
-  LBraceStack.back().Tok->setBlockKind(BK_Block);
-}
+const auto BlockKind = ProbablyBracedList ? BK_BracedInit : BK_Block;
+Tok->setBlockKind(BlockKind);
+LBrace->setBlockKind(BlockKind);
   }
   LBraceStack.pop_back();
   break;
@@ -2418,6 +2414,7 @@ bool UnwrappedLineParser::tryToParseChildBlock() {
 }
 
 bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
+  assert(!IsAngleBracket || !IsEnum);
   bool HasError = false;
 
   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
@@ -2440,8 +2437,11 @@ bool UnwrappedLineParser::parseBracedList(bool 
IsAngleBracket, bool IsEnum) {
   }
 }
 if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
-  if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
-addUnwrappedLine();
+  if (IsEnum) {
+FormatTok->setBlockKind(BK_Block);
+if (!Style.AllowShortEnumsOnASingleLine)
+  addUnwrappedLine();
+  }
   nextToken();
   return !HasError;
 }
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 6b8ab441cb46f8..dff5251d2e9406 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2915,6 +2915,23 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
   EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
   EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
+
+  Tokens = annotate("#ifdef DEBUG_ENABLED\n"
+"#else\n"
+"#endif\n"
+"class RenderingServer : Object {\n"
+"#ifndef DISABLE_DEPRECATED\n"
+"  enum Features {\n"
+"FEATURE_SHADERS,\n"
+"FEATURE_MULTITHREADED,\n"
+"  };\n"
+"#endif\n"
+"};");
+  ASSERT_EQ(Tokens.size(), 29u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[11], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[17], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[22], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[26], BK_Block);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Correctly annotate list init braces of class types (PR #89706)

2024-04-23 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/89706
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Correctly annotate list init braces of class types (PR #89706)

2024-04-22 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/89706

Fixes #71939.

>From 0fab05c4ab7ac95d7c08dd23a0441dc28c2ef813 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Mon, 22 Apr 2024 21:56:31 -0700
Subject: [PATCH] [clang-format] Correctly annotate list init braces of class
 types

Fixes #71939.
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 41 +++--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 60 +++
 2 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 603268f771ac52..6e4e6901e473f7 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -819,8 +819,11 @@ FormatToken *UnwrappedLineParser::parseBlock(bool 
MustBeDeclaration,
 return IfLBrace;
   }
 
-  if (FormatTok->is(tok::r_brace) && Tok->is(TT_NamespaceLBrace))
-FormatTok->setFinalizedType(TT_NamespaceRBrace);
+  if (FormatTok->is(tok::r_brace)) {
+FormatTok->setBlockKind(BK_Block);
+if (Tok->is(TT_NamespaceLBrace))
+  FormatTok->setFinalizedType(TT_NamespaceRBrace);
+  }
 
   const bool IsFunctionRBrace =
   FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
@@ -3910,6 +3913,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   const FormatToken  = *FormatTok;
   nextToken();
 
+  const FormatToken *ClassName = nullptr;
+  bool IsDerived = false;
   auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
 return Tok->is(tok::identifier) && Tok->TokenText != 
Tok->TokenText.upper();
   };
@@ -3934,15 +3939,35 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) 
{
 }
 if (FormatTok->is(tok::l_square) && handleCppAttributes())
   continue;
+const auto *Previous = FormatTok;
 nextToken();
-// We can have macros in between 'class' and the class name.
-if (!IsNonMacroIdentifier(FormatTok->Previous) &&
-FormatTok->is(tok::l_paren)) {
-  parseParens();
+switch (FormatTok->Tok.getKind()) {
+case tok::l_paren:
+  // We can have macros in between 'class' and the class name.
+  if (!IsNonMacroIdentifier(Previous))
+parseParens();
+  break;
+case tok::coloncolon:
+  break;
+default:
+  if (!ClassName && Previous->is(tok::identifier))
+ClassName = Previous;
 }
   }
 
+  auto IsListInitialization = [&] {
+if (!ClassName || IsDerived)
+  return false;
+assert(FormatTok->is(tok::l_brace));
+const auto *Prev = FormatTok->getPreviousNonComment();
+assert(Prev);
+return Prev != ClassName && Prev->is(tok::identifier) &&
+   Prev->isNot(Keywords.kw_final) && tryToParseBracedList();
+  };
+
   if (FormatTok->isOneOf(tok::colon, tok::less)) {
+if (FormatTok->is(tok::colon))
+  IsDerived = true;
 int AngleNestingLevel = 0;
 do {
   if (FormatTok->is(tok::less))
@@ -3955,6 +3980,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
 break;
   }
   if (FormatTok->is(tok::l_brace)) {
+if (AngleNestingLevel == 0 && IsListInitialization())
+  return;
 calculateBraceTypes(/*ExpectClassBody=*/true);
 if (!tryToParseBracedList())
   break;
@@ -3999,6 +4026,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
 }
   };
   if (FormatTok->is(tok::l_brace)) {
+if (IsListInitialization())
+  return;
 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
 FormatTok->setFinalizedType(OpenBraceType);
 if (ParseAsExpr) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 34999b7376397b..6b8ab441cb46f8 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2855,6 +2855,66 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   ASSERT_EQ(Tokens.size(), 18u) << Tokens;
   EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
   EXPECT_BRACE_KIND(Tokens[16], BK_BracedInit);
+
+  Tokens = annotate("struct {};");
+  ASSERT_EQ(Tokens.size(), 5u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[1], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[2], BK_Block);
+
+  Tokens = annotate("struct : Base {};");
+  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[3], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[4], BK_Block);
+
+  Tokens = annotate("struct Foo {};");
+  ASSERT_EQ(Tokens.size(), 6u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[2], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[3], BK_Block);
+
+  Tokens = annotate("struct ::Foo {};");
+  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[3], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[4], BK_Block);
+
+  Tokens = annotate("struct NS::Foo {};");
+  ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[4], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[5], BK_Block);
+
+  Tokens = annotate("struct Foo {};");
+  

[clang] [clang-format] Fix a bug in annotating CastRParen before unary && (PR #89346)

2024-04-19 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/89346
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [polly] [clang-format] Revert breaking stream operators to previous default (PR #89016)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick 29ecd6d50f14

https://github.com/llvm/llvm-project/pull/89016
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [polly] [clang-format] Revert breaking stream operators to previous default (PR #89016)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

Yep.

https://github.com/llvm/llvm-project/pull/89016
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating TrailingReturnArrow (PR #86624)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick a7f4576ff4e2

https://github.com/llvm/llvm-project/pull/86624
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in ContinuationIndenter (PR #88414)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick d61edecbfd09

https://github.com/llvm/llvm-project/pull/88414
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a regression in annotating BK_BracedInit (PR #87450)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick 7c9c38eaa9b7

https://github.com/llvm/llvm-project/pull/87450
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [polly] [clang-format] Correctly annotate braces in macros (PR #87953)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

/cherry-pick 58323de2e5ed

https://github.com/llvm/llvm-project/pull/87953
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [polly] [clang-format] Correctly annotate braces in macros (PR #87953)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

Ah, I still need to do that.

https://github.com/llvm/llvm-project/pull/87953
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [polly] [clang-format] Correctly annotate braces in macros (PR #87953)

2024-04-19 Thread Owen Pan via cfe-commits

owenca wrote:

@tstellar somehow this is not in 18.1.4, but the LLVM Release Status in the 
Projects box says "Status: Done". Did I miss something here?

https://github.com/llvm/llvm-project/pull/87953
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   6   7   8   9   10   >