SPeCS Packages Documentation
    Preparing search index...

    Module @specs-feup/clava-misra - v1.0.5

    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.

    To get started, ensure the tool's NPM package is installed in your project:

    npm install @specs-feup/clava-misra
    

    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.

    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.

    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, or c11).
    • 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 together
      • single: For rules whose violation detection is identified within individual translation units independently
      • all: 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.

    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
    

    Modules

    ast-visitor/Context
    ast-visitor/Visit
    ast-visitor/VisitWithContext
    main
    MISRA
    MISRAContext
    MISRARule
    MISRATool
    rules
    rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand
    rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch
    rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak
    rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault
    rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast
    rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses
    rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition
    rules/Section17_Functions/Rule_17_3_ImplicitFunction
    rules/Section17_Functions/Rule_17_4_NonVoidReturn
    rules/Section17_Functions/Rule_17_6_StaticArraySizeParam
    rules/Section17_Functions/Rule_17_7_UnusedReturnValue
    rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl
    rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl
    rules/Section2_UnusedCode/Rule_2_6_UnusedLabels
    rules/Section2_UnusedCode/Rule_2_7_UnusedParameters
    rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule
    rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions
    rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions
    rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory
    rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions
    rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions
    rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions
    rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort
    rules/Section3_Comments/Rule_3_1_CommentSequences
    rules/Section5_Identifiers/IdentifierRenameRule
    rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers
    rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames
    rules/Section5_Identifiers/Rule_5_7_UniqueTagNames
    rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers
    rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers
    rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition
    rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage
    rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition
    rules/UserConfigurableRule
    StandardGuideline
    tests/utils
    utils/CallUtils
    utils/CommentUtils
    utils/FileUtils
    utils/FunctionUtils
    utils/IdentifierUtils
    utils/JoinpointUtils
    utils/ProgramUtils
    utils/SwitchUtils
    utils/TypeDeclUtils
    utils/VarUtils