[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-07 Thread Erich Reitz via cfe-commits

https://github.com/Erich-Reitz created 
https://github.com/llvm/llvm-project/pull/94776

Addresses issue #45002. 

When `readToken()` sees a preprocessor directive, it eagerly flushes the 
comments.  This changes the behavior if it is within a braceless block.

Keeps behavior of 
```
void f() {
  if (foo) 
// test
#ifdef
  bax(); 
#endif
}
``` 
formatted as 
```
void f() {
  if (foo) 
  // test
#ifdef
  bax(); 
#endif
}
```
for (specifically) comments within the braceless block 

>From 6c910c8b40be79e3d573f6953860f60ebd27b39f Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:04:33 -0400
Subject: [PATCH 1/3] delay flushing comments before ifdef after braceless if;
 align with token that begins conditional

---
 clang/lib/Format/UnwrappedLineParser.cpp | 19 +++
 clang/lib/Format/UnwrappedLineParser.h   |  2 ++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..7bc066787bf46 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -354,6 +354,7 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() 
const {
 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
  IfStmtKind *IfKind,
  FormatToken **IfLeftBrace) {
+
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
   const bool IsPrecededByCommentOrPPDirective =
@@ -385,6 +386,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken 
*OpeningBrace,
 };
 
 switch (Kind) {
+
 case tok::comment:
   nextToken();
   addUnwrappedLine();
@@ -1419,6 +1421,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
 void UnwrappedLineParser::parseStructuralElement(
 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
+
   if (Style.Language == FormatStyle::LK_TableGen &&
   FormatTok->is(tok::pp_include)) {
 nextToken();
@@ -1696,6 +1699,7 @@ void UnwrappedLineParser::parseStructuralElement(
 
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
+
   do {
 const FormatToken *Previous = FormatTok->Previous;
 switch (FormatTok->Tok.getKind()) {
@@ -2705,6 +2709,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 
   addUnwrappedLine();
   ++Line->Level;
+  ++UnBracedBodyDepth;
   parseStructuralElement();
 
   if (Tok) {
@@ -2719,11 +2724,11 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 assert(Tok);
 ++Tok->BraceCount;
   }
-
   if (CheckEOF && eof())
 addUnwrappedLine();
 
   --Line->Level;
+  --UnBracedBodyDepth;
 }
 
 static void markOptionalBraces(FormatToken *LeftBrace) {
@@ -4736,6 +4741,7 @@ void UnwrappedLineParser::distributeComments(
   // the two lines about b form a maximal trail, so there are two sections, the
   // first one consisting of the single comment "// line about a" and the
   // second one consisting of the next two comments.
+
   if (Comments.empty())
 return;
   bool ShouldPushCommentsInCurrentLine = true;
@@ -4811,8 +4817,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
(!Style.isVerilog() ||
 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
FirstNonCommentOnLine) {
-  distributeComments(Comments, FormatTok);
-  Comments.clear();
+  if (!UnBracedBodyDepth) {
+distributeComments(Comments, FormatTok);
+Comments.clear();
+  }
   // If there is an unfinished unwrapped line, we flush the preprocessor
   // directives only after that unwrapped line was finished later.
   bool SwitchToPreprocessorLines = !Line->Tokens.empty();
@@ -4828,7 +4836,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
   PPBranchLevel > 0) {
 Line->Level += PPBranchLevel;
   }
-  flushComments(isOnNewLine(*FormatTok));
+  if (!UnBracedBodyDepth) {
+flushComments(isOnNewLine(*FormatTok));
+  }
+
   parsePPDirective();
   PreviousWasComment = FormatTok->is(tok::comment);
   FirstNonCommentOnLine = IsFirstNonCommentOnLine(
diff --git a/clang/lib/Format/UnwrappedLineParser.h 
b/clang/lib/Format/UnwrappedLineParser.h
index d7963a4211bb9..4d87896870a3e 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -338,6 +338,8 @@ class UnwrappedLineParser {
   // `decltype(auto)`.
   bool IsDecltypeAutoFunction = false;
 
+  int UnBracedBodyDepth = 0;
+
   // Represents preprocessor branch type, so we can find matching
   // #if/#else/#endif directives.
   enum PPBranchKind {

>From b9d52022e1caf314cb3f24f03775c8baf5da1c4a Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:08:13 -0400
S

[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-07 Thread Erich Reitz via cfe-commits

https://github.com/Erich-Reitz updated 
https://github.com/llvm/llvm-project/pull/94776

>From 6c910c8b40be79e3d573f6953860f60ebd27b39f Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:04:33 -0400
Subject: [PATCH 1/4] delay flushing comments before ifdef after braceless if;
 align with token that begins conditional

---
 clang/lib/Format/UnwrappedLineParser.cpp | 19 +++
 clang/lib/Format/UnwrappedLineParser.h   |  2 ++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..7bc066787bf46 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -354,6 +354,7 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() 
const {
 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
  IfStmtKind *IfKind,
  FormatToken **IfLeftBrace) {
+
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
   const bool IsPrecededByCommentOrPPDirective =
@@ -385,6 +386,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken 
*OpeningBrace,
 };
 
 switch (Kind) {
+
 case tok::comment:
   nextToken();
   addUnwrappedLine();
@@ -1419,6 +1421,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
 void UnwrappedLineParser::parseStructuralElement(
 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
+
   if (Style.Language == FormatStyle::LK_TableGen &&
   FormatTok->is(tok::pp_include)) {
 nextToken();
@@ -1696,6 +1699,7 @@ void UnwrappedLineParser::parseStructuralElement(
 
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
+
   do {
 const FormatToken *Previous = FormatTok->Previous;
 switch (FormatTok->Tok.getKind()) {
@@ -2705,6 +2709,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 
   addUnwrappedLine();
   ++Line->Level;
+  ++UnBracedBodyDepth;
   parseStructuralElement();
 
   if (Tok) {
@@ -2719,11 +2724,11 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 assert(Tok);
 ++Tok->BraceCount;
   }
-
   if (CheckEOF && eof())
 addUnwrappedLine();
 
   --Line->Level;
+  --UnBracedBodyDepth;
 }
 
 static void markOptionalBraces(FormatToken *LeftBrace) {
@@ -4736,6 +4741,7 @@ void UnwrappedLineParser::distributeComments(
   // the two lines about b form a maximal trail, so there are two sections, the
   // first one consisting of the single comment "// line about a" and the
   // second one consisting of the next two comments.
+
   if (Comments.empty())
 return;
   bool ShouldPushCommentsInCurrentLine = true;
@@ -4811,8 +4817,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
(!Style.isVerilog() ||
 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
FirstNonCommentOnLine) {
-  distributeComments(Comments, FormatTok);
-  Comments.clear();
+  if (!UnBracedBodyDepth) {
+distributeComments(Comments, FormatTok);
+Comments.clear();
+  }
   // If there is an unfinished unwrapped line, we flush the preprocessor
   // directives only after that unwrapped line was finished later.
   bool SwitchToPreprocessorLines = !Line->Tokens.empty();
@@ -4828,7 +4836,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
   PPBranchLevel > 0) {
 Line->Level += PPBranchLevel;
   }
-  flushComments(isOnNewLine(*FormatTok));
+  if (!UnBracedBodyDepth) {
+flushComments(isOnNewLine(*FormatTok));
+  }
+
   parsePPDirective();
   PreviousWasComment = FormatTok->is(tok::comment);
   FirstNonCommentOnLine = IsFirstNonCommentOnLine(
diff --git a/clang/lib/Format/UnwrappedLineParser.h 
b/clang/lib/Format/UnwrappedLineParser.h
index d7963a4211bb9..4d87896870a3e 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -338,6 +338,8 @@ class UnwrappedLineParser {
   // `decltype(auto)`.
   bool IsDecltypeAutoFunction = false;
 
+  int UnBracedBodyDepth = 0;
+
   // Represents preprocessor branch type, so we can find matching
   // #if/#else/#endif directives.
   enum PPBranchKind {

>From b9d52022e1caf314cb3f24f03775c8baf5da1c4a Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:08:13 -0400
Subject: [PATCH 2/4] whitespace formatting

---
 clang/lib/Format/UnwrappedLineParser.cpp | 7 +++
 clang/lib/Format/UnwrappedLineParser.h   | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 7bc066787bf46..b17ef33f95e98 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/l

[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-07 Thread Erich Reitz via cfe-commits

https://github.com/Erich-Reitz updated 
https://github.com/llvm/llvm-project/pull/94776

>From 6c910c8b40be79e3d573f6953860f60ebd27b39f Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:04:33 -0400
Subject: [PATCH 1/5] delay flushing comments before ifdef after braceless if;
 align with token that begins conditional

---
 clang/lib/Format/UnwrappedLineParser.cpp | 19 +++
 clang/lib/Format/UnwrappedLineParser.h   |  2 ++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..7bc066787bf46 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -354,6 +354,7 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() 
const {
 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
  IfStmtKind *IfKind,
  FormatToken **IfLeftBrace) {
+
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
   const bool IsPrecededByCommentOrPPDirective =
@@ -385,6 +386,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken 
*OpeningBrace,
 };
 
 switch (Kind) {
+
 case tok::comment:
   nextToken();
   addUnwrappedLine();
@@ -1419,6 +1421,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
 void UnwrappedLineParser::parseStructuralElement(
 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
+
   if (Style.Language == FormatStyle::LK_TableGen &&
   FormatTok->is(tok::pp_include)) {
 nextToken();
@@ -1696,6 +1699,7 @@ void UnwrappedLineParser::parseStructuralElement(
 
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
+
   do {
 const FormatToken *Previous = FormatTok->Previous;
 switch (FormatTok->Tok.getKind()) {
@@ -2705,6 +2709,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 
   addUnwrappedLine();
   ++Line->Level;
+  ++UnBracedBodyDepth;
   parseStructuralElement();
 
   if (Tok) {
@@ -2719,11 +2724,11 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 assert(Tok);
 ++Tok->BraceCount;
   }
-
   if (CheckEOF && eof())
 addUnwrappedLine();
 
   --Line->Level;
+  --UnBracedBodyDepth;
 }
 
 static void markOptionalBraces(FormatToken *LeftBrace) {
@@ -4736,6 +4741,7 @@ void UnwrappedLineParser::distributeComments(
   // the two lines about b form a maximal trail, so there are two sections, the
   // first one consisting of the single comment "// line about a" and the
   // second one consisting of the next two comments.
+
   if (Comments.empty())
 return;
   bool ShouldPushCommentsInCurrentLine = true;
@@ -4811,8 +4817,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
(!Style.isVerilog() ||
 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
FirstNonCommentOnLine) {
-  distributeComments(Comments, FormatTok);
-  Comments.clear();
+  if (!UnBracedBodyDepth) {
+distributeComments(Comments, FormatTok);
+Comments.clear();
+  }
   // If there is an unfinished unwrapped line, we flush the preprocessor
   // directives only after that unwrapped line was finished later.
   bool SwitchToPreprocessorLines = !Line->Tokens.empty();
@@ -4828,7 +4836,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
   PPBranchLevel > 0) {
 Line->Level += PPBranchLevel;
   }
-  flushComments(isOnNewLine(*FormatTok));
+  if (!UnBracedBodyDepth) {
+flushComments(isOnNewLine(*FormatTok));
+  }
+
   parsePPDirective();
   PreviousWasComment = FormatTok->is(tok::comment);
   FirstNonCommentOnLine = IsFirstNonCommentOnLine(
diff --git a/clang/lib/Format/UnwrappedLineParser.h 
b/clang/lib/Format/UnwrappedLineParser.h
index d7963a4211bb9..4d87896870a3e 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -338,6 +338,8 @@ class UnwrappedLineParser {
   // `decltype(auto)`.
   bool IsDecltypeAutoFunction = false;
 
+  int UnBracedBodyDepth = 0;
+
   // Represents preprocessor branch type, so we can find matching
   // #if/#else/#endif directives.
   enum PPBranchKind {

>From b9d52022e1caf314cb3f24f03775c8baf5da1c4a Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:08:13 -0400
Subject: [PATCH 2/5] whitespace formatting

---
 clang/lib/Format/UnwrappedLineParser.cpp | 7 +++
 clang/lib/Format/UnwrappedLineParser.h   | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 7bc066787bf46..b17ef33f95e98 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/l

[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-07 Thread Erich Reitz via cfe-commits


@@ -796,6 +796,24 @@ TEST_F(FormatTestComments, 
ParsesCommentsAdjacentToPPDirectives) {
 format("namespace {}\n   /* Test */#define A"));
 }
 
+
+TEST_F(FormatTestComments, DeIdentsCommentBeforeIfdefAfterBracelessIf) {
+  EXPECT_EQ("void f() {\n"

Erich-Reitz wrote:

Updated to do so. Not sure if I need to give a style.

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


[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-08 Thread Erich Reitz via cfe-commits

https://github.com/Erich-Reitz updated 
https://github.com/llvm/llvm-project/pull/94776

>From 6c910c8b40be79e3d573f6953860f60ebd27b39f Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:04:33 -0400
Subject: [PATCH 1/6] delay flushing comments before ifdef after braceless if;
 align with token that begins conditional

---
 clang/lib/Format/UnwrappedLineParser.cpp | 19 +++
 clang/lib/Format/UnwrappedLineParser.h   |  2 ++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index b15a87327240b..7bc066787bf46 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -354,6 +354,7 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() 
const {
 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
  IfStmtKind *IfKind,
  FormatToken **IfLeftBrace) {
+
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
   const bool IsPrecededByCommentOrPPDirective =
@@ -385,6 +386,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken 
*OpeningBrace,
 };
 
 switch (Kind) {
+
 case tok::comment:
   nextToken();
   addUnwrappedLine();
@@ -1419,6 +1421,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
 void UnwrappedLineParser::parseStructuralElement(
 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
+
   if (Style.Language == FormatStyle::LK_TableGen &&
   FormatTok->is(tok::pp_include)) {
 nextToken();
@@ -1696,6 +1699,7 @@ void UnwrappedLineParser::parseStructuralElement(
 
   const bool InRequiresExpression =
   OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
+
   do {
 const FormatToken *Previous = FormatTok->Previous;
 switch (FormatTok->Tok.getKind()) {
@@ -2705,6 +2709,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 
   addUnwrappedLine();
   ++Line->Level;
+  ++UnBracedBodyDepth;
   parseStructuralElement();
 
   if (Tok) {
@@ -2719,11 +2724,11 @@ void UnwrappedLineParser::parseUnbracedBody(bool 
CheckEOF) {
 assert(Tok);
 ++Tok->BraceCount;
   }
-
   if (CheckEOF && eof())
 addUnwrappedLine();
 
   --Line->Level;
+  --UnBracedBodyDepth;
 }
 
 static void markOptionalBraces(FormatToken *LeftBrace) {
@@ -4736,6 +4741,7 @@ void UnwrappedLineParser::distributeComments(
   // the two lines about b form a maximal trail, so there are two sections, the
   // first one consisting of the single comment "// line about a" and the
   // second one consisting of the next two comments.
+
   if (Comments.empty())
 return;
   bool ShouldPushCommentsInCurrentLine = true;
@@ -4811,8 +4817,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
(!Style.isVerilog() ||
 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
FirstNonCommentOnLine) {
-  distributeComments(Comments, FormatTok);
-  Comments.clear();
+  if (!UnBracedBodyDepth) {
+distributeComments(Comments, FormatTok);
+Comments.clear();
+  }
   // If there is an unfinished unwrapped line, we flush the preprocessor
   // directives only after that unwrapped line was finished later.
   bool SwitchToPreprocessorLines = !Line->Tokens.empty();
@@ -4828,7 +4836,10 @@ void UnwrappedLineParser::readToken(int LevelDifference) 
{
   PPBranchLevel > 0) {
 Line->Level += PPBranchLevel;
   }
-  flushComments(isOnNewLine(*FormatTok));
+  if (!UnBracedBodyDepth) {
+flushComments(isOnNewLine(*FormatTok));
+  }
+
   parsePPDirective();
   PreviousWasComment = FormatTok->is(tok::comment);
   FirstNonCommentOnLine = IsFirstNonCommentOnLine(
diff --git a/clang/lib/Format/UnwrappedLineParser.h 
b/clang/lib/Format/UnwrappedLineParser.h
index d7963a4211bb9..4d87896870a3e 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -338,6 +338,8 @@ class UnwrappedLineParser {
   // `decltype(auto)`.
   bool IsDecltypeAutoFunction = false;
 
+  int UnBracedBodyDepth = 0;
+
   // Represents preprocessor branch type, so we can find matching
   // #if/#else/#endif directives.
   enum PPBranchKind {

>From b9d52022e1caf314cb3f24f03775c8baf5da1c4a Mon Sep 17 00:00:00 2001
From: Erich Reitz 
Date: Fri, 7 Jun 2024 13:08:13 -0400
Subject: [PATCH 2/6] whitespace formatting

---
 clang/lib/Format/UnwrappedLineParser.cpp | 7 +++
 clang/lib/Format/UnwrappedLineParser.h   | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 7bc066787bf46..b17ef33f95e98 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/l

[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-08 Thread Erich Reitz via cfe-commits


@@ -796,6 +796,44 @@ TEST_F(FormatTestComments, 
ParsesCommentsAdjacentToPPDirectives) {
 format("namespace {}\n   /* Test */#define A"));
 }
 
+TEST_F(FormatTestComments, DeIdentsCommentBeforeIfdefAfterBracelessIf) {
+  verifyFormat("void f() {\n"
+   "  if (true)\n"
+   "int i;\n"
+   "  /* comment */\n"
+   "#ifdef A\n"
+   "  int j;\n"
+   "#endif\n"
+   "}",
+   "void f() {\n"
+   "  if (true)\n"
+   "int i;\n"
+   "/* comment */\n"
+   "#ifdef A\n"
+   "  int j;\n"
+   "#endif\n"
+   "}");
+
+  verifyFormat("void f() {\n"
+   "  if (true)\n"
+   "int i;\n"
+   "  /* comment */\n"

Erich-Reitz wrote:

Okay, I don't think that is the existing behavior. Using `clang-format-18`
```
void f() {
  if (foo) 
a = 3;
/* comment */

  int b = 4; 
}
```
is formatted to
```
void f() {
  if (foo)
a = 3;
  /* comment */

  int b = 4;
}
```

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


[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-12 Thread Erich Reitz via cfe-commits

Erich-Reitz wrote:

> This patch formats
> 
> ```
> void f() {
>   if (foo)
> bar(); // Comment
> #if BAZ
>   baz();
> #endif
> }
> ```
> 
> to
> 
> ```
> void f() {
>   if (foo)
> bar();
> // Comment
> #if BAZ
>   baz();
> #endif
> }
> ```
> 
> The trailing comment shouldn't be wrapped.

thank you for catching that. I am going to close this as I don't know how to 
prevent `parseUnbracedBody` from consuming the ppd below it.

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


[clang] [clang-format] Fix bad comment indentation before ifdef after braceless if (PR #94776)

2024-06-12 Thread Erich Reitz via cfe-commits

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