5

I'd like to find a list of the message IDs of the built-in code analyzer warning messages used for suppressing those messages either on a single line or in a file (see here). I know you can search by message ID, but the list in the preferences (see here) doesn't actually show the ID itself. Any help would be appreciated, thanks!

orenyk
  • 452
  • 5
  • 9

1 Answers1

4

Solution 1: (provides only some message tags)

After a while of digging within the MATLAB API, I've come up with the solution you wanted:

%// Obtain all definitions
msgDefinitions = com.mathworks.widgets.text.mcode.MLint.getMessageDefinitions();
%// Count definitions
numDefinitions = msgDefinitions.size();

definitionDictionary{numDefinitions,2}=[]; %//Preallocation

for ind1=1:numDefinitions
    definitionDictionary{ind1,1} = char(msgDefinitions.get(ind1-1).toString());
    definitionDictionary{ind1,2} = char(msgDefinitions.get(ind1-1).getTag().toString);
end

%//Optional for convenience:
definitionDictionary = sortrows(definitionDictionary,2);

Solution 2: (provides Category tags, Syntax Errors, Aesthetics and Readability ...)

An alternative solution (resulting in a longer, but messier list) is

allMsgs = mlint('-allmsg', filename); 

where filename can be any valid filename. Category tags may be recognized by a bunch of ======= in the message.

Solution comparison:

allMsgs = mlint('-allmsg', 'Untitled.m');
msgCodes = regexpi(strtrim({allMsgs.message}'),' ','split'); ...'
msgCodes = cellfun(@(v) v(1), msgCodes(:,1));

alternativeDictionary = sortrows([{allMsgs.message}' msgCodes],2);

In MATLAB 2014a the 2nd solution results in a dictionary of size 571. The 1st solution is of length 477. Generally speaking, the 2nd solution provides more information.


More information is available in the Undocumented MATLAB article. You can also look at this question on SO.


Below are some details on how I uncovered the 1st solution:

The internal workflow inside the code checker is as follows:

  1. When you right-click on a suppressible warning the Code Checker (AKA CodeAnalyzer, lint, mlint) it, MKit.class adds 3 options to suppress warnings that execute the following methods of CodeAnalyzerUtils.class:
    • On this line - executes the method suppressMessage.
    • In this file - executes the method suppressAllMessages.
    • In all files - executes the method disableMessage.
  2. Having clicked on one of the first two options, the doInsertSuppression method is called, adding the string: " %#ok<" + paramString + '>'.
  3. The mysterious paramString is a private property named fTag that is present within subclasses of MLint.class and accessible using a getTag() method. Where the tags come from is still unclear.

Sources (in order of appearance):

  1. $matlabroot/java/jar/widgets.jar -> com.mathworks.widgets.text.mcode.MKit
  2. $matlabroot/java/jar/widgets.jar -> com.mathworks.widgets.text.mcode.analyzer.CodeAnalyzerUtils
  3. $matlabroot/java/jar/widgets.jar -> com.mathworks.widgets.text.mcode.analyzer.CodeAnalyzerActions
Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • That's a great start, but if I'm understanding the article correctly this will only return a list of the errors / warnings present in a specific file. I was wondering if there was a way to find a master list of message IDs and messages for *all* of the built in errors and warnings. Never mind, it's in the Undocumented MATLAB article: `allMsgs = mlint('-allmsg', filename)` where `filename` can be any valid filename. This works with the `checkcode` function as well. – orenyk Jul 30 '14 at 15:58
  • Yea I figured it out, you don't need to use your second solution, the original will work just with a different option (although that returns 585 messages whereas your method only returns 477, not sure why). Thanks for your help! – orenyk Jul 30 '14 at 16:06
  • I wonder why the `-allmsg` solution results in `571` definitions, while the `getMessageDefinitions()` yields only `477`. – Dev-iL Jul 30 '14 at 16:15
  • Yea, that's pretty weird. Thanks for spending so much time on this! – orenyk Jul 30 '14 at 16:17