Skip to content

Commit 0fd38a3

Browse files
authored
Don't add implicit cast constraints to void* arguments (#676)
* avoid adding extra constraint when casting arguments to void * add test * more precise test name
1 parent e1897b5 commit 0fd38a3

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

clang/lib/3C/CastPlacement.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
6666

6767
CVarSet ArgConstraints = CR.getExprConstraintVarsSet(ArgExpr);
6868
for (auto *ArgC : ArgConstraints) {
69+
// If the function takes a void *, we already know about the wildness,
70+
// so allow the implicit cast.
71+
if (TypeVar == nullptr && FV->getExternalParam(PIdx)->isVoidPtr())
72+
continue;
6973
CastNeeded CastKind = needCasting(
7074
ArgC, ArgC, FV->getInternalParam(PIdx), FV->getExternalParam(PIdx));
7175
if (CastKind != NO_CAST) {

clang/lib/3C/ConstraintBuilder.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,11 @@ class FunctionVisitor : public RecursiveASTVisitor<FunctionVisitor> {
239239
for (const auto &A : E->arguments()) {
240240
CSetBkeyPair ArgumentConstraints;
241241
if (I < TargetFV->numParams()) {
242-
// Remove casts to void* on polymorphic types that are used
243-
// consistently.
244-
const int TyIdx =
245-
TargetFV->getExternalParam(I)->getGenericIndex();
246-
if (ConsistentTypeParams.find(TyIdx) !=
247-
ConsistentTypeParams.end())
242+
// When the function has a void* parameter, Clang will
243+
// add an implicit cast to void* here. Generating constraints
244+
// will add an extraneous wild constraint to void*. This
245+
// unnecessarily complicates results and root causes.
246+
if (TargetFV->getExternalParam(I)->isVoidPtr())
248247
ArgumentConstraints =
249248
CB.getExprConstraintVars(A->IgnoreImpCasts());
250249
else
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: 3c -base-dir=%S -alltypes -warn-root-cause %s -- -Xclang -verify -Wno-everything
2+
3+
// This test checks that void* casts of arguments are ignored by the root
4+
// cause code (and handled by the function parameter causes), but other casts
5+
// are still noted.
6+
7+
void has_void(void* v); // expected-warning {{3 unchecked pointers: Default void* type}}
8+
void test_no_cause() {
9+
int *b, *c;
10+
has_void(b); // specifically no warning here, since it shows up above
11+
has_void(c);
12+
}
13+
14+
void has_float(float* v); // expected-warning {{Unchecked pointer in parameter or return of external function has_float}}
15+
void test_float_cause() {
16+
int *b, *c;
17+
has_float(b); // expected-warning {{1 unchecked pointer: Cast from int * to float *}}
18+
has_float(c); // expected-warning {{1 unchecked pointer: Cast from int * to float *}}
19+
}
20+
21+
void has_body(float* v){}
22+
void test_only_args_cause() {
23+
int *b, *c;
24+
has_body(b); // expected-warning {{1 unchecked pointer: Cast from int * to float *}}
25+
has_body(c); // expected-warning {{1 unchecked pointer: Cast from int * to float *}}
26+
}

0 commit comments

Comments
 (0)