Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT Creator, syntax checking for c++11

How to turn off error highlighting (red wave under the code) for c++11 cycle range-based operators like that?

int myint[] = {1,2,3,4,5};
for (auto x : myint){/**/}

Hover prompt shows "unexpected token :". The code compiles perfectly.

Another issue - the autocomplete doesn't show unique_ptr in std:: namespace, though compiles OK.

  • Qt Creator 2.4.0 Based on Qt 4.7.4 (32 bit) Built on Dec 12 2011 at 01:10:32
like image 863
Dmitriy Kachko Avatar asked Mar 05 '12 00:03

Dmitriy Kachko


People also ask

How does Qt Creator work?

Qt Creator anticipates what you are going to write and completes code and code snippets for elements, properties, and IDs. Qt Creator indents text and code according to rules that you specify separately for files that contain C++, QML, or Nim (experimental) code and for other text files.

How do I check for errors in Qt Creator?

Checking Code Syntax Qt Creator checks for errors when you write code and displays inline error and warning messages. Similarly, it checks the data structure of an instance of a JavaScript object notation (JSON) entity. In addition, you can run static checks on the QML and JavaScript code in your project to find common problems.

How do I reset the code model in Qt Creator?

You can see the error message when you move the mouse pointer over code that Qt Creator underlines in the code editor or when you open a QML file in the Design mode. To reset the code model, select Tools > QML/JS > Reset Code Model.

How does Qt Creator indent text?

Qt Creator indents text and code according to rules that you specify separately for files that contain C++, QML, or Nim (experimental) code and for other text files. When you edit QML code in the code editor, you specify the properties of QML components. For some properties, such as colors and font names, this is not a trivial task.


3 Answers

I don't understand the line of reasoning in Andrew's answer. Why would customizing syntax checking make sense? The syntax is either correct given the context set by the compiler command line, or it is wrong. If it is wrong, it should be marked as such, if not, not. If correct code is marked as wrong, it is a bug, or at least unsupported feature, in the IDE, and it needs a fix, not "customization".

Having said that, the cited example

    int myint[] = {1,2,3,4,5};
    for (auto x : myint){/**/}

works fine here (fairly recent build from master branch).

Regarding the other comment on Kate: Kate's syntax highlighting is used by Qt Creator as fallback in cases where there is no more specific syntax highlighting available for a file. In the case of C++ (98/03/11) there is a real code model used, not Kate's definitions.

like image 50
tullo Avatar answered Sep 28 '22 06:09

tullo


There is "syntax highlighting" (coloring) and there is "syntax checking" (wavy underlines). The syntax coloring does seem to be related to Kate as @Koying suggests, you can modify the colors and turn it off. As of Qt Creator 2.5.0 it doesn't seem to have a problem with most C++11 examples I paste off the web, although your example does indeed still have the underline:

http://labs.qt.nokia.com/2012/05/09/qt-creator-2-5-0-released/

Also unfortunately, at the time of writing (June 13th 2012, which is about a month after the release) the latest integrated SDK package is still installing Qt Creator 2.4.1 by default. :-/

If this is the boat someone is in and wants to try the latest, don't do what I did by wiping out your Qt SDK install using /opt/QtSDK/SDKMaintenanceTool! That wasted time on a reinstall, after which I executed a sudo rm -r /opt/QtSDK/QtCreator, then told the new Qt Creator release to install to /opt/QtSDK/QtCreator. I'll update this post if I hit a snag with that choice!

http://qt-project.org/wiki/Qt_Creator_Releases

BUT though it seems to work with many C++11 constructs it does nothing for your example nor the particular case I tripped across, which is the unspaced<nested<syntax>> for templates.

(Note: It may seem like a minor thing, but it's a big C++11 feature for me. I hate dealing with having to do tailspaced<nested<syntax> >...which led me to always do fullyspaced< nested< syntax > > on all templates to keep things consistent. Even simple cases like vector< int >. Now that they fixed the compiler, I'm ready to kill all of that whitespace noise.)

There doesn't seem to be any way to turn off the wavy underlines (unless you recompile Qt Creator??).

in qt-creator-2.5.0-src\src\plugins\cpptools\cppmodelmanager.cpp:

        QTextCharFormat errorFormat;
        /* disable error underline
        errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
        errorFormat.setUnderlineColor(Qt::red);
        */

        // set up the format for the warnings.
        QTextCharFormat warningFormat;
        /* disable warning underline
        warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
        warningFormat.setUnderlineColor(Qt::darkYellow);
        */

Either way, it's an insane consequence of duplicating the work of the compiler in the IDE instead of having the two share front-end code. We live in the dark ages of software. Won't someone save us? [/rant]

Here's some of the relevant code (I think):

https://qt.gitorious.org/qt-creator/qt-creator/blobs/master/src/plugins/cppeditor/cpphighlighter.cpp

...and the lexer here, note the member _cxx0xEnabled:

https://qt.gitorious.org/qt-creator/qt-creator/blobs/master/src/libs/cplusplus/SimpleLexer.cpp#line80

Besides your for syntax, the template spacing is the only thing I've found in C++11 that causes the lines. That's major enough to me that I might just build my own QtCreator to address it!

like image 25
HostileFork says dont trust SE Avatar answered Sep 28 '22 04:09

HostileFork says dont trust SE


If you're writing in C++11 you should change from using QT Creator as the IDE to using Eclipse CDT. At least this is how I solved this problem. QT Creator doesn't seem to have any way to customize this real-time syntax checking. Eclipse on the other hand is extensively customizable in this regard.

Update: I think that QtCreator has improved its support for newer versions of C++ since this answer was given, so it is no longer correct. I can't delete it as it is an accepted answer.

like image 30
Andrew Tomazos Avatar answered Sep 28 '22 05:09

Andrew Tomazos