-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Processor for deconstruction operations
- Loading branch information
1 parent
b36349a
commit c2a0b7c
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...rAnalyzer.Common/SymbolicExecution/Roslyn/OperationProcessors/DeconstructionAssignment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2023 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.SymbolicExecution.Roslyn.OperationProcessors; | ||
|
||
internal sealed class DeconstructionAssignment : SimpleProcessor<IDeconstructionAssignmentOperationWrapper> | ||
{ | ||
protected override IDeconstructionAssignmentOperationWrapper Convert(IOperation operation) => | ||
IDeconstructionAssignmentOperationWrapper.FromOperation(operation); | ||
|
||
protected override ProgramState Process(SymbolicContext context, IDeconstructionAssignmentOperationWrapper assignment) => | ||
Process(context.State, assignment.Target, assignment.Value); | ||
|
||
private static ProgramState Process(ProgramState state, IOperation target, IOperation value) | ||
{ | ||
var leftTupleElements = UnwrapDeclaration(target).ToTuple().Elements; | ||
|
||
// If the right side is a tuple, then every symbol/constraint is copied to the left side. | ||
if (value.AsTuple() is { } rightSideTuple) | ||
{ | ||
for (var i = 0; i < leftTupleElements.Length; i++) | ||
{ | ||
var leftTupleMember = UnwrapDeclaration(leftTupleElements[i]); | ||
var rightTupleMember = rightSideTuple.Elements[i]; | ||
if (leftTupleMember.Kind == OperationKindEx.Discard) | ||
{ | ||
continue; | ||
} | ||
if (leftTupleMember.AsTuple() is { } nestedTuple) | ||
{ | ||
var rightSideMember = rightTupleMember.AsTuple()?.WrappedOperation ?? rightTupleMember; | ||
state = Process(state, nestedTuple.WrappedOperation, rightSideMember); | ||
} | ||
else | ||
{ | ||
state = state.SetOperationAndSymbolValue(leftTupleMember, state[rightTupleMember]); | ||
} | ||
} | ||
} | ||
// If the right side is not a tuple, then every member of the left side tuple is set to empty. | ||
else | ||
{ | ||
foreach (var tupleMember in leftTupleElements.Where(x => x.Kind != OperationKindEx.Discard)) | ||
{ | ||
state = state.SetOperationAndSymbolValue(tupleMember, SymbolicValue.Empty); | ||
} | ||
} | ||
return state; | ||
} | ||
|
||
private static IOperation UnwrapDeclaration(IOperation operation) => | ||
operation.AsDeclarationExpression()?.Expression ?? operation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters