8

I am having the same problem as this guy. Compiling with clang and ccache I get this warning everytime it encounters a Q_OBJECT:

warning: explicitly assigning value of variable of type 'int' to itself [-Wself-assign]

This only happens when using ccache, compiling the same code with clang alone works fine.

There seems to be a similar issue with macro expansions where the suggested solution is to set the environment variable

CCACHE_CPP2=yes

Unfortunately, this does not seems to fix my issue, or maybe I'm doing it wrong.

I have tried:

  • Building from command line with

    • CCACHE_CPP2=yes ninja

    • export CCACHE_CPP2=yes ninja

  • Building from Qt Creator, adding CCACHE_CPP2 to "Build Environment"

Is there anything else I can do to fix this macro expansion issue? I specifically do not want to disable warnings globally (because that's bad) or locally (because that means wrapping all macros in compiler-specific boilerplate).

ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43

3 Answers3

3

Try adding -Wno-self-assign to the CPP flags . It should allow you to disable self-assign errors :

CXXFLAGS= $(CXXFLAGS) -Wno-self-assign 

or

CPPFLAGS=$(CPPFLAGS) -Wno-self-assign
MichaelCMS
  • 4,703
  • 2
  • 23
  • 29
  • But I would still like to have that warning enabled for other parts of my code... – ValarDohaeris Jan 28 '15 at 09:04
  • I don't know an easy and fast way to approach this . You could change your makefile in order to compile some units with this flag and some units without, but it will be allot of work and you will still have the same problem on units with both Qt code and other code. – MichaelCMS Jan 28 '15 at 09:08
3

Forgive me for not having clang to test this with, but I felt I should help anyway. Expanding on Marek's answer, there's the possibility of placing the pragma inside another macro expansion. It's a very ugly solution, but that way you only need to define the macro once, instead of spawning pragmas all over your code base.

#define WARN int&x = x;

#define NO_WARN _Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wuninitialized\"") \
WARN \
_Pragma("GCC diagnostic pop")

int main(){
  NO_WARN
}

As you can see, I tested it with gcc(I have no means of testing with clang right now), but that should work fine in clang by substituting "GCC" with "clang" inside the macro(and using -Wself_assign). Applying to your problem(pseudocode):

#ifdef clang
#define MY_Q_OBJECT _Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wself-assign\"") \
Q_OBJECT \
_Pragma("clang diagnostic pop")
#else
#define MY_Q_OBJECT Q_OBJECT
#endif
class A{
  MY_Q_OBJECT // Unfortunately you still need to replace Q_OBJECT on your classes
}

Another ugly downside is that, at least on gcc, I had to run the preprocessor twice for it to work. Can't tell if the same is necessary for clang.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
  • As an adendum, note that it's probably not possible to fix this at all without suppressing the warning. It's not a simple configuration problem, and maybe a bug. Unless I'm wrong, the best you can do is file a bug report and suppress the warning, as suggested by Michael and Marek. – Not a real meerkat Feb 06 '15 at 10:37
1

IMO ignoring this warning globally is not a problem. It warns about dummy code, not about potential logic errors caused by typo. That is why I've voted up @MichaelCMS answer.

But there is a way to disable warning only is some section of code:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wself-assign"
Q_OBJECT
#pragma clang diagnostic pop

This should do the trick (if I didn't mess up the flag name), but I don't like it, to much boiler plate macros.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Sorry, I'm not happy with any kind of disabled warnings because a) that does not fix the underlying issue, e.g. what if I later discover that the macro expansion contains another warning and b) I'm trying to write portable code, so adding this kind of pragmas quickly becomes messy, e.g. I'm pretty sure I would need to wrap the clang pragma in an ifdef for msvc to ignore it etc. – ValarDohaeris Feb 03 '15 at 10:29
  • there is switch `-Weverything` which could be used to disable all warnings. – Marek R Feb 03 '15 at 11:49