Skip to content

Commit 38fda71

Browse files
nowarn
1 parent 7e33be3 commit 38fda71

File tree

14 files changed

+41
-29
lines changed

14 files changed

+41
-29
lines changed

src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
<_MauiXamlInflator Condition="' $(MauiXamlInflator)' != '' ">$(MauiXamlInflator)</_MauiXamlInflator>
1111
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' == 'Debug' ">Runtime</_MauiXamlInflator>
1212
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' != 'Debug' ">XamlC</_MauiXamlInflator>
13-
13+
1414
<MauiXamlEnableDiagnostics Condition=" '$(MauiXamlEnableDiagnostics)' == '' ">$(EnableDiagnostics)</MauiXamlEnableDiagnostics>
1515
<MauiXamlEnableDiagnostics Condition=" '$(MauiXamlEnableDiagnostics)' == '' And '$(Configuration)' == 'Debug' ">true</MauiXamlEnableDiagnostics>
16+
17+
<MauiXamlNoWarn Condition=" '$(MauiXamlNoWarn)' != '' ">$(MauiXamlNoWarn.Replace(';', ','))</MauiXamlNoWarn>
1618

1719
<!-- The WINUI check for this only runs when there is an empty string so I just convert false to an empty string to fall in line with our other properties -->
1820
<SkipMicrosoftUIXamlCheckTargetPlatformVersion Condition="'$(SkipMicrosoftUIXamlCheckTargetPlatformVersion)'==''">true</SkipMicrosoftUIXamlCheckTargetPlatformVersion>
@@ -86,6 +88,7 @@
8688
<!-- Assign the default inflator to MauiXaml that don't have any -->
8789
<!-- there's a roslyn bug that stops parsing value at the first semicolon. replace them all https://github.com/dotnet/roslyn/issues/43970 -->
8890
<MauiXaml Inflator="$([MSBuild]::ValueOrDefault('%(MauiXaml.Inflator)','$(_MauiXamlInflator)').Replace(';', ','))"/>
91+
<MauiXaml NoWarn="$([MSBuild]::ValueOrDefault('%(MauiXaml.NoWarn)','').Replace(';', ','))"/>
8992

9093
<_MauiXaml_SG Remove="@(_MauiXaml_SG)" />
9194
<_MauiXaml_RT Remove="@(_MauiXaml_RT)" />

src/Controls/src/SourceGen/GeneratorHelpers.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public static string EscapeIdentifier(string identifier)
8080
if (fileOptions.TryGetValue("build_metadata.additionalfiles.LineInfo", out lineInfo) && string.Compare(lineInfo, "disable", StringComparison.OrdinalIgnoreCase) == 0)
8181
enableLineInfo = false;
8282

83+
string noWarn = "";
84+
if (fileOptions.TryGetValue("build_property.MauiXamlNoWarn", out var noWarnValue))
85+
noWarn = noWarnValue;
86+
if (fileOptions.TryGetValue("build_metadata.additionalfiles.NoWarn", out noWarnValue))
87+
noWarn = noWarnValue;
88+
8389
return new ProjectItem
8490
{
8591
AdditionalText = additionalText,
@@ -90,6 +96,7 @@ public static string EscapeIdentifier(string identifier)
9096
Inflator = (XamlInflator)xamlinflator,
9197
EnableLineInfo = enableLineInfo,
9298
EnableDiagnostics = enableDiagnostics,
99+
NoWarn = noWarn,
93100
TargetFramework = targetFramework,
94101
Configuration = configuration!,
95102
};
@@ -153,10 +160,7 @@ public static string EscapeIdentifier(string identifier)
153160
continue;
154161
}
155162

