Parsing C++ Files with the Clang Code Model

The code model is the part of an IDE that understands the language you are using to write your application. It is the framework that allows Qt Creator to offer the following services:

The Clang code model plugin offers some of these services for C++ on top of Clangd.

About the Clang Code Model

The Clang project has libraries for parsing C language family source files. The feedback you get through warning and error markers is the same as a compiler will give you, not an incomplete set or a close approximation, as when using the built-in Qt Creator code model. Clang focuses on detailed information for diagnostics, which is really useful if the code has typos, for example. We make use of these libraries via the clangd tool, which implements an LSP server.

Clang keeps up with the development of the C++ language. At the time of this writing, it supports C++98/03, C++11, C++14, C++17, C89, C99, Objective-C, and Objective-C++.

On the downside, for large projects using Clang as code model is slower than using the built-in code model. Clang does not need to generate object files, but it still needs to parse and analyze the source files. For small projects that only use STL, this is relatively fast. But for larger projects that include several files, processing a single file and all the included files can take a while.

The Clang code model plugin now offers some of the same services as the built-in C/C++ code model:

  • Code completion
  • Syntactic and semantic highlighting
  • Diagnostics
  • Outline of symbols
  • Tooltips
  • Following symbols
  • Renaming symbols
  • Finding occurrences of symbols

To use the built-in code model instead, select Preferences > C++ > clangd, and deselect the Use clangd check box. This setting also exists on the project level, so that you can have the clang-based services generally enabled, but switch them off for certain projects, or vice versa.

You can configure Clang diagnostics either globally or separately for:

  • Clang code model (globally or at project level)
  • Clang tools (globally or at project level)

Configuring Clang Code Model

To configure the Clang code model globally:

  1. Select Preferences > C++ > Code Model.

    {C++ Code Model preferences}

  2. To instruct the code model to interpret ambiguous header files as C language files if you develop mainly using C, select the Interpret ambiguous headers as C headers check box.
  3. To process precompiled headers, deselect the Ignore precompiled headers check box.
  4. When you select Show Preprocessed Source in the code editor context menu, the built-in preprocessor is used to show the pre-processed source file in the editor. To invoke the actual compiler for showing the code, deselect the Use built-in preprocessor to show pre-processed files check box. For more information, see Inspecting Preprocessed C++ Code.
  5. To avoid out-of-memory crashes caused by indexing huge source files that are typically auto-generated by scripts or code, the size of files to index is limited to 5MB by default. To adjust the limit, edit the value for the Do not index files greater than check box. To index all files, deselect the check box.
  6. To ignore files that match wildcard patterns, select the Ignore files check box and enter each wildcard pattern on a separate line in the field.

Configuring clangd

The clangd index provides exact and complete results for services such as finding references, following symbols under cursor, and using the locator, even for complex constructs. When you open a project, clangd scans the source files to generate the index. For large projects, this can take a while, but the index is persistent and re-scanning is incremental, so nothing is lost by closing and re-starting Qt Creator.

To temporarily disable global indexing, select Toggle Progress Details (1), and then select X on the progress bar.

{Toggle Progress Details button}

By default, Qt Creator runs one clangd process per project. If you have created sessions that have related projects, you can specify that the projects in the session should be managed by a single clangd process.

The document outline in the Outline view is backed by clangd's document symbol support, which makes the results more reliable than before.

To specify settings for clangd:

  1. Select Preferences > C++ > Clangd > Use clangd.

    "Clangd preferences"

  2. In Path to executable, enter the path to clangd version 14, or later.
  3. In the Background indexing field, select Off to use a faster, but less accurate built-in indexer than the one used by default. You can set the indexing priority depending on whether the accuracy of results or speed is more important to you during global symbol searches.
  4. In Header/source switch mode, select the C/C++ backend for switching between header and source files. While the clangd implementation has more capabilities than the built-in code model, it tends to find false positives. Try Both uses clangd if the built-in code model does not find anything.
  5. By default, clangd attempts to use all unused cores. You can set a fixed number of cores to use in Worker thread count. Background indexing also uses this many worker threads.
  6. Set the number of Completion results if you regularly miss important results during code completion. Set it to 0 to remove the limit on the number of completion results. Setting this to 0 or a very high number can make code completion slow.
  7. In Completion ranking model, select the clangd model to use for ranking completion suggestions. This determines their order in the selection list. The Decision Forest model (Default) results from pre-trained machine learning and usually provides better results than the hand-crafted Heuristic model. Select the latter if the completion suggestions stray too much from your expectations for your code base.
  8. In Document update threshold, specify the amount of time Qt Creator waits before sending document changes to the server. If the document changes again while waiting, this timeout is reset.
  9. Select Ignore files greater than to make parsing faster by ignoring big files. Specify the maximum size of files to parse in the field next to the check box.
  10. The Diagnostic configuration field shows the Clang checks to perform. Click the value of the field to open the Diagnostic Configurations dialog, where you can select and edit the checks to perform.
  11. Select Add to select sessions that should use a single clangd process for indexing.

Clang Checks

In addition to using the built-in checks, you can select Copy to create copies of them and edit the copies to fit your needs.

Build-system warnings displays warnings as specified by the build system.

Checks for questionable constructs combines the -Wall and -Wextra checks for easily avoidable questionable constructions and some additional issues.

Clang checks begin with -W. Each check also has a negative version that begins with -Wno.

Keep in mind that some options turn on other options. For more information, see Options to Request or Suppress Warnings or the GCC or Clang manual pages.

Specifying Clang Code Model Settings at Project Level

You can specify Clang code model settings at project level by selecting Projects > clangd.

Using Compilation Databases

The JSON compilation database format specifies how to replay single builds independently of the build system.

A compilation database is basically a list of files and the compiler flags that are used to compile the files. The database is used to feed the code model with the necessary information for correctly parsing the code when you open a file for editing.

To generate a compilation database from the information that the code model has, select Build > Generate Compilation Database.

You can add files, such as non-C files, to the project in compile_database.json.files.

You can use the experimental Compilation Database Project Manager to open the files in a compilation database with access to all the editing features of the Clang code model.

Note: Enable the Compilation Database Project Manager plugin to use it.

To switch between header and source files, select Tools > C++ > Switch Header/Source.

You can specify custom build steps and run settings for compilation database projects in the Projects mode. For more information, see Adding Custom Build Steps and Specifying Run Settings.

See also Enable and disable plugins.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.