Skip to content

Commit 34e7be5

Browse files
committed
Address comments, rebase
Created using spr 1.3.5
2 parents 09b149c + b75f9f7 commit 34e7be5

File tree

392 files changed

+25048
-6509
lines changed

Some content is hidden

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

392 files changed

+25048
-6509
lines changed

clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void CrtpConstructorAccessibilityCheck::check(
165165

166166
WithFriendHintIfNeeded(
167167
diag(Ctor->getLocation(),
168-
"%0 contructor allows the CRTP to be %select{inherited "
168+
"%0 constructor allows the CRTP to be %select{inherited "
169169
"from|constructed}1 as a regular template class; consider making "
170170
"it private%select{| and declaring the derived class as friend}2")
171171
<< Access << IsPublic << NeedsFriend

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
7070
Options.get("WarnOnSizeOfCompareToConstant", true)),
7171
WarnOnSizeOfPointerToAggregate(
7272
Options.get("WarnOnSizeOfPointerToAggregate", true)),
73-
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)) {}
73+
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)),
74+
WarnOnOffsetDividedBySizeOf(
75+
Options.get("WarnOnOffsetDividedBySizeOf", true)) {}
7476

7577
void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7678
Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant);
@@ -82,6 +84,8 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
8284
Options.store(Opts, "WarnOnSizeOfPointerToAggregate",
8385
WarnOnSizeOfPointerToAggregate);
8486
Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer);
87+
Options.store(Opts, "WarnOnOffsetDividedBySizeOf",
88+
WarnOnOffsetDividedBySizeOf);
8589
}
8690

8791
void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
@@ -307,7 +311,8 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
307311
offsetOfExpr()))
308312
.bind("sizeof-in-ptr-arithmetic-scale-expr");
309313
const auto PtrArithmeticIntegerScaleExpr = binaryOperator(
310-
hasAnyOperatorName("*", "/"),
314+
WarnOnOffsetDividedBySizeOf ? binaryOperator(hasAnyOperatorName("*", "/"))
315+
: binaryOperator(hasOperatorName("*")),
311316
// sizeof(...) * sizeof(...) and sizeof(...) / sizeof(...) is handled
312317
// by this check on another path.
313318
hasOperands(expr(hasType(isInteger()), unless(SizeofLikeScaleExpr)),

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SizeofExpressionCheck : public ClangTidyCheck {
3131
const bool WarnOnSizeOfCompareToConstant;
3232
const bool WarnOnSizeOfPointerToAggregate;
3333
const bool WarnOnSizeOfPointer;
34+
const bool WarnOnOffsetDividedBySizeOf;
3435
};
3536

3637
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Changes in existing checks
158158
- Improved :doc:`bugprone-sizeof-expression
159159
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
160160
usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
161-
subtracting from a pointer.
161+
subtracting from a pointer directly or when used to scale a numeric value.
162162

163163
- Improved :doc:`bugprone-unchecked-optional-access
164164
<clang-tidy/checks/bugprone/unchecked-optional-access>` to support
@@ -245,7 +245,7 @@ Changes in existing checks
245245
remove `->`, when redundant `get()` is removed.
246246

247247
- Improved :doc:`readability-identifier-naming
248-
<clang-tidy/checks/readability/readability-identifier-naming>` check to
248+
<clang-tidy/checks/readability/identifier-naming>` check to
249249
validate ``namespace`` aliases.
250250

251251
Removed checks

clang-tools-extra/docs/clang-tidy/checks/bugprone/sizeof-expression.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ and the result is typically unintended, often out of bounds.
221221
``Ptr + sizeof(T)`` will offset the pointer by ``sizeof(T)`` elements,
222222
effectively exponentiating the scaling factor to the power of 2.
223223

224+
Similarly, multiplying or dividing a numeric value with the ``sizeof`` of an
225+
element or the whole buffer is suspicious, because the dimensional connection
226+
between the numeric value and the actual ``sizeof`` result can not always be
227+
deduced.
228+
While scaling an integer up (multiplying) with ``sizeof`` is likely **always**
229+
an issue, a scaling down (division) is not always inherently dangerous, in case
230+
the developer is aware that the division happens between an appropriate number
231+
of _bytes_ and a ``sizeof`` value.
232+
Turning :option:`WarnOnOffsetDividedBySizeOf` off will restrict the
233+
warnings to the multiplication case.
234+
224235
This case also checks suspicious ``alignof`` and ``offsetof`` usages in
225236
pointer arithmetic, as both return the "size" in bytes and not elements,
226237
potentially resulting in doubly-scaled offsets.
@@ -299,3 +310,9 @@ Options
299310
``sizeof`` is an expression that produces a pointer (except for a few
300311
idiomatic expressions that are probably intentional and correct).
301312
This detects occurrences of CWE 467. Default is `false`.
313+
314+
.. option:: WarnOnOffsetDividedBySizeOf
315+
316+
When `true`, the check will warn on pointer arithmetic where the
317+
element count is obtained from a division with ``sizeof(...)``,
318+
e.g., ``Ptr + Bytes / sizeof(*T)``. Default is `true`.

clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ template <typename T>
2626
class CRTP {
2727
public:
2828
CRTP() = default;
29-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
29+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
3030
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
3131
// CHECK-FIXES: friend T;
3232
};
@@ -39,7 +39,7 @@ template <typename T>
3939
class CRTP {
4040
public:
4141
CRTP(int) {}
42-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
42+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
4343
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
4444
// CHECK-FIXES: friend T;
4545
};
@@ -52,10 +52,10 @@ template <typename T>
5252
class CRTP {
5353
public:
5454
CRTP(int) {}
55-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
55+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
5656
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
5757
CRTP(float) {}
58-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
58+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
5959
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) {}{{[[:space:]]*}}public:
6060

6161
// CHECK-FIXES: friend T;
@@ -70,13 +70,13 @@ template <typename T>
7070
class CRTP {
7171
protected:
7272
CRTP(int) {}
73-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
73+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected constructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
7474
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}protected:
7575
CRTP() = default;
76-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
76+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected constructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
7777
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}protected:
7878
CRTP(float) {}
79-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
79+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected constructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
8080
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) {}{{[[:space:]]*}}protected:
8181

