From 5b3012c0030aa9e7cdb142f0194523e282218835 Mon Sep 17 00:00:00 2001 From: shyamn Date: Thu, 22 May 2014 16:20:17 -0700 Subject: [PATCH] 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) --- .../CSharp/Source/Parser/DirectiveParser.cs | 6 +++- .../Test/Syntax/Diagnostics/DiagnosticTest.cs | 30 +++++++++++++++++++ .../Source/CodeAnalysisResources.Designer.cs | 9 ++++++ .../Core/Source/CodeAnalysisResources.resx | 3 ++ .../Source/Diagnostic/DiagnosticDescriptor.cs | 7 ++++- 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Src/Compilers/CSharp/Source/Parser/DirectiveParser.cs b/Src/Compilers/CSharp/Source/Parser/DirectiveParser.cs index 65175af3a98..da913817129 100644 --- a/Src/Compilers/CSharp/Source/Parser/DirectiveParser.cs +++ b/Src/Compilers/CSharp/Source/Parser/DirectiveParser.cs @@ -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; diff --git a/Src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs b/Src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs index f3799700fbe..895ba1d9250 100644 --- a/Src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs +++ b/Src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs @@ -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() diff --git a/Src/Compilers/Core/Source/CodeAnalysisResources.Designer.cs b/Src/Compilers/Core/Source/CodeAnalysisResources.Designer.cs index dba178b05bd..d5e52cf6adf 100644 --- a/Src/Compilers/Core/Source/CodeAnalysisResources.Designer.cs +++ b/Src/Compilers/Core/Source/CodeAnalysisResources.Designer.cs @@ -249,6 +249,15 @@ internal class CodeAnalysisResources { } } + /// + /// 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.. + /// + internal static string DiagnosticIdCantBeNullOrWhitespace { + get { + return ResourceManager.GetString("DiagnosticIdCantBeNullOrWhitespace", resourceCulture); + } + } + /// /// Looks up a localized string similar to Empty or invalid file name. /// diff --git a/Src/Compilers/Core/Source/CodeAnalysisResources.resx b/Src/Compilers/Core/Source/CodeAnalysisResources.resx index db5ada69387..60662ff610b 100644 --- a/Src/Compilers/Core/Source/CodeAnalysisResources.resx +++ b/Src/Compilers/Core/Source/CodeAnalysisResources.resx @@ -363,4 +363,7 @@ The changes must be ordered and not overlapping. + + A DiagnosticDescriptor must have an Id that is neiter null nor an empty string nor a string that only contains white space. + \ No newline at end of file diff --git a/Src/Compilers/Core/Source/Diagnostic/DiagnosticDescriptor.cs b/Src/Compilers/Core/Source/Diagnostic/DiagnosticDescriptor.cs index 541f22223b1..c4ac1cec984 100644 --- a/Src/Compilers/Core/Source/Diagnostic/DiagnosticDescriptor.cs +++ b/Src/Compilers/Core/Source/Diagnostic/DiagnosticDescriptor.cs @@ -1,7 +1,7 @@ // 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 /// Optional custom tags for the diagnostic. See for some well known tags. 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; -- GitLab