@@ -113,22 +113,12 @@ private newtype TDefOrUseImpl =
113
113
TGlobalUse ( GlobalLikeVariable v , IRFunction f , int indirectionIndex ) {
114
114
// Represents a final "use" of a global variable to ensure that
115
115
// the assignment to a global variable isn't ruled out as dead.
116
- exists ( VariableAddressInstruction vai , int defIndex |
117
- vai .getEnclosingIRFunction ( ) = f and
118
- vai .getAstVariable ( ) = v and
119
- isDef ( _, _, _, vai , _, defIndex ) and
120
- indirectionIndex = [ 0 .. defIndex ] + 1
121
- )
116
+ isGlobalUse ( v , f , _, indirectionIndex )
122
117
} or
123
118
TGlobalDefImpl ( GlobalLikeVariable v , IRFunction f , int indirectionIndex ) {
124
119
// Represents the initial "definition" of a global variable when entering
125
120
// a function body.
126
- exists ( VariableAddressInstruction vai |
127
- vai .getEnclosingIRFunction ( ) = f and
128
- vai .getAstVariable ( ) = v and
129
- isUse ( _, _, vai , _, indirectionIndex ) and
130
- not isDef ( _, _, vai .getAUse ( ) , _, _, _)
131
- )
121
+ isGlobalDefImpl ( v , f , _, indirectionIndex )
132
122
} or
133
123
TIteratorDef (
134
124
Operand iteratorDerefAddress , BaseSourceVariableInstruction container , int indirectionIndex
@@ -150,6 +140,27 @@ private newtype TDefOrUseImpl =
150
140
)
151
141
}
152
142
143
+ private predicate isGlobalUse (
144
+ GlobalLikeVariable v , IRFunction f , int indirection , int indirectionIndex
145
+ ) {
146
+ exists ( VariableAddressInstruction vai |
147
+ vai .getEnclosingIRFunction ( ) = f and
148
+ vai .getAstVariable ( ) = v and
149
+ isDef ( _, _, _, vai , indirection , indirectionIndex )
150
+ )
151
+ }
152
+
153
+ private predicate isGlobalDefImpl (
154
+ GlobalLikeVariable v , IRFunction f , int indirection , int indirectionIndex
155
+ ) {
156
+ exists ( VariableAddressInstruction vai |
157
+ vai .getEnclosingIRFunction ( ) = f and
158
+ vai .getAstVariable ( ) = v and
159
+ isUse ( _, _, vai , indirection , indirectionIndex ) and
160
+ not isDef ( _, _, _, vai , _, indirectionIndex )
161
+ )
162
+ }
163
+
153
164
private predicate unspecifiedTypeIsModifiableAt ( Type unspecified , int indirectionIndex ) {
154
165
indirectionIndex = [ 1 .. getIndirectionForUnspecifiedType ( unspecified ) .getNumberOfIndirections ( ) ] and
155
166
exists ( CppType cppType |
@@ -438,7 +449,7 @@ class GlobalUse extends UseImpl, TGlobalUse {
438
449
439
450
override FinalGlobalValue getNode ( ) { result .getGlobalUse ( ) = this }
440
451
441
- override int getIndirection ( ) { result = ind + 1 }
452
+ override int getIndirection ( ) { isGlobalUse ( global , f , result , ind ) }
442
453
443
454
/** Gets the global variable associated with this use. */
444
455
GlobalLikeVariable getVariable ( ) { result = global }
@@ -460,7 +471,9 @@ class GlobalUse extends UseImpl, TGlobalUse {
460
471
)
461
472
}
462
473
463
- override SourceVariable getSourceVariable ( ) { sourceVariableIsGlobal ( result , global , f , ind ) }
474
+ override SourceVariable getSourceVariable ( ) {
475
+ sourceVariableIsGlobal ( result , global , f , this .getIndirection ( ) )
476
+ }
464
477
465
478
final override Cpp:: Location getLocation ( ) { result = f .getLocation ( ) }
466
479
@@ -501,16 +514,18 @@ class GlobalDefImpl extends DefOrUseImpl, TGlobalDefImpl {
501
514
502
515
/** Gets the global variable associated with this definition. */
503
516
override SourceVariable getSourceVariable ( ) {
504
- sourceVariableIsGlobal ( result , global , f , indirectionIndex )
517
+ sourceVariableIsGlobal ( result , global , f , this . getIndirection ( ) )
505
518
}
506
519
520
+ int getIndirection ( ) { result = indirectionIndex }
521
+
507
522
/**
508
523
* Gets the type of this use after specifiers have been deeply stripped
509
524
* and typedefs have been resolved.
510
525
*/
511
526
Type getUnspecifiedType ( ) { result = global .getUnspecifiedType ( ) }
512
527
513
- override string toString ( ) { result = "GlobalDef" }
528
+ override string toString ( ) { result = "Def of " + this . getSourceVariable ( ) }
514
529
515
530
override Location getLocation ( ) { result = f .getLocation ( ) }
516
531
@@ -980,7 +995,7 @@ class GlobalDef extends TGlobalDef, SsaDefOrUse {
980
995
final override Location getLocation ( ) { result = global .getLocation ( ) }
981
996
982
997
/** Gets a textual representation of this definition. */
983
- override string toString ( ) { result = "GlobalDef" }
998
+ override string toString ( ) { result = global . toString ( ) }
984
999
985
1000
/**
986
1001
* Holds if this definition has index `index` in block `block`, and
@@ -990,6 +1005,9 @@ class GlobalDef extends TGlobalDef, SsaDefOrUse {
990
1005
global .hasIndexInBlock ( block , index , sv )
991
1006
}
992
1007
1008
+ /** Gets the indirection index of this definition. */
1009
+ int getIndirection ( ) { result = global .getIndirection ( ) }
1010
+
993
1011
/** Gets the indirection index of this definition. */
994
1012
int getIndirectionIndex ( ) { result = global .getIndirectionIndex ( ) }
995
1013
0 commit comments