Skip to content

Commit 096ed0c

Browse files
committed
Merge "merge main into amd-staging" into amd-staging
2 parents 5f4539b + da6fbd6 commit 096ed0c

File tree

412 files changed

+23266
-5689
lines changed

Some content is hidden

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

412 files changed

+23266
-5689
lines changed

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

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
6767
WarnOnSizeOfCompareToConstant(
6868
Options.get("WarnOnSizeOfCompareToConstant", true)),
6969
WarnOnSizeOfPointerToAggregate(
70-
Options.get("WarnOnSizeOfPointerToAggregate", true)) {}
70+
Options.get("WarnOnSizeOfPointerToAggregate", true)),
71+
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)) {}
7172

7273
void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7374
Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant);
@@ -78,6 +79,7 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7879
WarnOnSizeOfCompareToConstant);
7980
Options.store(Opts, "WarnOnSizeOfPointerToAggregate",
8081
WarnOnSizeOfPointerToAggregate);
82+
Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer);
8183
}
8284

8385
void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
@@ -127,17 +129,30 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
127129
const auto ConstStrLiteralDecl =
128130
varDecl(isDefinition(), hasType(hasCanonicalType(CharPtrType)),
129131
hasInitializer(ignoringParenImpCasts(stringLiteral())));
132+
const auto VarWithConstStrLiteralDecl = expr(
133+
hasType(hasCanonicalType(CharPtrType)),
134+
ignoringParenImpCasts(declRefExpr(hasDeclaration(ConstStrLiteralDecl))));
130135
Finder->addMatcher(
131-
sizeOfExpr(has(ignoringParenImpCasts(
132-
expr(hasType(hasCanonicalType(CharPtrType)),
133-
ignoringParenImpCasts(declRefExpr(
134-
hasDeclaration(ConstStrLiteralDecl)))))))
136+
sizeOfExpr(has(ignoringParenImpCasts(VarWithConstStrLiteralDecl)))
135137
.bind("sizeof-charp"),
136138
this);
137139

138-
// Detect sizeof(ptr) where ptr points to an aggregate (i.e. sizeof(&S)).
139-
// Do not find it if RHS of a 'sizeof(arr) / sizeof(arr[0])' expression.
140-
if (WarnOnSizeOfPointerToAggregate) {
140+
// Detect sizeof(ptr) where ptr is a pointer (CWE-467).
141+
//
142+
// In WarnOnSizeOfPointerToAggregate mode only report cases when ptr points
143+
// to an aggregate type or ptr is an expression that (implicitly or
144+
// explicitly) casts an array to a pointer type. (These are more suspicious
145+
// than other sizeof(ptr) expressions because they can appear as distorted
146+
// forms of the common sizeof(aggregate) expressions.)
147+
//
148+
// To avoid false positives, the check doesn't report expressions like
149+
// 'sizeof(pp[0])' and 'sizeof(*pp)' where `pp` is a pointer-to-pointer or
150+
// array of pointers. (This filters out both `sizeof(arr) / sizeof(arr[0])`
151+
// expressions and other cases like `p = realloc(p, newsize * sizeof(*p));`.)
152+
//
153+
// Moreover this generic message is suppressed in cases that are also matched
154+
// by the more concrete matchers 'sizeof-this' and 'sizeof-charp'.
155+
if (WarnOnSizeOfPointerToAggregate || WarnOnSizeOfPointer) {
141156
const auto ArrayExpr =
142157
ignoringParenImpCasts(hasType(hasCanonicalType(arrayType())));
143158
const auto ArrayCastExpr = expr(anyOf(
@@ -149,32 +164,31 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
149164

150165
const auto PointerToStructType =
151166
hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
152-
const auto PointerToStructExpr = expr(
153-
hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr()));
154-
155-
const auto ArrayOfPointersExpr = ignoringParenImpCasts(
156-
hasType(hasCanonicalType(arrayType(hasElementType(pointerType()))
157-
.bind("type-of-array-of-pointers"))));
158-
const auto ArrayOfSamePointersExpr =
159-
ignoringParenImpCasts(hasType(hasCanonicalType(
160-
arrayType(equalsBoundNode("type-of-array-of-pointers")))));
167+
const auto PointerToStructTypeWithBinding =
168+
type(PointerToStructType).bind("struct-type");
169+
const auto PointerToStructExpr =
170+
expr(hasType(hasCanonicalType(PointerToStructType)));
171+
172+
const auto PointerToDetectedExpr =
173+
WarnOnSizeOfPointer
174+
? expr(hasType(hasUnqualifiedDesugaredType(pointerType())))
175+
: expr(anyOf(ArrayCastExpr, PointerToArrayExpr,
176+
PointerToStructExpr));
177+
161178
const auto ZeroLiteral = ignoringParenImpCasts(integerLiteral(equals(0)));
162-
const auto ArrayOfSamePointersZeroSubscriptExpr =
163-
ignoringParenImpCasts(arraySubscriptExpr(
164-
hasBase(ArrayOfSamePointersExpr), hasIndex(ZeroLiteral)));
165-
const auto ArrayLengthExprDenom =
166-
expr(hasParent(binaryOperator(hasOperatorName("/"),
167-
hasLHS(ignoringParenImpCasts(sizeOfExpr(
168-
has(ArrayOfPointersExpr)))))),
169-
sizeOfExpr(has(ArrayOfSamePointersZeroSubscriptExpr)));
179+
const auto SubscriptExprWithZeroIndex =
180+
arraySubscriptExpr(hasIndex(ZeroLiteral));
181+
const auto DerefExpr =
182+
ignoringParenImpCasts(unaryOperator(hasOperatorName("*")));
170183

171184
Finder->addMatcher(
172-
expr(sizeOfExpr(anyOf(
173-
has(ignoringParenImpCasts(anyOf(
174-
ArrayCastExpr, PointerToArrayExpr, PointerToStructExpr))),
175-
has(PointerToStructType))),
176-
unless(ArrayLengthExprDenom))
177-
.bind("sizeof-pointer-to-aggregate"),
185+
expr(sizeOfExpr(anyOf(has(ignoringParenImpCasts(
186+
expr(PointerToDetectedExpr, unless(DerefExpr),
187+
unless(SubscriptExprWithZeroIndex),
188+
unless(VarWithConstStrLiteralDecl),
189+
unless(cxxThisExpr())))),
190+
has(PointerToStructTypeWithBinding))))
191+
.bind("sizeof-pointer"),
178192
this);
179193
}
180194

@@ -292,11 +306,17 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
292306
diag(E->getBeginLoc(),
293307
"suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
294308
<< E->getSourceRange();
295-
} else if (const auto *E =
296-
Result.Nodes.getNodeAs<Expr>("sizeof-pointer-to-aggregate")) {
297-
diag(E->getBeginLoc(),
298-
"suspicious usage of 'sizeof(A*)'; pointer to aggregate")
299-
<< E->getSourceRange();
309+
} else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
310+
if (Result.Nodes.getNodeAs<Type>("struct-type")) {
311+
diag(E->getBeginLoc(),
312+
"suspicious usage of 'sizeof(A*)' on pointer-to-aggregate type; did "
313+
"you mean 'sizeof(A)'?")
314+
<< E->getSourceRange();
315+
} else {
316+
diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
317+
"that results in a pointer")
318+
<< E->getSourceRange();
319+
}
300320
} else if (const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
301321
"sizeof-compare-constant")) {
302322
diag(E->getOperatorLoc(),
@@ -332,18 +352,23 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
332352
" numerator is not a multiple of denominator")
333353
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
334354
} else if (NumTy && DenomTy && NumTy == DenomTy) {
355+
// FIXME: This message is wrong, it should not refer to sizeof "pointer"
356+
// usage (and by the way, it would be to clarify all the messages).
335357
diag(E->getOperatorLoc(),
336358
"suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'")
337359
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
338-
} else if (PointedTy && DenomTy && PointedTy == DenomTy) {
339-
diag(E->getOperatorLoc(),
340-
"suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'")
341-
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
342-
} else if (NumTy && DenomTy && NumTy->isPointerType() &&
343-
DenomTy->isPointerType()) {
344-
diag(E->getOperatorLoc(),
345-
"suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'")
346-
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
360+
} else if (!WarnOnSizeOfPointer) {
361+
// When 'WarnOnSizeOfPointer' is enabled, these messages become redundant:
362+
if (PointedTy && DenomTy && PointedTy == DenomTy) {
363+
diag(E->getOperatorLoc(),
364+
"suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'")
365+
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
366+
} else if (NumTy && DenomTy && NumTy->isPointerType() &&
367+
DenomTy->isPointerType()) {
368+
diag(E->getOperatorLoc(),
369+
"suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'")
370+
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
371+
}
347372
}
348373
} else if (const auto *E =
349374
Result.Nodes.getNodeAs<Expr>("sizeof-sizeof-expr")) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SizeofExpressionCheck : public ClangTidyCheck {
3030
const bool WarnOnSizeOfThis;
3131
const bool WarnOnSizeOfCompareToConstant;
3232
const bool WarnOnSizeOfPointerToAggregate;
33+
const bool WarnOnSizeOfPointer;
3334
};
3435

3536
} // namespace clang::tidy::bugprone

clang-tools-extra/clangd/index/remote/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ if (CLANGD_ENABLE_REMOTE)
2626
clangdRemoteIndexProto
2727
clangdRemoteIndexServiceProto
2828
clangdRemoteMarshalling
29-
clangBasic
3029
clangDaemon
3130
clangdSupport
3231

@@ -35,6 +34,11 @@ if (CLANGD_ENABLE_REMOTE)
3534
clangdRemoteIndexServiceProto
3635
)
3736

37+
clang_target_link_libraries(clangdRemoteIndex
38+
PRIVATE
39+
clangBasic
40+
)
41+
3842
add_subdirectory(marshalling)
3943
add_subdirectory(server)
4044
add_subdirectory(monitor)

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ Changes in existing checks
237237
<clang-tidy/checks/bugprone/optional-value-conversion>` check by eliminating
238238
false positives resulting from use of optionals in unevaluated context.
239239

240+
- Improved :doc:`bugprone-sizeof-expression
241+
<clang-tidy/checks/bugprone/sizeof-expression>` check by eliminating some
242+
false positives and adding a new (off-by-default) option
243+
`WarnOnSizeOfPointer` that reports all ``sizeof(pointer)`` expressions
244+
(except for a few that are idiomatic).
245+
240246
- Improved :doc:`bugprone-suspicious-include
241247
<clang-tidy/checks/bugprone/suspicious-include>` check by replacing the local
242248
options `HeaderFileExtensions` and `ImplementationFileExtensions` by the
@@ -331,6 +337,10 @@ Changes in existing checks
331337
<clang-tidy/checks/misc/header-include-cycle>` check by avoiding crash for self
332338
include cycles.
333339

340+
- Improved :doc:`misc-include-cleaner
341+
<clang-tidy/checks/misc/include-cleaner>` check by avoiding false positives for
342+
the functions with the same name as standard library functions.
343+
334344
- Improved :doc:`misc-unused-using-decls
335345
<clang-tidy/checks/misc/unused-using-decls>` check by replacing the local
336346
option `HeaderFileExtensions` by the global option of the same name.

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ Options
190190

191191
.. option:: WarnOnSizeOfPointerToAggregate
192192

193-
When `true`, the check will warn on an expression like
194-
``sizeof(expr)`` where the expression is a pointer
195-
to aggregate. Default is `true`.
193+
When `true`, the check will warn when the argument of ``sizeof`` is either a
194+
pointer-to-aggregate type, an expression returning a pointer-to-aggregate
195+
value or an expression that returns a pointer from an array-to-pointer
196+
conversion (that may be implicit or explicit, for example ``array + 2`` or
197+
``(int *)array``). Default is `true`.
198+
199+
.. option:: WarnOnSizeOfPointer
200+
201+
When `true`, the check will report all expressions where the argument of
202+
``sizeof`` is an expression that produces a pointer (except for a few
203+
idiomatic expressions that are probably intentional and correct).
204+
This detects occurrences of CWE 467. Default is `false`.

clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/AST/DeclTemplate.h"
1515
#include "clang/Tooling/Inclusions/StandardLibrary.h"
1616
#include "llvm/Support/Casting.h"
17+
#include "llvm/Support/raw_ostream.h"
1718
#include <utility>
1819
#include <vector>
1920

@@ -40,8 +41,11 @@ Hints declHints(const Decl *D) {
4041
std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
4142
std::vector<Hinted<SymbolLocation>> Result;
4243
// FIXME: Should we also provide physical locations?
43-
if (auto SS = tooling::stdlib::Recognizer()(&D))
44-
return {{*SS, Hints::CompleteSymbol}};
44+
if (auto SS = tooling::stdlib::Recognizer()(&D)) {
45+
Result.push_back({*SS, Hints::CompleteSymbol});
46+
if (!D.hasBody())
47+
return Result;
48+
}
4549
// FIXME: Signal foreign decls, e.g. a forward declaration not owned by a
4650
// library. Some useful signals could be derived by checking the DeclContext.
4751
// Most incidental forward decls look like:

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,17 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
628628
tooling::stdlib::Header::named("<assert.h>")));
629629
}
630630

631+
TEST_F(HeadersForSymbolTest, NonStandardHeaders) {
632+
Inputs.Code = "void assert() {}";
633+
buildAST();
634+
EXPECT_THAT(
635+
headersFor("assert"),
636+
// Respect the ordering from the stdlib mapping.
637+
UnorderedElementsAre(physicalHeader("input.mm"),
638+
tooling::stdlib::Header::named("<cassert>"),
639+
tooling::stdlib::Header::named("<assert.h>")));
640+
}
641+
631642
TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {
632643
Inputs.Code = R"cpp(
633644
#include "exporter/foo.h"

clang-tools-extra/pseudo/lib/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ add_clang_library(clangPseudo
1414
Token.cpp
1515

1616
LINK_LIBS
17-
clangBasic
18-
clangLex
1917
clangPseudoGrammar
2018

2119
DEPENDS
@@ -25,3 +23,9 @@ add_clang_library(clangPseudo
2523
target_include_directories(clangPseudo INTERFACE
2624
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
2725
)
26+
27+
clang_target_link_libraries(clangPseudo
28+
PRIVATE
29+
clangBasic
30+
clangLex
31+
)

clang-tools-extra/pseudo/lib/cxx/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ add_clang_library(clangPseudoCXX
99
cxx_gen
1010

1111
LINK_LIBS
12-
clangBasic
1312
clangPseudo
1413
clangPseudoGrammar
1514
)
15+
16+
clang_target_link_libraries(clangPseudoCXX
17+
PRIVATE
18+
clangBasic
19+
)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ int Test5() {
3434

3535
int sum = 0;
3636
sum += sizeof(&S);
37-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
37+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
3838
sum += sizeof(__typeof(&S));
3939
sum += sizeof(&TS);
40-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
40+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
4141
sum += sizeof(__typeof(&TS));
4242
sum += sizeof(STRKWD MyStruct*);
4343
sum += sizeof(__typeof(STRKWD MyStruct*));
4444
sum += sizeof(TypedefStruct*);
4545
sum += sizeof(__typeof(TypedefStruct*));
4646
sum += sizeof(PTTS);
47-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
47+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
4848
sum += sizeof(PMyStruct);
4949
sum += sizeof(PS);
50-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
50+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
5151
sum += sizeof(PS2);
52-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
52+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
5353
sum += sizeof(&A10);
54-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
54+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression that results in a pointer
5555

5656
#ifdef __cplusplus
5757
MyStruct &rS = S;

0 commit comments

Comments
 (0)