aboutsummaryrefslogtreecommitdiff
path: root/libcpp/identifiers.cc
diff options
context:
space:
mode:
authorLewis Hyatt <lhyatt@gmail.com>2023-09-07 17:02:47 -0400
committerLewis Hyatt <lhyatt@gmail.com>2023-10-23 18:35:26 -0400
commitcb05acdcea298b62e7fb00dcc153f5d506d085fe (patch)
tree40413e95b248ee9feb5eeeaf44221fc73c2b05c4 /libcpp/identifiers.cc
parent02aa322c8cfd3f60fa5a3a0eee4340bb644261fe (diff)
downloadgcc-cb05acdcea298b62e7fb00dcc153f5d506d085fe.zip
gcc-cb05acdcea298b62e7fb00dcc153f5d506d085fe.tar.gz
gcc-cb05acdcea298b62e7fb00dcc153f5d506d085fe.tar.bz2
libcpp: Improve the diagnostic for poisoned identifiers [PR36887]
The PR requests an enhancement to the diagnostic issued for the use of a poisoned identifier. Currently, we show the location of the usage, but not the location which requested the poisoning, which would be helpful for the user if the decision to poison an identifier was made externally, such as in a library header. In order to output this information, we need to remember a location_t for each identifier that has been poisoned, and that data needs to be preserved as well in a PCH. One option would be to add a field to struct cpp_hashnode, but there is no convenient place to add it without increasing the size of the struct for all identifiers. Given this facility will be needed rarely, it seemed better to add a second hash map, which is handled PCH-wise the same as the current one in gcc/stringpool.cc. This hash map associates a new struct cpp_hashnode_extra with each identifier that needs one. Currently that struct only contains the new location_t, but it could be extended in the future if there is other ancillary data that may be convenient to put there for other purposes. libcpp/ChangeLog: PR preprocessor/36887 * directives.cc (do_pragma_poison): Store in the extra hash map the location from which an identifier has been poisoned. * lex.cc (identifier_diagnostics_on_lex): When issuing a diagnostic for the use of a poisoned identifier, also add a note indicating the location from which it was poisoned. * identifiers.cc (alloc_node): Convert to template function. (_cpp_init_hashtable): Handle the new extra hash map. (_cpp_destroy_hashtable): Likewise. * include/cpplib.h (struct cpp_hashnode_extra): New struct. (cpp_create_reader): Update prototype to... * init.cc (cpp_create_reader): ...accept an argument for the extra hash table and pass it to _cpp_init_hashtable. * include/symtab.h (ht_lookup): New overload for convenience. * internal.h (struct cpp_reader): Add EXTRA_HASH_TABLE member. (_cpp_init_hashtable): Adjust prototype. gcc/c-family/ChangeLog: PR preprocessor/36887 * c-opts.cc (c_common_init_options): Pass new extra hash map argument to cpp_create_reader(). gcc/ChangeLog: PR preprocessor/36887 * toplev.h (ident_hash_extra): Declare... * stringpool.cc (ident_hash_extra): ...this new global variable. (init_stringpool): Handle ident_hash_extra as well as ident_hash. (ggc_mark_stringpool): Likewise. (ggc_purge_stringpool): Likewise. (struct string_pool_data_extra): New struct. (spd2): New GC root variable. (gt_pch_save_stringpool): Use spd2 to handle ident_hash_extra, analogous to how spd is used to handle ident_hash. (gt_pch_restore_stringpool): Likewise. gcc/testsuite/ChangeLog: PR preprocessor/36887 * c-c++-common/cpp/diagnostic-poison.c: New test. * g++.dg/pch/pr36887.C: New test. * g++.dg/pch/pr36887.Hs: New test.
Diffstat (limited to 'libcpp/identifiers.cc')
-rw-r--r--libcpp/identifiers.cc42
1 files changed, 28 insertions, 14 deletions
diff --git a/libcpp/identifiers.cc b/libcpp/identifiers.cc
index 7eccaa9..10cbbdf 100644
--- a/libcpp/identifiers.cc
+++ b/libcpp/identifiers.cc
@@ -27,24 +27,22 @@ along with this program; see the file COPYING3. If not see
#include "cpplib.h"
#include "internal.h"
-static hashnode alloc_node (cpp_hash_table *);
-
/* Return an identifier node for hashtable.c. Used by cpplib except
when integrated with the C front ends. */
+template<typename Node>
static hashnode
alloc_node (cpp_hash_table *table)
{
- cpp_hashnode *node;
-
- node = XOBNEW (&table->pfile->hash_ob, cpp_hashnode);
- memset (node, 0, sizeof (cpp_hashnode));
+ const auto node = XOBNEW (&table->pfile->hash_ob, Node);
+ memset (node, 0, sizeof (Node));
return HT_NODE (node);
}
/* Set up the identifier hash table. Use TABLE if non-null, otherwise
create our own. */
void
-_cpp_init_hashtable (cpp_reader *pfile, cpp_hash_table *table)
+_cpp_init_hashtable (cpp_reader *pfile, cpp_hash_table *table,
+ cpp_hash_table *extra_table)
{
struct spec_nodes *s;
@@ -52,13 +50,23 @@ _cpp_init_hashtable (cpp_reader *pfile, cpp_hash_table *table)
{
pfile->our_hashtable = 1;
table = ht_create (13); /* 8K (=2^13) entries. */
- table->alloc_node = alloc_node;
+ table->alloc_node = alloc_node<cpp_hashnode>;
+ }
- obstack_specify_allocation (&pfile->hash_ob, 0, 0, xmalloc, free);
+ if (extra_table == NULL)
+ {
+ pfile->our_extra_hashtable = true;
+ extra_table = ht_create (6); /* 64 entries. */
+ extra_table->alloc_node = alloc_node<cpp_hashnode_extra>;
}
+ if (pfile->our_hashtable || pfile->our_extra_hashtable)
+ obstack_specify_allocation (&pfile->hash_ob, 0, 0, xmalloc, free);
+
table->pfile = pfile;
+ extra_table->pfile = pfile;
pfile->hash_table = table;
+ pfile->extra_hash_table = extra_table;
/* Now we can initialize things that use the hash table. */
_cpp_init_directives (pfile);
@@ -80,10 +88,11 @@ void
_cpp_destroy_hashtable (cpp_reader *pfile)
{
if (pfile->our_hashtable)
- {
- ht_destroy (pfile->hash_table);
- obstack_free (&pfile->hash_ob, 0);
- }
+ ht_destroy (pfile->hash_table);
+ if (pfile->our_extra_hashtable)
+ ht_destroy (pfile->extra_hash_table);
+ if (pfile->our_hashtable || pfile->our_extra_hashtable)
+ obstack_free (&pfile->hash_ob, 0);
}
/* Returns the hash entry for the STR of length LEN, creating one
@@ -110,7 +119,12 @@ cpp_defined (cpp_reader *pfile, const unsigned char *str, int len)
/* We don't need a proxy since the hash table's identifier comes first
in cpp_hashnode. However, in case this is ever changed, we have a
static assertion for it. */
-extern char proxy_assertion_broken[offsetof (struct cpp_hashnode, ident) == 0 ? 1 : -1];
+static_assert (offsetof (cpp_hashnode, ident) == 0,
+ "struct cpp_hashnode must have a struct ht_identifier as"
+ " its first member");
+static_assert (offsetof (cpp_hashnode_extra, ident) == 0,
+ "struct cpp_hashnode_extra must have a struct ht_identifier as"
+ " its first member");
/* For all nodes in the hashtable, callback CB with parameters PFILE,
the node, and V. */