const SourceLocation End = S.PP.getLocForEndOfToken(Arg->getSourceRange()
.getEnd());
bool NeedParen = true;
- if (isa<ParenExpr>(Arg) || isa<DeclRefExpr>(Arg))
+ if (isa<ParenExpr>(Arg) ||
+ isa<DeclRefExpr>(Arg) ||
+ isa<ArraySubscriptExpr>(Arg) ||
+ isa<CallExpr>(Arg) ||
+ isa<MemberExpr>(Arg))
NeedParen = false;
+ if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Arg))
+ if (UO->isPostfix())
+ NeedParen = false;
// Check if the argument needs to be dereferenced
// (type * -> type) or (type * -> type &).
ImplicitConversionSequence ICS =
TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false);
+ OverloadFixItKind FixKind = OFIK_Dereference;
if (!ICS.isBad()) {
// Do not suggest dereferencing a Null pointer.
if (Arg->IgnoreParenCasts()->
isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
return false;
- if (NeedParen) {
+ if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Arg)) {
+ if (UO->getOpcode() == UO_AddrOf) {
+ ConvFix.Hints.push_back(
+ FixItHint::CreateRemoval(Arg->getSourceRange()));
+ FixKind = OFIK_RemoveTakeAddress;
+ }
+ } else if (NeedParen) {
ConvFix.Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));
ConvFix.Hints.push_back(FixItHint::CreateInsertion(End, ")"));
} else {
}
ConvFix.NumConversionsFixed++;
if (ConvFix.NumConversionsFixed == 1)
- ConvFix.Kind = OFIK_Dereference;
+ ConvFix.Kind = FixKind;
return true;
}
}
// (type -> type *) or (type & -> type *).
if (isa<PointerType>(ToQTy)) {
// Only suggest taking address of L-values.
- if (!Arg->isLValue())
+ if (!Arg->isLValue() || Arg->getObjectKind() != OK_Ordinary)
return false;
OpaqueValueExpr TmpExpr(Arg->getExprLoc(),
S.Context.getPointerType(FromQTy), VK_RValue);
ImplicitConversionSequence ICS =
TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false);
+
+ OverloadFixItKind FixKind = OFIK_TakeAddress;
if (!ICS.isBad()) {
- if (NeedParen) {
+
+ if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Arg)) {
+ if (UO->getOpcode() == UO_Deref) {
+ ConvFix.Hints.push_back(
+ FixItHint::CreateRemoval(Arg->getSourceRange()));
+ FixKind = OFIK_RemoveDereference;
+ }
+ } else if (NeedParen) {
ConvFix.Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
ConvFix.Hints.push_back(FixItHint::CreateInsertion(End, ")"));
} else {
}
ConvFix.NumConversionsFixed++;
if (ConvFix.NumConversionsFixed == 1)
- ConvFix.Kind = OFIK_TakeAddress;
+ ConvFix.Kind = FixKind;
return true;
}
}
<< (unsigned) (Cand->Fix.Kind);
// If we can fix the conversion, suggest the FixIts.
- for (llvm::SmallVector<FixItHint, 4>::iterator
+ for (llvm::SmallVector<FixItHint, 1>::iterator
HI = Cand->Fix.Hints.begin(), HE = Cand->Fix.Hints.end();
HI != HE; ++HI)
FDiag << *HI;
unsigned numRFixes = R->Fix.NumConversionsFixed;
numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes;
numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes;
- if (numLFixes != numRFixes)
+ if (numLFixes != numRFixes) {
if (numLFixes < numRFixes)
return true;
else
return false;
+ }
// If there's any ordering between the defined conversions...
// FIXME: this might not be transitive.
// This call cannot be fixed since without resulting in null pointer dereference.
// CHECK: error: no matching function for call to 'f1
-// CHECK-NOT: take the address of the argument with &
+// CHECK-NOT: dereference the argument with *
// CHECK-NOT: fix-it
f1((int *)0);
}
double y;
};
+class C : A {};
+
bool br(A &a);
bool bp(A *a);
bool dv(B b);
-void dbcaller(A *ptra, B *ptrb) {
+void dbcaller(A *ptra, B *ptrb, C &c) {
B b;
// CHECK: error: no matching function for call to 'br
// CHECK: fix-it{{.*}}*
br(ptrb); // good
+
// CHECK: error: no matching function for call to 'bp
// CHECK: fix-it{{.*}}&
bp(b); // good
// CHECK: error: no matching function for call to 'dv
// CHECK-NOT: fix-it
dv(ptra); // bad: base to derived
+
+// CHECK: error: no matching function for call to 'dv
+// CHECK: remove &
+ dv(&b);
+
+// CHECK: error: no matching function for call to 'bp
+// CHECK: remove *
+ bp(*ptra);
+
+// TODO: Test that we do not provide a fixit when inheritance is private.
+// CHECK: error: no matching function for call to 'bp
+// There should not be a fixit here:
+// CHECK: fix-it
+ bp(c);
}
// CHECK: errors generated