Skip to content

Commit 290c345

Browse files
authored
Merge pull request github#15535 from tamasvajk/buildless/winforms-usings
C# Add missing Windows Forms implicit usings
2 parents 23921af + 4eeca02 commit 290c345

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,16 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
116116
bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
117117
shouldExtractWebViews)
118118
{
119+
CompilationInfos.Add(("WebView extraction enabled", "1"));
119120
GenerateSourceFilesFromWebViews(allNonBinaryFiles);
120121
}
122+
else
123+
{
124+
CompilationInfos.Add(("WebView extraction enabled", "0"));
125+
}
126+
127+
CompilationInfos.Add(("UseWPF set", fileContent.UseWpf ? "1" : "0"));
128+
CompilationInfos.Add(("UseWindowsForms set", fileContent.UseWindowsForms ? "1" : "0"));
121129

122130
GenerateSourceFileFromImplicitUsings();
123131

@@ -434,6 +442,11 @@ private void GenerateSourceFileFromImplicitUsings()
434442
"Microsoft.Extensions.DependencyInjection", "Microsoft.Extensions.Hosting", "Microsoft.Extensions.Logging" });
435443
}
436444

445+
if (fileContent.UseWindowsForms)
446+
{
447+
usings.UnionWith(new[] { "System.Drawing", "System.Windows.Forms" });
448+
}
449+
437450
usings.UnionWith(fileContent.CustomImplicitUsings);
438451

439452
logger.LogInfo($"Generating source file for implicit usings. Namespaces: {string.Join(", ", usings.OrderBy(u => u))}");

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ public bool UseImplicitUsings
6161
}
6262
}
6363

64+
private bool useWpf = false;
65+
66+
public bool UseWpf
67+
{
68+
get
69+
{
70+
initialize.Run();
71+
return useWpf;
72+
}
73+
}
74+
75+
private bool useWindowsForms = false;
76+
77+
public bool UseWindowsForms
78+
{
79+
get
80+
{
81+
initialize.Run();
82+
return useWindowsForms;
83+
}
84+
}
85+
6486
private bool isLegacyProjectStructureUsed = false;
6587

