-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d18a67
commit bd720e2
Showing
6 changed files
with
264 additions
and
24 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
49 changes: 49 additions & 0 deletions
49
analyzers/src/SonarAnalyzer.CFG/CfgSerializer/CfgSerializer.RoslynLvaWalker.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,49 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 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. | ||
*/ | ||
|
||
using SonarAnalyzer.CFG.LiveVariableAnalysis; | ||
using SonarAnalyzer.CFG.Roslyn; | ||
|
||
namespace SonarAnalyzer.CFG; | ||
|
||
public static partial class CfgSerializer | ||
{ | ||
private sealed class RoslynLvaWalker : RoslynCfgWalker | ||
{ | ||
private readonly RoslynLiveVariableAnalysis lva; | ||
|
||
public RoslynLvaWalker(RoslynLiveVariableAnalysis lva, DotWriter writer, RoslynCfgIdProvider cfgIdProvider) : base(writer, cfgIdProvider) | ||
{ | ||
this.lva = lva; | ||
} | ||
|
||
protected override void WriteEdges(BasicBlock block) | ||
{ | ||
foreach (var predecessor in lva.BlockPredecessors[block.Ordinal]) | ||
{ | ||
writer.WriteEdge(BlockId(predecessor), BlockId(block), string.Empty); | ||
} | ||
if (block.FallThroughSuccessor is { Destination: null }) | ||
{ | ||
writer.WriteEdge(BlockId(block), "NoDestination_" + BlockId(block), block.FallThroughSuccessor.Semantics.ToString()); | ||
} | ||
} | ||
} | ||
} |
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
176 changes: 176 additions & 0 deletions
176
analyzers/tests/SonarAnalyzer.Test/CFG/Roslyn/RoslynLvaSerializerTest.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,176 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 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. | ||
*/ | ||
|
||
using SonarAnalyzer.CFG; | ||
using SonarAnalyzer.CFG.LiveVariableAnalysis; | ||
|
||
namespace SonarAnalyzer.Test.CFG.Roslyn; | ||
|
||
[TestClass] | ||
public class RoslynLvaSerializerTest | ||
{ | ||
[TestMethod] | ||
public void Serialize_TryCatchFinally() | ||
{ | ||
const string code = """ | ||
class Sample | ||
{ | ||
void Method() | ||
{ | ||
var value = 0; | ||
try | ||
{ | ||
Use(0); | ||
value = 42; | ||
} | ||
catch | ||
{ | ||
Use(value); | ||
value = 1; | ||
} | ||
finally | ||
{ | ||
Use(value); | ||
} | ||
} | ||
|
||
void Use(int v) {} | ||
} | ||
"""; | ||
var dot = CfgSerializer.Serialize(CreateLva(code)); | ||
dot.Should().BeIgnoringLineEndingsAndEmptyLines(""" | ||
digraph "RoslynCfg" { | ||
subgraph "cluster_1" { | ||
label = "LocalLifetime region, Locals: value" | ||
subgraph "cluster_2" { | ||
label = "TryAndFinally region" | ||
subgraph "cluster_3" { | ||
label = "Try region" | ||
subgraph "cluster_4" { | ||
label = "TryAndCatch region" | ||
subgraph "cluster_5" { | ||
label = "Try region" | ||
cfg0_block2 [shape=record label="{BLOCK #2|0#: ExpressionStatementOperation: Use(0);|1#: 0#.Operation: InvocationOperation: Use: Use(0)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: 0|3#: 2#.Value: LiteralOperation: 0|##########|0#: ExpressionStatementOperation: value = 42;|1#: 0#.Operation: SimpleAssignmentOperation: value = 42|2#: 1#.Target: LocalReferenceOperation: value|2#: 1#.Value: LiteralOperation: 42|##########}"] | ||
} | ||
subgraph "cluster_6" { | ||
label = "Catch region: object" | ||
cfg0_block3 [shape=record label="{BLOCK #3|0#: ExpressionStatementOperation: Use(value);|1#: 0#.Operation: InvocationOperation: Use: Use(value)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: value|3#: 2#.Value: LocalReferenceOperation: value|##########|0#: ExpressionStatementOperation: value = 1;|1#: 0#.Operation: SimpleAssignmentOperation: value = 1|2#: 1#.Target: LocalReferenceOperation: value|2#: 1#.Value: LiteralOperation: 1|##########}"] | ||
} | ||
} | ||
} | ||
subgraph "cluster_7" { | ||
label = "Finally region" | ||
cfg0_block4 [shape=record label="{BLOCK #4|0#: ExpressionStatementOperation: Use(value);|1#: 0#.Operation: InvocationOperation: Use: Use(value)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: value|3#: 2#.Value: LocalReferenceOperation: value|##########}"] | ||
} | ||
} | ||
cfg0_block1 [shape=record label="{BLOCK #1|0#: SimpleAssignmentOperation: value = 0|1#: 0#.Target: LocalReferenceOperation: value = 0|1#: 0#.Value: LiteralOperation: 0|##########}"] | ||
} | ||
cfg0_block0 [shape=record label="{ENTRY #0}"] | ||
cfg0_block5 [shape=record label="{EXIT #5}"] | ||
cfg0_block1 -> cfg0_block2 | ||
cfg0_block2 -> cfg0_block3 | ||
cfg0_block1 -> cfg0_block3 | ||
cfg0_block2 -> cfg0_block4 | ||
cfg0_block3 -> cfg0_block4 | ||
cfg0_block4 -> NoDestination_cfg0_block4 [label="StructuredExceptionHandling"] | ||
cfg0_block0 -> cfg0_block1 | ||
cfg0_block4 -> cfg0_block5 | ||
cfg0_block4 -> cfg0_block5 | ||
} | ||
"""); | ||
} | ||
|
||
[TestMethod] | ||
public void Serialize_TryCatchFinallyRethrow() | ||
{ | ||
const string code = """ | ||
class Sample | ||
{ | ||
void Method() | ||
{ | ||
var value = 0; | ||
try | ||
{ | ||
Use(0); | ||
value = 42; | ||
} | ||
catch | ||
{ | ||
Use(value); | ||
value = 1; | ||
throw; | ||
} | ||
finally | ||
{ | ||
Use(value); | ||
} | ||
} | ||
|
||
void Use(int v) {} | ||
} | ||
"""; | ||
var dot = CfgSerializer.Serialize(CreateLva(code)); | ||
dot.Should().BeIgnoringLineEndingsAndEmptyLines(""" | ||
digraph "RoslynCfg" { | ||
subgraph "cluster_1" { | ||
label = "LocalLifetime region, Locals: value" | ||
subgraph "cluster_2" { | ||
label = "TryAndFinally region" | ||
subgraph "cluster_3" { | ||
label = "Try region" | ||
subgraph "cluster_4" { | ||
label = "TryAndCatch region" | ||
subgraph "cluster_5" { | ||
label = "Try region" | ||
cfg0_block2 [shape=record label="{BLOCK #2|0#: ExpressionStatementOperation: Use(0);|1#: 0#.Operation: InvocationOperation: Use: Use(0)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: 0|3#: 2#.Value: LiteralOperation: 0|##########|0#: ExpressionStatementOperation: value = 42;|1#: 0#.Operation: SimpleAssignmentOperation: value = 42|2#: 1#.Target: LocalReferenceOperation: value|2#: 1#.Value: LiteralOperation: 42|##########}"] | ||
} | ||
subgraph "cluster_6" { | ||
label = "Catch region: object" | ||
cfg0_block3 [shape=record label="{BLOCK #3|0#: ExpressionStatementOperation: Use(value);|1#: 0#.Operation: InvocationOperation: Use: Use(value)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: value|3#: 2#.Value: LocalReferenceOperation: value|##########|0#: ExpressionStatementOperation: value = 1;|1#: 0#.Operation: SimpleAssignmentOperation: value = 1|2#: 1#.Target: LocalReferenceOperation: value|2#: 1#.Value: LiteralOperation: 1|##########}"] | ||
} | ||
} | ||
} | ||
subgraph "cluster_7" { | ||
label = "Finally region" | ||
cfg0_block4 [shape=record label="{BLOCK #4|0#: ExpressionStatementOperation: Use(value);|1#: 0#.Operation: InvocationOperation: Use: Use(value)|2#: 1#.Instance: InstanceReferenceOperation: Use|2#: ArgumentOperation: value|3#: 2#.Value: LocalReferenceOperation: value|##########}"] | ||
} | ||
} | ||
cfg0_block1 [shape=record label="{BLOCK #1|0#: SimpleAssignmentOperation: value = 0|1#: 0#.Target: LocalReferenceOperation: value = 0|1#: 0#.Value: LiteralOperation: 0|##########}"] | ||
} | ||
cfg0_block0 [shape=record label="{ENTRY #0}"] | ||
cfg0_block5 [shape=record label="{EXIT #5}"] | ||
cfg0_block1 -> cfg0_block2 | ||
cfg0_block2 -> cfg0_block3 | ||
cfg0_block1 -> cfg0_block3 | ||
cfg0_block3 -> NoDestination_cfg0_block3 [label="Rethrow"] | ||
cfg0_block2 -> cfg0_block4 | ||
cfg0_block4 -> NoDestination_cfg0_block4 [label="StructuredExceptionHandling"] | ||
cfg0_block0 -> cfg0_block1 | ||
cfg0_block4 -> cfg0_block5 | ||
} | ||
"""); | ||
} | ||
|
||
private static RoslynLiveVariableAnalysis CreateLva(string code) | ||
{ | ||
var cfg = TestHelper.CompileCfgCS(code); | ||
return new RoslynLiveVariableAnalysis(cfg, CancellationToken.None); | ||
} | ||
} |
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