Skip to content

Commit

Permalink
Fixed #223 and #134: Suppress implicit casts which are part of a expl…
Browse files Browse the repository at this point in the history
…icit cast.

Both issues raised that there is a second `static_cast` after a C-style
cast. It turns out, that an `ImplicitCastExpr` has a method
`isPartOfExplicitCast`. This method together which the
`-show-all-implicit-casts` option is used to suppress the second cast
per default.
  • Loading branch information
andreasfertig committed Sep 28, 2019
1 parent 43d7990 commit 93ffd3f
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,10 +1129,16 @@ void CodeGenerator::InsertArg(const ImplicitCastExpr* stmt)
{
const Expr* subExpr = stmt->getSubExpr();
const auto castKind = stmt->getCastKind();
const bool hideImplicitCasts{not GetInsightsOptions().ShowAllImplicitCasts};

if(!clang::ast_matchers::IsMatchingCast(castKind)) {
InsertArg(subExpr);
} else if(isa<IntegerLiteral>(subExpr) && not GetInsightsOptions().ShowAllImplicitCasts) {
} 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) {
InsertArg(stmt->IgnoreCasts());

} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/CharLiteral2Test.expect
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
int main()
{
if(1 == static_cast<int>((static_cast<unsigned char>(static_cast<unsigned char>(255))))) {
if(1 == static_cast<int>((static_cast<unsigned char>(255)))) {
}

}
Expand Down
6 changes: 3 additions & 3 deletions tests/ClassOperatorHandler5Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ std::basic_string<char> trim(const std::basic_string<char> & input)

std::string final = std::basic_string<char>(input);
int i = 0;
while(i < static_cast<int>(static_cast<int>(input.length())) && static_cast<int>(input.operator[](i)) <= static_cast<int>(' ')) {
while(i < static_cast<int>(input.length()) && static_cast<int>(input.operator[](i)) <= static_cast<int>(' ')) {
i++;
}

if(i >= static_cast<int>(static_cast<int>(input.length()))) {
if(i >= static_cast<int>(input.length())) {
return std::basic_string<char>("");
}

if(i > 0) {
final.operator=(input.substr(static_cast<unsigned long>(i), input.length() - static_cast<unsigned long>(i)));
}

i = static_cast<int>(static_cast<int>(final.length())) - 1;
i = static_cast<int>(final.length()) - 1;
while(i >= 0 && static_cast<int>(final.operator[](i)) <= static_cast<int>(' ')) {
i--;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ClassOperatorHandler6Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main()
const bool b4 = f1.operator==(NULL);
const bool b5 = f1.operator==(static_cast<const int>((t == nullptr)));
unsigned long l = 2;
const bool b6 = f1.operator==(static_cast<int>(static_cast<int>(l)));
const bool b6 = f1.operator==(static_cast<int>(l));
const bool b7 = f1.operator==(dynamic_cast<Bar *>(&f2));
const bool b8 = f1.operator==(reinterpret_cast<char *>(t));
const bool b9 = f1.operator==(new char[2]);
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionalCastTest.expect
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
int main()
{
int x;
x = int(static_cast<int>(0.5));
x = int(0.5);
}

2 changes: 1 addition & 1 deletion tests/ImplicitCast2Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ int main()
FloatingCast(static_cast<double>(f));
int i = 1;
IntegralToBoolean(static_cast<bool>(i));
int ii = int(static_cast<int>(A().operator int()));
int ii = int(A().operator int());
}

7 changes: 7 additions & 0 deletions tests/Issue223.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <cstdio>

int main()
{
float f = (float)(3.0);
return f;
}
8 changes: 8 additions & 0 deletions tests/Issue223_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// cmdlineinsights:-show-all-implicit-casts
#include <cstdio>

int main()
{
float f = (float)(3.0);
return f;
}
9 changes: 9 additions & 0 deletions tests/Issue223_2.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// cmdlineinsights:-show-all-implicit-casts
#include <cstdio>

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

2 changes: 1 addition & 1 deletion tests/VarTemplateDecl3Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ template<class T>
constexpr T pi = T(3.1415926535897932385L);

template<>
constexpr const int pi<int> = int(static_cast<int>(3.14159265358979323851L));
constexpr const int pi<int> = int(3.14159265358979323851L);


template<class T>
Expand Down
2 changes: 1 addition & 1 deletion tests/VarTemplateWithLambdaTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class __lambda_5_23
template<>
inline /*constexpr */ double operator()(int x) const
{
return double(static_cast<double>(x));
return double(x);
}
#endif

Expand Down

0 comments on commit 93ffd3f

Please sign in to comment.