Qt Creator DevIntro CppSupport: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Tools::QtCreator]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
[[Category:Tools::QtCreator]]


&lt;BIG RED&amp;gt;CAUTION: This page is no yet reviewed by the C++ Support maintainer.&lt;/BIG RED&amp;gt;
<BIG style="color:red">CAUTION: This page is no yet reviewed by the C++ Support maintainer.</BIG>


= C++ Support in Qt Creator =
= C++ Support in Qt Creator =
Line 7: Line 7:
This page is meant as an introduction / collection of hints for developers wanting to help with the 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: &quot;Creator / C++ &gt; All unresolved bugs&amp;quot;:https://bugreports.qt.io/secure/IssueNavigator.jspa?mode=hide&amp;amp;requestId=13573
Here is a filter showing all unresolved C++ bugs: [https://bugreports.qt.io/secure/IssueNavigator.jspa?mode=hide&requestId=13573 Creator / C++ > All unresolved bugs]


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


Hint: The paths on this page are relative to the top level directory of Qt Creator.
Hint: The paths on this page are relative to the top level directory of Qt Creator.
Line 17: Line 17:
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.
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'''<br />* src/libs/3rdparty/cplusplus - C++ Frontend<br />&lt;code&amp;gt;Token, Lexer, Parser, Bind, Control, AST, ASTVisitor, Scope, Type, TranslationUnit&amp;lt;/code&amp;gt;<br />* src/libs/cplusplus<br />&lt;code&amp;gt;Preprocessor, Snapshot, LookupContext, Document, Macro, FindUsages&amp;lt;/code&amp;gt;
'''Libraries'''
* src/libs/3rdparty/cplusplus - C++ Frontend
<code>Token, Lexer, Parser, Bind, Control, AST, ASTVisitor, Scope, Type, TranslationUnit</code>
* src/libs/cplusplus
<code>Preprocessor, Snapshot, LookupContext, Document, Macro, FindUsages</code>


'''Plugins'''
'''Plugins'''


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


== General hints ==
== General hints ==


* '''Care about the tests''' - See also section about tests below.<br />You can learn a lot from tests. Please run them. Please fix them. Please add new tests for new functionality.
* '''Care about the tests''' - See also section about tests below.
* '''Debugging / Remove optimization before debugging'''<br />src/libs/cplusplus/cplusplus.pro: Comment out &lt;code&amp;gt;QMAKE_CXXFLAGS_DEBUG ''= <s>O2&amp;lt;/code&amp;gt; for proper debugging.<br />* '''Debugging / Enable parser tracing'''<br />src/libs/3rdparty/cplusplus/Parser.cpp: Comment out &lt;code&amp;gt;#define CPLUSPLUS_NO_DEBUG_RULE&amp;lt;/code&amp;gt; to get output like:
You can learn a lot from tests. Please run them. Please fix them. Please add new tests for new functionality.
<br /><code></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 />…</code>
* '''Debugging / Remove optimization before debugging'''
<br />* '''Debugging / Enable preprocessor tracing'''<br />Set the environment variable &lt;code&amp;gt;QTCREATOR_DUMP_FILENAME_WHILE_PARSING&amp;lt;/code&amp;gt; (evaluated at run time) to get output like:<br /><code>…<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</code>
src/libs/cplusplus/cplusplus.pro: Comment out <code>QMAKE_CXXFLAGS_DEBUG ''= -O2</code> for proper debugging.
<br />* '''Debugging / Enable dumping the ModelManager configuration'''<br />Set the environment variable &lt;code&amp;gt;QTCREATOR_DUMP_PROJECT_INFO&amp;lt;/code&amp;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.
* '''Debugging / Enable parser tracing'''
<br />* '''Use the provided tools''' - See also section about tools below.
src/libs/3rdparty/cplusplus/Parser.cpp: Comment out <code>#define CPLUSPLUS_NO_DEBUG_RULE</code> to get output like:


<br />h2. Tools used for development
<code>- parseTranslationUnit, ahead: '' (1) - block-errors: 0
<br />There is a number of tools you can and should make use of. They should work on all major platforms (GNU/Linux, Mac, Windows).
- parseTranslationUnit, ahead: 'typedef' (1) - block-errors: 0
<br />* cplusplus-mkvisitor - Creates an AST visitor class.<br />* cplusplus-update-frontend - Updates sources in the parser after modifying AST.h.<br />* cplusplus-ast2png - Visualizes AST and symbol hierarchy by generating PNG files for given code.<br />* cplusplus-frontend - Parses given files (parser as standalone tool).
— parseDeclaration, ahead: 'typedef' (1) - block-errors: 0
<br />h3. cplusplus-mkvisitor
— parseSimpleDeclaration, ahead: 'typedef' (1) - block-errors: 0
<br />Source directory: src/tools/cplusplus-mkvisitor
—- parseBuiltinTypeSpecifier, ahead: 'long' (2) - block-errors: 0
<br />Usage &amp; Purpose:<br /><code>$ ./cplusplus-mkvisitor -h<br />Usage: cplusplus-mkvisitor [-v] [path to AST.h]
- parseBuiltinTypeSpecifier, ahead: 'int' (3) - block-errors: 0
<br />Print a visitor class based on AST.h to stdout.
</code>
<br />Default path: $QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h.<br /></code>
<br />Example Usage:<br /><code>$ ./cplusplus-mkvisitor &gt; myvisitor.cpp<code>
<br />h3. cplusplus-update-frontend
<br />Source directory: src/tools/cplusplus-update-frontend
<br />Usage &amp; Purpose:<br /></code>$ ./cplusplus-update-frontend -h<br />Usage: cplusplus-update-frontend<br /> cplusplus-update-frontend &lt;frontend-dir&amp;gt; &lt;dumpers-file&amp;gt;
<br />Generate appropriate header and source files of the C''+ frontend accordingly<br />to AST.h and print the paths of the written files. Run this tool after<br />modifying AST.h.


Default values:<br /> frontend-dir: $QTC_SOURCE/src/libs/3rdparty/cplusplus<br /> dumpers-file: $QTC_SOURCE/tests/tools/cplusplus-ast2png/dumpers.inc<code>
* '''Debugging / Enable preprocessor tracing'''
Set the environment variable <code>QTCREATOR_DUMP_FILENAME_WHILE_PARSING</code> (evaluated at run time) to get output like:
<code>…
Parsing file: "/usr/include/stdlib.h"
Parsing file: "/usr/include/c/4.6/new"
Parsing file: "/usr/lib/gcc/x86_64-linux-gn</code>


Example Usage:<br /></code>$ ./cplusplus-update-frontend<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTVisit.cpp<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatch0.cpp<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatcher.cpp<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTClone.cpp<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.cpp<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTVisitor.h<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTMatcher.h<br />$QTC_SOURCE/tests/tools/cplusplus-ast2png/dumpers.inc<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTfwd.h<br />$QTC_SOURCE/src/libs/3rdparty/cplusplus/ASTPatternBuilder.h<code>
* '''Debugging / Enable dumping the ModelManager configuration'''
Set the environment variable <code>QTCREATOR_DUMP_PROJECT_INFO</code> (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.
 
 
== 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).
 
=== cplusplus-mkvisitor ===
Source directory: src/tools/cplusplus-mkvisitor
 
Usage & Purpose:
<code>$ ./cplusplus-mkvisitor -h
Usage: cplusplus-mkvisitor [-v] [path to AST.h]
 
Print a visitor class based on AST.h to stdout.
 
Default path: $QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h.
</code>
 
Example Usage:
<code>$ ./cplusplus-mkvisitor > myvisitor.cpp<code>
 
=== cplusplus-update-frontend ===
Source directory: src/tools/cplusplus-update-frontend
 
Usage & Purpose:
</code>$ ./cplusplus-update-frontend -h
Usage: cplusplus-update-frontend
cplusplus-update-frontend <frontend-dir> <dumpers-file>
 
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<code>
 
Example Usage:
</code>$ ./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<code>


=== cplusplus-ast2png ===
=== cplusplus-ast2png ===
Line 55: Line 117:
Source directory: tests/tools/cplusplus-ast2png
Source directory: tests/tools/cplusplus-ast2png


Usage &amp; Purpose:<br /></code>$ ./cplusplus-ast2png -h<br />Usage: cplusplus-ast2png [-v] [-p ast] &lt;file1&amp;gt; &lt;file2&amp;gt;
Usage & Purpose:
</code>$ ./cplusplus-ast2png -h
Usage: cplusplus-ast2png [-v] [-p ast] <file1> <file2>


Visualize AST and symbol hierarchy of given C++ files by generating png image files<br />in the same directory as the input files. Print paths to generated image files.
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:<br /> -v Run with increased verbosity.<br /> <s>p &lt;ast&amp;gt; Parse each file as &lt;ast&amp;gt;. &lt;ast&amp;gt; is one of:<br /></s> 'declarator' or 'dr'<br /> - 'expression' or 'ex'<br /> - 'declaration' or 'dn'<br /> - 'statement' or 'st'<br /> - 'translationunit' or 'tr'<br /> If this option is not provided, each file is tried to be parsed as<br /> declarator, expression, etc. using the stated order.
Options:
-v Run with increased verbosity.
-p <ast> Parse each file as <ast>. <ast> 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 &quot;_stdincontents.cpp&amp;quot;<br />and are created in the current working directory. To show the AST for simple snippets<br />you might want to execute:
Standard input is also read. The resulting files start with "_stdincontents.cpp"
and are created in the current working directory. To show the AST for simple snippets
you might want to execute:


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


Prerequisites:<br /> 1) Make sure to have 'dot' from graphviz locatable by PATH.<br /> 2) Make sure to have an up to date dumpers file by using 'cplusplus-update-frontend'.<code>
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'.<code>


&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.
<code>dot</code> comes with [http://www.graphviz.org/ graphviz]. 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.
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 /></code>$ echo &quot;int foo() {}&quot; | ./cplusplus-ast2png<br />_stdincontents.cpp.ast.png<br />_stdincontents.cpp.symbols.png<br />$ ./cplusplus-ast2png /tmp/helloworld.cpp<br />/tmp/helloworld.cpp.ast.png<br />/tmp/helloworld.cpp.symbols.png<code>
Example Usage:
</code>$ 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<code>


=== cplusplus-frontend ===
=== cplusplus-frontend ===
Line 77: Line 161:
Source directory: tests/manual/cplusplus-frontend
Source directory: tests/manual/cplusplus-frontend


Usage &amp; Purpose:<br /></code>$ ./cplusplus-frontend -h<br />Usage: cplusplus-frontend [-v] &lt;file1&amp;gt; &lt;file2&amp;gt;
Usage & Purpose:
</code>$ ./cplusplus-frontend -h
Usage: cplusplus-frontend [-v] <file1> <file2>


Run the parser with the given files.<code>
Run the parser with the given files.<code>


Example Usage:<br /></code>$ ./cplusplus-frontend tests/t1.cpp<br />tests/t1.cpp:37: error: expected a declaration<br /> typedef enum { k };<br /> ^<code>
Example Usage:
</code>$ ./cplusplus-frontend tests/t1.cpp
tests/t1.cpp:37: error: expected a declaration
typedef enum { k };
^<code>


== Tests ==
== Tests ==


=== &quot;Plugin&amp;quot; auto tests ===
=== "Plugin" 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.
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 &quot;-test&amp;quot; option.
Make sure that WITH_TESTS is defined (currently this is set for a debug build). Otherwise Qt Creator will not understand the "-test" option.


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


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


=== &quot;External&amp;quot; auto tests ===
=== "External" auto tests ===


You can find these tests in tests/auto/cplusplus. They are the 'separable' tests.
You can find these tests in tests/auto/cplusplus. They are the 'separable' tests.
Line 101: Line 191:
Just build and run the relevant test project.
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, &lt;test project&amp;gt;, Dependencies).
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>, Dependencies).


==== Running all tests ====
==== Running all tests ====


# (Open &amp;) Build the project tests/auto/cplusplus.pro either in-source or as shadow-build.
# (Open &) Build the project tests/auto/cplusplus.pro either in-source or as shadow-build.
# Run &lt;code&amp;gt;$ make check&amp;lt;/code&amp;gt; to run all tests. If a test fails, subsequent tests will not be executed, therefore you might want to add &quot;-i&amp;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.
# Run <code>$ make check</code> to run all tests. If a test fails, subsequent tests will not be executed, therefore you might want to add "-i" 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 ===
=== Manual tests ===

Latest revision as of 14:21, 24 May 2015


CAUTION: This page is no yet reviewed by the C++ Support maintainer.

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

Please read the mailing list post Editors/Code-models: new year's resolutions .

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
Token, Lexer, Parser, Bind, Control, AST, ASTVisitor, Scope, Type, TranslationUnit
  • src/libs/cplusplus
Preprocessor, Snapshot, LookupContext, Document, Macro, FindUsages

Plugins

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

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

QMAKE_CXXFLAGS_DEBUG ''= -O2

for proper debugging.

  • Debugging / Enable parser tracing

src/libs/3rdparty/cplusplus/Parser.cpp: Comment out

#define CPLUSPLUS_NO_DEBUG_RULE

to get output like:

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

  • Debugging / Enable preprocessor tracing

Set the environment variable

QTCREATOR_DUMP_FILENAME_WHILE_PARSING

(evaluated at run time) to get output like:


Parsing file: "/usr/include/stdlib.h"
Parsing file: "/usr/include/c/4.6/new"
Parsing file: "/usr/lib/gcc/x86_64-linux-gn
  • Debugging / Enable dumping the ModelManager configuration

Set the environment variable

QTCREATOR_DUMP_PROJECT_INFO

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


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

cplusplus-mkvisitor

Source directory: src/tools/cplusplus-mkvisitor

Usage & Purpose:

$ ./cplusplus-mkvisitor -h
Usage: cplusplus-mkvisitor [-v] [path to AST.h]

Print a visitor class based on AST.h to stdout.

Default path: $QTC_SOURCE/src/libs/3rdparty/cplusplus/AST.h.

Example Usage:

$ ./cplusplus-mkvisitor > myvisitor.cpp<code>

=== cplusplus-update-frontend ===
Source directory: src/tools/cplusplus-update-frontend

Usage & Purpose:

$ ./cplusplus-update-frontend -h

Usage: cplusplus-update-frontend

cplusplus-update-frontend <frontend-dir> <dumpers-file>

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:

$ ./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 & Purpose:

$ ./cplusplus-ast2png -h

Usage: cplusplus-ast2png [-v] [-p ast] <file1> <file2> …

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> Parse each file as <ast>. <ast> 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" 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'.

<code>dot

comes with graphviz. 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: $ 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 & Purpose:

$ ./cplusplus-frontend -h

Usage: cplusplus-frontend [-v] <file1> <file2> …

Run the parser with the given files.

Example Usage:

$ ./cplusplus-frontend tests/t1.cpp

tests/t1.cpp:37: error: expected a declaration

typedef enum { k };

^

== Tests ==

=== "Plugin" 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" option.

Run the tests with: <code>$ ./qtcreator -test CppTools -test CppEditor

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

"External" 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>, Dependencies).

Running all tests

  1. (Open &) Build the project tests/auto/cplusplus.pro either in-source or as shadow-build.
  2. Run
    $ make check
    
    to run all tests. If a test fails, subsequent tests will not be executed, therefore you might want to add "-i" 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