cgmb / guardonce

Utilities for converting from C/C++ include guards to #pragma once and back again.
MIT License
142 stars 3 forks source link

Handle include guards with values #15

Closed cgmb closed 7 years ago

cgmb commented 7 years ago

checkguard will complain about perfectly valid guards like this:

#ifndef VULKAN_H_
#define VULKAN_H_ 1
...
#endif

The problem with just accepting a value for include guards is that it makes confusing an include guard and a constant declaration more likely. This looks pretty similar:

#ifndef M_PI
#define M_PI 3.14159265358979323
#endif

But, perhaps something could still be done about these cases? If guardonce checked that the #ifndef/#endif covered the whole file (aside from comments and preprocessor directives), it could be reasonably certain the it was dealing with an include guard. That, of course, depends on #7.

Do most people who give a value to their include guards use 1? Perhaps that would be a decent heuristic to use in the meantime? Needs research.

cgmb commented 7 years ago

It seems the confusion with definitions is a problem even without accepting values. Something like this bit of code from Apitrace will be misinterpreted:

#ifndef DECLSPEC_DEPRECATED
#define DECLSPEC_DEPRECATED
#endif

A fairly simple heuristic would be to check the code after the #define to see if there's an #endif immediately. It's not really guarding anything if there's nothing inside it.