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. The only difference between a code model and a compiler is that a code model does not generate an executable.

As Clang is a compiler, as well as a code model, it provides accurate information. The feedback you get through warning and error markers is the same as the 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.

Also, Clang already supports C++98/03, C89 and C99, Objective-C (and Objective-C++), and C++11 support is in active development.

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. You can specify that Clang processes them in Code Model options.

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

  • Highlighting
  • Code completion

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

Building Clang Code Model Plugin

  1. To build the Clang code model plugin, you must acquire Clang and LLVM in one of the following ways:
    • Build optimized versions of LLVM and Clang, as instructed in Getting Started: Building and Running Clang.

      The instructions describe how to build debug versions. To build optimized versions, enter the following command instead of just make:

      make ENABLE_OPTIMIZED=1

      For information about Git mirrors, see Git Mirror.

      Install the built versions by entering the following command:

      make install

    • Download and install LLVM from the LLVM Download Page or LLVM Snapshot Builds.
    • Use the package manager of your system.
  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.

    The following are examples of the LLVM_INSTALL_DIR values to use depending on the installation method:

    • Installed via package manager on Linux:

      LLVM_INSTALL_DIR=/usr/lib/llvm-3.4

    • Manually built on Unix in release mode:

      LLVM_INSTALL_DIR=$HOME/llvm-build/Release+Asserts

    • Installed from a snapshot on Windows:

      LLVM_INSTALL_DIR=C:\llvm

    The following messages indicate that the Clang code model plugin is built:

    • Project MESSAGE: Building ClangCodeModel plugin with Clang from /usr/lib/llvm-3.4
    • Project MESSAGE: INCLUDEPATH += /usr/lib/llvm-3.4/include
    • Project MESSAGE: LIBS += -L/usr/lib/llvm-3.4/lib -lclang
  3. Rebuild 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. Select Tools > Options > C++ > Code Model, and select the parser to use for files of each type.
  4. To process pre-compiled headers before processing any project files, deselect the Ignore pre-compiled headers check box.

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