Skip to content

Commit 648c08b

Browse files
committed
C++: Fix enclosing functions for static locals.
1 parent 9cc4bfe commit 648c08b

File tree

8 files changed

+78
-26
lines changed

8 files changed

+78
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ abstract class TranslatedSideEffects extends TranslatedElement {
180180
/** DEPRECATED: Alias for getAst */
181181
deprecated override Locatable getAST() { result = getAst() }
182182

183-
final override Declaration getFunction() { result = getExpr().getEnclosingDeclaration() }
183+
final override Declaration getFunction() { result = getEnclosingDeclaration(getExpr()) }
184184

185185
final override TranslatedElement getChild(int i) {
186186
result =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class TranslatedCondition extends TranslatedElement {
2828

2929
final Expr getExpr() { result = expr }
3030

31-
final override Function getFunction() { result = expr.getEnclosingFunction() }
31+
final override Function getFunction() { result = getEnclosingFunction(expr) }
3232

3333
final Type getResultType() { result = expr.getUnspecifiedType() }
3434
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ abstract class TranslatedDeclarationEntry extends TranslatedElement, TTranslated
2828

2929
TranslatedDeclarationEntry() { this = TTranslatedDeclarationEntry(entry) }
3030

31-
final override Function getFunction() {
32-
exists(DeclStmt stmt |
33-
stmt = entry.getStmt() and
31+
final override Declaration getFunction() {
32+
exists(DeclStmt stmt | stmt = entry.getStmt() |
33+
result = entry.getDeclaration().(StaticInitializedStaticLocalVariable)
34+
or
35+
result = entry.getDeclaration().(GlobalOrNamespaceVariable)
36+
or
37+
not entry.getDeclaration() instanceof StaticInitializedStaticLocalVariable and
38+
not entry.getDeclaration() instanceof GlobalOrNamespaceVariable and
3439
result = stmt.getEnclosingFunction()
3540
)
3641
}
@@ -237,7 +242,7 @@ class TranslatedStaticLocalVariableInitialization extends TranslatedElement,
237242

238243
final override LocalVariable getVariable() { result = var }
239244

240-
final override Function getFunction() { result = var.getFunction() }
245+
final override Declaration getFunction() { result = var.getFunction() }
241246
}
242247

243248
TranslatedConditionDecl getTranslatedConditionDecl(ConditionDeclExpr expr) {
@@ -264,7 +269,7 @@ class TranslatedConditionDecl extends TranslatedLocalVariableDeclaration, TTrans
264269
/** DEPRECATED: Alias for getAst */
265270
deprecated override Locatable getAST() { result = getAst() }
266271

267-
override Function getFunction() { result = conditionDeclExpr.getEnclosingFunction() }
272+
override Declaration getFunction() { result = getEnclosingFunction(conditionDeclExpr) }
268273

269274
override LocalVariable getVariable() { result = conditionDeclExpr.getVariable() }
270275
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ private predicate ignoreExprOnly(Expr expr) {
109109
// should not be translated.
110110
exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0))
111111
or
112-
not translateFunction(expr.getEnclosingFunction()) and
113-
not Raw::varHasIRFunc(expr.getEnclosingVariable())
112+
not translateFunction(getEnclosingFunction(expr)) and
113+
not Raw::varHasIRFunc(getEnclosingVariable(expr))
114114
or
115115
// We do not yet translate destructors properly, so for now we ignore the
116116
// destructor call. We do, however, translate the expression being

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ abstract class TranslatedExpr extends TranslatedElement {
7979
/** DEPRECATED: Alias for getAst */
8080
deprecated override Locatable getAST() { result = this.getAst() }
8181

82-
final override Declaration getFunction() { result = expr.getEnclosingDeclaration() }
82+
final override Declaration getFunction() { result = getEnclosingDeclaration(expr) }
8383

8484
/**
8585
* Gets the expression from which this `TranslatedExpr` is generated.
@@ -90,12 +90,57 @@ abstract class TranslatedExpr extends TranslatedElement {
9090
* Gets the `TranslatedFunction` containing this expression.
9191
*/
9292
final TranslatedRootElement getEnclosingFunction() {
93-
result = getTranslatedFunction(expr.getEnclosingFunction())
93+
result = getTranslatedFunction(getEnclosingFunction(expr))
9494
or
95-
result = getTranslatedVarInit(expr.getEnclosingVariable())
95+
result = getTranslatedVarInit(getEnclosingVariable(expr))
9696
}
9797
}
9898

99+
Function getEnclosingFunction(Expr e) {
100+
not exists(getEnclosingVariable(e)) and
101+
result = e.getEnclosingFunction()
102+
}
103+
104+
Declaration getEnclosingDeclaration0(Expr e) {
105+
result = getEnclosingDeclaration0(e.getParentWithConversions())
106+
or
107+
exists(Initializer i, Variable v |
108+
i.getExpr().getFullyConverted() = e and
109+
v = i.getDeclaration()
110+
|
111+
if v instanceof StaticInitializedStaticLocalVariable or v instanceof GlobalOrNamespaceVariable
112+
then result = v
113+
else result = e.getEnclosingDeclaration()
114+
)
115+
}
116+
117+
Declaration getEnclosingDeclaration(Expr e) {
118+
result = getEnclosingDeclaration0(e)
119+
or
120+
not exists(getEnclosingDeclaration0(e)) and
121+
result = e.getEnclosingDeclaration()
122+
}
123+
124+
Variable getEnclosingVariable0(Expr e) {
125+
result = getEnclosingVariable0(e.getParentWithConversions())
126+
or
127+
exists(Initializer i, Variable v |
128+
i.getExpr().getFullyConverted() = e and
129+
v = i.getDeclaration()
130+
|
131+
if v instanceof StaticInitializedStaticLocalVariable or v instanceof GlobalOrNamespaceVariable
132+
then result = v
133+
else result = e.getEnclosingVariable()
134+
)
135+
}
136+
137+
Variable getEnclosingVariable(Expr e) {
138+
result = getEnclosingVariable0(e)
139+
or
140+
not exists(getEnclosingVariable0(e)) and
141+
result = e.getEnclosingVariable()
142+
}
143+
99144
/**
100145
* The IR translation of the "core" part of an expression. This is the part of
101146
* the expression that produces the result value of the expression, before any
@@ -843,7 +888,7 @@ class TranslatedNonFieldVariableAccess extends TranslatedVariableAccess {
843888

844889
override IRVariable getInstructionVariable(InstructionTag tag) {
845890
tag = OnlyInstructionTag() and
846-
result = getIRUserVariable(expr.getEnclosingDeclaration(), expr.getTarget())
891+
result = getIRUserVariable(getEnclosingDeclaration(expr), expr.getTarget())
847892
}
848893
}
849894

@@ -2000,7 +2045,7 @@ class TranslatedDestructorFieldDestruction extends TranslatedNonConstantExpr, St
20002045
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
20012046
tag = OnlyInstructionTag() and
20022047
operandTag instanceof UnaryOperandTag and
2003-
result = getTranslatedFunction(expr.getEnclosingFunction()).getInitializeThisInstruction()
2048+
result = getTranslatedFunction(getEnclosingFunction(expr)).getInitializeThisInstruction()
20042049
}
20052050

20062051
final override Field getInstructionField(InstructionTag tag) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class TranslatedFunction extends TranslatedRootElement, TTranslatedFunction {
328328
) and
329329
exists(VariableAccess access |
330330
access.getTarget() = var and
331-
access.getEnclosingFunction() = func
331+
getEnclosingFunction(access) = func
332332
)
333333
or
334334
var.(LocalScopeVariable).getFunction() = func

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import semmle.code.cpp.ir.implementation.raw.internal.TranslatedElement
2+
private import TranslatedExpr
23
private import cpp
34
private import semmle.code.cpp.ir.implementation.IRType
45
private import semmle.code.cpp.ir.implementation.Opcode
@@ -117,7 +118,7 @@ class TranslatedStaticStorageDurationVarInit extends TranslatedRootElement,
117118
) and
118119
exists(VariableAccess access |
119120
access.getTarget() = varUsed and
120-
access.getEnclosingVariable() = var
121+
getEnclosingVariable(access) = var
121122
)
122123
or
123124
var = varUsed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ abstract class TranslatedInitialization extends TranslatedElement, TTranslatedIn
138138
final override string toString() { result = "init: " + expr.toString() }
139139

140140
final override Declaration getFunction() {
141-
result = expr.getEnclosingFunction() or
142-
result = expr.getEnclosingVariable().(GlobalOrNamespaceVariable) or
143-
result = expr.getEnclosingVariable().(StaticInitializedStaticLocalVariable)
141+
result = getEnclosingFunction(expr) or
142+
result = getEnclosingVariable(expr).(GlobalOrNamespaceVariable) or
143+
result = getEnclosingVariable(expr).(StaticInitializedStaticLocalVariable)
144144
}
145145

146146
final override Locatable getAst() { result = expr }
@@ -160,7 +160,7 @@ abstract class TranslatedInitialization extends TranslatedElement, TTranslatedIn
160160
final InitializationContext getContext() { result = getParent() }
161161

162162
final TranslatedFunction getEnclosingFunction() {
163-
result = getTranslatedFunction(expr.getEnclosingFunction())
163+
result = getTranslatedFunction(this.getFunction())
164164
}
165165
}
166166

@@ -494,8 +494,9 @@ abstract class TranslatedFieldInitialization extends TranslatedElement {
494494
deprecated override Locatable getAST() { result = getAst() }
495495

496496
final override Declaration getFunction() {
497-
result = ast.getEnclosingFunction() or
498-
result = ast.getEnclosingVariable().(GlobalOrNamespaceVariable)
497+
result = getEnclosingFunction(ast) or
498+
result = getEnclosingVariable(ast).(GlobalOrNamespaceVariable) or
499+
result = getEnclosingVariable(ast).(StaticInitializedStaticLocalVariable)
499500
}
500501

501502
final override Instruction getFirstInstruction() { result = getInstruction(getFieldAddressTag()) }
@@ -652,11 +653,11 @@ abstract class TranslatedElementInitialization extends TranslatedElement {
652653
deprecated override Locatable getAST() { result = getAst() }
653654

654655
final override Declaration getFunction() {
655-
result = initList.getEnclosingFunction()
656+
result = getEnclosingFunction(initList)
656657
or
657-
result = initList.getEnclosingVariable().(GlobalOrNamespaceVariable)
658+
result = getEnclosingVariable(initList).(GlobalOrNamespaceVariable)
658659
or
659-
result = initList.getEnclosingVariable().(StaticInitializedStaticLocalVariable)
660+
result = getEnclosingVariable(initList).(StaticInitializedStaticLocalVariable)
660661
}
661662

662663
final override Instruction getFirstInstruction() { result = getInstruction(getElementIndexTag()) }
@@ -855,7 +856,7 @@ abstract class TranslatedStructorCallFromStructor extends TranslatedElement, Str
855856
result = getStructorCall()
856857
}
857858

858-
final override Function getFunction() { result = call.getEnclosingFunction() }
859+
final override Function getFunction() { result = getEnclosingFunction(call) }
859860

860861
final override Instruction getChildSuccessor(TranslatedElement child) {
861862
child = getStructorCall() and

0 commit comments

Comments
 (0)