Skip to content

Commit 38ce4a5

Browse files
authored
Merge branch 'llvm:main' into gh-101657
2 parents a66defe + 82fecab commit 38ce4a5

28 files changed

+497
-356
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6843,6 +6843,45 @@ the configuration (without a prefix: ``Auto``).
68436843
68446844
For example: BOOST_PP_STRINGIZE
68456845

6846+
.. _WrapNamespaceBodyWithEmptyLines:
6847+
6848+
**WrapNamespaceBodyWithEmptyLines** (``WrapNamespaceBodyWithEmptyLinesStyle``) :versionbadge:`clang-format 20` :ref:`<WrapNamespaceBodyWithEmptyLines>`
6849+
Wrap namespace body with empty lines.
6850+
6851+
Possible values:
6852+
6853+
* ``WNBWELS_Never`` (in configuration: ``Never``)
6854+
Remove all empty lines at the beginning and the end of namespace body.
6855+
6856+
.. code-block:: c++
6857+
6858+
namespace N1 {
6859+
namespace N2
6860+
function();
6861+
}
6862+
}
6863+
6864+
* ``WNBWELS_Always`` (in configuration: ``Always``)
6865+
Always have at least one empty line at the beginning and the end of
6866+
namespace body except that the number of empty lines between consecutive
6867+
nested namespace definitions is not increased.
6868+
6869+
.. code-block:: c++
6870+
6871+
namespace N1 {
6872+
namespace N2 {
6873+
6874+
function();
6875+
6876+
}
6877+
}
6878+
6879+
* ``WNBWELS_Leave`` (in configuration: ``Leave``)
6880+
Keep existing newlines at the beginning and the end of namespace body.
6881+
``MaxEmptyLinesToKeep`` still applies.
6882+
6883+
6884+
68466885
.. END_FORMAT_STYLE_OPTIONS
68476886
68486887
Adding additional style options

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ clang-format
11271127
- Adds ``AllowShortNamespacesOnASingleLine`` option.
11281128
- Adds ``VariableTemplates`` option.
11291129
- Adds support for bash globstar in ``.clang-format-ignore``.
1130+
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.
11301131

11311132
libclang
11321133
--------

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class CodeGenOptions : public CodeGenOptionsBase {
186186
std::string ProfileExcludeFiles;
187187

188188
/// The version string to put into coverage files.
189-
char CoverageVersion[4];
189+
char CoverageVersion[4] = {'0', '0', '0', '0'};
190190

191191
/// Enable additional debugging information.
192192
std::string DebugPass;

clang/include/clang/Format/Format.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5143,6 +5143,39 @@ struct FormatStyle {
51435143
/// \version 11
51445144
std::vector<std::string> WhitespaceSensitiveMacros;
51455145

5146+
/// Different styles for wrapping namespace body with empty lines.
5147+
enum WrapNamespaceBodyWithEmptyLinesStyle : int8_t {
5148+
/// Remove all empty lines at the beginning and the end of namespace body.
5149+
/// \code
5150+
/// namespace N1 {
5151+
/// namespace N2
5152+
/// function();
5153+
/// }
5154+
/// }
5155+
/// \endcode
5156+
WNBWELS_Never,
5157+
/// Always have at least one empty line at the beginning and the end of
5158+
/// namespace body except that the number of empty lines between consecutive
5159+
/// nested namespace definitions is not increased.
5160+
/// \code
5161+
/// namespace N1 {
5162+
/// namespace N2 {
5163+
///
5164+
/// function();
5165+
///
5166+
/// }
5167+
/// }
5168+
/// \endcode
5169+
WNBWELS_Always,
5170+
/// Keep existing newlines at the beginning and the end of namespace body.
5171+
/// ``MaxEmptyLinesToKeep`` still applies.
5172+
WNBWELS_Leave
5173+
};
5174+
5175+
/// Wrap namespace body with empty lines.
5176+
/// \version 20
5177+
WrapNamespaceBodyWithEmptyLinesStyle WrapNamespaceBodyWithEmptyLines;
5178+
51465179
bool operator==(const FormatStyle &R) const {
51475180
return AccessModifierOffset == R.AccessModifierOffset &&
51485181
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
@@ -5326,7 +5359,8 @@ struct FormatStyle {
53265359
UseTab == R.UseTab && VariableTemplates == R.VariableTemplates &&
53275360
VerilogBreakBetweenInstancePorts ==
53285361
R.VerilogBreakBetweenInstancePorts &&
5329-
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros;
5362+
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros &&
5363+
WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines;
53305364
}
53315365

53325366
std::optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const;

clang/lib/Basic/CodeGenOptions.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ CodeGenOptions::CodeGenOptions() {
1717
#include "clang/Basic/CodeGenOptions.def"
1818

1919
RelocationModel = llvm::Reloc::PIC_;
20-
memcpy(CoverageVersion, "408*", 4);
2120
}
2221

2322
void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
@@ -54,7 +53,6 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
5453
}
5554

5655
RelocationModel = llvm::Reloc::PIC_;
57-
memcpy(CoverageVersion, "408*", 4);
5856
}
5957

6058
} // end namespace clang

