www.gusucode.com > polyspace_bf 案例源码 matlab代码程序 > polyspace_bf/GenerateAndAnalyzeCodeExample.m
%% Generate and Analyze Code % Generate code from referenced models and S-Functions, run a Polyspace % analysis from Simulink, and find code defects and MISRA-C:2012 rule violations. %% Generate Code and Run Analysis % Before running Polyspace on models, define the scope of your % analysis and generate code in Embedded Coder. %% % 1. Open the example model. psdemo_model_link_sl %% % 2. Right-click the |controller| subsystem. % % 3. From the context menu, select *C/C++ Code* > *Build This Subsystem*. % % 4. In the dialog box, select *Build*. % % 5. After the build is completed, right-click the |controller| subsystem. % % 6. From the context menu, select *Polyspace* > *Options* % % 7. In the Configuration Parameters window, select *Product Mode* > *Bug Finder*. % % 8. Apply your changes and close the Configuration Parameters window. % % 9. Right-click the |controller| subsystem. % % 10. Select *Polyspace* > *Verify code generated for* > *Selected % subsystem*. % % You can monitor progress from the Command Window. The results % are displayed in the Polyspace environment. %% Review Results % In the Polyspace Environment, explore your results and link back to the % model. % % <<../bf_results.png>> % % 1. Select the first result |Integer division by zero|. % % This result shows a possible division by zero. The Source pane shows the % division operation between variables |controller_B.threshold| and % |controller_B.Cumulatedangle|. % % 2. To see this division operation in your model, select the link % |<S4>/limit_ratio|. % In your model, the related block is highlighted in blue. % % <<../model_highlighted.png>> % %% Fix Errors by Modifying the Model % The division by zero error stems from the |Cumulated angle| block, whose % signal can be zero. To fix the error in the code, modify this block in % your model. %% % 1. Before dividing with the Cumulated angle, add a switch block that % checks for values equal to zero. % % <<../fixed_model.png>> % % 2. Rebuild the |controller| subsystem. % % 3. Rerun the Bug Finder analysis. % % The results show that your fix in the model eliminated the division by % zero defect. % % <<../fixed_bf_results.png>> %