Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c27dfb5

Browse files
author
Robert Marsh
committed
C++: IR translation for global variable inits
1 parent 143b79c commit c27dfb5

25 files changed

Lines changed: 329 additions & 41 deletions

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: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@ private newtype TIRFunction =
1313
* phases of the IR. Each instantiation of `IRFunction` extends this class.
1414
*/
1515
class IRFunctionBase extends TIRFunction {
16-
//Language::Function func;
17-
// IRFunctionBase() { this = TFunctionIRFunction(func) }
18-
/** Gets a textual representation of this element. */
19-
final string toString() {
20-
result = "IR: " + any(Language::Function func | this = TFunctionIRFunction(func)).toString()
16+
Language::Declaration decl;
17+
18+
IRFunctionBase() {
19+
this = TFunctionIRFunction(decl)
2120
or
22-
result = "IR: " + any(Language::GlobalVariable var | this = TVarInitIRFunction(var)).toString()
21+
this = TVarInitIRFunction(decl)
2322
}
2423

24+
/** Gets a textual representation of this element. */
25+
final string toString() { result = "IR: " + decl.toString() }
26+
2527
/** Gets the function whose IR is represented. */
26-
final Language::Function getFunction() { this = TFunctionIRFunction(result) }
28+
final Language::Declaration getFunction() { result = decl }
2729

2830
/** Gets the location of the function. */
29-
final Language::Location getLocation() {
30-
result = any(Language::Function func | this = TFunctionIRFunction(func)).getLocation()
31-
or
32-
result = any(Language::GlobalVariable var | this = TVarInitIRFunction(var)).getLocation()
33-
}
31+
final Language::Location getLocation() { result = decl.getLocation() }
3432
}

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/TranslatedElement.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ abstract class TranslatedElement extends TTranslatedElement {
802802
/**
803803
* Gets the `Function` that contains this element.
804804
*/
805-
abstract Function getFunction();
805+
abstract Declaration getFunction();
806806

807807
/**
808808
* Gets the successor instruction of the instruction that was generated by
@@ -952,3 +952,11 @@ abstract class TranslatedElement extends TTranslatedElement {
952952
*/
953953
final TranslatedElement getParent() { result.getAChild() = this }
954954
}
955+
956+
abstract class TranslatedInstructionContainer extends TranslatedElement {
957+
TranslatedInstructionContainer() {
958+
this instanceof TTranslatedFunction
959+
or
960+
this instanceof TTranslatedGlobalOrNamespaceVarInit
961+
}
962+
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private import TranslatedFunction
1313
private import TranslatedInitialization
1414
private import TranslatedFunction
1515
private import TranslatedStmt
16+
private import TranslatedGlobalVar
1617
import TranslatedCall
1718

1819
/**
@@ -79,7 +80,10 @@ abstract class TranslatedExpr extends TranslatedElement {
7980
/** DEPRECATED: Alias for getAst */
8081
deprecated override Locatable getAST() { result = this.getAst() }
8182

82-
final override Function getFunction() { result = expr.getEnclosingFunction() }
83+
final override Declaration getFunction() {
84+
result = expr.getEnclosingFunction() or
85+
result = expr.getEnclosingVariable().(GlobalOrNamespaceVariable)
86+
}
8387

8488
/**
8589
* Gets the expression from which this `TranslatedExpr` is generated.
@@ -89,8 +93,10 @@ abstract class TranslatedExpr extends TranslatedElement {
8993
/**
9094
* Gets the `TranslatedFunction` containing this expression.
9195
*/
92-
final TranslatedFunction getEnclosingFunction() {
96+
final TranslatedInstructionContainer getEnclosingFunction() {
9397
result = getTranslatedFunction(expr.getEnclosingFunction())
98+
or
99+
result = getTranslatedVarInit(expr.getEnclosingVariable())
94100
}
95101
}
96102

@@ -788,7 +794,7 @@ class TranslatedThisExpr extends TranslatedNonConstantExpr {
788794

789795
override IRVariable getInstructionVariable(InstructionTag tag) {
790796
tag = ThisAddressTag() and
791-
result = this.getEnclosingFunction().getThisVariable()
797+
result = this.getEnclosingFunction().(TranslatedFunction).getThisVariable()
792798
}
793799
}
794800

@@ -2523,7 +2529,7 @@ class TranslatedVarArgsStart extends TranslatedNonConstantExpr {
25232529

25242530
final override IRVariable getInstructionVariable(InstructionTag tag) {
25252531
tag = VarArgsStartEllipsisAddressTag() and
2526-
result = this.getEnclosingFunction().getEllipsisVariable()
2532+
result = this.getEnclosingFunction().(TranslatedFunction).getEllipsisVariable()
25272533
}
25282534

25292535
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {

0 commit comments

Comments
 (0)