Skip to content

Commit 800e4ea

Browse files
authored
Merge pull request #8515 from rdmarsh2/rdmarsh2/ir-global-vars
C++: generate IR for global variables with initializers
2 parents 7ce040f + d0fc348 commit 800e4ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+553
-76
lines changed

cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class Expr extends StmtParent, @expr {
4949
/** Gets the enclosing variable of this expression, if any. */
5050
Variable getEnclosingVariable() { result = exprEnclosingElement(this) }
5151

52+
/** Gets the enclosing variable or function of this expression. */
53+
Declaration getEnclosingDeclaration() { result = exprEnclosingElement(this) }
54+
5255
/** Gets a child of this expression. */
5356
Expr getAChild() { exists(int n | result = this.getChild(n)) }
5457

cpp/ql/lib/semmle/code/cpp/ir/implementation/IRConfiguration.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class IRConfiguration extends TIRConfiguration {
1616
/**
1717
* Holds if IR should be created for function `func`. By default, holds for all functions.
1818
*/
19-
predicate shouldCreateIRForFunction(Language::Function func) { any() }
19+
predicate shouldCreateIRForFunction(Language::Declaration func) { any() }
2020

2121
/**
2222
* Holds if the strings used as part of an IR dump should be generated for function `func`.
@@ -25,7 +25,7 @@ class IRConfiguration extends TIRConfiguration {
2525
* of debug strings for IR that will not be dumped. We still generate the actual IR for these
2626
* functions, however, to preserve the results of any interprocedural analysis.
2727
*/
28-
predicate shouldEvaluateDebugStringsForFunction(Language::Function func) { any() }
28+
predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) { any() }
2929
}
3030

