Skip to content

Overly-generic defines in utils.h break some compilations #389

Closed
@matthijskooijman

Description

@matthijskooijman

This core contains a few very generic defines in utils.h:

// Concatenate 2 strings
#define CONCAT(s1, s2) (s1 s2)
// Concatenate 2 strings separated by space
#define CONCATS(s1, s2) (s1" " s2)
// Stringification
#define xstr(s) str(s)
#define str(s) #s

Especially the str and xstr defines are problematic, since you might be using those as function or method names, which triggers the preprocessor. I ran into this issue when using the boost library, which has a str method somewhere, but this can be easily reproduced with this sketch:

void str() { }
void setup() { }
void loop() { }

Which gives:

In file included from /home/matthijs/.arduino15/packages/STM32/hardware/stm32/1.4.0/cores/arduino/wiring.h:31:0,
                 from /home/matthijs/.arduino15/packages/STM32/hardware/stm32/1.4.0/cores/arduino/Arduino.h:32,
                 from /tmp/arduino_build_gdfirmware/sketch/sketch_dec13a.ino.cpp:1:
sketch_dec13a:1:10: error: expected unqualified-id before string constant
 void str() { }
          ^
/home/matthijs/.arduino15/packages/STM32/hardware/stm32/1.4.0/cores/arduino/utils.h:13:17: note: in definition of macro 'str'
 #define str(s) #s

Here, it points to the problematic macro, but in my original problem case the second message was not present, making this particularly tricky to diagnose.

As for resolving this:

  • A grep suggests that the str() and xstr() macros are not actually used in the entire core, so I suggest removing them.
  • The CONCAT and CONCATS macros could perhaps be rename as well, but better would be to not expose them from header files. grep shows that these macros are only used in .c files, so simply including utils.h from those .c files, and dropping the include from wiring.h sounds like the best approach to me.

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions