How to work with C/C++ code in DVT IDE for VS Code
Overview
This video walks you through configuring C/C++ support in DVT IDE for VS Code and showcases SystemVerilog DPI-C cross-language linking and and flagging DPI signature mismatches.
This video was made using DVT IDE version 26.2.24.
Details
Introduction
This video covers how to configure and work with C and C++ code in DVT IDE for VS Code. DVT provides several features on top of LLVM clangd, for example SystemVerilog DPI-C cross-language linking. In this example we start with a project that's already configured and built for SystemVerilog. The build configuration file also specifies the C source files and compilation arguments.
Prerequisites
First, we install the LLVM clangd extension which provides features such as code completion, navigation and errors reported on the fly in C source code.
The clangd extension requires the clangd language server, which parses C source code and serves as the backend for all these features.
Once the language server is installed we also configure DVT_CLANGD_BIN environment variable to point to it.
If you choose to install clangd language server separately and it's available in the $PATH environemnt variable, this step is not required.
Next, we run the DVT: Change Languages... command and check the C/C++ language.
Now DVT IDE manages the clangd language server configuration,
namely it generates and maintains the compile_commands.json in sync with the C sources and compile arguments specified in the DVT IDE build configuration file.
If you prefer to manage clangd configuration yourself, simply provide it in .dvt/cpp_user_provided/compile_commands.json.
Working with C Files
Now that configuration is done, let's take a tour of the features. This Opentitan IP-level scoreboard calls a hashing function implemented in C via DPI. To navigate to its C implementation, either use the editor context menu or run the DVT: Go to C/C++ Definition command.
Inside the C source code the clangd extension provides full language support.
You can use hyperlinks to navigate, Ctrl+space to get completion proposals and compilation problems are reported on the fly.
In addition DVT IDE also provides the ability to see where a C function is used in SystemVerilog. Place the editor cursor on the C declaration and run the References: Find All References command. The results include both calls in C as well as SystemVerilog.
DVT IDE also flags mismatches between SystemVerilog DPI function signatures and their C counterparts: parameter count, parameter type and return type.
C/C++ Extended
If you also check C and C++ Extended in the language selection picker, an extended set of features becomes available. You can use DVT IDE query keys in Workspace Symbols, for example a search for "macro: size" shows only preprocessing macros matching the query, both from C and SystemVerilog. Similarly, the Compile Order View presents compiled C files side by side with the SystemVerilog ones.