Skip to content

Commit 1e1a4d5

Browse files
authored
merge main into amd-staging (llvm#4147)
2 parents 74e05a8 + eb93a2e commit 1e1a4d5

39 files changed

+637
-857
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,13 @@ the configuration (without a prefix: ``Auto``).
17951795

17961796

17971797

1798+
.. _AllowBreakBeforeQtProperty:
1799+
1800+
**AllowBreakBeforeQtProperty** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<AllowBreakBeforeQtProperty>`
1801+
Allow breaking before ``Q_Property`` keywords ``READ``, ``WRITE``, etc. as
1802+
if they were preceded by a comma (``,``). This allows them to be formatted
1803+
according to ``BinPackParameters``.
1804+
17981805
.. _AllowShortBlocksOnASingleLine:
17991806

18001807
**AllowShortBlocksOnASingleLine** (``ShortBlockStyle``) :versionbadge:`clang-format 3.5` :ref:`<AllowShortBlocksOnASingleLine>`

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ clang-format
784784
- Add ``NumericLiteralCase`` option for enforcing character case in numeric
785785
literals.
786786
- Add ``Leave`` suboption to ``IndentPPDirectives``.
787+
- Add ``AllowBreakBeforeQtProperty`` option.
787788

788789
libclang
789790
--------

clang/include/clang/Format/Format.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ struct FormatStyle {
732732
/// \version 18
733733
BreakBeforeNoexceptSpecifierStyle AllowBreakBeforeNoexceptSpecifier;
734734

735+
/// Allow breaking before ``Q_Property`` keywords ``READ``, ``WRITE``, etc. as
736+
/// if they were preceded by a comma (``,``). This allows them to be formatted
737+
/// according to ``BinPackParameters``.
738+
/// \version 22
739+
bool AllowBreakBeforeQtProperty;
740+
735741
/// Different styles for merging short blocks containing at most one
736742
/// statement.
737743
enum ShortBlockStyle : int8_t {
@@ -5458,6 +5464,7 @@ struct FormatStyle {
54585464
R.AllowAllParametersOfDeclarationOnNextLine &&
54595465
AllowBreakBeforeNoexceptSpecifier ==
54605466
R.AllowBreakBeforeNoexceptSpecifier &&
5467+
AllowBreakBeforeQtProperty == R.AllowBreakBeforeQtProperty &&
54615468
AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine &&
54625469
AllowShortCaseExpressionOnASingleLine ==
54635470
R.AllowShortCaseExpressionOnASingleLine &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ template <> struct MappingTraits<FormatStyle> {
10281028
Style.AllowAllParametersOfDeclarationOnNextLine);
10291029
IO.mapOptional("AllowBreakBeforeNoexceptSpecifier",
10301030
Style.AllowBreakBeforeNoexceptSpecifier);
1031+
IO.mapOptional("AllowBreakBeforeQtProperty",
1032+
Style.AllowBreakBeforeQtProperty);
10311033
IO.mapOptional("AllowShortBlocksOnASingleLine",
10321034
Style.AllowShortBlocksOnASingleLine);
10331035
IO.mapOptional("AllowShortCaseExpressionOnASingleLine",
@@ -1567,6 +1569,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15671569
LLVMStyle.AllowAllArgumentsOnNextLine = true;
15681570
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
15691571
LLVMStyle.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Never;
1572+
LLVMStyle.AllowBreakBeforeQtProperty = false;
15701573
LLVMStyle.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
15711574
LLVMStyle.AllowShortCaseExpressionOnASingleLine = true;
15721575
LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;

clang/lib/Format/FormatToken.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ const char *getTokenTypeName(TokenType Type) {
3333
return nullptr;
3434
}
3535

36+
static constexpr std::array<StringRef, 14> QtPropertyKeywords = {
37+
"BINDABLE", "CONSTANT", "DESIGNABLE", "FINAL", "MEMBER",
38+
"NOTIFY", "READ", "REQUIRED", "RESET", "REVISION",
39+
"SCRIPTABLE", "STORED", "USER", "WRITE",
40+
};
41+
42+
bool FormatToken::isQtProperty() const {
43+
assert(llvm::is_sorted(QtPropertyKeywords));
44+
return std::binary_search(QtPropertyKeywords.begin(),
45+
QtPropertyKeywords.end(), TokenText);
46+
}
47+
3648
// Sorted common C++ non-keyword types.
37-
static SmallVector<StringRef> CppNonKeywordTypes = {
49+
static constexpr std::array<StringRef, 14> CppNonKeywordTypes = {
3850
"clock_t", "int16_t", "int32_t", "int64_t", "int8_t",
3951
"intptr_t", "ptrdiff_t", "size_t", "time_t", "uint16_t",
4052
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
@@ -330,6 +342,8 @@ bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style) {
330342
}
331343
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
332344
return true;
345+
if (Current.is(TT_QtProperty))
346+
return true;
333347
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
334348
((Previous.isNot(TT_CtorInitializerComma) ||
335349
Style.BreakConstructorInitializers !=

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ namespace format {
136136
TYPE(PointerOrReference) \
137137
TYPE(ProtoExtensionLSquare) \
138138
TYPE(PureVirtualSpecifier) \
139+
TYPE(QtProperty) \
139140
TYPE(RangeBasedForLoopColon) \
140141
TYPE(RecordLBrace) \
141142
TYPE(RecordRBrace) \
@@ -703,6 +704,7 @@ struct FormatToken {
703704
isAttribute();
704705
}
705706

707+
[[nodiscard]] bool isQtProperty() const;
706708
[[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const;
707709
[[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
708710

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ class AnnotatingParser {
384384
OpeningParen.Previous->is(tok::kw__Generic)) {
385385
Contexts.back().ContextType = Context::C11GenericSelection;
386386
Contexts.back().IsExpression = true;
387+
} else if (OpeningParen.Previous &&
388+
OpeningParen.Previous->TokenText == "Q_PROPERTY") {
389+
Contexts.back().ContextType = Context::QtProperty;
390+
Contexts.back().IsExpression = false;
387391
} else if (Line.InPPDirective &&
388392
(!OpeningParen.Previous ||
389393
OpeningParen.Previous->isNot(tok::identifier))) {
@@ -1803,6 +1807,11 @@ class AnnotatingParser {
18031807
return false;
18041808
}
18051809
}
1810+
if (Style.AllowBreakBeforeQtProperty &&
1811+
Contexts.back().ContextType == Context::QtProperty &&
1812+
Tok->isQtProperty()) {
1813+
Tok->setFinalizedType(TT_QtProperty);
1814+
}
18061815
break;
18071816
case tok::arrow:
18081817
if (Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
@@ -2169,6 +2178,7 @@ class AnnotatingParser {
21692178
TemplateArgument,
21702179
// C11 _Generic selection.
21712180
C11GenericSelection,
2181+
QtProperty,
21722182
// Like in the outer parentheses in `ffnand ff1(.q());`.
21732183
VerilogInstancePortList,
21742184
} ContextType = Unknown;
@@ -6254,7 +6264,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62546264
Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
62556265
}
62566266
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6257-
TT_ClassHeadName, tok::kw_operator)) {
6267+
TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
62586268
return true;
62596269
}
62606270
if (Left.is(TT_PointerOrReference))

clang/test/AST/ByteCode/const-eval.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
/// This is a version of test/Sema/const-eval.c with the
77
/// tests commented out that the new constant expression interpreter does
8-
/// not support yet. They are all marked with the NEW_INTERP define:
9-
///
10-
/// - builtin_constant_p
11-
/// - unions
12-
8+
/// not support yet. They are all marked with the NEW_INTERP define.
139

1410
#define EVAL_EXPR(testno, expr) enum { test##testno = (expr) }; struct check_positive##testno { int a[test##testno]; };
1511
int x;
@@ -52,9 +48,7 @@ struct s {
5248

5349
EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
5450

55-
#ifndef NEW_INTERP
5651
EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));
57-
#endif
5852

5953
EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);
6054

@@ -112,11 +106,9 @@ int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // both-error {{variable lengt
112106
union u { int a; char b[4]; };
113107
char c = ((union u)(123456)).b[0]; // both-error {{not a compile-time constant}}
114108

115-
#ifndef NEW_INTERP
116109
extern const int weak_int __attribute__((weak));
117110
const int weak_int = 42;
118111
int weak_int_test = weak_int; // both-error {{not a compile-time constant}}
119-
#endif
120112

121113
int literalVsNull1 = "foo" == 0;
122114
int literalVsNull2 = 0 == "foo";
@@ -125,10 +117,8 @@ int literalVsNull2 = 0 == "foo";
125117
int castViaInt[*(int*)(unsigned long)"test"]; // both-error {{variable length array}}
126118

127119
// PR11391.
128-
#ifndef NEW_INTERP
129120
struct PR11391 { _Complex float f; } pr11391;
130121
EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1))
131-
#endif
132122

133123
// PR12043
134124
float varfloat;

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
161161
Style.Language = FormatStyle::LK_Cpp;
162162
CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
163163
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
164+
CHECK_PARSE_BOOL(AllowBreakBeforeQtProperty);
164165
CHECK_PARSE_BOOL(AllowShortCaseExpressionOnASingleLine);
165166
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
166167
CHECK_PARSE_BOOL(AllowShortCompoundRequirementOnASingleLine);

clang/unittests/Format/FormatTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28772,6 +28772,36 @@ TEST_F(FormatTest, BreakBeforeClassName) {
2877228772
" ArenaSafeUniquePtr {};");
2877328773
}
2877428774

28775+
TEST_F(FormatTest, KeywordedFunctionLikeMacros) {
28776+
constexpr StringRef Code("Q_PROPERTY(int name\n"
28777+
" READ name\n"
28778+
" WRITE setName\n"
28779+
" NOTIFY nameChanged)");
28780+
constexpr StringRef Code2("class A {\n"
28781+
" Q_PROPERTY(int name\n"
28782+
" READ name\n"
28783+
" WRITE setName\n"
28784+
" NOTIFY nameChanged)\n"
28785+
"};");
28786+
28787+
auto Style = getLLVMStyle();
28788+
Style.AllowBreakBeforeQtProperty = true;
28789+
28790+
Style.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
28791+
verifyFormat(Code, Style);
28792+
verifyFormat(Code2, Style);
28793+
28794+
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
28795+
Style.ColumnLimit = 40;
28796+
verifyFormat(Code, Style);
28797+
verifyFormat(Code2, Style);
28798+
verifyFormat("/* sdf */ Q_PROPERTY(int name\n"
28799+
" READ name\n"
28800+
" WRITE setName\n"
28801+
" NOTIFY nameChanged)",
28802+
Style);
28803+
}
28804+
2877528805
} // namespace
2877628806
} // namespace test
2877728807
} // namespace format

0 commit comments

Comments
 (0)