Skip to content

Commit 136f257

Browse files
authored
[clang-format] Lex C++ only keywords as identifiers in C (llvm#129426)
Fix llvm#128847
1 parent 0ed2945 commit 136f257

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

clang/lib/Format/Format.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,34 +3942,42 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
39423942
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
39433943
LangOptions LangOpts;
39443944

3945-
FormatStyle::LanguageStandard LexingStd = Style.Standard;
3946-
if (LexingStd == FormatStyle::LS_Auto)
3947-
LexingStd = FormatStyle::LS_Latest;
3948-
if (LexingStd == FormatStyle::LS_Latest)
3945+
auto LexingStd = Style.Standard;
3946+
if (LexingStd == FormatStyle::LS_Auto || LexingStd == FormatStyle::LS_Latest)
39493947
LexingStd = FormatStyle::LS_Cpp20;
3950-
LangOpts.CPlusPlus = 1;
3951-
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
3952-
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
3953-
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
3954-
LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
3955-
LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
3948+
3949+
const bool SinceCpp11 = LexingStd >= FormatStyle::LS_Cpp11;
3950+
const bool SinceCpp20 = LexingStd >= FormatStyle::LS_Cpp20;
3951+
3952+
switch (Style.Language) {
3953+
case FormatStyle::LK_C:
3954+
LangOpts.C17 = 1;
3955+
break;
3956+
case FormatStyle::LK_Cpp:
3957+
case FormatStyle::LK_ObjC:
3958+
LangOpts.CXXOperatorNames = 1;
3959+
LangOpts.CPlusPlus11 = SinceCpp11;
3960+
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
3961+
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
3962+
LangOpts.CPlusPlus20 = SinceCpp20;
3963+
[[fallthrough]];
3964+
default:
3965+
LangOpts.CPlusPlus = 1;
3966+
}
3967+
3968+
LangOpts.Char8 = SinceCpp20;
39563969
// Turning on digraphs in standards before C++0x is error-prone, because e.g.
39573970
// the sequence "<::" will be unconditionally treated as "[:".
39583971
// Cf. Lexer::LexTokenInternal.
3959-
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
3972+
LangOpts.Digraphs = SinceCpp11;
39603973

39613974
LangOpts.LineComment = 1;
3962-
3963-
const auto Language = Style.Language;
3964-
LangOpts.C17 = Language == FormatStyle::LK_C;
3965-
LangOpts.CXXOperatorNames =
3966-
Language == FormatStyle::LK_Cpp || Language == FormatStyle::LK_ObjC;
3967-
39683975
LangOpts.Bool = 1;
39693976
LangOpts.ObjC = 1;
39703977
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
39713978
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
39723979
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
3980+
39733981
return LangOpts;
39743982
}
39753983

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,6 +3705,15 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
37053705
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
37063706
}
37073707

3708+
TEST_F(TokenAnnotatorTest, CppOnlyKeywordInC) {
3709+
auto Tokens = annotate("int maximized = new & STATE_MAXIMIZED;",
3710+
getLLVMStyle(FormatStyle::LK_C));
3711+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3712+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown); // Not tok::kw_new
3713+
EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
3714+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown); // Not TT_StartOfName
3715+
}
3716+
37083717
TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
37093718
auto Tokens =
37103719
annotate("Foo::Foo(int x, int y) try\n"

0 commit comments

Comments
 (0)