-
Notifications
You must be signed in to change notification settings - Fork 48
/
object_repository.cc
974 lines (875 loc) · 35.9 KB
/
object_repository.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "lifetime_analysis/object_repository.h"
#include <cassert>
#include <cstddef>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "absl/strings/str_cat.h"
#include "lifetime_analysis/builtin_lifetimes.h"
#include "lifetime_analysis/object.h"
#include "lifetime_analysis/object_set.h"
#include "lifetime_analysis/points_to_map.h"
#include "lifetime_annotations/function_lifetimes.h"
#include "lifetime_annotations/lifetime.h"
#include "lifetime_annotations/pointee_type.h"
#include "lifetime_annotations/type_lifetimes.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/Type.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/Specifiers.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
namespace clang {
namespace tidy {
namespace lifetimes {
FunctionLifetimesOrError GetFunctionLifetimes(
const FunctionDecl* decl, const FunctionLifetimesMap& known_lifetimes) {
// Note that this cannot be a simple .find() on the map because `decl` might
// be a builtin.
bool is_builtin = decl->getBuiltinID() != 0;
if (is_builtin) {
return GetBuiltinLifetimes(decl);
}
if (!known_lifetimes.count(decl->getCanonicalDecl())) {
return FunctionAnalysisError(
absl::StrCat("Unknown callee ", decl->getNameAsString()));
}
return known_lifetimes.lookup(decl->getCanonicalDecl());
}
class ObjectRepository::VarDeclVisitor
: public clang::RecursiveASTVisitor<VarDeclVisitor> {
public:
explicit VarDeclVisitor(ObjectRepository& object_repository,
const FunctionLifetimesMap& callee_lifetimes)
: object_repository_(object_repository),
callee_lifetimes_(callee_lifetimes) {}
// We need to visit implicitly-defined constructors and assignment operators.
bool shouldVisitImplicitCode() { return true; }
bool VisitVarDecl(clang::VarDecl* var) {
// Add objects for any local variables declared in this function.
AddObjectForVar(var);
return true;
}
bool VisitReturnStmt(clang::ReturnStmt* stmt) {
const clang::Expr* expr = stmt->getRetValue();
if (IsInitExprInitializingARecordObject(expr)) {
PropagateResultObject(expr, object_repository_.return_object_);
}
return true;
}
bool VisitMemberExpr(clang::MemberExpr* member) {
if (auto* method =
clang::dyn_cast<clang::CXXMethodDecl>(member->getMemberDecl());
method && method->isStatic()) {
// Create objects for static member functions.
AddObjectForFunc(method);
}
return true;
}
bool VisitDeclRefExpr(clang::DeclRefExpr* decl_ref) {
// Add objects for any global variables referenced in this function.
// This also runs for local variables, but we don't have to treat those
// differently as AddObjectForVar() protects against duplication.
if (auto* var_decl = clang::dyn_cast<clang::VarDecl>(decl_ref->getDecl())) {
AddObjectForVar(var_decl);
}
// Add objects for any function referenced in this function.
if (auto* function_decl =
clang::dyn_cast<clang::FunctionDecl>(decl_ref->getDecl())) {
AddObjectForFunc(function_decl);
}
return true;
}
bool VisitObjCMessageExpr(clang::ObjCMessageExpr* msg_expr) {
// ObjCMessageExpr is an initializer expression terminator, so we should
// have walked down from the result object to find its terminating
// expressions, which should have found this expression and connected it to
// that object already.
if (!object_repository_.result_objects_.count(msg_expr)) {
msg_expr->dump();
llvm::report_fatal_error(
"Missing initializer for ObjCMessageExpr, we did not record it "
"when we visited something earlier in the tree yet?");
}
return true;
}
// Create objects for function call arguments.
bool VisitCallExpr(clang::CallExpr* call_expr) {
if (IsInitExprInitializingARecordObject(call_expr)) {
assert(ResultObjectWasPropagatedTo(call_expr));
}
FunctionLifetimeFactorySingleCallback lifetime_factory(
[](auto) { return Lifetime::CreateVariable(); });
// If we have a direct callee, construct a FunctionLifetimes out of the
// function/method definition.
if (auto callee = call_expr->getDirectCallee()) {
bool is_operator_call = clang::isa<clang::CXXOperatorCallExpr>(call_expr);
bool is_method = clang::isa<clang::CXXMethodDecl>(callee);
object_repository_.call_expr_virtual_lifetimes_[call_expr] =
FunctionLifetimes::CreateForDecl(callee, lifetime_factory).get();
PrepareFunctionCall(
call_expr, /*index_shift=*/is_operator_call && is_method ? 1 : 0);
} else {
// Always a function pointer.
// TODO(veluca): pointers-to-members are not supported (yet?)
clang::QualType callee_type =
call_expr->getCallee()->getType()->getPointeeType().IgnoreParens();
// TODO(veluca): what about FunctionNoProtoType??
object_repository_.call_expr_virtual_lifetimes_[call_expr] =
FunctionLifetimes::CreateForFunctionType(
clang::cast<clang::FunctionProtoType>(callee_type),
lifetime_factory)
.get();
PrepareFunctionCall(call_expr, /*index_shift=*/0);
}
return true;
}
bool VisitStringLiteral(clang::StringLiteral* lit_expr) {
object_repository_.string_literal_object_ = object_repository_.CreateObject(
lit_expr->getType(), Lifetime::Static(),
[](const clang::Expr*) { return Lifetime::Static(); });
return true;
}
bool VisitCXXConstructExpr(clang::CXXConstructExpr* construct_expr) {
assert(ResultObjectWasPropagatedTo(construct_expr));
FunctionLifetimeFactorySingleCallback lifetime_factory(
[](auto) { return Lifetime::CreateVariable(); });
const clang::FunctionDecl* constructor = construct_expr->getConstructor();
object_repository_.call_expr_virtual_lifetimes_[construct_expr] =
FunctionLifetimes::CreateForDecl(constructor, lifetime_factory).get();
PrepareFunctionCall(construct_expr,
/*index_shift=*/0);
return true;
}
bool VisitInitListExpr(clang::InitListExpr* init_list_expr) {
// We only want to visit in Semantic form, we ignore Syntactic form.
if (IsInitExprInitializingARecordObject(init_list_expr) &&
init_list_expr->isSemanticForm() && !init_list_expr->isTransparent()) {
assert(ResultObjectWasPropagatedTo(init_list_expr));
}
return true;
}
bool VisitMaterializeTemporaryExpr(
clang::MaterializeTemporaryExpr* temporary_expr) {
object_repository_.temporary_objects_[temporary_expr] =
AddTemporaryObjectForExpression(temporary_expr->getSubExpr());
return true;
}
bool VisitCompoundStmt(clang::CompoundStmt* compound) {
// Create temporary objects for any top-level `CXXTemporaryObjectExpr`s,
// i.e. ones that are used as statements.
for (clang::Stmt* stmt : compound->body()) {
if (auto* temporary = clang::dyn_cast<CXXTemporaryObjectExpr>(stmt)) {
AddTemporaryObjectForExpression(temporary);
}
}
return true;
}
void PrepareFunctionCall(const clang::Expr* expr, size_t index_shift) {
const auto& func_lifetimes =
object_repository_.call_expr_virtual_lifetimes_[expr];
auto make_object = [this](const ValueLifetimes& lifetime) {
return object_repository_.CreateObject(
ObjectLifetimes(Lifetime::CreateLocal(), lifetime),
object_repository_.initial_points_to_map_);
};
for (size_t i = 0; i < func_lifetimes.GetNumParams(); ++i) {
object_repository_
.call_expr_args_objects_[std::make_pair(expr, i + index_shift)] =
make_object(func_lifetimes.GetParamLifetimes(i));
}
if (func_lifetimes.IsNonStaticMethod()) {
object_repository_.call_expr_this_pointers_[expr] =
make_object(func_lifetimes.GetThisLifetimes());
}
object_repository_.call_expr_ret_objects_[expr] =
make_object(func_lifetimes.GetReturnLifetimes());
}
void AddObjectForVar(clang::VarDecl* var) {
if (object_repository_.object_repository_.count(var)) {
return;
}
Lifetime lifetime;
LifetimeFactory lifetime_factory;
switch (var->getStorageClass()) {
case clang::SC_Extern:
case clang::SC_Static:
case clang::SC_PrivateExtern:
lifetime = Lifetime::Static();
lifetime_factory = [](const clang::Expr*) {
return Lifetime::Static();
};
break;
default:
lifetime = Lifetime::CreateLocal();
lifetime_factory = [](const clang::Expr*) {
return Lifetime::CreateVariable();
};
break;
}
const Object* object = object_repository_.CreateObject(
var->getType(), lifetime, lifetime_factory);
object_repository_.object_repository_[var] = object;
if (!var->getType()->isArrayType()) {
object_repository_.initial_single_valued_objects_.Add(object);
}
// Remember the original value of function parameters.
if (auto parm_var_decl = clang::dyn_cast<const clang::ParmVarDecl>(var)) {
object_repository_.initial_parameter_object_[parm_var_decl] =
object_repository_.CloneObject(object);
}
if (var->hasInit() && var->getType()->isRecordType()) {
PropagateResultObject(var->getInit(), object);
}
}
void AddObjectForFunc(clang::FunctionDecl* func) {
if (object_repository_.object_repository_.count(func)) {
return;
}
FunctionLifetimesOrError func_lifetimes =
GetFunctionLifetimes(func, callee_lifetimes_);
if (std::holds_alternative<FunctionAnalysisError>(func_lifetimes)) {
error_ = "No lifetimes for callee '" + func->getNameAsString() +
"': " + std::get<FunctionAnalysisError>(func_lifetimes).message;
} else {
object_repository_.object_repository_[func] =
object_repository_.ConstructObject(
Lifetime::Static(), func->getType(),
std::get<FunctionLifetimes>(func_lifetimes));
}
}
const Object* AddTemporaryObjectForExpression(clang::Expr* expr) {
clang::QualType type = expr->getType().getCanonicalType();
const Object* object = object_repository_.CreateObject(
type, Lifetime::CreateLocal(),
[](const clang::Expr*) { return Lifetime::CreateVariable(); });
if (type->isRecordType()) {
PropagateResultObject(expr, object);
}
return object;
}
// Propagates a result object `object` of record type to the expressions that
// actually perform the initialization (we call these "terminating
// expressions").
//
// `expr` is the initializer for a variable; this will contain one or
// several terminating expressions (such as a CXXConstructExpr, InitListExpr,
// or CallExpr).
//
// Note that not all terminating expressions below `expr` necessarily
// initialize `object`; some of these terminating expressions may also
// initialize temporary objects. This function takes care to propagate
// `object` only to the appropriate terminating expressions.
//
// The mapping from a terminating expression to the result object it
// initializes is stored in `object_repository_.result_objects_`.
void PropagateResultObject(const clang::Expr* expr, const Object* object) {
// TODO(danakj): Use StmtVisitor to implement this method.
// Terminating expressions. Expressions that don't initialize a record
// object can not be such, and their existence is unexpected as we should
// be converting to and initializing a record object from such expressions
// further up in the initializer expression's AST. We will assert later in
// this function if we find this situation somehow due to incorrect
// expectations in this comment.
if (IsInitExprInitializingARecordObject(expr)) {
if (clang::isa<clang::CXXConstructExpr>(expr) ||
clang::isa<clang::CallExpr>(expr) ||
clang::isa<clang::ObjCMessageExpr>(expr) ||
clang::isa<clang::LambdaExpr>(expr)) {
object_repository_.result_objects_[expr] = object;
return;
}
if (auto* e = clang::dyn_cast<clang::InitListExpr>(expr)) {
if (!e->isSemanticForm()) return;
if (e->isTransparent()) {
// A field initializer like `S s{cond ? S{} : S{}}` is considered
// transparent, and the actual initializer is within.
for (const clang::Expr* init : e->inits()) {
PropagateResultObject(init, object);
}
} else {
object_repository_.result_objects_[e] = object;
}
return;
}
}
// Expressions to walk through. Logic is similar to the AggExprEmitter in
// clang third_party/llvm-project/clang/lib/CodeGen/CGExprAgg.cpp though we
// don't have to visit all the sub-expressions that clang codegen needs to,
// as we can stop at terminating expressions and ignore many expressions
// that don't occur in the code we're analyzing.
if (auto* e = clang::dyn_cast<clang::ParenExpr>(expr)) {
PropagateResultObject(e->getSubExpr(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::UnaryOperator>(expr)) {
PropagateResultObject(e->getSubExpr(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::SubstNonTypeTemplateParmExpr>(expr)) {
PropagateResultObject(e->getReplacement(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::CastExpr>(expr)) {
PropagateResultObject(e->getSubExpr(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::CXXDefaultArgExpr>(expr)) {
PropagateResultObject(e->getExpr(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::CXXDefaultInitExpr>(expr)) {
PropagateResultObject(e->getExpr(), object);
return;
}
if (auto* e = clang::dyn_cast<clang::ExprWithCleanups>(expr)) {
PropagateResultObject(e->getSubExpr(), object);
return;
}
// Expressions that produce a temporary object.
if (auto* e = clang::dyn_cast<clang::BinaryOperator>(expr)) {
if (e->isCommaOp()) {
AddTemporaryObjectForExpression(e->getLHS());
PropagateResultObject(e->getRHS(), object);
return;
}
// Any other binary operator should not produce a record type, it would be
// used to construct a record further up the AST, so we should not arrive
// here.
expr->dump();
llvm::report_fatal_error(
"Unexpected binary operator in initializer expression tree");
}
if (auto* e = clang::dyn_cast<clang::AbstractConditionalOperator>(expr)) {
AddTemporaryObjectForExpression(e->getCond());
PropagateResultObject(e->getTrueExpr(), object);
PropagateResultObject(e->getFalseExpr(), object);
return;
}
expr->dump();
llvm::report_fatal_error(
"Unexpected expression in initializer expression tree");
}
bool ResultObjectWasPropagatedTo(clang::Expr* terminating_expr) {
// An expression that initializes an object should have already been
// connected to the result object it initializes. We should have walked down
// from the result object to find its terminating expressions.
if (!object_repository_.result_objects_.count(terminating_expr)) {
llvm::errs() << "Missing result object for terminating expression, "
"we did not record it when we visited something earlier "
"in the tree yet?\n";
terminating_expr->dump();
return false;
} else {
return true;
}
}
void TraverseCXXMemberInitializers(
const clang::CXXConstructorDecl* constructor) {
// For constructors, we also need to create lifetimes for variables
// referenced by in-class member initializers; the visitor by default only
// visits expressions in the initializer list.
// We also need to associate member initializers with the members they
// initialize.
for (const auto* init : constructor->inits()) {
const auto* init_expr = init->getInit();
if (const auto* default_init =
clang::dyn_cast<clang::CXXDefaultInitExpr>(init_expr)) {
init_expr = default_init->getExpr();
}
if (init->getMember() && init->getMember()->getType()->isRecordType()) {
std::optional<const Object*> this_object =
object_repository_.GetThisObject();
assert(this_object.has_value());
const Object* field_object =
object_repository_.GetFieldObject(*this_object, init->getMember());
PropagateResultObject(init_expr, field_object);
} else if (init->getBaseClass()) {
std::optional<const Object*> this_object =
object_repository_.GetThisObject();
assert(this_object.has_value());
const Object* base_object = object_repository_.GetBaseClassObject(
*this_object, init->getBaseClass());
PropagateResultObject(init_expr, base_object);
}
// Traverse after finishing with the outer expression, including
// connecting the initializer (constructor) to its object.
TraverseStmt(const_cast<clang::Expr*>(init_expr));
}
}
ObjectRepository& object_repository_;
const FunctionLifetimesMap& callee_lifetimes_;
std::optional<std::string> error_;
};
llvm::Expected<ObjectRepository> ObjectRepository::Create(
const clang::FunctionDecl* func,
const llvm::DenseMap<const clang::FunctionDecl*, FunctionLifetimesOrError>&
callee_lifetimes) {
ObjectRepository object_repository;
const auto* method_decl = clang::dyn_cast<clang::CXXMethodDecl>(func);
const auto* definition = func->getDefinition();
assert(definition || (method_decl && method_decl->isPure()));
if (definition) func = definition;
object_repository.func_ = func;
object_repository.return_object_ = object_repository.CreateObject(
func->getReturnType(), Lifetime::CreateVariable(),
[](const clang::Expr*) { return Lifetime::CreateVariable(); });
if (method_decl) {
if (!method_decl->isStatic()) {
object_repository.this_object_ = object_repository.CreateObject(
method_decl->getFunctionObjectParameterType(),
Lifetime::CreateVariable(),
[](const clang::Expr*) { return Lifetime::CreateVariable(); });
}
}
VarDeclVisitor decl_visitor(object_repository, callee_lifetimes);
if (auto* constructor = clang::dyn_cast<clang::CXXConstructorDecl>(func)) {
decl_visitor.TraverseCXXMemberInitializers(constructor);
}
decl_visitor.TraverseFunctionDecl(const_cast<clang::FunctionDecl*>(func));
if (decl_visitor.error_.has_value()) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
*decl_visitor.error_);
}
return object_repository;
}
std::string ObjectRepository::DebugString() const {
std::string result;
llvm::raw_string_ostream os(result);
if (this_object_) {
os << "This " << (*this_object_)->DebugString() << "\n";
}
for (const auto& [decl, object] : object_repository_) {
os << decl->getDeclKindName() << " " << decl << " (";
decl->printName(os);
os << ") object: " << object->DebugString() << "\n";
}
for (const auto& [expr_i, object] : call_expr_args_objects_) {
const auto& [expr, i] = expr_i;
os << "Call " << expr << " (arg " << i
<< ") object: " << object->DebugString() << "\n";
}
for (const auto& [expr, object] : call_expr_this_pointers_) {
os << "Call " << expr << " (this) pointer: " << object->DebugString()
<< "\n";
}
os << "InitialPointsToMap:\n" << initial_points_to_map_.DebugString() << "\n";
for (const auto& [field, object] : field_object_map_) {
os << "Field '";
field.second->printName(os, field.second->getASTContext().getPrintingPolicy());
os << "' on " << field.first->DebugString()
<< " object: " << object->DebugString() << "\n";
}
for (const auto& [base, object] : base_object_map_) {
os << "Base of type " << clang::QualType(base.second, 0).getAsString()
<< " of " << base.first->DebugString()
<< " object: " << object->DebugString() << "\n";
}
os << "Return " << return_object_->DebugString() << "\n";
os.flush();
return result;
}
const Object* ObjectRepository::GetDeclObject(
const clang::ValueDecl* decl) const {
auto iter = object_repository_.find(decl);
if (iter == object_repository_.end()) {
llvm::errs() << "Didn't find object for Decl:\n";
decl->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find object for Decl");
}
return iter->second;
}
const Object* ObjectRepository::GetTemporaryObject(
const clang::MaterializeTemporaryExpr* expr) const {
auto iter = temporary_objects_.find(expr);
if (iter == temporary_objects_.end()) {
llvm::errs() << "Didn't find object for temporary expression:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find object for temporary expression");
}
return iter->second;
}
const Object* ObjectRepository::GetOriginalParameterValue(
const clang::ParmVarDecl* var_decl) const {
auto iter = initial_parameter_object_.find(var_decl);
if (iter == initial_parameter_object_.end()) {
llvm::errs() << "Didn't find caller object for parameter:\n";
var_decl->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find caller object for parameter");
}
return iter->second;
}
FunctionLifetimes ObjectRepository::GetOriginalFunctionLifetimes() const {
FunctionLifetimes ret;
auto get_initial_lifetimes_or_die = [&](const Object* object) {
auto iter = initial_object_lifetimes_.find(object);
if (iter == initial_object_lifetimes_.end()) {
llvm::errs() << "Didn't find lifetimes for object "
<< object->DebugString();
llvm::report_fatal_error("Didn't find lifetimes for object");
}
return iter->second;
};
ret.return_lifetimes_ =
get_initial_lifetimes_or_die(GetReturnObject()).GetValueLifetimes();
if (this_object_.has_value()) {
ret.this_lifetimes_ = ValueLifetimes::PointerTo(
clang::dyn_cast<clang::CXXMethodDecl>(func_)->getThisType(),
get_initial_lifetimes_or_die(*this_object_));
}
ret.param_lifetimes_.reserve(func_->getNumParams());
for (size_t i = 0; i < func_->getNumParams(); i++) {
ret.param_lifetimes_.push_back(
get_initial_lifetimes_or_die(
GetOriginalParameterValue(func_->getParamDecl(i)))
.GetValueLifetimes());
}
if (!ret.IsValidForDecl(func_)) {
llvm::errs() << "Internal error: did not produce valid function lifetimes";
llvm::report_fatal_error(
"Internal error: did not produce valid function lifetimes");
}
return ret;
}
const Object* ObjectRepository::GetCallExprArgumentObject(
const clang::CallExpr* expr, size_t arg_index) const {
auto iter = call_expr_args_objects_.find(std::make_pair(expr, arg_index));
if (iter == call_expr_args_objects_.end()) {
llvm::errs() << "Didn't find object for argument " << arg_index
<< " of call:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find object for argument");
}
return iter->second;
}
const Object* ObjectRepository::GetCallExprRetObject(
const clang::Expr* expr) const {
auto iter = call_expr_ret_objects_.find(expr);
if (iter == call_expr_ret_objects_.end()) {
llvm::errs() << "Didn't find object for return value of call:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find object for return value");
}
return iter->second;
}
const FunctionLifetimes& ObjectRepository::GetCallExprVirtualLifetimes(
const clang::Expr* expr) const {
auto iter = call_expr_virtual_lifetimes_.find(expr);
if (iter == call_expr_virtual_lifetimes_.end()) {
llvm::errs() << "Didn't find object for return value of call:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find object for return value");
}
return iter->second;
}
const Object* ObjectRepository::GetCallExprThisPointer(
const clang::CallExpr* expr) const {
auto iter = call_expr_this_pointers_.find(expr);
if (iter == call_expr_this_pointers_.end()) {
llvm::errs() << "Didn't find `this` object for call:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find `this` object for call");
}
return iter->second;
}
const Object* ObjectRepository::GetCXXConstructExprArgumentObject(
const clang::CXXConstructExpr* expr, size_t arg_index) const {
auto iter = call_expr_args_objects_.find(std::make_pair(expr, arg_index));
if (iter == call_expr_args_objects_.end()) {
llvm::errs() << "Didn't find object for argument " << arg_index
<< " of constructor call:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error(
"Didn't find object for argument of constructor call");
}
return iter->second;
}
const Object* ObjectRepository::GetCXXConstructExprThisPointer(
const clang::CXXConstructExpr* expr) const {
auto iter = call_expr_this_pointers_.find(expr);
if (iter == call_expr_this_pointers_.end()) {
llvm::errs() << "Didn't find `this` object for constructor:\n";
expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find `this` object for constructor");
}
return iter->second;
}
const Object* ObjectRepository::GetResultObject(
const clang::Expr* initializer_expr) const {
assert(clang::isa<clang::CXXConstructExpr>(initializer_expr) ||
clang::isa<clang::InitListExpr>(initializer_expr) ||
clang::isa<clang::CallExpr>(initializer_expr));
auto iter = result_objects_.find(initializer_expr);
if (iter == result_objects_.end()) {
llvm::errs() << "Didn't find result object for initializer:\n";
initializer_expr->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find result object for initializer");
}
return iter->second;
}
const Object* ObjectRepository::GetFieldObject(
const Object* struct_object, const clang::FieldDecl* field) const {
std::optional<const Object*> field_object =
GetFieldObjectInternal(struct_object, field);
if (!field_object.has_value()) {
llvm::errs() << "On an object of type "
<< struct_object->Type().getAsString()
<< ", trying to get field:\n";
field->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find field object");
}
return *field_object;
}
ObjectSet ObjectRepository::GetFieldObject(
const ObjectSet& struct_objects, const clang::FieldDecl* field) const {
ObjectSet ret;
for (const Object* object : struct_objects) {
ret.Add(GetFieldObject(object, field));
}
return ret;
}
const Object* ObjectRepository::GetBaseClassObject(
const Object* struct_object, const clang::Type* base) const {
base = base->getCanonicalTypeInternal().getTypePtr();
auto iter = base_object_map_.find(std::make_pair(struct_object, base));
if (iter == base_object_map_.end()) {
llvm::errs() << "On object " << struct_object->DebugString()
<< ", trying to get base:\n";
base->dump();
llvm::errs() << "\n" << DebugString();
llvm::report_fatal_error("Didn't find base object");
}
return iter->second;
}
ObjectSet ObjectRepository::GetBaseClassObject(const ObjectSet& struct_objects,
const clang::Type* base) const {
ObjectSet ret;
for (const Object* object : struct_objects) {
ret.Add(GetBaseClassObject(object, base));
}
return ret;
}
namespace {
llvm::SmallVector<std::string> GetFieldLifetimeArguments(
const clang::FieldDecl* field) {
// TODO(mboehme): Report errors as Clang diagnostics, not through
// llvm::report_fatal_error().
const clang::AnnotateAttr* member_lifetimes_attr = nullptr;
for (auto annotate : field->specific_attrs<clang::AnnotateAttr>()) {
if (annotate->getAnnotation() == "member_lifetimes") {
if (member_lifetimes_attr) {
llvm::report_fatal_error("repeated lifetime annotation");
}
member_lifetimes_attr = annotate;
}
}
if (!member_lifetimes_attr) {
return {};
}
llvm::SmallVector<std::string> ret;
for (const auto& arg : member_lifetimes_attr->args()) {
llvm::StringRef lifetime;
if (llvm::Error err = EvaluateAsStringLiteral(arg, field->getASTContext())
.moveInto(lifetime)) {
llvm::report_fatal_error(llvm::StringRef(toString(std::move(err))));
}
ret.push_back(lifetime.str());
}
return ret;
}
template <typename CallbackField, typename CallackBase>
void ForEachFieldAndBase(clang::QualType record_type,
const ObjectLifetimes& object_lifetimes,
const CallbackField& callback_field,
const CallackBase& callback_base) {
assert(record_type->isRecordType());
for (clang::FieldDecl* f :
record_type->getAs<clang::RecordType>()->getDecl()->fields()) {
ObjectLifetimes field_lifetimes = object_lifetimes.GetFieldOrBaseLifetimes(
f->getType(), GetFieldLifetimeArguments(f));
callback_field(field_lifetimes, f);
}
if (auto* cxxrecord = clang::dyn_cast<clang::CXXRecordDecl>(
record_type->getAs<clang::RecordType>()->getDecl())) {
for (const clang::CXXBaseSpecifier& base : cxxrecord->bases()) {
clang::QualType base_type = base.getType();
auto base_object_lifetimes = object_lifetimes.GetFieldOrBaseLifetimes(
base_type, GetLifetimeParameters(base_type));
callback_base(base_object_lifetimes, &*base_type.getCanonicalType());
ForEachFieldAndBase(base.getType(), base_object_lifetimes, callback_field,
callback_base);
}
}
}
} // namespace
class ObjectRepository::ObjectCreator {
public:
ObjectCreator(ObjectRepository& object_repository, PointsToMap& points_to_map)
: object_repository_(object_repository), points_to_map_(points_to_map) {}
const Object* CreateObjectsRecursively(
const ObjectLifetimes& object_lifetimes) {
if (auto it = object_cache_.find(object_lifetimes);
it != object_cache_.end()) {
return it->second;
}
const clang::QualType type = object_lifetimes.Type();
const ValueLifetimes& value_lifetimes =
object_lifetimes.GetValueLifetimes();
std::optional<FunctionLifetimes> function_lifetimes;
if (type->getAs<clang::FunctionType>()) {
function_lifetimes = value_lifetimes.GetFuncLifetimes();
}
const Object* obj = object_repository_.ConstructObject(
object_lifetimes.GetLifetime(), object_lifetimes.Type(),
function_lifetimes);
object_cache_[object_lifetimes] = obj;
object_repository_.initial_object_lifetimes_[obj] = object_lifetimes;
if (type->isIncompleteType()) {
// Nothing we can do.
return obj;
}
// Pointer type.
if (!PointeeType(type).isNull()) {
points_to_map_.ExtendPointerPointsToSet(
obj,
{CreateObjectsRecursively(value_lifetimes.GetPointeeLifetimes())});
return obj;
}
// Record type.
if (type->getAs<clang::RecordType>()) {
ForEachFieldAndBase(
type, object_lifetimes,
[this, obj](const ObjectLifetimes& field_lifetimes,
const clang::FieldDecl* f) {
const Object* field = CreateObjectsRecursively(field_lifetimes);
object_repository_.field_object_map_[std::make_pair(obj, f)] =
field;
},
[this, obj](const ObjectLifetimes& base_lifetimes,
const clang::Type* base_type) {
const Object* base_obj = CreateObjectsRecursively(base_lifetimes);
object_repository_
.base_object_map_[std::make_pair(obj, base_type)] = base_obj;
}
);
}
return obj;
}
private:
ObjectRepository& object_repository_;
PointsToMap& points_to_map_;
// We re-use the same Object for all the sub-objects with the same type and
// lifetimes. This avoids infinite loops in the case of structs like lists.
llvm::DenseMap<ObjectLifetimes, const Object*> object_cache_;
};
const Object* ObjectRepository::CreateObject(
const ObjectLifetimes& object_lifetimes, PointsToMap& points_to_map) {
ObjectCreator object_creator(*this, points_to_map);
return object_creator.CreateObjectsRecursively(object_lifetimes);
}
const Object* ObjectRepository::CreateObject(clang::QualType type,
Lifetime root_object_lifetime,
LifetimeFactory lifetime_factory) {
return CreateObject(
ObjectLifetimes(root_object_lifetime,
ValueLifetimes::Create(type, lifetime_factory).get()),
initial_points_to_map_);
}
template <typename... Args>
const Object* ObjectRepository::ConstructObject(Args&&... args) {
return new (object_allocator_.Allocate()) Object(args...);
}
// Clones an object and its base classes and fields, if any.
const Object* ObjectRepository::CloneObject(const Object* object) {
struct ObjectPair {
const Object* orig_object;
const Object* new_object;
};
auto clone = [this](const Object* obj) {
auto new_obj = ConstructObject(obj->GetLifetime(), obj->Type(),
obj->GetFuncLifetimes());
initial_points_to_map_.SetPointerPointsToSet(
new_obj, initial_points_to_map_.GetPointerPointsToSet(obj));
return new_obj;
};
const Object* new_root = clone(object);
initial_object_lifetimes_[new_root] = initial_object_lifetimes_[object];
std::vector<ObjectPair> object_stack{{object, new_root}};
while (!object_stack.empty()) {
auto [orig_object, new_object] = object_stack.back();
assert(orig_object->Type() == new_object->Type());
object_stack.pop_back();
auto record_type = orig_object->Type()->getAs<clang::RecordType>();
if (!record_type) {
continue;
}
// Base classes.
if (auto* cxxrecord =
clang::dyn_cast<clang::CXXRecordDecl>(record_type->getDecl())) {
for (const clang::CXXBaseSpecifier& base : cxxrecord->bases()) {
const Object* base_obj =
GetBaseClassObject(orig_object, base.getType());
const Object* new_base_obj = clone(base_obj);
base_object_map_[std::make_pair(
new_object, base.getType().getCanonicalType().getTypePtr())] =
new_base_obj;
object_stack.push_back(ObjectPair{base_obj, new_base_obj});
}
}
// Fields.
for (auto f : record_type->getDecl()->fields()) {
const Object* field_obj = GetFieldObject(orig_object, f);
const Object* new_field_obj = clone(field_obj);
field_object_map_[std::make_pair(new_object, f)] = new_field_obj;
object_stack.push_back(ObjectPair{field_obj, new_field_obj});
}
}
return new_root;
}
std::optional<const Object*> ObjectRepository::GetFieldObjectInternal(
const Object* struct_object, const clang::FieldDecl* field) const {
auto iter = field_object_map_.find(std::make_pair(struct_object, field));
if (iter != field_object_map_.end()) {
return iter->second;
}
if (auto* cxxrecord = clang::dyn_cast<clang::CXXRecordDecl>(
struct_object->Type()->getAs<clang::RecordType>()->getDecl())) {
for (const clang::CXXBaseSpecifier& base : cxxrecord->bases()) {
std::optional<const Object*> field_object = GetFieldObjectInternal(
GetBaseClassObject(struct_object, base.getType()), field);
if (field_object.has_value()) {
return field_object;
}
}
}
return std::nullopt;
}
} // namespace lifetimes
} // namespace tidy
} // namespace clang