This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] cppcoreguidelines-macro-usage: print macro names
ClosedPublic

Authored by lebedev.ri on Oct 29 2018, 7:19 AM.

Details

Summary

The macro may not have location (or more generally, the location may not exist),
e.g. if it originates from compiler's command-line.

The check complains on all the macros, even those without the location info.
Which means, it only says it does not like it. What is 'it'? I have no idea.
If we don't print the name, then there is no way to deal with that situation.

And in general, not printing name here forces the user to try to understand,
given, the macro definition location, what is the macro name?
This isn't fun.

I suspect some more issues may crop up later.

Diff Detail

Repository
rL LLVM

Event Timeline

lebedev.ri created this revision.Oct 29 2018, 7:19 AM

How does this patch change the behaviour for macros without location? They will be diagnosed at the beginning of the TU?

It is probably better to ignore these kind of macros, as there is no way around them (compile time configuration can only be done with macros?!) and therefore warnings for theses macros are false positives. Using constexpr constructs does not help either.

How does this patch change the behaviour for macros without location?

It doesn't.

They will be diagnosed at the beginning of the TU?

It is probably better to ignore these kind of macros, as there is no way around them (compile time configuration can only be done with macros?!) and therefore warnings for theses macros are false positives. Using constexpr constructs does not help either.

I agree that it might be good idea to add an *option* to not diagnose such macros.
But i think it is not the spirit of the check/rule to ignore them completely [by default],
because those macros are still horrible, and can affect all kinds of things,
just like the 'normal' macros.

They will be diagnosed at the beginning of the TU?
It is probably better to ignore these kind of macros, as there is no way around them (compile time configuration can only be done with macros?!) and therefore warnings for theses macros are false positives. Using constexpr constructs does not help either.

I agree that it might be good idea to add an *option* to not diagnose such macros.
But i think it is not the spirit of the check/rule to ignore them completely [by default],
because those macros are still horrible, and can affect all kinds of things,
just like the 'normal' macros.

Yes and no, the CPPCG do acknowledge the fact, that macros are (still)
necessary for some jobs, like compile-time configurations of values and
require that the macros should at least be super visible in the code
(enforceable with the regex option in this check).

Turning off assertions for release builds would be a good example of
necessary macros. I would say this check must take this into account.

As for printing the name in the diagnostic: Not sure if this really adds
value. The location the diagnostic is pointing to is the name of macro
itself. The unclear diag comes from the fact that the macro is not
defined in code, so i think we should find a position on that issue first.

My opinion is, that macros without location (however compiler defined
macros) should not be diagnosed. It is unlikely that complicated
functions or the like a defined via compiler flags. These tend to be
used for integral constants to turn things on/off.

How does this patch change the behaviour for macros without location?

It doesn't.

They will be diagnosed at the beginning of the TU?

It is probably better to ignore these kind of macros, as there is no way around them (compile time configuration can only be done with macros?!) and therefore warnings for theses macros are false positives. Using constexpr constructs does not help either.

I agree that it might be good idea to add an *option* to not diagnose such macros.
But i think it is not the spirit of the check/rule to ignore them completely [by default],
because those macros are still horrible, and can affect all kinds of things,
just like the 'normal' macros.

I'm not certain an option makes sense in practice. Predefined macros and macros that come from the command line are a different beast from macros defined by the developer -- I don't think we should be diagnosing those cases as they're generally outside of programmer control. Predefined macros are entirely outside of the user's control. Macros from the command line are most often going to be controlling build toggles, but there is no reason to assume the user can control those (like 3rd party packages) or that they can be replaced with an alternative like constexpr.

How does this patch change the behaviour for macros without location?

It doesn't.

They will be diagnosed at the beginning of the TU?

It is probably better to ignore these kind of macros, as there is no way around them (compile time configuration can only be done with macros?!) and therefore warnings for theses macros are false positives. Using constexpr constructs does not help either.

I agree that it might be good idea to add an *option* to not diagnose such macros.
But i think it is not the spirit of the check/rule to ignore them completely [by default],
because those macros are still horrible, and can affect all kinds of things,
just like the 'normal' macros.

I'm not certain an option makes sense in practice. Predefined macros and macros that come from the command line are a different beast from macros defined by the developer -- I don't think we should be diagnosing those cases as they're generally outside of programmer control. Predefined macros are entirely outside of the user's control. Macros from the command line are most often going to be controlling build toggles, but there is no reason to assume the user can control those (like 3rd party packages) or that they can be replaced with an alternative like constexpr.

I personally haven't yet seen this check firing on *predefined* macros.
The only problem i have are these command-line-based macros.
They *may* be outside of programmer's control, but if one has access to the buildsystem
(as in, not *just* to the source code to be compiled, but to the build system definitions
that say *how* to build it), then one can easily add such command-line-based macros.

I personally haven't yet seen this check firing on *predefined* macros.

I did specifically check the compiler defined macros I could see in the AST and they were not diagnosed from the beginning.
I would guess, only the compiler-flag macros are passed in the PPCallbacks, which would explain the current behaviour.

The only problem i have are these command-line-based macros.
They *may* be outside of programmer's control, but if one has access to the buildsystem
(as in, not *just* to the source code to be compiled, but to the build system definitions
that say *how* to build it), then one can easily add such command-line-based macros.

It is a burden with little to gain as they the toggle-stuff use-case is not replaceable with other constructs.
Having it as opt-in might be ok, but by default the check should not diagnose them.

I personally haven't yet seen this check firing on *predefined* macros.

I did specifically check the compiler defined macros I could see in the AST and they were not diagnosed from the beginning.
I would guess, only the compiler-flag macros are passed in the PPCallbacks, which would explain the current behaviour.

Thanks for checking; I hadn't had the chance to try it out yet and was making a guess.

The only problem i have are these command-line-based macros.
They *may* be outside of programmer's control, but if one has access to the buildsystem
(as in, not *just* to the source code to be compiled, but to the build system definitions
that say *how* to build it), then one can easily add such command-line-based macros.

It is a burden with little to gain as they the toggle-stuff use-case is not replaceable with other constructs.
Having it as opt-in might be ok, but by default the check should not diagnose them.

+1; I'd be fine with an opt-in option for the behavior.

Add an opt-in option to diagnose such command-line-based macros.

JonasToth added inline comments.Oct 30 2018, 4:11 AM
docs/clang-tidy/checks/cppcoreguidelines-macro-usage.rst
30 ↗(On Diff #171583)

I think the another option name would fit better: how about IgnoreCommandLineMacros or the like?

lebedev.ri marked an inline comment as done.

Better option name.

docs/clang-tidy/checks/cppcoreguidelines-macro-usage.rst
30 ↗(On Diff #171583)

Yep, i didn't like that name anyway :)

JonasToth accepted this revision.Oct 30 2018, 7:24 AM

LGTM, @aaron.ballman do you have more comments?

This revision is now accepted and ready to land.Oct 30 2018, 7:24 AM

LGTM

Thank you for the review!

@aaron.ballman do you have more comments?

If there are no more comments, i'll land this in a few (~3) hours.

This revision was automatically updated to reflect the committed changes.