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

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Apr 5, 2025

Fix #134453

@llvmbot
Copy link
Member

llvmbot commented Apr 5, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #134453


Full diff: https://github.com/llvm/llvm-project/pull/134472.diff

6 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+1-1)
  • (modified) clang/lib/Format/FormatToken.cpp (+1-1)
  • (modified) clang/lib/Format/TokenAnnotator.cpp (+1-2)
  • (modified) clang/lib/Format/TokenAnnotator.h (+1-3)
  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+1-3)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+6)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b74a8631efe0f..226d39f635676 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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:
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 7752139142430..1d49d787f9cc9 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 dfb59e8d6f420..bd54470dcba37 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();
   }
@@ -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)) {
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 213b706807b2a..9641da1577ded 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 7e0af1c7b4c36..38dc10a08f640 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why does this fix it?

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Apr 5, 2025
@owenca
Copy link
Contributor Author

owenca commented Apr 5, 2025

But why does this fix it?

See here. Somehow I thought that setting C17 would also set C11. 😞

@owenca owenca added the invalid-code-generation Tool (e.g. clang-format) produced invalid code that no longer compiles label Apr 5, 2025
@owenca owenca merged commit d71ee7d into llvm:main Apr 5, 2025
13 of 15 checks passed
@owenca owenca deleted the 134453 branch April 5, 2025 20:35
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Apr 5, 2025
@owenca
Copy link
Contributor Author

owenca commented Apr 5, 2025

/cherry-pick d71ee7d

@llvmbot
Copy link
Member

llvmbot commented Apr 5, 2025

Failed to cherry-pick: d71ee7d

https://github.com/llvm/llvm-project/actions/runs/14285590977

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

owenca added a commit to owenca/llvm-project that referenced this pull request Apr 5, 2025
@owenca owenca removed this from the LLVM 20.X Release milestone Apr 5, 2025
@owenca
Copy link
Contributor Author

owenca commented Apr 6, 2025

See #134514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format invalid-code-generation Tool (e.g. clang-format) produced invalid code that no longer compiles regression
Projects
Development

Successfully merging this pull request may close these issues.

[clang-format]: Inserts a space after u8 string literal modifiers in C: u8"My utf-8 string" -> u8 "My utf-8 string" and breaks compilation
3 participants