Qt Creator DevIntro CppSupport

From Qt Wiki
Revision as of 09:48, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


[toc align_right="yes" depth="3"]

<BIG RED&gt;CAUTION: This page is no yet reviewed by the C++ Support maintainer.</BIG RED&gt;

C++ Support in Qt Creator

This page is meant as an introduction / collection of hints for developers wanting to help with the C++ Support in Qt Creator.

Here is a filter showing all unresolved C++ bugs: "Creator / C++ > All unresolved bugs&quot;:https://bugreports.qt.io/secure/IssueNavigator.jspa?mode=hide&amp;requestId=13573

Please read the mailing list post "Editors/Code-models: new year's resolutions&quot;:http://lists.qt.io/pipermail/qt-creator/2013-January/001768.html .

Hint: The paths on this page are relative to the top level directory of Qt Creator.

Related source directories and classes

The sources for the C++ Support are spread over several directories. To give you a quick overview, here are some important source directories and prominent classes you will find in them.

Libraries
* src/libs/3rdparty/cplusplus - C++ Frontend
<code&gt;Token, Lexer, Parser, Bind, Control, AST, ASTVisitor, Scope, Type, TranslationUnit&lt;/code&gt;
* src/libs/cplusplus
<code&gt;Preprocessor, Snapshot, LookupContext, Document, Macro, FindUsages&lt;/code&gt;

Plugins

  • src/plugins/cppeditor - C++ Editor
    <code&gt;CppPlugin, CppClass, CppFunction, CppInclude, SemanticHighlighter&lt;/code&gt;
  • src/plugins/cpptools - C++ Tools
    <code&gt;CppToolsPlugin, FindInClass, FindLocalSymbols, FindFunctionDefinition, IndexingSupport, CheckSymbols, CppCompletionAssistProvider, CppPreprocessor, CppModelManager, CppLocatorFilter, SemanticInfo&lt;/code&gt;

General hints

  • Care about the tests - See also section about tests below.
    You can learn a lot from tests. Please run them. Please fix them. Please add new tests for new functionality.
  • Debugging / Remove optimization before debugging
    src/libs/cplusplus/cplusplus.pro: Comment out <code&gt;QMAKE_CXXFLAGS_DEBUG = O2&lt;/code&gt; for proper debugging.
    * Debugging / Enable parser tracing
    src/libs/3rdparty/cplusplus/Parser.cpp: Comment out <code&gt;#define CPLUSPLUS_NO_DEBUG_RULE&lt;/code&gt; to get output like:


</s> parseTranslationUnit, ahead: '' (1) - block-errors: 0<br />- parseTranslationUnit, ahead: 'typedef' (1) - block-errors: 0<br /> parseDeclaration, ahead: 'typedef' (1) - block-errors: 0<br /> parseSimpleDeclaration, ahead: 'typedef' (1) - block-errors: 0<br />- parseBuiltinTypeSpecifier, ahead: 'long' (2) - block-errors: 0<br />- parseBuiltinTypeSpecifier, ahead: 'int' (3) - block-errors: 0<br />


* Debugging / Enable preprocessor tracing
Set the environment variable <code&gt;QTCREATOR_DUMP_FILENAME_WHILE_PARSING&lt;/code&gt; (evaluated at run time) to get output like:

<br />Parsing file: &quot;/usr/include/stdlib.h&amp;quot;<br />Parsing file: &quot;/usr/include/c/4.6/new&amp;quot;<br />Parsing file: &quot;/usr/lib/gcc/x86_64-linux-gn


* Debugging / Enable dumping the ModelManager configuration
Set the environment variable <code&gt;QTCREATOR_DUMP_PROJECT_INFO&lt;/code&gt; (evaluated at run time) to get debug statements regarding the detected language, C11 detection, Qt Version, precompiled header, defines, includes, frameworkPaths, sources. The merged include paths, merged framework paths and merged defined macros will also be printed. In total you will get quite much output. This configuration changes on e.g. switching Kits.
* Use the provided tools - See also section about tools below.


h2. Tools used for development
There is a number of tools you can and should make use of. They should work on all major platforms (GNU/Linux, Mac, Windows).
* cplusplus-mkvisitor - Creates an AST visitor class.
* cplusplus-update-frontend - Updates sources in the parser after modifying AST.h.
* cplusplus-ast2png - Visualizes AST and symbol hierarchy by generating PNG files for given code.
* cplusplus-frontend - Parses given files (parser as standalone tool).
h3. cplusplus-mkvisitor
Source directory: src/tools/cplusplus-mkvisitor


Usage & Purpose:

$ ./cplusplus-mkvisitor -h<br />Usage: cplusplus-mkvisitor [-v] [path to AST.h]
<br />Print a visitor class based on AST.h to stdout.
<br />Default path: $QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h.<br />


Example Usage:

