@@ -44,7 +44,7 @@ private module GuardsInput implements
4444 }
4545
4646 private class NullConstant extends ConstantExpr {
47- NullConstant ( ) { nullValueImplied ( this ) }
47+ NullConstant ( ) { nullValue ( this ) }
4848
4949 override predicate isNull ( ) { any ( ) }
5050 }
@@ -86,7 +86,7 @@ private module GuardsInput implements
8686 }
8787
8888 class NonNullExpr extends Expr {
89- NonNullExpr ( ) { nonNullValueImplied ( this ) }
89+ NonNullExpr ( ) { nonNullValue ( this ) }
9090 }
9191
9292 class Case extends AstNode instanceof CS:: Case {
@@ -234,7 +234,7 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
234234 e instanceof DereferenceableExpr and
235235 ct .getAnArgument ( ) = e and
236236 ct .getAnArgument ( ) = arg and
237- nonNullValueImplied ( arg ) and
237+ nonNullValue ( arg ) and
238238 ck = ct .getComparisonKind ( ) and
239239 e != arg and
240240 isNull = false and
@@ -376,7 +376,7 @@ deprecated module AbstractValues {
376376 predicate isNonNull ( ) { this .isNonNullValue ( ) }
377377
378378 DereferenceableExpr getAnExpr ( ) {
379- if this .isNull ( ) then nullValueImplied ( result ) else nonNullValueImplied ( result )
379+ if this .isNull ( ) then nullValue ( result ) else nonNullValue ( result )
380380 }
381381 }
382382}
@@ -440,13 +440,13 @@ class DereferenceableExpr extends Expr {
440440
441441 /** Holds if `guard` suggests that this expression may be `null`. */
442442 predicate guardSuggestsMaybeNull ( Guards:: Guard guard ) {
443- not nonNullValueImplied ( this ) and
443+ not nonNullValue ( this ) and
444444 (
445445 exists ( NullnessCompletion c | c .isValidFor ( this ) and c .isNull ( ) and guard = this )
446446 or
447447 LogicInput:: additionalNullCheck ( guard , _, this , true )
448448 or
449- guard .isEquality ( this , any ( Expr n | nullValueImplied ( n ) ) , _)
449+ guard .isEquality ( this , any ( Expr n | nullValue ( n ) ) , _)
450450 or
451451 Guards:: nullGuard ( guard , any ( GuardValue v | exists ( v .asBooleanValue ( ) ) ) , this , true )
452452 )
@@ -917,7 +917,7 @@ module Internal {
917917 bao .getAnOperand ( ) = o and
918918 // The other operand must be provably non-null in order
919919 // for `only if` to hold
920- nonNullValueImplied ( o ) and
920+ nonNullValue ( o ) and
921921 e != o
922922 )
923923 }
@@ -936,154 +936,21 @@ module Internal {
936936 e = any ( BinaryArithmeticOperation bao | result = bao .getAnOperand ( ) )
937937 }
938938
939- // The predicates in this module should be evaluated in the same stage as the CFG
940- // construction stage. This is to avoid recomputation of pre-basic-blocks and
941- // pre-SSA predicates
942- private module PreCfg {
943- private import semmle.code.csharp.controlflow.internal.PreBasicBlocks as PreBasicBlocks
944- private import semmle.code.csharp.controlflow.internal.PreSsa
945-
946- private predicate nullDef ( PreSsa:: Definition def ) {
947- nullValueImplied ( def .getDefinition ( ) .getSource ( ) )
948- }
949-
950- private predicate nonNullDef ( PreSsa:: Definition def ) {
951- nonNullValueImplied ( def .getDefinition ( ) .getSource ( ) )
952- }
953-
954- private predicate emptyDef ( PreSsa:: Definition def ) {
955- emptyValue ( def .getDefinition ( ) .getSource ( ) )
956- }
957-
958- private predicate nonEmptyDef ( PreSsa:: Definition def ) {
959- nonEmptyValue ( def .getDefinition ( ) .getSource ( ) )
960- }
961-
962- deprecated predicate isGuard ( Expr e , GuardValue val ) {
963- (
964- e .getType ( ) instanceof BoolType and
965- not e instanceof BoolLiteral and
966- not e instanceof SwitchCaseExpr and
967- not e instanceof PatternExpr and
968- exists ( val .asBooleanValue ( ) )
969- or
970- e instanceof DereferenceableExpr and
971- val .isNullness ( _)
972- ) and
973- not e = any ( ExprStmt es ) .getExpr ( ) and
974- not e = any ( LocalVariableDeclStmt s ) .getAVariableDeclExpr ( )
975- }
976-
977- cached
978- private module CachedWithCfg {
979- private import semmle.code.csharp.Caching
980-
981- private predicate firstReadSameVarUniquePredecessor (
982- PreSsa:: Definition def , AssignableRead read
983- ) {
984- read = def .getAFirstRead ( ) and
985- (
986- not PreSsa:: adjacentReadPairSameVar ( _, read )
987- or
988- read = unique( AssignableRead read0 | PreSsa:: adjacentReadPairSameVar ( read0 , read ) )
989- )
990- }
991-
992- cached
993- predicate nullValueImplied ( Expr e ) {
994- nullValue ( e )
995- or
996- exists ( Expr e1 | nullValueImplied ( e1 ) and nullValueImpliedUnary ( e1 , e ) )
997- or
998- exists ( Expr e1 , Expr e2 |
999- nullValueImplied ( e1 ) and nullValueImplied ( e2 ) and nullValueImpliedBinary ( e1 , e2 , e )
1000- )
1001- or
1002- e =
1003- any ( PreSsa:: Definition def |
1004- forex ( PreSsa:: Definition u | u = def .getAnUltimateDefinition ( ) | nullDef ( u ) )
1005- ) .getARead ( )
1006- }
1007-
1008- cached
1009- predicate nonNullValueImplied ( Expr e ) {
1010- nonNullValue ( e )
1011- or
1012- exists ( Expr e1 | nonNullValueImplied ( e1 ) and nonNullValueImpliedUnary ( e1 , e ) )
1013- or
1014- e =
1015- any ( PreSsa:: Definition def |
1016- forex ( PreSsa:: Definition u | u = def .getAnUltimateDefinition ( ) | nonNullDef ( u ) )
1017- ) .getARead ( )
1018- }
1019-
1020- private predicate adjacentReadPairSameVarUniquePredecessor (
1021- AssignableRead read1 , AssignableRead read2
1022- ) {
1023- PreSsa:: adjacentReadPairSameVar ( read1 , read2 ) and
1024- (
1025- read1 = read2 and
1026- read1 = unique( AssignableRead other | PreSsa:: adjacentReadPairSameVar ( other , read2 ) )
1027- or
1028- read1 =
1029- unique( AssignableRead other |
1030- PreSsa:: adjacentReadPairSameVar ( other , read2 ) and other != read2
1031- )
1032- )
1033- }
1034-
1035- cached
1036- predicate emptyValue ( Expr e ) {
1037- e .( ArrayCreation ) .getALengthArgument ( ) .getValue ( ) .toInt ( ) = 0
1038- or
1039- e .( ArrayInitializer ) .hasNoElements ( )
1040- or
1041- exists ( Expr mid | emptyValue ( mid ) |
1042- mid = e .( AssignExpr ) .getRValue ( )
1043- or
1044- mid = e .( Cast ) .getExpr ( )
1045- )
1046- or
1047- exists ( PreSsa:: Definition def | emptyDef ( def ) | firstReadSameVarUniquePredecessor ( def , e ) )
1048- or
1049- exists ( MethodCall mc |
1050- mc .getTarget ( ) .getAnUltimateImplementee ( ) .getUnboundDeclaration ( ) =
1051- any ( SystemCollectionsGenericICollectionInterface c ) .getClearMethod ( ) and
1052- adjacentReadPairSameVarUniquePredecessor ( mc .getQualifier ( ) , e )
1053- )
1054- }
1055-
1056- cached
1057- predicate nonEmptyValue ( Expr e ) {
1058- forex ( Expr length | length = e .( ArrayCreation ) .getALengthArgument ( ) |
1059- length .getValue ( ) .toInt ( ) != 0
1060- )
1061- or
1062- e .( ArrayInitializer ) .getNumberOfElements ( ) > 0
1063- or
1064- exists ( Expr mid | nonEmptyValue ( mid ) |
1065- mid = e .( AssignExpr ) .getRValue ( )
1066- or
1067- mid = e .( Cast ) .getExpr ( )
1068- )
1069- or
1070- exists ( PreSsa:: Definition def | nonEmptyDef ( def ) |
1071- firstReadSameVarUniquePredecessor ( def , e )
1072- )
1073- or
1074- exists ( MethodCall mc |
1075- mc .getTarget ( ) .getAnUltimateImplementee ( ) .getUnboundDeclaration ( ) =
1076- any ( SystemCollectionsGenericICollectionInterface c ) .getAddMethod ( ) and
1077- adjacentReadPairSameVarUniquePredecessor ( mc .getQualifier ( ) , e )
1078- )
1079- }
1080- }
1081-
1082- import CachedWithCfg
939+ deprecated predicate isGuard ( Expr e , GuardValue val ) {
940+ (
941+ e .getType ( ) instanceof BoolType and
942+ not e instanceof BoolLiteral and
943+ not e instanceof SwitchCaseExpr and
944+ not e instanceof PatternExpr and
945+ exists ( val .asBooleanValue ( ) )
946+ or
947+ e instanceof DereferenceableExpr and
948+ val .isNullness ( _)
949+ ) and
950+ not e = any ( ExprStmt es ) .getExpr ( ) and
951+ not e = any ( LocalVariableDeclStmt s ) .getAVariableDeclExpr ( )
1083952 }
1084953
1085- import PreCfg
1086-
1087954 private predicate interestingDescendantCandidate ( Expr e ) {
1088955 guardControls ( e , _, _)
1089956 or
0 commit comments