3131
private newtype TIREscapeAnalysisConfiguration = MkIREscapeAnalysisConfiguration()

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class IRBlockBase extends TIRBlock {
9797
/**
9898
* Gets the `Function` that contains this block.
9999
*/
100-
final Language::Function getEnclosingFunction() {
100+
final Language::Declaration getEnclosingFunction() {
101101
result = getFirstInstruction(this).getEnclosingFunction()
102102
}
103103
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Instruction extends Construction::TStageInstruction {
194194
/**
195195
* Gets the function that contains this instruction.
196196
*/
197-
final Language::Function getEnclosingFunction() {
197+
final Language::Declaration getEnclosingFunction() {
198198
result = this.getEnclosingIRFunction().getFunction()
199199
}
200200

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ class PrintIRConfiguration extends TPrintIRConfiguration {
2626
* Holds if the IR for `func` should be printed. By default, holds for all
2727
* functions.
2828
*/
29-
predicate shouldPrintFunction(Language::Function func) { any() }
29+
predicate shouldPrintFunction(Language::Declaration decl) { any() }
3030
}
3131

3232
/**
3333
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
3434
*/
3535
private class FilteredIRConfiguration extends IRConfiguration {
36-
override predicate shouldEvaluateDebugStringsForFunction(Language::Function func) {
36+
override predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) {
3737
shouldPrintFunction(func)
3838
}
3939
}
4040

41-
private predicate shouldPrintFunction(Language::Function func) {
42-
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
41+
private predicate shouldPrintFunction(Language::Declaration decl) {
42+
exists(PrintIRConfiguration config | config.shouldPrintFunction(decl))
4343
}
4444

4545
private string getAdditionalInstructionProperty(Instruction instr, string key) {

cpp/ql/lib/semmle/code/cpp/ir/implementation/internal/IRFunctionBase.qll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55
private import IRFunctionBaseInternal
66

77
private newtype TIRFunction =
8-
MkIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) }
8+
TFunctionIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) } or
9+
TVarInitIRFunction(Language::GlobalVariable var) { IRConstruction::Raw::varHasIRFunc(var) }
910

1011
/**
1112
* The IR for a function. This base class contains only the predicates that are the same between all
1213
* phases of the IR. Each instantiation of `IRFunction` extends this class.
1314
*/
1415
class IRFunctionBase extends TIRFunction {
15-
Language::Function func;
16+
Language::Declaration decl;
1617

17-
IRFunctionBase() { this = MkIRFunction(func) }
18+
IRFunctionBase() {
19+
this = TFunctionIRFunction(decl)
20+
or
21+
this = TVarInitIRFunction(decl)
22+
}
1823

1924
/** Gets a textual representation of this element. */
20-
final string toString() { result = "IR: " + func.toString() }
25+
final string toString() { result = "IR: " + decl.toString() }
2126

2227
/** Gets the function whose IR is represented. */
23-
final Language::Function getFunction() { result = func }
28+
final Language::Declaration getFunction() { result = decl }
2429

2530
/** Gets the location of the function. */
26-
final Language::Location getLocation() { result = func.getLocation() }
31+
final Language::Location getLocation() { result = decl.getLocation() }
2732
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRBlock.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class IRBlockBase extends TIRBlock {
9797
/**
9898
* Gets the `Function` that contains this block.
9999
*/
100-
final Language::Function getEnclosingFunction() {
100+
final Language::Declaration getEnclosingFunction() {
101101
result = getFirstInstruction(this).getEnclosingFunction()
102102
}
103103
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Instruction extends Construction::TStageInstruction {
194194
/**
195195
* Gets the function that contains this instruction.
196196
*/
197-
final Language::Function getEnclosingFunction() {
197+
final Language::Declaration getEnclosingFunction() {
198198
result = this.getEnclosingIRFunction().getFunction()
199199
}
200200

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/PrintIR.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ class PrintIRConfiguration extends TPrintIRConfiguration {
2626
* Holds if the IR for `func` should be printed. By default, holds for all
2727
* functions.
2828
*/
29-
predicate shouldPrintFunction(Language::Function func) { any() }
29+
predicate shouldPrintFunction(Language::Declaration decl) { any() }
3030
}
3131

3232
/**
3333
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
3434
*/
3535
private class FilteredIRConfiguration extends IRConfiguration {
36-
override predicate shouldEvaluateDebugStringsForFunction(Language::Function func) {
36+
override predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) {
3737
shouldPrintFunction(func)
3838
}
3939
}
4040

41-
private predicate shouldPrintFunction(Language::Function func) {
42-
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
41+
private predicate shouldPrintFunction(Language::Declaration decl) {
42+
exists(PrintIRConfiguration config | config.shouldPrintFunction(decl))
4343
}
4444

4545
private string getAdditionalInstructionProperty(Instruction instr, string key) {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module Raw {
3535
cached
3636
predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) }
3737

38+
cached
39+
predicate varHasIRFunc(GlobalOrNamespaceVariable var) { any() } // TODO: restrict?
40+
3841
cached
3942
predicate hasInstruction(TranslatedElement element, InstructionTag tag) {
4043
element.hasInstruction(_, tag, _)
@@ -46,18 +49,18 @@ module Raw {
4649
}
4750

4851
cached
49-
predicate hasTempVariable(Function func, Locatable ast, TempVariableTag tag, CppType type) {
52+
predicate hasTempVariable(Declaration decl, Locatable ast, TempVariableTag tag, CppType type) {
5053
exists(TranslatedElement element |
5154
element.getAst() = ast and
52-
func = element.getFunction() and
55+
decl = element.getFunction() and
5356
element.hasTempVariable(tag, type)
5457
)
5558
}
5659

5760
cached
58-
predicate hasStringLiteral(Function func, Locatable ast, CppType type, StringLiteral literal) {
61+
predicate hasStringLiteral(Declaration decl, Locatable ast, CppType type, StringLiteral literal) {
5962
literal = ast and
60-
literal.getEnclosingFunction() = func and
63+
literal.getEnclosingDeclaration() = decl and
6164
getTypeForPRValue(literal.getType()) = type
6265
}
6366

0 commit comments

Comments
 (0)