clang/lib/Format/Format.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,18 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
839839
}
840840
};
841841

842+
template <>
843+
struct ScalarEnumerationTraits<
844+
FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle> {
845+
static void
846+
enumeration(IO &IO,
847+
FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle &Value) {
848+
IO.enumCase(Value, "Never", FormatStyle::WNBWELS_Never);
849+
IO.enumCase(Value, "Always", FormatStyle::WNBWELS_Always);
850+
IO.enumCase(Value, "Leave", FormatStyle::WNBWELS_Leave);
851+
}
852+
};
853+
842854
template <> struct MappingTraits<FormatStyle> {
843855
static void mapping(IO &IO, FormatStyle &Style) {
844856
// When reading, read the language first, we need it for getPredefinedStyle.
@@ -1171,6 +1183,8 @@ template <> struct MappingTraits<FormatStyle> {
11711183
Style.VerilogBreakBetweenInstancePorts);
11721184
IO.mapOptional("WhitespaceSensitiveMacros",
11731185
Style.WhitespaceSensitiveMacros);
1186+
IO.mapOptional("WrapNamespaceBodyWithEmptyLines",
1187+
Style.WrapNamespaceBodyWithEmptyLines);
11741188

11751189
// If AlwaysBreakAfterDefinitionReturnType was specified but
11761190
// BreakAfterReturnType was not, initialize the latter from the former for
@@ -1639,6 +1653,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16391653
LLVMStyle.WhitespaceSensitiveMacros.push_back("NS_SWIFT_NAME");
16401654
LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE");
16411655
LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE");
1656+
LLVMStyle.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave;
16421657

16431658
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
16441659
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,23 @@ static auto computeNewlines(const AnnotatedLine &Line,
15841584
Newlines = 1;
15851585
}
15861586

1587+
if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave) {
1588+
// Modify empty lines after TT_NamespaceLBrace.
1589+
if (PreviousLine && PreviousLine->endsWith(TT_NamespaceLBrace)) {
1590+
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1591+
Newlines = 1;
1592+
else if (!Line.startsWithNamespace())
1593+
Newlines = std::max(Newlines, 2u);
1594+
}
1595+
// Modify empty lines before TT_NamespaceRBrace.
1596+
if (Line.startsWith(TT_NamespaceRBrace)) {
1597+
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1598+
Newlines = 1;
1599+
else if (!PreviousLine->startsWith(TT_NamespaceRBrace))
1600+
Newlines = std::max(Newlines, 2u);
1601+
}
1602+
}
1603+
15871604
// Insert or remove empty line before access specifiers.
15881605
if (PreviousLine && RootToken.isAccessSpecifier()) {
15891606
switch (Style.EmptyLineBeforeAccessModifier) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ void CompilerInvocationBase::GenerateCodeGenArgs(const CodeGenOptions &Opts,
16911691
}
16921692
}
16931693

1694-
if (memcmp(Opts.CoverageVersion, "408*", 4) != 0)
1694+
if (memcmp(Opts.CoverageVersion, "0000", 4))
16951695
GenerateArg(Consumer, OPT_coverage_version_EQ,
16961696
StringRef(Opts.CoverageVersion, 4));
16971697

@@ -2007,7 +2007,6 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
20072007
} else if (Args.hasArg(OPT_fmemory_profile))
20082008
Opts.MemoryProfileOutput = MemProfileBasename;
20092009

