-
Notifications
You must be signed in to change notification settings - Fork 53
/
consume_trigger.go
2156 lines (1873 loc) · 72.4 KB
/
consume_trigger.go
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package annotation
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"go.uber.org/nilaway/util"
"go.uber.org/nilaway/util/orderedmap"
)
// A ConsumingAnnotationTrigger indicated a possible reason that a nil flow to this site would indicate
// an error
//
// All ConsumingAnnotationTriggers must embed one of the following 3 structs:
// -TriggerIfNonnil
// -TriggerIfDeepNonnil
// -ConsumeTriggerTautology
type ConsumingAnnotationTrigger interface {
// CheckConsume can be called to determined whether this trigger should be triggered
// given a particular Annotation map
// for example - an `ArgPass` trigger triggers iff the corresponding function arg has
// nonNil type
CheckConsume(Map) bool
Prestring() Prestring
// Kind returns the kind of the trigger.
Kind() TriggerKind
// UnderlyingSite returns the underlying site this trigger's nilability depends on. If the
// trigger always or never fires, the site is nil.
UnderlyingSite() Key
customPos() (token.Pos, bool)
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
equals(ConsumingAnnotationTrigger) bool
// Copy returns a deep copy of this ConsumingAnnotationTrigger
Copy() ConsumingAnnotationTrigger
// AddAssignment adds an assignment to the trigger for tracking and printing informative error message.
// NilAway's `backpropAcrossOneToOneAssignment()` lifts consumer triggers from the RHS of an assignment to the LHS.
// This implies loss of information about the assignment. This method is used to track such assignments and print
// a more informative error message.
AddAssignment(Assignment)
// NeedsGuard returns true if the trigger needs to be guarded, for example, by a nil check or an ok form.
NeedsGuard() bool
// SetNeedsGuard sets the underlying Guard-Neediness of this ConsumerTrigger, if present.
// Default setting for ConsumerTriggers is that they need a guard. Override this method to set the need for a guard to false.
SetNeedsGuard(bool)
}
// Prestring is an interface used to encode objects that have compact on-the-wire encodings
// (via gob) but can still be expanded into verbose string representations on demand using
// type information. These are key for compact encoding of InferredAnnotationMaps
type Prestring interface {
String() string
}
// Assignment is a struct that represents an assignment to an expression
type Assignment struct {
LHSExprStr string
RHSExprStr string
Position token.Position
}
func (a *Assignment) String() string {
return fmt.Sprintf("`%s` to `%s` at %s", a.RHSExprStr, a.LHSExprStr, a.Position)
}
// assignmentFlow is a struct that represents a flow of assignments.
// Note that we implement a copy method for this struct, since we want to deep copy the assignments map when we copy
// ConsumerTriggers. However, we don't implement an `equals` method for this struct, since it would incur a performance
// penalty in situations where multiple nilable flows reach a dereference site by creating more full triggers and possibly
// more rounds through backpropagation fix point. Consider the following example:
//
// func f(m map[int]*int) {
// var v *int
// var ok1, ok2 bool
// if cond {
// v, ok1 = m[0] // nilable flow 1, ok1 is false
// } else {
// v, ok2 = m[1] // nilable flow 2, ok2 is false
// }
// _, _ = ok1, ok2
// _ = *v // nil panic!
// }
//
// Here `v` can be potentiall nilable from two flows: ok1 or ok2 is false. We would like to print only one error message
// for this situation with one representative flow printed in the error message. However, with an `equals` method, we would
// report multiple error messages, one for each flow, by creating multiple full triggers, thereby affecting performance.
type assignmentFlow struct {
// We use ordered map for `assignments` to maintain the order of assignments in the flow, and also to avoid
// duplicates that can get introduced due to fix point convergence in backpropagation.
assignments *orderedmap.OrderedMap[Assignment, bool]
}
func (a *assignmentFlow) addEntry(entry Assignment) {
if a.assignments == nil {
a.assignments = orderedmap.New[Assignment, bool]()
}
a.assignments.Store(entry, true)
}
func (a *assignmentFlow) copy() assignmentFlow {
if a.assignments == nil {
return assignmentFlow{}
}
assignments := orderedmap.New[Assignment, bool]()
for _, p := range a.assignments.Pairs {
assignments.Store(p.Key, true)
}
return assignmentFlow{assignments: assignments}
}
func (a *assignmentFlow) String() string {
if a.assignments == nil || len(a.assignments.Pairs) == 0 {
return ""
}
// backprop algorithm populates assignment entries in backward order. Reverse entries to get forward order of
// assignments, and store in `strs` slice.
strs := make([]string, 0, len(a.assignments.Pairs))
for i := len(a.assignments.Pairs) - 1; i >= 0; i-- {
strs = append(strs, a.assignments.Pairs[i].Key.String())
}
// build the informative print string tracking the assignments
var sb strings.Builder
sb.WriteString(" via the assignment(s):\n\t\t- ")
sb.WriteString(strings.Join(strs, ",\n\t\t- "))
return sb.String()
}
// TriggerIfNonNil is triggered if the contained Annotation is non-nil
type TriggerIfNonNil struct {
Ann Key
IsGuardNotNeeded bool // ConsumeTriggers need guards by default, when applicable. Set this to true when guards are not needed.
assignmentFlow
}
// Kind returns Conditional.
func (*TriggerIfNonNil) Kind() TriggerKind { return Conditional }
// UnderlyingSite the underlying site this trigger's nilability depends on.
func (t *TriggerIfNonNil) UnderlyingSite() Key { return t.Ann }
// CheckConsume returns true if the underlying annotation is present in the passed map and nonnil
func (t *TriggerIfNonNil) CheckConsume(annMap Map) bool {
ann, ok := t.Ann.Lookup(annMap)
return ok && !ann.IsNilable
}
// customPos has the below default implementation for TriggerIfNonNil, in which case ConsumeTrigger.Pos() will return a default value.
// To return non-default position values, this method should be overridden appropriately.
func (*TriggerIfNonNil) customPos() (token.Pos, bool) { return token.NoPos, false }
// NeedsGuard is the default implementation for TriggerIfNonNil. To return non-default value, this method should be overridden.
func (t *TriggerIfNonNil) NeedsGuard() bool { return !t.IsGuardNotNeeded }
// SetNeedsGuard sets the underlying Guard-Neediness of this ConsumerTrigger
func (t *TriggerIfNonNil) SetNeedsGuard(b bool) {
t.IsGuardNotNeeded = !b
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (t *TriggerIfNonNil) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*TriggerIfNonNil); ok {
return t.Ann.equals(other.Ann) && t.IsGuardNotNeeded == other.IsGuardNotNeeded
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (t *TriggerIfNonNil) Copy() ConsumingAnnotationTrigger {
copyConsumer := *t
copyConsumer.Ann = t.Ann.copy()
copyConsumer.assignmentFlow = t.assignmentFlow.copy()
return ©Consumer
}
// AddAssignment adds an assignment to the trigger.
func (t *TriggerIfNonNil) AddAssignment(e Assignment) {
t.assignmentFlow.addEntry(e)
}
// Prestring returns this Prestring as a Prestring
func (t *TriggerIfNonNil) Prestring() Prestring {
return TriggerIfNonNilPrestring{
AssignmentStr: t.assignmentFlow.String(),
}
}
// TriggerIfNonNilPrestring is a Prestring storing the needed information to compactly encode a TriggerIfNonNil
type TriggerIfNonNilPrestring struct {
AssignmentStr string
}
func (t TriggerIfNonNilPrestring) String() string {
var sb strings.Builder
sb.WriteString("nonnil value")
sb.WriteString(t.AssignmentStr)
return sb.String()
}
// TriggerIfDeepNonNil is triggered if the contained Annotation is deeply non-nil
type TriggerIfDeepNonNil struct {
Ann Key
IsGuardNotNeeded bool // ConsumeTriggers need guards by default, when applicable. Set this to true when guards are not needed.
assignmentFlow
}
// Kind returns DeepConditional.
func (*TriggerIfDeepNonNil) Kind() TriggerKind { return DeepConditional }
// UnderlyingSite the underlying site this trigger's nilability depends on.
func (t *TriggerIfDeepNonNil) UnderlyingSite() Key { return t.Ann }
// CheckConsume returns true if the underlying annotation is present in the passed map and deeply nonnil
func (t *TriggerIfDeepNonNil) CheckConsume(annMap Map) bool {
ann, ok := t.Ann.Lookup(annMap)
return ok && !ann.IsDeepNilable
}
// customPos has the below default implementation for TriggerIfDeepNonNil, in which case ConsumeTrigger.Pos() will return a default value.
// To return non-default position values, this method should be overridden appropriately.
func (*TriggerIfDeepNonNil) customPos() (token.Pos, bool) { return token.NoPos, false }
// NeedsGuard default implementation for TriggerIfDeepNonNil. To return non-default value, this method should be overridden.
func (t *TriggerIfDeepNonNil) NeedsGuard() bool { return !t.IsGuardNotNeeded }
// SetNeedsGuard sets the underlying Guard-Neediness of this ConsumerTrigger
func (t *TriggerIfDeepNonNil) SetNeedsGuard(b bool) {
t.IsGuardNotNeeded = !b
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (t *TriggerIfDeepNonNil) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*TriggerIfDeepNonNil); ok {
return t.Ann.equals(other.Ann) && t.IsGuardNotNeeded == other.IsGuardNotNeeded
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (t *TriggerIfDeepNonNil) Copy() ConsumingAnnotationTrigger {
copyConsumer := *t
copyConsumer.Ann = t.Ann.copy()
copyConsumer.assignmentFlow = t.assignmentFlow.copy()
return ©Consumer
}
// AddAssignment adds an assignment to the trigger.
func (t *TriggerIfDeepNonNil) AddAssignment(e Assignment) {
t.assignmentFlow.addEntry(e)
}
// Prestring returns this Prestring as a Prestring
func (t *TriggerIfDeepNonNil) Prestring() Prestring {
return TriggerIfDeepNonNilPrestring{
AssignmentStr: t.assignmentFlow.String(),
}
}
// TriggerIfDeepNonNilPrestring is a Prestring storing the needed information to compactly encode a TriggerIfDeepNonNil
type TriggerIfDeepNonNilPrestring struct {
AssignmentStr string
}
func (t TriggerIfDeepNonNilPrestring) String() string {
var sb strings.Builder
sb.WriteString("deeply nonnil value")
sb.WriteString(t.AssignmentStr)
return sb.String()
}
// ConsumeTriggerTautology is used at consumption sites were consuming nil is always an error
type ConsumeTriggerTautology struct {
IsGuardNotNeeded bool // ConsumeTriggers need guards by default, when applicable. Set this to true when guards are not needed.
assignmentFlow
}
// Kind returns Always.
func (*ConsumeTriggerTautology) Kind() TriggerKind { return Always }
// UnderlyingSite always returns nil.
func (*ConsumeTriggerTautology) UnderlyingSite() Key { return nil }
// CheckConsume returns true
func (*ConsumeTriggerTautology) CheckConsume(Map) bool { return true }
// customPos has the below default implementation for ConsumeTriggerTautology, in which case ConsumeTrigger.Pos() will return a default value.
// To return non-default position values, this method should be overridden appropriately.
func (*ConsumeTriggerTautology) customPos() (token.Pos, bool) { return token.NoPos, false }
// NeedsGuard default implementation for ConsumeTriggerTautology. To return non-default value, this method should be overridden.
func (c *ConsumeTriggerTautology) NeedsGuard() bool { return !c.IsGuardNotNeeded }
// SetNeedsGuard sets the underlying Guard-Neediness of this ConsumerTrigger
func (c *ConsumeTriggerTautology) SetNeedsGuard(b bool) {
c.IsGuardNotNeeded = !b
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (c *ConsumeTriggerTautology) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*ConsumeTriggerTautology); ok {
return c.IsGuardNotNeeded == other.IsGuardNotNeeded
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (c *ConsumeTriggerTautology) Copy() ConsumingAnnotationTrigger {
copyConsumer := *c
copyConsumer.assignmentFlow = c.assignmentFlow.copy()
return ©Consumer
}
// AddAssignment adds an assignment to the trigger.
func (c *ConsumeTriggerTautology) AddAssignment(e Assignment) {
c.assignmentFlow.addEntry(e)
}
// Prestring returns this Prestring as a Prestring
func (c *ConsumeTriggerTautology) Prestring() Prestring {
return ConsumeTriggerTautologyPrestring{
AssignmentStr: c.assignmentFlow.String(),
}
}
// ConsumeTriggerTautologyPrestring is a Prestring storing the needed information to compactly encode a ConsumeTriggerTautology
type ConsumeTriggerTautologyPrestring struct {
AssignmentStr string
}
func (c ConsumeTriggerTautologyPrestring) String() string {
var sb strings.Builder
sb.WriteString("must be nonnil")
sb.WriteString(c.AssignmentStr)
return sb.String()
}
// PtrLoad is when a value flows to a point where it is loaded as a pointer
type PtrLoad struct {
*ConsumeTriggerTautology
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (p *PtrLoad) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*PtrLoad); ok {
return p.ConsumeTriggerTautology.equals(other.ConsumeTriggerTautology)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (p *PtrLoad) Copy() ConsumingAnnotationTrigger {
copyConsumer := *p
copyConsumer.ConsumeTriggerTautology = p.ConsumeTriggerTautology.Copy().(*ConsumeTriggerTautology)
return ©Consumer
}
// Prestring returns this PtrLoad as a Prestring
func (p *PtrLoad) Prestring() Prestring {
return PtrLoadPrestring{
AssignmentStr: p.assignmentFlow.String(),
}
}
// PtrLoadPrestring is a Prestring storing the needed information to compactly encode a PtrLoad
type PtrLoadPrestring struct {
AssignmentStr string
}
func (p PtrLoadPrestring) String() string {
var sb strings.Builder
sb.WriteString("dereferenced")
sb.WriteString(p.AssignmentStr)
return sb.String()
}
// MapAccess is when a map value flows to a point where it is indexed, and thus must be non-nil
//
// note: this trigger is produced only if config.ErrorOnNilableMapRead == true
type MapAccess struct {
*ConsumeTriggerTautology
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (i *MapAccess) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*MapAccess); ok {
return i.ConsumeTriggerTautology.equals(other.ConsumeTriggerTautology)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (i *MapAccess) Copy() ConsumingAnnotationTrigger {
copyConsumer := *i
copyConsumer.ConsumeTriggerTautology = i.ConsumeTriggerTautology.Copy().(*ConsumeTriggerTautology)
return ©Consumer
}
// Prestring returns this MapAccess as a Prestring
func (i *MapAccess) Prestring() Prestring {
return MapAccessPrestring{
AssignmentStr: i.assignmentFlow.String(),
}
}
// MapAccessPrestring is a Prestring storing the needed information to compactly encode a MapAccess
type MapAccessPrestring struct {
AssignmentStr string
}
func (i MapAccessPrestring) String() string {
var sb strings.Builder
sb.WriteString("keyed into")
sb.WriteString(i.AssignmentStr)
return sb.String()
}
// MapWrittenTo is when a map value flows to a point where one of its indices is written to, and thus
// must be non-nil
type MapWrittenTo struct {
*ConsumeTriggerTautology
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (m *MapWrittenTo) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*MapWrittenTo); ok {
return m.ConsumeTriggerTautology.equals(other.ConsumeTriggerTautology)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (m *MapWrittenTo) Copy() ConsumingAnnotationTrigger {
copyConsumer := *m
copyConsumer.ConsumeTriggerTautology = m.ConsumeTriggerTautology.Copy().(*ConsumeTriggerTautology)
return ©Consumer
}
// Prestring returns this MapWrittenTo as a Prestring
func (m *MapWrittenTo) Prestring() Prestring {
return MapWrittenToPrestring{
AssignmentStr: m.assignmentFlow.String(),
}
}
// MapWrittenToPrestring is a Prestring storing the needed information to compactly encode a MapWrittenTo
type MapWrittenToPrestring struct {
AssignmentStr string
}
func (m MapWrittenToPrestring) String() string {
var sb strings.Builder
sb.WriteString("written to at an index")
sb.WriteString(m.AssignmentStr)
return sb.String()
}
// SliceAccess is when a slice value flows to a point where it is sliced, and thus must be non-nil
type SliceAccess struct {
*ConsumeTriggerTautology
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (s *SliceAccess) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*SliceAccess); ok {
return s.ConsumeTriggerTautology.equals(other.ConsumeTriggerTautology)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (s *SliceAccess) Copy() ConsumingAnnotationTrigger {
copyConsumer := *s
copyConsumer.ConsumeTriggerTautology = s.ConsumeTriggerTautology.Copy().(*ConsumeTriggerTautology)
return ©Consumer
}
// Prestring returns this SliceAccess as a Prestring
func (s *SliceAccess) Prestring() Prestring {
return SliceAccessPrestring{
AssignmentStr: s.assignmentFlow.String(),
}
}
// SliceAccessPrestring is a Prestring storing the needed information to compactly encode a SliceAccess
type SliceAccessPrestring struct {
AssignmentStr string
}
func (s SliceAccessPrestring) String() string {
var sb strings.Builder
sb.WriteString("sliced into")
sb.WriteString(s.AssignmentStr)
return sb.String()
}
// FldAccess is when a value flows to a point where a field of it is accessed, and so it must be non-nil
type FldAccess struct {
*ConsumeTriggerTautology
Sel types.Object
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (f *FldAccess) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*FldAccess); ok {
return f.ConsumeTriggerTautology.equals(other.ConsumeTriggerTautology) && f.Sel == other.Sel
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (f *FldAccess) Copy() ConsumingAnnotationTrigger {
copyConsumer := *f
copyConsumer.ConsumeTriggerTautology = f.ConsumeTriggerTautology.Copy().(*ConsumeTriggerTautology)
return ©Consumer
}
// Prestring returns this FldAccess as a Prestring
func (f *FldAccess) Prestring() Prestring {
fieldName, methodName := "", ""
switch t := f.Sel.(type) {
case *types.Var:
fieldName = t.Name()
case *types.Func:
methodName = t.Name()
default:
panic(fmt.Sprintf("unexpected Sel type %T in FldAccess", t))
}
return FldAccessPrestring{
FieldName: fieldName,
MethodName: methodName,
AssignmentStr: f.assignmentFlow.String(),
}
}
// FldAccessPrestring is a Prestring storing the needed information to compactly encode a FldAccess
type FldAccessPrestring struct {
FieldName string
MethodName string
AssignmentStr string
}
func (f FldAccessPrestring) String() string {
var sb strings.Builder
if f.MethodName != "" {
sb.WriteString(fmt.Sprintf("called `%s()`", f.MethodName))
} else {
sb.WriteString(fmt.Sprintf("accessed field `%s`", f.FieldName))
}
sb.WriteString(f.AssignmentStr)
return sb.String()
}
// UseAsErrorResult is when a value flows to the error result of a function, where it is expected to be non-nil
type UseAsErrorResult struct {
*TriggerIfNonNil
RetStmt *ast.ReturnStmt
IsNamedReturn bool
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (u *UseAsErrorResult) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*UseAsErrorResult); ok {
return u.TriggerIfNonNil.equals(other.TriggerIfNonNil) &&
u.RetStmt == other.RetStmt &&
u.IsNamedReturn == other.IsNamedReturn
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (u *UseAsErrorResult) Copy() ConsumingAnnotationTrigger {
copyConsumer := *u
copyConsumer.TriggerIfNonNil = u.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this UseAsErrorResult as a Prestring
func (u *UseAsErrorResult) Prestring() Prestring {
retAnn := u.Ann.(*RetAnnotationKey)
return UseAsErrorResultPrestring{
Pos: retAnn.RetNum,
ReturningFuncStr: retAnn.FuncDecl.Name(),
IsNamedReturn: u.IsNamedReturn,
RetName: retAnn.FuncDecl.Type().(*types.Signature).Results().At(retAnn.RetNum).Name(),
AssignmentStr: u.assignmentFlow.String(),
}
}
// UseAsErrorResultPrestring is a Prestring storing the needed information to compactly encode a UseAsErrorResult
type UseAsErrorResultPrestring struct {
Pos int
ReturningFuncStr string
IsNamedReturn bool
RetName string
AssignmentStr string
}
func (u UseAsErrorResultPrestring) String() string {
var sb strings.Builder
if u.IsNamedReturn {
sb.WriteString(fmt.Sprintf("returned as named error result `%s` of `%s()`", u.RetName, u.ReturningFuncStr))
} else {
sb.WriteString(fmt.Sprintf("returned as error result %d of `%s()`", u.Pos, u.ReturningFuncStr))
}
sb.WriteString(u.AssignmentStr)
return sb.String()
}
// overriding position value to point to the raw return statement, which is the source of the potential error
func (u *UseAsErrorResult) customPos() (token.Pos, bool) {
if u.IsNamedReturn {
return u.RetStmt.Pos(), true
}
return 0, false
}
// FldAssign is when a value flows to a point where it is assigned into a field
type FldAssign struct {
*TriggerIfNonNil
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (f *FldAssign) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*FldAssign); ok {
return f.TriggerIfNonNil.equals(other.TriggerIfNonNil)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (f *FldAssign) Copy() ConsumingAnnotationTrigger {
copyConsumer := *f
copyConsumer.TriggerIfNonNil = f.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this FldAssign as a Prestring
func (f *FldAssign) Prestring() Prestring {
fldAnn := f.Ann.(*FieldAnnotationKey)
return FldAssignPrestring{
FieldName: fldAnn.FieldDecl.Name(),
AssignmentStr: f.assignmentFlow.String(),
}
}
// FldAssignPrestring is a Prestring storing the needed information to compactly encode a FldAssign
type FldAssignPrestring struct {
FieldName string
AssignmentStr string
}
func (f FldAssignPrestring) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("assigned into field `%s`", f.FieldName))
sb.WriteString(f.AssignmentStr)
return sb.String()
}
// ArgFldPass is when a struct field value (A.f) flows to a point where it is passed to a function with a param of
// the same struct type (A)
type ArgFldPass struct {
*TriggerIfNonNil
IsPassed bool
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (f *ArgFldPass) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*ArgFldPass); ok {
return f.TriggerIfNonNil.equals(other.TriggerIfNonNil) && f.IsPassed == other.IsPassed
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (f *ArgFldPass) Copy() ConsumingAnnotationTrigger {
copyConsumer := *f
copyConsumer.TriggerIfNonNil = f.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this ArgFldPass as a Prestring
func (f *ArgFldPass) Prestring() Prestring {
ann := f.Ann.(*ParamFieldAnnotationKey)
recvName := ""
if ann.IsReceiver() {
recvName = ann.FuncDecl.Type().(*types.Signature).Recv().Name()
}
return ArgFldPassPrestring{
FieldName: ann.FieldDecl.Name(),
FuncName: ann.FuncDecl.Name(),
ParamNum: ann.ParamNum,
RecvName: recvName,
IsPassed: f.IsPassed,
AssignmentStr: f.assignmentFlow.String(),
}
}
// ArgFldPassPrestring is a Prestring storing the needed information to compactly encode a ArgFldPass
type ArgFldPassPrestring struct {
FieldName string
FuncName string
ParamNum int
RecvName string
IsPassed bool
AssignmentStr string
}
func (f ArgFldPassPrestring) String() string {
var sb strings.Builder
prefix := ""
if f.IsPassed {
prefix = "assigned to "
}
if len(f.RecvName) > 0 {
sb.WriteString(fmt.Sprintf("%sfield `%s` of method receiver `%s`", prefix, f.FieldName, f.RecvName))
} else {
sb.WriteString(fmt.Sprintf("%sfield `%s` of argument %d to `%s()`", prefix, f.FieldName, f.ParamNum, f.FuncName))
}
sb.WriteString(f.AssignmentStr)
return sb.String()
}
// GlobalVarAssign is when a value flows to a point where it is assigned into a global variable
type GlobalVarAssign struct {
*TriggerIfNonNil
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (g *GlobalVarAssign) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*GlobalVarAssign); ok {
return g.TriggerIfNonNil.equals(other.TriggerIfNonNil)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (g *GlobalVarAssign) Copy() ConsumingAnnotationTrigger {
copyConsumer := *g
copyConsumer.TriggerIfNonNil = g.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this GlobalVarAssign as a Prestring
func (g *GlobalVarAssign) Prestring() Prestring {
varAnn := g.Ann.(*GlobalVarAnnotationKey)
return GlobalVarAssignPrestring{
VarName: varAnn.VarDecl.Name(),
AssignmentStr: g.assignmentFlow.String(),
}
}
// GlobalVarAssignPrestring is a Prestring storing the needed information to compactly encode a GlobalVarAssign
type GlobalVarAssignPrestring struct {
VarName string
AssignmentStr string
}
func (g GlobalVarAssignPrestring) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("assigned into global variable `%s`", g.VarName))
sb.WriteString(g.AssignmentStr)
return sb.String()
}
// ArgPass is when a value flows to a point where it is passed as an argument to a function. This
// consumer trigger can be used on top of two different sites: ParamAnnotationKey &
// CallSiteParamAnnotationKey. ParamAnnotationKey is the parameter site in the function
// declaration; CallSiteParamAnnotationKey is the argument site in the call expression.
// CallSiteParamAnnotationKey is specifically used for functions with contracts since we need to
// duplicate the sites for context sensitivity.
type ArgPass struct {
*TriggerIfNonNil
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (a *ArgPass) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*ArgPass); ok {
return a.TriggerIfNonNil.equals(other.TriggerIfNonNil)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (a *ArgPass) Copy() ConsumingAnnotationTrigger {
copyConsumer := *a
copyConsumer.TriggerIfNonNil = a.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this ArgPass as a Prestring
func (a *ArgPass) Prestring() Prestring {
switch key := a.Ann.(type) {
case *ParamAnnotationKey:
return ArgPassPrestring{
ParamName: key.MinimalString(),
FuncName: key.FuncDecl.Name(),
Location: "",
AssignmentStr: a.assignmentFlow.String(),
}
case *CallSiteParamAnnotationKey:
return ArgPassPrestring{
ParamName: key.MinimalString(),
FuncName: key.FuncDecl.Name(),
Location: key.Location.String(),
AssignmentStr: a.assignmentFlow.String(),
}
default:
panic(fmt.Sprintf(
"Expected ParamAnnotationKey or CallSiteParamAnnotationKey but got: %T", key))
}
}
// ArgPassPrestring is a Prestring storing the needed information to compactly encode a ArgPass
type ArgPassPrestring struct {
ParamName string
FuncName string
// Location points to the code location of the argument pass at the call site for a ArgPass
// enclosing CallSiteParamAnnotationKey; Location is empty for a ArgPass enclosing ParamAnnotationKey.
Location string
AssignmentStr string
}
func (a ArgPassPrestring) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("passed as %s to `%s()`", a.ParamName, a.FuncName))
if a.Location != "" {
sb.WriteString(fmt.Sprintf(" at %s", a.Location))
}
sb.WriteString(a.AssignmentStr)
return sb.String()
}
// ArgPassDeep is when a value deeply flows to a point where it is passed as an argument to a function
type ArgPassDeep struct {
*TriggerIfDeepNonNil
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (a *ArgPassDeep) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*ArgPassDeep); ok {
return a.TriggerIfDeepNonNil.equals(other.TriggerIfDeepNonNil)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (a *ArgPassDeep) Copy() ConsumingAnnotationTrigger {
copyConsumer := *a
copyConsumer.TriggerIfDeepNonNil = a.TriggerIfDeepNonNil.Copy().(*TriggerIfDeepNonNil)
return ©Consumer
}
// Prestring returns this ArgPassDeep as a Prestring
func (a *ArgPassDeep) Prestring() Prestring {
switch key := a.Ann.(type) {
case *ParamAnnotationKey:
return ArgPassPrestring{
ParamName: key.MinimalString(),
FuncName: key.FuncDecl.Name(),
Location: "",
AssignmentStr: a.assignmentFlow.String(),
}
case *CallSiteParamAnnotationKey:
return ArgPassPrestring{
ParamName: key.MinimalString(),
FuncName: key.FuncDecl.Name(),
Location: key.Location.String(),
AssignmentStr: a.assignmentFlow.String(),
}
default:
panic(fmt.Sprintf(
"Expected ParamAnnotationKey or CallSiteParamAnnotationKey but got: %T", key))
}
}
// ArgPassDeepPrestring is a Prestring storing the needed information to compactly encode a ArgPassDeep
type ArgPassDeepPrestring struct {
ParamName string
FuncName string
// Location points to the code location of the argument pass at the call site for a ArgPass
// enclosing CallSiteParamAnnotationKey; Location is empty for a ArgPass enclosing ParamAnnotationKey.
Location string
AssignmentStr string
}
func (a ArgPassDeepPrestring) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("passed deeply as %s to `%s()`", a.ParamName, a.FuncName))
if a.Location != "" {
sb.WriteString(fmt.Sprintf(" at %s", a.Location))
}
sb.WriteString(a.AssignmentStr)
return sb.String()
}
// RecvPass is when a receiver value flows to a point where it is used to invoke a method.
// E.g., `s.foo()`, here `s` is a receiver and forms the RecvPass Consumer
type RecvPass struct {
*TriggerIfNonNil
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (a *RecvPass) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*RecvPass); ok {
return a.TriggerIfNonNil.equals(other.TriggerIfNonNil)
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (a *RecvPass) Copy() ConsumingAnnotationTrigger {
copyConsumer := *a
copyConsumer.TriggerIfNonNil = a.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this RecvPass as a Prestring
func (a *RecvPass) Prestring() Prestring {
recvAnn := a.Ann.(*RecvAnnotationKey)
return RecvPassPrestring{
FuncName: recvAnn.FuncDecl.Name(),
AssignmentStr: a.assignmentFlow.String(),
}
}
// RecvPassPrestring is a Prestring storing the needed information to compactly encode a RecvPass
type RecvPassPrestring struct {
FuncName string
AssignmentStr string
}
func (a RecvPassPrestring) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("used as receiver to call `%s()`", a.FuncName))
sb.WriteString(a.AssignmentStr)
return sb.String()
}
// InterfaceResultFromImplementation is when a result is determined to flow from a concrete method to an interface method via implementation
type InterfaceResultFromImplementation struct {
*TriggerIfNonNil
AffiliationPair
}
// equals returns true if the passed ConsumingAnnotationTrigger is equal to this one
func (i *InterfaceResultFromImplementation) equals(other ConsumingAnnotationTrigger) bool {
if other, ok := other.(*InterfaceResultFromImplementation); ok {
return i.TriggerIfNonNil.equals(other.TriggerIfNonNil) &&
i.AffiliationPair.InterfaceMethod == other.AffiliationPair.InterfaceMethod &&
i.AffiliationPair.ImplementingMethod == other.AffiliationPair.ImplementingMethod
}
return false
}
// Copy returns a deep copy of this ConsumingAnnotationTrigger
func (i *InterfaceResultFromImplementation) Copy() ConsumingAnnotationTrigger {
copyConsumer := *i
copyConsumer.TriggerIfNonNil = i.TriggerIfNonNil.Copy().(*TriggerIfNonNil)
return ©Consumer
}
// Prestring returns this InterfaceResultFromImplementation as a Prestring
func (i *InterfaceResultFromImplementation) Prestring() Prestring {
retAnn := i.Ann.(*RetAnnotationKey)
return InterfaceResultFromImplementationPrestring{
retAnn.RetNum,
util.PartiallyQualifiedFuncName(retAnn.FuncDecl),
util.PartiallyQualifiedFuncName(i.ImplementingMethod),
i.assignmentFlow.String(),