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:
- Code completion
- Syntactic and semantic highlighting
- Navigating in the code by using the locator, following symbols, and so on
- Inspecting code by using the class browser, the outline, and so on
- Finding and renaming symbols
- Refactoring actions
The Clang code model plugin offers some of these services for C++ on top of Clangd.
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
- Outline of symbols
- 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)
To configure the Clang code model globally:
- Select Preferences > C++ > Code Model.
- 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.
- To process precompiled headers, deselect the Ignore precompiled headers check box.
- 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.
- 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.
- 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.
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.
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:
- Select Preferences > C++ > Clangd > Use clangd.
- In Path to executable, enter the path to clangd version 14, or later.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Select Add to select sessions that should use a single clangd process for indexing.
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
-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
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.
You can specify Clang code model settings at project level by selecting Projects > clangd.
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.
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.