Skip to content

Commit bc7f445

Browse files
committed
Merge remote-tracking branch 'origin/main' into manage-blocks-in-vplan
2 parents fc34ca5 + 998bdae commit bc7f445

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+801
-690
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,11 @@ the configuration (without a prefix: ``Auto``).
20882088
If ``true``, ``while (true) continue;`` can be put on a single
20892089
line.
20902090

2091+
.. _AllowShortNamespacesOnASingleLine:
2092+
2093+
**AllowShortNamespacesOnASingleLine** (``Boolean``) :versionbadge:`clang-format 20` :ref:`<AllowShortNamespacesOnASingleLine>`
2094+
If ``true``, ``namespace a { class b; }`` can be put on a single line.
2095+
20912096
.. _AlwaysBreakAfterDefinitionReturnType:
20922097

20932098
**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) :versionbadge:`clang-format 3.7` :ref:`<AlwaysBreakAfterDefinitionReturnType>`

clang/docs/LibASTMatchersReference.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2>
25362536
matches "decltype(i + j)"
25372537
</pre></td></tr>
25382538

2539+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>&gt;</td><td class="name" onclick="toggle('dependentNameType0')"><a name="dependentNameType0Anchor">dependentNameType</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>&gt;...</td></tr>
2540+
<tr><td colspan="4" class="doc" id="dependentNameType0"><pre>Matches a dependent name type.
2541+
2542+
Example matches T::type
2543+
2544+
template <typename T> struct declToImport {
2545+
typedef typename T::type dependent_name;
2546+
};
2547+
</pre></td></tr>
25392548

25402549
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>&gt;</td><td class="name" onclick="toggle('deducedTemplateSpecializationType0')"><a name="deducedTemplateSpecializationType0Anchor">deducedTemplateSpecializationType</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DeducedTemplateSpecializationType.html">DeducedTemplateSpecializationType</a>&gt;...</td></tr>
25412550
<tr><td colspan="4" class="doc" id="deducedTemplateSpecializationType0"><pre>Matches C++17 deduced template specialization types, e.g. deduced class

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,8 @@ AST Matchers
11101110

11111111
- Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.
11121112

1113+
- Add ``dependentNameType`` matcher to match a dependent name type.
1114+
11131115
clang-format
11141116
------------
11151117

@@ -1121,6 +1123,7 @@ clang-format
11211123
``Never``, and ``true`` to ``Always``.
11221124
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.
11231125
- Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style.
1126+
- Adds ``AllowShortNamespacesOnASingleLine`` option.
11241127

11251128
libclang
11261129
--------

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
77117711
return InnerType.matches(Node.getDecayedType(), Finder, Builder);
77127712
}
77137713

