Skip to content

Commit

Permalink
Add WarningsAsErrors and WarningsNotAsErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneDelcroix committed Dec 12, 2023
1 parent 6fb9f38 commit dfd0004
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ static FieldReference GetBindablePropertyReference(VariableDefinition parent, st

var isObsolete = bpDef.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.ObsoleteAttribute");
if (isObsolete)
context.LoggingHelper.LogWarningOrError("0618", context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"BindableProperty {localName} is deprecated.", null);
context.LoggingHelper.LogWarningOrError(618, context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"BindableProperty {localName} is deprecated.", null);

return bpRef;
}
Expand Down Expand Up @@ -1351,12 +1351,12 @@ static IEnumerable<Instruction> Set(VariableDefinition parent, string localName,
var property = parent.VariableType.GetProperty(context.Cache, pd => pd.Name == localName, out declaringTypeReference);
var propertyIsObsolete = property.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.ObsoleteAttribute");
if (propertyIsObsolete)
context.LoggingHelper.LogWarningOrError("0618", context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"Property {localName} is deprecated.", null);
context.LoggingHelper.LogWarningOrError(618, context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"Property {localName} is deprecated.", null);

var propertySetter = property.SetMethod;
var propertySetterIsObsolete = propertySetter.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.ObsoleteAttribute");
if (propertySetterIsObsolete)
context.LoggingHelper.LogWarningOrError("0618", context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"Property setter for {localName} is deprecated.", null);
context.LoggingHelper.LogWarningOrError(618, context.XamlFilePath, iXmlLineInfo.LineNumber, iXmlLineInfo.LinePosition, 0, 0, $"Property setter for {localName} is deprecated.", null);

// IL_0007: ldloc.0
// IL_0008: ldstr "foo"
Expand Down
64 changes: 53 additions & 11 deletions src/Controls/src/Build.Tasks/XamlCTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,71 @@ public static class LoggingHelperExtensions
{
class LoggingHelperContext
{
public int WarningLevel { get; set; } =4;
public bool TreatWarningsAsErrors { get; set; }=false;
public string NoWarn { get; set; }
public int WarningLevel { get; set; } = 4; //unused so far
public bool TreatWarningsAsErrors { get; set; } =false;
public IList<int> WarningsAsErrors { get; set; }
public IList<int> WarningsNotAsErrors { get; set; }
public IList<int> NoWarn { get; set; }
}

static LoggingHelperContext Context { get; set; }

public static void SetContext(this TaskLoggingHelper loggingHelper, int warningLevel, bool treatWarningsAsErrors, string noWarn)
public static void SetContext(this TaskLoggingHelper loggingHelper, int warningLevel, bool treatWarningsAsErrors, string noWarn, string warningsAsErrors, string warningsNotAsErrors)
{
if (Context == null)
Context = new LoggingHelperContext();
Context.WarningLevel = warningLevel;
Context.TreatWarningsAsErrors = treatWarningsAsErrors;
Context.NoWarn = noWarn;

Context.NoWarn = noWarn?.Split([';'], StringSplitOptions.RemoveEmptyEntries).Select(s => {
if (int.TryParse(s, out var i))
return i;
if (s.StartsWith("XC"))
{
var code = s.Substring(2);
if (int.TryParse(code, out i))
return i;
}
return -1;
}).Where(i => i != -1).ToList();

Context.WarningsAsErrors = warningsAsErrors?.Split([';'], StringSplitOptions.RemoveEmptyEntries).Select(s => {
if (int.TryParse(s, out var i))
return i;
if (s.StartsWith("XC"))
{
var code = s.Substring(2);
if (int.TryParse(code, out i))
return i;
}
return -1;
}).Where(i => i != -1).ToList();

Context.WarningsNotAsErrors = warningsNotAsErrors?.Split([';'], StringSplitOptions.RemoveEmptyEntries).Select(s => {
if (int.TryParse(s, out var i))
return i;
if (s.StartsWith("XC"))
{
var code = s.Substring(2);
if (int.TryParse(code, out i))
return i;
}
return -1;
}).Where(i => i != -1).ToList();
}

public static void LogWarningOrError(this TaskLoggingHelper loggingHelper, string code, string xamlFilePath, int lineNumber, int linePosition, int endLineNumber, int endLinePosition, string message, params object[] messageArgs)
public static void LogWarningOrError(this TaskLoggingHelper loggingHelper, int code, string xamlFilePath, int lineNumber, int linePosition, int endLineNumber, int endLinePosition, string message, params object[] messageArgs)
{
if (Context == null)
Context = new LoggingHelperContext();
if (Context.NoWarn != null && Context.NoWarn.Contains(code))
return;
if (Context.TreatWarningsAsErrors)
loggingHelper.LogError($"XamlC {code}" , null, null, xamlFilePath, lineNumber, linePosition, endLineNumber, endLinePosition, message, messageArgs);
if ((Context.TreatWarningsAsErrors && !Context.WarningsNotAsErrors.Contains(code)) || Context.WarningsAsErrors.Contains(code))
loggingHelper.LogError($"XamlC {code:0000}" , null, null, xamlFilePath, lineNumber, linePosition, endLineNumber, endLinePosition, message, messageArgs);
else
loggingHelper.LogWarning($"XamlC {code}", xamlFilePath, lineNumber, linePosition, endLineNumber, endLinePosition, message, messageArgs);
loggingHelper.LogWarning($"XamlC {code:0000}", xamlFilePath, lineNumber, linePosition, endLineNumber, endLinePosition, message, messageArgs);
}

}

public class XamlCTask : XamlTask
Expand All @@ -56,9 +94,13 @@ public class XamlCTask : XamlTask
public bool DefaultCompile { get; set; }
public bool ForceCompile { get; set; }
public string TargetFramework { get; set; }

public int WarningLevel { get; set; } = 4; //unused so far
public bool TreatWarningsAsErrors {get;set;} =false;
public string WarningsAsErrors { get;set;}
public string WarningsNotAsErrors {get;set;}
public string NoWarn {get;set;}
public int WarningLevel { get; set; } = 4;


public IAssemblyResolver DefaultAssemblyResolver { get; set; }

Expand All @@ -74,7 +116,7 @@ public class XamlCTask : XamlTask
public override bool Execute(out IList<Exception> thrownExceptions)
{
thrownExceptions = null;
LoggingHelper.SetContext (WarningLevel, TreatWarningsAsErrors, NoWarn);
LoggingHelper.SetContext(WarningLevel, TreatWarningsAsErrors, NoWarn, WarningsAsErrors, WarningsNotAsErrors);
LoggingHelper.LogMessage(Normal, $"{new string(' ', 0)}Compiling Xaml, assembly: {Assembly}");
var skipassembly = !DefaultCompile;
bool success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,14 @@
DefaultCompile = "true"
ValidateOnly = "$(_MauiXamlCValidateOnly)"
TargetFramework = "$(TargetFramework)"
KeepXamlResources = "$(MauiKeepXamlResources)"
KeepXamlResources = "$(MauiKeepXamlResources)"

WarningLevel = "$(WarningLevel)"
TreatWarningsAsErrors = "$(TreatWarningsAsErrors)"
NoWarn = "$(NoWarn)"
WarningLevel = "$(WarningLevel)" />
WarningsAsErrors = "$(WarningsAsErrors)"
WarningsNotAsErrors = "$(WarningsNotAsErrors)" />

<Touch Files="$(IntermediateOutputPath)XamlC.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(IntermediateOutputPath)XamlC.stamp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<RootNamespace>Microsoft.Maui.Controls.Xaml.UnitTests</RootNamespace>
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests</AssemblyName>
<WarningLevel>4</WarningLevel>
<NoWarn>0672;0219;0414;CS0436;0618</NoWarn>
<NoWarn>0672;0219;0414;CS0436;CS0618</NoWarn>
<WarningsNotAsErrors>XC0618</WarningsNotAsErrors>
<IsPackable>false</IsPackable>
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
</PropertyGroup>
Expand Down

0 comments on commit dfd0004

Please sign in to comment.