Skip to content

Commit 2aa67b3

Browse files
owencatstellar
authored andcommitted
[clang-format] Restrict the special handling for K&R C to C/C++
Commits 58494c8, f6bc614, and 0fc27ef added special handlings for K&R C function definitions and caused some JavaScript/TypeScript regressions which were addressed in D107267, D108538, and D108620. This patch would have prevented these known regressions and will fix any unknown ones. Differential Revision: https://reviews.llvm.org/D109582 (cherry picked from commit 3205dd3)
1 parent f1342c7 commit 2aa67b3

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
23982398

23992399
// This function heuristically determines whether 'Current' starts the name of a
24002400
// function declaration.
2401-
static bool isFunctionDeclarationName(const FormatToken &Current,
2401+
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
24022402
const AnnotatedLine &Line) {
24032403
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
24042404
for (; Next; Next = Next->Next) {
@@ -2476,14 +2476,21 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
24762476
if (Next->MatchingParen->Next &&
24772477
Next->MatchingParen->Next->is(TT_PointerOrReference))
24782478
return true;
2479-
// Check for K&R C function definitions, e.g.:
2479+
2480+
// Check for K&R C function definitions (and C++ function definitions with
2481+
// unnamed parameters), e.g.:
24802482
// int f(i)
24812483
// {
24822484
// return i + 1;
24832485
// }
2484-
if (Next->Next && Next->Next->is(tok::identifier) &&
2486+
// bool g(size_t = 0, bool b = false)
2487+
// {
2488+
// return !b;
2489+
// }
2490+
if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
24852491
!Line.endsWith(tok::semi))
24862492
return true;
2493+
24872494
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
24882495
Tok = Tok->Next) {
24892496
if (Tok->is(TT_TypeDeclarationParen))
@@ -2544,7 +2551,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
25442551
calculateArrayInitializerColumnList(Line);
25452552

25462553
while (Current) {
2547-
if (isFunctionDeclarationName(*Current, Line))
2554+
if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
25482555
Current->setType(TT_FunctionDeclarationName);
25492556
if (Current->is(TT_LineComment)) {
25502557
if (Current->Previous->is(BK_BracedInit) &&

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8253,6 +8253,12 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
82538253
verifyFormat("int\n"
82548254
"f(a)",
82558255
Style);
8256+
verifyFormat("bool\n"
8257+
"f(size_t = 0, bool b = false)\n"
8258+
"{\n"
8259+
" return !b;\n"
8260+
"}",
8261+
Style);
82568262

82578263
// The return breaking style doesn't affect:
82588264
// * function and object definitions with attribute-like macros

0 commit comments

Comments
 (0)