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 provide the following services:

Qt Creator comes with a plugin that provides some of these services for C++ on top of Clang.

About the Clang Code Model

The Clang project provides 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 contains typos, for example.

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 following services are currently implemented in the Clang code model plugin:

  • Code completion
  • Syntactic and semantic highlighting
  • Diagnostics
  • Clang-Tidy checks
  • Clazy checks
  • Tooltips
  • Renaming of local symbols

To use the plugin, you must activate it and configure it in Qt Creator.

Code Model Warnings

The predefined configurations request Clang warnings at the following levels:

  • Pedantic Warnings uses the -Wpendantic option that requests all the warnings demanded by strict ISO C and ISO C++.
  • Warnings for Questionable Constructs combines the -Wall and -Wextra options to request all warnings about easily avoidable questionable constructions and some additional warnings.
  • Warnings for Almost Everything uses the -Weverything option with negative options to suppress some warnings.

You can edit the predefined configurations to request specific warnings beginning with -W. Each of these warnings 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.

Using Clang-Tidy Checks

Clang-Tidy is delivered as a part of the Clang library delivered with Qt Creator. It provides diagnostics and fixes for typical programming errors, such as style violations, interface misuse, or issues that can be found via static analysis.

Qt Creator integrates the fixes and diagnostics into the Clang library, and therefore they cannot be easily changed or extended when used with Qt Creator.

Using Clazy

Clazy is delivered as a part of the Clang library delivered with Qt Creator. It helps Clang understand Qt semantics. It prints out Qt related compiler warnings, ranging from unnecessary memory allocation to misuse of API and provides refactoring actions for fixing some of the issues.

The Clazy checks are divided into levels from 0 to 3. Each level adds checks to the previous level. The checks at level 0 are very stable and provide hardly any false positives, while the checks at level 3 can be considered experimental.

For more information about the checks run at each level, see List of Checks in the Clazy documentation.

Activating Clang Code Model

If you build Qt Creator yourself, ensure that the plugin is also built, as described in the Qt Creator README file.

To activate the plugin:

  1. Select Help > About Plugins > C++ > ClangCodeModel to enable the plugin.
  2. Restart Qt Creator to be able to use the plugin.

    If you build Qt Creator yourself, add ${LLVM_INSTALL_DIR}\bin to the PATH variable so the LLVM libraries will be found on startup.

Configuring Clang Code Model

To specify settings for the Clang code model:

  1. Select Tools > Options > C++ > Code Model.

  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. 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.
  5. In Clang Dianostics, select one of the predefined configurations, or select Copy to create a copy of a configuration and edit it to fit your needs.
    • In the Clang tab, edit the predefined configuration to request specific warnings.
    • In the Clang-Tidy tab, select the checks to perform.

    • In the Clazy tab, select the level of Clazy checks to perform.

Specifying Clang Settings at Project Level

You can specify Clang settings at project level in the build settings of the project by selecting Projects > Clang Code Model. In addition to configuring the diagnostics, you can select the Enable MSVC-compliant template parsing check box to parse templates in a MSVC-compliant way. This enables Clang to parse headers for example from Active Template Library (ATL) or Windows Runtime Library (WRL). However, using the relaxed and extended rules means that no highlighting or completion can be provided within template functions.

© 2018 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.