Control generated C/C++ code using Mathworks tooling

1 view (last 30 days)
Hello everybody,
Please bear with me if the question may be trivial, i'm still pretty new to MATLAB and, in general, Mathworks tools.
So far, being a software engineer, i have mainly been the recipient of C/C++ code generated by Embedded Coder. It happens that this code sometimes has various undesired characteristics, eg:
  • Huge arrays (like millions of elements)
  • Wild C-style casts (for example casting to void*)
  • Floating point numbers direct comparison (ie with ==)
  • Very high cyclomatic complexity
I can detect this kind of problems with tools external to the Mathworks' ones, but this means i can only do it "after the fact".
Ideally i would like also the provider of the code to be able to detect them.
Is there a Mathworks tool capable of doing this?
I am thinking, for example, to the Polyspace ones or some "static analysis" app available in Matlab.
It would be fantastic to be able to also introduce "custom rules" for this static analysis.
Thanks in advance!

Accepted Answer

Anirban
Anirban on 24 Jan 2022
Edited: Anirban on 25 Jan 2022
The Polyspace static analysis tools are meant exactly for the kind of issues you are talking about, but even in Embedded Coder, it is possible to set up code generation to minimize/avoid some of these issues.
For instance, following the MISRA C:2012 rules 10.x and 11.x prevent C-style casts to incompatible types. Using Embedded Coder, you can generate code that is compliant with this set of rules. See Generate C/C++ Code with Improved MISRA Compliance. But with Polyspace Bug Finder, you can independently check for violations of these rules. See Check for Coding Standard Violations.
Polyspace Bug Finder has checks for all the things you mentioned:
In fact, Polyspace Bug Finder can be installed as an add-on in Simulink and run on the generated code. See Run Polyspace Analysis on Code Generated from Embedded Coder.
  3 Comments
Anirban
Anirban on 25 Jan 2022
Edited: Anirban on 25 Jan 2022
Polyspace does provide quite a few more complex customizations and every release, new customization capabilities are added. If you want specific customizations, they can be requested through Technical Support. For instance, you can define your own list of keywords to forbid from the code and then have Polyspace Bug Finder flag uses of those keywords or have checks on standard libraries be extended to checks on custom libraries, and so on.
As for the specific case of arrays exceeding a certain size, there is no check yet. The closest that is available in Code Prover is stack size estimation. You can put a threshold on the stack size usage and identify cases where the threshold is violated. So, if there are huge local arrays, those would be flagged. Since global arrays do not use the stack, this check will not apply to those.
Note: The analysis actually errors out for very large objects. See this troubleshooting for the error. But this error has a very high threshold, so it would not serve your needs.
fraben
fraben on 26 Jan 2022
Thank you Anirban, all the answers were really helpful :)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Polyspace Products for Ada in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!