https://github.com/rymiel created https://github.com/llvm/llvm-project/pull/65409:
When encountering braces, such as those of a designated initializer, clang-format scans ahead to see what is contained within the braces. If it found a statement, like an if-statement of for-loop, it would deem the braces as not an initializer, but as a block instead. However, this heuristic incorrectly included a preprocessor `#if` line as an if-statement. This manifested in strange results and discrepancies between `#ifdef` and `#if defined`. With this patch, `if` is now ignored if it is preceeded by `#`. Fixes most of https://github.com/llvm/llvm-project/issues/56685 >From e15868400981600b7dc68229660391a7874e67af Mon Sep 17 00:00:00 2001 From: Emilia Kond <emilia@rymiel.space> Date: Tue, 5 Sep 2023 22:44:41 +0300 Subject: [PATCH] [clang-format] Correctly annotate designated initializer with PP if When encountering braces, such as those of a designated initializer, clang-format scans ahead to see what is contained within the braces. If it found a statement, like an if-statement of for-loop, it would deem the braces as not an initializer, but as a block instead. However, this heuristic incorrectly included a preprocessor `#if` line as an if-statement. This manifested in strange results and discrepancies between `#ifdef` and `#if defined`. With this patch, `if` is now ignored if it is preceeded by `#`. Fixes most of https://github.com/llvm/llvm-project/issues/56685 --- clang/lib/Format/UnwrappedLineParser.cpp | 2 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 5a82b200055a87..eaebc69879b973 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -626,6 +626,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { case tok::kw_switch: case tok::kw_try: case tok::kw___try: + if (PrevTok->is(tok::hash)) + break; if (!LBraceStack.empty() && LBraceStack.back().Tok->is(BK_Unknown)) LBraceStack.back().Tok->setBlockKind(BK_Block); break; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 467ade965d7c86..ac2b60530f2fb1 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1998,6 +1998,41 @@ TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) { EXPECT_BRACE_KIND(Tokens[10], BK_Block); } +TEST_F(TokenAnnotatorTest, UnderstandDesignatedInitializers) { + auto Tokens = annotate("SomeStruct { .a = 1 };"); + ASSERT_EQ(Tokens.size(), 9u) << Tokens; + EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit); + EXPECT_TOKEN(Tokens[2], tok::period, TT_DesignatedInitializerPeriod); + + Tokens = annotate("SomeStruct { .a = 1, .b = 2 };"); + ASSERT_EQ(Tokens.size(), 14u) << Tokens; + EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit); + EXPECT_TOKEN(Tokens[2], tok::period, TT_DesignatedInitializerPeriod); + EXPECT_TOKEN(Tokens[7], tok::period, TT_DesignatedInitializerPeriod); + + Tokens = annotate("SomeStruct {\n" + "#ifdef FOO\n" + " .a = 1,\n" + "#endif\n" + " .b = 2\n" + "};"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit); + EXPECT_TOKEN(Tokens[5], tok::period, TT_DesignatedInitializerPeriod); + EXPECT_TOKEN(Tokens[12], tok::period, TT_DesignatedInitializerPeriod); + + Tokens = annotate("SomeStruct {\n" + "#if defined FOO\n" + " .a = 1,\n" + "#endif\n" + " .b = 2\n" + "};"); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit); + EXPECT_TOKEN(Tokens[6], tok::period, TT_DesignatedInitializerPeriod); + EXPECT_TOKEN(Tokens[13], tok::period, TT_DesignatedInitializerPeriod); +} + } // namespace } // namespace format } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits