Code covered by the BSD License  

Highlights from
Code Coverage Tool

image thumbnail

Code Coverage Tool

by

 

15 Mar 2007 (Updated )

Code Coverage Tool measures C statement coverage in generated code.

html_dump(sfname,cname)
function html_dump(sfname,cname)
%HTML_DUMP Insert coverage data into RTW HTML report
%   HTML_DUMP(SFNAME,CNAME) finds an HTML report generated from the file 
%   named CNAME.c in absolute directory SFNAME. The coverage data 
%   generated by cct in the base workspace (cct_CNAME and cct_CNAME_l) will
%   be inserted into the file CNAME_c.html. The file with coverage 
%   measurement inserted is finally saved as CNAME_cov.html.

%   Copyright 2006-2007 The MathWorks, Inc.
%   $File: $
%   $Revision: 1.1.4.2.2.1 $
%   $Date: 2007/07/11 14:31:18 $

% Open HTML file
% Assume it will run from the model directory

sfname_html=[sfname '\' cname '_c.html'];

% Get the coverage data from the base workspace
covcount = evalin('base',['cct_' cname]);
covline = evalin('base',['cct_' cname '_l']);

fid=fopen(sfname_html,'r');
fid2=fopen([sfname '\' cname '_cov.html'],'w');

%the length of the "</span>" tag
length_closing_span_tag = 6;

%disp(['Version ' version('-release')]);
switch version('-release')
    case {'2007a','2006b','2006a'}
        line_number_html_string = '<SPAN class="LineNumber">';
        col_no_where_line_no_starts = 26;
        span_close_tag_no = 1;           
        col_no_where_first_line_no_starts = 34;        
        line_no_width = 7;                
    case {'2007b','2008a','2008b'}
        line_number_html_string = '<SPAN class="LN">';
        col_no_where_line_no_starts = 31;            
        span_close_tag_no = 2;
        line_no_width = 7;        
        col_no_where_first_line_no_starts = 34;
    case {'2009a'}
        line_number_html_string = '<SPAN CLASS="LN">';
        col_no_where_line_no_starts = 31;            
        span_close_tag_no = 2;
        line_no_width = 7;        
        col_no_where_first_line_no_starts = 34;
    otherwise
        warning('CCT not fully tested on this version. Assuming R2009a settings for report generation.')
        line_number_html_string = '<SPAN CLASS="LN">';
        col_no_where_line_no_starts = 31;            
        span_close_tag_no = 2;            
        line_no_width = 7;
        col_no_where_first_line_no_starts = 34;        
end


while 1,
    fl = fgetl(fid);
    if ~ischar(fl),break,end
    llstr='.....';
    
    if findstr(fl,line_number_html_string),
        % Found a line, now identify where the first /SPAN is located
        closing_span_tag_locations = findstr(fl,'</SPAN>');
        if ~isempty(closing_span_tag_locations),
            % The /SPAN should always be the span_close_tag_no-th
            % Now find the code line number
            tmp2=fl(col_no_where_line_no_starts:(closing_span_tag_locations(span_close_tag_no)-1));
            tmp2=str2num(tmp2);
            if ~isempty(tmp2)
                tmp3=find(covline == tmp2);
                if ~isempty(tmp3)
                    % We have a line count for this
                    if covcount(tmp3) == 0,
                        llstr='#####';
                    else
                        llstr=sprintf('%05d',covcount(tmp3));
                    end
                    % Special case for CCT: The line after the call to cct_dump will always
                    % show zero coverage because it executes after the instrumentation. 
                    % This dump rouine will artifically show a count of 1 for this line.
                    if findstr(fl,'__cct_dump();'),
                        covcount(tmp3+1) = 1;
                        % Correct base workspace data also
                        evalin('base',['cct_' cname '(' num2str(tmp3+1) ')=uint16(1);']);
                    end

                end
            end
  
            if length(fl) > (closing_span_tag_locations(span_close_tag_no)),
                temp_span_close_tag_no = span_close_tag_no;                
                if strcmp(version('-release'),'2007b') & (str2num(fl(col_no_where_first_line_no_starts:(col_no_where_first_line_no_starts + line_no_width))) == 1) % the second tests if the line number is 1
                    % in the case of the version being 2007b, the first
                    % line only has one </span> tag so it needs a different
                    % value for span_close_tag_no
                    span_close_tag_no = 1;
                end
                fl = [fl(1:(closing_span_tag_locations(span_close_tag_no)+length_closing_span_tag)) '<SPAN class="Keyword"> ' llstr ' </SPAN>' fl((closing_span_tag_locations(span_close_tag_no)+length_closing_span_tag + 1):length(fl))];
                span_close_tag_no = temp_span_close_tag_no;
            else
                fl = [fl(1:closing_span_tag_locations(span_close_tag_no)+length_closing_span_tag) '<SPAN class="Keyword"> ' llstr ' </SPAN>'];
            end
        end

    end
    % Check for former internal links and replace with links to the
    % coverage report
    fl=strrep(fl,[cname '_c.html'],[cname '_cov.html']);
    fprintf(fid2,'%s\n',fl);
end        

                     
    

Contact us