?? generatestats.m
字號:
function stats = generateStats(dirName)
%generateReport Create a report for the given directory.
%
% Example:
% stats = generateStats(pwd);
% metrics = computeMetrics(stats);
% createReport(metrics,pwd)
% Pramod Kumar and Matthew Simoneau
% Copyright 2005-2006 The MathWorks, Inc.
% Get a listing of all files.
if ~isdir(dirName)
error('"%s" is not a valid directory.',dirName);
end
% Get a listing of all the M-files, including subdirectories.
fileList = getFilenames(dirName);
% Since cloning is cross-file, compute this first.
cloneInfo = getCloneInfo(fileList,dirName);
% Now iterate over each file in fileList.
stats = struct('lines',{},'comments',{},'complexity',{},'help',{},'mlint',{},'clash',{},'clone',{});
for i = 1:size(fileList,2)
file = fileList{i};
stats(i,1).filename = removeBase(file,dirName);
stats(i,1).name = hashFilename(file,dirName);
[stats(i,1).lines,stats(i,1).comments] = getLineCounts(file);
stats(i,1).complexity = cyclomaticComplexity(file);
[stats(i,1).help,stats(i,1).helpmessages] = generatedHelpMetric(file);
stats(i,1).mlintmessages = getMlintMessages(file);
stats(i,1).mlint = numel(stats(i,1).mlintmessages);
[stats(i,1).clash,stats(i,1).clashname] = getClash(file);
cloneMatches = strmatch(stats(i,1).filename,{cloneInfo.file1},'exact');
stats(i,1).clone = numel(cloneMatches);
stats(i,1).cloneInfo = cloneInfo(cloneMatches);
end
%==========================================================================
function s = removeBase(s,base)
s = strrep(s,[base filesep],'');
s = strrep(s,filesep,'/');
%==========================================================================
function s = hashFilename(s,dirName)
s = strrep(s,[dirName filesep],'');
s = strrep(s,'\','_');
%==========================================================================
function filenames = getFilenames(root)
% getFilenames Returns all the M-files this directory and in subdirectories.
% Everything in this folder.
allFiles = dir([root filesep '*']);
% The files we need in this folder.
myFiles = dir([root filesep '*.m']);
% Build a list of all the files matching the regular expression in this folder.
filenames = {};
for k = 1:length(myFiles)
filenames{k} = [root filesep myFiles(k).name];
end
% Do a recursive call of this function for every sub directory of this folder.
for k = 1:length(allFiles)
if allFiles(k).isdir && ...
~strcmp(allFiles(k).name,'.') && ...
~strcmp(allFiles(k).name,'..')
thisFolder = [root filesep allFiles(k).name];
filenames = [filenames getFilenames(thisFolder)];
end
end
%==========================================================================
function [lineCount,commentCount] = getLineCounts(filename)
% Returns the number of lines of code and comments in a file.
% Count code lines. It does not include the lines of comments or blank
% lines. If a statement is continued on many lines it counts as only one
f = textread(filename,'%s','delimiter','\n','whitespace','','bufsize',4095*10);
[unused,bptok] = xmtok(f);
% The first line of code of a function is not counted in bptok
codeCount = sum(bptok)+1;
% Count comment lines.
percent = regexp(f, '\s*[%]','start', 'once');
linesWithPercent = sum(~cellfun('isempty',percent));
percentInQuotes = regexp(f, '''.*[%].*''','start', 'once');
linesWithPercentInQuotes = sum(~cellfun('isempty',percentInQuotes));
commentCount = linesWithPercent-linesWithPercentInQuotes;
% Sum for total.
lineCount = codeCount + commentCount;
%==========================================================================
function y = cyclomaticComplexity(fileName)
%CYCLOMATICCOMPLEXITY finds max Cyclomatic Complexity of functions in the file
% y = CYCLOMATICCOMPLEXITY(myFile) calculates the cyclomatic complexity of
% the function in "fileName". If the file has more than one function in
% it then CYCLOMATICCOMPLEXITY calculates the cyclomatic complexity of
% each function and reports the maximum
%
% For example if you have 2 functions in the file with complexity of 7 and
% 9 this function will report a complexity value of 9
% Note that the "-cyc" flag is still undocumented and will change in a
% future release.
s = mlint('-cyc','-struct',fileName);
lfunctions = {};
lcomplexities = [];
p1 = 'The McCabe complexity of ''(\w+)'' is (\d+)\.';
p2 = 'The McCabe complexity is (\d+)\.';
for i = 1:length(s)
m1 = regexp(s(i).message,p1,'tokens');
m2 = regexp(s(i).message,p2,'tokens');
if ~isempty(m1)
lfunctions{end+1,1} = m1{1}{1};
lcomplexities(end+1,1) = str2double(m1{1}{2});
elseif ~isempty(m2)
[unused,name,ext] = fileparts(fileName);
lfunctions{end+1,1} = [name ext];
lcomplexities(end+1,1) = str2double(m2{1}{1});
end
end
if isempty(lcomplexities)
y = 0;
else
y = max(lcomplexities);
end
%==========================================================================
function [metric,y] = generatedHelpMetric(fileName)
%This function generates a number for each file based on the help content
%of the file. H1, Help, and Example each could result in 1 warning. so if
%all three are missing 3 is returned. if two are missing 2 is returned etc.
temp = 0;
y = generateHelpInfo(fileName,0);
if(isempty(y.description))
temp = temp+1;
end
if(isempty(y.example))
temp = temp+1;
end
if(isempty(y.help))
temp = temp+1;
end
metric = temp;
%==========================================================================
function helpInfo = generateHelpInfo(fileName, helpSubfunsDisplay)
%HELPRPT Scan a file or directory for help.
strc = [];
if (helpSubfunsDisplay==1)
callStrc = getcallinfo(fileName,'subfuns');
else
callStrc = getcallinfo(fileName,'file');
end
for m = 1:length(callStrc)
strc(end+1).filename = fileName;
strc(end).name = callStrc(m).name;
strc(end).type = callStrc(m).type;
strc(end).firstLine = callStrc(m).firstline;
if strcmp(strc(end).type,'subfunction')
helpStr = helpfunc([strc(end).filename filemarker strc(end).name]);
else
helpStr = helpfunc(strc(end).filename);
end
strc(end).help = code2html(helpStr);
% Remove any leading spaces and percent signs
% Grab all the text up to the first carriage return
helpTkn = regexp(helpStr,'^\s*%*\s*([^\n]*)\n','tokens','once');
if isempty(helpTkn)
strc(end).description = '';
else
strc(end).description = helpTkn{1};
end
% Now we grep through the function line by line looking for
% copyright, example, and see-also information. Don't bother
% looking for these things in a subfunction.
% NOTE: This will not work for Japanese files
if ~strcmp(strc(end).type,'subfunction')
f = textread(fileName,'%s','delimiter','\n','whitespace','','bufsize',4095*10);
strc(end).example = '';
for i = 1:length(f)
% The help report searches all comment lines in the file,
% even if they occur after the typical "help block" at the
% top of the file.
% If there's no comment character, short circuit the loop.
if isempty(strfind(f{i},'%'))
continue
end
exTkn = regexpi(f{i},'^\s*%(\s*example)','tokens','once');
if ~isempty(exTkn)
exampleStr = {' ',exTkn{1}};
strc(end).exampleLine = i;
% Loop through and grep the entire example
% We assume the example ends when there is a blank
% line or when the comments end.
exampleCompleteFlag = 0;
for j = (i+1):length(f)
codeTkn = regexp(f{j},'^\s*%(\s*[^\s].*$)','tokens','once');
if isempty(codeTkn)
exampleCompleteFlag = 1;
else
exampleStr{end+1} = codeTkn{1};
end
if exampleCompleteFlag
break
end
end
strc(end).example = sprintf('%s\n',exampleStr{:});
end
end
end
end
helpInfo= strc;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -