https://github.com/tomekpaszek updated https://github.com/llvm/llvm-project/pull/70338
>From 8404d703d58658593e6112b478533edb124cd0e1 Mon Sep 17 00:00:00 2001 From: Tomek Paszek <to...@unity3d.com> Date: Thu, 26 Oct 2023 16:19:36 +0200 Subject: [PATCH 1/2] Added an option to ignore macro definitions. --- clang/include/clang/Format/Format.h | 5 ++++- clang/lib/Format/Format.cpp | 2 ++ clang/lib/Format/UnwrappedLineFormatter.cpp | 4 +++- clang/unittests/Format/ConfigParseTest.cpp | 1 + clang/unittests/Format/FormatTest.cpp | 16 ++++++++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3e9d1915badd87f..632d45c6275472e 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4648,6 +4648,8 @@ struct FormatStyle { /// \version 11 std::vector<std::string> WhitespaceSensitiveMacros; + bool IgnorePPDefinitions; + bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && @@ -4810,7 +4812,8 @@ struct FormatStyle { UseTab == R.UseTab && VerilogBreakBetweenInstancePorts == R.VerilogBreakBetweenInstancePorts && - WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros; + WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros && + IgnorePPDefinitions == R.IgnorePPDefinitions; } std::optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index edb33f4af4defef..820408ae1a272e3 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1127,6 +1127,7 @@ template <> struct MappingTraits<FormatStyle> { Style.VerilogBreakBetweenInstancePorts); IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); + IO.mapOptional("IgnorePPDefinitions", Style.IgnorePPDefinitions); // If AlwaysBreakAfterDefinitionReturnType was specified but // AlwaysBreakAfterReturnType was not, initialize the latter from the @@ -1591,6 +1592,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.WhitespaceSensitiveMacros.push_back("NS_SWIFT_NAME"); LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE"); LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE"); + LLVMStyle.IgnorePPDefinitions = false; LLVMStyle.PenaltyBreakAssignment = prec::Assignment; LLVMStyle.PenaltyBreakComment = 300; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 280485d9a90d1bf..21adb4b4b225b45 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1354,7 +1354,9 @@ unsigned UnwrappedLineFormatter::format( bool FixIndentation = (FixBadIndentation || ContinueFormatting) && Indent != TheLine.First->OriginalColumn; - bool ShouldFormat = TheLine.Affected || FixIndentation; + bool ShouldFormat = (TheLine.Affected || FixIndentation); + if (Style.IgnorePPDefinitions && TheLine.Type == LT_PreprocessorDirective) + ShouldFormat = false; // We cannot format this line; if the reason is that the line had a // parsing error, remember that. if (ShouldFormat && TheLine.Type == LT_Invalid && Status) { diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index f90ed178d99c286..3e1be1abdda0670 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -198,6 +198,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); CHECK_PARSE_BOOL(VerilogBreakBetweenInstancePorts); + CHECK_PARSE_BOOL(IgnorePPDefinitions); CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements, Enabled); CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index b2d84f2ee389551..659132dcb9c3970 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24153,6 +24153,22 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) { verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style); } +TEST_F(FormatTest, IgnorePPDefinitions) { + FormatStyle Style = getLLVMStyle(); + Style.IgnorePPDefinitions = true; + + verifyNoChange("#define A", Style); + verifyNoChange("#define A b", Style); + verifyNoChange("#define A ( args )", Style); + verifyNoChange("#define A ( args ) = func ( args )", Style); + verifyNoChange("#define TEXT Text . With periods.", Style); + verifyNoChange("#define TEXT \\\nLine number one . \\\nNumber two .", + Style); + + // TODO + // verifyNoChange("/* comment */ #define A ( args )", Style); +} + TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { // These tests are not in NamespaceEndCommentsFixerTest because that doesn't // test its interaction with line wrapping >From 228301b783fe365bb01cc8b7ba122ed9845c9a37 Mon Sep 17 00:00:00 2001 From: Tomek Paszek <to...@unity3d.com> Date: Fri, 27 Oct 2023 13:57:50 +0200 Subject: [PATCH 2/2] Added edge cases to the test and code that fixes handling them --- clang/include/clang/Format/Format.h | 4 +++- clang/lib/Format/UnwrappedLineParser.cpp | 8 ++++++++ clang/unittests/Format/FormatTest.cpp | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 632d45c6275472e..57880fc28c238c7 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4647,7 +4647,9 @@ struct FormatStyle { /// For example: BOOST_PP_STRINGIZE /// \version 11 std::vector<std::string> WhitespaceSensitiveMacros; - + + /// Ignore formatting in preprocessor definitions. + /// \version 18 bool IgnorePPDefinitions; bool operator==(const FormatStyle &R) const { diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 488d8dc07b1eae3..b9a41797a8b2dc3 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1133,6 +1133,14 @@ void UnwrappedLineParser::parsePPDefine() { parsePPUnknown(); return; } + + if(Style.IgnorePPDefinitions){ + do { + nextToken(); + } while (!eof()); + addUnwrappedLine(); + return; + } if (IncludeGuard == IG_IfNdefed && IncludeGuardToken->TokenText == FormatTok->TokenText) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 659132dcb9c3970..a1f8961c3e5edb6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24161,9 +24161,10 @@ TEST_F(FormatTest, IgnorePPDefinitions) { verifyNoChange("#define A b", Style); verifyNoChange("#define A ( args )", Style); verifyNoChange("#define A ( args ) = func ( args )", Style); - verifyNoChange("#define TEXT Text . With periods.", Style); verifyNoChange("#define TEXT \\\nLine number one . \\\nNumber two .", Style); + verifyNoChange("#define A x:", Style); + verifyNoChange("#define A a. b", Style); // TODO // verifyNoChange("/* comment */ #define A ( args )", Style); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits