%% Run Quality Checks on S-Functions 
%
% This example shows how to use S-Function analyzer APIs to check
% S-functions, in order to identify potential problems and improvements.
% 
%
% The S-Function analyzer MATLAB command line APIs consist of:
%
% * *Simulink.sfunction.Analyzer*               - Class used to launch S-function analyzer and get analysis results.   
% * *Simulink.sfunction.analyzer.BuildInfo* - Class used to create an object to specify the build information for an S-function.
% * *Simulink.sfunction.analyzer.Options*      - Class used to specify the running options for S-function Analyzer.
% * *Simulink.sfunction.analyzer.findSfunctions* - Static method to return all feasible S-functions in a model or library for S-function analyzer.
%
% See the help for more detailed information about the above APIs.

% Copyright 2017-2020 The MathWorks, Inc. 

%% Specify the Input Model or Library (required)
%
% The S-Function analyzer requires a model or a library in MATLAB path as
% input, which contains the S-function blocks to be analyzed.
model = 'slexSfunctionCheckExample';

%% Specify the Build Information for S-functions (optional)
% 
% S-Function analyzer checks the source code of S-functions when they are available. 
% Heuristics are applied to automatically locate the source code based on S-function names.
% For example, if S-function source code and the input model are in the same folder,
% the source code will be included in the analysis automatically. Otherwise, the build information 
% has to be specified through Simulink.sfunction.analyzer.BuildInfo. If no
% source code is available, this section can be skipped.
%
% To specify the build information,
% first determine the eligible S-functions in the input model, then create a Simulink.sfunction.analyzer.BuildInfo object for each S-function. For S-function slexBadSFcn,
% there are two associated source files: slexBadSFcn.c and
% slexBadSFcn_wrapper.c
sfunctions= Simulink.sfunction.analyzer.findSfunctions(model);
bdInfo= Simulink.sfunction.analyzer.BuildInfo(fullfile(matlabroot,'examples','simulink_features','data','slexBadSFcn.c'),...
                                              'ExtraSrcFileList',{'slexBadSFcn_wrapper.c'},...
                                              'SrcPaths',{fullfile(matlabroot,'examples','simulink_features','data')});
%% Specify the Running Options for S-function Analyzer (optional)
% 
% The execution options for S-function analyzer can be configured through
% Simulink.sfunction.analyzer.Options object, such as whether to enable Polyspace checks and Parameter Robustness
% checks, model simulation timeout, and output path for result report. If no
% specific options are needed, this section can be skipped. All default
% options will be applied.
opts = Simulink.sfunction.analyzer.Options();
opts.EnableRobustness = 1;
%% Run the S-function Analyzer and See the Result (required)
sfunAnalyzer = Simulink.sfunction.Analyzer(model,'BuildInfo',{bdInfo},'Options',opts);  
analysisResult=sfunAnalyzer.run();
sfunAnalyzer.generateReport();
