Skip to content

Commit

Permalink
S2E1
Browse files Browse the repository at this point in the history
  • Loading branch information
csaba-sagi-sonarsource committed Mar 21, 2022
1 parent 9a166df commit c7e2512
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ other is not null
&& other.OperationValue.DictionaryEquals(OperationValue)
&& other.SymbolValue.DictionaryEquals(SymbolValue)
&& other.CaptureOperation.DictionaryEquals(CaptureOperation)
&& other.PreservedSymbols.Count == PreservedSymbols.Count
&& other.PreservedSymbols.All(x => PreservedSymbols.Contains(x));
&& other.PreservedSymbols.SetEquals(PreservedSymbols);

public override string ToString() =>
Equals(Empty) ? "Empty" : SerializeSymbols() + SerializeOperations() + SerializeCaptures();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Microsoft.CodeAnalysis;
using SonarAnalyzer.CFG.LiveVariableAnalysis;
using SonarAnalyzer.CFG.Roslyn;
using SonarAnalyzer.Helpers;
using SonarAnalyzer.SymbolicExecution.Constraints;
using SonarAnalyzer.SymbolicExecution.Roslyn.Checks;
using SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors;
Expand Down Expand Up @@ -93,7 +94,6 @@ private IEnumerable<ExplodedNode> ProcessBranching(ExplodedNode node)
}
else if (node.Block.ContainsThrow())
{
node = new ExplodedNode(cfg.ExitBlock, CleanUnusedState(node.State, node.Block), null);
yield return CreateNode(cfg.ExitBlock, null, null);
}
else
Expand Down Expand Up @@ -174,7 +174,7 @@ private static ProgramState ProcessOperation(SymbolicContext context) =>

private ProgramState CleanUnusedState(ProgramState programState, BasicBlock block)
{
var liveVariables = lva.LiveOut(block);
var liveVariables = lva.LiveOut(block).ToHashSet();
return programState.RemoveSymbols(x => (x is ILocalSymbol or IParameterSymbol { RefKind: RefKind.None }) && !liveVariables.Contains(x));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void SymbolsWith_IgnoreNullValue()
}

[TestMethod]
public void Preserve_PreservedSymbolCannotBeDeleted()
public void Preserve_PreservedSymbolCannotBeRemoved()
{
var symbolicValue = new SymbolicValue(new()).WithConstraint(DummyConstraint.Dummy);
var symbol = CreateSymbols().First();
Expand All @@ -132,7 +132,7 @@ public void Preserve_PreservedSymbolCannotBeDeleted()
}

[TestMethod]
public void RemoveSymbols_RemovesTheSymbolMatchingThePredicate()
public void RemoveSymbols_RemovesSymbolsMatchingThePredicate()
{
var symbolicValue = new SymbolicValue(new()).WithConstraint(DummyConstraint.Dummy);
var symbols = CreateSymbols().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void Branching_FieldSymbolsAreNotCleaned()
[DataTestMethod]
[DataRow("out", "outParam")]
[DataRow("ref", "refParam")]
public void Branching_RefAndOutParameters_NotCleared(string paramType, string paramName)
public void Branching_RefAndOutParameters_NotCleared(string refKind, string paramName)
{
var code = $@"
if (boolParameter)
Expand Down Expand Up @@ -293,7 +293,7 @@ public void Branching_RefAndOutParameters_NotCleared(string paramType, string pa
}
return x.State;
});
var validator = SETestContext.CreateCS(code, $", {paramType} int {paramName}", postProcess).Validator;
var validator = SETestContext.CreateCS(code, $", {refKind} int {paramName}", postProcess).Validator;
validator.ValidateExitReachCount(2); // Once with First constraint, once with Second constraint on "value"
validator.TagValues("End").Should().HaveCount(2)
.And.ContainSingle(x => x.HasConstraint(TestConstraint.First))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Loops_InstructionVisitedMaxTwice_EvenWithMultipleStates()
{
const string code = @"
var value = 42;
bool condition;
bool condition = false;
if(boolParameter) // This generates two different ProgramStates, each tracks its own visits
condition = true;
else
Expand All @@ -64,10 +64,9 @@ public void Loops_InstructionVisitedMaxTwice_EvenWithMultipleStates()
{
value.ToString(); // Add another constraint to 'value'
} while (boolParameter);
Tag(""HoldRef"", condition);
Tag(""End"", value);";
var validator = SETestContext.CreateCS(code, ", int[] items", new AddConstraintOnInvocationCheck()).Validator;
validator.ValidateExitReachCount(1);
var validator = SETestContext.CreateCS(code, ", int[] items", new AddConstraintOnInvocationCheck(), new PreserveTestCheck("condition")).Validator;
validator.ValidateExitReachCount(2);
var states = validator.TagStates("End");
var condition = states.SelectMany(x => x.SymbolsWith(BoolConstraint.False)).First(); // "False" is never set for "value"
var value = states.SelectMany(x => x.SymbolsWith(TestConstraint.First)).First(); // "First" is never set for "condition"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,51 +179,28 @@ static ISymbol LocalReferenceOperationSymbol(IOperationWrapperSonar operation) =
public void Execute_UnusedVariable_ClearedAfterBlock()
{
const string code = @"
bool first;
if(boolParameter)
first = true;
else
first = false;
var first = boolParameter ? true : false;
Tag(""BeforeLastUse"");
bool second = first;
if(boolParameter)
boolParameter.ToString();
boolParameter.ToString();
Tag(""AfterLastUse"");
bool third = second;
if(boolParameter)
boolParameter.ToString();
Tag(""END"");
";
ISymbol firstSymbol = null;
ISymbol secondSymbol = null;
var postProcess = new PostProcessTestCheck(x =>
{
if (x.Operation.Instance.TrackedSymbol() is { } symbol)
{
if (symbol.Name.Equals("first"))
if (symbol.Name == "first")
{
firstSymbol = symbol;
}
else if (symbol.Name.Equals("second"))
{
secondSymbol = symbol;
}
}
return x.State;
});
var validator = SETestContext.CreateCS(code, postProcess).Validator;
validator.ValidateTagOrder(
"BeforeLastUse",
"BeforeLastUse",
"AfterLastUse",
"AfterLastUse",
"END");
var beforeLastUseStates = validator.TagStates("BeforeLastUse");
beforeLastUseStates.Should().HaveCount(2);
beforeLastUseStates.All(x => x[firstSymbol] != null).Should().BeTrue();
var afterLastUseStates = validator.TagStates("AfterLastUse");
afterLastUseStates.Should().HaveCount(2);
afterLastUseStates.All(x => x[firstSymbol] == null && x[secondSymbol] != null).Should().BeTrue();
validator.TagStates("BeforeLastUse").Should().HaveCount(2).And.OnlyContain(x => x[firstSymbol] != null);
validator.TagStates("AfterLastUse").Should().HaveCount(1).And.OnlyContain(x => x[firstSymbol] == null);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public PreserveTestCheck(string symbolName) =>
this.symbolName = symbolName;

protected override ProgramState PreProcessSimple(SymbolicContext context) =>
context.Operation.Instance.TrackedSymbol() is { } symbol
&& symbol.Name == symbolName
context.Operation.Instance.TrackedSymbol() is { } symbol && symbol.Name == symbolName
? context.State.Preserve(symbol)
: context.State;
}
Expand Down

0 comments on commit c7e2512

Please sign in to comment.