$ ./cplusplus-mkvisitor &gt; myvisitor.cpp<code>
<br />h3. cplusplus-update-frontend
<br />Source directory: src/tools/cplusplus-update-frontend
<br />Usage &amp; Purpose:<br />

$ ./cplusplus-update-frontend -h
Usage: cplusplus-update-frontend
cplusplus-update-frontend <frontend-dir&gt; <dumpers-file&gt;


Generate appropriate header and source files of the C+ frontend accordingly
to AST.h and print the paths of the written files. Run this tool after
modifying AST.h.

Default values:
frontend-dir: $QTC_SOURCE/src/libs/3rdparty/cplusplus
dumpers-file: $QTC_SOURCE/tests/tools/cplusplus-ast2png/dumpers.inc

Example Usage:<br />

$ ./cplusplus-update-frontend
$QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTVisit.cpp
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatch0.cpp
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatcher.cpp
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTClone.cpp
$QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.cpp
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTVisitor.h
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatcher.h
$QTC_SOURCE/tests/tools/cplusplus-ast2png/dumpers.inc
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTfwd.h
$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTPatternBuilder.h

=== cplusplus-ast2png ===

Source directory: tests/tools/cplusplus-ast2png

Usage &amp; Purpose:<br />

$ ./cplusplus-ast2png -h
Usage: cplusplus-ast2png [-v] [-p ast] <file1&gt; <file2&gt; …

Visualize AST and symbol hierarchy of given C++ files by generating png image files
in the same directory as the input files. Print paths to generated image files.

Options:
-v Run with increased verbosity.
p <ast&gt; Parse each file as <ast&gt;. <ast&gt; is one of:
'declarator' or 'dr'
- 'expression' or 'ex'
- 'declaration' or 'dn'
- 'statement' or 'st'
- 'translationunit' or 'tr'
If this option is not provided, each file is tried to be parsed as
declarator, expression, etc. using the stated order.

Standard input is also read. The resulting files start with "_stdincontents.cpp&quot;
and are created in the current working directory. To show the AST for simple snippets
you might want to execute:

$ echo "int foo() {}" | ./cplusplus-ast2png && xdg-open _stdincontents.cpp.ast.png

Prerequisites:
1) Make sure to have 'dot' from graphviz locatable by PATH.
2) Make sure to have an up to date dumpers file by using 'cplusplus-update-frontend'.

&lt;code&amp;gt;dot&amp;lt;/code&amp;gt; comes with &quot;graphviz&amp;quot;:http://www.graphviz.org/. On Ubuntu you have to install the package graphviz.

Note also that processing might fail for too big input files. Generating an image (dot) is a cpu and memory intensive task. This tool is meant for small snippets, therefore the processing of input from stdin.

Example Usage:<br />

$ echo "int foo() {}" | ./cplusplus-ast2png
_stdincontents.cpp.ast.png
_stdincontents.cpp.symbols.png
$ ./cplusplus-ast2png /tmp/helloworld.cpp
/tmp/helloworld.cpp.ast.png
/tmp/helloworld.cpp.symbols.png

=== cplusplus-frontend ===

Source directory: tests/manual/cplusplus-frontend

Usage &amp; Purpose:<br />

$ ./cplusplus-frontend -h
Usage: cplusplus-frontend [-v] <file1&gt; <file2&gt; … Run the parser with the given files.

Example Usage:<br />

$ ./cplusplus-frontend tests/t1.cpp
tests/t1.cpp:37: error: expected a declaration
typedef enum { k };
^

Tests

"Plugin&quot; auto tests

These tests are in src/plugins/cpptools/*_test.cpp and src/plugins/cppeditor/*_test.cpp. They are included in the plugin itself, since they can't be separated easily.

Make sure that WITH_TESTS is defined (currently this is set for a debug build). Otherwise Qt Creator will not understand the "-test&quot; option.

Run the tests with: <code&gt;$ ./qtcreator -test CppTools -test CppEditor&lt;/code&gt;

Qt Creator will start, execute all tests and quit. Watch the output.

"External&quot; auto tests

You can find these tests in tests/auto/cplusplus. They are the 'separable' tests.

Just build and run the relevant test project.

If you're using shadow builds, make sure to place the build directory accordingly to the source tree inside the build directory of Qt Creator since the tests referene libCPlusPlus.so.1 from Qt Creator. If you run the tests from Qt Creator, you probably want to add the Qt Creator project as a dependency (Projects, <test project&gt;, Dependencies).

Running all tests

  1. (Open &) Build the project tests/auto/cplusplus.pro either in-source or as shadow-build.
  2. Run <code&gt;$ make check&lt;/code&gt; to run all tests. If a test fails, subsequent tests will not be executed, therefore you might want to add "-i&quot; to the command for ignoring failures. If you run the tests from Qt Creator, you probably want to add a custom run configuration calling these commands.

Manual tests