Parsing C++ Files

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:

An IDE needs a parser for the language and the semantic analyzes.

Using Clang Code Model

The Clang project provides libraries for parsing C and C++ 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, 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.

To make parsing faster, pre-compiled headers are ignored by default. To specify that Clang processes them, select Tools > Options > C++ > Code Model, and deselect the Ignore pre-compiled headers check box.

When Clang encounters risky or possibly erroneous constructions, it issues warnings. To request or suppress warnings, select Tools > Options > C++ > Code Model. You can either select one of the predefined configurations, or create a copy of a configuration and edit it to fit your needs:

  • 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.

The following services are currently implemented in the experimental Clang code model plugin:

  • Code completion
  • Syntactic and semantic highlighting

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

Building Clang Code Model Plugin

  1. Acquire Clang 3.8.0 or higher in one of the following ways:
    • Use the package manager of your system.
    • Download and install LLVM from the LLVM Download Page or LLVM Snapshot Builds.
    • Build Clang by following Getting Started: Building and Running Clang.

      Add the following options to the cmake call in order to build an optimized version and to specify the installation directory:

      -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/your/install/dir

      After building, install with

      make install

  2. Set LLVM_INSTALL_DIR to point to the installation directory of LLVM either as part of the build environment or pass it directly to qmake when you build Qt Creator.
  3. Rerun qmake and build Qt Creator.

Configuring Clang Code Model Plugin

  1. Select Help > About Plugins > C++ > ClangCodeModel to enable the plugin.
  2. Restart Qt Creator to be able to use the plugin.
  3. To specify settings for the Clang code model, select Tools > Options > C++ > Code Model.

  4. In the Configuration to use list, select the warnings to request.
  5. To have Clang process pre-compiled headers, deselect the Ignore pre-compiled headers check box.

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