Author: Emilia Dreamer Date: 2023-01-11T06:00:16+02:00 New Revision: 0904e0bac8314249ee7c1b0658f1a1cc92ccd050
URL: https://github.com/llvm/llvm-project/commit/0904e0bac8314249ee7c1b0658f1a1cc92ccd050 DIFF: https://github.com/llvm/llvm-project/commit/0904e0bac8314249ee7c1b0658f1a1cc92ccd050.diff LOG: [clang-format] Properly handle the C11 _Generic keyword. This patch properly recognizes the generic selection expression introduced in C11, by adding an additional token type for the colons present in such expressions. Previously, they would be recognized as "inline ASM colons" purely by the fact that those are the last thing checked for. I tried to avoid adding an addition token type, but since colons by default like having spaces around them, I chose to add a new type so that no space is added after the type selector. Currently, no aspect of the formatting of these expressions in able to be configured, as I'm not sure what could even be configured here. One notable thing is that association list is always formatted as either entirely on one line, if it can fit, or with line breaks after every comma in the expression (also after the controlling expr.) This visually makes them more similar to switch statements when long, matching the behaviour of the selection expression, being that of a sort of switch on types, but also allows for terseness when only selecting for a few things. Fixes https://github.com/llvm/llvm-project/issues/18080 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D139211 Added: Modified: clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/FormatToken.h clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 04244c146827..833b10e3a578 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1538,6 +1538,12 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace); } + // Special case for generic selection expressions, its comma-separated + // expressions are not aligned to the opening paren like regular calls, but + // rather continuation-indented relative to the _Generic keyword. + if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic)) + NewParenState.Indent = CurrentState.LastSpace; + if (Previous && (Previous->getPrecedence() == prec::Assignment || Previous->isOneOf(tok::kw_return, TT_RequiresClause) || @@ -1684,8 +1690,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, (State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) || (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList); + bool GenericSelection = + Current.getPreviousNonComment() && + Current.getPreviousNonComment()->is(tok::kw__Generic); + AvoidBinPacking = - (CurrentState.IsCSharpGenericTypeConstraint) || + (CurrentState.IsCSharpGenericTypeConstraint) || GenericSelection || (Style.isJavaScript() && EndsInComma) || (State.Line->MustBeDeclaration && !BinPackDeclaration) || (!State.Line->MustBeDeclaration && !Style.BinPackArguments) || diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index e8c6cd9a5d4b..6de9dfa383d5 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -70,6 +70,8 @@ namespace format { TYPE(FunctionLBrace) \ TYPE(FunctionLikeOrFreestandingMacro) \ TYPE(FunctionTypeLParen) \ + /* The colons as part of a C11 _Generic selection */ \ + TYPE(GenericSelectionColon) \ /* The colon at the end of a goto label or a case label. Currently only used \ * for Verilog. */ \ TYPE(GotoLabelColon) \ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index c081b24d3372..8bd7bcf307db 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -322,6 +322,10 @@ class AnnotatingParser { Contexts.back().IsExpression = false; } else if (OpeningParen.is(TT_RequiresExpressionLParen)) { Contexts.back().IsExpression = false; + } else if (OpeningParen.Previous && + OpeningParen.Previous->is(tok::kw__Generic)) { + Contexts.back().ContextType = Context::C11GenericSelection; + Contexts.back().IsExpression = true; } else if (Line.InPPDirective && (!OpeningParen.Previous || !OpeningParen.Previous->is(tok::identifier))) { @@ -1027,6 +1031,8 @@ class AnnotatingParser { } } else if (Contexts.back().ColonIsForRangeExpr) { Tok->setType(TT_RangeBasedForLoopColon); + } else if (Contexts.back().ContextType == Context::C11GenericSelection) { + Tok->setType(TT_GenericSelectionColon); } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { Tok->setType(TT_BitFieldColon); } else if (Contexts.size() == 1 && @@ -1626,6 +1632,8 @@ class AnnotatingParser { StructArrayInitializer, // Like in `static_cast<int>`. TemplateArgument, + // C11 _Generic selection. + C11GenericSelection, } ContextType = Unknown; }; @@ -3254,7 +3262,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return 100; } if (Left.is(tok::l_paren) && Left.Previous && - (Left.Previous->is(tok::kw_for) || Left.Previous->isIf())) { + (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) || + Left.Previous->isIf())) { return 1000; } if (Left.is(tok::equal) && InFunctionDecl) @@ -4163,6 +4172,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return false; if (Right.is(TT_CSharpNamedArgumentColon)) return false; + if (Right.is(TT_GenericSelectionColon)) + return false; if (Right.is(TT_BitFieldColon)) { return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index b39cae1ebe3f..5aa50cc6f9fd 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -23012,6 +23012,41 @@ TEST_F(FormatTest, AtomicQualifier) { verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); } +TEST_F(FormatTest, C11Generic) { + verifyFormat("_Generic(x, int: 1, default: 0)"); + verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)"); + verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);"); + verifyFormat("_Generic(x, int: f1, const int: f2)();"); + verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);"); + + verifyFormat("_Generic(x,\n" + " float: f,\n" + " default: d,\n" + " long double: ld,\n" + " float _Complex: fc,\n" + " double _Complex: dc,\n" + " long double _Complex: ldc)"); + + FormatStyle Style = getLLVMStyle(); + Style.ColumnLimit = 40; + verifyFormat("#define LIMIT_MAX(T) \\\n" + " _Generic(((T)0), \\\n" + " unsigned int: UINT_MAX, \\\n" + " unsigned long: ULONG_MAX, \\\n" + " unsigned long long: ULLONG_MAX)", + Style); + verifyFormat("_Generic(x,\n" + " struct A: 1,\n" + " void (*)(void): 2);", + Style); + + Style.ContinuationIndentWidth = 2; + verifyFormat("_Generic(x,\n" + " struct A: 1,\n" + " void (*)(void): 2);", + Style); +} + TEST_F(FormatTest, AmbersandInLamda) { // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 FormatStyle AlignStyle = getLLVMStyle(); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 66903f1fe898..3ed6521d48c1 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1152,6 +1152,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) { EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName); } +TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) { + auto Tokens = annotate("_Generic(x, int: 1, default: 0)"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::kw__Generic, TT_Unknown); + EXPECT_TOKEN(Tokens[5], tok::colon, TT_GenericSelectionColon); + EXPECT_TOKEN(Tokens[9], tok::colon, TT_GenericSelectionColon); +} + TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) { auto Annotate = [this](llvm::StringRef Code) { return annotate(Code, getLLVMStyle(FormatStyle::LK_Verilog)); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits