warn-cast-qual.cpp
6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
#include <stdint.h>
// do *NOT* warn on const_cast<>()
// use clang-tidy's cppcoreguidelines-pro-type-const-cast for that.
void foo_ptr() {
const char *const ptr = 0;
char *t0 = const_cast<char *>(ptr); // no warning
volatile char *ptr2 = 0;
char *t1 = const_cast<char *>(ptr2); // no warning
const volatile char *ptr3 = 0;
char *t2 = const_cast<char *>(ptr3); // no warning
}
void cstr() {
void* p0 = (void*)(const void*)"txt"; // expected-warning {{cast from 'const void *' to 'void *' drops const qualifier}}
void* p1 = (void*)"txt"; // FIXME
char* p2 = (char*)"txt"; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
}
void foo_0() {
const int a = 0;
const int &a0 = a; // no warning
const int &a1 = (const int &)a; // no warning
int &a2 = (int &)a; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
const int &a3 = (int &)a; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
int &a4 = (int &)((const int &)a); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
int &a5 = (int &)((int &)a); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
const int &a6 = (int &)((int &)a); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
const int &a7 = (int &)((const int &)a); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
const int &a8 = (const int &)((int &)a); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
}
void foo_1() {
volatile int a = 0;
volatile int &a0 = a; // no warning
volatile int &a1 = (volatile int &)a; // no warning
int &a2 = (int &)a; // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
volatile int &a3 = (int &)a; // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
int &a4 = (int &)((volatile int &)a); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
int &a5 = (int &)((int &)a); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
volatile int &a6 = (int &)((int &)a); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
volatile int &a7 = (int &)((volatile int &)a); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
volatile int &a8 = (volatile int &)((int &)a); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
}
void foo_2() {
const volatile int a = 0;
const volatile int &a0 = a; // no warning
const volatile int &a1 = (const volatile int &)a; // no warning
int &a2 = (int &)a; // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
const volatile int &a3 = (int &)a; // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
int &a4 = (int &)((const volatile int &)a); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
int &a5 = (int &)((int &)a); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
const volatile int &a6 = (int &)((int &)a); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
const volatile int &a7 = (int &)((const volatile int &)a); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
const volatile int &a8 = (const volatile int &)((int &)a); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
}
void bar_0() {
const int *_a = 0;
const int **a = &_a;
int **a0 = (int **)((const int **)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
int **a1 = (int **)((int **)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
// const int **a2 = (int **)((int **)a);
// const int **a3 = (int **)((const int **)a);
const int **a4 = (const int **)((int **)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}} expected-warning {{cast from 'int **' to 'const int **' must have all intermediate pointers const qualified to be safe}}
const int **a5 = (const int **)((const int **)a); // no warning
}
void bar_1() {
const int *_a = 0;
const int *&a = _a;
int *&a0 = (int *&)((const int *&)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
int *&a1 = (int *&)((int *&)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
// const int *&a2 = (int *&)((int *&)a);
// const int *&a3 = (int *&)((const int *&)a);
const int *&a4 = (const int *&)((int *&)a); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}} expected-warning {{cast from 'int *' to 'const int *&' must have all intermediate pointers const qualified to be safe}}
const int *&a5 = (const int *&)((const int *&)a); // no warning
}
void baz_0() {
struct C {
void A() {}
void B() const {}
};
const C S;
S.B();
((C &)S).B(); // expected-warning {{cast from 'const C' to 'C &' drops const qualifier}}
((C &)S).A(); // expected-warning {{cast from 'const C' to 'C &' drops const qualifier}}
((C *)&S)->B(); // expected-warning {{cast from 'const C *' to 'C *' drops const qualifier}}
((C *)&S)->A(); // expected-warning {{cast from 'const C *' to 'C *' drops const qualifier}}
}
void baz_1() {
struct C {
const int a;
int b;
C() : a(0) {}
};
{
C S;
S.b = 0;
(int &)(S.a) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
(int &)(S.b) = 0; // no warning
*(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(int *)(&S.b) = 0; // no warning
}
{
const C S;
(int &)(S.a) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
(int &)(S.b) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
*(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(int *)(&S.b) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
}
}