Skip to content

Commit f6654e3

Browse files
committed
C++: As a bonus we now no longer need the 'TTranslatedRangeBasedForVariableDeclaration' IPA branch. This previously existed only to account for the missing 'DeclarationEntry's happening in range-based for loops. But these are now also handled by 'PseudoDeclarationEntry's.
1 parent fc85ef7 commit f6654e3

File tree

3 files changed

+36
-72
lines changed

3 files changed

+36
-72
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedDeclarationEntry.qll

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -238,40 +238,6 @@ class TranslatedStaticLocalVariableInitialization extends TranslatedElement,
238238
final override Function getFunction() { result = var.getFunction() }
239239
}
240240

241-
/**
242-
* Gets the `TranslatedRangeBasedForVariableDeclaration` that represents the declaration of
243-
* `var`.
244-
*/
245-
TranslatedRangeBasedForVariableDeclaration getTranslatedRangeBasedForVariableDeclaration(
246-
LocalVariable var
247-
) {
248-
result.getVariable() = var
249-
}
250-
251-
/**
252-
* Represents the IR translation of a compiler-generated variable in a range-based `for` loop.
253-
*/
254-
class TranslatedRangeBasedForVariableDeclaration extends TranslatedLocalVariableDeclaration,
255-
TTranslatedRangeBasedForVariableDeclaration {
256-
RangeBasedForStmt forStmt;
257-
LocalVariable var;
258-
259-
TranslatedRangeBasedForVariableDeclaration() {
260-
this = TTranslatedRangeBasedForVariableDeclaration(forStmt, var)
261-
}
262-
263-
override string toString() { result = var.toString() }
264-
265-
override Locatable getAst() { result = var }
266-
267-
/** DEPRECATED: Alias for getAst */
268-
deprecated override Locatable getAST() { result = getAst() }
269-
270-
override Function getFunction() { result = forStmt.getEnclosingFunction() }
271-
272-
override LocalVariable getVariable() { result = var }
273-
}
274-
275241
TranslatedConditionDecl getTranslatedConditionDecl(ConditionDeclExpr expr) {
276242
result.getAst() = expr
277243
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,16 +714,6 @@ newtype TTranslatedElement =
714714
translateDeclarationEntry(entry) and
715715
entry.getDeclaration() instanceof StaticLocalVariable
716716
} or
717-
// A compiler-generated variable to implement a range-based for loop. These don't have a
718-
// `DeclarationEntry` in the database, so we have to go by the `Variable` itself.
719-
TTranslatedRangeBasedForVariableDeclaration(RangeBasedForStmt forStmt, LocalVariable var) {
720-
translateStmt(forStmt) and
721-
(
722-
var = forStmt.getRangeVariable() or
723-
var = forStmt.getBeginEndDeclaration().getADeclaration() or
724-
var = forStmt.getVariable()
725-
)
726-
} or
727717
// An allocator call in a `new` or `new[]` expression
728718
TTranslatedAllocatorCall(NewOrNewArrayExpr newExpr) { not ignoreExpr(newExpr) } or
729719
// An allocation size for a `new` or `new[]` expression

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedStmt.qll

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ class TranslatedDeclStmt extends TranslatedStmt {
7676

7777
private int getChildCount() { result = count(getDeclarationEntry(_)) }
7878

79+
PseudoDeclarationEntry getPseudoDeclarationEntry(int index) {
80+
result.hasIndex(index) and
81+
result.getStmt() = stmt
82+
}
83+
84+
PseudoDeclarationEntry getAPseudoDeclarationEntry() { result = this.getPseudoDeclarationEntry(_) }
85+
7986
/**
8087
* Gets the `TranslatedDeclarationEntry` child at zero-based index `index`. Since not all
8188
* `DeclarationEntry` objects have a `TranslatedDeclarationEntry` (e.g. extern functions), we map
@@ -85,7 +92,7 @@ class TranslatedDeclStmt extends TranslatedStmt {
8592
private TranslatedDeclarationEntry getDeclarationEntry(int index) {
8693
result =
8794
rank[index + 1](TranslatedDeclarationEntry entry, int originalIndex |
88-
entry = getTranslatedDeclarationEntry(stmt.getDeclarationEntry(originalIndex))
95+
entry = getTranslatedDeclarationEntry(this.getPseudoDeclarationEntry(originalIndex))
8996
|
9097
entry order by originalIndex
9198
)
@@ -597,36 +604,32 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
597604
override RangeBasedForStmt stmt;
598605

599606
override TranslatedElement getChild(int id) {
600-
id = 0 and result = getRangeVariableDeclaration()
607+
id = 0 and result = getRangeVariableDeclStmt()
601608
or
602-
id = 1 and result = getBeginVariableDeclaration()
609+
// Note: `__begin` and `__end` are declared by the same `DeclStmt`
610+
id = 1 and result = getBeginEndVariableDeclStmt()
603611
or
604-
id = 2 and result = getEndVariableDeclaration()
612+
id = 2 and result = getCondition()
605613
or
606-
id = 3 and result = getCondition()
614+
id = 3 and result = getUpdate()
607615
or
608-
id = 4 and result = getUpdate()
616+
id = 4 and result = getVariableDeclStmt()
609617
or
610-
id = 5 and result = getVariableDeclaration()
611-
or
612-
id = 6 and result = getBody()
618+
id = 5 and result = getBody()
613619
}
614620

615621
override Instruction getFirstInstruction() {
616-
result = getRangeVariableDeclaration().getFirstInstruction()
622+
result = getRangeVariableDeclStmt().getFirstInstruction()
617623
}
618624

619625
override Instruction getChildSuccessor(TranslatedElement child) {
620-
child = getRangeVariableDeclaration() and
621-
result = getBeginVariableDeclaration().getFirstInstruction()
622-
or
623-
child = getBeginVariableDeclaration() and
624-
result = getEndVariableDeclaration().getFirstInstruction()
626+
child = getRangeVariableDeclStmt() and
627+
result = getBeginEndVariableDeclStmt().getFirstInstruction()
625628
or
626-
child = getEndVariableDeclaration() and
629+
child = getBeginEndVariableDeclStmt() and
627630
result = getCondition().getFirstInstruction()
628631
or
629-
child = getVariableDeclaration() and
632+
child = getVariableDeclStmt() and
630633
result = getBody().getFirstInstruction()
631634
or
632635
child = getBody() and
@@ -643,23 +646,25 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
643646
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
644647

645648
override Instruction getChildTrueSuccessor(TranslatedCondition child) {
646-
child = getCondition() and result = getVariableDeclaration().getFirstInstruction()
649+
child = getCondition() and result = getVariableDeclStmt().getFirstInstruction()
647650
}
648651

649652
override Instruction getChildFalseSuccessor(TranslatedCondition child) {
650653
child = getCondition() and result = getParent().getChildSuccessor(this)
651654
}
652655

653-
private TranslatedRangeBasedForVariableDeclaration getRangeVariableDeclaration() {
654-
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getRangeVariable())
655-
}
656-
657-
private TranslatedRangeBasedForVariableDeclaration getBeginVariableDeclaration() {
658-
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getBeginVariable())
656+
private TranslatedDeclStmt getRangeVariableDeclStmt() {
657+
exists(PseudoVariableDeclarationEntry entry |
658+
entry.getDeclaration() = stmt.getRangeVariable() and
659+
result.getAPseudoDeclarationEntry() = entry
660+
)
659661
}
660662

661-
private TranslatedRangeBasedForVariableDeclaration getEndVariableDeclaration() {
662-
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getEndVariable())
663+
private TranslatedDeclStmt getBeginEndVariableDeclStmt() {
664+
exists(PseudoVariableDeclarationEntry entry |
665+
entry.getStmt() = stmt.getBeginEndDeclaration() and
666+
result.getAPseudoDeclarationEntry() = entry
667+
)
663668
}
664669

665670
// Public for getInstructionBackEdgeSuccessor
@@ -672,8 +677,11 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
672677
result = getTranslatedExpr(stmt.getUpdate().getFullyConverted())
673678
}
674679

675-
private TranslatedRangeBasedForVariableDeclaration getVariableDeclaration() {
676-
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getVariable())
680+
private TranslatedDeclStmt getVariableDeclStmt() {
681+
exists(PseudoVariableDeclarationEntry entry |
682+
entry.getDeclaration() = stmt.getVariable() and
683+
result.getAPseudoDeclarationEntry() = entry
684+
)
677685
}
678686

679687
private TranslatedStmt getBody() { result = getTranslatedStmt(stmt.getStmt()) }

0 commit comments

Comments
 (0)