主要内容

polyspace.test.SanitizerProfilingResults Class

Namespace: polyspace.test

(Python) Review code sanitizer profiling results

Since R2026a

Description

This Python® class contains code sanitizer profiling results obtained from executing C/C++ tests.

Creation

Description

sanitizerResults = profilingResults.Sanitizer loads code sanitizer results:

Properties

expand all

Number of instrumented expressions that caused errors during test runs. Each expression is instrumented for certain types of errors that can result during execution (for instance, an array access is instrumented for potential out of bounds access). If one of the expected errors happens during the execution of a test, it is counted within FailedCount.

If an expression is instrumented for multiple errors, each instrumentation is counted separately.

Number of instrumented expressions that did not cause errors during test runs. Each expression is instrumented for certain types of errors that can result during execution (for instance, an array access is instrumented for potential out of bounds access). If none of the expected errors happen during the execution of a test, it is counted within PassedCount.

If an expression is instrumented for multiple errors, each instrumentation is counted separately.

Total number of expressions instrumented for possible errors during test execution. If an expression is instrumented for multiple errors, each instrumentation is counted separately.

File-by-file breakdown of code sanitizer results, returned as a list of polyspace.test.SanitizerFile objects with the following properties.

PropertyDescription
FullPathFull path to a file, returned as a string.
PathFilename, returned as a string.
PassedCountNumber of instrumented expressions in the file that did not cause errors during test execution.
FailedCountNumber of instrumented expressions in the file that caused errors during test execution.
TotalCountNumber of instrumented expressions in the file
FunctionsFunction-by-function breakdown of code sanitizer results in the file, returned as a list of polyspace.test.SanitizerFunction objects.

Each polyspace.test.SanitizerFunction object represents the code sanitizer results for a single function and consists of these properties:

PropertyDescription
FileFull path to file containing the function, returned as a string.
FunctionFunction signature, returned as a string.
PassedCountNumber of instrumented expressions in the function that did not cause errors during test execution.
FailedCountNumber of instrumented expressions in the function that caused errors during test execution.
TotalCountNumber of instrumented expressions in the function
ExpressionsExpression-by-expression breakdown of code sanitizer results in the function, returned as a list of polyspace.test.SanitizerExpressionDetail objects.

Each polyspace.test.SanitizerExpressionDetail object represents the code sanitizer results for a single instrumented expression and consists of the following properties. If an expression is instrumented for multiple errors, each instrumentation is counted separately.

PropertyDescription
FileFull path to file containing the expression, returned as a string.
FunctionSignature of function containing the expression, returned as a string.
IsPassedWhether the expression was free of the error during test runs (True or False).
IsFailedWhether the expressed caused an error during test runs (True or False)..
KindType of error, returned as a string.
PredicatesMore details of the error, specified as a list of polyspace.test.SanitizerPredicate objects with properties Details (full error message), FailedCount, PassedCount, and TotalCount.
SourceLocationLocation of the expression in the source code returned as a polyspace.test.SourceLocation object with properties Startline, Endline, StartColumn, and EndColumn
TextText of the expression, returned as a string.

Methods

expand all

Examples

collapse all

This example shows how to print the list of expressions that led an to an error when running C/C++ tests.

In general, you generate and manage execution profiling results by using classes from the polyspace.project and polyspace.test modules. Before starting, make sure you can import these modules on a Python shell or in a Python script without errors. For more information, see Set Up Python API for Polyspace.

  1. Import the required modules:

    import polyspace.project
    import polyspace.test
    import os

  2. Add source files and xUnit test files to a project, and parse the source code in the project. This example uses some example source and test files available with a Polyspace® Test™ installation.

    examples_path = os.path.join(polyspace.__install_path__, "polyspace", 
                                "examples", "doc_pstest", "sanitizer_profiling")
    polyspaceProject = polyspace.project.Project("testProject")
    polyspaceProject.Code.Folders.add(os.path.join(examples_path, "src"))
    polyspaceProject.refreshSources()
    polyspaceProject.ActiveBuildConfiguration.Language="CPP"
    codeInfo = polyspace.project.parseCode(polyspaceProject)
    

  3. Write three tests for the function int32_t compute_results(int32_t input). Each test invokes the function with an input that is designed to trigger a specific error.

    functionComputeResults = codeInfo.getFunctionBySignature(
        "int32_t compute_results(int32_t input)"
    )
    
    suiteComputeResults = polyspaceProject.TestSuites.create(
        "suite_compute_results"
    )
    
    testPow = suiteComputeResults.TestCases.create("test_pow")
    testPowStep = testPow.TestSteps.createTabular(
        "test_pow_step",
        functionComputeResults
    )
    testPowStep.Inputs["input"].Value = "2"
    
    testArrayAccess = suiteComputeResults.TestCases.create("test_array_access")
    testArrayAccessStep = testArrayAccess.TestSteps.createTabular(
        "test_array_access_step",
        functionComputeResults
    )
    testArrayAccessStep.Inputs["input"].Value = "3"
    
    testShift = suiteComputeResults.TestCases.create("test_shift")
    testShiftStep = testShift.TestSteps.createTabular(
        "test_shift",
        functionComputeResults
    )
    testShiftStep.Inputs["input"].Value = "4"

  4. Run the tests added to the project with code sanitizer profiling enabled.

    res = polyspace.test.run(
       polyspaceProject,
       ProfilingSelection=polyspace.test.ProfilingSelection.SANITIZER
    )

  5. Read the code sanitizer profiling results from the test results.

    profilingResults = res.Profiling
    sanitizerResults = profilingResults.Sanitizer
  6. Loop through the details of the sanitizer profiling results and print the expressions that led to an error.

    for f in sanitizerResults.Files:
        for func in f.Functions:
            for expr in func.Expressions:
                if expr.IsFailed:   # expression caused an error
                    line = expr.SourceLocation.StartLine
                    kind = expr.Kind
                    expr_text = expr.Text.strip()
    
                    # Print each failing predicate's full message
                    for pred in expr.Predicates:
                        if pred.FailedCount > 0:
                            print(
                                f"{f.FullPath}:{line} "
                                f"[{kind}] {pred.Details} :: Expression with issue: '{expr_text}'"
                            )
    You should see an output such as the following:
    example.cpp:7 [UINT_CONV_OVFL] Conversion from unsigned int to unsigned int must not overflow :: Expression with issue: '<'
    example.cpp:12 [OUT_BOUND_ARRAY] Array index i must be within bounds (Array size: 33) :: Expression with issue: 'gsX3.a[i][0]'
    example.cpp:21 [FLOAT_STD_LIB] Function must not return Not-a-Number and not cause floating exception :: Expression with issue: '(*funcPtr)(x, y)'
    example.cpp:21 [FLOAT_STD_LIB] Function must not return Infinity :: Expression with issue: '(*funcPtr)(x, y)'

Version History

Introduced in R2026a