8282
// CHECK-FIXES: friend T;
@@ -101,7 +101,7 @@ namespace struct_default_ctor {
101101
template <typename T>
102102
struct CRTP {
103103
CRTP() = default;
104-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
104+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
105105
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
106106
// CHECK-FIXES: friend T;
107107
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- \
2+
// RUN: -config='{CheckOptions: { \
3+
// RUN: bugprone-sizeof-expression.WarnOnOffsetDividedBySizeOf: false \
4+
// RUN: }}'
5+
6+
typedef __SIZE_TYPE__ size_t;
7+
8+
void situational14(int *Buffer, size_t BufferSize) {
9+
int *P = &Buffer[0];
10+
while (P < Buffer + BufferSize / sizeof(*Buffer)) {
11+
// NO-WARNING: This test opted out of "P +- N */ sizeof(...)" warnings.
12+
++P;
13+
}
14+
}

clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-pointer-arithmetics.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,30 @@ void good13(void) {
352352
int Buffer[BufferSize];
353353

354354
int *P = &Buffer[0];
355-
while (P < (Buffer + sizeof(Buffer) / sizeof(int))) {
355+
while (P < Buffer + sizeof(Buffer) / sizeof(int)) {
356356
// NO-WARNING: Calculating the element count of the buffer here, which is
357357
// safe with this idiom (as long as the types don't change).
358358
++P;
359359
}
360360

361-
while (P < (Buffer + sizeof(Buffer) / sizeof(Buffer[0]))) {
361+
while (P < Buffer + sizeof(Buffer) / sizeof(Buffer[0])) {
362362
// NO-WARNING: Calculating the element count of the buffer here, which is
363363
// safe with this idiom.
364364
++P;
365365
}
366366

367-
while (P < (Buffer + sizeof(Buffer) / sizeof(*P))) {
367+
while (P < Buffer + sizeof(Buffer) / sizeof(*P)) {
368368
// NO-WARNING: Calculating the element count of the buffer here, which is
369369
// safe with this idiom.
370370
++P;
371371
}
372372
}
373+
374+
void situational14(int *Buffer, size_t BufferSize) {
375+
int *P = &Buffer[0];
376+
while (P < Buffer + BufferSize / sizeof(*Buffer)) {
377+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: suspicious usage of 'sizeof(...)' in pointer arithmetic; this scaled value will be scaled again by the '+' operator
378+
// CHECK-MESSAGES: :[[@LINE-2]]:21: note: '+' in pointer arithmetic internally scales with 'sizeof(int)' == {{[0-9]+}}
379+
++P;
380+
}
381+
}

clang/docs/LanguageExtensions.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5883,3 +5883,26 @@ specify the starting offset to begin embedding from. The resources is treated
58835883
as being empty if the specified offset is larger than the number of bytes in
58845884
the resource. The offset will be applied *before* any ``limit`` parameters are
58855885
applied.
5886+
5887+
Union and aggregate initialization in C
5888+
=======================================
5889+
5890+
In C23 (N2900), when an object is initialized from initializer ``= {}``, all
5891+
elements of arrays, all members of structs, and the first members of unions are
5892+
empty-initialized recursively. In addition, all padding bits are initialized to
5893+
zero.
5894+
5895+
Clang guarantees the following behaviors:
5896+
5897+
* ``1:`` Clang supports initializer ``= {}`` mentioned above in all C
5898+
standards.
5899+
5900+
* ``2:`` When unions are initialized from initializer ``= {}``, bytes outside
5901+
of the first members of unions are also initialized to zero.
5902+
5903+
* ``3:`` When unions, structures and arrays are initialized from initializer
5904+
``= { initializer-list }``, all members not explicitly initialized in
5905+
the initializer list are empty-initialized recursively. In addition, all
5906+
padding bits are initialized to zero.
5907+
5908+
Currently, the above extension only applies to C source code, not C++.

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,14 @@ Bug Fixes to C++ Support
509509
a class template. (#GH102320)
510510
- Fix a crash when parsing a pseudo destructor involving an invalid type. (#GH111460)
511511
- Fixed an assertion failure when invoking recovery call expressions with explicit attributes
512-
and undeclared templates. (#GH107047, #GH49093)
512+
and undeclared templates. (#GH107047), (#GH49093)
513513
- Clang no longer crashes when a lambda contains an invalid block declaration that contains an unexpanded
514514
parameter pack. (#GH109148)
515515
- Fixed overload handling for object parameters with top-level cv-qualifiers in explicit member functions (#GH100394)
516+
- Fixed a bug in lambda captures where ``constexpr`` class-type objects were not properly considered ODR-used in
517+
certain situations. (#GH47400), (#GH90896)
518+
- Fix erroneous templated array size calculation leading to crashes in generated code. (#GH41441)
519+
- During the lookup for a base class name, non-type names are ignored. (#GH16855)
516520

517521
Bug Fixes to AST Handling
518522
^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)