llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.82k stars 11k forks source link

Missing qualifiers in derived to base implicit casts #23783

Open Abramo-Bagnara opened 9 years ago

Abramo-Bagnara commented 9 years ago
Bugzilla Link 23409
Version trunk
OS Linux
Attachments Candidate patch
CC @DougGregor,@zygoloid

Extended Description

The typescript below shows that the UncheckedDerivedToBase implicit cast destination type is not const qualified as it should.

I've attached a patch to fix that for review.

$ cat p.cc struct b { int x; };

struct d : b { };

void f() { const d p; int v = p->x; } $ clang++ -cc1 -ast-dump p.cc TranslationUnitDecl 0x1aff920 <> |-TypedefDecl 0x1affe60 <> implicit int128_t 'int128' |-TypedefDecl 0x1affec0 <> implicit uint128_t 'unsigned int128' |-TypedefDecl 0x1b00280 <> implicit builtin_va_list 'va_list_tag [1]' |-CXXRecordDecl 0x1b002d0 <p.cc:1:1, line:3:1> line:1:8 struct b definition | |-CXXRecordDecl 0x1b003e0 <col:1, col:8> col:8 implicit struct b | -FieldDecl 0x1b00480 <line:2:3, col:7> col:7 referenced x 'int' |-CXXRecordDecl 0x1b004d0 <line:5:1, line:6:1> line:5:8 struct d definition | |-public 'struct b' |-CXXRecordDecl 0x1b431f0 <col:1, col:8> col:8 implicit struct d -FunctionDecl 0x1b432d0 <line:8:1, line:11:1> line:8:6 f 'void (void)' -CompoundStmt 0x1b43578 <col:10, line:11:1> |-DeclStmt 0x1b43408 <line:9:3, col:13> | `-VarDecl 0x1b433b0 <col:3, col:12> col:12 used p 'const struct d ' -DeclStmt 0x1b43560 <line:10:3, col:15> -VarDecl 0x1b43430 <col:3, col:14> col:7 v 'int' cinit -ImplicitCastExpr 0x1b43548 <col:11, col:14> 'int' <LValueToRValue> -MemberExpr 0x1b43518 <col:11, col:14> 'const int' lvalue ->x 0x1b00480 -ImplicitCastExpr 0x1b434f8 <col:11> 'struct b *' <UncheckedDerivedToBase (b)> -ImplicitCastExpr 0x1b434b0 'const struct d ' `-DeclRefExpr 0x1b43488 'const struct d ' lvalue Var 0x1b433b0 'p' 'const struct d *'

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 9 years ago

Makes sense, but please send patches to cfe-commits.

Endilll commented 9 months ago

Still reproduces in post-17 trunk: https://godbolt.org/z/Kox8Tb39v

`-FunctionDecl <line:8:1, line:11:1> line:8:6 f 'void ()'
  `-CompoundStmt <col:10, line:11:1>
    |-DeclStmt <line:9:3, col:13>
    | `-VarDecl <col:3, col:12> col:12 used p 'const d *'
    `-DeclStmt <line:10:3, col:15>
      `-VarDecl <col:3, col:14> col:7 v 'int' cinit
        `-ImplicitCastExpr <col:11, col:14> 'int' <LValueToRValue>
          `-MemberExpr <col:11, col:14> 'const int' lvalue ->x 0xdb99ab0
            `-ImplicitCastExpr <col:11> 'b *' <UncheckedDerivedToBase (b)>
              `-ImplicitCastExpr <col:11> 'const d *' <LValueToRValue>
                `-DeclRefExpr <col:11> 'const d *' lvalue Var 0xdb99f40 'p' 'const d *'
llvmbot commented 9 months ago

@llvm/issue-subscribers-clang-frontend

| | | | --- | --- | | Bugzilla Link | [23409](https://llvm.org/bz23409) | | Version | trunk | | OS | Linux | | Attachments | [Candidate patch](https://user-images.githubusercontent.com/95090728/143751366-1913832e-225e-4065-99c1-745995384c9a.gz) | | CC | @DougGregor,@zygoloid | ## Extended Description The typescript below shows that the UncheckedDerivedToBase implicit cast destination type is not const qualified as it should. I've attached a patch to fix that for review. $ cat p.cc struct b { int x; }; struct d : b { }; void f() { const d* p; int v = p->x; } $ clang++ -cc1 -ast-dump p.cc TranslationUnitDecl 0x1aff920 <> |-TypedefDecl 0x1affe60 <> implicit __int128_t '__int128' |-TypedefDecl 0x1affec0 <> implicit __uint128_t 'unsigned __int128' |-TypedefDecl 0x1b00280 <> implicit __builtin_va_list '__va_list_tag [1]' |-CXXRecordDecl 0x1b002d0 line:1:8 struct b definition | |-CXXRecordDecl 0x1b003e0 col:8 implicit struct b | `-FieldDecl 0x1b00480 col:7 referenced x 'int' |-CXXRecordDecl 0x1b004d0 line:5:8 struct d definition | |-public 'struct b' | `-CXXRecordDecl 0x1b431f0 col:8 implicit struct d `-FunctionDecl 0x1b432d0 line:8:6 f 'void (void)' `-CompoundStmt 0x1b43578 |-DeclStmt 0x1b43408 | `-VarDecl 0x1b433b0 col:12 used p 'const struct d *' `-DeclStmt 0x1b43560 `-VarDecl 0x1b43430 col:7 v 'int' cinit `-ImplicitCastExpr 0x1b43548 'int' `-MemberExpr 0x1b43518 'const int' lvalue ->x 0x1b00480 `-ImplicitCastExpr 0x1b434f8 'struct b *' `-ImplicitCastExpr 0x1b434b0 'const struct d *' `-DeclRefExpr 0x1b43488 'const struct d *' lvalue Var 0x1b433b0 'p' 'const struct d *'