Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QtCreator + clang warning about operator << precedence #191

Closed
FSund opened this issue Mar 6, 2019 · 6 comments
Closed

QtCreator + clang warning about operator << precedence #191

FSund opened this issue Mar 6, 2019 · 6 comments

Comments

@FSund
Copy link

FSund commented Mar 6, 2019

The static code analyzer (clang) in QtCreator is giving warning about operator precedence

  • "warning: overloaded operator << has higher precendence than comparison operator" -Woverloaded-shift-op-parentheses

From reading #156 I think these warnings are supposed to be suppressed, but it seems like this method is not working in QtCreator, leading to a lot of warnings. Any idea how to get this working in QtCreator?

I can suppress them on a global level using clang settings, but I'd rather not, if possible.

Extra information

  • doctest version: 2.2.3
  • Operating System: Windows 10
  • QtCreator 4.8.1
  • Using QtCreator and GCC from MSYS2
@onqtam
Copy link
Member

onqtam commented Mar 6, 2019

So only the static analysis is giving such problems? Is the clang coming with QtCreator or is it installed along with GCC in MSYS2?

What is the version of clang?

Is the issue reproducible outside of QtCreator - perhaps on the command line with a direct call to the compiler?

Indeed these warnings are supposed to be suppressed - I remember GCC bugs used to exist with suppressing warnings with such pragmas but I think that's not the case for the latest versions of GCC - and in your case this seems to be a clang warning...

@FSund
Copy link
Author

FSund commented Mar 6, 2019

They only use it for static code analysis in the IDE, I use cmake/gcc for compiling.

I am not sure which clang exe/lib they are using (I can only find a reference to C:\msys64\mingw64\lib\qtcreator\plugins\ClangCodeModel4.dll on my machine), but the following is listed as an optional dependency for mingw-w64-x86_64-qt-creator

  • mingw-w64-x86_64-clang: for clang code model [installed]

so I think they use the system clang somehow.

I have

$ clang --version
clang version 7.0.1 (tags/RELEASE_701/final)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:\msys64\mingw64\bin

I have never used clang from the command line, but I will try to make a simple example and see what happens.

@FSund
Copy link
Author

FSund commented Mar 7, 2019

I managed to run clang using the same options as QtCreator uses, and got no warnings there.

Here is a minimal example

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }

TEST_CASE("testing the factorial function") {
    CHECK(factorial(1) == 1);
    CHECK(factorial(2) == 2);
    CHECK(factorial(3) == 6);
    CHECK(factorial(10) == 3628800);
}

This is how it looks in QtCreator

image

Here is the output from running clang from the command line, using the same options as QtCreator

$ clang main.cpp -fsyntax-only -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-unused-macros -Wno-newline-eof -Wno-exit-time-destructors -Wno-global-constructors -Wno-gnu-zero-variadic-macro-arguments -Wno-documentation -Wno-shadow -Wno-switch-enum -Wno-missing-prototypes -Wno-used-but-marked-unused
In file included from main.cpp:2:
./doctest.h:3314:10: warning: non-portable path to file '<windows.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
#include <Windows.h>
         ^~~~~~~~~~~
         <windows.h>
1 warning generated.

The default QtCreator options for clang static analysis are found under Tools -> Options -> C++ -> Code Model -> Manage

image

It seems like the doctest header is being parsed in the context of GCC and not clang, making the DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH function not working as intended, and thus the -Woverloaded-shift-op-parentheses is not added correctly.

This is what the doctest header looks like in the editor. Greyed out code is excluded by the preprocessor. Although this might just be what QtCreator displays, there might be a separate "clang parsing" version of the file.

image

Perhaps this issue belongs with QtCreator, and not doctest?

@onqtam
Copy link
Member

onqtam commented Mar 13, 2019

Sorry for the late response...

Could you try inserting DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses") right next to DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses") and also DOCTEST_GCC_SUPPRESS_WARNING_POP next to DOCTEST_CLANG_SUPPRESS_WARNING_POP and tell me if that works?

Even if it works though I'm not sure I would add it to the macros in doctest - there would be too much pragmas unnecessary for most users and perhaps there would be a warning for an unrecognized pragma, in which case the -Wunknown-pragmas warning might have to be handled as well.

So if it works you might either re-define DOCTEST_ASSERT_IMPLEMENT_2 locally (by first #undef-ing it and then #define-ing it) or you might just globally call once DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses").

If it doesn't work - that might be because the analyzer identifies itself as GCC (so __GNUC__ is defined) but doesn't respect pragma GCC ... because it is actually... clang, so you might have to use #pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses".

In any case I see this as an issue with QtCreator. Let me know how you've handled it!

@FSund
Copy link
Author

FSund commented Mar 14, 2019

I fixed the issues I was having by making a new file

// debug.h
#pragma once
#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
#include "doctest.h"

And including that instead of including doctest.h directly. As you guessed I got a lot of warnings about unknown pragmas, so I hadded the following to my CMakeLists

target_compile_options(test_runner PRIVATE "-Wno-unknown-pragmas")

I will try to file a bug with QtCreator. So you can close this issue if you want.

@onqtam
Copy link
Member

onqtam commented Mar 14, 2019

Fair enough, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants