Macromancy, Part 2

The Challenge

Given a macro that expands to an arbitrary list of headers, include every header in the list, in order.

#define HEADERS "stdio.h", "stdlib.h", "assert.h"
#include "include_dynamic.h"
// stdio.h, stdlib.h, and assert.h are now included and available.

Progress So Far

The C preprocessor is a powerful tool, but not a particularly versatile one. Rather, it does a few specific things, and people have taken advantage of its workings to compose those capabilities to accomplish their tasks. And sometimes, to do something ridiculous and overengineered…like this.

Macromancy

In section 6.10.2, paragraph 8 of the C11 standard, this example is specifically called out as legal (if implementation-defined):

#if VERSION == 1
  #define INCFILE "vers1.h"
#elif VERSION == 2
  #define INCFILE "vers2.h" // and so on
#else
  #define INCFILE "versN.h"
#endif
#include INCFILE

Yikes! Do people really want to do this? If I encountered this in a real program, I’d try to get rid of it—in most cases I figure you can just put the #includes inside the #if and be done with it. You can’t even do any clever tricks like building up a path, at least not portably.

So of course, after telling someone else about this, my next reaction was how to take it further. (Please do not put this in a real program.)

Pronoun Buttons

Disclaimer: This is not a technical article. If it’s not your thing, please feel free to skip it.

If you saw me in the labs at WWDC, you might have noticed I was wearing one of these buttons:

Older Posts

  1. 2016-05-29 Webmailer's Update Bar
  2. 2016-05-23 So You Want to Be a (Compiler) Wizard
  3. 2015-12-29 Re: Contempt Culture
  4. 2015-11-18 Recommendations
  5. 2015-05-09 Nibblesort: Adventures in Optimization
  6. 2015-02-22 AlterConf SF/Oakland
  7. 2015-01-24 “Skip the FFI”
  8. 2012-12-08 How to Write a Checker in 24 Hours
  9. 2012-10-02 Git Tricks
  10. 2012-05-16 Big News
  11. 2012-04-26 Keyboard Adventures
  12. 2012-04-10 Mail Aliases
  13. 2011-08-14 Header Anchors: A Safari Extension
  14. 2011-08-10 "Little Big Details"
  15. 2011-07-29 Weak Linking
  16. 2011-07-25 Using Clang from SVN in Xcode
  17. 2011-07-22 rm vs. Time Machine
  18. 2011-07-14 Priorities
  19. 2011-06-30 Quick Look in TextMate
  20. 2011-06-27 git add
  21. 2011-06-20 Automatic Reference Counting
  22. 2011-06-16 Dealing with "Sandwich Code"
  23. 2011-06-05 gdba
  24. 2011-06-03 Chrome vs. Safari
  25. 2011-05-31 User-Side Troubleshooting
  26. 2011-05-30 Auspicious Continuation
  27. 2009-07-22 Scripting Bridge
  28. 2009-07-08 What Happened to Dockyard?
  29. 2009-06-26 Garbage Collectors and Stack Drawers
  30. 2009-06-21 Z shell
  31. 2009-05-22 “Several New Features”
  32. 2009-05-06 C++ Templates are Turing-Complete
  33. 2009-04-21 [Meme] How Many HTML Elements Can You Name in 5 Minutes?
  34. 2009-04-16 Safer Plugin Categories
  35. 2009-04-02 Hacking Safari 4...for Great Convenience
  36. 2009-03-27 Const Correctness
  37. 2009-03-19 Categories and +load
  38. 2009-03-15 Subversion Checksum Problems
  39. 2009-03-06 JavaScript Tetris
  40. 2008-09-04 Objective-J and Objective-C
  41. 2008-08-28 HTTPS and Name-based Virtual Hosting
  42. 2008-03-08 Alerts Without Apps (or nibs)
  43. 2008-01-14 NSNumber, CFNumber, and CFBoolean
  44. 2007-12-13 GenericToolbar and IB3
  45. 2007-10-27 Performance Optimization: Why We Can't Use valueForKeyPath:
  46. 2007-09-01 Short Xcode Tip: Plugins
  47. 2007-07-15 Port 25 Blocked?
  48. 2007-06-27 GenericToolbar Icon
  49. 2007-04-14 The Symbolism of Pretty URLS
  50. 2007-03-07 Flexible PHP
  51. 2007-01-12 Inauspicious Beginnings