The uPython API can report any violations that appear in the Violation Browser in Understand, as the two work synchronously. These violations can include CodeCheck violations, analysis errors, and warnings. This could be useful in generating compliance reports or finding high risk areas of your code.
Let's go through a simple example. My goal is to print a list of every file in my project that contains a violation from SciTools' Recommended Checks. Here I'll use one of Understand's bundled Python scripts as my jumping off point. This script simply lists every file in a project database:
Now that we have our starting code, we need to set up our CodeCheck configuration as well as the Violation Browser to make the violations accessible to our script.
In Understand's top level menu go to Checks > Select Checks. Here we'll create a new CodeCheck configuration and select all 18 of Scitools' Recommended Checks:
Note that the option at the top of the configuration to have this configuration run automatically in the background MUST be selected in order for these checks to populate the Violation Browser.
Another thing to keep in mind is that we can also import SARIF files to populate the Violation Browser if so desired.
Now, once we close CodeCheck and analyze the project, our custom configuration will populate the Violation Browser with violations. We can confirm this by selecting Checks > Browse Violations in the top level menu.
Ok, we're ready to access these violations using our Python API script!
As you can see, the first change we made was accessing all violations in the given file with the .violations() API method. I then declare an empty list to hold violation check ID's. If the file contains any violations, I print the file name along with the total number of violations in that file. Then, we go through each violation, calling the .check_id() method on that violation object. This returns the unique ID for that CodeCheck violation (or UND_ERROR/UND_WARNING for errors and warnings respectively), which is then printed below the filename.
Here is a snippet of the output following invocation of the script:
As you could imagine, this is just one of many use cases that retrieving violations from the API could have. We encourage you to explore the Python API, and especially the new understand.Violation class, to see how powerful it is for yourself!
If you have some custom checks but don't have the time to implement them right now, we are now accepting requests for custom checks!
Need help? Contact [email protected] or visit our About the Understand Python API page.