https://github.com/amane-ame updated https://github.com/llvm/llvm-project/pull/119428
From 3a4c1a924faef3a7a09126694fcb943bd7083451 Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Wed, 11 Dec 2024 02:13:43 +0800 Subject: [PATCH 1/7] Fix crashes when the macro expansion is empty --- clang/lib/Format/MacroExpander.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/lib/Format/MacroExpander.cpp b/clang/lib/Format/MacroExpander.cpp index fd2a16894d643d..ed9e51dfbfef1f 100644 --- a/clang/lib/Format/MacroExpander.cpp +++ b/clang/lib/Format/MacroExpander.cpp @@ -233,6 +233,10 @@ MacroExpander::expand(FormatToken *ID, if (Result.size() > 1) { ++Result[0]->MacroCtx->StartOfExpansion; ++Result[Result.size() - 2]->MacroCtx->EndOfExpansion; + } else { + // If the macro expansion is empty, mark the start and end + Result[0]->MacroCtx->StartOfExpansion = 1; + Result[0]->MacroCtx->EndOfExpansion = 1; } return Result; } From 8bf68f87b897bab9da9464d594082be5012afdb1 Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Sun, 15 Dec 2024 15:15:17 +0800 Subject: [PATCH 2/7] [clang-format] Add a testcase for empty macro --- clang/test/Format/empty-macro.cpp | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 clang/test/Format/empty-macro.cpp diff --git a/clang/test/Format/empty-macro.cpp b/clang/test/Format/empty-macro.cpp new file mode 100644 index 00000000000000..81b255219b0c2b --- /dev/null +++ b/clang/test/Format/empty-macro.cpp @@ -0,0 +1,5 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -style="{Macros: [A(x)=x]}" \ +// RUN: | FileCheck -strict-whitespace %s + +// CHECK: A() +A() From 37a5b1b1877a7292869a93f7bfd4c09999ec9814 Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Mon, 16 Dec 2024 16:30:27 +0800 Subject: [PATCH 3/7] [clang-format] Move the testcase to unittest --- clang/test/Format/empty-macro.cpp | 5 ----- .../unittests/Format/MacroCallReconstructorTest.cpp | 13 +++++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) delete mode 100644 clang/test/Format/empty-macro.cpp diff --git a/clang/test/Format/empty-macro.cpp b/clang/test/Format/empty-macro.cpp deleted file mode 100644 index 81b255219b0c2b..00000000000000 --- a/clang/test/Format/empty-macro.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -style="{Macros: [A(x)=x]}" \ -// RUN: | FileCheck -strict-whitespace %s - -// CHECK: A() -A() diff --git a/clang/unittests/Format/MacroCallReconstructorTest.cpp b/clang/unittests/Format/MacroCallReconstructorTest.cpp index b4ee8d0e37efa8..08088fe48fdf6a 100644 --- a/clang/unittests/Format/MacroCallReconstructorTest.cpp +++ b/clang/unittests/Format/MacroCallReconstructorTest.cpp @@ -217,6 +217,19 @@ TEST_F(MacroCallReconstructorTest, Identifier) { EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X")))); } +TEST_F(MacroCallReconstructorTest, EmptyExpansion) { + auto Macros = createExpander({"A(x)=y"}); + Expansion Exp(Lex, *Macros); + TokenList Call = Exp.expand("A", {""}); + + MacroCallReconstructor Unexp(0, Exp.getUnexpanded()); + Unexp.addLine(line(Exp.getTokens())); + EXPECT_TRUE(Unexp.finished()); + Matcher U(Call, Lex); + EXPECT_THAT(std::move(Unexp).takeResult(), + matchesLine(line(U.consume("A()")))); +} + TEST_F(MacroCallReconstructorTest, NestedLineWithinCall) { auto Macros = createExpander({"C(a)=class X { a; };"}); Expansion Exp(Lex, *Macros); From e46f0e059d61fb370c5f76ede99423846f5959c8 Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Thu, 19 Dec 2024 17:11:16 +0800 Subject: [PATCH 4/7] [clang-format] Fix about Tokens in MacroCallReconstructorTest When the macro expansion is empty, the tok::eof Token should be preserved. The reason is we have marked the start point and end point on this token in MacroExpander::expand. --- clang/unittests/Format/MacroCallReconstructorTest.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/unittests/Format/MacroCallReconstructorTest.cpp b/clang/unittests/Format/MacroCallReconstructorTest.cpp index 08088fe48fdf6a..1364de0727f4e7 100644 --- a/clang/unittests/Format/MacroCallReconstructorTest.cpp +++ b/clang/unittests/Format/MacroCallReconstructorTest.cpp @@ -65,7 +65,9 @@ class Expansion { } Unexpanded[ID] = std::move(UnexpandedLine); - auto Expanded = uneof(Macros.expand(ID, Args)); + auto Expanded = Macros.expand(ID, Args); + if (Expanded.size() > 1) + Expanded = uneof(Expanded); Tokens.append(Expanded.begin(), Expanded.end()); TokenList UnexpandedTokens; @@ -218,7 +220,7 @@ TEST_F(MacroCallReconstructorTest, Identifier) { } TEST_F(MacroCallReconstructorTest, EmptyExpansion) { - auto Macros = createExpander({"A(x)=y"}); + auto Macros = createExpander({"A(x)=x"}); Expansion Exp(Lex, *Macros); TokenList Call = Exp.expand("A", {""}); From 6a390e3cf9e7fff9c55d8bc516aa8aba4d15dedf Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Sat, 21 Dec 2024 13:29:00 +0800 Subject: [PATCH 5/7] [clang-format] Fix a comment --- clang/lib/Format/MacroExpander.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Format/MacroExpander.cpp b/clang/lib/Format/MacroExpander.cpp index ed9e51dfbfef1f..ba2992889e6743 100644 --- a/clang/lib/Format/MacroExpander.cpp +++ b/clang/lib/Format/MacroExpander.cpp @@ -234,7 +234,7 @@ MacroExpander::expand(FormatToken *ID, ++Result[0]->MacroCtx->StartOfExpansion; ++Result[Result.size() - 2]->MacroCtx->EndOfExpansion; } else { - // If the macro expansion is empty, mark the start and end + // If the macro expansion is empty, mark the start and end. Result[0]->MacroCtx->StartOfExpansion = 1; Result[0]->MacroCtx->EndOfExpansion = 1; } From bd0b6fa1c2c8a17f35464f8360d3231f5f8ca14b Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Sat, 21 Dec 2024 17:26:18 +0800 Subject: [PATCH 6/7] [clang-format] Add a unittest for GH119258 --- .../unittests/Format/MacroCallReconstructorTest.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/clang/unittests/Format/MacroCallReconstructorTest.cpp b/clang/unittests/Format/MacroCallReconstructorTest.cpp index 1364de0727f4e7..44aec38061be25 100644 --- a/clang/unittests/Format/MacroCallReconstructorTest.cpp +++ b/clang/unittests/Format/MacroCallReconstructorTest.cpp @@ -219,6 +219,18 @@ TEST_F(MacroCallReconstructorTest, Identifier) { EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X")))); } +TEST_F(MacroCallReconstructorTest, IdentifierObject) { + auto Macros = createExpander({"X"}); + Expansion Exp(Lex, *Macros); + TokenList Call = Exp.expand("X"); + + MacroCallReconstructor Unexp(0, Exp.getUnexpanded()); + Unexp.addLine(line(Exp.getTokens())); + EXPECT_TRUE(Unexp.finished()); + Matcher U(Call, Lex); + EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X")))); +} + TEST_F(MacroCallReconstructorTest, EmptyExpansion) { auto Macros = createExpander({"A(x)=x"}); Expansion Exp(Lex, *Macros); From 33d5b5977f3ea74146ad0c73fa7213ce4d53c3b5 Mon Sep 17 00:00:00 2001 From: amane-ame <i...@amane-a.me> Date: Sun, 22 Dec 2024 13:31:24 +0800 Subject: [PATCH 7/7] [clang-format] Change unittest name --- clang/unittests/Format/MacroCallReconstructorTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/unittests/Format/MacroCallReconstructorTest.cpp b/clang/unittests/Format/MacroCallReconstructorTest.cpp index 44aec38061be25..b58241fd8c4e80 100644 --- a/clang/unittests/Format/MacroCallReconstructorTest.cpp +++ b/clang/unittests/Format/MacroCallReconstructorTest.cpp @@ -219,7 +219,7 @@ TEST_F(MacroCallReconstructorTest, Identifier) { EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X")))); } -TEST_F(MacroCallReconstructorTest, IdentifierObject) { +TEST_F(MacroCallReconstructorTest, EmptyDefinition) { auto Macros = createExpander({"X"}); Expansion Exp(Lex, *Macros); TokenList Call = Exp.expand("X"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits