Skip to content

Commit 3d445be

Browse files
committed
C++: Small refactor.
1 parent e0a7889 commit 3d445be

File tree

1 file changed

+52
-48
lines changed
  • cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal

1 file changed

+52
-48
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaImpl.qll

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,8 @@ abstract class DefImpl extends TDefImpl {
223223
*/
224224
abstract int getIndirection();
225225

226-
/**
227-
* Gets the base source variable (i.e., the variable without
228-
* any indirection) of this definition or use.
229-
*/
230-
abstract BaseSourceVariable getBaseSourceVariable();
231-
232226
/** Gets the variable that is defined or used. */
233-
SourceVariable getSourceVariable() {
234-
exists(BaseSourceVariable v, int indirection |
235-
sourceVariableHasBaseAndIndex(result, v, indirection) and
236-
defHasSourceVariable(this, v, indirection)
237-
)
238-
}
227+
abstract SourceVariable getSourceVariable();
239228

240229
/**
241230
* Holds if this definition is guaranteed to totally overwrite the
@@ -293,19 +282,8 @@ abstract class UseImpl extends TUseImpl {
293282
/** Gets the indirection index of this use. */
294283
final int getIndirectionIndex() { result = indirectionIndex }
295284

296-
/**
297-
* Gets the base source variable (i.e., the variable without
298-
* any indirection) of this definition or use.
299-
*/
300-
abstract BaseSourceVariable getBaseSourceVariable();
301-
302285
/** Gets the variable that is defined or used. */
303-
SourceVariable getSourceVariable() {
304-
exists(BaseSourceVariable v, int indirection |
305-
sourceVariableHasBaseAndIndex(result, v, indirection) and
306-
useHasSourceVariable(this, v, indirection)
307-
)
308-
}
286+
abstract SourceVariable getSourceVariable();
309287

310288
/**
311289
* Holds if this use is guaranteed to read the
@@ -314,18 +292,6 @@ abstract class UseImpl extends TUseImpl {
314292
abstract predicate isCertain();
315293
}
316294

317-
pragma[noinline]
318-
private predicate defHasSourceVariable(DefImpl def, BaseSourceVariable bv, int ind) {
319-
bv = def.getBaseSourceVariable() and
320-
ind = def.getIndirection()
321-
}
322-
323-
pragma[noinline]
324-
private predicate useHasSourceVariable(UseImpl use, BaseSourceVariable bv, int ind) {
325-
bv = use.getBaseSourceVariable() and
326-
ind = use.getIndirection()
327-
}
328-
329295
pragma[noinline]
330296
private predicate sourceVariableHasBaseAndIndex(SourceVariable v, BaseSourceVariable bv, int ind) {
331297
v.getBaseVariable() = bv and
@@ -366,8 +332,6 @@ abstract private class DefAddressImpl extends DefImpl, TDefAddressImpl {
366332
result.getBaseVariable() = v and
367333
result.getIndirection() = 0
368334
}
369-
370-
final override BaseSourceVariable getBaseSourceVariable() { result = v }
371335
}
372336

373337
private class DefVariableAddressImpl extends DefAddressImpl {
@@ -413,8 +377,17 @@ private class DirectDef extends DefImpl, TDirectDefImpl {
413377
isDef(_, _, address, result, _, indirectionIndex)
414378
}
415379

416-
override BaseSourceVariable getBaseSourceVariable() {
417-
result = this.getBase().getBaseSourceVariable()
380+
pragma[nomagic]
381+
private predicate hasBaseSourceVariableAndIndirection(BaseSourceVariable v, int indirection) {
382+
v = this.getBase().getBaseSourceVariable() and
383+
indirection = this.getIndirection()
384+
}
385+
386+
final override SourceVariable getSourceVariable() {
387+
exists(BaseSourceVariable v, int indirection |
388+
sourceVariableHasBaseAndIndex(result, v, indirection) and
389+
this.hasBaseSourceVariableAndIndirection(v, indirection)
390+
)
418391
}
419392

420393
override int getIndirection() { isDef(_, _, address, _, result, indirectionIndex) }
@@ -437,8 +410,17 @@ private class DirectUseImpl extends UseImpl, TDirectUseImpl {
437410

438411
private BaseSourceVariableInstruction getBase() { isUse(_, operand, result, _, indirectionIndex) }
439412

440-
override BaseSourceVariable getBaseSourceVariable() {
441-
result = this.getBase().getBaseSourceVariable()
413+
pragma[nomagic]
414+
private predicate hasBaseSourceVariableAndIndirection(BaseSourceVariable bv, int indirection) {
415+
this.getBase().getBaseSourceVariable() = bv and
416+
this.getIndirection() = indirection
417+
}
418+
419+
override SourceVariable getSourceVariable() {
420+
exists(BaseSourceVariable v, int indirection |
421+
sourceVariableHasBaseAndIndex(result, v, indirection) and
422+
this.hasBaseSourceVariableAndIndirection(v, indirection)
423+
)
442424
}
443425

444426
final Operand getOperand() { result = operand }
@@ -516,7 +498,18 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
516498
result instanceof UnknownLocation
517499
}
518500

519-
override BaseIRVariable getBaseSourceVariable() { result.getIRVariable().getAst() = p }
501+
pragma[nomagic]
502+
private predicate hasBaseSourceVariableAndIndirectrion(BaseIRVariable v, int indirection) {
503+
v.getIRVariable().getAst() = p and
504+
indirection = this.getIndirection()
505+
}
506+
507+
override SourceVariable getSourceVariable() {
508+
exists(BaseIRVariable v, int indirection |
509+
sourceVariableHasBaseAndIndex(result, v, indirection) and
510+
this.hasBaseSourceVariableAndIndirectrion(v, indirection)
511+
)
512+
}
520513
}
521514

522515
/**
@@ -596,8 +589,17 @@ class GlobalUse extends UseImpl, TGlobalUse {
596589
hasReturnPosition(f, block, index)
597590
}
598591

599-
override BaseSourceVariable getBaseSourceVariable() {
600-
baseSourceVariableIsGlobal(result, global, f)
592+
pragma[nomagic]
593+
private predicate hasBaseSourceVariableAndIndirection(BaseIRVariable v, int indirection) {
594+
baseSourceVariableIsGlobal(v, global, f) and
595+
indirection = this.getIndirection()
596+
}
597+
598+
override SourceVariable getSourceVariable() {
599+
exists(BaseIRVariable v, int indirection |
600+
sourceVariableHasBaseAndIndex(result, v, indirection) and
601+
this.hasBaseSourceVariableAndIndirection(v, indirection)
602+
)
601603
}
602604

603605
final override Cpp::Location getLocation() { result = f.getLocation() }
@@ -642,9 +644,11 @@ class GlobalDefImpl extends DefImpl, TGlobalDefImpl {
642644
)
643645
}
644646

645-
/** Gets the global variable associated with this definition. */
646-
override BaseSourceVariable getBaseSourceVariable() {
647-
baseSourceVariableIsGlobal(result, global, f)
647+
final override SourceVariable getSourceVariable() {
648+
exists(BaseSourceVariable v |
649+
sourceVariableHasBaseAndIndex(result, v, indirectionIndex) and
650+
baseSourceVariableIsGlobal(v, global, f)
651+
)
648652
}
649653

650654
override int getIndirection() { result = indirectionIndex }

0 commit comments

Comments
 (0)