2010-
memcpy(Opts.CoverageVersion, "408*", 4);
20112010
if (Opts.CoverageNotesFile.size() || Opts.CoverageDataFile.size()) {
20122011
if (Args.hasArg(OPT_coverage_version_EQ)) {
20132012
StringRef CoverageVersion = Args.getLastArgValue(OPT_coverage_version_EQ);

clang/test/CodeGen/code-coverage.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
/// 4.7 enables cfg_checksum.
44
/// 4.8 (default, compatible with gcov 7) emits the exit block the second.
55
// RUN: rm -rf %t && mkdir %t && cd %t
6-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='304*' %s -o - | \
7-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-CTOR-INIT,304 %s
8-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='407*' %s -o - | \
9-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-CTOR-INIT,407 %s
6+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='B21*' %s -o - | \
7+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-CTOR-INIT,1210 %s
108
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-red-zone -coverage-data-file=/dev/null %s -o - | \
11-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-CTOR-INIT,408 %s
12-
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='304*' %s -o - | \
13-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-RT-INIT,304 %s
14-
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='407*' %s -o - | \
15-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-RT-INIT,407 %s
9+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-CTOR-INIT,1110 %s
10+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -disable-red-zone -coverage-data-file=/dev/null -coverage-version='B21*' %s -o - | \
11+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-RT-INIT,1210 %s
1612
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -disable-red-zone -coverage-data-file=/dev/null %s -o - | \
17-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-RT-INIT,408 %s
13+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-RT-INIT,1110 %s
1814

1915
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -coverage-notes-file=aaa.gcno -coverage-data-file=bbb.gcda -debug-info-kind=limited -dwarf-version=4 %s -o - | FileCheck %s --check-prefix GCOV_FILE_INFO
2016

@@ -48,12 +44,10 @@ int test2(int b) {
4844
// CHECK-SAME: [%emit_function_args_ty { i32 0, i32 {{[-0-9]+}}, i32 {{[-0-9]+}} }, %emit_function_args_ty { i32 1, i32 {{[-0-9]+}}, i32 {{[-0-9]+}} }]
4945

5046
// CHECK: @__llvm_internal_gcov_emit_file_info = internal unnamed_addr constant [1 x %file_info]
51-
/// 0x3330342a '3' '0' '4' '*'
52-
// 304-SAME: i32 858797098
53-
/// 0x3430372a '4' '0' '7' '*'
54-
// 407-SAME: i32 875575082
55-
/// 0x3430382a '4' '0' '8' '*'
56-
// 408-SAME: i32 875575338
47+
/// 0x4231312a 'B' '1' '1' '*'
48+
// 1110-SAME: i32 1110520106
49+
/// 0x4232312a 'B' '2' '1' '*'
50+
// 1210-SAME: i32 1110585642
5751

5852
// Check for gcov initialization function pointers.
5953
// CHECK-RT-INIT: @__llvm_covinit_functions = private constant { ptr, ptr } { ptr @__llvm_gcov_writeout, ptr @__llvm_gcov_reset }, section "__llvm_covinit"

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,13 @@ TEST(ConfigParseTest, ParsesConfiguration) {
865865
CHECK_PARSE("SortUsingDeclarations: true", SortUsingDeclarations,
866866
FormatStyle::SUD_LexicographicNumeric);
867867

868+
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Never",
869+
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Never);
870+
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Always",
871+
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Always);
872+
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Leave",
873+
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Leave);
874+
868875
// FIXME: This is required because parsing a configuration simply overwrites
869876
// the first N elements of the list instead of resetting it.
870877
Style.ForEachMacros.clear();

0 commit comments

Comments
 (0)