[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

2024-05-31 Thread Owen Pan via llvm-branch-commits

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

…ing literals (#92214)

>From 88711473d2affcb21703e7b4f78420c512192acf Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 31 May 2024 20:13:27 -0700
Subject: [PATCH] release/18.x: [clang-format] Don't always break before <<
 between string literals (#92214)

---
 clang/lib/Format/TokenAnnotator.cpp   |  8 +---
 clang/unittests/Format/FormatTest.cpp | 11 +++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index c1f1662481922..628fe46cc348e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5159,9 +5159,11 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
&Line,
 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)) {
-return true;
+  if (const auto *BeforeLeft = Left.Previous, *AfterRight = Right.Next;
+  BeforeLeft && BeforeLeft->is(tok::lessless) &&
+  Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
+  AfterRight->is(tok::string_literal)) {
+return Right.NewlinesBefore > 0;
   }
   if (Right.is(TT_RequiresClause)) {
 switch (Style.RequiresClausePosition) {
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 0161a1685eb12..d69632f7f0f8c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10426,6 +10426,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"

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

2024-05-31 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

2024-06-01 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar can we also get this one into 18.1.7?

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a bug in formatting goto labels in macros (#92494) (PR #93494)

2024-06-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar this is a regression fix as labeled by the issue the original 
pr/commit fixed. :)

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a bug in formatting goto labels in macros (#92494) (PR #93494)

2024-06-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar

> 1. How risky is this fix?

Very low IMO. (@mydeveloperday @HazardyKnusperkeks @rymiel can chime in if 
anyone thinks otherwise.)

> 2. When was the last known working version of clang-format?

17.0.6 (regressed since 18.1.1).

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

2024-06-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar strictly speaking, this is a regression fix. (It's a churn within 
18.x release cycle.)

1. Risk level: low IMO. (The other code owner @mydeveloperday approved it to be 
included in 18.1.7.)
2. Last known working version: 18.1.4

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

2024-06-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar thanks!

> @owenca Do you have a release note we can put in the release announcement?

Release note:
Fixes clang-format (since 18.1.1) regressions on breaking before a stream 
insertion operator (<<) when both operands are string literals.

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a bug in formatting goto labels in macros (#92494) (PR #93494)

2024-06-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> @owenca Do you have a release note we can put in the release announcement?

Release note:
Fixes a clang-format regression (since 17.0.6) on formatting `goto` labels in 
macro definitions.

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


[llvm-branch-commits] [clang] release/19.x: Revert "[clang-format] Fix a bug in annotating `*` in `#define`s (#99433)" (PR #100499)

2024-07-27 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#79549 (PR #80591)

2024-02-04 Thread Owen Pan via llvm-branch-commits

owenca wrote:

Replaced with #80593.

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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#79549 (PR #80591)

2024-02-04 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#78892 (PR #80259)

2024-02-04 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#78892 (PR #80259)

2024-02-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar can you move this PR to Needs Merge? Thanks!

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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#78892 (PR #80259)

2024-02-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> As stated in the discussion, it is an absolutely must to merge it in the 
> release. In my opinion we can't just drop an option, for the next release.

Yep! See 
https://github.com/llvm/llvm-project/issues/78892#issuecomment-1926009360.

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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#78965 (PR #80593)

2024-02-06 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#81104 (PR #81106)

2024-02-08 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] PR for llvm/llvm-project#80628 (PR #81096)

2024-02-08 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Correctly annotate braces of empty ctors/dtors (#82097) (PR #87735)

2024-04-16 Thread Owen Pan via llvm-branch-commits

owenca wrote:

This is just a bug fix 
[requested](https://github.com/llvm/llvm-project/issues/79834#issuecomment-2030179647)
 to be cherry picked, so I don't think we need to add a note about it in the 
release note.

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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Correctly annotate braces of empty ctors/dtors (#82097) (PR #87735)

2024-04-18 Thread Owen Pan via llvm-branch-commits

owenca wrote:

The only bug fix for clang-format is in the change log section of 18.1.4 
release notes, so I'm not sure if it's useful to repeat it at the top of the 
release notes. For 18.1.5, there will be a reversion of behavior from 18.1.1. 
I'll definitely add that to the release notes.

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in annotating BK_Braced… (PR #89489)

2024-04-19 Thread Owen Pan via llvm-branch-commits

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

…Init (#87450)

Backport 7c9c38eaa9b7

>From 497357a593a65534691091ef155e80d43d51d9c9 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 19 Apr 2024 21:59:29 -0700
Subject: [PATCH] release/18.x: [clang-format] Fix a regression in annotating
 BK_BracedInit (#87450)

Backport 7c9c38eaa9b7
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 20 ---
 clang/unittests/Format/FormatTest.cpp | 12 +++
 clang/unittests/Format/TokenAnnotatorTest.cpp | 13 
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 57391979887078..b7d970f45271da 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -489,18 +489,23 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
   };
   SmallVector LBraceStack;
   assert(Tok->is(tok::l_brace));
+
   do {
-// Get next non-comment, non-preprocessor token.
 FormatToken *NextTok;
 do {
   NextTok = Tokens->getNextToken();
 } while (NextTok->is(tok::comment));
-while (NextTok->is(tok::hash) && !Line->InMacroBody) {
-  NextTok = Tokens->getNextToken();
-  do {
-NextTok = Tokens->getNextToken();
-  } while (NextTok->is(tok::comment) ||
-   (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof)));
+
+if (!Line->InMacroBody) {
+  // Skip PPDirective lines and comments.
+  while (NextTok->is(tok::hash)) {
+do {
+  NextTok = Tokens->getNextToken();
+} while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
+
+while (NextTok->is(tok::comment))
+  NextTok = Tokens->getNextToken();
+  }
 }
 
 switch (Tok->Tok.getKind()) {
@@ -631,6 +636,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 default:
   break;
 }
+
 PrevTok = Tok;
 Tok = NextTok;
   } while (Tok->isNot(tok::eof) && !LBraceStack.empty());
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 0beba12dda62ae..6b4f9075d8f0ab 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27019,6 +27019,18 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {
"};");
 }
 
+TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
+  verifyFormat("{\n"
+   "  char *a[] = {\n"
+   "  /* abc */ \"abc\",\n"
+   "#if FOO\n"
+   "  /* xyz */ \"xyz\",\n"
+   "#endif\n"
+   "  /* last */ \"last\"};\n"
+   "}",
+   getLLVMStyleWithColumns(30));
+}
+
 TEST_F(FormatTest, StreamOutputOperator) {
   verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
   verifyFormat("std::cout << \"foo\\n\"\n"
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c72c9384ff91d6..f767b83a875471 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2609,6 +2609,19 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
   EXPECT_BRACE_KIND(Tokens[13], BK_Block);
   EXPECT_BRACE_KIND(Tokens[14], BK_Block);
+
+  Tokens = annotate("{\n"
+"  char *a[] = {\n"
+"  /* abc */ \"abc\",\n"
+"#if FOO\n"
+"  /* xyz */ \"xyz\",\n"
+"#endif\n"
+"  /* last */ \"last\"};\n"
+"}");
+  ASSERT_EQ(Tokens.size(), 25u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[0], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
 }
 
 TEST_F(TokenAnnotatorTest, StreamOperator) {

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in annotating BK_Braced… (PR #89489)

2024-04-19 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Correctly annotate braces in macros (#87… (PR #89491)

2024-04-19 Thread Owen Pan via llvm-branch-commits

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

…953)

Backport 58323de2e5ed

>From 042f92bcdaed5b4ba1fceddb0c2187df76b51006 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 19 Apr 2024 23:20:47 -0700
Subject: [PATCH] release/18.x: [clang-format] Correctly annotate braces in
 macros (#87953)

Backport 58323de2e5ed
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 20 +--
 clang/unittests/Format/FormatTest.cpp |  9 -
 clang/unittests/Format/TokenAnnotatorTest.cpp | 14 +
 .../lib/Analysis/ScopDetectionDiagnostic.cpp  |  2 +-
 4 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 57391979887078..e3afe78738e2c7 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -534,16 +534,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 if (Style.Language == FormatStyle::LK_Proto) {
   ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
 } else {
-  // Skip NextTok over preprocessor lines, otherwise we may not
-  // properly diagnose the block as a braced intializer
-  // if the comma separator appears after the pp directive.
-  while (NextTok->is(tok::hash)) {
-ScopedMacroState MacroState(*Line, Tokens, NextTok);
-do {
-  NextTok = Tokens->getNextToken();
-} while (NextTok->isNot(tok::eof));
-  }
-
   // Using OriginalColumn to distinguish between ObjC methods and
   // binary operators is a bit hacky.
   bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -602,6 +592,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 NextTok = Tokens->getNextToken();
 ProbablyBracedList = NextTok->isNot(tok::l_square);
   }
+
+  // Cpp macro definition body that is a nonempty braced list or block:
+  if (Style.isCpp() && Line->InMacroBody && PrevTok != FormatTok &&
+  !FormatTok->Previous && NextTok->is(tok::eof) &&
+  // A statement can end with only `;` (simple statement), a block
+  // closing brace (compound statement), or `:` (label statement).
+  // If PrevTok is a block opening brace, Tok ends an empty block.
+  !PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
+ProbablyBracedList = true;
+  }
 }
 if (ProbablyBracedList) {
   Tok->setBlockKind(BK_BracedInit);
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 0beba12dda62ae..5a64e535b4a85f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1865,6 +1865,13 @@ TEST_F(FormatTest, UnderstandsMacros) {
   verifyFormat("MACRO(co_return##something)");
 
   verifyFormat("#define A x:");
+
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { \\\n"
+  "#Bar \\\n"
+  "  }");
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { #Bar }");
 }
 
 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
@@ -10865,7 +10872,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
   verifyFormat("some_templated_type");
 
   verifyFormat("#define FOO(typeName, realClass)   
\\\n"
-   "  { #typeName, foo(new foo(#typeName)) }",
+   "  {#typeName, foo(new foo(#typeName))}",
getLLVMStyleWithColumns(60));
 }
 
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c72c9384ff91d6..528abc414f7ed3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1885,14 +1885,20 @@ TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
  "#Bar \\\n"
  "  }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
 
   Tokens = annotate("#define Foo(Bar) \\\n"
 "  { #Bar }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
+
+  Tokens = annotate("#define FOO(typeName, realClass) \\\n"
+"  {#typeName, foo(new foo(#typeName))}");
+  ASSERT_EQ(Tokens.size(), 29u) << Tokens;
+  EXPECT_BRACE_KIND(To

[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Correctly annotate braces in macros (#87… (PR #89491)

2024-04-19 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Revert breaking stream operators to prev… (PR #89492)

2024-04-20 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Revert breaking stream operators to prev… (PR #89492)

2024-04-20 Thread Owen Pan via llvm-branch-commits

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

…ious default (#89016)

Backport 29ecd6d50f14

>From 7fd5c11287f10fa94f58b4c7aee293d7df17b498 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 19 Apr 2024 23:58:35 -0700
Subject: [PATCH] release/18.x: [clang-format] Revert breaking stream operators
 to previous default (#89016)

Backport 29ecd6d50f14
---
 clang/lib/Format/TokenAnnotator.cpp   | 8 ++--
 clang/unittests/Format/FormatTest.cpp | 7 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 9 -
 polly/lib/Analysis/DependenceInfo.cpp | 4 ++--
 polly/lib/Analysis/ScopBuilder.cpp| 7 ---
 5 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 4d482e6543d6f5..9c4a8381f99824 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5157,12 +5157,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
&Line,
 return true;
   if (Left.IsUnterminatedLiteral)
 return true;
-  // FIXME: Breaking after newlines seems useful in general. Turn this into an
-  // option and recognize more cases like endl etc, and break independent of
-  // what comes after operator lessless.
-  if (Right.is(tok::lessless) && Right.Next &&
-  Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
-  Left.TokenText.ends_with("\\n\"")) {
+  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
+  Right.Next->is(tok::string_literal)) {
 return true;
   }
   if (Right.is(TT_RequiresClause)) {
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 0beba12dda62ae..816e58bd976db1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27019,12 +27019,6 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {
"};");
 }
 
-TEST_F(FormatTest, StreamOutputOperator) {
-  verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
-  verifyFormat("std::cout << \"foo\\n\"\n"
-   "  << \"bar\";");
-}
-
 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   constexpr StringRef Code{
   "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
@@ -27039,6 +27033,7 @@ TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   Style.BreakAdjacentStringLiterals = false;
   verifyFormat(Code, Style);
 }
+
 } // namespace
 } // namespace test
 } // namespace format
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c72c9384ff91d6..dea94a5c6b8356 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2611,15 +2611,6 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[14], BK_Block);
 }
 
-TEST_F(TokenAnnotatorTest, StreamOperator) {
-  auto Tokens = annotate("\"foo\\n\" << aux << \"foo\\n\" << \"foo\";");
-  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
-  EXPECT_FALSE(Tokens[1]->MustBreakBefore);
-  EXPECT_FALSE(Tokens[3]->MustBreakBefore);
-  // Only break between string literals if the former ends with \n.
-  EXPECT_TRUE(Tokens[5]->MustBreakBefore);
-}
-
 } // namespace
 } // namespace format
 } // namespace clang
diff --git a/polly/lib/Analysis/DependenceInfo.cpp 
b/polly/lib/Analysis/DependenceInfo.cpp
index 69257c603877ea..d58dc9917bc91f 100644
--- a/polly/lib/Analysis/DependenceInfo.cpp
+++ b/polly/lib/Analysis/DependenceInfo.cpp
@@ -950,8 +950,8 @@ class DependenceInfoPrinterLegacyPass final : public 
ScopPass {
   bool runOnScop(Scop &S) override {
 DependenceInfo &P = getAnalysis();
 
-OS << "Printing analysis '" << P.getPassName() << "' for " << "region: '"
-   << S.getRegion().getNameStr() << "' in function '"
+OS << "Printing analysis '" << P.getPassName() << "' for "
+   << "region: '" << S.getRegion().getNameStr() << "' in function '"
<< S.getFunction().getName() << "':\n";
 P.printScop(OS, S);
 
diff --git a/polly/lib/Analysis/ScopBuilder.cpp 
b/polly/lib/Analysis/ScopBuilder.cpp
index c62cb2a85c835c..64314d6041b8e9 100644
--- a/polly/lib/Analysis/ScopBuilder.cpp
+++ b/polly/lib/Analysis/ScopBuilder.cpp
@@ -2689,9 +2689,10 @@ void ScopBuilder::addUserContext() {
 if (NameContext != NameUserContext) {
   std::string SpaceStr = stringFromIslObj(Space, "null");
   errs() << "Error: the name of dimension " << i
- << " provided in -polly-context " << "is '" << NameUserContext
- << "', but the name in the computed " << "context is '"
- << NameContext << "'. Due to this name mismatch, "
+ << " provided in -polly-context "
+ << "is '" << NameUserContext << "', but the name in the computed "
+ << "context is '" << NameContext
+ << "'. Due to this name mismatch, "
  << "the -pol

[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in ContinuationIndenter (#88414) (PR #89412)

2024-04-22 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in annotating TrailingReturnArrow (#86624) (PR #89415)

2024-04-22 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Revert breaking stream operators to prev… (PR #89492)

2024-04-24 Thread Owen Pan via llvm-branch-commits

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

>From c0b48372d82aa0adaef671788ee1a37a650ee4fd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 19 Apr 2024 23:58:35 -0700
Subject: [PATCH] release/18.x: [clang-format] Revert breaking stream operators
 to previous default (#89016)

Backport 29ecd6d50f14
---
 clang/lib/Format/TokenAnnotator.cpp   | 8 ++--
 clang/unittests/Format/FormatTest.cpp | 7 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 9 -
 polly/lib/Analysis/DependenceInfo.cpp | 4 ++--
 polly/lib/Analysis/ScopBuilder.cpp| 7 ---
 5 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 4d482e6543d6f5..9c4a8381f99824 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5157,12 +5157,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
&Line,
 return true;
   if (Left.IsUnterminatedLiteral)
 return true;
-  // FIXME: Breaking after newlines seems useful in general. Turn this into an
-  // option and recognize more cases like endl etc, and break independent of
-  // what comes after operator lessless.
-  if (Right.is(tok::lessless) && Right.Next &&
-  Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
-  Left.TokenText.ends_with("\\n\"")) {
+  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
+  Right.Next->is(tok::string_literal)) {
 return true;
   }
   if (Right.is(TT_RequiresClause)) {
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 6b4f9075d8f0ab..2ac0b0aab1bee9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27031,12 +27031,6 @@ TEST_F(FormatTest, 
PPDirectivesAndCommentsInBracedInit) {
getLLVMStyleWithColumns(30));
 }
 
-TEST_F(FormatTest, StreamOutputOperator) {
-  verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
-  verifyFormat("std::cout << \"foo\\n\"\n"
-   "  << \"bar\";");
-}
-
 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   constexpr StringRef Code{
   "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
@@ -27051,6 +27045,7 @@ TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   Style.BreakAdjacentStringLiterals = false;
   verifyFormat(Code, Style);
 }
+
 } // namespace
 } // namespace test
 } // namespace format
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f767b83a875471..cbe6b79a366882 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2624,15 +2624,6 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
 }
 
-TEST_F(TokenAnnotatorTest, StreamOperator) {
-  auto Tokens = annotate("\"foo\\n\" << aux << \"foo\\n\" << \"foo\";");
-  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
-  EXPECT_FALSE(Tokens[1]->MustBreakBefore);
-  EXPECT_FALSE(Tokens[3]->MustBreakBefore);
-  // Only break between string literals if the former ends with \n.
-  EXPECT_TRUE(Tokens[5]->MustBreakBefore);
-}
-
 } // namespace
 } // namespace format
 } // namespace clang
diff --git a/polly/lib/Analysis/DependenceInfo.cpp 
b/polly/lib/Analysis/DependenceInfo.cpp
index 69257c603877ea..d58dc9917bc91f 100644
--- a/polly/lib/Analysis/DependenceInfo.cpp
+++ b/polly/lib/Analysis/DependenceInfo.cpp
@@ -950,8 +950,8 @@ class DependenceInfoPrinterLegacyPass final : public 
ScopPass {
   bool runOnScop(Scop &S) override {
 DependenceInfo &P = getAnalysis();
 
-OS << "Printing analysis '" << P.getPassName() << "' for " << "region: '"
-   << S.getRegion().getNameStr() << "' in function '"
+OS << "Printing analysis '" << P.getPassName() << "' for "
+   << "region: '" << S.getRegion().getNameStr() << "' in function '"
<< S.getFunction().getName() << "':\n";
 P.printScop(OS, S);
 
diff --git a/polly/lib/Analysis/ScopBuilder.cpp 
b/polly/lib/Analysis/ScopBuilder.cpp
index c62cb2a85c835c..64314d6041b8e9 100644
--- a/polly/lib/Analysis/ScopBuilder.cpp
+++ b/polly/lib/Analysis/ScopBuilder.cpp
@@ -2689,9 +2689,10 @@ void ScopBuilder::addUserContext() {
 if (NameContext != NameUserContext) {
   std::string SpaceStr = stringFromIslObj(Space, "null");
   errs() << "Error: the name of dimension " << i
- << " provided in -polly-context " << "is '" << NameUserContext
- << "', but the name in the computed " << "context is '"
- << NameContext << "'. Due to this name mismatch, "
+ << " provided in -polly-context "
+ << "is '" << NameUserContext << "', but the name in the computed "
+ << "context is '" << NameContext
+ << "'. Due to this name mismatch, "
  << "the -polly-co

[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Revert breaking stream operators to prev… (PR #89492)

2024-04-24 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> @owenca Can you rebase this branch?

@tstellar I've rebased it.

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


[llvm-branch-commits] [clang] c0b4837 - release/18.x: [clang-format] Revert breaking stream operators to previous default (#89016)

2024-04-25 Thread Owen Pan via llvm-branch-commits

Author: Owen Pan
Date: 2024-04-24T21:29:46-07:00
New Revision: c0b48372d82aa0adaef671788ee1a37a650ee4fd

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

LOG: release/18.x: [clang-format] Revert breaking stream operators to previous 
default (#89016)

Backport 29ecd6d50f14

Added: 


Modified: 
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
polly/lib/Analysis/DependenceInfo.cpp
polly/lib/Analysis/ScopBuilder.cpp

Removed: 




diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 4d482e6543d6f5..9c4a8381f99824 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5157,12 +5157,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine 
&Line,
 return true;
   if (Left.IsUnterminatedLiteral)
 return true;
-  // FIXME: Breaking after newlines seems useful in general. Turn this into an
-  // option and recognize more cases like endl etc, and break independent of
-  // what comes after operator lessless.
-  if (Right.is(tok::lessless) && Right.Next &&
-  Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
-  Left.TokenText.ends_with("\\n\"")) {
+  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
+  Right.Next->is(tok::string_literal)) {
 return true;
   }
   if (Right.is(TT_RequiresClause)) {

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 6b4f9075d8f0ab..2ac0b0aab1bee9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27031,12 +27031,6 @@ TEST_F(FormatTest, 
PPDirectivesAndCommentsInBracedInit) {
getLLVMStyleWithColumns(30));
 }
 
-TEST_F(FormatTest, StreamOutputOperator) {
-  verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
-  verifyFormat("std::cout << \"foo\\n\"\n"
-   "  << \"bar\";");
-}
-
 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   constexpr StringRef Code{
   "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
@@ -27051,6 +27045,7 @@ TEST_F(FormatTest, BreakAdjacentStringLiterals) {
   Style.BreakAdjacentStringLiterals = false;
   verifyFormat(Code, Style);
 }
+
 } // namespace
 } // namespace test
 } // namespace format

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f767b83a875471..cbe6b79a366882 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2624,15 +2624,6 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
 }
 
-TEST_F(TokenAnnotatorTest, StreamOperator) {
-  auto Tokens = annotate("\"foo\\n\" << aux << \"foo\\n\" << \"foo\";");
-  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
-  EXPECT_FALSE(Tokens[1]->MustBreakBefore);
-  EXPECT_FALSE(Tokens[3]->MustBreakBefore);
-  // Only break between string literals if the former ends with \n.
-  EXPECT_TRUE(Tokens[5]->MustBreakBefore);
-}
-
 } // namespace
 } // namespace format
 } // namespace clang

diff  --git a/polly/lib/Analysis/DependenceInfo.cpp 
b/polly/lib/Analysis/DependenceInfo.cpp
index 69257c603877ea..d58dc9917bc91f 100644
--- a/polly/lib/Analysis/DependenceInfo.cpp
+++ b/polly/lib/Analysis/DependenceInfo.cpp
@@ -950,8 +950,8 @@ class DependenceInfoPrinterLegacyPass final : public 
ScopPass {
   bool runOnScop(Scop &S) override {
 DependenceInfo &P = getAnalysis();
 
-OS << "Printing analysis '" << P.getPassName() << "' for " << "region: '"
-   << S.getRegion().getNameStr() << "' in function '"
+OS << "Printing analysis '" << P.getPassName() << "' for "
+   << "region: '" << S.getRegion().getNameStr() << "' in function '"
<< S.getFunction().getName() << "':\n";
 P.printScop(OS, S);
 

diff  --git a/polly/lib/Analysis/ScopBuilder.cpp 
b/polly/lib/Analysis/ScopBuilder.cpp
index c62cb2a85c835c..64314d6041b8e9 100644
--- a/polly/lib/Analysis/ScopBuilder.cpp
+++ b/polly/lib/Analysis/ScopBuilder.cpp
@@ -2689,9 +2689,10 @@ void ScopBuilder::addUserContext() {
 if (NameContext != NameUserContext) {
   std::string SpaceStr = stringFromIslObj(Space, "null");
   errs() << "Error: the name of dimension " << i
- << " provided in -polly-context " << "is '" << NameUserContext
- << "', but the name in the computed " << "context is '"
- << NameContext << "'. Due to this name mismatch, "
+ << " provided in -polly-context "
+ << "is '" << NameUserContext << "', but the name in the computed "
+ << "context is '" << NameContext
+

[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Correctly annotate braces in macros (#87… (PR #89491)

2024-04-25 Thread Owen Pan via llvm-branch-commits

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

>From 35fea1032741526f76d7fea49794061bf3a1d3ee Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 19 Apr 2024 23:20:47 -0700
Subject: [PATCH] release/18.x: [clang-format] Correctly annotate braces in
 macros (#87953)

Backport 58323de2e5ed
---
 clang/lib/Format/UnwrappedLineParser.cpp  | 20 +--
 clang/unittests/Format/FormatTest.cpp |  9 -
 clang/unittests/Format/TokenAnnotatorTest.cpp | 14 +
 .../lib/Analysis/ScopDetectionDiagnostic.cpp  |  2 +-
 4 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b7d970f45271da..a6eb18bb2b3227 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -539,16 +539,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 if (Style.Language == FormatStyle::LK_Proto) {
   ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
 } else {
-  // Skip NextTok over preprocessor lines, otherwise we may not
-  // properly diagnose the block as a braced intializer
-  // if the comma separator appears after the pp directive.
-  while (NextTok->is(tok::hash)) {
-ScopedMacroState MacroState(*Line, Tokens, NextTok);
-do {
-  NextTok = Tokens->getNextToken();
-} while (NextTok->isNot(tok::eof));
-  }
-
   // Using OriginalColumn to distinguish between ObjC methods and
   // binary operators is a bit hacky.
   bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -607,6 +597,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 NextTok = Tokens->getNextToken();
 ProbablyBracedList = NextTok->isNot(tok::l_square);
   }
+
+  // Cpp macro definition body that is a nonempty braced list or block:
+  if (Style.isCpp() && Line->InMacroBody && PrevTok != FormatTok &&
+  !FormatTok->Previous && NextTok->is(tok::eof) &&
+  // A statement can end with only `;` (simple statement), a block
+  // closing brace (compound statement), or `:` (label statement).
+  // If PrevTok is a block opening brace, Tok ends an empty block.
+  !PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
+ProbablyBracedList = true;
+  }
 }
 if (ProbablyBracedList) {
   Tok->setBlockKind(BK_BracedInit);
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 2ac0b0aab1bee9..88877e53d014c6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1865,6 +1865,13 @@ TEST_F(FormatTest, UnderstandsMacros) {
   verifyFormat("MACRO(co_return##something)");
 
   verifyFormat("#define A x:");
+
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { \\\n"
+  "#Bar \\\n"
+  "  }");
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { #Bar }");
 }
 
 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
@@ -10865,7 +10872,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
   verifyFormat("some_templated_type");
 
   verifyFormat("#define FOO(typeName, realClass)   
\\\n"
-   "  { #typeName, foo(new foo(#typeName)) }",
+   "  {#typeName, foo(new foo(#typeName))}",
getLLVMStyleWithColumns(60));
 }
 
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index cbe6b79a366882..c530339826a1d3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1885,14 +1885,20 @@ TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
  "#Bar \\\n"
  "  }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
 
   Tokens = annotate("#define Foo(Bar) \\\n"
 "  { #Bar }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
+
+  Tokens = annotate("#define FOO(typeName, realClass) \\\n"
+"  {#typeName, foo(new foo(#typeName))}");
+  ASSERT_EQ(Tokens.size(), 29u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
+  EX

[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in ContinuationIndenter (#88414) (PR #89412)

2024-04-25 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar is this waiting for @HazardyKnusperkeks?

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a regression in annotating TrailingReturnArrow (#86624) (PR #89415)

2024-04-25 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar is this still waiting for @HazardyKnusperkeks?

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


[llvm-branch-commits] [polly] 35fea10 - release/18.x: [clang-format] Correctly annotate braces in macros (#87953)

2024-04-25 Thread Owen Pan via llvm-branch-commits

Author: Owen Pan
Date: 2024-04-25T08:31:16-07:00
New Revision: 35fea1032741526f76d7fea49794061bf3a1d3ee

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

LOG: release/18.x: [clang-format] Correctly annotate braces in macros (#87953)

Backport 58323de2e5ed

Added: 


Modified: 
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
polly/lib/Analysis/ScopDetectionDiagnostic.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b7d970f45271da..a6eb18bb2b3227 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -539,16 +539,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 if (Style.Language == FormatStyle::LK_Proto) {
   ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
 } else {
-  // Skip NextTok over preprocessor lines, otherwise we may not
-  // properly diagnose the block as a braced intializer
-  // if the comma separator appears after the pp directive.
-  while (NextTok->is(tok::hash)) {
-ScopedMacroState MacroState(*Line, Tokens, NextTok);
-do {
-  NextTok = Tokens->getNextToken();
-} while (NextTok->isNot(tok::eof));
-  }
-
   // Using OriginalColumn to distinguish between ObjC methods and
   // binary operators is a bit hacky.
   bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -607,6 +597,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 NextTok = Tokens->getNextToken();
 ProbablyBracedList = NextTok->isNot(tok::l_square);
   }
+
+  // Cpp macro definition body that is a nonempty braced list or block:
+  if (Style.isCpp() && Line->InMacroBody && PrevTok != FormatTok &&
+  !FormatTok->Previous && NextTok->is(tok::eof) &&
+  // A statement can end with only `;` (simple statement), a block
+  // closing brace (compound statement), or `:` (label statement).
+  // If PrevTok is a block opening brace, Tok ends an empty block.
+  !PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
+ProbablyBracedList = true;
+  }
 }
 if (ProbablyBracedList) {
   Tok->setBlockKind(BK_BracedInit);

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 2ac0b0aab1bee9..88877e53d014c6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1865,6 +1865,13 @@ TEST_F(FormatTest, UnderstandsMacros) {
   verifyFormat("MACRO(co_return##something)");
 
   verifyFormat("#define A x:");
+
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { \\\n"
+  "#Bar \\\n"
+  "  }");
+  verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
+  "  { #Bar }");
 }
 
 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
@@ -10865,7 +10872,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
   verifyFormat("some_templated_type");
 
   verifyFormat("#define FOO(typeName, realClass)   
\\\n"
-   "  { #typeName, foo(new foo(#typeName)) }",
+   "  {#typeName, foo(new foo(#typeName))}",
getLLVMStyleWithColumns(60));
 }
 

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index cbe6b79a366882..c530339826a1d3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1885,14 +1885,20 @@ TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
  "#Bar \\\n"
  "  }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
 
   Tokens = annotate("#define Foo(Bar) \\\n"
 "  { #Bar }");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
-  EXPECT_BRACE_KIND(Tokens[6], BK_Block);
-  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
+
+  Tokens = annotate("#define FOO(typeName, realClass) \\\n"
+"  {#typeName, foo(new foo(#typeName))}");
+  ASSERT_EQ(Tokens.

[llvm-branch-commits] [clang] [polly] release/18.x: [clang-format] Revert breaking stream operators to prev… (PR #89492)

2024-04-29 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar below is the release notes for clang-format 18.1.5.
- Restores clang-format behavior that always breaks before a stream insertion 
operator (`<<`) when both operands are string literals.
- Fixes clang-format regressions in annotating initialization list braces and 
trailing return type arrow, and breaking after the block comment before a 
function argument.

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a crash with AlignArrayOfStructures option (#86420) (PR #91049)

2024-05-09 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a crash with AlignArrayOfStructures option (#86420) (PR #91049)

2024-05-10 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@HazardyKnusperkeks can you approve it too?

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a bug in formatting goto labels in macros (#92494) (PR #93494)

2024-05-27 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tstellar can we get this one in if we are doing another point release?

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


[llvm-branch-commits] [clang] release/18.x: [clang-format] Fix a bug in formatting goto labels in macros (#92494) (PR #93494)

2024-05-27 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Don't insert a space between :: and * (#105043) (PR #105773)

2024-08-22 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix a misannotation of redundant r_paren as CastRParen (#105921) (PR #105967)

2024-08-26 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix a misannotation of less/greater as angle brackets (#105941) (PR #105971)

2024-08-26 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix misalignments of pointers in angle brackets (#106013) (PR #106326)

2024-08-28 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] js handle anonymous classes (#106242) (PR #106390)

2024-08-28 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] [clang-format] Revert "[clang-format][NFC] Delete TT_LambdaArrow (#70… (PR #106482)

2024-08-28 Thread Owen Pan via llvm-branch-commits

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

…… (#105923)

…519)"

This reverts commit e00d32afb9d33a1eca48e2b041c9688436706c5b and adds a test 
for lambda arrow SplitPenalty.

Fixes #105480.

>From 386f54403a6b38fd14d8e3126fcc46b7e579f575 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Wed, 28 Aug 2024 18:23:54 -0700
Subject: [PATCH] =?UTF-8?q?[clang-format]=20Revert=20"[clang-format][NFC]?=
 =?UTF-8?q?=20Delete=20TT=5FLambdaArrow=20(#70=E2=80=A6=20(#105923)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

…519)"

This reverts commit e00d32afb9d33a1eca48e2b041c9688436706c5b and adds a
test for lambda arrow SplitPenalty.

Fixes #105480.
---
 clang/lib/Format/ContinuationIndenter.cpp | 10 +++---
 clang/lib/Format/FormatToken.h|  3 +-
 clang/lib/Format/TokenAnnotator.cpp   | 33 ++
 clang/lib/Format/UnwrappedLineParser.cpp  |  2 +-
 clang/unittests/Format/TokenAnnotatorTest.cpp | 34 ++-
 5 files changed, 50 insertions(+), 32 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index b07360425ca6e1..7d89f0e63dd225 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -842,10 +842,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
 CurrentState.ContainsUnwrappedBuilder = true;
   }
 
-  if (Current.is(TT_TrailingReturnArrow) &&
-  Style.Language == FormatStyle::LK_Java) {
+  if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
 CurrentState.NoLineBreak = true;
-  }
   if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
   (Previous.MatchingParen &&
(Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) {
@@ -1000,7 +998,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState 
&State,
   //
   // is common and should be formatted like a free-standing function. The same
   // goes for wrapping before the lambda return type arrow.
-  if (Current.isNot(TT_TrailingReturnArrow) &&
+  if (Current.isNot(TT_LambdaArrow) &&
   (!Style.isJavaScript() || Current.NestingLevel != 0 ||
!PreviousNonComment || PreviousNonComment->isNot(tok::equal) ||
!Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) {
@@ -1257,7 +1255,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const 
LineState &State) {
 }
 return CurrentState.Indent;
   }
-  if (Current.is(TT_TrailingReturnArrow) &&
+  if (Current.is(TT_LambdaArrow) &&
   Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
tok::kw_consteval, tok::kw_static, TT_AttributeSquare)) 
{
 return ContinuationIndent;
@@ -1590,7 +1588,7 @@ unsigned 
ContinuationIndenter::moveStateToNextToken(LineState &State,
   }
   if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
 CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1;
-  if (Current.isOneOf(TT_LambdaLSquare, TT_TrailingReturnArrow))
+  if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
 CurrentState.LastSpace = State.Column;
   if (Current.is(TT_RequiresExpression) &&
   Style.RequiresExpressionIndentation == FormatStyle::REI_Keyword) {
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index cc45d5a8c5c1ec..9bfeb2052164ee 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -102,6 +102,7 @@ namespace format {
   TYPE(JsTypeColon)
\
   TYPE(JsTypeOperator) 
\
   TYPE(JsTypeOptionalQuestion) 
\
+  TYPE(LambdaArrow)
\
   TYPE(LambdaLBrace)   
\
   TYPE(LambdaLSquare)  
\
   TYPE(LeadingJavaAnnotation)  
\
@@ -725,7 +726,7 @@ struct FormatToken {
   bool isMemberAccess() const {
 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
!isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
-TT_LeadingJavaAnnotation);
+TT_LambdaArrow, TT_LeadingJavaAnnotation);
   }
 
   bool isPointerOrReference() const {
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 851f79895ac5ac..07b42e79ba9a61 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -831,7 +831,7 @@ class AnnotatingParser {
 }
 // An arrow after an ObjC method expression is not a lambda arrow.
 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
-Current

[llvm-branch-commits] [clang] [clang-format] Revert "[clang-format][NFC] Delete TT_LambdaArrow (#70… (PR #106482)

2024-08-28 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Correctly identify token-pasted record names (PR #106897)

2024-09-01 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Correctly identify token-pasted record names (PR #106897)

2024-09-01 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Correctly identify token-pasted record names (PR #106897)

2024-09-01 Thread Owen Pan via llvm-branch-commits

owenca wrote:

See https://github.com/llvm/llvm-project/pull/106484#issuecomment-2319705402.

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


[llvm-branch-commits] [clang] [clang-format] Revert "[clang-format][NFC] Delete TT_LambdaArrow (#70… (PR #106482)

2024-08-30 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@HazardyKnusperkeks @mydeveloperday @rymiel please see 
https://github.com/llvm/llvm-project/pull/105923#issuecomment-2316563384.

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


[llvm-branch-commits] [clang] [clang-format] Revert "[clang-format][NFC] Delete TT_LambdaArrow (#70… (PR #106482)

2024-09-01 Thread Owen Pan via llvm-branch-commits

owenca wrote:

Will do. Sorry about that!

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in ObjC square brackets (#106654) (PR #106819)

2024-08-30 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] [clang-format] Correctly identify token-pasted record names (PR #106897)

2024-09-01 Thread Owen Pan via llvm-branch-commits

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

See https://github.com/llvm/llvm-project/pull/89706#issuecomment-2315549955.

>From 0b75d0dd80d712c83d6188f931ad18eb6b6c8071 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 1 Sep 2024 02:46:38 -0700
Subject: [PATCH] [clang-format] Correctly identify token-pasted record names

See https://github.com/llvm/llvm-project/pull/89706#issuecomment-2315549955.
---
 clang/lib/Format/UnwrappedLineParser.cpp  |  1 +
 clang/unittests/Format/TokenAnnotatorTest.cpp | 10 ++
 2 files changed, 11 insertions(+)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 53ddb710d2143b..7f5d157ae95891 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4016,6 +4016,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   }
   break;
 case tok::coloncolon:
+case tok::hashhash:
   break;
 default:
   if (!JSPastExtendsOrImplements && !ClassName &&
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 81e2628f28e583..f0533c92f6538c 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3208,6 +3208,16 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_ClassLBrace);
   EXPECT_BRACE_KIND(Tokens[7], BK_Block);
+  EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_ClassRBrace);
+  EXPECT_BRACE_KIND(Tokens[8], BK_Block);
+
+  Tokens = annotate("#define FOO(X) \\\n"
+"  struct X##_tag_ {};");
+  ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+  EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_StructLBrace);
+  EXPECT_BRACE_KIND(Tokens[10], BK_Block);
+  EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace);
+  EXPECT_BRACE_KIND(Tokens[11], BK_Block);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definition (#106662) (PR #107058)

2024-09-03 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definition (#106662) (PR #107058)

2024-09-03 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> @tru, this introduced what looks like a (confirmed) regression, see [#107096 
> (comment)](https://github.com/llvm/llvm-project/issues/107096#issuecomment-2326830100).
>  Could this be reverted?

I've reverted 0fa78b6c7bd43c2498700a98c47a02cf4fd06388 with 
3449ed8dece600f387357b71ff74ae4bc46828b6. I agree that we should do the same 
for 19.x.

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definition (#106662) (PR #107058)

2024-09-04 Thread Owen Pan via llvm-branch-commits

owenca wrote:

Alternatively, we can backport #107352.

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


[llvm-branch-commits] [clang] [clang-format] Correctly annotate braces in macro definition (#107352) (PR #107531)

2024-09-06 Thread Owen Pan via llvm-branch-commits

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

This reverts commit 2d90e8f7402b0a8114978b6f014cfe76c96c94a1 and backports 
commit 616a8ce6203d8c7569266bfaf163e74df1f440ad.

>From 4d8827c9b63ecbc9de984e19621cafad025e4380 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 5 Sep 2024 23:59:11 -0700
Subject: [PATCH] [clang-format] Correctly annotate braces in macro definition
 (#107352)

This reverts commit 2d90e8f7402b0a8114978b6f014cfe76c96c94a1 and backports
commit 616a8ce6203d8c7569266bfaf163e74df1f440ad.
---
 clang/lib/Format/UnwrappedLineParser.cpp  |  6 --
 clang/unittests/Format/TokenAnnotatorTest.cpp | 15 +++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 60e65aaa83e9c1..7813d86ff0ea10 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -570,7 +570,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 NextTok->isOneOf(Keywords.kw_of, 
Keywords.kw_in,
  Keywords.kw_as));
   ProbablyBracedList =
-  ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren));
+  ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() ||
+   NextTok->is(tok::l_paren)));
 
   // If there is a comma, semicolon or right paren after the closing
   // brace, we assume this is a braced initializer list.
@@ -609,8 +610,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool 
ExpectClassBody) {
 ProbablyBracedList = NextTok->isNot(tok::l_square);
   }
 
-  // Cpp macro definition body containing nonempty braced list or 
block:
+  // Cpp macro definition body that is a nonempty braced list or block:
   if (IsCpp && Line->InMacroBody && PrevTok != FormatTok &&
+  !FormatTok->Previous && NextTok->is(tok::eof) &&
   // A statement can end with only `;` (simple statement), a block
   // closing brace (compound statement), or `:` (label statement).
   // If PrevTok is a block opening brace, Tok ends an empty block.
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index db580d70058811..dd58fbc70cb91e 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3219,6 +3219,21 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace);
   EXPECT_BRACE_KIND(Tokens[11], BK_Block);
 
+  Tokens = annotate("#define MACRO\\\n"
+"  struct hash {\\\n"
+"void f() { return; } \\\n"
+"  };");
+  ASSERT_EQ(Tokens.size(), 20u) << Tokens;
+  EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
+  EXPECT_BRACE_KIND(Tokens[8], BK_Block);
+  EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
+  EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
+  EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
+  EXPECT_BRACE_KIND(Tokens[13], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[16], BK_Block);
+  EXPECT_TOKEN(Tokens[17], tok::r_brace, TT_StructRBrace);
+  EXPECT_BRACE_KIND(Tokens[17], BK_Block);
+
   Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definition (#106662) (PR #107058)

2024-09-06 Thread Owen Pan via llvm-branch-commits

owenca wrote:

@tru, I've created #107531 which reverts 
2d90e8f7402b0a8114978b6f014cfe76c96c94a1 and backports #107352.

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


[llvm-branch-commits] [clang] [clang-format] Correctly annotate braces in macro definition (#107352) (PR #107531)

2024-09-06 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Correctly annotate braces in macro definition (#107352) (PR #107531)

2024-09-10 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> Hi, since we are wrapping up LLVM 19.1.0 we are very strict with the fixes we 
> pick at this point. Can you please respond to the following questions to help 
> me understand if this has to be included in the final release or not.
> 
> Is this PR a fix for a regression or a critical issue?

This is a fix for a regression as labeled.

> What is the risk of accepting this into the release branch?

The risk of accepting this is very low IMO.

> What is the risk of NOT accepting this into the release branch?

The risk of NOT accepting this is 100%.

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


[llvm-branch-commits] [clang] [clang-format] Handle template closer followed by empty paretheses (#… (PR #111245)

2024-10-05 Thread Owen Pan via llvm-branch-commits

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

…110408)

Fixes #109925.

>From 8f516b15b30f7d17f82f958e758e448929c6e9a1 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Wed, 2 Oct 2024 19:06:56 -0700
Subject: [PATCH] [clang-format] Handle template closer followed by empty
 paretheses (#110408)

Fixes #109925.
---
 clang/lib/Format/TokenAnnotator.cpp   | 42 ++-
 clang/lib/Format/UnwrappedLineParser.cpp  | 12 --
 clang/unittests/Format/TokenAnnotatorTest.cpp |  7 
 3 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 4512e539cc7947..ad9ed7b47d002c 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -189,25 +189,29 @@ class AnnotatingParser {
   next();
 }
 
-for (bool SeenTernaryOperator = false; CurrentToken;) {
+for (bool SeenTernaryOperator = false, MaybeAngles = true; CurrentToken;) {
   const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
   if (CurrentToken->is(tok::greater)) {
 const auto *Next = CurrentToken->Next;
-// Try to do a better job at looking for ">>" within the condition of
-// a statement. Conservatively insert spaces between consecutive ">"
-// tokens to prevent splitting right bitshift operators and potentially
-// altering program semantics. This check is overly conservative and
-// will prevent spaces from being inserted in select nested template
-// parameter cases, but should not alter program semantics.
-if (Next && Next->is(tok::greater) &&
-Left->ParentBracket != tok::less &&
-CurrentToken->getStartOfNonWhitespace() ==
-Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
-  return false;
-}
-if (InExpr && SeenTernaryOperator &&
-(!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
-  return false;
+if (CurrentToken->isNot(TT_TemplateCloser)) {
+  // Try to do a better job at looking for ">>" within the condition of
+  // a statement. Conservatively insert spaces between consecutive ">"
+  // tokens to prevent splitting right shift operators and potentially
+  // altering program semantics. This check is overly conservative and
+  // will prevent spaces from being inserted in select nested template
+  // parameter cases, but should not alter program semantics.
+  if (Next && Next->is(tok::greater) &&
+  Left->ParentBracket != tok::less &&
+  CurrentToken->getStartOfNonWhitespace() ==
+  Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
+return false;
+  }
+  if (InExpr && SeenTernaryOperator &&
+  (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
+return false;
+  }
+  if (!MaybeAngles)
+return false;
 }
 Left->MatchingParen = CurrentToken;
 CurrentToken->MatchingParen = Left;
@@ -243,11 +247,11 @@ class AnnotatingParser {
   // operator that was misinterpreted because we are parsing template
   // parameters.
   // FIXME: This is getting out of hand, write a decent parser.
-  if (InExpr && !Line.startsWith(tok::kw_template) &&
+  if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
   Prev.is(TT_BinaryOperator)) {
 const auto Precedence = Prev.getPrecedence();
 if (Precedence > prec::Conditional && Precedence < prec::Relational)
-  return false;
+  MaybeAngles = false;
   }
   if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
 SeenTernaryOperator = true;
@@ -1615,7 +1619,7 @@ class AnnotatingParser {
 return false;
   break;
 case tok::greater:
-  if (Style.Language != FormatStyle::LK_TextProto)
+  if (Style.Language != FormatStyle::LK_TextProto && Tok->is(TT_Unknown))
 Tok->setType(TT_BinaryOperator);
   if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
 Tok->SpacesRequiredBefore = 1;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 7813d86ff0ea10..f7b3561f6e0335 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2551,7 +2551,7 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
 parseChildBlock();
   break;
 case tok::r_paren: {
-  const auto *Prev = LeftParen->Previous;
+  auto *Prev = LeftParen->Previous;
   if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
   Style.RemoveParentheses > FormatStyle::RPS_Leave) {
 const auto *Next = Tokens->peekNextToken();
@@ -2575,9 +2575,13 @@ bool UnwrappedLineParser::parseParens(TokenType 
Am

[llvm-branch-commits] [clang] [clang-format] Handle template closer followed by empty paretheses (#… (PR #111245)

2024-10-05 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Handle template closer followed by empty paretheses (PR #111245)

2024-10-05 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Handle template closer followed by empty paretheses (PR #111245)

2024-10-05 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Handle template closer followed by braces (#110971) (PR #111958)

2024-10-11 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Reimplement InsertNewlineAtEOF (#108513) (PR #109170)

2024-09-19 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix idempotent format of hash in macro body (#118513) (PR #119503)

2024-12-17 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> can we get a review on this?

I already approved it.

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix idempotent format of hash in macro body (#118513) (PR #119503)

2024-12-13 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definitions (#123279) (PR #123439)

2025-01-27 Thread Owen Pan via llvm-branch-commits

owenca wrote:

> So far this is the only slated fix for 19.1.8 - I don't think it's important 
> enough to do a release. Let me know if you disagree.

+1.

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] Add ClassHeadName to help annotating StartOfName (#124891) (PR #126866)

2025-02-11 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] Release/20.x: [clang-format] Set C11 instead of C17 for LK_C (PR #134514)

2025-04-05 Thread Owen Pan via llvm-branch-commits

owenca wrote:

Copied the entire `getFormattingLangOpts()` function from commit 
d71ee7d23048ca64d14a7536927a006867cea39a to resolve conflicts.

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


[llvm-branch-commits] [clang] Release/20.x: [clang-format] Set C11 instead of C17 for LK_C (PR #134514)

2025-04-05 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] Release/20.x: [clang-format] Set C11 instead of C17 for LK_C (PR #134514)

2025-04-05 Thread Owen Pan via llvm-branch-commits

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

Backport d71ee7d23048ca64d14a7536927a006867cea39a

>From 4b705f9a9a06276f9586d628a409814104ce8e3a Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sat, 5 Apr 2025 13:35:45 -0700
Subject: [PATCH] Release/20.x: [clang-format] Set C11 instead of C17 for LK_C

Backport d71ee7d23048ca64d14a7536927a006867cea39a
---
 clang/lib/Format/Format.cpp   | 42 +++
 clang/lib/Format/FormatToken.cpp  |  2 +-
 clang/lib/Format/TokenAnnotator.cpp   |  3 +-
 clang/lib/Format/TokenAnnotator.h |  4 +-
 clang/lib/Format/UnwrappedLineParser.cpp  |  4 +-
 clang/unittests/Format/TokenAnnotatorTest.cpp |  6 +++
 6 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 768e655f65ce7..b97d8928178b5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3946,34 +3946,42 @@ tooling::Replacements sortUsingDeclarations(const 
FormatStyle &Style,
 LangOptions getFormattingLangOpts(const FormatStyle &Style) {
   LangOptions LangOpts;
 
-  FormatStyle::LanguageStandard LexingStd = Style.Standard;
-  if (LexingStd == FormatStyle::LS_Auto)
-LexingStd = FormatStyle::LS_Latest;
-  if (LexingStd == FormatStyle::LS_Latest)
+  auto LexingStd = Style.Standard;
+  if (LexingStd == FormatStyle::LS_Auto || LexingStd == FormatStyle::LS_Latest)
 LexingStd = FormatStyle::LS_Cpp20;
-  LangOpts.CPlusPlus = 1;
-  LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
-  LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
-  LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
-  LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
-  LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
+
+  const bool SinceCpp11 = LexingStd >= FormatStyle::LS_Cpp11;
+  const bool SinceCpp20 = LexingStd >= FormatStyle::LS_Cpp20;
+
+  switch (Style.Language) {
+  case FormatStyle::LK_C:
+LangOpts.C11 = 1;
+break;
+  case FormatStyle::LK_Cpp:
+  case FormatStyle::LK_ObjC:
+LangOpts.CXXOperatorNames = 1;
+LangOpts.CPlusPlus11 = SinceCpp11;
+LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
+LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
+LangOpts.CPlusPlus20 = SinceCpp20;
+[[fallthrough]];
+  default:
+LangOpts.CPlusPlus = 1;
+  }
+
+  LangOpts.Char8 = SinceCpp20;
   // Turning on digraphs in standards before C++0x is error-prone, because e.g.
   // the sequence "<::" will be unconditionally treated as "[:".
   // Cf. Lexer::LexTokenInternal.
-  LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
+  LangOpts.Digraphs = SinceCpp11;
 
   LangOpts.LineComment = 1;
-
-  const auto Language = Style.Language;
-  LangOpts.C17 = Language == FormatStyle::LK_C;
-  LangOpts.CXXOperatorNames =
-  Language == FormatStyle::LK_Cpp || Language == FormatStyle::LK_ObjC;
-
   LangOpts.Bool = 1;
   LangOpts.ObjC = 1;
   LangOpts.MicrosoftExt = 1;// To get kw___try, kw___finally.
   LangOpts.DeclSpecKeyword = 1; // To get __declspec.
   LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
+
   return LangOpts;
 }
 
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 60e428123d26d..a4e2acc922c0d 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -44,7 +44,7 @@ static SmallVector CppNonKeywordTypes = {
 bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
   if (is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts))
 return true;
-  return (LangOpts.CXXOperatorNames || LangOpts.C17) && is(tok::identifier) &&
+  return (LangOpts.CXXOperatorNames || LangOpts.C11) && is(tok::identifier) &&
  std::binary_search(CppNonKeywordTypes.begin(),
 CppNonKeywordTypes.end(), TokenText);
 }
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 976c4d888e1fd..3e1a5c7df7f77 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -129,7 +129,6 @@ class AnnotatingParser {
   : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
 IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
-assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
 resetTokenMetadata();
   }
@@ -3820,7 +3819,7 @@ static bool isFunctionDeclarationName(const LangOptions 
&LangOpts,
   };
 
   const auto *Next = Current.Next;
-  const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C17;
+  const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
 
   // Find parentheses of parameter list.
   if (Current.is(tok::kw_operator)) {
diff --git a/clang/lib/Format/TokenAnnotator.h 
b/cl

[llvm-branch-commits] [clang] release/20.x: [clang-format] RemoveParentheses shouldn't remove empty… (PR #138380)

2025-05-03 Thread Owen Pan via llvm-branch-commits

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

>From 4b85fd08252bfe37382d4f9a7ecde898422830c7 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 2 May 2025 19:19:39 -0700
Subject: [PATCH 1/2] release/20.x: [clang-format] RemoveParentheses shouldn't
 remove empty parentheses (#138229)

Backport d3506ee573a2aa1403817642ef45f8c0305bb572
---
 clang/lib/Format/UnwrappedLineParser.cpp | 3 ++-
 clang/unittests/Format/FormatTest.cpp| 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 2b348c926294e..c3ffabce15ec8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2581,7 +2581,8 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
   if (Prev) {
 auto OptionalParens = [&] {
   if (MightBeStmtExpr || MightBeFoldExpr || Line->InMacroBody ||
-  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave) {
+  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave ||
+  RParen->getPreviousNonComment() == LParen) {
 return false;
   }
   const bool DoubleParens =
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index bf3eff129efd5..49e1fde1d9ccf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27895,6 +27895,8 @@ TEST_F(FormatTest, RemoveParentheses) {
   verifyFormat("return ((... && std::is_convertible_v));",
"return (((... && std::is_convertible_v)));",
Style);
+  verifyFormat("MOCK_METHOD(void, Function, (), override);",
+   "MOCK_METHOD(void, Function, (), (override));", Style);
 
   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
   verifyFormat("#define Return0 return (0);", Style);

>From 0e88d0b2e6c88c3996ec72f31e211c91976cb7b0 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 2 May 2025 19:20:42 -0700
Subject: [PATCH 2/2] release/20.x: [clang-format] Fix a crash on formatting
 missing r_paren/r_brace (#138230)

Backport 79210feb2993ff9a79ef11f8a7016a527d4fcf22
---
 clang/lib/Format/UnwrappedLineParser.cpp | 4 ++--
 clang/unittests/Format/FormatTest.cpp| 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index c3ffabce15ec8..673b3e6c4b8c2 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1837,8 +1837,8 @@ void UnwrappedLineParser::parseStructuralElement(
   nextToken();
   if (FormatTok->is(tok::l_paren)) {
 parseParens();
-assert(FormatTok->Previous);
-if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
+if (FormatTok->Previous &&
+FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
   tok::l_paren)) {
   Line->SeenDecltypeAuto = true;
 }
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 49e1fde1d9ccf..90a79230e9f4c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13962,6 +13962,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct Foo {\n"
 "  operator foo(bar\n"
 "};");
+  verifyNoCrash("decltype( {\n"
+"  {");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] RemoveParentheses shouldn't remove empty… (PR #138380)

2025-05-03 Thread Owen Pan via llvm-branch-commits

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

>From 4b85fd08252bfe37382d4f9a7ecde898422830c7 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 2 May 2025 19:19:39 -0700
Subject: [PATCH] release/20.x: [clang-format] RemoveParentheses shouldn't
 remove empty parentheses (#138229)

Backport d3506ee573a2aa1403817642ef45f8c0305bb572
---
 clang/lib/Format/UnwrappedLineParser.cpp | 3 ++-
 clang/unittests/Format/FormatTest.cpp| 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 2b348c926294e..c3ffabce15ec8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2581,7 +2581,8 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
   if (Prev) {
 auto OptionalParens = [&] {
   if (MightBeStmtExpr || MightBeFoldExpr || Line->InMacroBody ||
-  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave) {
+  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave ||
+  RParen->getPreviousNonComment() == LParen) {
 return false;
   }
   const bool DoubleParens =
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index bf3eff129efd5..49e1fde1d9ccf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27895,6 +27895,8 @@ TEST_F(FormatTest, RemoveParentheses) {
   verifyFormat("return ((... && std::is_convertible_v));",
"return (((... && std::is_convertible_v)));",
Style);
+  verifyFormat("MOCK_METHOD(void, Function, (), override);",
+   "MOCK_METHOD(void, Function, (), (override));", Style);
 
   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
   verifyFormat("#define Return0 return (0);", Style);

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] RemoveParentheses shouldn't remove empty… (PR #138380)

2025-05-02 Thread Owen Pan via llvm-branch-commits

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

… parentheses (#138229)

Backport d3506ee573a2aa1403817642ef45f8c0305bb572

>From 4b85fd08252bfe37382d4f9a7ecde898422830c7 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 2 May 2025 19:19:39 -0700
Subject: [PATCH] release/20.x: [clang-format] RemoveParentheses shouldn't
 remove empty parentheses (#138229)

Backport d3506ee573a2aa1403817642ef45f8c0305bb572
---
 clang/lib/Format/UnwrappedLineParser.cpp | 3 ++-
 clang/unittests/Format/FormatTest.cpp| 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 2b348c926294e..c3ffabce15ec8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2581,7 +2581,8 @@ bool UnwrappedLineParser::parseParens(TokenType 
AmpAmpTokenType) {
   if (Prev) {
 auto OptionalParens = [&] {
   if (MightBeStmtExpr || MightBeFoldExpr || Line->InMacroBody ||
-  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave) {
+  SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave ||
+  RParen->getPreviousNonComment() == LParen) {
 return false;
   }
   const bool DoubleParens =
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index bf3eff129efd5..49e1fde1d9ccf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27895,6 +27895,8 @@ TEST_F(FormatTest, RemoveParentheses) {
   verifyFormat("return ((... && std::is_convertible_v));",
"return (((... && std::is_convertible_v)));",
Style);
+  verifyFormat("MOCK_METHOD(void, Function, (), override);",
+   "MOCK_METHOD(void, Function, (), (override));", Style);
 
   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
   verifyFormat("#define Return0 return (0);", Style);

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] RemoveParentheses shouldn't remove empty… (PR #138380)

2025-05-02 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] release/19.x: [clang-format] Correctly annotate braces in macro definitions (#123279) (PR #123439)

2025-03-01 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] [clang-format] Fix a bug that changes keyword `or` to an identifier (PR #128996)

2025-02-26 Thread Owen Pan via llvm-branch-commits

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

Backports ffc61dc393e4 0968df9c3a55 2d585ccecc45

Fixes #105482

>From 678c9eda153fcaebbf1b1bf34c9253c40f3564fc Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 21 Feb 2025 20:46:43 -0800
Subject: [PATCH] [clang-format] Fix a bug that changes keyword `or` to an
 identifier (#128410)

Backports ffc61dc393e4 0968df9c3a55 2d585ccecc45

Fixes #105482
---
 clang/docs/ClangFormatStyleOptions.rst| 13 +-
 clang/docs/ReleaseNotes.rst   |  4 ++
 clang/include/clang/Format/Format.h   | 17 ++--
 clang/lib/Format/Format.cpp   | 43 ++-
 clang/lib/Format/FormatToken.cpp  | 10 ++---
 clang/lib/Format/FormatToken.h| 23 --
 clang/lib/Format/TokenAnnotator.cpp   |  4 +-
 clang/lib/Format/TokenAnnotator.h |  2 +-
 clang/lib/Format/UnwrappedLineParser.cpp  |  8 +---
 clang/unittests/Format/FormatTest.cpp | 19 +++-
 clang/unittests/Format/TokenAnnotatorTest.cpp | 11 -
 11 files changed, 106 insertions(+), 48 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index bbb912eb10e94..4b4c412a13323 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -4735,15 +4735,24 @@ the configuration (without a prefix: ``Auto``).
 .. _Language:
 
 **Language** (``LanguageKind``) :versionbadge:`clang-format 3.5` :ref:`¶ 
`
-  Language, this format style is targeted at.
+  The language that this format style targets.
+
+  .. note::
+
+   You can specify the language (``C``, ``Cpp``, or ``ObjC``) for ``.h``
+   files by adding a ``// clang-format Language:`` line before the first
+   non-comment (and non-empty) line, e.g. ``// clang-format Language: Cpp``.
 
   Possible values:
 
   * ``LK_None`` (in configuration: ``None``)
 Do not use.
 
+  * ``LK_C`` (in configuration: ``C``)
+Should be used for C.
+
   * ``LK_Cpp`` (in configuration: ``Cpp``)
-Should be used for C, C++.
+Should be used for C++.
 
   * ``LK_CSharp`` (in configuration: ``CSharp``)
 Should be used for C#.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 153afdb3d59e3..57a567509a068 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1358,6 +1358,10 @@ clang-format
 - Adds ``WrapNamespaceBodyWithEmptyLines`` option.
 - Adds the ``IndentExportBlock`` option.
 - Adds ``PenaltyBreakBeforeMemberAccess`` option.
+- Add the C language instead of treating it like C++.
+- Allow specifying the language (C, C++, or Objective-C) for a ``.h`` file by
+  adding a special comment (e.g. ``// clang-format Language: ObjC``) near the
+  top of the file.
 
 libclang
 
diff --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 6f432d1d50315..abab543518222 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3275,7 +3275,9 @@ struct FormatStyle {
   enum LanguageKind : int8_t {
 /// Do not use.
 LK_None,
-/// Should be used for C, C++.
+/// Should be used for C.
+LK_C,
+/// Should be used for C++.
 LK_Cpp,
 /// Should be used for C#.
 LK_CSharp,
@@ -3300,7 +3302,9 @@ struct FormatStyle {
 /// https://sci-hub.st/10.1109/IEEESTD.2018.8299595
 LK_Verilog
   };
-  bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; }
+  bool isCpp() const {
+return Language == LK_Cpp || Language == LK_C || Language == LK_ObjC;
+  }
   bool isCSharp() const { return Language == LK_CSharp; }
   bool isJson() const { return Language == LK_Json; }
   bool isJavaScript() const { return Language == LK_JavaScript; }
@@ -3310,7 +3314,12 @@ struct FormatStyle {
   }
   bool isTableGen() const { return Language == LK_TableGen; }
 
-  /// Language, this format style is targeted at.
+  /// The language that this format style targets.
+  /// \note
+  ///  You can specify the language (``C``, ``Cpp``, or ``ObjC``) for ``.h``
+  ///  files by adding a ``// clang-format Language:`` line before the first
+  ///  non-comment (and non-empty) line, e.g. ``// clang-format Language: 
Cpp``.
+  /// \endnote
   /// \version 3.5
   LanguageKind Language;
 
@@ -5665,6 +5674,8 @@ FormatStyle::LanguageKind guessLanguage(StringRef 
FileName, StringRef Code);
 // Returns a string representation of ``Language``.
 inline StringRef getLanguageName(FormatStyle::LanguageKind Language) {
   switch (Language) {
+  case FormatStyle::LK_C:
+return "C";
   case FormatStyle::LK_Cpp:
 return "C++";
   case FormatStyle::LK_CSharp:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f02bf95cfeed7..0bb8545884442 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -401,6 +401,7 @@ template <> struct 
MappingTraits {
 
 template <> struct ScalarEnumerati

[llvm-branch-commits] [clang] release/20.x: [clang-format] Hanlde qualified type name for `QualifierAlignment` (#125327) (PR #126839)

2025-02-12 Thread Owen Pan via llvm-branch-commits

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


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


[llvm-branch-commits] [clang] [clang-format] Fix a bug that changes keyword `or` to an identifier (PR #128996)

2025-02-27 Thread Owen Pan via llvm-branch-commits

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] Fix a crash on formatting missing r_pare… (PR #139345)

2025-05-09 Thread Owen Pan via llvm-branch-commits

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

…n/r_brace (#138230)

Backport 79210feb2993ff9a79ef11f8a7016a527d4fcf22

>From 6b27319fcf50dbb0d682cb240049d93022d456af Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Fri, 9 May 2025 17:55:48 -0700
Subject: [PATCH] release/20.x: [clang-format] Fix a crash on formatting
 missing r_paren/r_brace (#138230)

Backport 79210feb2993ff9a79ef11f8a7016a527d4fcf22
---
 clang/lib/Format/UnwrappedLineParser.cpp | 4 ++--
 clang/unittests/Format/FormatTest.cpp| 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index c3ffabce15ec8..673b3e6c4b8c2 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1837,8 +1837,8 @@ void UnwrappedLineParser::parseStructuralElement(
   nextToken();
   if (FormatTok->is(tok::l_paren)) {
 parseParens();
-assert(FormatTok->Previous);
-if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
+if (FormatTok->Previous &&
+FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
   tok::l_paren)) {
   Line->SeenDecltypeAuto = true;
 }
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 49e1fde1d9ccf..90a79230e9f4c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13962,6 +13962,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct Foo {\n"
 "  operator foo(bar\n"
 "};");
+  verifyNoCrash("decltype( {\n"
+"  {");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] Handle Java text blocks (#141334) (PR #141433)

2025-05-25 Thread Owen Pan via llvm-branch-commits

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

>From d32d17a56f6d53cebc6299c0474fc95abeb141ed Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 25 May 2025 15:40:45 -0700
Subject: [PATCH 1/2] [clang-format] Handle Java text blocks (#141334)

Fix #61954

(cherry picked from commit b7f5950bb3b97eac979925a3bbf015530c26962e)
---
 clang/lib/Format/FormatTokenLexer.cpp | 32 +
 clang/lib/Format/FormatTokenLexer.h   |  2 +
 clang/unittests/Format/FormatTestJava.cpp | 57 +++
 3 files changed, 91 insertions(+)

diff --git a/clang/lib/Format/FormatTokenLexer.cpp 
b/clang/lib/Format/FormatTokenLexer.cpp
index 16f0a76f3a954..d8ad3a1d52115 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -636,6 +636,36 @@ bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken 
*Prev) {
   return true;
 }
 
+void FormatTokenLexer::tryParseJavaTextBlock() {
+  if (FormatTok->TokenText != "\"\"")
+return;
+
+  const auto *S = Lex->getBufferLocation();
+  const auto *End = Lex->getBuffer().end();
+
+  if (S == End || *S != '\"')
+return;
+
+  ++S; // Skip the `"""` that begins a text block.
+
+  // Find the `"""` that ends the text block.
+  for (int Count = 0; Count < 3 && S < End; ++S) {
+switch (*S) {
+case '\\':
+  Count = -1;
+  break;
+case '\"':
+  ++Count;
+  break;
+default:
+  Count = 0;
+}
+  }
+
+  // Ignore the possibly invalid text block.
+  resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(S)));
+}
+
 // Tries to parse a JavaScript Regex literal starting at the current token,
 // if that begins with a slash and is in a location where JavaScript allows
 // regex literals. Changes the current token to a regex literal and updates
@@ -1326,6 +1356,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
 ++Column;
 StateStack.push(LexerState::TOKEN_STASHED);
+  } else if (Style.isJava() && FormatTok->is(tok::string_literal)) {
+tryParseJavaTextBlock();
   }
 
   if (Style.isVerilog() && Tokens.size() > 0 &&
diff --git a/clang/lib/Format/FormatTokenLexer.h 
b/clang/lib/Format/FormatTokenLexer.h
index 61474a3f9ada8..d9a25c8ef3538 100644
--- a/clang/lib/Format/FormatTokenLexer.h
+++ b/clang/lib/Format/FormatTokenLexer.h
@@ -71,6 +71,8 @@ class FormatTokenLexer {
 
   bool canPrecedeRegexLiteral(FormatToken *Prev);
 
+  void tryParseJavaTextBlock();
+
   // Tries to parse a JavaScript Regex literal starting at the current token,
   // if that begins with a slash and is in a location where JavaScript allows
   // regex literals. Changes the current token to a regex literal and updates
diff --git a/clang/unittests/Format/FormatTestJava.cpp 
b/clang/unittests/Format/FormatTestJava.cpp
index 33998bc7ff858..d0a3b4eb96d69 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -789,6 +789,63 @@ TEST_F(FormatTestJava, AlignCaseArrows) {
Style);
 }
 
+TEST_F(FormatTestJava, TextBlock) {
+  verifyNoChange("String myStr = \"\"\"\n"
+ "hello\n"
+ "there\n"
+ "\"\"\";");
+
+  verifyNoChange("String tb = \"\"\"\n"
+ "the new\"\"\";");
+
+  verifyNoChange("System.out.println(\"\"\"\n"
+ "This is the first line\n"
+ "This is the second line\n"
+ "\"\"\");");
+
+  verifyNoChange("void writeHTML() {\n"
+ "  String html = \"\"\" \n"
+ "\n"
+ "Hello World.\n"
+ "\n"
+ "\"\"\";\n"
+ "  writeOutput(html);\n"
+ "}");
+
+  verifyNoChange("String colors = \"\"\"\t\n"
+ "red\n"
+ "green\n"
+ "blue\"\"\".indent(4);");
+
+  verifyNoChange("String code = \"\"\"\n"
+ "String source = \\\"\"\"\n"
+ "String message = \"Hello, World!\";\n"
+ "System.out.println(message);\n"
+ "\\\"\"\";\n"
+ "\"\"\";");
+
+  verifyNoChange(
+  "class Outer {\n"
+  "  void printPoetry() {\n"
+  "String lilacs = \"\"\"\n"
+  "Passing the apple-tree blows of white and pink in the orchards\n"
+  "\"\"\";\n"
+  "System.out.println(lilacs);\n"
+  "  }\n"
+  "}");
+
+  verifyNoChange("String name = \"\"\"\r\n"
+ "red\n"
+ "green\n"
+ "blue\\\n"
+ "\"\"\";");
+
+  verifyFormat("String name = \"\"\"Pat Q. Smith\"\"\";");
+
+  verifyNoChange("String name = \"\"\"\n"
+ "  Pat Q. Smith");
+}
+
 } // namespace
 } // namespace test
 } // namespace format


[llvm-branch-commits] [clang] release/20.x: [clang-format] Handle Java text blocks (#141334) (PR #141433)

2025-05-25 Thread Owen Pan via llvm-branch-commits

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

>From d32d17a56f6d53cebc6299c0474fc95abeb141ed Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 25 May 2025 15:40:45 -0700
Subject: [PATCH 1/3] [clang-format] Handle Java text blocks (#141334)

Fix #61954

(cherry picked from commit b7f5950bb3b97eac979925a3bbf015530c26962e)
---
 clang/lib/Format/FormatTokenLexer.cpp | 32 +
 clang/lib/Format/FormatTokenLexer.h   |  2 +
 clang/unittests/Format/FormatTestJava.cpp | 57 +++
 3 files changed, 91 insertions(+)

diff --git a/clang/lib/Format/FormatTokenLexer.cpp 
b/clang/lib/Format/FormatTokenLexer.cpp
index 16f0a76f3a954..d8ad3a1d52115 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -636,6 +636,36 @@ bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken 
*Prev) {
   return true;
 }
 
+void FormatTokenLexer::tryParseJavaTextBlock() {
+  if (FormatTok->TokenText != "\"\"")
+return;
+
+  const auto *S = Lex->getBufferLocation();
+  const auto *End = Lex->getBuffer().end();
+
+  if (S == End || *S != '\"')
+return;
+
+  ++S; // Skip the `"""` that begins a text block.
+
+  // Find the `"""` that ends the text block.
+  for (int Count = 0; Count < 3 && S < End; ++S) {
+switch (*S) {
+case '\\':
+  Count = -1;
+  break;
+case '\"':
+  ++Count;
+  break;
+default:
+  Count = 0;
+}
+  }
+
+  // Ignore the possibly invalid text block.
+  resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(S)));
+}
+
 // Tries to parse a JavaScript Regex literal starting at the current token,
 // if that begins with a slash and is in a location where JavaScript allows
 // regex literals. Changes the current token to a regex literal and updates
@@ -1326,6 +1356,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
 ++Column;
 StateStack.push(LexerState::TOKEN_STASHED);
+  } else if (Style.isJava() && FormatTok->is(tok::string_literal)) {
+tryParseJavaTextBlock();
   }
 
   if (Style.isVerilog() && Tokens.size() > 0 &&
diff --git a/clang/lib/Format/FormatTokenLexer.h 
b/clang/lib/Format/FormatTokenLexer.h
index 61474a3f9ada8..d9a25c8ef3538 100644
--- a/clang/lib/Format/FormatTokenLexer.h
+++ b/clang/lib/Format/FormatTokenLexer.h
@@ -71,6 +71,8 @@ class FormatTokenLexer {
 
   bool canPrecedeRegexLiteral(FormatToken *Prev);
 
+  void tryParseJavaTextBlock();
+
   // Tries to parse a JavaScript Regex literal starting at the current token,
   // if that begins with a slash and is in a location where JavaScript allows
   // regex literals. Changes the current token to a regex literal and updates
diff --git a/clang/unittests/Format/FormatTestJava.cpp 
b/clang/unittests/Format/FormatTestJava.cpp
index 33998bc7ff858..d0a3b4eb96d69 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -789,6 +789,63 @@ TEST_F(FormatTestJava, AlignCaseArrows) {
Style);
 }
 
+TEST_F(FormatTestJava, TextBlock) {
+  verifyNoChange("String myStr = \"\"\"\n"
+ "hello\n"
+ "there\n"
+ "\"\"\";");
+
+  verifyNoChange("String tb = \"\"\"\n"
+ "the new\"\"\";");
+
+  verifyNoChange("System.out.println(\"\"\"\n"
+ "This is the first line\n"
+ "This is the second line\n"
+ "\"\"\");");
+
+  verifyNoChange("void writeHTML() {\n"
+ "  String html = \"\"\" \n"
+ "\n"
+ "Hello World.\n"
+ "\n"
+ "\"\"\";\n"
+ "  writeOutput(html);\n"
+ "}");
+
+  verifyNoChange("String colors = \"\"\"\t\n"
+ "red\n"
+ "green\n"
+ "blue\"\"\".indent(4);");
+
+  verifyNoChange("String code = \"\"\"\n"
+ "String source = \\\"\"\"\n"
+ "String message = \"Hello, World!\";\n"
+ "System.out.println(message);\n"
+ "\\\"\"\";\n"
+ "\"\"\";");
+
+  verifyNoChange(
+  "class Outer {\n"
+  "  void printPoetry() {\n"
+  "String lilacs = \"\"\"\n"
+  "Passing the apple-tree blows of white and pink in the orchards\n"
+  "\"\"\";\n"
+  "System.out.println(lilacs);\n"
+  "  }\n"
+  "}");
+
+  verifyNoChange("String name = \"\"\"\r\n"
+ "red\n"
+ "green\n"
+ "blue\\\n"
+ "\"\"\";");
+
+  verifyFormat("String name = \"\"\"Pat Q. Smith\"\"\";");
+
+  verifyNoChange("String name = \"\"\"\n"
+ "  Pat Q. Smith");
+}
+
 } // namespace
 } // namespace test
 } // namespace format


[llvm-branch-commits] [clang] release/20.x: [clang-format] Fix the indent of StartOfName after Attr… (PR #141004)

2025-05-21 Thread Owen Pan via llvm-branch-commits

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

…ibuteMacro (#140361)

Backport 0cac25bcf5a246eb8a1f02d5041731ae9a6f00e0

>From b05983ec62a831de375e96f9ddb9e87e8043049d Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Wed, 21 May 2025 21:37:54 -0700
Subject: [PATCH] release/20.x: [clang-format] Fix the indent of StartOfName
 after AttributeMacro (#140361)

Backport 0cac25bcf5a246eb8a1f02d5041731ae9a6f00e0
---
 clang/lib/Format/ContinuationIndenter.cpp | 4 +++-
 clang/unittests/Format/FormatTest.cpp | 7 +++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 6f7d213c0b559..d953348b0258d 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1452,7 +1452,9 @@ unsigned ContinuationIndenter::getNewLineColumn(const 
LineState &State) {
(PreviousNonComment->ClosesTemplateDeclaration ||
 PreviousNonComment->ClosesRequiresClause ||
 (PreviousNonComment->is(TT_AttributeMacro) &&
- Current.isNot(tok::l_paren)) ||
+ Current.isNot(tok::l_paren) &&
+ !Current.endsSequence(TT_StartOfName, TT_AttributeMacro,
+   TT_PointerOrReference)) ||
 PreviousNonComment->isOneOf(
 TT_AttributeRParen, TT_AttributeSquare, 
TT_FunctionAnnotationRParen,
 TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 90a79230e9f4c..1afcc75a2e19e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12419,6 +12419,13 @@ TEST_F(FormatTest, UnderstandsAttributes) {
   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
+
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AttributeMacros.push_back("my_fancy_attr");
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("void foo(const MyLongTypeName* my_fancy_attr\n"
+   " testt);",
+   Style);
 }
 
 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {

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


[llvm-branch-commits] [clang] release/20.x: [clang-format] Fix the indent of StartOfName after Attr… (PR #141004)

2025-05-21 Thread Owen Pan via llvm-branch-commits

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