diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -2223,6 +2223,10 @@ Use features of C++11, C++14 and C++1z (e.g. ``A>`` instead of ``A >``). + * ``LS_Cpp20`` (in configuration: ``Cpp20``) + Use features of C++20 and C++2a (e.g.: treating ``co_yield`` as a keyword, + not an identifier, so ``co_yield++ i`` is formatted as ``co_yield ++i``). + * ``LS_Auto`` (in configuration: ``Auto``) Automatic detection based on the input. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1875,6 +1875,10 @@ /// Use features of C++11, C++14 and C++1z (e.g. ``A>`` instead of /// ``A >``). LS_Cpp11, + /// Use features of C++20 and C++2a (e.g.: treating ``co_yield`` as a + /// keyword, not an identifier, so ``co_yield++ i`` is formatted as + /// ``co_yield ++i``). + LS_Cpp20, /// Automatic detection based on the input. LS_Auto }; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -71,6 +71,8 @@ IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); + IO.enumCase(Value, "Cpp20", FormatStyle::LS_Cpp20); + IO.enumCase(Value, "C++20", FormatStyle::LS_Cpp20); IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); } }; @@ -2368,7 +2370,7 @@ LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; LangOpts.CPlusPlus17 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; - LangOpts.CPlusPlus2a = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; + LangOpts.CPlusPlus2a = Style.Standard == FormatStyle::LS_Cpp20 ? 1 : 0; LangOpts.LineComment = 1; bool AlternativeOperators = Style.isCpp(); LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2859,7 +2859,8 @@ (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) return !Style.Cpp11BracedListStyle; return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && - (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles); + (Style.Standard == FormatStyle::LS_Cpp03 || + Style.Standard == FormatStyle::LS_Auto || Style.SpacesInAngles); } if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3713,10 +3713,20 @@ "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" "}"); + + // Only in C++20 and above is <=> treated as as operator. verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <= >\n" + " 5) {\n" "}"); + FormatStyle Cpp20 = getLLVMStyle(); + Cpp20.Standard = FormatStyle::LS_Cpp20; + verifyFormat( + "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" + "}", Cpp20); + // Even explicit parentheses stress the precedence enough to make the // additional break unnecessary. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" @@ -3736,10 +3746,15 @@ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" " 5) {\n" "}"); + // Only in C++20 and above is <=> treated as as operator. + verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=\n" + " > 5) {\n" + "}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" " 5) {\n" - "}"); + "}", Cpp20); FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.BinPackParameters = false; @@ -13792,6 +13807,18 @@ verifyFormat("STACK_OF(int*)* a;", Macros); } +TEST_F(FormatTest, Coroutines) { + FormatStyle Cpp20 = getLLVMStyle(); + Cpp20.Standard = FormatStyle::LS_Cpp20; + + verifyFormat("co_yield++ i;"); + verifyFormat("co_yield ++i;", Cpp20); + + verifyFormat("co_await[]() { co_return; }\n" + "();"); + verifyFormat("co_await []() { co_return; }();", Cpp20); +} + } // end namespace } // end namespace format } // end namespace clang