[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits

owenca wrote:

Reverted in d08fcc817eba.

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread via cfe-commits

dyung wrote:

I'm seeing the same failures on our linux/Windows build bots:
https://lab.llvm.org/buildbot/#/builders/139/builds/50966
https://lab.llvm.org/buildbot/#/builders/216/builds/28315

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Björn Schäpers via cfe-commits

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


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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits

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

>From 15d37075331311020020c5741e2432cd3fc0be74 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 1 Oct 2023 23:01:30 -0700
Subject: [PATCH 1/2] [clang-format] Annotate ctors/dtors as CtorDtorDeclName
 instead

After annotating constructors/destructors as FunctionDeclarationName in
commit 08630512088, we have seen several issues because ctors/dtors had been
treated differently than functions in aligning, wrapping, and indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.
---
 clang/lib/Format/FormatToken.h|  1 +
 clang/lib/Format/TokenAnnotator.cpp   | 18 ---
 clang/lib/Format/WhitespaceManager.cpp|  6 +--
 clang/unittests/Format/FormatTest.cpp | 10 +++-
 .../Format/FormatTestMacroExpansion.cpp   |  8 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 54 +++
 6 files changed, 56 insertions(+), 41 deletions(-)

diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index dbd3a6e70f037ef..5877b0a6124742a 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -61,6 +61,7 @@ namespace format {
   TYPE(CSharpStringLiteral)
\
   TYPE(CtorInitializerColon)   
\
   TYPE(CtorInitializerComma)   
\
+  TYPE(CtorDtorDeclName)   
\
   TYPE(DesignatedInitializerLSquare)   
\
   TYPE(DesignatedInitializerPeriod)
\
   TYPE(DictLiteral)
\
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 3ea65707da90369..e1c85d8a08fbf09 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3210,9 +3210,6 @@ static bool isCtorOrDtorName(const FormatToken *Tok) {
 }
 
 void TokenAnnotator::annotate(AnnotatedLine &Line) {
-  for (auto &Child : Line.Children)
-annotate(*Child);
-
   AnnotatingParser Parser(Style, Line, Keywords, Scopes);
   Line.Type = Parser.parseLine();
 
@@ -3233,7 +3230,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 auto *Tok = getFunctionName(Line);
 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+  Tok->setFinalizedType(TT_CtorDtorDeclName);
 }
   }
 
@@ -3246,6 +3243,9 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 
   Line.First->SpacesRequiredBefore = 1;
   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
+
+  for (auto &Child : Line.Children)
+annotate(*Child);
 }
 
 // This function heuristically determines whether 'Current' starts the name of 
a
@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor ||
+isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;
+Tok->setFinalizedType(TT_FunctionDeclarationName);
+  }
   if (AfterLastAttribute &&
   mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
 AfterLastAttribute->MustBreakBefore = true;
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 762729d1c4166a5..1790a9df42b5d14 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -974,11 +974,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
   AlignTokens(
   Style,
   [](Change const &C) {
-if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
-C.Tok->Previous->isNot(tok::tilde)) {
-  return true;
-}
-if (C.Tok->is(TT_FunctionTypeLParen))
+if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
   return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 63ef294ce9d2949..a3723b421f161ef 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10622,6 +10622,12 @@ TEST_F(FormatTes

[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits

owenca wrote:

> https://buildkite.com/llvm-project/clang-ci/builds/4275#018af20e-3d3c-4344-b92d-88ac8b09b484
>  Tests do not pass?

I saw that but don't know why. Did you have the same failures? I'll push 
another commit shortly to trigger buildkite again.

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits


@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor ||
+isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;

owenca wrote:

The whole point of not annotating ctors/dtors as `FunctionDeclarationName` is 
to avoid the problems stated in the commit message above. That is, ctors/dtors 
are treated as non-functions in general. We can add options for (or fix 
existing behaviors of) ctors/dtors if we want them to be treated like functions 
wrt spacing, aligning wrapping, etc. in the future.

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits


@@ -16339,7 +16345,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
 
   verifyFormat("int f();", SpaceFuncDef);
   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
-  verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
+  verifyFormat("A::A() : a(1) {}", SpaceFuncDef);

owenca wrote:

The space was inserted in 086305120887 because ctors/dtors were annotated as 
`FunctionDeclarationName`. Now that we annotate them as `CtorDtorDeclName`, 
they are no longer functions.

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Owen Pan via cfe-commits


@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);

owenca wrote:

Yep! We have 
[moved](https://discourse.llvm.org/t/c-17-in-llvm-code-base/64120/4) to 
[C++17](https://llvm.org/docs/CodingStandards.html#c-standard-versions) for 
more than a year.

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Emilia Kond via cfe-commits

rymiel wrote:

https://buildkite.com/llvm-project/clang-ci/builds/4275#018af20e-3d3c-4344-b92d-88ac8b09b484
 Tests do not pass?

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Björn Schäpers via cfe-commits


@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor ||
+isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;

HazardyKnusperkeks wrote:

Why not this for a CTor?

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Björn Schäpers via cfe-commits


@@ -16339,7 +16345,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
 
   verifyFormat("int f();", SpaceFuncDef);
   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
-  verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
+  verifyFormat("A::A() : a(1) {}", SpaceFuncDef);

HazardyKnusperkeks wrote:

But that is a function definition, is it not?

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-03 Thread Björn Schäpers via cfe-commits


@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);

HazardyKnusperkeks wrote:

A honest question, can we assume to have C++17?

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-02 Thread via cfe-commits

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

Looks like it fixes some bugs in the tests too!

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


[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-02 Thread Owen Pan via cfe-commits

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

>From 15d37075331311020020c5741e2432cd3fc0be74 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 1 Oct 2023 23:01:30 -0700
Subject: [PATCH] [clang-format] Annotate ctors/dtors as CtorDtorDeclName
 instead

After annotating constructors/destructors as FunctionDeclarationName in
commit 08630512088, we have seen several issues because ctors/dtors had been
treated differently than functions in aligning, wrapping, and indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.
---
 clang/lib/Format/FormatToken.h|  1 +
 clang/lib/Format/TokenAnnotator.cpp   | 18 ---
 clang/lib/Format/WhitespaceManager.cpp|  6 +--
 clang/unittests/Format/FormatTest.cpp | 10 +++-
 .../Format/FormatTestMacroExpansion.cpp   |  8 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 54 +++
 6 files changed, 56 insertions(+), 41 deletions(-)

diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index dbd3a6e70f037ef..5877b0a6124742a 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -61,6 +61,7 @@ namespace format {
   TYPE(CSharpStringLiteral)
\
   TYPE(CtorInitializerColon)   
\
   TYPE(CtorInitializerComma)   
\
+  TYPE(CtorDtorDeclName)   
\
   TYPE(DesignatedInitializerLSquare)   
\
   TYPE(DesignatedInitializerPeriod)
\
   TYPE(DictLiteral)
\
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 3ea65707da90369..e1c85d8a08fbf09 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3210,9 +3210,6 @@ static bool isCtorOrDtorName(const FormatToken *Tok) {
 }
 
 void TokenAnnotator::annotate(AnnotatedLine &Line) {
-  for (auto &Child : Line.Children)
-annotate(*Child);
-
   AnnotatingParser Parser(Style, Line, Keywords, Scopes);
   Line.Type = Parser.parseLine();
 
@@ -3233,7 +3230,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 auto *Tok = getFunctionName(Line);
 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+  Tok->setFinalizedType(TT_CtorDtorDeclName);
 }
   }
 
@@ -3246,6 +3243,9 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 
   Line.First->SpacesRequiredBefore = 1;
   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
+
+  for (auto &Child : Line.Children)
+annotate(*Child);
 }
 
 // This function heuristically determines whether 'Current' starts the name of 
a
@@ -3447,9 +3447,13 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor ||
+isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;
+Tok->setFinalizedType(TT_FunctionDeclarationName);
+  }
   if (AfterLastAttribute &&
   mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
 AfterLastAttribute->MustBreakBefore = true;
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 762729d1c4166a5..1790a9df42b5d14 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -974,11 +974,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
   AlignTokens(
   Style,
   [](Change const &C) {
-if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
-C.Tok->Previous->isNot(tok::tilde)) {
-  return true;
-}
-if (C.Tok->is(TT_FunctionTypeLParen))
+if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
   return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 63ef294ce9d2949..a3723b421f161ef 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10622,6 +10622,12 @@ TEST_F(FormatTest, W

[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-01 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format


Changes

After annotating constructors/destructors as FunctionDeclarationName in commit 
08630512088, we have seen several issues because ctors/dtors had been treated 
differently than functions in aligning, wrapping, and indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would 
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.

---
Full diff: https://github.com/llvm/llvm-project/pull/67955.diff


6 Files Affected:

- (modified) clang/lib/Format/FormatToken.h (+1) 
- (modified) clang/lib/Format/TokenAnnotator.cpp (+10-7) 
- (modified) clang/lib/Format/WhitespaceManager.cpp (+1-5) 
- (modified) clang/unittests/Format/FormatTest.cpp (+8-2) 
- (modified) clang/unittests/Format/FormatTestMacroExpansion.cpp (+2-6) 
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+33-21) 


``diff
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index dbd3a6e70f037ef..5877b0a6124742a 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -61,6 +61,7 @@ namespace format {
   TYPE(CSharpStringLiteral)
\
   TYPE(CtorInitializerColon)   
\
   TYPE(CtorInitializerComma)   
\
+  TYPE(CtorDtorDeclName)   
\
   TYPE(DesignatedInitializerLSquare)   
\
   TYPE(DesignatedInitializerPeriod)
\
   TYPE(DictLiteral)
\
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ae2cbbdce934618..dfb059ac8464ed2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3211,9 +3211,6 @@ static bool isCtorOrDtorName(const FormatToken *Tok) {
 }
 
 void TokenAnnotator::annotate(AnnotatedLine &Line) {
-  for (auto &Child : Line.Children)
-annotate(*Child);
-
   AnnotatingParser Parser(Style, Line, Keywords, Scopes);
   Line.Type = Parser.parseLine();
 
@@ -3234,7 +3231,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 auto *Tok = getFunctionName(Line);
 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+  Tok->setFinalizedType(TT_CtorDtorDeclName);
 }
   }
 
@@ -3247,6 +3244,9 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 
   Line.First->SpacesRequiredBefore = 1;
   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
+
+  for (auto &Child : Line.Children)
+annotate(*Child);
 }
 
 // This function heuristically determines whether 'Current' starts the name of 
a
@@ -3446,9 +3446,12 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor || isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;
+Tok->setFinalizedType(TT_FunctionDeclarationName);
+  }
   if (AfterLastAttribute &&
   mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
 AfterLastAttribute->MustBreakBefore = true;
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 762729d1c4166a5..1790a9df42b5d14 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -974,11 +974,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
   AlignTokens(
   Style,
   [](Change const &C) {
-if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
-C.Tok->Previous->isNot(tok::tilde)) {
-  return true;
-}
-if (C.Tok->is(TT_FunctionTypeLParen))
+if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
   return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 0403de8a9a65594..0ab57398a6cc319 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10622,6 +10622,12 @@ TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
   verifyFormat("a::\n"
"a\n"
".

[clang] [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (PR #67955)

2023-10-01 Thread Owen Pan via cfe-commits

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

After annotating constructors/destructors as FunctionDeclarationName in commit 
08630512088, we have seen several issues because ctors/dtors had been treated 
differently than functions in aligning, wrapping, and indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would 
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.

>From 62b4e7b5def02f09580d5308f4dc77c475e423bd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Sun, 1 Oct 2023 23:01:30 -0700
Subject: [PATCH] [clang-format] Annotate ctors/dtors as CtorDtorDeclName
 instead

After annotating constructors/destructors as FunctionDeclarationName in
commit 08630512088, we have seen several issues because ctors/dtors had been
treated differently than functions in aligning, wrapping, and indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.
---
 clang/lib/Format/FormatToken.h|  1 +
 clang/lib/Format/TokenAnnotator.cpp   | 17 +++---
 clang/lib/Format/WhitespaceManager.cpp|  6 +--
 clang/unittests/Format/FormatTest.cpp | 10 +++-
 .../Format/FormatTestMacroExpansion.cpp   |  8 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 54 +++
 6 files changed, 55 insertions(+), 41 deletions(-)

diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index dbd3a6e70f037ef..5877b0a6124742a 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -61,6 +61,7 @@ namespace format {
   TYPE(CSharpStringLiteral)
\
   TYPE(CtorInitializerColon)   
\
   TYPE(CtorInitializerComma)   
\
+  TYPE(CtorDtorDeclName)   
\
   TYPE(DesignatedInitializerLSquare)   
\
   TYPE(DesignatedInitializerPeriod)
\
   TYPE(DictLiteral)
\
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ae2cbbdce934618..dfb059ac8464ed2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3211,9 +3211,6 @@ static bool isCtorOrDtorName(const FormatToken *Tok) {
 }
 
 void TokenAnnotator::annotate(AnnotatedLine &Line) {
-  for (auto &Child : Line.Children)
-annotate(*Child);
-
   AnnotatingParser Parser(Style, Line, Keywords, Scopes);
   Line.Type = Parser.parseLine();
 
@@ -3234,7 +3231,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 auto *Tok = getFunctionName(Line);
 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+  Tok->setFinalizedType(TT_CtorDtorDeclName);
 }
   }
 
@@ -3247,6 +3244,9 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
 
   Line.First->SpacesRequiredBefore = 1;
   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
+
+  for (auto &Child : Line.Children)
+annotate(*Child);
 }
 
 // This function heuristically determines whether 'Current' starts the name of 
a
@@ -3446,9 +3446,12 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok = Tok->Next) {
 if (Tok->Previous->EndsCppAttributeGroup)
   AfterLastAttribute = Tok;
-if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
-  LineIsFunctionDeclaration = true;
-  Tok->setFinalizedType(TT_FunctionDeclarationName);
+if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
+IsCtorOrDtor || isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
+  if (!IsCtorOrDtor) {
+LineIsFunctionDeclaration = true;
+Tok->setFinalizedType(TT_FunctionDeclarationName);
+  }
   if (AfterLastAttribute &&
   mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
 AfterLastAttribute->MustBreakBefore = true;
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 762729d1c4166a5..1790a9df42b5d14 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -974,11 +974,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
   AlignTokens(
   Style,
   [](Change const &C) {
-if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
-C.Tok->Previous->isNot(tok::tilde)) {
-  return true;
-}
-if (C.Tok->is(TT_FunctionTypeLParen))
+if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))