Can I create custom alerts for matlab parser code?

The Matlab code analyzer has a lot of good suggestions for fixing bugs and inefficiencies, but sometimes I come across situations that I would like to catch with the analyzer. In particular, I think of code like the following:

if numel(list > x) ... end 

I can’t think from head to head about any situation when I need to use the above code, and the following code:

 if numel(list) > x ... end 

quite often used.

I looked at the list of possible things that the code analyzer could warn about, and I did not find this feature.

So my question is: is it possible to add my own warnings to the code analyzer, and if so, how?

I understand that, if possible, this can be a difficult task, so any alternatives or workarounds for a particular problem will also be appreciated!

+6
source share
1 answer

I do not believe that you can add new code templates for MATLAB Code Analyzer for search. All you can do is set which existing alerts are displayed or suppressed.

I'm not sure which third-party tools can be used to analyze the code, and creating a general-purpose analyzer will be quite difficult. However, if there were some very specific, well-defined patterns that you would like to try and highlight in your code, you can try to parse it using regular expressions (screaming music and screams).

This is often difficult, but doable. As an example, I wrote this piece of code that is looking for the template mentioned above. One of the things that often need to be managed when doing something like this is the set of sets of enclosing parentheses that I process by first removing the uninteresting pairs of parentheses and their contents:

 function check_code(filePath) % Read lines from the file: fid = fopen(filePath, 'r'); codeLines = textscan(fid, '%s', 'Delimiter', '\n'); fclose(fid); codeLines = codeLines{1}; % Remove sets of parentheses that do not encapsulate a logical statement: tempCode = codeLines; modCode = regexprep(tempCode, '\([^\(\)<>=~\|\&]*\)', ''); while ~isequal(modCode, tempCode) tempCode = modCode; modCode = regexprep(tempCode, '\([^\(\)<>=~\|\&]*\)', ''); end % Match patterns using regexp: matchIndex = regexp(modCode, 'numel\([^\(\)]+[<>=~\|\&]+[^\(\)]+\)'); % Format return information: nMatches = cellfun(@numel, matchIndex); index = find(nMatches); lineNumbers = repelem(index, nMatches(index)); fprintf('Line %d: Potential incorrect use of NUMEL in logical statement.\n', ... lineNumbers); end % Test cases: % if numel(list < x) % if numel(list) < x % if numel(list(:,1)) < x % if numel(list(:,1) < x) % if (numel(list(:,1)) < x) % if numel(list < x) & numel(list < y) % if (numel(list) < x) & (numel(list) < y) 

Note. I added some test cases in the comments at the bottom of the file. When I run this code on its own, I get the following:

 >> check_code('check_code.m') Line 28: Potential incorrect use of NUMEL in logical statement. Line 31: Potential incorrect use of NUMEL in logical statement. Line 33: Potential incorrect use of NUMEL in logical statement. Line 33: Potential incorrect use of NUMEL in logical statement. 

Please note that for the first, fourth and sixth test cases, a message is indicated that matches your error code (twice for the sixth test case, since there are two errors in this line).

Will this work in all possible situations? I would suggest no. You may need to increase the complexity of regex patterns to handle additional situations. But at least this can serve as an example of what you need to consider when analyzing code.

+2
source

Source: https://habr.com/ru/post/1013335/


All Articles