Created
May 22, 2019 13:30
-
-
Save johnmcfarlane/61efdd62c91d07d40639aa6179d5c196 to your computer and use it in GitHub Desktop.
What happened when I added preprocessor_fun.h to a modern, tooled-up C++ project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Out of curiosity I took preprocessor_fun.h | |
// https://gist.github.com/aras-p/6224951 | |
// and added it to a project at work to see what happens when random C | |
// preprocessor hacks are applied to a modern C++ project with CI. | |
// Unfortunately, about half were flagged as warnings/errors by the compiler. | |
// Almost all of the rest had no effect because the codebase uses too much | |
// of the Standard Library / just doesn't use those APIs. | |
// The only one which wasn't caught by static tools was __builtin_expect | |
// which showed up as a failed death test. It is used in <gsl/gsl_assert> so | |
// should cause a decent amount of consternation in GSL-dependent projects. | |
// JMcF: Comments below | |
// ... | |
// Just before switching jobs: | |
// Add one of these. | |
// Preferably into the same commit where you do a large merge. | |
// | |
// This started as a tweet with a joke of "C++ pro-tip: #define private public", | |
// and then it quickly escalated into more and more evil suggestions. | |
// I've tried to capture interesting suggestions here. | |
// | |
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_, | |
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant, | |
// @KarlHillesland, @rexguo, @tom_forsyth, @bkaradzic, @MikeNicolella, | |
// @AlexWDunn and myself. | |
// | |
// In case it's not clear: I am not suggesting you *actually* do this! | |
// Easy keyword replacement. Too easy to detect I think! | |
// JMcF: Indeed, Clang 7 rejects all of these immediately | |
#define struct union | |
#define if while | |
#define else | |
#define break | |
#define if(x) | |
#define double float | |
#define volatile // this one is cool | |
// I heard you like math | |
// JMcF: You need to undefined M_PI before redefining it or compilers will complain. | |
#undef M_PI | |
#define M_PI 3.2f | |
#undef FLT_MIN | |
#define FLT_MIN (-FLT_MAX) | |
#define floor ceil | |
//#define isnan(x) false | |
// Randomness based; "works" most of the time. | |
// JMcF: Clang 7 rejects these three because they are keywords. GCC 5.1 rejects the second because it is a redefinition. | |
#define true ((__LINE__&15)!=15) | |
#define true ((rand()&15)!=15) | |
#define if(x) if ((x) && (rand() < RAND_MAX * 0.99)) | |
// String/memory handling, probably can live undetected quite long! | |
// JMcF: Some of these functions take different pointer types (void* vs char*) so cannot be substituted. | |
// JMcF: Also, their use violates C++ Core Guidelines and is flagged by Clang-Tidy. | |
#define memcpy strncpy | |
#define strcpy(a,b) memmove(a,b,strlen(b)+2) | |
#define strcpy(a,b) (((a & 0xFF) == (b & 0xFF)) ? strcpy(a+1,b) : strcpy(a, b)) | |
#define memcpy(d,s,sz) do { for (int i=0;i<sz;i++) { ((char*)d)[i]=((char*)s)[i]; } ((char*)s)[ rand() % sz ] ^= 0xff; } while (0) | |
// JMcF: Again, rejected outright by Clang 7. | |
#define sizeof(x) (sizeof(x)-1) | |
// Let's have some fun with threads & atomics. | |
// JMcF: Would love to this evil translated into C++11 where it would have an effect. | |
#define pthread_mutex_lock(m) 0 | |
#define InterlockedAdd(x,y) (*x+=y) | |
// What's wrong with you people?! | |
#define __dcbt __dcbz // for PowerPC platforms | |
#define __dcbt __dcbf // for PowerPC platforms | |
// JMcF: This is the one definition that had any effect (AFAIK!) and which made it all the way to unit tests before failing. | |
#define __builtin_expect(a,b) b // for gcc | |
#define continue if (HANDLE h = OpenProcess(PROCESS_TERMINATE, false, rand()) ) { TerminateProcess(h, 0); CloseHandle(h); } break | |
// Some for HLSL shaders: | |
#define row_major column_major | |
#define nointerpolation | |
#define branch flatten | |
#define any all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment