Skip to content

Commit 2ca0855

Browse files
authored
[clang-format] Fix a bug in wrapping function return type (#129374)
Fixes #113766
1 parent 136f257 commit 2ca0855

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
628628
// name.
629629
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
630630
CurrentState.BreakBeforeParameter) {
631+
for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous)
632+
if (Tok->FirstAfterPPDirectiveLine || Tok->is(TT_LineComment))
633+
return false;
634+
631635
return true;
632636
}
633637

clang/lib/Format/FormatToken.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ struct FormatToken {
594594
/// Has "\n\f\n" or "\n\f\r\n" before TokenText.
595595
bool HasFormFeedBefore = false;
596596

597+
/// Is the first token after a PPDirective line.
598+
bool FirstAfterPPDirectiveLine = false;
599+
597600
/// Number of optional braces to be inserted after this token:
598601
/// -1: a single left brace
599602
/// 0: no braces

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5091,8 +5091,10 @@ UnwrappedLineParser::parseMacroCall() {
50915091
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
50925092
Line->Tokens.push_back(UnwrappedLineNode(Tok));
50935093
if (MustBreakBeforeNextToken) {
5094-
Line->Tokens.back().Tok->MustBreakBefore = true;
5095-
Line->Tokens.back().Tok->MustBreakBeforeFinalized = true;
5094+
auto &Tok = *Line->Tokens.back().Tok;
5095+
Tok.MustBreakBefore = true;
5096+
Tok.MustBreakBeforeFinalized = true;
5097+
Tok.FirstAfterPPDirectiveLine = true;
50965098
MustBreakBeforeNextToken = false;
50975099
}
50985100
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6832,6 +6832,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
68326832
" param3) {\n"
68336833
" f();\n"
68346834
"}");
6835+
6836+
verifyFormat("#ifdef __cplusplus\n"
6837+
"extern \"C\"\n"
6838+
"#endif\n"
6839+
" void f();");
68356840
}
68366841

68376842
TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
@@ -8539,6 +8544,7 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
85398544
verifyGoogleFormat(
85408545
"SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
85418546
" const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8547+
85428548
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
85438549
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
85448550
verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
@@ -8552,6 +8558,9 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
85528558
"aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
85538559
" aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
85548560

8561+
verifyFormat("extern \"C\" //\n"
8562+
" void f();");
8563+
85558564
FormatStyle Style = getLLVMStyle();
85568565
Style.PointerAlignment = FormatStyle::PAS_Left;
85578566
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"

0 commit comments

Comments
 (0)