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

Skip to content

Commit 143b79c

Browse files
author
Robert Marsh
committed
C++/WIP: Generate IR for global variables
1 parent bf21a47 commit 143b79c

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
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-
17-
IRFunctionBase() { this = MkIRFunction(func) }
18-
16+
//Language::Function func;
17+
// IRFunctionBase() { this = TFunctionIRFunction(func) }
1918
/** Gets a textual representation of this element. */
20-
final string toString() { result = "IR: " + func.toString() }
19+
final string toString() {
20+
result = "IR: " + any(Language::Function func | this = TFunctionIRFunction(func)).toString()
21+
or
22+
result = "IR: " + any(Language::GlobalVariable var | this = TVarInitIRFunction(var)).toString()
23+
}
2124

2225
/** Gets the function whose IR is represented. */
23-
final Language::Function getFunction() { result = func }
26+
final Language::Function getFunction() { this = TFunctionIRFunction(result) }
2427

2528
/** Gets the location of the function. */
26-
final Language::Location getLocation() { result = func.getLocation() }
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+
}
2734
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module Raw {
3434

3535
cached
3636
predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) }
37+
38+
cached
39+
predicate varHasIRFunc(GlobalOrNamespaceVariable var) { any() } // TODO: restrict?
3740

3841
cached
3942
predicate hasInstruction(TranslatedElement element, InstructionTag tag) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private predicate ignoreExprAndDescendants(Expr expr) {
6767
exists(Initializer init, StaticStorageDurationVariable var |
6868
init = var.getInitializer() and
6969
not var.hasDynamicInitialization() and
70-
expr = init.getExpr().getFullyConverted()
70+
expr = init.getExpr().getFullyConverted() and
71+
not var instanceof GlobalOrNamespaceVariable
7172
)
7273
or
7374
// Ignore descendants of `__assume` expressions, since we translated these to `NoOp`.
@@ -117,7 +118,8 @@ private predicate ignoreExprOnly(Expr expr) {
117118
// should not be translated.
118119
exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0))
119120
or
120-
not translateFunction(expr.getEnclosingFunction())
121+
not translateFunction(expr.getEnclosingFunction()) and
122+
not expr.getEnclosingVariable() instanceof GlobalOrNamespaceVariable
121123
or
122124
// We do not yet translate destructors properly, so for now we ignore the
123125
// destructor call. We do, however, translate the expression being
@@ -669,7 +671,8 @@ newtype TTranslatedElement =
669671
opcode = getASideEffectOpcode(call, -1)
670672
} or
671673
// The side effect that initializes newly-allocated memory.
672-
TTranslatedAllocationSideEffect(AllocationExpr expr) { not ignoreSideEffects(expr) }
674+
TTranslatedAllocationSideEffect(AllocationExpr expr) { not ignoreSideEffects(expr) } or
675+
TTranslatedGlobalOrNamespaceVarInit(GlobalOrNamespaceVariable var) { any() }
673676

674677
/**
675678
* Gets the index of the first explicitly initialized element in `initList`

cpp/ql/lib/semmle/code/cpp/ir/internal/IRCppLanguage.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class AutomaticVariable = Cpp::StackVariable;
5050

5151
class StaticVariable = Cpp::Variable;
5252

53+
class GlobalVariable = Cpp::GlobalOrNamespaceVariable;
54+
5355
class Parameter = Cpp::Parameter;
5456

5557
class Field = Cpp::Field;

0 commit comments

Comments
 (0)