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

Skip to content

Commit badb117

Browse files
author
Dave Bartolomeo
committed
AST and IR support for TemporaryObjectExpr
1 parent 060c19a commit badb117

7 files changed

Lines changed: 634 additions & 55 deletions

File tree

cpp/ql/src/semmle/code/cpp/exprs/Cast.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,27 @@ class ArrayToPointerConversion extends Conversion, @array_to_pointer {
840840
override predicate mayBeGloballyImpure() { none() }
841841
}
842842

843+
/**
844+
* A node representing a temporary object created as part of an expression.
845+
*
846+
* This is most commonly seen in the following cases (from [class.temporary]/2):
847+
* - when binding a reference to a prvalue
848+
* - when performing member access on a class prvalue
849+
* - when performing an array-to-pointer conversion or subscripting on an array prvalue
850+
* - when initializing an object of type std::initializer_list from a braced-init-list
851+
* - for certain unevaluated operands
852+
* - when a prvalue that has type other than cv void appears as a discarded-value expression
853+
*
854+
* This node will only exist if the object is of class type, and even then only if either the
855+
* object's initialization or destruction is non-trivial.
856+
*/
857+
class TemporaryObjectExpr extends Conversion, @temp_init {
858+
/** Gets a textual representation of this conversion. */
859+
override string toString() { result = "temporary object" }
860+
861+
override string getAPrimaryQlClass() { result = "TemporaryObjectExpr" }
862+
}
863+
843864
/**
844865
* A node representing the Cast sub-class of entity `cast`.
845866
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ newtype TTranslatedElement =
351351
exists(ConstructorFieldInit fieldInit | fieldInit.getExpr().getFullyConverted() = expr) or
352352
exists(NewExpr newExpr | newExpr.getInitializer().getFullyConverted() = expr) or
353353
exists(ThrowExpr throw | throw.getExpr().getFullyConverted() = expr) or
354+
exists(TemporaryObjectExpr temp | temp.getExpr() = expr) or
354355
exists(LambdaExpression lambda | lambda.getInitializer().getFullyConverted() = expr)
355356
)
356357
} or

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,37 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
20492049
}
20502050
}
20512051

2052+
/**
2053+
* IR translation of the materialization of a temporary object.
2054+
*
2055+
* This translation allocates a temporary variable, and initializes it treating `expr.getExpr()` as
2056+
* its initializer.
2057+
*/
2058+
class TranslatedTemporaryObjectExpr extends TranslatedNonConstantExpr, TranslatedVariableInitialization {
2059+
override TemporaryObjectExpr expr;
2060+
2061+
final override predicate hasTempVariable(TempVariableTag tag, CppType type) {
2062+
tag = TempObjectTempVar() and
2063+
type = getTypeForPRValue(expr.getType())
2064+
}
2065+
2066+
override Type getTargetType() { result = expr.getType() }
2067+
2068+
final override TranslatedInitialization getInitialization() {
2069+
result = getTranslatedInitialization(expr.getExpr())
2070+
}
2071+
2072+
final override IRVariable getIRVariable() { result = getIRTempVariable(expr, TempObjectTempVar()) }
2073+
2074+
final override Instruction getInitializationSuccessor() {
2075+
result = getParent().getChildSuccessor(this)
2076+
}
2077+
2078+
final override Instruction getResult() {
2079+
result = getTargetAddress()
2080+
}
2081+
}
2082+
20522083
/**
20532084
* IR translation of a `throw` expression.
20542085
*/

cpp/ql/src/semmle/code/cpp/ir/internal/TempVariableTag.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ newtype TTempVariableTag =
44
ThrowTempVar() or
55
LambdaTempVar() or
66
EllipsisTempVar() or
7-
ThisTempVar()
7+
ThisTempVar() or
8+
TempObjectTempVar()
89

910
string getTempVariableTagId(TTempVariableTag tag) {
1011
tag = ConditionValueTempVar() and result = "CondVal"
@@ -18,4 +19,6 @@ string getTempVariableTagId(TTempVariableTag tag) {
1819
tag = EllipsisTempVar() and result = "Ellipsis"
1920
or
2021
tag = ThisTempVar() and result = "This"
22+
or
23+
tag = TempObjectTempVar() and result = "Temp"
2124
}

cpp/ql/src/semmlecode.cpp.dbscheme

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ conversionkinds(
11711171
| @parexpr
11721172
| @reference_to
11731173
| @ref_indirect
1174+
| @temp_init
11741175
;
11751176

11761177
/*
@@ -1673,6 +1674,7 @@ case @expr.kind of
16731674
| 326 = @spaceshipexpr
16741675
| 327 = @co_await
16751676
| 328 = @co_yield
1677+
| 329 = @temp_init
16761678
;
16771679

16781680
@var_args_expr = @vastartexpr

0 commit comments

Comments
 (0)