diff options
author | Nathan Sidwell <nathan@acm.org> | 2020-01-20 05:39:59 -0800 |
---|---|---|
committer | Nathan Sidwell <nathan@acm.org> | 2020-01-20 05:39:59 -0800 |
commit | ad1a3914ae8d67c94b0d2428e3f9672e7db491a1 (patch) | |
tree | 60f0771d802b20be5b8a827738e48324dcb24d2b /libcpp/directives.c | |
parent | e82ba180d6641a1e2bad1ac327234fc1cda658ef (diff) | |
download | gcc-ad1a3914ae8d67c94b0d2428e3f9672e7db491a1.zip gcc-ad1a3914ae8d67c94b0d2428e3f9672e7db491a1.tar.gz gcc-ad1a3914ae8d67c94b0d2428e3f9672e7db491a1.tar.bz2 |
[PR 80005] Fix __has_include
__has_include is funky in that it is macro-like from the POV of #ifdef and
friends, but lexes its parenthesize argument #include-like. We were
failing the second part of that, because we used a forwarding macro to an
internal name, and hence always lexed the argument in macro-parameter
context. We componded that by not setting the right flag when lexing, so
it didn't even know. Mostly users got lucky.
This reimplements the handline.
1) Remove the forwarding, but declare object-like macros that
expand to themselves. This satisfies the #ifdef requirement
2) Correctly set angled_brackets when lexing the parameter. This tells
the lexer (a) <...> is a header name and (b) "..." is too (not a string).
3) Remove the in__has_include lexer state, just tell find_file that that's
what's happenning, so it doesn't emit an error.
We lose the (undocumented) ability to #undef __has_include. That may well
have been an accident of implementation. There are no tests for it.
We gain __has_include behaviour for all users of the preprocessors -- not
just the C-family ones that defined a forwarding macro.
libcpp/
PR preprocessor/80005
* include/cpplib.h (BT_HAS_ATTRIBUTE): Fix comment.
* internal.h (struct lexer_state): Delete in__has_include field.
(struct spec_nodes): Rename n__has_include{,_next}__ fields.
(_cpp_defined_macro_p): New.
(_cpp_find_file): Add has_include parm.
* directives.c (lex_macro_node): Combine defined,
__has_inline{,_next} checking.
(do_ifdef, do_ifndef): Use _cpp_defined_macro_p.
(_cpp_init_directives): Refactor.
* expr.c (parse_defined): Use _cpp_defined_macro_p.
(eval_token): Adjust parse_has_include calls.
(parse_has_include): Add OP parameter. Reimplement.
* files.c (_cpp_find_file): Add HAS_INCLUDE parm. Use it to
inhibit error message.
(_cpp_stack_include): Adjust _cpp_find_file call.
(_cpp_fake_include, _cpp_compare_file_date): Likewise.
(open_file_failed): Remove in__has_include check.
(_cpp_has_header): Adjust _cpp_find_file call.
* identifiers.c (_cpp_init_hashtable): Don't init
__has_include{,_next} here ...
* init.c (cpp_init_builtins): ... init them here. Define as
macros.
(cpp_read_main_file): Adjust _cpp_find_file call.
* pch.c (cpp_read_state): Adjust __has_include{,_next} access.
* traditional.c (_cpp_scan_out_locgical_line): Likewise.
gcc/c-family/
PR preprocessor/80005
* c-cppbuiltins.c (c_cpp_builtins): Don't define __has_include{,_next}.
gcc/testsuite/
PR preprocessor/80005
* g++.dg/cpp1y/feat-cxx14.C: Adjust.
* g++.dg/cpp1z/feat-cxx17.C: Adjust.
* g++.dg/cpp2a/feat-cxx2a.C: Adjust.
* g++.dg/cpp/pr80005.C: New.
Diffstat (limited to 'libcpp/directives.c')
-rw-r--r-- | libcpp/directives.c | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/libcpp/directives.c b/libcpp/directives.c index 983206a..10735c8 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -595,14 +595,13 @@ lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) { cpp_hashnode *node = token->val.node.node; - if (is_def_or_undef && node == pfile->spec_nodes.n_defined) + if (is_def_or_undef + && (node == pfile->spec_nodes.n_defined + || node == pfile->spec_nodes.n__has_include + || node == pfile->spec_nodes.n__has_include_next)) cpp_error (pfile, CPP_DL_ERROR, - "\"defined\" cannot be used as a macro name"); - else if (is_def_or_undef - && (node == pfile->spec_nodes.n__has_include__ - || node == pfile->spec_nodes.n__has_include_next__)) - cpp_error (pfile, CPP_DL_ERROR, - "\"__has_include__\" cannot be used as a macro name"); + "\"%s\" cannot be used as a macro name", + NODE_NAME (node)); else if (! (node->flags & NODE_POISONED)) return node; } @@ -1966,11 +1965,7 @@ do_ifdef (cpp_reader *pfile) if (node) { - /* Do not treat conditional macros as being defined. This is due to - the powerpc port using conditional macros for 'vector', 'bool', - and 'pixel' to act as conditional keywords. This messes up tests - like #ifndef bool. */ - skip = !cpp_macro_p (node) || (node->flags & NODE_CONDITIONAL); + skip = !_cpp_defined_macro_p (node); _cpp_mark_macro_used (node); _cpp_maybe_notify_macro_use (pfile, node); if (pfile->cb.used) @@ -1999,8 +1994,7 @@ do_ifndef (cpp_reader *pfile) the powerpc port using conditional macros for 'vector', 'bool', and 'pixel' to act as conditional keywords. This messes up tests like #ifndef bool. */ - skip = (cpp_macro_p (node) - && !(node->flags & NODE_CONDITIONAL)); + skip = _cpp_defined_macro_p (node); _cpp_mark_macro_used (node); _cpp_maybe_notify_macro_use (pfile, node); if (pfile->cb.used) @@ -2638,12 +2632,9 @@ _cpp_pop_buffer (cpp_reader *pfile) void _cpp_init_directives (cpp_reader *pfile) { - unsigned int i; - cpp_hashnode *node; - - for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) + for (int i = 0; i < N_DIRECTIVES; i++) { - node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); + cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); node->is_directive = 1; node->directive_index = i; } |