Skip to content

Commit edb54a7

Browse files
owencatstellar
authored andcommitted
Release/20.x: [clang-format] Set C11 instead of C17 for LK_C
Backport d71ee7d
1 parent 4181e82 commit edb54a7

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

Diff for: clang/lib/Format/Format.cpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -3946,34 +3946,42 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
39463946
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
39473947
LangOptions LangOpts;
39483948

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

39653978
LangOpts.LineComment = 1;
3966-
3967-
const auto Language = Style.Language;
3968-
LangOpts.C17 = Language == FormatStyle::LK_C;
3969-
LangOpts.CXXOperatorNames =
3970-
Language == FormatStyle::LK_Cpp || Language == FormatStyle::LK_ObjC;
3971-
39723979
LangOpts.Bool = 1;
39733980
LangOpts.ObjC = 1;
39743981
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
39753982
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
39763983
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
3984+
39773985
return LangOpts;
39783986
}
39793987

Diff for: clang/lib/Format/FormatToken.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
4444
bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
4545
if (is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts))
4646
return true;
47-
return (LangOpts.CXXOperatorNames || LangOpts.C17) && is(tok::identifier) &&
47+
return (LangOpts.CXXOperatorNames || LangOpts.C11) && is(tok::identifier) &&
4848
std::binary_search(CppNonKeywordTypes.begin(),
4949
CppNonKeywordTypes.end(), TokenText);
5050
}

Diff for: clang/lib/Format/TokenAnnotator.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class AnnotatingParser {
129129
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
130130
IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
131131
Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132-
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
133132
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
134133
resetTokenMetadata();
135134
}
@@ -3820,7 +3819,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
38203819
};
38213820

38223821
const auto *Next = Current.Next;
3823-
const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C17;
3822+
const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
38243823

38253824
// Find parentheses of parameter list.
38263825
if (Current.is(tok::kw_operator)) {

Diff for: clang/lib/Format/TokenAnnotator.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ class TokenAnnotator {
224224
public:
225225
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
226226
: Style(Style), IsCpp(Style.isCpp()),
227-
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {
228-
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
229-
}
227+
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {}
230228

231229
/// Adapts the indent levels of comment lines to the indent of the
232230
/// subsequent line.

Diff for: clang/lib/Format/UnwrappedLineParser.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ UnwrappedLineParser::UnwrappedLineParser(
167167
? IG_Rejected
168168
: IG_Inited),
169169
IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
170-
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {
171-
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
172-
}
170+
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
173171

174172
void UnwrappedLineParser::reset() {
175173
PPBranchLevel = -1;

Diff for: clang/unittests/Format/TokenAnnotatorTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3793,6 +3793,12 @@ TEST_F(TokenAnnotatorTest, AfterPPDirective) {
37933793
EXPECT_TOKEN(Tokens[2], tok::minusminus, TT_AfterPPDirective);
37943794
}
37953795

3796+
TEST_F(TokenAnnotatorTest, UTF8StringLiteral) {
3797+
auto Tokens = annotate("return u8\"foo\";", getLLVMStyle(FormatStyle::LK_C));
3798+
ASSERT_EQ(Tokens.size(), 4u) << Tokens;
3799+
EXPECT_TOKEN(Tokens[1], tok::utf8_string_literal, TT_Unknown);
3800+
}
3801+
37963802
} // namespace
37973803
} // namespace format
37983804
} // namespace clang

0 commit comments

Comments
 (0)