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 @@ -3242,6 +3242,10 @@ LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17; LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20; LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20; + // Turning on digraphs in standards before C++0x is error-prone, because e.g. + // the sequence "<::" will be unconditionally treated as "[:". + // Cf. Lexer::LexTokenInternal. + LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; LangOpts.LineComment = 1; bool AlternativeOperators = Style.isCpp(); 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 @@ -24219,6 +24219,16 @@ Style); } +TEST_F(FormatTest, UnderstandsDigraphs) { + verifyFormat("int arr<:5:> = {};"); + verifyFormat("int arr[5] = <%%>;"); + verifyFormat("int arr<:::qualified_variable:> = {};"); + verifyFormat("int arr[::qualified_variable] = <%%>;"); + verifyFormat("%:include
"); + verifyFormat("%:define A x##y"); + verifyFormat("#define A x%:%:y"); +} + } // namespace } // namespace format } // namespace clang