[clang-format] Option to ignore macro definitions by tomekpaszek · Pull Request #70338 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-clang

Author: Tomek (tomekpaszek)

Changes

Sometimes macro definitions contain a layout we don't want to change in any context. This PR adds a new style option IgnorePPDefinitions that prevents clang-format from touching PP directives.

This PR addresses issue #67991


Full diff: https://github.com/llvm/llvm-project/pull/70338.diff

7 Files Affected:

diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 21342e1b89ea866..80565620d8f24bf 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3160,6 +3160,11 @@ the configuration (without a prefix: Auto). For example: KJ_IF_MAYBE <https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>_

+.. _IgnorePPDefinitions: + +IgnorePPDefinitions (Boolean) :versionbadge:clang-format 18 :ref:¶ <IgnorePPDefinitions>

diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3e9d1915badd87f..3af7241441c8b13 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2442,6 +2442,10 @@ struct FormatStyle { /// https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes`_ /// \version 13 std::vectorstd::string IfMacros;

@@ -4719,6 +4723,7 @@ struct FormatStyle { R.IncludeStyle.IncludeIsMainRegex && IncludeStyle.IncludeIsMainSourceRegex == R.IncludeStyle.IncludeIsMainSourceRegex &&

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index edb33f4af4defef..6e5ec754dfdcdd9 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1000,6 +1000,7 @@ template <> struct MappingTraits { IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments); IO.mapOptional("ForEachMacros", Style.ForEachMacros); IO.mapOptional("IfMacros", Style.IfMacros);

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 280485d9a90d1bf..bbf6383ff7673f6 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1355,6 +1355,8 @@ unsigned UnwrappedLineFormatter::format( bool FixIndentation = (FixBadIndentation || ContinueFormatting) && Indent != TheLine.First->OriginalColumn; bool ShouldFormat = TheLine.Affected || FixIndentation;

diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index f90ed178d99c286..7ce9ae26b36fbb9 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -166,6 +166,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(DerivePointerAlignment); CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); CHECK_PARSE_BOOL(DisableFormat);

+TEST_F(FormatTest, IgnorePPDefinitions) {

+} + TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { // These tests are not in NamespaceEndCommentsFixerTest because that doesn't // test its interaction with line wrapping