owenpan created this revision.
owenpan added reviewers: krasimir, MyDeveloperDay, HazardyKnusperkeks.
owenpan added a project: clang-format.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Commits 58494c856a15
<https://reviews.llvm.org/rG58494c856a15f5b0e886c7baf5d505ac6c05dfe5>,
f6bc614546e1
<https://reviews.llvm.org/rGf6bc614546e169bb1b17a29c422ebace038e6c62>, and
0fc27ef19670
<https://reviews.llvm.org/rG0fc27ef19670676689d3317948c81eb171bb25f8> added
special handlings for K&R C function definitions and caused some JavaScript
regressions which were addressed in https://reviews.llvm.org/D107267 and
https://reviews.llvm.org/D108538. This patch could have prevented these known
regressions and will fix any unknown ones.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D109582
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8277,6 +8277,12 @@
verifyFormat("int\n"
"f(a)",
Style);
+ verifyFormat("bool\n"
+ "f(size_t = 0, bool b = false)\n"
+ "{\n"
+ " return !b;\n"
+ "}",
+ Style);
// The return breaking style doesn't affect:
// * function and object definitions with attribute-like macros
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2398,7 +2398,7 @@
// This function heuristically determines whether 'Current' starts the name of
a
// function declaration.
-static bool isFunctionDeclarationName(const FormatToken &Current,
+static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
const AnnotatedLine &Line) {
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
@@ -2476,14 +2476,21 @@
if (Next->MatchingParen->Next &&
Next->MatchingParen->Next->is(TT_PointerOrReference))
return true;
- // Check for K&R C function definitions, e.g.:
+
+ // Check for K&R C function definitions (and C++ function definitions with
+ // unnamed parameters), e.g.:
// int f(i)
// {
// return i + 1;
// }
- if (Next->Next && Next->Next->is(tok::identifier) &&
+ // bool g(size_t = 0, bool b = false)
+ // {
+ // return !b;
+ // }
+ if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
!Line.endsWith(tok::semi))
return true;
+
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
Tok = Tok->Next) {
if (Tok->is(TT_TypeDeclarationParen))
@@ -2544,7 +2551,7 @@
calculateArrayInitializerColumnList(Line);
while (Current) {
- if (isFunctionDeclarationName(*Current, Line))
+ if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
Current->setType(TT_FunctionDeclarationName);
if (Current->is(TT_LineComment)) {
if (Current->Previous->is(BK_BracedInit) &&
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8277,6 +8277,12 @@
verifyFormat("int\n"
"f(a)",
Style);
+ verifyFormat("bool\n"
+ "f(size_t = 0, bool b = false)\n"
+ "{\n"
+ " return !b;\n"
+ "}",
+ Style);
// The return breaking style doesn't affect:
// * function and object definitions with attribute-like macros
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2398,7 +2398,7 @@
// This function heuristically determines whether 'Current' starts the name of a
// function declaration.
-static bool isFunctionDeclarationName(const FormatToken &Current,
+static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
const AnnotatedLine &Line) {
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
@@ -2476,14 +2476,21 @@
if (Next->MatchingParen->Next &&
Next->MatchingParen->Next->is(TT_PointerOrReference))
return true;
- // Check for K&R C function definitions, e.g.:
+
+ // Check for K&R C function definitions (and C++ function definitions with
+ // unnamed parameters), e.g.:
// int f(i)
// {
// return i + 1;
// }
- if (Next->Next && Next->Next->is(tok::identifier) &&
+ // bool g(size_t = 0, bool b = false)
+ // {
+ // return !b;
+ // }
+ if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
!Line.endsWith(tok::semi))
return true;
+
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
Tok = Tok->Next) {
if (Tok->is(TT_TypeDeclarationParen))
@@ -2544,7 +2551,7 @@
calculateArrayInitializerColumnList(Line);
while (Current) {
- if (isFunctionDeclarationName(*Current, Line))
+ if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
Current->setType(TT_FunctionDeclarationName);
if (Current->is(TT_LineComment)) {
if (Current->Previous->is(BK_BracedInit) &&
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits