Skip to content

Commit

Permalink
Fixed #313: Show CK_NoOp correctly.
Browse files Browse the repository at this point in the history
`CK_NoOp` stands for adding `const` or other qualifiers. Show this
conversion, if `--show-all-implicit-casts` is active.

This patch enables showing implicit casts for member-function calls
as well.
  • Loading branch information
andreasfertig committed May 23, 2020
1 parent 44d4c44 commit bea5681
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 9 deletions.
28 changes: 20 additions & 8 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,16 +1382,16 @@ void CodeGenerator::InsertArg(const ImplicitCastExpr* stmt)
case CastKind::CK_FloatingCast: [[fallthrough]];
case CastKind::CK_IntegralToFloating: [[fallthrough]];
case CastKind::CK_FloatingToIntegral: [[fallthrough]];
/* these are implicit conversions. We get them right, but they may end up in a compiler internal type,
* which leads to compiler errors */
// case CastKind::CK_NoOp:
case CastKind::CK_NonAtomicToAtomic: return true;
default:
// Show this casts only if ShowAllImplicitCasts is turned on.
if(not hideImplicitCasts) {
switch(kind) {
case CastKind::CK_NullToPointer: [[fallthrough]];
case CastKind::CK_NullToMemberPointer: [[fallthrough]];
/* these are implicit conversions. We get them right, but they may end up in a compiler internal
* type, which leads to compiler errors */
case CastKind::CK_NoOp: [[fallthrough]];
case CastKind::CK_ArrayToPointerDecay: return true;
default: break;
}
Expand All @@ -1406,9 +1406,9 @@ void CodeGenerator::InsertArg(const ImplicitCastExpr* stmt)
} else if(isa<IntegerLiteral>(subExpr) && hideImplicitCasts) {
InsertArg(stmt->IgnoreCasts());

// If this is part of an explicit cast, for example a CStyleCast ignore it, if ShowAllImplicitCasts is not
// selected
} else if(stmt->isPartOfExplicitCast() && hideImplicitCasts) {
// If this is part of an explicit cast, for example a CStyleCast or static_cast, ignore it, because it belongs
// to the cast written by the user.
} else if(stmt->isPartOfExplicitCast()) {
InsertArg(stmt->IgnoreCasts());

} else {
Expand Down Expand Up @@ -1699,8 +1699,20 @@ void CodeGenerator::InsertArg(const CXXOperatorCallExpr* stmt)
const bool isCXXMethod{callee && isa<CXXMethodDecl>(callee->getDecl())};

if(2 == stmt->getNumArgs()) {
const auto* param1 = dyn_cast_or_null<DeclRefExpr>(stmt->getArg(0)->IgnoreImpCasts());
const auto* param2 = dyn_cast_or_null<DeclRefExpr>(stmt->getArg(1)->IgnoreImpCasts());
auto getArg = [&](unsigned idx) {
const auto* arg = stmt->getArg(idx);

// In show all casts mode don't filter this. It shows how the compiler adds const to arguments, if the
// argument is non-const but the parameter demands a const object
if(not GetInsightsOptions().ShowAllImplicitCasts) {
arg = arg->IgnoreImpCasts();
}

return dyn_cast_or_null<DeclRefExpr>(arg);
};

const auto* param1 = getArg(0);
const auto* param2 = getArg(1);

if(callee && param1 && param2) {

Expand Down
2 changes: 1 addition & 1 deletion tests/Issue223_2.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

int main()
{
float f = static_cast<float>(static_cast<float>((3.0)));
float f = static_cast<float>((3.0));
return static_cast<int>(f);
}

13 changes: 13 additions & 0 deletions tests/Issue313.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// cmdlineinsights:--show-all-implicit-casts

struct A {};
struct B {
explicit operator A() { return {}; }
};

void DangerousFunc(const A&) {}

int main()
{
DangerousFunc(static_cast<A>(B{}));
}
29 changes: 29 additions & 0 deletions tests/Issue313.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// cmdlineinsights:--show-all-implicit-casts

struct A
{
};


struct B
{
using retType_5_4 = A;
inline operator retType_5_4 ()
{
return {};
}

};



void DangerousFunc(const A &)
{
}


int main()
{
DangerousFunc(static_cast<const A>(static_cast<A>(B{}.operator A())));
}

14 changes: 14 additions & 0 deletions tests/Issue313_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// cmdlineinsights:--show-all-implicit-casts
struct A
{
bool operator==(const A&) const { return true; }
bool operator!=(const A&) { return true; }
};

int main()
{
A a{};

if(a == a) {} // a gets constified as object and parameter
if(a != a) {} // only the parameter gets constified, as != is not const
}
28 changes: 28 additions & 0 deletions tests/Issue313_2.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// cmdlineinsights:--show-all-implicit-casts
struct A
{
inline bool operator==(const A &) const
{
return true;
}

inline bool operator!=(const A &)
{
return true;
}

};



int main()
{
A a = {};
if(static_cast<const A>(a).operator==(static_cast<const A>(a))) {
}

if(a.operator!=(static_cast<const A>(a))) {
}

}

0 comments on commit bea5681

Please sign in to comment.