Author: owenca Date: 2021-09-10T15:51:35-07:00 New Revision: 3205dd3d59b3cc36f96b7eff6387de8d2f42825f
URL: https://github.com/llvm/llvm-project/commit/3205dd3d59b3cc36f96b7eff6387de8d2f42825f DIFF: https://github.com/llvm/llvm-project/commit/3205dd3d59b3cc36f96b7eff6387de8d2f42825f.diff LOG: [clang-format] Restrict the special handling for K&R C to C/C++ Commits 58494c856a15, f6bc614546e1, and 0fc27ef19670 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 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 044a96e0d44e2..c8d4ee47ee7e3 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2398,7 +2398,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { // 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 @@ static bool isFunctionDeclarationName(const FormatToken &Current, 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 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 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) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 45ed0f9de7d36..80fa21d3a2118 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8277,6 +8277,12 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits