Skip to content

Commit

Permalink
[clang-format] Fix a bug in wrapping function return type
Browse files Browse the repository at this point in the history
Fixes #113766
  • Loading branch information
owenca committed Mar 1, 2025
1 parent d29a1be commit 5bfc035
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5bfc035

Please sign in to comment.