Skip to content

C++: Fix small PrintAST and PrintIR issue #18277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cpp/ql/lib/semmle/code/cpp/PrintAST.qll
Original file line number Diff line number Diff line change
Expand Up @@ -954,17 +954,26 @@ private predicate namedExprChildPredicates(Expr expr, Element ele, string pred)
expr.(C11GenericExpr).getAssociationExpr(n) = ele and pred = "getAssociationExpr(" + n + ")"
)
or
expr.(Call).getQualifier() = ele and pred = "getQualifier()"
or
exists(int n | expr.(Call).getArgument(n) = ele and pred = "getArgument(" + n.toString() + ")")
// OverloadedArrayExpr::getArrayBase/0 also considers qualifiers, and is already handled below.
not expr.(OverloadedArrayExpr).getArrayBase() = expr.(Call).getQualifier() and
expr.(Call).getQualifier() = ele and
pred = "getQualifier()"
or
// OverloadedArrayExpr::getArrayBase/0 and OverloadedArrayExpr::getArrayOffset/0 also consider arguments, and are already handled below.
exists(int n, Expr arg | expr.(Call).getArgument(n) = arg |
not expr.(OverloadedArrayExpr).getArrayBase() = arg and
not expr.(OverloadedArrayExpr).getArrayOffset() = arg and
arg = ele and
pred = "getArgument(" + n.toString() + ")"
)
or
expr.(ExprCall).getExpr() = ele and pred = "getExpr()"
or
expr.(OverloadedArrayExpr).getArrayBase() = ele and pred = "getArrayBase()"
or
expr.(OverloadedArrayExpr).getArrayOffset() = ele and pred = "getArrayOffset()"
or
// OverloadedPointerDereferenceExpr::getExpr/0 also considers qualifiers, which are already handled above for all Call classes.
// OverloadedPointerDereferenceExpr::getExpr/0 also considers qualifiers, and is already handled above for all Call classes.
not expr.(OverloadedPointerDereferenceExpr).getQualifier() =
expr.(OverloadedPointerDereferenceExpr).getExpr() and
expr.(OverloadedPointerDereferenceExpr).getExpr() = ele and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,22 @@ private int getSuccessorIndex(IRBlock pred, IRBlock succ) {
* has the given `value`.
*/
query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) {
exists(EdgeKind kind, IRBlock predBlock, IRBlock succBlock |
exists(IRBlock predBlock, IRBlock succBlock |
predBlock = pred.getBlock() and
succBlock = succ.getBlock() and
predBlock.getSuccessor(kind) = succBlock and
(
(
key = "semmle.label" and
if predBlock.getBackEdgeSuccessor(kind) = succBlock
then value = kind.toString() + " (back edge)"
else value = kind.toString()
key = "semmle.label" and
exists(string kinds |
kinds =
strictconcat(EdgeKind k |
predBlock.getSuccessor(k) = succBlock
|
k.toString(), "|" order by k.toString()
)
|
if predBlock.getBackEdgeSuccessor(_) = succBlock
then value = kinds + " (back edge)"
else value = kinds
)
or
key = "semmle.order" and
Expand Down
20 changes: 13 additions & 7 deletions cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/PrintIR.qll
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,22 @@ private int getSuccessorIndex(IRBlock pred, IRBlock succ) {
* has the given `value`.
*/
query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) {
exists(EdgeKind kind, IRBlock predBlock, IRBlock succBlock |
exists(IRBlock predBlock, IRBlock succBlock |
predBlock = pred.getBlock() and
succBlock = succ.getBlock() and
predBlock.getSuccessor(kind) = succBlock and
(
(
key = "semmle.label" and
if predBlock.getBackEdgeSuccessor(kind) = succBlock
then value = kind.toString() + " (back edge)"
else value = kind.toString()
key = "semmle.label" and
exists(string kinds |
kinds =
strictconcat(EdgeKind k |
predBlock.getSuccessor(k) = succBlock
|
k.toString(), "|" order by k.toString()
)
|
if predBlock.getBackEdgeSuccessor(_) = succBlock
then value = kinds + " (back edge)"
else value = kinds
)
or
key = "semmle.order" and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,22 @@ private int getSuccessorIndex(IRBlock pred, IRBlock succ) {
* has the given `value`.
*/
query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) {
exists(EdgeKind kind, IRBlock predBlock, IRBlock succBlock |
exists(IRBlock predBlock, IRBlock succBlock |
predBlock = pred.getBlock() and
succBlock = succ.getBlock() and
predBlock.getSuccessor(kind) = succBlock and
(
(
key = "semmle.label" and
if predBlock.getBackEdgeSuccessor(kind) = succBlock
then value = kind.toString() + " (back edge)"
else value = kind.toString()
key = "semmle.label" and
exists(string kinds |
kinds =
strictconcat(EdgeKind k |
predBlock.getSuccessor(k) = succBlock
|
k.toString(), "|" order by k.toString()
)
|
if predBlock.getBackEdgeSuccessor(_) = succBlock
then value = kinds + " (back edge)"
else value = kinds
)
or
key = "semmle.order" and
Expand Down
32 changes: 32 additions & 0 deletions cpp/ql/test/library-tests/ir/ir/PrintAST.expected
Original file line number Diff line number Diff line change
Expand Up @@ -24086,6 +24086,38 @@ ir.cpp:
# 2717| ValueCategory = prvalue
# 2717| getThen(): [BlockStmt] { ... }
# 2718| getStmt(16): [ReturnStmt] return ...
# 2720| [CopyAssignmentOperator] WithBracketOperator& WithBracketOperator::operator=(WithBracketOperator const&)
# 2720| <params>:
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
#-----| Type = [LValueReferenceType] const WithBracketOperator &
# 2720| [MoveAssignmentOperator] WithBracketOperator& WithBracketOperator::operator=(WithBracketOperator&&)
# 2720| <params>:
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
#-----| Type = [RValueReferenceType] WithBracketOperator &&
# 2721| [ConstMemberFunction] char const& WithBracketOperator::operator[](int) const
# 2721| <params>:
# 2721| getParameter(0): [Parameter] pos
# 2721| Type = [IntType] int
# 2724| [TopLevelFunction] char UseBracketOperator(WithBracketOperator const, int)
# 2724| <params>:
# 2724| getParameter(0): [Parameter] x
# 2724| Type = [SpecifiedType] const WithBracketOperator
# 2724| getParameter(1): [Parameter] i
# 2724| Type = [IntType] int
# 2724| getEntryPoint(): [BlockStmt] { ... }
# 2725| getStmt(0): [ReturnStmt] return ...
# 2725| getExpr(): [OverloadedArrayExpr] call to operator[]
# 2725| Type = [LValueReferenceType] const char &
# 2725| ValueCategory = prvalue
# 2725| getArrayBase(): [VariableAccess] x
# 2725| Type = [SpecifiedType] const WithBracketOperator
# 2725| ValueCategory = lvalue
# 2725| getArrayOffset(): [VariableAccess] i
# 2725| Type = [IntType] int
# 2725| ValueCategory = prvalue(load)
# 2725| getExpr().getFullyConverted(): [ReferenceDereferenceExpr] (reference dereference)
# 2725| Type = [PlainCharType] char
# 2725| ValueCategory = prvalue(load)
many-defs-per-use.cpp:
# 34| [TopLevelFunction] void many_defs_per_use()
# 34| <params>:
Expand Down
27 changes: 27 additions & 0 deletions cpp/ql/test/library-tests/ir/ir/aliased_ir.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19713,6 +19713,33 @@ ir.cpp:
# 2698| v2698_10(void) = AliasedUse : m2698_3
# 2698| v2698_11(void) = ExitFunction :

# 2724| char UseBracketOperator(WithBracketOperator const, int)
# 2724| Block 0
# 2724| v2724_1(void) = EnterFunction :
# 2724| m2724_2(unknown) = AliasedDefinition :
# 2724| m2724_3(unknown) = InitializeNonLocal :
# 2724| m2724_4(unknown) = Chi : total:m2724_2, partial:m2724_3
# 2724| r2724_5(glval<WithBracketOperator>) = VariableAddress[x] :
# 2724| m2724_6(WithBracketOperator) = InitializeParameter[x] : &:r2724_5
# 2724| m2724_7(unknown) = Chi : total:m2724_4, partial:m2724_6
# 2724| r2724_8(glval<int>) = VariableAddress[i] :
# 2724| m2724_9(int) = InitializeParameter[i] : &:r2724_8
# 2725| r2725_1(glval<char>) = VariableAddress[#return] :
# 2725| r2725_2(glval<WithBracketOperator>) = VariableAddress[x] :
# 2725| r2725_3(glval<unknown>) = FunctionAddress[operator[]] :
# 2725| r2725_4(glval<int>) = VariableAddress[i] :
# 2725| r2725_5(int) = Load[i] : &:r2725_4, m2724_9
# 2725| r2725_6(char &) = Call[operator[]] : func:r2725_3, this:r2725_2, 0:r2725_5
# 2725| m2725_7(unknown) = ^CallSideEffect : ~m2724_7
# 2725| m2725_8(unknown) = Chi : total:m2724_7, partial:m2725_7
# 2725| v2725_9(void) = ^IndirectReadSideEffect[-1] : &:r2725_2, ~m2725_8
# 2725| r2725_10(char) = Load[?] : &:r2725_6, ~m2725_8
# 2725| m2725_11(char) = Store[#return] : &:r2725_1, r2725_10
# 2724| r2724_10(glval<char>) = VariableAddress[#return] :
# 2724| v2724_11(void) = ReturnValue : &:r2724_10, m2725_11
# 2724| v2724_12(void) = AliasedUse : ~m2725_8
# 2724| v2724_13(void) = ExitFunction :

many-defs-per-use.cpp:
# 34| void many_defs_per_use()
# 34| Block 0
Expand Down
8 changes: 8 additions & 0 deletions cpp/ql/test/library-tests/ir/ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2717,4 +2717,12 @@ void branch_on_integral_in_cpp(int x1, int x2) {
if(!x_1_and_2) {}
}

struct WithBracketOperator {
const char& operator[](int pos) const;
};

char UseBracketOperator(const WithBracketOperator x, int i) {
return x[i];
}

// semmle-extractor-options: -std=c++20 --clang
24 changes: 24 additions & 0 deletions cpp/ql/test/library-tests/ir/ir/raw_ir.expected
Original file line number Diff line number Diff line change
Expand Up @@ -18031,6 +18031,30 @@ ir.cpp:
# 2698| v2698_9(void) = AliasedUse : ~m?
# 2698| v2698_10(void) = ExitFunction :

# 2724| char UseBracketOperator(WithBracketOperator const, int)
# 2724| Block 0
# 2724| v2724_1(void) = EnterFunction :
# 2724| mu2724_2(unknown) = AliasedDefinition :
# 2724| mu2724_3(unknown) = InitializeNonLocal :
# 2724| r2724_4(glval<WithBracketOperator>) = VariableAddress[x] :
# 2724| mu2724_5(WithBracketOperator) = InitializeParameter[x] : &:r2724_4
# 2724| r2724_6(glval<int>) = VariableAddress[i] :
# 2724| mu2724_7(int) = InitializeParameter[i] : &:r2724_6
# 2725| r2725_1(glval<char>) = VariableAddress[#return] :
# 2725| r2725_2(glval<WithBracketOperator>) = VariableAddress[x] :
# 2725| r2725_3(glval<unknown>) = FunctionAddress[operator[]] :
# 2725| r2725_4(glval<int>) = VariableAddress[i] :
# 2725| r2725_5(int) = Load[i] : &:r2725_4, ~m?
# 2725| r2725_6(char &) = Call[operator[]] : func:r2725_3, this:r2725_2, 0:r2725_5
# 2725| mu2725_7(unknown) = ^CallSideEffect : ~m?
# 2725| v2725_8(void) = ^IndirectReadSideEffect[-1] : &:r2725_2, ~m?
# 2725| r2725_9(char) = Load[?] : &:r2725_6, ~m?
# 2725| mu2725_10(char) = Store[#return] : &:r2725_1, r2725_9
# 2724| r2724_8(glval<char>) = VariableAddress[#return] :
# 2724| v2724_9(void) = ReturnValue : &:r2724_8, ~m?
# 2724| v2724_10(void) = AliasedUse : ~m?
# 2724| v2724_11(void) = ExitFunction :

many-defs-per-use.cpp:
# 34| void many_defs_per_use()
# 34| Block 0
Expand Down
Loading