Revert "[clang-format] Handle C++ keywords in other languages better (#132941)"

This reverts commit ab7cee8a0e which had
formatting errors.
This commit is contained in:
Owen Pan
2025-04-01 18:56:19 -07:00
parent 55ac652745
commit 97dcbdef60
3 changed files with 18 additions and 29 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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"