Skip to content

[clang-format] Handle C++ keywords in other languages better #132941

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 2 commits into from
Apr 10, 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
3 changes: 0 additions & 3 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,15 +1306,12 @@ 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);
Expand Down
43 changes: 27 additions & 16 deletions clang/unittests/Format/FormatTestJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,20 +828,25 @@ 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) {}",
getGoogleJSStyleWithColumns(10));
"async function hello(myparamnameiswaytooloooong) {}", Style);
verifyFormat("async function\n"
"union(\n"
" myparamnameiswaytooloooong) {\n"
"}",
Style);
verifyFormat("class C {\n"
" async hello(\n"
" myparamnameiswaytooloooong) {\n"
" }\n"
"}",
"class C {\n"
" async hello(myparamnameiswaytooloooong) {} }",
getGoogleJSStyleWithColumns(10));
Style);
verifyFormat("async function* f() {\n"
" yield fetch(x);\n"
"}");
Expand Down Expand Up @@ -1338,15 +1343,16 @@ 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.
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));
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 [\n"
" aaa\n"
"];",
Expand All @@ -1366,12 +1372,13 @@ TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
// Ideally the foo() bit should be indented relative to the async function().
verifyFormat("async function\n"
"foo() {}",
getGoogleJSStyleWithColumns(10));
verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
Style);
verifyFormat("await theReckoning;", Style);
verifyFormat("some['a']['b']", Style);
verifyFormat("union['a']['b']", Style);
verifyFormat("x = (a['a']\n"
" ['b']);",
getGoogleJSStyleWithColumns(10));
Style);
verifyFormat("function f() {\n"
" return foo.bar(\n"
" (param): param is {\n"
Expand Down Expand Up @@ -2500,6 +2507,10 @@ 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
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTestJava.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ 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"
Expand Down
Loading