== Why is this an issue?

A macro definition should not be redefined without marking that intent specifically by un-defining it first.


=== Noncompliant code example

[source,cpp]
----
#define A 1
#define A 2
----


=== Compliant solution

[source,cpp]
----
#define A 1
#undef A
#define A 2
----


=== Exceptions

If the redefinition has the same value as the original one. This is consistent with most C compilers warnings.

[source,cpp]
----
#define A 1
#define A 1
----


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Remove this redefinition or precede it with "#undef" first.


=== Highlighting

primary macro name

secondary previous definition


'''
== Comments And Links
(visible only on this page)

=== on 12 Oct 2016, 17:42:06 Jean-Christophe Collet wrote:
\[~ann.campbell.2] insisted on removing the exception. Talk to her if you disagree.

=== on 13 Oct 2016, 15:16:48 Alban Auzeill wrote:
By removing the exception we find issue like the following:

https://github.com/GNOME/glib/blob/f924d0b1f7d2b019f1abb56685dcfda74266c608/glib/gdatetime.c#L177

But it's a rare example.


\[~jeanchristophe.collet] I will show you issues in our ruling so we can decide what to do.

=== on 13 Oct 2016, 15:25:09 Jean-Christophe Collet wrote:
That still looks like something that should be pointed out.

Either the 2nd definition is useless or the developer meant to write something else. Either way, that's a code smell to me.

Or did I miss something?

=== on 13 Oct 2016, 16:06:38 Alban Auzeill wrote:
Yes the issue on GNOME looks like an unfortunate copy and past. I hope that the developer intention was not to declare something else.

This finding tipped the balance in favour of exception removal proposed by [~ann.campbell.2].


But [~massimo.paladin] argues than clang, gcc and visual studio only report issues on redefinition of macro with a different values, and ignore when the values are identical.

So [~massimo.paladin] suggest to do like compilers to not generate noise.


Note:

____
The C standard says (ISO/IEC 9899:1999, §6.10.3, 2):


An identifier currently defined as an object-like macro shall not be redefined by another

#define preprocessing directive unless the second definition is an object-like macro

definition and the two replacement lists are identical. Likewise, an identifier currently

defined as a function-like macro shall not be redefined by another #define

preprocessing directive unless the second definition is a function-like macro definition

that has the same number and spelling of parameters, and the two replacement lists are

identical.

____

endif::env-github,rspecator-view[]
