llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) <details> <summary>Changes</summary> Fixes #<!-- -->113766 --- Full diff: https://github.com/llvm/llvm-project/pull/129374.diff 4 Files Affected: - (modified) clang/lib/Format/ContinuationIndenter.cpp (+4) - (modified) clang/lib/Format/FormatToken.h (+3) - (modified) clang/lib/Format/UnwrappedLineParser.cpp (+4-2) - (modified) clang/unittests/Format/FormatTest.cpp (+5) ``````````diff 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) { `````````` </details> https://github.com/llvm/llvm-project/pull/129374 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits