Skip to content

Commit cc0a010

Browse files
authored
Merge pull request llvm#599 from AMD-Lightning-Internal/upstream_merge_202502110103
merge main into amd-staging
2 parents 52ae76b + f8ef723 commit cc0a010

File tree

30 files changed

+520
-174
lines changed

30 files changed

+520
-174
lines changed

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
8282
Finder->addMatcher(
8383
cxxConstructExpr(
8484
hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
85-
hasArgument(0, hasType(qualType(isInteger()))),
85+
argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger()))),
8686
hasArgument(1, hasType(qualType(isInteger()))),
8787
anyOf(
8888
// Detect the expression: string('x', 40);
@@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
102102
cxxConstructExpr(
103103
hasDeclaration(cxxConstructorDecl(ofClass(
104104
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
105-
hasArgument(0, hasType(CharPtrType)),
105+
argumentCountIs(2), hasArgument(0, hasType(CharPtrType)),
106106
hasArgument(1, hasType(isInteger())),
107107
anyOf(
108108
// Detect the expression: string("...", 0);
@@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
114114
// Detect the expression: string("lit", 5)
115115
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
116116
hasArgument(1, ignoringParenImpCasts(
117-
integerLiteral().bind("int"))))))
117+
integerLiteral().bind("length"))))))
118+
.bind("constructor"),
119+
this);
120+
121+
// Check the literal string constructor with char pointer, start position and
122+
// length parameters. [i.e. string (const char* s, size_t pos, size_t count);]
123+
Finder->addMatcher(
124+
cxxConstructExpr(
125+
hasDeclaration(cxxConstructorDecl(ofClass(
126+
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
127+
argumentCountIs(3), hasArgument(0, hasType(CharPtrType)),
128+
hasArgument(1, hasType(qualType(isInteger()))),
129+
hasArgument(2, hasType(qualType(isInteger()))),
130+
anyOf(
131+
// Detect the expression: string("...", 1, 0);
132+
hasArgument(2, ZeroExpr.bind("empty-string")),
133+
// Detect the expression: string("...", -4, 1);
134+
hasArgument(1, NegativeExpr.bind("negative-pos")),
135+
// Detect the expression: string("...", 0, -4);
136+
hasArgument(2, NegativeExpr.bind("negative-length")),
137+
// Detect the expression: string("lit", 0, 0x1234567);
138+
hasArgument(2, LargeLengthExpr.bind("large-length")),
139+
// Detect the expression: string("lit", 1, 5)
140+
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
141+
hasArgument(
142+
1, ignoringParenImpCasts(integerLiteral().bind("pos"))),
143+
hasArgument(2, ignoringParenImpCasts(
144+
integerLiteral().bind("length"))))))
118145
.bind("constructor"),
119146
this);
120147

@@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
155182
diag(Loc, "constructor creating an empty string");
156183
} else if (Result.Nodes.getNodeAs<Expr>("negative-length")) {
157184
diag(Loc, "negative value used as length parameter");
185+
} else if (Result.Nodes.getNodeAs<Expr>("negative-pos")) {
186+
diag(Loc, "negative value used as position of the "
187+
"first character parameter");
158188
} else if (Result.Nodes.getNodeAs<Expr>("large-length")) {
159189
if (WarnOnLargeLength)
160190
diag(Loc, "suspicious large length parameter");
161191
} else if (Result.Nodes.getNodeAs<Expr>("literal-with-length")) {
162192
const auto *Str = Result.Nodes.getNodeAs<StringLiteral>("str");
163-
const auto *Lit = Result.Nodes.getNodeAs<IntegerLiteral>("int");
164-
if (Lit->getValue().ugt(Str->getLength())) {
193+
const auto *Length = Result.Nodes.getNodeAs<IntegerLiteral>("length");
194+
if (Length->getValue().ugt(Str->getLength())) {
165195
diag(Loc, "length is bigger than string literal size");
196+
return;
197+
}
198+
if (const auto *Pos = Result.Nodes.getNodeAs<IntegerLiteral>("pos")) {
199+
if (Pos->getValue().uge(Str->getLength())) {
200+
diag(Loc, "position of the first character parameter is bigger than "
201+
"string literal character range");
202+
} else if (Length->getValue().ugt(
203+
(Str->getLength() - Pos->getValue()).getZExtValue())) {
204+
diag(Loc, "length is bigger than remaining string literal size");
205+
}
166206
}
167207
} else if (const auto *Ptr = Result.Nodes.getNodeAs<Expr>("from-ptr")) {
168208
Expr::EvalResult ConstPtr;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ New check aliases
9797
Changes in existing checks
9898
^^^^^^^^^^^^^^^^^^^^^^^^^^
9999

100+
- Improved :doc:`bugprone-string-constructor
101+
<clang-tidy/checks/bugprone/string-constructor>` check to find suspicious
102+
calls of ``std::string`` constructor with char pointer, start position and
103+
length parameters.
104+
100105
- Improved :doc:`bugprone-unsafe-functions
101106
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
102107
additional C++ member functions to match.

clang-tools-extra/docs/clang-tidy/checks/bugprone/string-constructor.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Examples:
2121
.. code-block:: c++
2222

2323
std::string("test", 200); // Will include random characters after "test".
24+
std::string("test", 2, 5); // Will include random characters after "st".
2425
std::string_view("test", 200);
2526

2627
Creating an empty string from constructors with parameters is considered
@@ -31,8 +32,19 @@ Examples:
3132
.. code-block:: c++
3233

3334
std::string("test", 0); // Creation of an empty string.
35+
std::string("test", 1, 0);
3436
std::string_view("test", 0);
3537

38+
Passing an invalid first character position parameter to constructor will
39+
cause ``std::out_of_range`` exception at runtime.
40+
41+
Examples:
42+
43+
.. code-block:: c++
44+
45+
std::string("test", -1, 10); // Negative first character position.
46+
std::string("test", 10, 10); // First character position is bigger than string literal character range".
47+
3648
Options
3749
-------
3850

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct basic_string {
1111
basic_string(const C*, unsigned int size);
1212
basic_string(const C *, const A &allocator = A());
1313
basic_string(unsigned int size, C c);
14+
basic_string(const C*, unsigned int pos, unsigned int size);
1415
};
1516
typedef basic_string<char> string;
1617
typedef basic_string<wchar_t> wstring;
@@ -61,6 +62,21 @@ void Test() {
6162
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructing string from nullptr is undefined behaviour
6263
std::string q7 = 0;
6364
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
65+
66+
std::string r1("test", 1, 0);
67+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
68+
std::string r2("test", 0, -4);
69+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
70+
std::string r3("test", -4, 1);
71+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as position of the first character parameter
72+
std::string r4("test", 0, 0x1000000);
73+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
74+
std::string r5("test", 0, 5);
75+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
76+
std::string r6("test", 3, 2);
77+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
78+
std::string r7("test", 4, 1);
79+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: position of the first character parameter is bigger than string literal character range
6480
}
6581

6682
void TestView() {
@@ -82,6 +98,17 @@ void TestView() {
8298
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
8399
}
84100

101+
void TestUnsignedArguments() {
102+
std::string s0("test", 0u);
103+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
104+
std::string s1(0x1000000ull, 'x');
105+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
106+
std::string s2("test", 3ull, 2u);
107+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
108+
std::string s3("test", 0u, 5ll);
109+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
110+
}
111+
85112
std::string StringFromZero() {
86113
return 0;
87114
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
@@ -101,6 +128,9 @@ void Valid() {
101128
std::string s3("test");
102129
std::string s4("test\000", 5);
103130
std::string s6("te" "st", 4);
131+
std::string s7("test", 0, 4);
132+
std::string s8("test", 3, 1);
133+
std::string s9("te" "st", 1, 2);
104134

105135
std::string_view emptyv();
106136
std::string_view sv1("test", 4);

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Attribute Changes in Clang
117117
--------------------------
118118

119119
- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.
120+
- Clang now diagnoses use of declaration attributes on void parameters. (#GH108819)
120121

121122
Improvements to Clang's diagnostics
122123
-----------------------------------

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,9 @@ def warn_type_attribute_wrong_type : Warning<
38323832
"'%0' only applies to %select{function|pointer|"
38333833
"Objective-C object or block pointer}1 types; type here is %2">,
38343834
InGroup<IgnoredAttributes>;
3835+
def warn_attribute_on_void_param: Warning<
3836+
"attribute %0 cannot be applied to a 'void' parameter">,
3837+
InGroup<IgnoredAttributes>;
38353838
def err_type_attribute_wrong_type : Error<
38363839
warn_type_attribute_wrong_type.Summary>;
38373840
def warn_incomplete_encoded_type : Warning<

clang/lib/Sema/SemaAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,11 @@ void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
11891189
void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
11901190
if (PragmaAttributeStack.empty())
11911191
return;
1192+
1193+
if (const auto *P = dyn_cast<ParmVarDecl>(D))
1194+
if (P->getType()->isVoidType())
1195+
return;
1196+
11921197
for (auto &Group : PragmaAttributeStack) {
11931198
for (auto &Entry : Group.Entries) {
11941199
ParsedAttr *Attribute = Entry.Attribute;

clang/lib/Sema/SemaType.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5183,6 +5183,11 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
51835183
if (ParamTy.hasQualifiers())
51845184
S.Diag(DeclType.Loc, diag::err_void_param_qualified);
51855185

5186+
for (const auto *A : Param->attrs()) {
5187+
S.Diag(A->getLoc(), diag::warn_attribute_on_void_param)
5188+
<< A << A->getRange();
5189+
}
5190+
51865191
// Reject, but continue to parse 'float(this void)' as
51875192
// 'float(void)'.
51885193
if (Param->isExplicitObjectParameter()) {

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ class TrivialFunctionAnalysisVisitor
636636
return true;
637637
}
638638

639+
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *IVIE) {
640+
// An implicit value initialization is trvial.
641+
return true;
642+
}
643+
639644
private:
640645
CacheTy &Cache;
641646
CacheTy RecursiveFn;

clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ class RefCounted {
368368
}
369369
RefPtr<RefCounted> trivial66() { return children[0]; }
370370
Ref<RefCounted> trivial67() { return *children[0]; }
371+
struct point {
372+
double x;
373+
double y;
374+
};
375+
void trivial68() { point pt = { 1.0 }; }
371376

372377
static RefCounted& singleton() {
373378
static RefCounted s_RefCounted;
@@ -554,6 +559,7 @@ class UnrelatedClass {
554559
getFieldTrivial().trivial65(); // no-warning
555560
getFieldTrivial().trivial66()->trivial6(); // no-warning
556561
getFieldTrivial().trivial67()->trivial6(); // no-warning
562+
getFieldTrivial().trivial68(); // no-warning
557563

558564
RefCounted::singleton().trivial18(); // no-warning
559565
RefCounted::singleton().someFunction(); // no-warning

0 commit comments

Comments
 (0)