Skip to main content

C++ Static Code Analysis

Apache Doris supports static analysis of C++ code with Clangd and Clang-Tidy. Both are bundled in the LDB-toolchain, and you can also install or build them yourself.

Tool Comparison

ToolPurposeConfiguration file
ClangdProvides IDE capabilities such as code navigation, completion, and hover hintsPassed in via clangd.arguments
Clang-TidyStatic analysis and code quality checks (can be invoked by Clangd).clang-tidy in the Doris root directory

Compared with vscode-cpptools, Clangd offers more accurate code navigation and integrates the diagnostics and quick-fix features of Clang-Tidy.

Clang-Tidy Configuration

Clang-Tidy check rules are centralized in the .clang-tidy file in the Doris root directory. You can enable or disable specific checks by editing this file.

Configuring Clangd in VSCode

Steps

  1. Install the clangd plugin.
  2. Before use, compile be(RELEASE) and be-ut(ASAN) once to generate the corresponding compile_commands.json files.
  3. Edit settings.json or change the plugin configuration directly in Preferences.

Configuration Example

{
"clangd.path": "ldb_toolchain/bin/clangd",
"clangd.arguments": [
"--background-index",
"--clang-tidy",
"--compile-commands-dir=doris/be/build_Release/",
"--completion-style=detailed",
"-j=5",
"--all-scopes-completion",
"--pch-storage=memory",
"--pretty",
"--query-driver=ldb_toolchain/bin/*"
],
"clangd.trace": "output/clangd-server.log"
}

Key Parameters

ParameterDescription
clangd.pathPath to the Clangd executable
--background-indexIndexes the entire project in the background to speed up symbol lookup
--clang-tidyEnables Clang-Tidy static checks
--compile-commands-dirSpecifies the directory containing compile_commands.json
--completion-style=detailedShows detailed information during completion
-j=5Number of parallel jobs Clangd uses to analyze files
--query-driverPath to the compiler, used to resolve system header files

FAQ

Q: How do I troubleshoot clangd when it reports a missing header file or symbol?

Confirm that BE has been compiled at least once to generate compile_commands.json, and that --compile-commands-dir points to the correct directory.