[clang] 136f257 - [clang-format] Lex C++ only keywords as identifiers in C (#129426)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 3 17:40:31 PST 2025
Author: Owen Pan
Date: 2025-03-03T17:40:28-08:00
New Revision: 136f2574ddfe81e73376ada0ea299b67170caf2c
URL: https://github.com/llvm/llvm-project/commit/136f2574ddfe81e73376ada0ea299b67170caf2c
DIFF: https://github.com/llvm/llvm-project/commit/136f2574ddfe81e73376ada0ea299b67170caf2c.diff
LOG: [clang-format] Lex C++ only keywords as identifiers in C (#129426)
Fix #128847
Added:
Modified:
clang/lib/Format/Format.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c699a96d3f45e..b5f1241321891 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;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1e1774ba5b3b5..b44a369f77d92 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3705,6 +3705,15 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
}
+TEST_F(TokenAnnotatorTest, CppOnlyKeywordInC) {
+ auto Tokens = annotate("int maximized = new & STATE_MAXIMIZED;",
+ getLLVMStyle(FormatStyle::LK_C));
+ ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown); // Not tok::kw_new
+ EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown); // Not TT_StartOfName
+}
+
TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
auto Tokens =
annotate("Foo::Foo(int x, int y) try\n"
More information about the cfe-commits
mailing list