目录

Using codescanner

Overview

Since version 26.0.1, Command Line Tools has integrated codescanner to detect resource leaks in the entire project. You can manually fix code defects based on the warning prompts in the check results to guarantee code quality during development.

The codescanner command format is as follows:

codescanner [options] [projectPath]

options: (optional) command-line configuration parameters. For details, see Table 1.

projectPath: (optional) root directory of the project to be checked. Both absolute and relative paths are supported. The default value is the current directory.

Table 1 codescanner command-line parameters

Parameter

Description

-h, --help

Flag to display the help information for codescanner.

-v, --version

Flag to display the version of codescanner.

-s, --sdkPath

(Optional) HarmonyOS SDK path specified during ArkTS code check.

The HarmonyOS SDK loading priority is as follows: SDK path specified in the command > Path of the SDK bundled with Command Line Tools > Paths from environment variables DEVECO_SDK_HOME and DEVECO_PATH/sdk/default.

By default, the path of the HarmonyOS SDK bundled with Command Line Tools is used.

-o, --output

(Optional) Output location of the check report.

By default, the report is output to the output folder in the current directory.

-f, --format

(Optional) Output format of the check report, which can be sarif (default) and html.

-l, --logPath

(Optional) Output location of runtime logs.

By default, runtime logs are output to the scanner folder in the current directory.

-c, --config

(Optional) Location of the codescanner check rule configuration file. The tool performs checks based on the configuration file.

By default, all ArkTS and C++ code check rules are applied.

--compileCommandsDir

(Mandatory for C++ code check) Directory containing compile_commands.json (compilation database file).

  • For apps developed with DevEco Studio: Project/.idea/.deveco/cxx
  • For apps not developed with DevEco Studio: Project root directory/Module/.cxx/default/default/debug/Computer environment (for example, arm_x86)/

--no-cpp

(Optional) Flag to disable C++ code check rules.

--no-arkts

(Optional) Flag to disable ArkTS code check rules.

codescanner check rule configuration file

The rule configuration file is a JSON file manually created by developers. The configuration fields and descriptions are as follows:

Example:

// Rule configuration file: xxxxxx.json
{
  "rules": {  // Rule structure
    "cpp": {      // C++ code check rules
      "ResourceLeak": [  // Rule set
        {
          "ruleId": "FileHandleNotClosed",        // Rule name
          "severity": "warning/error/fatal",   // Alarm severity of this rule
          "skip": false                           // Whether to skip the check against this rule
        },
        {
          "ruleId": "MemoryNotFreed",             // Rule name
          "severity": "warning/error/fatal",   // Alarm severity of this rule
          "skip": false                           // Whether to skip the check against this rule
        }
      ]
    },
    "arkts": {     // ArkTS code check rules
      "ResourceLeak": [        // Rule set
        {
          "ruleId": "MediaResourceNotReleased",    // Rule name
          "severity": "warning/error/fatal",    // Alarm severity of this rule
          "skip": false                            // Whether to skip the check against this rule
        },
        {
          "ruleId": "FileDescriptorNotClosed",    // Rule name
          "severity": "warning/error/fatal",    // Alarm severity of this rule
          "skip": false                            // Whether to skip the check against this rule
        }
      ]
    }
  }
}

Constraints

This tool is available only in the Chinese mainland.

Examples