Skip to content

Commit 9c4c185

Browse files
committed
Bug: Prevent inlining a const variable if a child variable with the same name exists.
A #define with a comment referencing a function would be incorrectly flagged as using that function. E.g. #define FOO // Define to tweak behaviour of MyFunc() ...would mean MyFunc() would always be treated as referenced, and could not be automatically removed.
1 parent 4ae809e commit 9c4c185

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/RemoveUnusedFunctionsExtension.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// </summary>
1010
// -----------------------------------------------------------------------
1111

12+
using System;
1213
using System.Linq;
1314
using Shrinker.Parser.SyntaxNodes;
1415

@@ -35,7 +36,7 @@ public static void RemoveUnusedFunctions(this SyntaxNode rootNode)
3536
continue; // Function was used.
3637

3738
// Perhaps used by a #define?
38-
if (rootNode.TheTree.OfType<PragmaDefineSyntaxNode>().Any(o => o.ToCode().Contains(testFunction.Name)))
39+
if (rootNode.TheTree.OfType<PragmaDefineSyntaxNode>().Any(o => DoesPragmaDefineReferenceFunction(o, testFunction)))
3940
continue; // Yup - Used.
4041

4142
// Function not used - Remove it (and any matching declaration).
@@ -48,5 +49,21 @@ public static void RemoveUnusedFunctions(this SyntaxNode rootNode)
4849
return;
4950
}
5051
}
52+
53+
private static bool DoesPragmaDefineReferenceFunction(PragmaDefineSyntaxNode define, FunctionDefinitionSyntaxNode function)
54+
{
55+
var defineCode = define.ToCode();
56+
var functionIndex = defineCode.IndexOf(function.Name, StringComparison.Ordinal);
57+
if (functionIndex == -1)
58+
return false;
59+
60+
var cppCommentIndex = defineCode.IndexOf("//", StringComparison.Ordinal);
61+
var cCommentIndex = defineCode.IndexOf("/*", StringComparison.Ordinal);
62+
var comments = new[] { cppCommentIndex, cCommentIndex }.Where(o => o >= 0).ToList();
63+
if (comments.Any() && comments.Min() < functionIndex)
64+
return false; // Reference found, but in a comment.
65+
66+
return true;
67+
}
5168
}
5269
}

0 commit comments

Comments
 (0)