Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3941,34 +3941,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;
}

Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down