Skip to content

[clang-format] Set C11 instead of C17 for LK_C #134472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2025
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
2 changes: 1 addition & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4010,7 +4010,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {

switch (Style.Language) {
case FormatStyle::LK_C:
LangOpts.C17 = 1;
LangOpts.C11 = 1;
break;
case FormatStyle::LK_Cpp:
case FormatStyle::LK_ObjC:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
if (is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts))
return true;
return (LangOpts.CXXOperatorNames || LangOpts.C17) && is(tok::identifier) &&
return (LangOpts.CXXOperatorNames || LangOpts.C11) && is(tok::identifier) &&
std::binary_search(CppNonKeywordTypes.begin(),
CppNonKeywordTypes.end(), TokenText);
}
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class AnnotatingParser {
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
resetTokenMetadata();
}
Expand Down Expand Up @@ -3847,7 +3846,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
};

const auto *Next = Current.Next;
const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C17;
const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;

// Find parentheses of parameter list.
if (Current.is(tok::kw_operator)) {
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Format/TokenAnnotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ class TokenAnnotator {
public:
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
: Style(Style), IsCpp(Style.isCpp()),
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
}
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {}

/// Adapts the indent levels of comment lines to the indent of the
/// subsequent line.
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ UnwrappedLineParser::UnwrappedLineParser(
? IG_Rejected
: IG_Inited),
IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
}
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}

void UnwrappedLineParser::reset() {
PPBranchLevel = -1;
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,12 @@ TEST_F(TokenAnnotatorTest, UserDefinedConversionFunction) {
EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_FunctionDeclarationLParen);
}

TEST_F(TokenAnnotatorTest, UTF8StringLiteral) {
auto Tokens = annotate("return u8\"foo\";", getLLVMStyle(FormatStyle::LK_C));
ASSERT_EQ(Tokens.size(), 4u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::utf8_string_literal, TT_Unknown);
}

} // namespace
} // namespace format
} // namespace clang
Loading