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.

This commit is contained in:
Mathias Vorreiter Pedersen
2022-09-05 22:18:41 +01:00
parent fc85ef76ed
commit f6654e3d69
3 changed files with 36 additions and 72 deletions

View File

@@ -238,40 +238,6 @@ class TranslatedStaticLocalVariableInitialization extends TranslatedElement,
final override Function getFunction() { result = var.getFunction() }
}
/**
* Gets the `TranslatedRangeBasedForVariableDeclaration` that represents the declaration of
* `var`.
*/
TranslatedRangeBasedForVariableDeclaration getTranslatedRangeBasedForVariableDeclaration(
LocalVariable var
) {
result.getVariable() = var
}
/**
* Represents the IR translation of a compiler-generated variable in a range-based `for` loop.
*/
class TranslatedRangeBasedForVariableDeclaration extends TranslatedLocalVariableDeclaration,
TTranslatedRangeBasedForVariableDeclaration {
RangeBasedForStmt forStmt;
LocalVariable var;
TranslatedRangeBasedForVariableDeclaration() {
this = TTranslatedRangeBasedForVariableDeclaration(forStmt, var)
}
override string toString() { result = var.toString() }
override Locatable getAst() { result = var }
/** DEPRECATED: Alias for getAst */
deprecated override Locatable getAST() { result = getAst() }
override Function getFunction() { result = forStmt.getEnclosingFunction() }
override LocalVariable getVariable() { result = var }
}
TranslatedConditionDecl getTranslatedConditionDecl(ConditionDeclExpr expr) {
result.getAst() = expr
}

View File

@@ -714,16 +714,6 @@ newtype TTranslatedElement =
translateDeclarationEntry(entry) and
entry.getDeclaration() instanceof StaticLocalVariable
} or
// A compiler-generated variable to implement a range-based for loop. These don't have a
// `DeclarationEntry` in the database, so we have to go by the `Variable` itself.
TTranslatedRangeBasedForVariableDeclaration(RangeBasedForStmt forStmt, LocalVariable var) {
translateStmt(forStmt) and
(
var = forStmt.getRangeVariable() or
var = forStmt.getBeginEndDeclaration().getADeclaration() or
var = forStmt.getVariable()
)
} or
// An allocator call in a `new` or `new[]` expression
TTranslatedAllocatorCall(NewOrNewArrayExpr newExpr) { not ignoreExpr(newExpr) } or
// An allocation size for a `new` or `new[]` expression

View File

@@ -76,6 +76,13 @@ class TranslatedDeclStmt extends TranslatedStmt {
private int getChildCount() { result = count(getDeclarationEntry(_)) }
PseudoDeclarationEntry getPseudoDeclarationEntry(int index) {
result.hasIndex(index) and
result.getStmt() = stmt
}
PseudoDeclarationEntry getAPseudoDeclarationEntry() { result = this.getPseudoDeclarationEntry(_) }
/**
* Gets the `TranslatedDeclarationEntry` child at zero-based index `index`. Since not all
* `DeclarationEntry` objects have a `TranslatedDeclarationEntry` (e.g. extern functions), we map
@@ -85,7 +92,7 @@ class TranslatedDeclStmt extends TranslatedStmt {
private TranslatedDeclarationEntry getDeclarationEntry(int index) {
result =
rank[index + 1](TranslatedDeclarationEntry entry, int originalIndex |
entry = getTranslatedDeclarationEntry(stmt.getDeclarationEntry(originalIndex))
entry = getTranslatedDeclarationEntry(this.getPseudoDeclarationEntry(originalIndex))
|
entry order by originalIndex
)
@@ -597,36 +604,32 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
override RangeBasedForStmt stmt;
override TranslatedElement getChild(int id) {
id = 0 and result = getRangeVariableDeclaration()
id = 0 and result = getRangeVariableDeclStmt()
or
id = 1 and result = getBeginVariableDeclaration()
// Note: `__begin` and `__end` are declared by the same `DeclStmt`
id = 1 and result = getBeginEndVariableDeclStmt()
or
id = 2 and result = getEndVariableDeclaration()
id = 2 and result = getCondition()
or
id = 3 and result = getCondition()
id = 3 and result = getUpdate()
or
id = 4 and result = getUpdate()
id = 4 and result = getVariableDeclStmt()
or
id = 5 and result = getVariableDeclaration()
or
id = 6 and result = getBody()
id = 5 and result = getBody()
}
override Instruction getFirstInstruction() {
result = getRangeVariableDeclaration().getFirstInstruction()
result = getRangeVariableDeclStmt().getFirstInstruction()
}
override Instruction getChildSuccessor(TranslatedElement child) {
child = getRangeVariableDeclaration() and
result = getBeginVariableDeclaration().getFirstInstruction()
child = getRangeVariableDeclStmt() and
result = getBeginEndVariableDeclStmt().getFirstInstruction()
or
child = getBeginVariableDeclaration() and
result = getEndVariableDeclaration().getFirstInstruction()
or
child = getEndVariableDeclaration() and
child = getBeginEndVariableDeclStmt() and
result = getCondition().getFirstInstruction()
or
child = getVariableDeclaration() and
child = getVariableDeclStmt() and
result = getBody().getFirstInstruction()
or
child = getBody() and
@@ -643,23 +646,25 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
override Instruction getChildTrueSuccessor(TranslatedCondition child) {
child = getCondition() and result = getVariableDeclaration().getFirstInstruction()
child = getCondition() and result = getVariableDeclStmt().getFirstInstruction()
}
override Instruction getChildFalseSuccessor(TranslatedCondition child) {
child = getCondition() and result = getParent().getChildSuccessor(this)
}
private TranslatedRangeBasedForVariableDeclaration getRangeVariableDeclaration() {
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getRangeVariable())
private TranslatedDeclStmt getRangeVariableDeclStmt() {
exists(PseudoVariableDeclarationEntry entry |
entry.getDeclaration() = stmt.getRangeVariable() and
result.getAPseudoDeclarationEntry() = entry
)
}
private TranslatedRangeBasedForVariableDeclaration getBeginVariableDeclaration() {
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getBeginVariable())
}
private TranslatedRangeBasedForVariableDeclaration getEndVariableDeclaration() {
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getEndVariable())
private TranslatedDeclStmt getBeginEndVariableDeclStmt() {
exists(PseudoVariableDeclarationEntry entry |
entry.getStmt() = stmt.getBeginEndDeclaration() and
result.getAPseudoDeclarationEntry() = entry
)
}
// Public for getInstructionBackEdgeSuccessor
@@ -672,8 +677,11 @@ class TranslatedRangeBasedForStmt extends TranslatedStmt, ConditionContext {
result = getTranslatedExpr(stmt.getUpdate().getFullyConverted())
}
private TranslatedRangeBasedForVariableDeclaration getVariableDeclaration() {
result = getTranslatedRangeBasedForVariableDeclaration(stmt.getVariable())
private TranslatedDeclStmt getVariableDeclStmt() {
exists(PseudoVariableDeclarationEntry entry |
entry.getDeclaration() = stmt.getVariable() and
result.getAPseudoDeclarationEntry() = entry
)
}
private TranslatedStmt getBody() { result = getTranslatedStmt(stmt.getStmt()) }