[clang] [clang-format] Detect nesting in template strings (PR #119989)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 14 21:29:40 PST 2024
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/119989
>From fa8d1b12eee0164f2b4c8223281d0e59dfa693e1 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Sat, 14 Dec 2024 15:25:44 -0700
Subject: [PATCH 1/2] [clang-format] detect nesting in template strings
The helper to check if a token is in a template string scans
too far backward. It should stop if a different scope is found.
Fixes #107571
---
clang/lib/Format/ContinuationIndenter.cpp | 4 +++-
clang/unittests/Format/FormatTestJS.cpp | 7 +++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index aed86c1fb99551..9ffdc044e6784d 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -826,8 +826,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
if (Prev->is(TT_TemplateString) && Prev->opensScope())
return true;
- if (Prev->is(TT_TemplateString) && Prev->closesScope())
+ if (Prev->opensScope() ||
+ (Prev->is(TT_TemplateString) && Prev->closesScope())) {
break;
+ }
}
return false;
};
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 4b15e7b7da3393..678fd1f3fc8d0e 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2157,6 +2157,13 @@ TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
" aaaa: aaaaa,\n"
" bbbb: bbbbb,\n"
" })}`;");
+
+ verifyFormat("`${\n"
+ " (\n"
+ " FOOFOOFOOFOO____FOO_FOO_FO_FOO_FOOO -\n"
+ " (barbarbarbar____bar_bar_bar_bar_bar_bar +\n"
+ " bar_bar_bar_barbarbar___bar_bar_bar + 1),\n"
+ " )}`;\n");
}
TEST_F(FormatTestJS, TemplateStringASI) {
>From 77904096ff2ed1a17113e5e4b9c5f040a52d9fb0 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Sat, 14 Dec 2024 22:29:32 -0700
Subject: [PATCH 2/2] Update clang/unittests/Format/FormatTestJS.cpp
Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
clang/unittests/Format/FormatTestJS.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 678fd1f3fc8d0e..4f41e4779f6ed2 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2163,7 +2163,7 @@ TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
" FOOFOOFOOFOO____FOO_FOO_FO_FOO_FOOO -\n"
" (barbarbarbar____bar_bar_bar_bar_bar_bar +\n"
" bar_bar_bar_barbarbar___bar_bar_bar + 1),\n"
- " )}`;\n");
+ " )}`;");
}
TEST_F(FormatTestJS, TemplateStringASI) {
More information about the cfe-commits
mailing list