Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ahmad-moussawi/c4920c86fe57ffa03fa58abcb663009a to your computer and use it in GitHub Desktop.

Select an option

Save ahmad-moussawi/c4920c86fe57ffa03fa58abcb663009a to your computer and use it in GitHub Desktop.

Revisions

  1. @thomaslevesque thomaslevesque revised this gist Jun 14, 2019. No changes.
  2. @thomaslevesque thomaslevesque revised this gist Jun 14, 2019. No changes.
  3. @thomaslevesque thomaslevesque revised this gist Jun 14, 2019. 1 changed file with 377 additions and 189 deletions.
    566 changes: 377 additions & 189 deletions CSharpErrorsAndWarnings.md
    377 additions, 189 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  4. @thomaslevesque thomaslevesque revised this gist Jun 14, 2019. No changes.
  5. @thomaslevesque thomaslevesque revised this gist May 10, 2017. 2 changed files with 0 additions and 151 deletions.
    1 change: 0 additions & 1 deletion CSharpErrorsAndWarnings.md
    Original file line number Diff line number Diff line change
    @@ -1237,4 +1237,3 @@
    |CS9001|Error|Use of default literal is not valid in this context|
    |CS9002|Warning|Did you mean to use the default switch label (`default:`) rather than `case default:`? If you really mean to use the default literal, consider `case (default):` or another literal (`case 0:` or `case null:`) as appropriate.|
    |CS9003|Error|An expression of type '{0}' cannot be handled by a pattern of type '{1}' in C# {2}. Please use language version {3} or greater.|

    150 changes: 0 additions & 150 deletions generator.cs
    Original file line number Diff line number Diff line change
    @@ -1,150 +0,0 @@
    static void Main()
    {
    var errorCodes = GetErrorCodes();
    WriteMarkdownTable(errorCodes, Console.Out);
    }

    const string ErrorCodesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs";
    const string ErrorResourcesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/CSharpResources.resx";

    private static IReadOnlyList<ErrorCode> GetErrorCodes()
    {
    using (var wc = new WebClient())
    {
    var enumMembers = GetErrorCodeEnumMembers(wc);
    var dictionary = GetResourceDictionary(wc);

    var errorCodes =
    enumMembers
    .Select(m =>
    ErrorCode.Parse(
    m,
    dictionary.TryGetValue(m.Identifier.ValueText, out var message) ? message : ""))
    .ToList();

    return errorCodes;
    }
    }

    private static IEnumerable<EnumMemberDeclarationSyntax> GetErrorCodeEnumMembers(WebClient wc)
    {
    string errorCodesFileContent = wc.DownloadString(ErrorCodesUrl);
    var syntaxTree = CSharpSyntaxTree.ParseText(errorCodesFileContent);
    var root = syntaxTree.GetRoot();
    var enumDeclaration =
    root.DescendantNodes()
    .OfType<EnumDeclarationSyntax>()
    .First(e => e.Identifier.ValueText == "ErrorCode");
    return enumDeclaration.Members;
    }

    private static IReadOnlyDictionary<string, string> GetResourceDictionary(WebClient wc)
    {
    string resourcesFileContent = wc.DownloadString(ErrorResourcesUrl);
    var doc = XDocument.Parse(resourcesFileContent);
    var dictionary =
    doc.Root.Elements("data")
    .ToDictionary(
    e => e.Attribute("name").Value,
    e => e.Element("value").Value);
    return dictionary;
    }

    private static void WriteRuleSet(IEnumerable<ErrorCode> errorCodes, TextWriter writer)
    {
    var doc =
    new XDocument(
    new XElement("RuleSet",
    new XAttribute("Name", "C# Warnings as Errors"),
    new XAttribute("ToolsVersion", "15.0"),
    new XElement("Rules",
    new XAttribute("AnalyzerId", "Microsoft.CodeAnalysis.CSharp"),
    new XAttribute("RuleNamespace", "Microsoft.CodeAnalysis.CSharp"),
    from e in errorCodes
    where e.Level == DiagnosticLevel.Warning
    select new XElement("Rule",
    new XAttribute("Id", e.Code),
    new XAttribute("Action", "Error")))));
    doc.Save(writer);
    }

    private static void WriteMarkdownTable(IEnumerable<ErrorCode> errorCodes, TextWriter writer)
    {
    writer.WriteLine("# All C# errors and warnings");

    writer.WriteLine();
    writer.WriteLine("*Parsed from the [Roslyn source code](https://github.com/dotnet/roslyn) using Roslyn.*");
    writer.WriteLine();

    writer.WriteLine("|Code|Level|Name|Message|");
    writer.WriteLine("|----|-----|----|-------|");
    foreach (var e in errorCodes)
    {
    if (e.Value == 0)
    continue;
    writer.WriteLine($"|{e.Code}|{e.Level}|{e.Name}|{e.Message}|");
    }
    }

    class ErrorCode
    {
    public static ErrorCode Parse(EnumMemberDeclarationSyntax member, string message)
    {
    string name = member.Identifier.ValueText;
    if (name == "Void" || name == "Unknown")
    {
    return new ErrorCode(name, 0, DiagnosticLevel.Unknown, message);
    }
    else
    {
    return new ErrorCode(
    name.Substring(4),
    int.Parse(member.EqualsValue?.Value?.GetText()?.ToString() ?? "0"),
    ParseLevel(name.Substring(0, 3)),
    message);
    }
    }

    private ErrorCode(string name, int value, DiagnosticLevel level, string message)
    {
    Name = name;
    Value = value;
    Level = level;
    Message = message;
    }

    public string Name { get; }
    public int Value { get; }
    public string Code => $"CS{Value:D4}";
    public DiagnosticLevel Level { get; }
    public string Message { get; }

    private static DiagnosticLevel ParseLevel(string level)
    {
    switch (level)
    {
    case "HDN":
    return DiagnosticLevel.Hidden;
    case "INF":
    return DiagnosticLevel.Info;
    case "WRN":
    return DiagnosticLevel.Warning;
    case "ERR":
    return DiagnosticLevel.Error;
    case "FTL":
    return DiagnosticLevel.Fatal;
    default:
    return DiagnosticLevel.Unknown;
    }
    }
    }

    enum DiagnosticLevel
    {
    Unknown,
    Hidden,
    Info,
    Warning,
    Error,
    Fatal
    }
  6. @thomaslevesque thomaslevesque revised this gist May 10, 2017. 2 changed files with 1236 additions and 1236 deletions.
    2,472 changes: 1,236 additions & 1,236 deletions CSharpErrorsAndWarnings.md
    1,236 additions, 1,236 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    File renamed without changes.
  7. @thomaslevesque thomaslevesque created this gist May 10, 2017.
    150 changes: 150 additions & 0 deletions CSharpErrorCodes.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,150 @@
    static void Main()
    {
    var errorCodes = GetErrorCodes();
    WriteMarkdownTable(errorCodes, Console.Out);
    }

    const string ErrorCodesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs";
    const string ErrorResourcesUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/CSharpResources.resx";

    private static IReadOnlyList<ErrorCode> GetErrorCodes()
    {
    using (var wc = new WebClient())
    {
    var enumMembers = GetErrorCodeEnumMembers(wc);
    var dictionary = GetResourceDictionary(wc);

    var errorCodes =
    enumMembers
    .Select(m =>
    ErrorCode.Parse(
    m,
    dictionary.TryGetValue(m.Identifier.ValueText, out var message) ? message : ""))
    .ToList();

    return errorCodes;
    }
    }

    private static IEnumerable<EnumMemberDeclarationSyntax> GetErrorCodeEnumMembers(WebClient wc)
    {
    string errorCodesFileContent = wc.DownloadString(ErrorCodesUrl);
    var syntaxTree = CSharpSyntaxTree.ParseText(errorCodesFileContent);
    var root = syntaxTree.GetRoot();
    var enumDeclaration =
    root.DescendantNodes()
    .OfType<EnumDeclarationSyntax>()
    .First(e => e.Identifier.ValueText == "ErrorCode");
    return enumDeclaration.Members;
    }

    private static IReadOnlyDictionary<string, string> GetResourceDictionary(WebClient wc)
    {
    string resourcesFileContent = wc.DownloadString(ErrorResourcesUrl);
    var doc = XDocument.Parse(resourcesFileContent);
    var dictionary =
    doc.Root.Elements("data")
    .ToDictionary(
    e => e.Attribute("name").Value,
    e => e.Element("value").Value);
    return dictionary;
    }

    private static void WriteRuleSet(IEnumerable<ErrorCode> errorCodes, TextWriter writer)
    {
    var doc =
    new XDocument(
    new XElement("RuleSet",
    new XAttribute("Name", "C# Warnings as Errors"),
    new XAttribute("ToolsVersion", "15.0"),
    new XElement("Rules",
    new XAttribute("AnalyzerId", "Microsoft.CodeAnalysis.CSharp"),
    new XAttribute("RuleNamespace", "Microsoft.CodeAnalysis.CSharp"),
    from e in errorCodes
    where e.Level == DiagnosticLevel.Warning
    select new XElement("Rule",
    new XAttribute("Id", e.Code),
    new XAttribute("Action", "Error")))));
    doc.Save(writer);
    }

    private static void WriteMarkdownTable(IEnumerable<ErrorCode> errorCodes, TextWriter writer)
    {
    writer.WriteLine("# All C# errors and warnings");

    writer.WriteLine();
    writer.WriteLine("*Parsed from the [Roslyn source code](https://github.com/dotnet/roslyn) using Roslyn.*");
    writer.WriteLine();

    writer.WriteLine("|Code|Level|Name|Message|");
    writer.WriteLine("|----|-----|----|-------|");
    foreach (var e in errorCodes)
    {
    if (e.Value == 0)
    continue;
    writer.WriteLine($"|{e.Code}|{e.Level}|{e.Name}|{e.Message}|");
    }
    }

    class ErrorCode
    {
    public static ErrorCode Parse(EnumMemberDeclarationSyntax member, string message)
    {
    string name = member.Identifier.ValueText;
    if (name == "Void" || name == "Unknown")
    {
    return new ErrorCode(name, 0, DiagnosticLevel.Unknown, message);
    }
    else
    {
    return new ErrorCode(
    name.Substring(4),
    int.Parse(member.EqualsValue?.Value?.GetText()?.ToString() ?? "0"),
    ParseLevel(name.Substring(0, 3)),
    message);
    }
    }

    private ErrorCode(string name, int value, DiagnosticLevel level, string message)
    {
    Name = name;
    Value = value;
    Level = level;
    Message = message;
    }

    public string Name { get; }
    public int Value { get; }
    public string Code => $"CS{Value:D4}";
    public DiagnosticLevel Level { get; }
    public string Message { get; }

    private static DiagnosticLevel ParseLevel(string level)
    {
    switch (level)
    {
    case "HDN":
    return DiagnosticLevel.Hidden;
    case "INF":
    return DiagnosticLevel.Info;
    case "WRN":
    return DiagnosticLevel.Warning;
    case "ERR":
    return DiagnosticLevel.Error;
    case "FTL":
    return DiagnosticLevel.Fatal;
    default:
    return DiagnosticLevel.Unknown;
    }
    }
    }

    enum DiagnosticLevel
    {
    Unknown,
    Hidden,
    Info,
    Warning,
    Error,
    Fatal
    }
    1,240 changes: 1,240 additions & 0 deletions CSharpErrorsAndWarnings.md
    1,240 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.