ROSE 0.11.145.192
ROSE_UNUSED.h
1#ifndef ROSE_UNUSED_H
2#define ROSE_UNUSED_H
3
4// Cause a variable to be used so that the compiler doesn't generate unused-parameter, unused-variable, or unused-but-set-variable
5// warnings for that variable. For instance given this code:
6//
7// | 1|int foo(int x) {
8// | 2|#ifdef SOMETHING
9// | 3| return x;
10// | 4|#else
11// | 5| return 0;
12// | 6|#endif
13// | 7|}
14//
15// The compiler will emit an unused-parameter warning for 'x' at line 1. We can't just remove the formal argument name because
16// it actually is used in certain configurations (just not this particular configuration).
17//
18// | 1|int foo(int) {
19// | 2|#ifdef SOMETHING
20// | 3| return x; // error here
21// | 4|#else
22// | 5| return 0;
23// | 6|#endif
24// | 7|}
25//
26//
27// Another way is to conditionally specify the argument name as follows, but this is ugly (pretend that the argument list and body
28// are much larger than shown here, and that we want the parens and braces to still match correctly for the sake of IDEs that use
29// them in as syntax highlighting cues).
30//
31// | 1|int foo(int
32// | 2|#ifdef SOMETHING
33// | 3| x
34// | 4|#endif
35// | 5| ) {
36// | 6|#ifdef SOMETHING
37// | 7| return x; // error here
38// | 8|#else
39// | 9| return 0;
40// |10|#endif
41// |11|}
42//
43// The best way is to use C++11 attributes. Unfortunately this results in a different warning for older GCC compilers that don't
44// understand the attribute:
45//
46// | 1|int foo(int x [[maybe_unused]]) { // possible "attribute directive ignored" warning
47// | 2|#ifdef SOMETHING
48// | 3| return x;
49// | 4|#else
50// | 5| return 0;
51// | 6|#endif
52// | 7|}
53//
54// Therefore, the only choice we have left is to actually use the variable in some way that doesn't result in another warning. Since
55// every compiler is different, and for documentation purposes, we hide the special code in a macro like this:
56//
57// | 1|int foo(int x) {
58// | 2|#ifdef SOMETHING
59// | 3| return x;
60// | 4|#else
61// | 5| ROSE_UNUSED(x);
62// | 6| return 0;
63// | 7|#endif
64// | 8|}
65//
66
67#define ROSE_UNUSED(X) ((void)(X))
68
69// Another possibility if the above doesn't work:
70// #define ROSE_UNUSED(X) (*(volatile typeof(X)*)&(X) = (X))
71
72#endif