https://github.com/owenca created https://github.com/llvm/llvm-project/pull/134514
Backport d71ee7d23048ca64d14a7536927a006867cea39a >From 4b705f9a9a06276f9586d628a409814104ce8e3a Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sat, 5 Apr 2025 13:35:45 -0700 Subject: [PATCH] Release/20.x: [clang-format] Set C11 instead of C17 for LK_C Backport d71ee7d23048ca64d14a7536927a006867cea39a --- clang/lib/Format/Format.cpp | 42 +++++++++++-------- clang/lib/Format/FormatToken.cpp | 2 +- clang/lib/Format/TokenAnnotator.cpp | 3 +- clang/lib/Format/TokenAnnotator.h | 4 +- clang/lib/Format/UnwrappedLineParser.cpp | 4 +- clang/unittests/Format/TokenAnnotatorTest.cpp | 6 +++ 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 768e655f65ce7..b97d8928178b5 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3946,34 +3946,42 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, LangOptions getFormattingLangOpts(const FormatStyle &Style) { LangOptions LangOpts; - FormatStyle::LanguageStandard LexingStd = Style.Standard; - if (LexingStd == FormatStyle::LS_Auto) - LexingStd = FormatStyle::LS_Latest; - if (LexingStd == FormatStyle::LS_Latest) + auto LexingStd = Style.Standard; + if (LexingStd == FormatStyle::LS_Auto || LexingStd == FormatStyle::LS_Latest) LexingStd = FormatStyle::LS_Cpp20; - LangOpts.CPlusPlus = 1; - LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11; - LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14; - LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17; - LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20; - LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20; + + const bool SinceCpp11 = LexingStd >= FormatStyle::LS_Cpp11; + const bool SinceCpp20 = LexingStd >= FormatStyle::LS_Cpp20; + + switch (Style.Language) { + case FormatStyle::LK_C: + LangOpts.C11 = 1; + break; + case FormatStyle::LK_Cpp: + case FormatStyle::LK_ObjC: + LangOpts.CXXOperatorNames = 1; + LangOpts.CPlusPlus11 = SinceCpp11; + LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14; + LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17; + LangOpts.CPlusPlus20 = SinceCpp20; + [[fallthrough]]; + default: + LangOpts.CPlusPlus = 1; + } + + LangOpts.Char8 = SinceCpp20; // 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.Digraphs = SinceCpp11; LangOpts.LineComment = 1; - - const auto Language = Style.Language; - LangOpts.C17 = Language == FormatStyle::LK_C; - LangOpts.CXXOperatorNames = - Language == FormatStyle::LK_Cpp || Language == FormatStyle::LK_ObjC; - LangOpts.Bool = 1; LangOpts.ObjC = 1; LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. LangOpts.DeclSpecKeyword = 1; // To get __declspec. LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict. + return LangOpts; } diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 60e428123d26d..a4e2acc922c0d 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -44,7 +44,7 @@ static SmallVector<StringRef> CppNonKeywordTypes = { bool FormatToken::isTypeName(const LangOptions &LangOpts) const { if (is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts)) return true; - return (LangOpts.CXXOperatorNames || LangOpts.C17) && is(tok::identifier) && + return (LangOpts.CXXOperatorNames || LangOpts.C11) && is(tok::identifier) && std::binary_search(CppNonKeywordTypes.begin(), CppNonKeywordTypes.end(), TokenText); } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 976c4d888e1fd..3e1a5c7df7f77 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -129,7 +129,6 @@ class AnnotatingParser { : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) { - assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17)); Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); resetTokenMetadata(); } @@ -3820,7 +3819,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, }; const auto *Next = Current.Next; - const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C17; + const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11; // Find parentheses of parameter list. if (Current.is(tok::kw_operator)) { diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index c0c13941ef4f7..e4b94431e68b4 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -224,9 +224,7 @@ class TokenAnnotator { public: TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords) : Style(Style), IsCpp(Style.isCpp()), - LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) { - assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17)); - } + LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {} /// Adapts the indent levels of comment lines to the indent of the /// subsequent line. diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 9a03e9409fcbc..2b348c926294e 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -167,9 +167,7 @@ UnwrappedLineParser::UnwrappedLineParser( ? IG_Rejected : IG_Inited), IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn), - Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) { - assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17)); -} + Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {} void UnwrappedLineParser::reset() { PPBranchLevel = -1; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index f1a6999cfdfb8..b7b8a21b726b6 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3793,6 +3793,12 @@ TEST_F(TokenAnnotatorTest, AfterPPDirective) { EXPECT_TOKEN(Tokens[2], tok::minusminus, TT_AfterPPDirective); } +TEST_F(TokenAnnotatorTest, UTF8StringLiteral) { + auto Tokens = annotate("return u8\"foo\";", getLLVMStyle(FormatStyle::LK_C)); + ASSERT_EQ(Tokens.size(), 4u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::utf8_string_literal, TT_Unknown); +} + } // namespace } // namespace format } // namespace clang _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits