diff options
author | Owen Pan <owenpiano@gmail.com> | 2025-03-03 17:40:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-03 17:40:28 -0800 |
commit | 136f2574ddfe81e73376ada0ea299b67170caf2c (patch) | |
tree | 4c2c6a075f069ee4ba5faba249c914ed729a0533 /clang/lib/Format/Format.cpp | |
parent | 0ed2945a596991b75e4ca090fe04240abba6012b (diff) | |
download | llvm-136f2574ddfe81e73376ada0ea299b67170caf2c.zip llvm-136f2574ddfe81e73376ada0ea299b67170caf2c.tar.gz llvm-136f2574ddfe81e73376ada0ea299b67170caf2c.tar.bz2 |
[clang-format] Lex C++ only keywords as identifiers in C (#129426)
Fix #128847
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c699a96..b5f1241 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3942,34 +3942,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.C17 = 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; } |