Commit f2e2a24
committed
Generate -Wunsafe-buffer-usage warnings in ctor and field initializers
Field initializers must be found by visiting FieldDecl and then running the warning checks/matchers against the in-class initializer, which is itself a Stmt. This catches warnings in places such as: struct C { P* Ptr; AnUnsafeCtor U{Ptr}; }; CXXCtorInitializers are not statements either, but they point to an initializer expression which is. When visiting a FunctionDecl, also walk through any constructor initializers and run the warning checks/matchers against their initializer expressions. This catches warnings for initializing fields and calling other constructors, such as: struct C { C(P* Ptr) : AnUnsafeCtor(Ptr) {} } We add tests for explicit construction, for field initialization, base class constructor calls, delegated constructor calls, and aggregate initialization. Note that aggregate initialization through `()` is treated differently today by the AST matchers than `{}`. The former is not considered as calling an implicit constructor, while the latter is. MatchDescendantVisitor::shouldVisitImplicitCode() returns false with a TODO, which means we do not catch warnings of the form: struct AggregateInitType { AnUnsafeCtor U; } AggregateInitType{Ptr}; But we do already catch them when written as (in C++20): struct AggregateInitType { AnUnsafeCtor U; } AggregateInitType(Ptr); Returning true from MatchDescendantVisitor::shouldVisitImplicitCode(), however, breaks expectations for field in-class initializers by moving the SourceLocation, possibly to inside the implicit ctor instead of on the line where the field initialization happens. struct C { P* Ptr; AnUnsafeCtor U{Ptr}; // expected-warning{{this is never seen then}} };1 parent 84da0fe commit f2e2a24
File tree
4 files changed
+155
-38
lines changed- clang
- lib
- Analysis
- Sema
- test/SemaCXX
4 files changed
+155
-38
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1387 | 1387 | | |
1388 | 1388 | | |
1389 | 1389 | | |
1390 | | - | |
1391 | | - | |
| 1390 | + | |
| 1391 | + | |
1392 | 1392 | | |
1393 | 1393 | | |
1394 | 1394 | | |
| |||
1483 | 1483 | | |
1484 | 1484 | | |
1485 | 1485 | | |
1486 | | - | |
| 1486 | + | |
1487 | 1487 | | |
1488 | 1488 | | |
1489 | 1489 | | |
| |||
3030 | 3030 | | |
3031 | 3031 | | |
3032 | 3032 | | |
3033 | | - | |
3034 | | - | |
3035 | | - | |
3036 | | - | |
3037 | | - | |
3038 | | - | |
3039 | | - | |
3040 | | - | |
3041 | | - | |
3042 | | - | |
3043 | | - | |
3044 | | - | |
3045 | | - | |
3046 | | - | |
3047 | | - | |
3048 | | - | |
3049 | | - | |
3050 | | - | |
3051 | | - | |
3052 | | - | |
3053 | | - | |
3054 | | - | |
3055 | | - | |
3056 | | - | |
3057 | | - | |
3058 | | - | |
3059 | | - | |
3060 | | - | |
3061 | | - | |
3062 | | - | |
3063 | | - | |
3064 | | - | |
3065 | | - | |
| 3033 | + | |
| 3034 | + | |
| 3035 | + | |
3066 | 3036 | | |
3067 | 3037 | | |
3068 | 3038 | | |
| |||
3106 | 3076 | | |
3107 | 3077 | | |
3108 | 3078 | | |
3109 | | - | |
3110 | | - | |
| 3079 | + | |
| 3080 | + | |
| 3081 | + | |
| 3082 | + | |
3111 | 3083 | | |
3112 | 3084 | | |
3113 | 3085 | | |
| |||
3328 | 3300 | | |
3329 | 3301 | | |
3330 | 3302 | | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
| 3314 | + | |
| 3315 | + | |
| 3316 | + | |
| 3317 | + | |
| 3318 | + | |
| 3319 | + | |
| 3320 | + | |
| 3321 | + | |
| 3322 | + | |
| 3323 | + | |
| 3324 | + | |
| 3325 | + | |
| 3326 | + | |
| 3327 | + | |
| 3328 | + | |
| 3329 | + | |
| 3330 | + | |
| 3331 | + | |
| 3332 | + | |
| 3333 | + | |
| 3334 | + | |
| 3335 | + | |
| 3336 | + | |
| 3337 | + | |
| 3338 | + | |
| 3339 | + | |
| 3340 | + | |
| 3341 | + | |
| 3342 | + | |
| 3343 | + | |
| 3344 | + | |
| 3345 | + | |
| 3346 | + | |
| 3347 | + | |
| 3348 | + | |
| 3349 | + | |
| 3350 | + | |
| 3351 | + | |
| 3352 | + | |
| 3353 | + | |
| 3354 | + | |
| 3355 | + | |
| 3356 | + | |
| 3357 | + | |
| 3358 | + | |
| 3359 | + | |
| 3360 | + | |
| 3361 | + | |
| 3362 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2468 | 2468 | | |
2469 | 2469 | | |
2470 | 2470 | | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
2471 | 2479 | | |
2472 | 2480 | | |
2473 | 2481 | | |
| |||
Lines changed: 67 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 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 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
114 | 150 | | |
115 | 151 | | |
116 | 152 | | |
| |||
122 | 158 | | |
123 | 159 | | |
124 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
125 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
0 commit comments