Define USE (x) (x) = (x)

In one of the C source code files, I found the following line (macro):

#define USE(x) (x) = (x) 

It is used as follows:

 int method(Obj *context) { USE(context); return 1; } 

After searching it, I found the following description:

// Macro to get rid of some warning compiler

Could you tell me more about this macro?

Thank you for your responses!

+6
c c-preprocessor
source share
4 answers

Some compilers complain when variables are never actually used for anything. eg:

 int main(int argc, char **argv) { return 0; } 

gives:

 Output from llvm C/C++/Fortran front-end (llvm-gcc) /tmp/webcompile/_7618_1.c: In function 'main': /tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argc' /tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argv' 

Basically, I can just get rid of these warnings using your macro:

 #define USE(x) (x) = (x) int main(int argc, char **argv) { USE(argc); /* get rid of warnings */ USE(argv); /* get rid of warnings */ return 0; } 
+13
source share

Compilers provide warnings when a variable is defined / declared, but never used. These include function arguments. Some coding styles always need to specify function arguments, but some of them may not be used in this function. They are reserved for future use. For these cases, you can USE(param) avoid the warning

+8
source share

With gcc, you can use __attribute__((unused)) to suppress the warning.

+5
source share

Most (if not all) of the main compilers will offer warnings if local variables are not used within the function that they are defined. I assume that the macro performs an arbitrary operation on a variable to ensure that no warning is flagged for the variable.

 void func1(void) { int unusedVariable = 0; /* do stuff without using unusedVariable */ } /* warning about not using unusedVariable */ void func2(void) { int unusedVariable = 0; USE(unusedVariable); /* do stuff without using unusedVariable */ } /* no warning is issued */ 
+4
source share

All Articles