提交 5b3012c0 编写于 作者: S shyamn

Ensure that we report warning when the argument for #pragma warning disable /...

Ensure that we report warning when the argument for #pragma warning disable / restore is an empty string / string that only contains whitespace.

Also throw exception if someone tries to construct a DiagnosticDescriptor with null / empty string as Id. (changeset 1262338)
上级 0de4871e
......@@ -398,7 +398,11 @@ private DirectiveTriviaSyntax ParsePragmaDirective(SyntaxToken hash, SyntaxToken
{
string value = (string)id.Value;
var messageProvider = MessageProvider.Instance;
if (value.StartsWith(messageProvider.CodePrefix))
if (string.IsNullOrWhiteSpace(value))
{
id = this.AddError(id, ErrorCode.WRN_BadWarningNumber, value);
}
else if (value.StartsWith(messageProvider.CodePrefix))
{
// For diagnostic IDs of the form "CS[0-9]*", verify the error code is that of a warning
int compilerWarningNumber;
......
......@@ -1519,6 +1519,36 @@ public static void Main()
Diagnostic(ErrorCode.WRN_StringOrNumericLiteralExpected, ","));
}
[WorkItem(913567)]
[Fact]
public void PragmaWarningWithErrors_EmptyStrings()
{
var text = @"
public class C
{
public static void Main()
{
#pragma warning disable """"
#pragma warning restore """"
#pragma warning disable "" ""
#pragma warning restore """ + "\t" + @"""
}
}";
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (6,25): warning CS1691: '' is not a valid warning number
// #pragma warning disable ""
Diagnostic(ErrorCode.WRN_BadWarningNumber, "\"\"").WithArguments("").WithLocation(6, 25),
// (7,25): warning CS1691: '' is not a valid warning number
// #pragma warning restore ""
Diagnostic(ErrorCode.WRN_BadWarningNumber, "\"\"").WithArguments("").WithLocation(7, 25),
// (8,25): warning CS1691: ' ' is not a valid warning number
// #pragma warning disable " "
Diagnostic(ErrorCode.WRN_BadWarningNumber, "\" \"").WithArguments(" ").WithLocation(8, 25),
// (9,25): warning CS1691: '{tab}' is not a valid warning number
// #pragma warning restore "{tab}"
Diagnostic(ErrorCode.WRN_BadWarningNumber, "\"\t\"").WithArguments("\t").WithLocation(9, 25));
}
[WorkItem(546814, "DevDiv")]
[Fact]
public void PragmaWarningAlign_0()
......
......@@ -249,6 +249,15 @@ internal class CodeAnalysisResources {
}
}
/// <summary>
/// Looks up a localized string similar to A DiagnosticDescriptor must have an Id that is neiter null nor an empty string nor a string that only contains white space..
/// </summary>
internal static string DiagnosticIdCantBeNullOrWhitespace {
get {
return ResourceManager.GetString("DiagnosticIdCantBeNullOrWhitespace", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty or invalid file name.
/// </summary>
......
......@@ -363,4 +363,7 @@
<data name="ChangesMustBeOrderedAndNotOverlapping" xml:space="preserve">
<value>The changes must be ordered and not overlapping.</value>
</data>
<data name="DiagnosticIdCantBeNullOrWhitespace" xml:space="preserve">
<value>A DiagnosticDescriptor must have an Id that is neiter null nor an empty string nor a string that only contains white space.</value>
</data>
</root>
\ No newline at end of file
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.CodeAnalysis
{
......@@ -59,6 +59,11 @@ public class DiagnosticDescriptor
/// <param name="customTags">Optional custom tags for the diagnostic. See <see cref="WellKnownDiagnosticTags"/> for some well known tags.</param>
public DiagnosticDescriptor(string id, string description, string messageFormat, string category, DiagnosticSeverity defaultSeverity, bool isEnabledByDefault, params string[] customTags)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException(CodeAnalysisResources.DiagnosticIdCantBeNullOrWhitespace, "id");
}
this.Id = id;
this.Description = description;
this.Category = category;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册