From 42fd12b1e7fc50183bcf62861aae81301a9efbcf Mon Sep 17 00:00:00 2001 From: Edward Smith-Rowland <3dw4rd@verizon.net> Date: Mon, 10 Nov 2014 15:53:12 +0000 Subject: cpplib.h (cpp_callbacks): Add has_attribute. libcpp: 2014-11-10 Edward Smith-Rowland <3dw4rd@verizon.net> * include/cpplib.h (cpp_callbacks): Add has_attribute. * internal.h (lexer_state): Add in__has_attribute__. * directives.c (lex_macro_node): Prevent use of __has_attribute__ as a macro. * expr.c (parse_has_attribute): New function; (eval_token): Look for __has_attribute__ and route to parse_has_attribute. * identifiers.c (_cpp_init_hashtable): Initialize n__has_attribute__. * pch.c (cpp_read_state): Initialize n__has_attribute__. * traditional.c (enum ls): Add ls_has_attribute, ls_has_attribute_close; (_cpp_scan_out_logical_line): Attend to __has_attribute__. gcc/c-family: 2014-11-10 Edward Smith-Rowland <3dw4rd@verizon.net> * c-cppbuiltin.c (__has_attribute, __has_cpp_attribute): New macros; (__cpp_rtti, __cpp_exceptions): New macros for C++98; (__cpp_range_based_for, __cpp_initializer_lists, __cpp_delegating_constructors, __cpp_nsdmi, __cpp_inheriting_constructors, __cpp_ref_qualifiers): New macros for C++11; (__cpp_attribute_deprecated): Remove in favor of __has_cpp_attribute. * c-lex.c (cb_has_attribute): New callback CPP function; (init_c_lex): Set has_attribute callback. gcc/testsuite: 2014-11-10 Edward Smith-Rowland <3dw4rd@verizon.net> * g++.dg/cpp1y/feat-cxx11.C: Test new feature macros for C++98 and C++11; Test existence of __has_cpp_attribute; Test C++11 attributes. * g++.dg/cpp1y/feat-cxx11-neg.C: Ditto. * g++.dg/cpp1y/feat-cxx14.C: Ditto and test for C++14 attributes. * g++.dg/cpp1y/feat-cxx98.C: Test new feature macros for C++98. * g++.dg/cpp1y/feat-cxx98-neg.C: Ditto. * g++.dg/cpp1y/feat-neg.C: Test that __cpp_rtti, _cpp_exceptions will be undefined for -fno-rtti -fno-exceptions. From-SVN: r217292 --- gcc/c-family/c-lex.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'gcc/c-family/c-lex.c') diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c index d2c231c..357d137 100644 --- a/gcc/c-family/c-lex.c +++ b/gcc/c-family/c-lex.c @@ -37,6 +37,8 @@ along with GCC; see the file COPYING3. If not see #include "target.h" #include "wide-int.h" +#include "attribs.h" + /* We may keep statistics about how long which files took to compile. */ static int header_time, body_time; static splay_tree file_info_tree; @@ -62,6 +64,7 @@ static void cb_ident (cpp_reader *, unsigned int, const cpp_string *); static void cb_def_pragma (cpp_reader *, unsigned int); static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *); static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *); +static int cb_has_attribute (cpp_reader *); void init_c_lex (void) @@ -86,6 +89,7 @@ init_c_lex (void) cb->def_pragma = cb_def_pragma; cb->valid_pch = c_common_valid_pch; cb->read_pch = c_common_read_pch; + cb->has_attribute = cb_has_attribute; /* Set the debug callbacks if we can use them. */ if ((debug_info_level == DINFO_LEVEL_VERBOSE @@ -283,6 +287,75 @@ cb_undef (cpp_reader * ARG_UNUSED (pfile), source_location loc, (*debug_hooks->undef) (SOURCE_LINE (map, loc), (const char *) NODE_NAME (node)); } + +/* Callback for has_attribute. */ +static int +cb_has_attribute (cpp_reader *pfile) +{ + int result = 0; + bool paren = false; + tree attr_ns = NULL_TREE, attr_id = NULL_TREE, attr_name = NULL_TREE; + const cpp_token *token; + + token = cpp_get_token (pfile); + if (token->type == CPP_OPEN_PAREN) + { + paren = true; + token = cpp_get_token (pfile); + } + + if (token->type == CPP_NAME) + { + //node = token->val.node.node; + const cpp_token *nxt_token = cpp_peek_token (pfile, 0); + if (c_dialect_cxx() && nxt_token->type == CPP_SCOPE) + { + nxt_token = cpp_get_token (pfile); // Eat scope. + nxt_token = cpp_get_token (pfile); + if (nxt_token->type == CPP_NAME) + { + attr_ns = get_identifier ( + (const char *) cpp_token_as_text (pfile, token)); + attr_id = get_identifier ( + (const char *) cpp_token_as_text (pfile, nxt_token)); + attr_name = build_tree_list (attr_ns, attr_id); + } + else + cpp_error (pfile, CPP_DL_ERROR, + "attribute identifier required after scope"); + } + else + { + attr_ns = get_identifier ("gnu"); + attr_id = get_identifier ( + (const char *) cpp_token_as_text (pfile, token)); + attr_name = build_tree_list (attr_ns, attr_id); + } + if (attr_name) + { + const struct attribute_spec *attr = lookup_attribute_spec (attr_name); + if (attr) + { + if (is_attribute_p ("noreturn", TREE_VALUE (attr_name))) + result = 200809; + else if (is_attribute_p ("deprecated", TREE_VALUE (attr_name))) + result = 201309; + else + result = 1; + } + } + } + else + cpp_error (pfile, CPP_DL_ERROR, + "operator \"__has_attribute__\" requires an identifier"); + + if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) + cpp_error (pfile, CPP_DL_ERROR, + "missing ')' after \"__has_attribute__\""); + + return result; +} + /* Read a token and return its type. Fill *VALUE with its value, if applicable. Fill *CPP_FLAGS with the token's flags, if it is -- cgit v1.1