[clang] [clang-format] Fix a bug in wrapping function return type (PR #129374)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 1 01:55:34 PST 2025


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

Fixes #113766

>From 5bfc03509419452a22f2c9c663e53152390a310a Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 1 Mar 2025 01:53:01 -0800
Subject: [PATCH] [clang-format] Fix a bug in wrapping function return type

Fixes #113766
---
 clang/lib/Format/ContinuationIndenter.cpp | 4 ++++
 clang/lib/Format/FormatToken.h            | 3 +++
 clang/lib/Format/UnwrappedLineParser.cpp  | 6 ++++--
 clang/unittests/Format/FormatTest.cpp     | 5 +++++
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index d49128c2b40f8..fb4bcf965ac33 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -628,6 +628,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
       // name.
       !Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
       CurrentState.BreakBeforeParameter) {
+    for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous)
+      if (Tok->FirstAfterPPDirectiveLine || Tok->is(TT_LineComment))
+        return false;
+
     return true;
   }
 
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 02429970599c0..2cace7c3f060e 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -594,6 +594,9 @@ struct FormatToken {
   /// Has "\n\f\n" or "\n\f\r\n" before TokenText.
   bool HasFormFeedBefore = false;
 
+  /// Is the first token after a PPDirective line.
+  bool FirstAfterPPDirectiveLine = false;
+
   /// Number of optional braces to be inserted after this token:
   ///   -1: a single left brace
   ///    0: no braces
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 16f19e955bf55..2da0432816df7 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -5091,8 +5091,10 @@ UnwrappedLineParser::parseMacroCall() {
 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
   Line->Tokens.push_back(UnwrappedLineNode(Tok));
   if (MustBreakBeforeNextToken) {
-    Line->Tokens.back().Tok->MustBreakBefore = true;
-    Line->Tokens.back().Tok->MustBreakBeforeFinalized = true;
+    auto &Tok = *Line->Tokens.back().Tok;
+    Tok.MustBreakBefore = true;
+    Tok.MustBreakBeforeFinalized = true;
+    Tok.FirstAfterPPDirectiveLine = true;
     MustBreakBeforeNextToken = false;
   }
 }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 537be3632f439..15b58fc584997 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6832,6 +6832,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
                "                  param3) {\n"
                "  f();\n"
                "}");
+
+  verifyFormat("#ifdef __cplusplus\n"
+               "extern \"C\"\n"
+               "#endif\n"
+               "    void f();");
 }
 
 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {



More information about the cfe-commits mailing list