#error directive

Jonathan Wakely jwakely.gcc@gmail.com
Wed Mar 6 15:45:00 GMT 2019


On Wed, 6 Mar 2019 at 15:37, David Aldrich <david.aldrich.ntml@gmail.com> wrote:
>
> >  The error comes from the #error directive, so that's the location shown.
>
> Ok, thanks. So I take it that this is expected behaviour.

Yes, I would be be surprised by any other behaviour.

If it showed the condition, what would you want the location to be for:

#if __cplusplus >= 201703L
#include <optional>
using std::optional;
#elif __has_include(<experimental/optional>)
#include <experimental/optional>
using std::experimental::optional;
#else
#error No optional type available
#endif

It would not be helpful to show:

e.C:7:1: error: #error No optional type available
#else
^

And if you're going to show the preceding condition that the #else
corresponds to, which one? The programmer has decided that it should
be an error if *none* of the conditions is true, so picking the
location of a single one doesn't make much sense.

And what about a file that is empty apart from "#error this header
should not be used, use foobar.h instead"? You have to show the #error
location in that case.

Using the location of  the #error as the location of the caret
diagnostic seems like the only consistently useful behaviour.



More information about the Gcc-help mailing list