6688
public bool IsLegacyProjectStructureUsed
@@ -105,10 +127,10 @@ internal FileContent(ILogger logger,
105127
public FileContent(ILogger logger, IEnumerable<string> files) : this(logger, files, new UnsafeFileReader())
106128
{ }
107129

108-
private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch, string groupPrefix, bool toLower)
130+
private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch, string groupPrefix)
109131
{
110132
var match = input.Slice(valueMatch.Index, valueMatch.Length);
111-
var includeIndex = match.IndexOf(groupPrefix, StringComparison.InvariantCultureIgnoreCase);
133+
var includeIndex = match.IndexOf(groupPrefix, StringComparison.OrdinalIgnoreCase);
112134
if (includeIndex == -1)
113135
{
114136
return string.Empty;
@@ -119,22 +141,15 @@ private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch,
119141
var quoteIndex1 = match.IndexOf("\"");
120142
var quoteIndex2 = match.Slice(quoteIndex1 + 1).IndexOf("\"");
121143

122-
var result = match.Slice(quoteIndex1 + 1, quoteIndex2).ToString();
123-
124-
if (toLower)
125-
{
126-
result = result.ToLowerInvariant();
127-
}
128-
129-
return result;
144+
return match.Slice(quoteIndex1 + 1, quoteIndex2).ToString();
130145
}
131146

132147
private static bool IsGroupMatch(ReadOnlySpan<char> line, Regex regex, string groupPrefix, string value)
133148
{
134149
foreach (var valueMatch in regex.EnumerateMatches(line))
135150
{
136151
// We can't get the group from the ValueMatch, so doing it manually:
137-
if (GetGroup(line, valueMatch, groupPrefix, toLower: true) == value.ToLowerInvariant())
152+
if (string.Equals(GetGroup(line, valueMatch, groupPrefix), value, StringComparison.OrdinalIgnoreCase))
138153
{
139154
return true;
140155
}
@@ -150,12 +165,11 @@ private void DoInitialize()
150165
{
151166
foreach (ReadOnlySpan<char> line in unsafeFileReader.ReadLines(file))
152167
{
153-
154168
// Find all the packages.
155169
foreach (var valueMatch in PackageReference().EnumerateMatches(line))
156170
{
157171
// We can't get the group from the ValueMatch, so doing it manually:
158-
var packageName = GetGroup(line, valueMatch, "Include", toLower: true);
172+
var packageName = GetGroup(line, valueMatch, "Include").ToLowerInvariant();
159173
if (!string.IsNullOrEmpty(packageName))
160174
{
161175
allPackages.Add(packageName);
@@ -167,16 +181,23 @@ private void DoInitialize()
167181
|| IsGroupMatch(line, ProjectSdk(), "Sdk", "Microsoft.NET.Sdk.Web")
168182
|| IsGroupMatch(line, FrameworkReference(), "Include", "Microsoft.AspNetCore.App");
169183

170-
171184
// Determine if implicit usings are used.
172185
useImplicitUsings = useImplicitUsings
173-
|| line.Contains("<ImplicitUsings>enable</ImplicitUsings>".AsSpan(), StringComparison.Ordinal)
174-
|| line.Contains("<ImplicitUsings>true</ImplicitUsings>".AsSpan(), StringComparison.Ordinal);
186+
|| line.Contains("<ImplicitUsings>enable</ImplicitUsings>".AsSpan(), StringComparison.OrdinalIgnoreCase)
187+
|| line.Contains("<ImplicitUsings>true</ImplicitUsings>".AsSpan(), StringComparison.OrdinalIgnoreCase);
188+
189+
// Determine if WPF is used.
190+
useWpf = useWpf
191+
|| line.Contains("<UseWPF>true</UseWPF>".AsSpan(), StringComparison.OrdinalIgnoreCase);
192+
193+
// Determine if Windows Forms is used.
194+
useWindowsForms = useWindowsForms
195+
|| line.Contains("<UseWindowsForms>true</UseWindowsForms>".AsSpan(), StringComparison.OrdinalIgnoreCase);
175196

176197
// Find all custom implicit usings.
177198
foreach (var valueMatch in CustomImplicitUsingDeclarations().EnumerateMatches(line))
178199
{
179-
var ns = GetGroup(line, valueMatch, "Include", toLower: false);
200+
var ns = GetGroup(line, valueMatch, "Include");
180201
if (!string.IsNullOrEmpty(ns))
181202
{
182203
implicitUsingNamespaces.Add(ns);

csharp/extractor/Semmle.Extraction.Tests/FileContent.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void TestFileContent2()
8484
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), allPackages);
8585
}
8686

87-
private static void ImplicitUsingsTest(string line, bool expected)
87+
private static void CsProjSettingsTest(string line, bool expected, Func<FileContent, bool> func)
8888
{
8989
// Setup
9090
var lines = new List<string>()
@@ -94,28 +94,52 @@ private static void ImplicitUsingsTest(string line, bool expected)
9494
var fileContent = new TestFileContent(lines);
9595

9696
// Execute
97-
var useImplicitUsings = fileContent.UseImplicitUsings;
97+
var actual = func(fileContent);
9898

9999
// Verify
100-
Assert.Equal(expected, useImplicitUsings);
100+
Assert.Equal(expected, actual);
101101
}
102102

103103
[Fact]
104104
public void TestFileContent_ImplicitUsings0()
105105
{
106-
ImplicitUsingsTest("<ImplicitUsings>false</ImplicitUsings>", false);
106+
CsProjSettingsTest("<ImplicitUsings>false</ImplicitUsings>", false, fc => fc.UseImplicitUsings);
107107
}
108108

109109
[Fact]
110110
public void TestFileContent_ImplicitUsings1()
111111
{
112-
ImplicitUsingsTest("<ImplicitUsings>true</ImplicitUsings>", true);
112+
CsProjSettingsTest("<ImplicitUsings>true</ImplicitUsings>", true, fc => fc.UseImplicitUsings);
113113
}
114114

115115
[Fact]
116116
public void TestFileContent_ImplicitUsings2()
117117
{
118-
ImplicitUsingsTest("<ImplicitUsings>enable</ImplicitUsings>", true);
118+
CsProjSettingsTest("<ImplicitUsings>enable</ImplicitUsings>", true, fc => fc.UseImplicitUsings);
119+
}
120+
121+
[Fact]
122+
public void TestFileContent_UseWpf0()
123+
{
124+
CsProjSettingsTest("<UseWPF>false</UseWPF>", false, fc => fc.UseWpf);
125+
}
126+
127+
[Fact]
128+
public void TestFileContent_UseWpf1()
129+
{
130+
CsProjSettingsTest("<UseWPF>true</UseWPF>", true, fc => fc.UseWpf);
131+
}
132+
133+
[Fact]
134+
public void TestFileContent_UseWindowsForms0()
135+
{
136+
CsProjSettingsTest("<UseWindowsForms>false</UseWindowsForms>", false, fc => fc.UseWindowsForms);
137+
}
138+
139+
[Fact]
140+
public void TestFileContent_UseWindowsForms1()
141+
{
142+
CsProjSettingsTest("<UseWindowsForms>true</UseWindowsForms>", true, fc => fc.UseWindowsForms);
119143
}
120144

121145
[Fact]

0 commit comments

Comments
 (0)