1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | #ifndef LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H |
17 | #define LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H |
18 | |
19 | #include "clang/Analysis/Support/BumpVector.h" |
20 | #include "clang/AST/ExprCXX.h" |
21 | #include "clang/AST/ExprObjC.h" |
22 | |
23 | namespace clang { |
24 | |
25 | |
26 | |
27 | |
28 | class ConstructionContextItem { |
29 | public: |
30 | enum ItemKind { |
31 | VariableKind, |
32 | NewAllocatorKind, |
33 | ReturnKind, |
34 | MaterializationKind, |
35 | TemporaryDestructorKind, |
36 | ElidedDestructorKind, |
37 | ElidableConstructorKind, |
38 | ArgumentKind, |
39 | STATEMENT_WITH_INDEX_KIND_BEGIN=ArgumentKind, |
40 | STATEMENT_WITH_INDEX_KIND_END=ArgumentKind, |
41 | STATEMENT_KIND_BEGIN = VariableKind, |
42 | STATEMENT_KIND_END = ArgumentKind, |
43 | InitializerKind, |
44 | INITIALIZER_KIND_BEGIN=InitializerKind, |
45 | INITIALIZER_KIND_END=InitializerKind |
46 | }; |
47 | |
48 | LLVM_DUMP_METHOD static StringRef getKindAsString(ItemKind K) { |
49 | switch (K) { |
50 | case VariableKind: return "construct into local variable"; |
51 | case NewAllocatorKind: return "construct into new-allocator"; |
52 | case ReturnKind: return "construct into return address"; |
53 | case MaterializationKind: return "materialize temporary"; |
54 | case TemporaryDestructorKind: return "destroy temporary"; |
55 | case ElidedDestructorKind: return "elide destructor"; |
56 | case ElidableConstructorKind: return "elide constructor"; |
57 | case ArgumentKind: return "construct into argument"; |
58 | case InitializerKind: return "construct into member variable"; |
59 | }; |
60 | llvm_unreachable("Unknown ItemKind"); |
61 | } |
62 | |
63 | private: |
64 | const void *const Data; |
65 | const ItemKind Kind; |
66 | const unsigned Index = 0; |
67 | |
68 | bool hasStatement() const { |
69 | return Kind >= STATEMENT_KIND_BEGIN && |
70 | Kind <= STATEMENT_KIND_END; |
71 | } |
72 | |
73 | bool hasIndex() const { |
74 | return Kind >= STATEMENT_WITH_INDEX_KIND_BEGIN && |
75 | Kind >= STATEMENT_WITH_INDEX_KIND_END; |
76 | } |
77 | |
78 | bool hasInitializer() const { |
79 | return Kind >= INITIALIZER_KIND_BEGIN && |
80 | Kind <= INITIALIZER_KIND_END; |
81 | } |
82 | |
83 | public: |
84 | |
85 | |
86 | |
87 | ConstructionContextItem(const DeclStmt *DS) |
88 | : Data(DS), Kind(VariableKind) {} |
89 | |
90 | ConstructionContextItem(const CXXNewExpr *NE) |
91 | : Data(NE), Kind(NewAllocatorKind) {} |
92 | |
93 | ConstructionContextItem(const ReturnStmt *RS) |
94 | : Data(RS), Kind(ReturnKind) {} |
95 | |
96 | ConstructionContextItem(const MaterializeTemporaryExpr *MTE) |
97 | : Data(MTE), Kind(MaterializationKind) {} |
98 | |
99 | ConstructionContextItem(const CXXBindTemporaryExpr *BTE, |
100 | bool IsElided = false) |
101 | : Data(BTE), |
102 | Kind(IsElided ? ElidedDestructorKind : TemporaryDestructorKind) {} |
103 | |
104 | ConstructionContextItem(const CXXConstructExpr *CE) |
105 | : Data(CE), Kind(ElidableConstructorKind) {} |
106 | |
107 | ConstructionContextItem(const CallExpr *CE, unsigned Index) |
108 | : Data(CE), Kind(ArgumentKind), Index(Index) {} |
109 | |
110 | ConstructionContextItem(const CXXConstructExpr *CE, unsigned Index) |
111 | : Data(CE), Kind(ArgumentKind), Index(Index) {} |
112 | |
113 | ConstructionContextItem(const ObjCMessageExpr *ME, unsigned Index) |
114 | : Data(ME), Kind(ArgumentKind), Index(Index) {} |
115 | |
116 | |
117 | ConstructionContextItem(const Expr *E, unsigned Index) |
118 | : Data(E), Kind(ArgumentKind), Index(Index) { |
119 | (E) || isa(E) || isa(E)", "/home/seafit/code_projects/clang_source/clang/include/clang/Analysis/ConstructionContext.h", 120, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isa<CallExpr>(E) || isa<CXXConstructExpr>(E) || |
120 | (E) || isa(E) || isa(E)", "/home/seafit/code_projects/clang_source/clang/include/clang/Analysis/ConstructionContext.h", 120, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> isa<ObjCMessageExpr>(E)); |
121 | } |
122 | |
123 | ConstructionContextItem(const CXXCtorInitializer *Init) |
124 | : Data(Init), Kind(InitializerKind), Index(0) {} |
125 | |
126 | ItemKind getKind() const { return Kind; } |
127 | |
128 | LLVM_DUMP_METHOD StringRef getKindAsString() const { |
129 | return getKindAsString(getKind()); |
130 | } |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | const Stmt *getStmt() const { |
137 | assert(hasStatement()); |
138 | return static_cast<const Stmt *>(Data); |
139 | } |
140 | |
141 | const Stmt *getStmtOrNull() const { |
142 | return hasStatement() ? getStmt() : nullptr; |
143 | } |
144 | |
145 | |
146 | |
147 | |
148 | const CXXCtorInitializer *getCXXCtorInitializer() const { |
149 | assert(hasInitializer()); |
150 | return static_cast<const CXXCtorInitializer *>(Data); |
151 | } |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | unsigned getIndex() const { |
158 | |
159 | |
160 | assert(hasIndex()); |
161 | return Index; |
162 | } |
163 | |
164 | void Profile(llvm::FoldingSetNodeID &ID) const { |
165 | ID.AddPointer(Data); |
166 | ID.AddInteger(Kind); |
167 | ID.AddInteger(Index); |
168 | } |
169 | |
170 | bool operator==(const ConstructionContextItem &Other) const { |
171 | |
172 | |
173 | |
174 | return std::make_tuple(Data, Kind, Index) == |
175 | std::make_tuple(Other.Data, Other.Kind, Other.Index); |
176 | } |
177 | |
178 | bool operator<(const ConstructionContextItem &Other) const { |
179 | return std::make_tuple(Data, Kind, Index) < |
180 | std::make_tuple(Other.Data, Other.Kind, Other.Index); |
181 | } |
182 | }; |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | class ConstructionContextLayer { |
204 | const ConstructionContextLayer *Parent = nullptr; |
205 | ConstructionContextItem Item; |
206 | |
207 | ConstructionContextLayer(ConstructionContextItem Item, |
208 | const ConstructionContextLayer *Parent) |
209 | : Parent(Parent), Item(Item) {} |
210 | |
211 | public: |
212 | static const ConstructionContextLayer * |
213 | create(BumpVectorContext &C, const ConstructionContextItem &Item, |
214 | const ConstructionContextLayer *Parent = nullptr); |
215 | |
216 | const ConstructionContextItem &getItem() const { return Item; } |
217 | const ConstructionContextLayer *getParent() const { return Parent; } |
218 | bool isLast() const { return !Parent; } |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | bool isStrictlyMoreSpecificThan(const ConstructionContextLayer *Other) const; |
226 | }; |
227 | |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | class ConstructionContext { |
234 | public: |
235 | enum Kind { |
236 | SimpleVariableKind, |
237 | CXX17ElidedCopyVariableKind, |
238 | VARIABLE_BEGIN = SimpleVariableKind, |
239 | VARIABLE_END = CXX17ElidedCopyVariableKind, |
240 | SimpleConstructorInitializerKind, |
241 | CXX17ElidedCopyConstructorInitializerKind, |
242 | INITIALIZER_BEGIN = SimpleConstructorInitializerKind, |
243 | INITIALIZER_END = CXX17ElidedCopyConstructorInitializerKind, |
244 | NewAllocatedObjectKind, |
245 | SimpleTemporaryObjectKind, |
246 | ElidedTemporaryObjectKind, |
247 | TEMPORARY_BEGIN = SimpleTemporaryObjectKind, |
248 | TEMPORARY_END = ElidedTemporaryObjectKind, |
249 | SimpleReturnedValueKind, |
250 | CXX17ElidedCopyReturnedValueKind, |
251 | RETURNED_VALUE_BEGIN = SimpleReturnedValueKind, |
252 | RETURNED_VALUE_END = CXX17ElidedCopyReturnedValueKind, |
253 | ArgumentKind |
254 | }; |
255 | |
256 | protected: |
257 | Kind K; |
258 | |
259 | |
260 | |
261 | explicit ConstructionContext(Kind K) : K(K) {} |
262 | |
263 | private: |
264 | |
265 | template <typename T, typename... ArgTypes> |
266 | static T *create(BumpVectorContext &C, ArgTypes... Args) { |
267 | auto *CC = C.getAllocator().Allocate<T>(); |
268 | return new (CC) T(Args...); |
269 | } |
270 | |
271 | |
272 | |
273 | |
274 | static const ConstructionContext *createMaterializedTemporaryFromLayers( |
275 | BumpVectorContext &C, const MaterializeTemporaryExpr *MTE, |
276 | const CXXBindTemporaryExpr *BTE, |
277 | const ConstructionContextLayer *ParentLayer); |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | static const ConstructionContext * |
284 | createBoundTemporaryFromLayers( |
285 | BumpVectorContext &C, const CXXBindTemporaryExpr *BTE, |
286 | const ConstructionContextLayer *ParentLayer); |
287 | |
288 | public: |
289 | |
290 | |
291 | |
292 | static const ConstructionContext * |
293 | createFromLayers(BumpVectorContext &C, |
294 | const ConstructionContextLayer *TopLayer); |
295 | |
296 | Kind getKind() const { return K; } |
297 | }; |
298 | |
299 | |
300 | class VariableConstructionContext : public ConstructionContext { |
301 | const DeclStmt *DS; |
302 | |
303 | protected: |
304 | VariableConstructionContext(ConstructionContext::Kind K, const DeclStmt *DS) |
305 | : ConstructionContext(K), DS(DS) { |
306 | assert(classof(this)); |
307 | assert(DS); |
308 | } |
309 | |
310 | public: |
311 | const DeclStmt *getDeclStmt() const { return DS; } |
312 | |
313 | static bool classof(const ConstructionContext *CC) { |
314 | return CC->getKind() >= VARIABLE_BEGIN && |
315 | CC->getKind() <= VARIABLE_END; |
316 | } |
317 | }; |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | class SimpleVariableConstructionContext : public VariableConstructionContext { |
324 | friend class ConstructionContext; |
325 | |
326 | explicit SimpleVariableConstructionContext(const DeclStmt *DS) |
327 | : VariableConstructionContext(ConstructionContext::SimpleVariableKind, |
328 | DS) {} |
329 | |
330 | public: |
331 | static bool classof(const ConstructionContext *CC) { |
332 | return CC->getKind() == SimpleVariableKind; |
333 | } |
334 | }; |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | class CXX17ElidedCopyVariableConstructionContext |
346 | : public VariableConstructionContext { |
347 | const CXXBindTemporaryExpr *BTE; |
348 | |
349 | friend class ConstructionContext; |
350 | |
351 | explicit CXX17ElidedCopyVariableConstructionContext( |
352 | const DeclStmt *DS, const CXXBindTemporaryExpr *BTE) |
353 | : VariableConstructionContext(CXX17ElidedCopyVariableKind, DS), BTE(BTE) { |
354 | assert(BTE); |
355 | } |
356 | |
357 | public: |
358 | const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; } |
359 | |
360 | static bool classof(const ConstructionContext *CC) { |
361 | return CC->getKind() == CXX17ElidedCopyVariableKind; |
362 | } |
363 | }; |
364 | |
365 | |
366 | class ConstructorInitializerConstructionContext : public ConstructionContext { |
367 | const CXXCtorInitializer *I; |
368 | |
369 | protected: |
370 | explicit ConstructorInitializerConstructionContext( |
371 | ConstructionContext::Kind K, const CXXCtorInitializer *I) |
372 | : ConstructionContext(K), I(I) { |
373 | assert(classof(this)); |
374 | assert(I); |
375 | } |
376 | |
377 | public: |
378 | const CXXCtorInitializer *getCXXCtorInitializer() const { return I; } |
379 | |
380 | static bool classof(const ConstructionContext *CC) { |
381 | return CC->getKind() >= INITIALIZER_BEGIN && |
382 | CC->getKind() <= INITIALIZER_END; |
383 | } |
384 | }; |
385 | |
386 | |
387 | |
388 | class SimpleConstructorInitializerConstructionContext |
389 | : public ConstructorInitializerConstructionContext { |
390 | friend class ConstructionContext; |
391 | |
392 | explicit SimpleConstructorInitializerConstructionContext( |
393 | const CXXCtorInitializer *I) |
394 | : ConstructorInitializerConstructionContext( |
395 | ConstructionContext::SimpleConstructorInitializerKind, I) {} |
396 | |
397 | public: |
398 | static bool classof(const ConstructionContext *CC) { |
399 | return CC->getKind() == SimpleConstructorInitializerKind; |
400 | } |
401 | }; |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | class CXX17ElidedCopyConstructorInitializerConstructionContext |
414 | : public ConstructorInitializerConstructionContext { |
415 | const CXXBindTemporaryExpr *BTE; |
416 | |
417 | friend class ConstructionContext; |
418 | |
419 | explicit CXX17ElidedCopyConstructorInitializerConstructionContext( |
420 | const CXXCtorInitializer *I, const CXXBindTemporaryExpr *BTE) |
421 | : ConstructorInitializerConstructionContext( |
422 | CXX17ElidedCopyConstructorInitializerKind, I), |
423 | BTE(BTE) { |
424 | assert(BTE); |
425 | } |
426 | |
427 | public: |
428 | const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; } |
429 | |
430 | static bool classof(const ConstructionContext *CC) { |
431 | return CC->getKind() == CXX17ElidedCopyConstructorInitializerKind; |
432 | } |
433 | }; |
434 | |
435 | |
436 | |
437 | class NewAllocatedObjectConstructionContext : public ConstructionContext { |
438 | const CXXNewExpr *NE; |
439 | |
440 | friend class ConstructionContext; |
441 | |
442 | explicit NewAllocatedObjectConstructionContext(const CXXNewExpr *NE) |
443 | : ConstructionContext(ConstructionContext::NewAllocatedObjectKind), |
444 | NE(NE) { |
445 | assert(NE); |
446 | } |
447 | |
448 | public: |
449 | const CXXNewExpr *getCXXNewExpr() const { return NE; } |
450 | |
451 | static bool classof(const ConstructionContext *CC) { |
452 | return CC->getKind() == NewAllocatedObjectKind; |
453 | } |
454 | }; |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | class TemporaryObjectConstructionContext : public ConstructionContext { |
461 | const CXXBindTemporaryExpr *BTE; |
462 | const MaterializeTemporaryExpr *MTE; |
463 | |
464 | protected: |
465 | explicit TemporaryObjectConstructionContext( |
466 | ConstructionContext::Kind K, const CXXBindTemporaryExpr *BTE, |
467 | const MaterializeTemporaryExpr *MTE) |
468 | : ConstructionContext(K), BTE(BTE), MTE(MTE) { |
469 | |
470 | |
471 | |
472 | |
473 | } |
474 | |
475 | public: |
476 | |
477 | |
478 | const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { |
479 | return BTE; |
480 | } |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | const MaterializeTemporaryExpr *getMaterializedTemporaryExpr() const { |
488 | return MTE; |
489 | } |
490 | |
491 | static bool classof(const ConstructionContext *CC) { |
492 | return CC->getKind() >= TEMPORARY_BEGIN && CC->getKind() <= TEMPORARY_END; |
493 | } |
494 | }; |
495 | |
496 | |
497 | |
498 | |
499 | |
500 | class SimpleTemporaryObjectConstructionContext |
501 | : public TemporaryObjectConstructionContext { |
502 | friend class ConstructionContext; |
503 | |
504 | explicit SimpleTemporaryObjectConstructionContext( |
505 | const CXXBindTemporaryExpr *BTE, const MaterializeTemporaryExpr *MTE) |
506 | : TemporaryObjectConstructionContext( |
507 | ConstructionContext::SimpleTemporaryObjectKind, BTE, MTE) {} |
508 | |
509 | public: |
510 | static bool classof(const ConstructionContext *CC) { |
511 | return CC->getKind() == SimpleTemporaryObjectKind; |
512 | } |
513 | }; |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | class ElidedTemporaryObjectConstructionContext |
524 | : public TemporaryObjectConstructionContext { |
525 | const CXXConstructExpr *ElidedCE; |
526 | const ConstructionContext *ElidedCC; |
527 | |
528 | friend class ConstructionContext; |
529 | |
530 | explicit ElidedTemporaryObjectConstructionContext( |
531 | const CXXBindTemporaryExpr *BTE, const MaterializeTemporaryExpr *MTE, |
532 | const CXXConstructExpr *ElidedCE, const ConstructionContext *ElidedCC) |
533 | : TemporaryObjectConstructionContext( |
534 | ConstructionContext::ElidedTemporaryObjectKind, BTE, MTE), |
535 | ElidedCE(ElidedCE), ElidedCC(ElidedCC) { |
536 | |
537 | |
538 | |
539 | isElidable() && ElidedCC", "/home/seafit/code_projects/clang_source/clang/include/clang/Analysis/ConstructionContext.h", 539, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(ElidedCE && ElidedCE->isElidable() && ElidedCC); |
540 | } |
541 | |
542 | public: |
543 | const CXXConstructExpr *getConstructorAfterElision() const { |
544 | return ElidedCE; |
545 | } |
546 | |
547 | const ConstructionContext *getConstructionContextAfterElision() const { |
548 | return ElidedCC; |
549 | } |
550 | |
551 | static bool classof(const ConstructionContext *CC) { |
552 | return CC->getKind() == ElidedTemporaryObjectKind; |
553 | } |
554 | }; |
555 | |
556 | class ReturnedValueConstructionContext : public ConstructionContext { |
557 | const ReturnStmt *RS; |
558 | |
559 | protected: |
560 | explicit ReturnedValueConstructionContext(ConstructionContext::Kind K, |
561 | const ReturnStmt *RS) |
562 | : ConstructionContext(K), RS(RS) { |
563 | assert(classof(this)); |
564 | assert(RS); |
565 | } |
566 | |
567 | public: |
568 | const ReturnStmt *getReturnStmt() const { return RS; } |
569 | |
570 | static bool classof(const ConstructionContext *CC) { |
571 | return CC->getKind() >= RETURNED_VALUE_BEGIN && |
572 | CC->getKind() <= RETURNED_VALUE_END; |
573 | } |
574 | }; |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | |
581 | class SimpleReturnedValueConstructionContext |
582 | : public ReturnedValueConstructionContext { |
583 | friend class ConstructionContext; |
584 | |
585 | explicit SimpleReturnedValueConstructionContext(const ReturnStmt *RS) |
586 | : ReturnedValueConstructionContext( |
587 | ConstructionContext::SimpleReturnedValueKind, RS) {} |
588 | |
589 | public: |
590 | static bool classof(const ConstructionContext *CC) { |
591 | return CC->getKind() == SimpleReturnedValueKind; |
592 | } |
593 | }; |
594 | |
595 | |
596 | |
597 | |
598 | |
599 | |
600 | |
601 | |
602 | |
603 | class CXX17ElidedCopyReturnedValueConstructionContext |
604 | : public ReturnedValueConstructionContext { |
605 | const CXXBindTemporaryExpr *BTE; |
606 | |
607 | friend class ConstructionContext; |
608 | |
609 | explicit CXX17ElidedCopyReturnedValueConstructionContext( |
610 | const ReturnStmt *RS, const CXXBindTemporaryExpr *BTE) |
611 | : ReturnedValueConstructionContext( |
612 | ConstructionContext::CXX17ElidedCopyReturnedValueKind, RS), |
613 | BTE(BTE) { |
614 | assert(BTE); |
615 | } |
616 | |
617 | public: |
618 | const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; } |
619 | |
620 | static bool classof(const ConstructionContext *CC) { |
621 | return CC->getKind() == CXX17ElidedCopyReturnedValueKind; |
622 | } |
623 | }; |
624 | |
625 | class ArgumentConstructionContext : public ConstructionContext { |
626 | |
627 | const Expr *CE; |
628 | |
629 | |
630 | |
631 | |
632 | unsigned Index; |
633 | |
634 | |
635 | const CXXBindTemporaryExpr *BTE; |
636 | |
637 | friend class ConstructionContext; |
638 | |
639 | explicit ArgumentConstructionContext(const Expr *CE, unsigned Index, |
640 | const CXXBindTemporaryExpr *BTE) |
641 | : ConstructionContext(ArgumentKind), CE(CE), |
642 | Index(Index), BTE(BTE) { |
643 | (CE) || isa(CE) || isa(CE)", "/home/seafit/code_projects/clang_source/clang/include/clang/Analysis/ConstructionContext.h", 644, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isa<CallExpr>(CE) || isa<CXXConstructExpr>(CE) || |
644 | (CE) || isa(CE) || isa(CE)", "/home/seafit/code_projects/clang_source/clang/include/clang/Analysis/ConstructionContext.h", 644, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> isa<ObjCMessageExpr>(CE)); |
645 | |
646 | } |
647 | |
648 | public: |
649 | const Expr *getCallLikeExpr() const { return CE; } |
650 | unsigned getIndex() const { return Index; } |
651 | const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; } |
652 | |
653 | static bool classof(const ConstructionContext *CC) { |
654 | return CC->getKind() == ArgumentKind; |
655 | } |
656 | }; |
657 | |
658 | } |
659 | |
660 | #endif |
661 | |