Skip to content

Commit 5bfc035

Browse files
committed
[clang-format] Fix a bug in wrapping function return type
Fixes #113766
1 parent d29a1be commit 5bfc035

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-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: 5 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) {

0 commit comments

Comments
 (0)