[clang] 97dcbde - Revert "[clang-format] Handle C++ keywords in other languages better (#132941)"
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 1 18:59:48 PDT 2025
Author: Owen Pan
Date: 2025-04-01T18:59:12-07:00
New Revision: 97dcbdef6089175c45e14fcbcf5c88b10233a79a
URL: https://github.com/llvm/llvm-project/commit/97dcbdef6089175c45e14fcbcf5c88b10233a79a
DIFF: https://github.com/llvm/llvm-project/commit/97dcbdef6089175c45e14fcbcf5c88b10233a79a.diff
LOG: Revert "[clang-format] Handle C++ keywords in other languages better (#132941)"
This reverts commit ab7cee8a0ecf29fdb47c64c8d431a694d63390d2 which had
formatting errors.
Added:
Modified:
clang/lib/Format/FormatTokenLexer.cpp
clang/unittests/Format/FormatTestJS.cpp
clang/unittests/Format/FormatTestJava.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 014b10b206d90..eed54a11684b5 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1306,12 +1306,15 @@ FormatToken *FormatTokenLexer::getNextToken() {
FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
tok::kw_operator)) {
FormatTok->Tok.setKind(tok::identifier);
+ FormatTok->Tok.setIdentifierInfo(nullptr);
} else if (Style.isJavaScript() &&
FormatTok->isOneOf(tok::kw_struct, tok::kw_union,
tok::kw_operator)) {
FormatTok->Tok.setKind(tok::identifier);
+ FormatTok->Tok.setIdentifierInfo(nullptr);
} else if (Style.isTableGen() && !Keywords.isTableGenKeyword(*FormatTok)) {
FormatTok->Tok.setKind(tok::identifier);
+ FormatTok->Tok.setIdentifierInfo(nullptr);
}
} else if (FormatTok->is(tok::greatergreater)) {
FormatTok->Tok.setKind(tok::greater);
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 3dae67fbcdfcb..78c9f887a159b 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -828,18 +828,12 @@ TEST_F(FormatTestJS, AsyncFunctions) {
"} ");
// clang-format must not insert breaks between async and function, otherwise
// automatic semicolon insertion may trigger (in particular in a class body).
- auto Style = getGoogleJSStyleWithColumns(10);
verifyFormat("async function\n"
"hello(\n"
" myparamnameiswaytooloooong) {\n"
"}",
"async function hello(myparamnameiswaytooloooong) {}",
- Style);
- verifyFormat("async function\n"
- "union(\n"
- " myparamnameiswaytooloooong) {\n"
- "}",
- Style);
+ getGoogleJSStyleWithColumns(10));
verifyFormat("class C {\n"
" async hello(\n"
" myparamnameiswaytooloooong) {\n"
@@ -847,7 +841,7 @@ TEST_F(FormatTestJS, AsyncFunctions) {
"}",
"class C {\n"
" async hello(myparamnameiswaytooloooong) {} }",
- Style);
+ getGoogleJSStyleWithColumns(10));
verifyFormat("async function* f() {\n"
" yield fetch(x);\n"
"}");
@@ -1344,16 +1338,15 @@ TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
// The following statements must not wrap, as otherwise the program meaning
// would change due to automatic semicolon insertion.
// See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
- auto Style =getGoogleJSStyleWithColumns(10);
- verifyFormat("return aaaaa;", Style);
- verifyFormat("yield aaaaa;", Style);
- verifyFormat("return /* hello! */ aaaaa;", Style);
- verifyFormat("continue aaaaa;", Style);
- verifyFormat("continue /* hello! */ aaaaa;", Style);
- verifyFormat("break aaaaa;", Style);
- verifyFormat("throw aaaaa;", Style);
- verifyFormat("aaaaaaaaa++;", Style);
- verifyFormat("aaaaaaaaa--;", Style);
+ verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("yield aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
verifyFormat("return [\n"
" aaa\n"
"];",
@@ -1373,13 +1366,12 @@ TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
// Ideally the foo() bit should be indented relative to the async function().
verifyFormat("async function\n"
"foo() {}",
- Style);
- verifyFormat("await theReckoning;", Style);
- verifyFormat("some['a']['b']", Style);
- verifyFormat("union['a']['b']", Style);
+ getGoogleJSStyleWithColumns(10));
+ verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
verifyFormat("x = (a['a']\n"
" ['b']);",
- Style);
+ getGoogleJSStyleWithColumns(10));
verifyFormat("function f() {\n"
" return foo.bar(\n"
" (param): param is {\n"
@@ -2508,10 +2500,6 @@ TEST_F(FormatTestJS, NonNullAssertionOperator) {
TEST_F(FormatTestJS, CppKeywords) {
// Make sure we don't mess stuff up because of C++ keywords.
verifyFormat("return operator && (aa);");
- verifyFormat("enum operator {\n"
- " A = 1,\n"
- " B\n"
- "}");
// .. or QT ones.
verifyFormat("const slots: Slot[];");
// use the "!" assertion operator to validate that clang-format understands
diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index e01c1d6d7e684..33998bc7ff858 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -158,8 +158,6 @@ TEST_F(FormatTestJava, AnonymousClasses) {
TEST_F(FormatTestJava, EnumDeclarations) {
verifyFormat("enum SomeThing { ABC, CDE }");
- // A C++ keyword should not mess things up.
- verifyFormat("enum union { ABC, CDE }");
verifyFormat("enum SomeThing {\n"
" ABC,\n"
" CDE,\n"
More information about the cfe-commits
mailing list