@specs-feup/clava-misra
v1.0.4
Published
Clava library to automatically detect and correct violations of the MISRA-C:2012 standard in C code
Readme
Clava-MISRA Tool
A Clava-based library to automatically detect and correct parts of C code that violate MISRA-C:2012 coding standard.
For more details, see the Clava Transpiler repository.
Installation
To get started, ensure the tool's NPM package is installed in your project:
npm install @specs-feup/clava-misraUsage
1. Analysis
You can use the tool to detect violations by executing the following statements in your script:
import MISRATool from "@specs-feup/clava-misra/MISRATool";
MISRATool.checkCompliance();After analysis, each identified violation will be displayed, including its location and description.
2. Correction
Besides analysis, the tool can also correct the provided source code to comply with the coding guidelines, using the following statements:
import MISRATool from "@specs-feup/clava-misra/MISRATool";
MISRATool.correctViolations();After the transformation, any violations that could not be fixed will be displayed along with their justification. The corrected files will be saved in the woven_code folder.
You can provide an optional JSON config file to assist in correcting specific rules, such as implicit function calls, disallowed functions, and missing return statements. For instance, you can:
- Define default values for certain types to address functions with missing return statements.
- Specify the path or library for implicit function calls.
- Provide custom implementations for disallowed functions.
The config file should follow this structure:
{
"defaultValues": {
"unsigned int": 0,
"float": 0.0,
"enum Status": "SUCCESS",
"Color": "RED",
"my_int_type": "0"
},
"implicitCalls": {
"printf": "stdio.h",
"foo": "CxxSources/utils/functions.c"
},
"disallowedFunctions": {
"stdlib.h": {
"malloc": {
"replacement": "custom_malloc",
"location": "utils/custom_stdlib.c"
},
"exit": {
"replacement": "custom_exit",
"location": "utils/custom_stdlib.c"
}
}
}
}Note: Not all fields (defaultValues, implicitCalls, disallowedFunctions) are mandatory. If the config file is not provided or lacks the necessary information to fix a violation, the violation will remain and be displayed as unresolved.
Execution
To execute a project that uses this tool, provide the following information:
- The path to your script file.
- The C standard to use (
c90,c99, orc11). - The path to the source code to process.
- (Optional) The full path to a config file.
- (Optional) The analysis type:
system: For rules whose violation detection requires analyzing multiple files togethersingle: For rules whose violation detection is identified within individual translation units independentlyall: For both system and single translation rules (default)
npx clava classic <scriptFile.js> -pi -std <c90 | c99 | c11> -p <path/to/source/code> [-av "<options>"]Note: Some guidelines are tied to a specific C language standard (e.g., c90, c99, c11). Therefore, they will not be detected when running under other standards. For instance, the rule 17.3 ("A function shall not be declared implicitly") only applies to c90 and is not reported when analyzing c99.
Examples:
Default use (no config file or analysis type):
npx clava classic dist/main.js -pi -std c99 -p CxxSources/In this case, analysis type is all by default.
With analysis type specified:
npx clava classic dist/main.js -pi -std c99 -p CxxSources/ -av "type=system"Using a config file:
npx clava classic dist/main.js -pi -std c99 -p CxxSources/ -av "config=misra_config.json"With both analysis and config file specified:
npx clava classic dist/main.js -pi -std c99 -p CxxSources/ -av "type=system config=misra_config.json"To view other available options, run:
npx clava classic --help