-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Release/20.x: [clang-format] Set C11 instead of C17 for LK_C #134514
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
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesBackport d71ee7d Full diff: https://github.com/llvm/llvm-project/pull/134514.diff 6 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 768e655f65ce7..b97d8928178b5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3946,34 +3946,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.C11 = 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/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 60e428123d26d..a4e2acc922c0d 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -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);
}
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 976c4d888e1fd..3e1a5c7df7f77 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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();
}
@@ -3820,7 +3819,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)) {
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index c0c13941ef4f7..e4b94431e68b4 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -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.
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 9a03e9409fcbc..2b348c926294e 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f1a6999cfdfb8..b7b8a21b726b6 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3793,6 +3793,12 @@ TEST_F(TokenAnnotatorTest, AfterPPDirective) {
EXPECT_TOKEN(Tokens[2], tok::minusminus, TT_AfterPPDirective);
}
+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
|
Copied the entire |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@owenca (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR. |
Backport d71ee7d