7714+
/// Matches a dependent name type
7715+
///
7716+
/// Example matches T::type
7717+
/// \code
7718+
/// template <typename T> struct declToImport {
7719+
/// typedef typename T::type dependent_name;
7720+
/// };
7721+
/// \endcode
7722+
extern const AstTypeMatcher<DependentNameType> dependentNameType;
7723+
77147724
/// Matches declarations whose declaration context, interpreted as a
77157725
/// Decl, matches \c InnerMatcher.
77167726
///

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,10 @@ struct FormatStyle {
988988
/// \version 3.7
989989
bool AllowShortLoopsOnASingleLine;
990990

991+
/// If ``true``, ``namespace a { class b; }`` can be put on a single line.
992+
/// \version 20
993+
bool AllowShortNamespacesOnASingleLine;
994+
991995
/// Different ways to break after the function definition return type.
992996
/// This option is **deprecated** and is retained for backwards compatibility.
993997
enum DefinitionReturnTypeBreakingStyle : int8_t {
@@ -5168,6 +5172,8 @@ struct FormatStyle {
51685172
R.AllowShortIfStatementsOnASingleLine &&
51695173
AllowShortLambdasOnASingleLine == R.AllowShortLambdasOnASingleLine &&
51705174
AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine &&
5175+
AllowShortNamespacesOnASingleLine ==
5176+
R.AllowShortNamespacesOnASingleLine &&
51715177
AlwaysBreakBeforeMultilineStrings ==
51725178
R.AlwaysBreakBeforeMultilineStrings &&
51735179
AttributeMacros == R.AttributeMacros &&

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ const AstTypeMatcher<SubstTemplateTypeParmType> substTemplateTypeParmType;
11081108
const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType;
11091109
const AstTypeMatcher<InjectedClassNameType> injectedClassNameType;
11101110
const AstTypeMatcher<DecayedType> decayedType;
1111+
const AstTypeMatcher<DependentNameType> dependentNameType;
11111112
AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasElementType,
11121113
AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
11131114
ComplexType));

clang/lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ RegistryMaps::RegistryMaps() {
222222
REGISTER_MATCHER(decompositionDecl);
223223
REGISTER_MATCHER(declCountIs);
224224
REGISTER_MATCHER(declRefExpr);
225+
REGISTER_MATCHER(dependentNameType);
225226
REGISTER_MATCHER(dependentScopeDeclRefExpr);
226227
REGISTER_MATCHER(declStmt);
227228
REGISTER_MATCHER(declaratorDecl);

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ template <> struct MappingTraits<FormatStyle> {
975975
Style.AllowShortLambdasOnASingleLine);
976976
IO.mapOptional("AllowShortLoopsOnASingleLine",
977977
Style.AllowShortLoopsOnASingleLine);
978+
IO.mapOptional("AllowShortNamespacesOnASingleLine",
979+
Style.AllowShortNamespacesOnASingleLine);
978980
IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
979981
Style.AlwaysBreakAfterDefinitionReturnType);
980982
IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
@@ -1480,6 +1482,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14801482
LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
14811483
LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
14821484
LLVMStyle.AllowShortLoopsOnASingleLine = false;
1485+
LLVMStyle.AllowShortNamespacesOnASingleLine = false;
14831486
LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
14841487
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
14851488
LLVMStyle.AttributeMacros.push_back("__capability");

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,18 @@ class LineJoiner {
361361
const auto *FirstNonComment = TheLine->getFirstNonComment();
362362
if (!FirstNonComment)
363363
return 0;
364+
364365
// FIXME: There are probably cases where we should use FirstNonComment
365366
// instead of TheLine->First.
366367

368+
if (Style.AllowShortNamespacesOnASingleLine &&
369+
TheLine->First->is(tok::kw_namespace) &&
370+
TheLine->Last->is(tok::l_brace)) {
371+
const auto result = tryMergeNamespace(I, E, Limit);
372+
if (result > 0)
373+
return result;
374+
}
375+
367376
if (Style.CompactNamespaces) {
368377
if (const auto *NSToken = TheLine->First->getNamespaceToken()) {
369378
int J = 1;
@@ -373,7 +382,7 @@ class LineJoiner {
373382
ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&
374383
I[J]->Last->TotalLength < Limit;
375384
++J, --ClosingLineIndex) {
376-
Limit -= I[J]->Last->TotalLength;
385+
Limit -= I[J]->Last->TotalLength + 1;
377386

378387
// Reduce indent level for bodies of namespaces which were compacted,
379388
// but only if their content was indented in the first place.
@@ -420,6 +429,7 @@ class LineJoiner {
420429
TheLine->First != LastNonComment) {
421430
return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
422431
}
432+
423433
// Try to merge a control statement block with left brace unwrapped.
424434
if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
425435
FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
@@ -616,6 +626,72 @@ class LineJoiner {
616626
return 1;
617627
}
618628

629+
unsigned tryMergeNamespace(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
630+
SmallVectorImpl<AnnotatedLine *>::const_iterator E,
631+
unsigned Limit) {
632+
if (Limit == 0)
633+
return 0;
634+
635+
assert(I[1]);
636+
const auto &L1 = *I[1];
637+
if (L1.InPPDirective != (*I)->InPPDirective ||
638+
(L1.InPPDirective && L1.First->HasUnescapedNewline)) {
639+
return 0;
640+
}
641+
642+
if (std::distance(I, E) <= 2)
643+
return 0;
644+
645+
assert(I[2]);
646+
const auto &L2 = *I[2];
647+
if (L2.Type == LT_Invalid)
648+
return 0;
649+
650+
Limit = limitConsideringMacros(I + 1, E, Limit);
651+
652+
if (!nextTwoLinesFitInto(I, Limit))
653+
return 0;
654+
655+
// Check if it's a namespace inside a namespace, and call recursively if so.
656+
// '3' is the sizes of the whitespace and closing brace for " _inner_ }".
657+
if (L1.First->is(tok::kw_namespace)) {
658+
if (L1.Last->is(tok::comment) || !Style.CompactNamespaces)
659+
return 0;
660+
661+
assert(Limit >= L1.Last->TotalLength + 3);
662+
const auto InnerLimit = Limit - L1.Last->TotalLength - 3;
663+
const auto MergedLines = tryMergeNamespace(I + 1, E, InnerLimit);
664+
if (MergedLines == 0)
665+
return 0;
666+
const auto N = MergedLines + 2;
667+
// Check if there is even a line after the inner result.
668+
if (std::distance(I, E) <= N)
669+
return 0;
670+
// Check that the line after the inner result starts with a closing brace
671+
// which we are permitted to merge into one line.
672+
if (I[N]->First->is(tok::r_brace) && !I[N]->First->MustBreakBefore &&
673+
I[MergedLines + 1]->Last->isNot(tok::comment) &&
674+
nextNLinesFitInto(I, I + N + 1, Limit)) {
675+
return N;
676+
}
677+
return 0;
678+
}
679+
680+
// There's no inner namespace, so we are considering to merge at most one
681+
// line.
682+
683+
// The line which is in the namespace should end with semicolon.
684+
if (L1.Last->isNot(tok::semi))
685+
return 0;
686+
687+
// Last, check that the third line starts with a closing brace.
688+
if (L2.First->isNot(tok::r_brace) || L2.First->MustBreakBefore)
689+
return 0;
690+
691+
// If so, merge all three lines.
692+
return 2;
693+
}
694+
619695
unsigned tryMergeSimpleControlStatement(
620696
SmallVectorImpl<AnnotatedLine *>::const_iterator I,
621697
SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
@@ -916,6 +992,21 @@ class LineJoiner {
916992
return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
917993
}
918994

995+
bool nextNLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
996+
SmallVectorImpl<AnnotatedLine *>::const_iterator E,
997+
unsigned Limit) {
998+
unsigned JoinedLength = 0;
999+
for (const auto *J = I + 1; J != E; ++J) {
1000+
if ((*J)->First->MustBreakBefore)
1001+
return false;
1002+
1003+
JoinedLength += 1 + (*J)->Last->TotalLength;
1004+
if (JoinedLength > Limit)
1005+
return false;
1006+
}
1007+
return true;
1008+
}
1009+
9191010
bool containsMustBreak(const AnnotatedLine *Line) {
9201011
assert(Line->First);
9211012
// Ignore the first token, because in this situation, it applies more to the

0 commit comments

Comments
 (0)