我使用MATLAB中的checkcode函数为我提供了所提供文件名中所有错误消息的结构,以及它们的McCabe复杂度和与该错误相关的ID。 IE;

info = checkcode(fileName, '-cyc','-id');

在MATLAB的首选项中,列出了所有可能的错误,并将其分为几类。例如“美学和可读性”,“语法错误”,“功能受损”等。

有没有一种方法可以使用从上面的代码行中获得的错误ID来访问这些类别?

最佳答案

为了解决这个问题,我脑海里浮现出各种不同的想法,最后终于为解决这个问题提出了一个绝妙的解决方案。

解决方案

该解决方案的关键组件是undocumented -allmsg 标志of checkcode (or mlint )。如果提供此参数,则将打印mlint ID,严重性代码和描述的完整列表。更重要的是,类别也打印在此列表中,并且所有mlint ID都在其各自的mlint类别下方列出。

执行

现在我们不能仅使用checkcode标志简单地调用mlint(或-allmsg),因为那太容易了。相反,它需要一个实际的文件来尝试解析和检查错误。您可以传递任何有效的m文件,但是我选择传递内置的sum.m,因为实际文件本身仅包含帮助信息(因为它的实际实现可能是C++),因此mlint能够非常快速地解析它而无需警告。

checkcode('sum.m', '-allmsg');

打印到命令窗口的输出摘录为:
   INTER    ========== Internal Message Fragments ==========
   MSHHH  7   this is used for %#ok and should never be seen!
    BAIL  7   done with run due to error
   INTRN    ========== Serious Internal Errors and Assertions ==========
   NOLHS  3   Left side of an assignment is empty.
   TMMSG  3   More than 50,000 Code Analyzer messages were generated, leading to some being deleted.
  MXASET  4   Expression is too complex for code analysis to complete.
   LIN2L  3   A source file line is too long for Code Analyzer.
    QUIT  4   Earlier syntax errors confused Code Analyzer (or a possible Code Analyzer bug).
   FILER    ========== File Errors ==========
   NOSPC  4   File <FILE> is too large or complex to analyze.
    MBIG  4   File <FILE> is too big for Code Analyzer to handle.
   NOFIL  4   File <FILE> cannot be opened for reading.
   MDOTM  4   Filename <FILE> must be a valid MATLAB code file.
   BDFIL  4   Filename <FILE> is not formed from a valid MATLAB identifier.
   RDERR  4   Unable to read file <FILE>.
   MCDIR  2   Class name <name> and @directory name do not agree: <FILE>.
   MCFIL  2   Class name <name> and file name do not agree: <file>.
   CFERR  1   Cannot open or read the Code Analyzer settings from file <FILE>. Using default settings instead.
   ...
    MCLL  1   MCC does not allow C++ files to be read directly using LOADLIBRARY.
   MCWBF  1   MCC requires that the first argument of WEBFIGURE not come from   FIGURE(n).
   MCWFL  1   MCC requires that the first argument of WEBFIGURE not come from FIGURE(n) (line <line #>).
    NITS    ========== Aesthetics and Readability ==========
    DSPS  1   DISP(SPRINTF(...)) can usually be replaced by FPRINTF(...).
   SEPEX  0   For better readability, use newline, semicolon, or comma before this statement.
   NBRAK  0   Use of brackets [] is unnecessary. Use parentheses to group, if needed.
   ...

第一列显然是mlint ID,第二列实际上是严重性编号(0 =大部分无害,1 =警告,2 =错误,4-7 =更严重的内部问题),第三列是以下消息:显示。

如您所见,所有类别也都有一个标识符,但没有严重性,其消息格式为===== Category Name =====

因此,现在我们可以解析这些信息并创建一些数据结构,使我们可以轻松地查找给定mlint ID的严重性和类别。

同样,它并不总是那么容易。不幸的是,checkcode(或mlint)只是将此信息打印到命令窗口中,而没有将其分配给我们的任何输出变量。因此,有必要使用 evalc (抖动)来捕获输出并将其存储为字符串。然后,我们可以轻松地解析此字符串以获取与每个mlint ID相关联的类别和严重性。

解析器示例

我将前面讨论的所有内容放到一个小函数中,该函数将生成一个结构,其中所有字段均为mlint ID。在每个字段中,您将收到以下信息:
warnings = mlintCatalog();
warnings.DWVRD

             id: 'DWVRD'
       severity: 2
        message: 'WAVREAD has been removed. Use AUDIOREAD instead.'
       category: 'Discouraged Function Usage'
    category_id: 17

如果您感兴趣的话,这里有个小功能。
function [warnings, categories] = mlintCatalog()
    % Get a list of all categories, mlint IDs, and severity rankings
    output = evalc('checkcode sum.m -allmsg');

    % Break each line into it's components
    lines = regexp(output, '\n', 'split').';
    pattern = '^\s*(?<id>[^\s]*)\s*(?<severity>\d*)\s*(?<message>.*?\s*$)';
    warnings = regexp(lines, pattern, 'names');
    warnings = cat(1, warnings{:});

    % Determine which ones are category names
    isCategory = cellfun(@isempty, {warnings.severity});
    categories = warnings(isCategory);

    % Fix up the category names
    pattern = '(^\s*=*\s*|\s*=*\s*$)';
    messages = {categories.message};
    categoryNames = cellfun(@(x)regexprep(x, pattern, ''), messages, 'uni', 0);
    [categories.message] = categoryNames{:};

    % Now pair each mlint ID with it's category
    comp = bsxfun(@gt, 1:numel(warnings), find(isCategory).');
    [category_id, ~] = find(diff(comp, [], 1) == -1);
    category_id(end+1:numel(warnings)) = numel(categories);

    % Assign a category field to each mlint ID
    [warnings.category] = categoryNames{category_id};

    category_id = num2cell(category_id);
    [warnings.category_id] = category_id{:};

    % Remove the categories from the warnings list
    warnings = warnings(~isCategory);

    % Convert warning severity to a number
    severity = num2cell(str2double({warnings.severity}));
    [warnings.severity] = severity{:};

    % Save just the categories
    categories = rmfield(categories, 'severity');

    % Convert array of structs to a struct where the MLINT ID is the field
    warnings = orderfields(cell2struct(num2cell(warnings), {warnings.id}));
end

概括

这是获取与给定mlint ID关联的类别和严重性的完全未公开的文档,但相当可靠。该功能在2010年甚至更早之前就已存在,因此它可以与您必须处理的任何版本的MATLAB一起使用。与简单地注解给定mlint ID属于什么类别相比,此方法还具有更大的灵活性,因为随着添加新功能和不推荐使用旧功能,类别(和严重性)在发行版本之间会有所变化。

感谢您提出这个具有挑战性的问题,我希望这个答案能为您提供一些帮助和见解!

09-07 05:43