Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable Obsolete diagnostics #42518

Merged
merged 21 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/CustomObsoleteDiagnosticInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.CSharp.Errors
{
internal sealed class CustomObsoleteDiagnosticInfo : DiagnosticInfo
{
internal ObsoleteAttributeData Data { get; }

internal CustomObsoleteDiagnosticInfo(Symbol obsoletedSymbol, ObsoleteAttributeData data, bool inCollectionInitializer)
: base(CSharp.MessageProvider.Instance, (int)GetErrorCode(data, inCollectionInitializer), arguments: GetArguments(obsoletedSymbol, data))
{
Data = data;
}

private static ErrorCode GetErrorCode(ObsoleteAttributeData data, bool inCollectionInitializer)
{
// dev11 had a bug in this area (i.e. always produce a warning when there's no message) and we have to match it.
if (data.Message is null)
{
return inCollectionInitializer ? ErrorCode.WRN_DeprecatedCollectionInitAdd : ErrorCode.WRN_DeprecatedSymbol;
}

return (data.IsError, inCollectionInitializer) switch
{
(true, true) => ErrorCode.ERR_DeprecatedCollectionInitAddStr,
(true, false) => ErrorCode.ERR_DeprecatedSymbolStr,
(false, true) => ErrorCode.WRN_DeprecatedCollectionInitAddStr,
(false, false) => ErrorCode.WRN_DeprecatedSymbolStr
};
}

private static object[] GetArguments(Symbol obsoletedSymbol, ObsoleteAttributeData obsoleteAttributeData)
{
var message = obsoleteAttributeData.Message;
if (message is object)
{
return new object[] { obsoletedSymbol, message };
}
else
{
return new object[] { obsoletedSymbol };
}
}

public override string MessageIdentifier
{
get
{
var id = Data.DiagnosticId;
if (!string.IsNullOrEmpty(id))
{
return id;
}

return base.MessageIdentifier;
}
}

public override DiagnosticDescriptor Descriptor
{
get
{
var baseDescriptor = base.Descriptor;
var diagnosticId = Data.DiagnosticId;
var urlFormat = Data.UrlFormat;
if (diagnosticId is null && urlFormat is null)
{
return baseDescriptor;
}

var id = MessageIdentifier;
var helpLinkUri = baseDescriptor.HelpLinkUri;

if (urlFormat is object)
{
try
{
helpLinkUri = string.Format(urlFormat, id);
}
catch
{
// TODO: should we report a meta-diagnostic of some kind when the string.Format fails?
// also, should we do some validation of the 'UrlFormat' values provided in source to prevent people from shipping
// obsoleted symbols with malformed 'UrlFormat' values?
}
}

var customTags = ArrayBuilder<string>.GetInstance();
foreach (var tag in baseDescriptor.CustomTags)
{
customTags.Add(tag);
}
customTags.Add(WellKnownDiagnosticTags.CustomObsolete);

// TODO: we expect some users to repeatedly use
// the same diagnostic IDs and url format values for many symbols.
// do we want to cache similar diagnostic descriptors?
return new DiagnosticDescriptor(
id: id,
title: baseDescriptor.Title,
messageFormat: baseDescriptor.MessageFormat,
category: baseDescriptor.Category,
defaultSeverity: baseDescriptor.DefaultSeverity,
isEnabledByDefault: baseDescriptor.IsEnabledByDefault,
description: baseDescriptor.Description,
helpLinkUri: helpLinkUri,
customTags: customTags.ToImmutableAndFree());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Errors;
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
Expand Down Expand Up @@ -160,17 +161,11 @@ internal static DiagnosticInfo CreateObsoleteDiagnostic(Symbol symbol, BinderFla

if (data.Message == null)
{
// It seems like we should be able to assert that data.IsError is false, but we can't because dev11 had
// a bug in this area (i.e. always produce a warning when there's no message) and we have to match it.
// Debug.Assert(!data.IsError);
return new CSDiagnosticInfo(isColInit ? ErrorCode.WRN_DeprecatedCollectionInitAdd : ErrorCode.WRN_DeprecatedSymbol, symbol);
return new CustomObsoleteDiagnosticInfo(symbol, data, isColInit);
}
else
{
ErrorCode errorCode = data.IsError
? (isColInit ? ErrorCode.ERR_DeprecatedCollectionInitAddStr : ErrorCode.ERR_DeprecatedSymbolStr)
: (isColInit ? ErrorCode.WRN_DeprecatedCollectionInitAddStr : ErrorCode.WRN_DeprecatedSymbolStr);
return new CSDiagnosticInfo(errorCode, symbol, data.Message);
return new CustomObsoleteDiagnosticInfo(symbol, data, isColInit);
}
}
}
Expand Down
Loading