如何禁用特定文件的特定编译器警告 [英] How to disable a particular compiler warning for a particular file

查看:240
本文介绍了如何禁用特定文件的特定编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个小的编码项目,将被出售给其他公司。我需要为它创建一些文件,所以我决定用沙堡。服用后远长下载并安装,我终于得到了它的工作,并注意到任何公开的方法或类,没有评论有红色文字说明注释失踪。然后我安装Ghostdoc,以加快我的评论。这开启了编译器警告缺少XML注释,这是伟大的,因为我现在什么都有了,我需要评论的列表。

I'm working on a small coding project that is going to be sold to other companies. I needed to create some documentation for it, so I decided to use Sandcastle. After taking far to long to download and install, I finally got it working, and noticed any public method or class that didn't have a comment had red text stating that the comment was missing. I then installed Ghostdoc to help speed up my commenting. This turned on the compiler warnings for missing xml comments, which was great because I now had a list of everything I needed to comment.

我的一个code文件是自动生成的文件,其中包含约3000编译器警告。我需要能够跳过该文件从创建的任何缺少XML注释编译器警告。我知道这些东西从<一个href="http://stackoverflow.com/questions/7982525/visual-studio-disabling-missing-xml-comment-warning">this帖子:

One of my code files is an auto-generated file, which contains around 3000 compiler warnings. I need to be able to skip that file from creating any "Missing Xml Comment" compiler warnings. I know these things from this post:

  • 在我知道我可以关闭编译器警告的项目,但也有认为应该有编译器警告该项目的其他文件。
  • 在我知道我可以使用的#pragma警告禁止1591 删除编译器警告,但该文件是自动生成的,我真的不希望有重新每次都手动添加。
  • 在我知道我可以添加一个空的评论,但再一次,我真的不希望有重新添加它的每一个文件被再生的时间。
  • 我可以拉出该文件到它自己的项目,因为它是唯一一类在它的命名空间,然后删除XML注释要求,但我不希望客户必须处理另一个DLL。
  • 的类是局部类,所以我在想试图找到一种方式来增加使用#pragma警告禁止在部分类,但即使这是可能的,还有枚举,将抛出警告。
  • I know I can turn off the compiler warning for the project, but there are other files in the project that should have the compiler warning.
  • I know I can use #pragma warning disable 1591 to remove the compiler warning, but the file is autogenerated, and I really don't want to have to re-add it manually every time.
  • I know I can add an empty comment, but once again, I really don't want to have to re-add it every time the file gets regenerated.
  • I could pull the file out into it's own project since it is the only class in it's namespace, then remove the XML comment requirement, but I don't want the customer to have to deal with another dll.
  • The classes are partial classes so I was thinking about trying to find a way to add the #pragma warning disable in a partial class but even if that was possible, there are still enums that would throw warnings.

我怎么能告诉VS忽略一个文件为特定类型的警告?

How can I tell VS to ignore a single file for a particular type of warning?

推荐答案

我用大卫·蒂伦的建议,并创造了一个C#程序中插入的#pragma警告禁用邮件到我的自动生成的文件。理想的情况是我把它作为后操作,文件本身的产生,但现在一个pre-编译命令就足够了,因为我的发言将是文件中的第一行之一,它只会有读了几行,看到禁用说法已经存在,并退出,所以它不应该放慢建设。下面是我的程序,供大家欣赏! :)

My Solution

I used David Thielen's suggestion and created a C# program that inserts #pragma warning disable messages into my autogenerated files. Ideally I'd call it as a post operation to the generation of the files themselves, but for now a pre-compile command will suffice since my statement will be one of the first lines in the file, it will only have to read a few lines, see that the disable statement is already there, and quit, so it shouldn't slow down the build. Below is my program for all to enjoy! :)

/// <summary>
/// Addes #pragma warning disable messages to source code as part of a prebuild to ignore warnings.
/// Primarly used for autogenerated classes that may contain some compiler warnings by default
/// </summary>
public class Program
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="args">
    /// [0] - file to edit
    /// [1] - line number to insert / edit warning disable at
    /// [2+] - warnings numbers to disable</param>
    static void Main(string[] args)
    {
        // Preconditions
        if (args.Length < 2)
        {
            throw new ArgumentException(String.Format("Unexpected number of parameters.{0}Parameters should be [0] - file to edit{0}[1] - line number to insert / edit warning disable at{0}[2+] - warnings numbers to disable", Environment.NewLine));
        }
        else if (args.Length == 2) { return; }

        // Valid number of args, validate arguments
        string filePath = args[0];
        long lineNumber;

        if(!long.TryParse(args[1], out lineNumber)){
            throw new InvalidCastException("Unable to cast \"" + args[1] + "\" to a long");
        }

        string[] compilerWarningNumbers = new string[args.Length - 2];
        Array.ConstrainedCopy(args, 2, compilerWarningNumbers, 0, compilerWarningNumbers.Length);

        // File Name and line number are valid, perform search and replace
        AddOrUpdateCompilerWarningDisabler(filePath, lineNumber, String.Join(",", compilerWarningNumbers));

    }

    private const string TEMP_FILE_POSTFIX = ".CompilerWarningDisabler.txt";
    public static void AddOrUpdateCompilerWarningDisabler(string filePath, long lineNumber, string compilerWarningNumberCSV)
    {
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException("File path not found!", filePath);
        }

        // Set Clear Readonly Flag
        FileInfo fileInfo = new FileInfo(filePath);
        bool isReadOnly = fileInfo.IsReadOnly;

        // Get Temp File Name and Delete if it already exists
        string tempFile = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + TEMP_FILE_POSTFIX);
        File.Delete(tempFile);

        // Read from the target file and write to a new file.
        int currentLine = 1;
        string line;
        string textToWrite = "#pragma warning disable " + compilerWarningNumberCSV;
        try
        {
            using (StreamReader reader = new StreamReader(filePath))
            using (StreamWriter writer = new StreamWriter(tempFile))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    if (currentLine == lineNumber)
                    {
                        if (line.StartsWith("#pragma warning disable"))
                        {
                            if (line == textToWrite)
                            {
                                // Nothing has changed, don't bother copying file
                                return;
                            }
                            else
                            {
                                line = textToWrite;
                            }
                        }
                        else
                        {
                            writer.WriteLine(textToWrite);
                            writer.WriteLine(line);
                        }
                    }
                    else
                    {
                        writer.WriteLine(line);
                    }
                    currentLine++;
                }

                if (currentLine == lineNumber)
                {
                    writer.WriteLine(textToWrite);
                }

                if (currentLine < lineNumber)
                {
                    throw new InvalidDataException("File " + filePath + " does not contain line number " + lineNumber);
                }
            }

            // This could potentially delete the source file, but this should be messing with autogenerated files, so even if it does happen, it shouldn't be to hard to get it back
            if (isReadOnly)
            {
                fileInfo.IsReadOnly = false;
            }
            File.Delete(filePath);
            File.Move(tempFile, filePath);
            if (isReadOnly)
            {
                new FileInfo(filePath).IsReadOnly = true;
            }
        }
        finally
        {
            File.Delete(tempFile);
        }
    }
}

这篇关于如何禁用特定文件的特定编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