QT Creator, syntax checking for c++11 - c++

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

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.

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!

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.

Qt Creator is using the syntax highlighting definitions from the Kate editor (from KDE). See Tools-Options-Text Editor-Generic Highlighter.
If Kate has a definition for c++11, you can use it or you can develop your own.

Using MSVC compiler, you need to add the line:
/std:c++11
To your project's project.cxxflags configuration file, where project must be replaced by the name of your project. This file is located at the root of your project directory, you may have to create it if it does not already exist.
Remark: this setting is a MSVC parameter so the syntax would likely be different for other compilers.
Tested with Qt Creator 4.15.0-rc1 (April 2021) and Visual Studio 2017 on Windows, where the error:
`error: no member named 'optional' in namespace 'std'` when trying to use std::optional from header <optional>
disappear when adding /std:c++17 (std::optional was added in C++17).

Related

MacOS: VSCode C/C++ intellisense fails to deduce types

MacOS Catalina 10.15.7, VSCode 1.64.2 (Universal) :I had the intellisense working for my project without problems, but then for whatever reason it has stopped working in some cases:
whenever I assign something to an 'auto variable', for example: auto val = (float)foo; I'd get intellisense error: int val: explicit type is missing ('int' assumed)C/C++(260).
Class enums are not recognised as they should, so I can't use EnumClass::Enum or get any enum-related autocomplete support.
Those are the most reoccuring problems, but I'd say the intellisense generally doesn't work properly.
I removed everything related to VSCode (using this: How to completely uninstall vscode on mac) and reinstalled with just C/C++ extention enabled and the problem persists. I have other people using the same setup with this project and they don't have this problem. I tried older versions of the extention without success aswell.
Is there anything I could try to get it back to work?
The issue seems to be that intellisense is using older c++ version for determining the syntax.
The way to fix this is to set to some newer version like c++17
Go to settings in your VSCode and search for Cpp Standard and from the dropdown select c++17 or any newer version that you use.
In case you follow JSON style settings, then search for following
"C_Cpp.default.cppStandard": "c++17"
Attaching the screenshot of the settings page

QtCreator and ClangCodeModel plugin

I just installed QtCreator 4.7.2, which comes with ClangCodeModel plugin by default on. After my CMake project was parsed I saw much better highlighting of keywords in the code and also awesome intellisense handling of auto declared variables.
The downside was that the ClangCodeModel plugin was buggy, and gave me errors and warnings in parts of code that didn't make sense, especially the ones in 3rd party library header files. So I was forced to turn it off.
I'm wondering if Clang can be configured in QtCreator, so that minimal checks can happen. I played with these settings, but nothing made a difference:
1) Has anyone been able to configure this plugin in QtCreator so that it can work better? I am mostly concerned about intellisense. I can open the same CMake project in Visual Studio and I have much better intellisense there, but I prefer using Qt Creator.
2) If #1 is not possible because the plugin has bugs, what are the other means of improving intellisense in Qt Creator with CMake projects?
The screenshot you took is from the "Analyzer" settings.
The Analyzer performs on-demand checks like running static analysis, clang-tidy, callgrind, etc.
To configure the Code Model, you should go to "C++ > Code Model":
You also have the possibility to override this settings on a per-project basis:
In both cases you can, by clicking on Manage..., create your own configuration with whatever flags you want:

Qt Creator IDE appears to be falsely marking reinterpret_cast<::GlobalType> as invalid

I am currently in the process of moving a number of class files from our Visual Studio source environment into Qt Creator (version 3.4.2).
I have made several changes to cater for the differences in the compiler (into MinGW). One thing that is puzzling me is that the the IDE is reporting a specific error (red underline) but still compiles fine.
I am pretty sure the issue is isolated to using reinterpret_cast<T> where T is a global namespace.
For example, the following line will show as incorrect in the IDE but will still compile:
::GetWindowThreadProcessId(window, reinterpret_cast<::LPDWORD>(&processId));
The tooltip states:
expected ';' got ':'
If I change the code to remove :: from LPDWORD the syntax highlighting disappears. This will be fine in the IDE:
::GetWindowThreadProcessId(window, reinterpret_cast<LPDWORD>(&processId));
I suspect this is a bug in the IDE. Is this a safe assumption?
I don't plan on changing the code style for using the global namespace.
Thanks to lisyarus's comment, I was able to find that this is in fact a bug in QT Creator. There is already a bug report here.
As I mentioned in the question, the bug doesn't stop you from compiling the code. You can, if you wish, add a space between < and ::.

Netbeans is not highlighting unresolved identifiers

I've been working with Netbeans on Mac OS for the past few months (in C++) but have now moved to Ubuntu 12.04. I'm compiling via command line and just using Netbeans as an editor because I like the code assistance and things like that that it has. Normally it seems people want to disable code assistance and highlighting but in my case, they are not working and I don't know why. All the boxes are checked under the Editor>Highlighting tab. When I deliberately misspell a variable it does not complain. Syntax highlighting works fine. Is there some other thing I need to install?
I apologize is this is a very simple thing but googling and searching docs has not helped since it seems everyone is concerned with the opposite problem.
Does NetBeans use clang for static code analysis? Maybe you need to install it.

NetBeans 6.8 marks keywords as fault

I have just installed NetBeans 6.8. on Ubuntu 10.04 and it marks c++ keywords such as "using" or "namespace" as a fault. I checked that it compiles with g++ and I found an answer to that already here, but there it was a bug in Netbeans which had been fixed via updates within a few days. When I try to update, it tells me, I have the latest version...no surprise...
If I define me a class and want to use it in the main file, it does neither work. The header file of my class is included ;-)
And actually, when I write something like myClass::myMethod(int n){ myClass::Variable +=n;} it does not even accept the definition of n in the round brackets...
Does anybody have an idea?
The latest version is 6.91. You might have to download it again from netbeans.org if you're having issues with the update. Package manager may not have the latest version.
When you say you have errors are they when compiling or is the IDE underlining them in red before you compile? The IDE tries to be helpful but it's not very accurate sometimes.