Skip to content

Commit 52db1c1

Browse files
committed
C++: Introduce abstract class to share code between if and constexpr if
1 parent fa06d88 commit 52db1c1

File tree

1 file changed

+27
-65
lines changed

1 file changed

+27
-65
lines changed

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

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,7 @@ class TranslatedCatchAnyHandler extends TranslatedHandler {
842842
}
843843
}
844844

845-
class TranslatedIfStmt extends TranslatedStmt, ConditionContext {
846-
override IfStmt stmt;
847-
845+
abstract class TranslatedIfLikeStmt extends TranslatedStmt, ConditionContext {
848846
override Instruction getFirstInstruction(EdgeKind kind) {
849847
if this.hasInitialization()
850848
then result = this.getInitialization().getFirstInstruction(kind)
@@ -867,25 +865,21 @@ class TranslatedIfStmt extends TranslatedStmt, ConditionContext {
867865
id = 3 and result = this.getElse()
868866
}
869867

870-
private predicate hasInitialization() { exists(stmt.getInitialization()) }
868+
abstract predicate hasInitialization();
871869

872-
private TranslatedStmt getInitialization() {
873-
result = getTranslatedStmt(stmt.getInitialization())
874-
}
870+
abstract TranslatedStmt getInitialization();
875871

876-
private TranslatedCondition getCondition() {
877-
result = getTranslatedCondition(stmt.getCondition().getFullyConverted())
878-
}
872+
abstract TranslatedCondition getCondition();
879873

880874
private Instruction getFirstConditionInstruction(EdgeKind kind) {
881875
result = this.getCondition().getFirstInstruction(kind)
882876
}
883877

884-
private TranslatedStmt getThen() { result = getTranslatedStmt(stmt.getThen()) }
878+
abstract TranslatedStmt getThen();
885879

886-
private TranslatedStmt getElse() { result = getTranslatedStmt(stmt.getElse()) }
880+
abstract TranslatedStmt getElse();
887881

888-
private predicate hasElse() { exists(stmt.getElse()) }
882+
abstract predicate hasElse();
889883

890884
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) { none() }
891885

@@ -914,76 +908,44 @@ class TranslatedIfStmt extends TranslatedStmt, ConditionContext {
914908
}
915909
}
916910

917-
class TranslatedConstExprIfStmt extends TranslatedStmt, ConditionContext {
918-
override ConstexprIfStmt stmt;
919-
920-
override Instruction getFirstInstruction(EdgeKind kind) {
921-
if this.hasInitialization()
922-
then result = this.getInitialization().getFirstInstruction(kind)
923-
else result = this.getFirstConditionInstruction(kind)
924-
}
925-
926-
override Instruction getALastInstructionInternal() {
927-
result = this.getElse().getALastInstruction() or result = this.getThen().getALastInstruction()
928-
}
929-
930-
override TranslatedElement getLastChild() { result = this.getElse() or result = this.getThen() }
931-
932-
override TranslatedElement getChildInternal(int id) {
933-
id = 0 and result = this.getInitialization()
934-
or
935-
id = 1 and result = this.getCondition()
936-
or
937-
id = 2 and result = this.getThen()
938-
or
939-
id = 3 and result = this.getElse()
940-
}
911+
class TranslatedIfStmt extends TranslatedIfLikeStmt {
912+
override IfStmt stmt;
941913

942-
private predicate hasInitialization() { exists(stmt.getInitialization()) }
914+
override predicate hasInitialization() { exists(stmt.getInitialization()) }
943915

944-
private TranslatedStmt getInitialization() {
916+
override TranslatedStmt getInitialization() {
945917
result = getTranslatedStmt(stmt.getInitialization())
946918
}
947919

948-
private TranslatedCondition getCondition() {
920+
override TranslatedCondition getCondition() {
949921
result = getTranslatedCondition(stmt.getCondition().getFullyConverted())
950922
}
951923

952-
private Instruction getFirstConditionInstruction(EdgeKind kind) {
953-
result = this.getCondition().getFirstInstruction(kind)
954-
}
924+
override TranslatedStmt getThen() { result = getTranslatedStmt(stmt.getThen()) }
955925

956-
private TranslatedStmt getThen() { result = getTranslatedStmt(stmt.getThen()) }
926+
override TranslatedStmt getElse() { result = getTranslatedStmt(stmt.getElse()) }
957927

958-
private TranslatedStmt getElse() { result = getTranslatedStmt(stmt.getElse()) }
928+
override predicate hasElse() { exists(stmt.getElse()) }
929+
}
959930

960-
private predicate hasElse() { exists(stmt.getElse()) }
931+
class TranslatedConstExprIfStmt extends TranslatedIfLikeStmt {
932+
override ConstexprIfStmt stmt;
961933

962-
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) { none() }
934+
override predicate hasInitialization() { exists(stmt.getInitialization()) }
963935

964-
override Instruction getChildTrueSuccessor(TranslatedCondition child, EdgeKind kind) {
965-
child = this.getCondition() and
966-
result = this.getThen().getFirstInstruction(kind)
936+
override TranslatedStmt getInitialization() {
937+
result = getTranslatedStmt(stmt.getInitialization())
967938
}
968939

969-
override Instruction getChildFalseSuccessor(TranslatedCondition child, EdgeKind kind) {
970-
child = this.getCondition() and
971-
if this.hasElse()
972-
then result = this.getElse().getFirstInstruction(kind)
973-
else result = this.getParent().getChildSuccessor(this, kind)
940+
override TranslatedCondition getCondition() {
941+
result = getTranslatedCondition(stmt.getCondition().getFullyConverted())
974942
}
975943

976-
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
977-
child = this.getInitialization() and
978-
result = this.getFirstConditionInstruction(kind)
979-
or
980-
(child = this.getThen() or child = this.getElse()) and
981-
result = this.getParent().getChildSuccessor(this, kind)
982-
}
944+
override TranslatedStmt getThen() { result = getTranslatedStmt(stmt.getThen()) }
983945

984-
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
985-
none()
986-
}
946+
override TranslatedStmt getElse() { result = getTranslatedStmt(stmt.getElse()) }
947+
948+
override predicate hasElse() { exists(stmt.getElse()) }
987949
}
988950

989951
abstract class TranslatedLoop extends TranslatedStmt, ConditionContext {

0 commit comments

Comments
 (0)