Skip to content

Commit dce0a67

Browse files
Merge pull request #613 from andreasfertig/fixIssue610
Fixed #610: Visualization of a `const_cast` from a C-style cast.
2 parents 77b102e + b192bc6 commit dce0a67

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

CodeGenerator.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,16 @@ static std::string AccessToStringWithColon(const AccessSpecifier& access)
9090

9191
using namespace asthelpers;
9292

93-
static std::string_view GetCastName(const CastKind castKind)
93+
static std::string_view GetCastName(const CastKind castKind, bool constnessChange = false)
9494
{
9595
if(is{castKind}.any_of(CastKind::CK_BitCast, CastKind::CK_IntegralToPointer, CastKind::CK_PointerToIntegral)) {
9696
return kwReinterpretCast;
9797
}
9898

99+
if((CastKind::CK_NoOp == castKind) and constnessChange) {
100+
return "const_cast"sv;
101+
}
102+
99103
return kwStaticCast;
100104
}
101105
//-----------------------------------------------------------------------------
@@ -2482,11 +2486,26 @@ void CodeGenerator::InsertArg(const ForStmt* stmt)
24822486
}
24832487
//-----------------------------------------------------------------------------
24842488

2489+
static bool IsConstQualifiedType(QualType type)
2490+
{
2491+
if(not type.isNull()) {
2492+
if(auto* typePtr = type.getTypePtrOrNull()) {
2493+
if(auto pointee = typePtr->getPointeeType(); not pointee.isNull()) {
2494+
return pointee.isConstQualified();
2495+
}
2496+
}
2497+
}
2498+
2499+
return false;
2500+
}
2501+
//-----------------------------------------------------------------------------
2502+
24852503
void CodeGenerator::InsertArg(const CStyleCastExpr* stmt)
24862504
{
24872505
const auto castKind = stmt->getCastKind();
2488-
const auto castName = GetCastName(castKind);
24892506
const QualType castDestType = stmt->getType().getCanonicalType();
2507+
const auto castName = GetCastName(
2508+
castKind, IsConstQualifiedType(castDestType) != IsConstQualifiedType(stmt->getSubExpr()->getType()));
24902509

24912510
FormatCast(castName, castDestType, stmt->getSubExpr(), castKind);
24922511
}

tests/Issue610.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
#include <limits.h>
3+
4+
int main (int argc, char *argv[]) {
5+
const int a = 10;
6+
int *p = (int *)&a;
7+
return 0;
8+
}

tests/Issue610.expect

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
#include <limits.h>
3+
4+
int main(int argc, char ** argv)
5+
{
6+
const int a = 10;
7+
int * p = const_cast<int *>(&a);
8+
return 0;
9+
}
10+

0 commit comments

Comments
 (0)