Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid superfluous warnings when compiling Qt code with ccache / clang

Tags:

c++

clang

qt

ccache

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

like image 825
ValarDohaeris Avatar asked Jan 27 '15 09:01

ValarDohaeris


2 Answers

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
like image 191
MichaelCMS Avatar answered Sep 28 '22 11:09

MichaelCMS


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.

like image 31
Cássio Renan Avatar answered Sep 28 '22 11:09

Cássio Renan