156-
var rootnode = new SGRootNode(new XmlType(reader.NamespaceURI, reader.Name, XamlParser.GetTypeArguments(reader)), /*typeReference, */(IXmlNamespaceResolver)reader, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition)
157-
{
158-
DisableWarnings = warningDisableList,
159-
};
163+
var rootnode = new SGRootNode(new XmlType(reader.NamespaceURI, reader.Name, XamlParser.GetTypeArguments(reader)), /*typeReference, */(IXmlNamespaceResolver)reader, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
160164
XamlParser.ParseXaml(rootnode, reader);
161165

162166
return rootnode;

src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ PrePost newblock() =>
2525
PrePost.NewBlock(codeWriter);
2626

2727
codeWriter.WriteLine(GeneratorHelpers.AutoGeneratedHeaderText);
28-
var warningDisable = xamlItem.Root!.DisableWarnings != null ? string.Join(", ", xamlItem.Root!.DisableWarnings) : null;
29-
if (warningDisable != null && warningDisable.Length > 0)
28+
if (xamlItem.ProjectItem.NoWarn != null && xamlItem.ProjectItem.NoWarn.Length > 0)
3029
{
31-
codeWriter.WriteLine($"#pragma warning disable {warningDisable}");
30+
codeWriter.WriteLine($"#pragma warning disable {xamlItem.ProjectItem.NoWarn}");
3231
codeWriter.WriteLine();
3332
}
3433
var root = xamlItem.Root!;

src/Controls/src/SourceGen/ProjectItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ record ProjectItem
1818
public string Configuration { get; internal set; } = "Debug";
1919
public bool EnableLineInfo { get; internal set; } = true;
2020
public bool EnableDiagnostics { get; internal set; } = false;
21+
public string NoWarn { get; internal set; } = "";
2122
}

src/Controls/src/SourceGen/SGRootNode.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ public SGRootNode(XmlType xmlType, /*TypeReference typeReference,*/ IXmlNamespac
1212
// TypeReference = typeReference;
1313
}
1414

15-
public IList<string>? DisableWarnings { get; internal set; }
16-
1715
// public TypeReference TypeReference { get; private set; }
1816
}

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SourceGenXamlInitializeComponentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public class SourceGenXamlInitializeComponentTestBase : SourceGenTestsBase
1212
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
1313
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework);
1414

15-
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code)
15+
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code, string noWarn = "")
1616
{
1717
var compilation = CreateMauiCompilation();
1818
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
19-
var result = RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml));
19+
var result = RunGenerator<CodeBehindGenerator>(compilation, noWarn, new AdditionalXamlFile("Test.xaml", xaml));
2020
var generated = result.Results.SingleOrDefault().GeneratedSources.SingleOrDefault(gs => gs.HintName.EndsWith(".xsg.cs")).SourceText?.ToString();
2121

2222
return (result, generated);

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/WarningIgnore.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public void WarningIgnore()
1111
var xaml =
1212
"""
1313
<?xml version="1.0" encoding="UTF-8"?>
14-
<?xaml-comp warning-disable="0168,CS0612"?>
15-
<?xaml-comp warning-disable="FOOBAR" compile=true?>
14+
<?xaml-comp compile=true?>
1615
<ContentPage
1716
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
1817
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
@@ -52,7 +51,7 @@ public TestPage()
5251
//------------------------------------------------------------------------------
5352
#nullable enable
5453
55-
#pragma warning disable 0168, CS0612, FOOBAR
54+
#pragma warning disable 0168, CS0612
5655
5756
namespace Test;
5857
@@ -62,9 +61,9 @@ public partial class TestPage
6261
private partial void InitializeComponent()
6362
{
6463
var button = new global::Microsoft.Maui.Controls.Button();
65-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(button!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 4);
64+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(button!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 7, 4);
6665
var __root = this;
67-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 4, 2);
66+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 3, 2);
6867
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
6968
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope = global::Microsoft.Maui.Controls.Internals.NameScope.GetNameScope(__root) ?? new global::Microsoft.Maui.Controls.Internals.NameScope();
7069
#endif
@@ -86,7 +85,7 @@ private partial void InitializeComponent()
8685
8786
""";
8887

89-
var (result, generated) = RunGenerator(xaml, code);
88+
var (result, generated) = RunGenerator(xaml, code, "0168, CS0612");
9089
Assert.IsFalse(result.Diagnostics.Any());
9190

9291
Assert.AreEqual(expected, generated);

src/Controls/tests/SourceGen.UnitTests/SourceGeneratorDriver.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static class SourceGeneratorDriver
2121
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework);
2222

