File tree Expand file tree Collapse file tree 5 files changed +36
-3
lines changed Expand file tree Collapse file tree 5 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 9
9
// </summary>
10
10
// -----------------------------------------------------------------------
11
11
12
- using System . Collections . Generic ;
13
12
using System . Linq ;
14
13
using Shrinker . Lexer ;
15
14
using Shrinker . Parser . SyntaxNodes ;
Original file line number Diff line number Diff line change @@ -46,9 +46,9 @@ public static void RemoveUnusedVariables(this SyntaxNode rootNode)
46
46
. Where ( o => o . Name == varName ) . ToList ( ) ;
47
47
foreach ( var assignment in assignments )
48
48
{
49
- // If RHS of the assignment calls a function with an 'out/inout' param, it is not save to remove.
49
+ // If RHS of the assignment calls a function with an 'out/inout' param, it is not safe to remove.
50
50
// Instead, replace the assignment with the RHS component.
51
- if ( assignment . TheTree . OfType < FunctionCallSyntaxNode > ( ) . Any ( o => o . HasOutParam ) )
51
+ if ( assignment . TheTree . OfType < FunctionCallSyntaxNode > ( ) . Any ( o => o . HasOutParam || o . ModifiesGlobalVariables ( ) ) )
52
52
{
53
53
var rhs = assignment . ValueNodes . ToList ( ) ;
54
54
rhs . Add ( new GenericSyntaxNode ( new SemicolonToken ( ) ) ) ;
Original file line number Diff line number Diff line change @@ -37,5 +37,8 @@ private FunctionCallSyntaxNode(string name)
37
37
protected override SyntaxNode CreateSelf ( ) => new FunctionCallSyntaxNode ( Name ) ;
38
38
39
39
public virtual bool HasOutParam => this . Root ( ) . Children . OfType < FunctionSyntaxNodeBase > ( ) . Any ( o => o . Name == Name && o . HasOutParam ) ;
40
+
41
+ public bool ModifiesGlobalVariables ( ) =>
42
+ this . Root ( ) . FunctionDefinitions ( ) . FirstOrDefault ( o => o . Name == Name ) ? . ModifiesGlobalVariables ( ) == true ;
40
43
}
41
44
}
Original file line number Diff line number Diff line change 11
11
12
12
using System ;
13
13
using System . Linq ;
14
+ using Shrinker . Lexer ;
14
15
15
16
namespace Shrinker . Parser . SyntaxNodes
16
17
{
@@ -41,5 +42,18 @@ private FunctionDefinitionSyntaxNode()
41
42
public override string UiName => $ "{ ReturnType } { Name } { ( Params . Children . Any ( ) ? "(...)" : "()" ) } {{...}}";
42
43
43
44
protected override SyntaxNode CreateSelf ( ) => new FunctionDefinitionSyntaxNode { ReturnType = ReturnType } ;
45
+
46
+ public bool ModifiesGlobalVariables ( )
47
+ {
48
+ var globals = this . GlobalVariables ( ) . Select ( o => o . Name ) . ToList ( ) ;
49
+ var theTree = Braces . TheTree . ToList ( ) ;
50
+
51
+ // Check self.
52
+ if ( theTree . Any ( o => globals . Any ( g => o . Token ? . Content ? . StartsWithVarName ( g ) == true ) ) )
53
+ return true ;
54
+
55
+ // Check any calls made by self...
56
+ return theTree . OfType < FunctionCallSyntaxNode > ( ) . Any ( o => o . ModifiesGlobalVariables ( ) ) ;
57
+ }
44
58
}
45
59
}
Original file line number Diff line number Diff line change @@ -1597,6 +1597,23 @@ public void CheckVariableAssignedByFunctionCallOutParamCalledByReturnStatementIs
1597
1597
Assert . That ( rootNode . ToCode ( ) . ToSimple ( ) , Is . EqualTo ( Code ) ) ;
1598
1598
}
1599
1599
1600
+ [ Test ]
1601
+ public void CheckUnusedVariableAssignmentWhichCallsFunctionWhichModifiedGlobalVariableDoesNotRemoveFunctionCall ( )
1602
+ {
1603
+ const string Code = "int g; int f() { g = 1; return 1; } int ff() { return f(); } void main() { int n = ff(n); }" ;
1604
+
1605
+ var lexer = new Lexer ( ) ;
1606
+ lexer . Load ( Code ) ;
1607
+
1608
+ var options = CustomOptions . None ( ) ;
1609
+ options . CombineAssignmentWithSingleUse = true ;
1610
+ var rootNode = new Parser ( lexer )
1611
+ . Parse ( )
1612
+ . Simplify ( options ) ;
1613
+
1614
+ Assert . That ( rootNode . ToCode ( ) . ToSimple ( ) , Is . EqualTo ( Code ) ) ;
1615
+ }
1616
+
1600
1617
[ Test , Sequential ]
1601
1618
public void CheckFindingVariablesToMakeConst (
1602
1619
[ Values ( "int g = 2; int main() { return g; }" ,
You can’t perform that action at this time.
0 commit comments