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
| Tool | Purpose | Configuration file |
|---|---|---|
| Clangd | Provides IDE capabilities such as code navigation, completion, and hover hints | Passed in via clangd.arguments |
| Clang-Tidy | Static 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
- Install the
clangdplugin. - Before use, compile
be(RELEASE)andbe-ut(ASAN)once to generate the correspondingcompile_commands.jsonfiles. - Edit
settings.jsonor 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
| Parameter | Description |
|---|---|
clangd.path | Path to the Clangd executable |
--background-index | Indexes the entire project in the background to speed up symbol lookup |
--clang-tidy | Enables Clang-Tidy static checks |
--compile-commands-dir | Specifies the directory containing compile_commands.json |
--completion-style=detailed | Shows detailed information during completion |
-j=5 | Number of parallel jobs Clangd uses to analyze files |
--query-driver | Path 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.