2323
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, params AdditionalFile[] additionalFiles)
24+
where T : IIncrementalGenerator, new() => RunGenerator<T>(compilation, "", additionalFiles);
25+
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, string noWarn, params AdditionalFile[] additionalFiles)
2426
where T : IIncrementalGenerator, new()
2527
{
2628
ISourceGenerator generator = new T().AsSourceGenerator();
@@ -32,7 +34,7 @@ public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation,
3234

3335
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator], driverOptions: options)
3436
.AddAdditionalTexts(additionalFiles.Select(f => f.Text).ToImmutableArray())
35-
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles));
37+
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles, noWarn));
3638

3739
driver = driver.RunGenerators(compilation);
3840

@@ -106,10 +108,12 @@ private static MetadataReference[] GetMauiReferences()
106108
private class CustomAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
107109
{
108110
private readonly IImmutableDictionary<string, AdditionalFile> _additionalFiles;
111+
private readonly string _noWarn;
109112

110-
public CustomAnalyzerConfigOptionsProvider(AdditionalFile[] additionalFiles)
113+
public CustomAnalyzerConfigOptionsProvider(AdditionalFile[] additionalFiles, string noWarn = "")
111114
{
112115
_additionalFiles = additionalFiles.ToImmutableDictionary(f => f.Text.Path, f => f);
116+
_noWarn = noWarn;
113117
}
114118

115119
public override AnalyzerConfigOptions GlobalOptions => throw new System.NotImplementedException();
@@ -122,20 +126,22 @@ public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
122126
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
123127
{
124128
return _additionalFiles.TryGetValue(textFile.Path, out var additionalFile)
125-
? (AnalyzerConfigOptions)new CustomAnalyzerConfigOptions(additionalFile)
129+
? (AnalyzerConfigOptions)new CustomAnalyzerConfigOptions(additionalFile, _noWarn)
126130
: CustomAnalyzerConfigOptions.Empty;
127131
}
128132

129133
private class CustomAnalyzerConfigOptions : AnalyzerConfigOptions
130134
{
131135
readonly AdditionalFile? _additionalFile;
136+
readonly string _noWarn;
132137

133-
public CustomAnalyzerConfigOptions(AdditionalFile? additionalFile)
138+
public CustomAnalyzerConfigOptions(AdditionalFile? additionalFile, string noWarn)
134139
{
135140
_additionalFile = additionalFile;
141+
_noWarn = noWarn;
136142
}
137143

138-
public static AnalyzerConfigOptions Empty { get; } = new CustomAnalyzerConfigOptions(null);
144+
public static AnalyzerConfigOptions Empty { get; } = new CustomAnalyzerConfigOptions(null, "");
139145

140146
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value)
141147
{
@@ -155,7 +161,8 @@ public override bool TryGetValue(string key, [NotNullWhen(true)] out string? val
155161
"build_property.targetframework" => _additionalFile.TargetFramework,
156162
"build_property.MauiXamlEnableDiagnostics" => "true",
157163
"build_property.MauiXamlLineInfo" => "enable",
158-
164+
"build_property.MauiXamlNoWarn" => _noWarn,
165+
159166
_ => null
160167
};
161168

src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
<MauiXaml Update="**\*.rtsg.xaml" Inflator="Runtime;SourceGen" />
8080
<MauiXaml Update="**\*.rtxc.xaml" Inflator="Runtime;XamlC" />
8181
<MauiXaml Update="**\*.sgxc.xaml" Inflator="SourceGen;XamlC" />
82+
83+
<MauiXaml Update="FontSize.xaml" NoWarn="0612;0618" />
84+
<MauiXaml Update="Issues\Bz55343.xaml" NoWarn="0612" />
85+
<MauiXaml Update="Issues\Issue1438.xaml" NoWarn="0612" />
86+
<MauiXaml Update="Issues\Issue2578.xaml" NoWarn="0612" />
87+
<MauiXaml Update="Issues\Issue2659.xaml" NoWarn="0612" />
8288
</ItemGroup>
8389

8490
</Project>

src/Controls/tests/Xaml.UnitTests/FontSize.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<?xaml-comp warning-disable="0612,0618" ?>
32
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
43
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
54
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.FontSize">

0 commit comments

Comments
 (0)