gcc/gcc/plugin.h
Marek Polacek a1ad0d84d7 attribs: Implement -Wno-attributes=vendor::attr [PR101940]
It is desirable for -Wattributes to warn about e.g.

[[deprecate]] void g(); // typo, should warn

However, -Wattributes also warns about vendor-specific attributes
(that's because lookup_scoped_attribute_spec -> find_attribute_namespace
finds nothing), which, with -Werror, causes grief.  We don't want the
-Wattributes warning for

[[company::attr]] void f();

GCC warns because it doesn't know the "company" namespace; it only knows
the "gnu" and "omp" namespaces.  We could entirely disable warning about
attributes in unknown scopes but then the compiler would also miss typos
like

  [[company::attrx]] void f();

or

  [[gmu::warn_used_result]] int write();

so that is not a viable solution.  A workaround is to use a #pragma:

  #pragma GCC diagnostic push
  #pragma GCC diagnostic ignored "-Wattributes"
  [[company::attr]] void f() {}
  #pragma GCC diagnostic pop

but that's a mouthful and awkward to use and could also hide typos.  In
fact, any macro-based solution doesn't seem like a way forward.

This patch implements -Wno-attributes=, which takes these arguments:

company::attr
company::

This option should go well with using @file: the user could have a file
containing
-Wno-attributes=vendor::attr1,vendor::attr2
and then invoke gcc with '@attrs' or similar.

I've also added a new pragma which has the same effect:

The pragma along with the new option should help with various static
analysis tools.

	PR c++/101940

gcc/ChangeLog:

	* attribs.c (struct scoped_attributes): Add a bool member.
	(lookup_scoped_attribute_spec): Forward declare.
	(register_scoped_attributes): New bool parameter, defaulted to
	false.  Use it.
	(handle_ignored_attributes_option): New function.
	(free_attr_data): New function.
	(init_attributes): Call handle_ignored_attributes_option.
	(attr_namespace_ignored_p): New function.
	(decl_attributes): Check attr_namespace_ignored_p before
	warning.
	* attribs.h (free_attr_data): Declare.
	(register_scoped_attributes): Adjust declaration.
	(handle_ignored_attributes_option): Declare.
	(canonicalize_attr_name): New function template.
	(canonicalize_attr_name): Use it.
	* common.opt (Wattributes=): New option with a variable.
	* doc/extend.texi: Document #pragma GCC diagnostic ignored_attributes.
	* doc/invoke.texi: Document -Wno-attributes=.
	* opts.c (common_handle_option) <case OPT_Wattributes_>: Handle.
	* plugin.h (register_scoped_attributes): Adjust declaration.
	* toplev.c (compile_file): Call free_attr_data.

gcc/c-family/ChangeLog:

	* c-pragma.c (handle_pragma_diagnostic): Handle #pragma GCC diagnostic
	ignored_attributes.

gcc/testsuite/ChangeLog:

	* c-c++-common/Wno-attributes-1.c: New test.
	* c-c++-common/Wno-attributes-2.c: New test.
	* c-c++-common/Wno-attributes-3.c: New test.
2021-11-10 09:17:13 -05:00

205 lines
6.3 KiB
C

/* Header file for internal GCC plugin mechanism.
Copyright (C) 2009-2021 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef PLUGIN_H
#define PLUGIN_H
#include "highlev-plugin-common.h"
/* Event names. */
enum plugin_event
{
# define DEFEVENT(NAME) NAME,
# include "plugin.def"
# undef DEFEVENT
PLUGIN_EVENT_FIRST_DYNAMIC
};
/* All globals declared here have C linkage to reduce link compatibility
issues with implementation language choice and mangling. */
#ifdef __cplusplus
extern "C" {
#endif
extern const char **plugin_event_name;
struct plugin_argument
{
char *key; /* key of the argument. */
char *value; /* value is optional and can be NULL. */
};
/* Additional information about the plugin. Used by --help and --version. */
struct plugin_info
{
const char *version;
const char *help;
};
/* Represents the gcc version. Used to avoid using an incompatible plugin. */
struct plugin_gcc_version
{
const char *basever;
const char *datestamp;
const char *devphase;
const char *revision;
const char *configuration_arguments;
};
/* Object that keeps track of the plugin name and its arguments. */
struct plugin_name_args
{
char *base_name; /* Short name of the plugin (filename without
.so suffix). */
const char *full_name; /* Path to the plugin as specified with
-fplugin=. */
int argc; /* Number of arguments specified with
-fplugin-arg-... */
struct plugin_argument *argv; /* Array of ARGC key-value pairs. */
const char *version; /* Version string provided by plugin. */
const char *help; /* Help string provided by plugin. */
};
/* The default version check. Compares every field in VERSION. */
extern bool plugin_default_version_check (struct plugin_gcc_version *,
struct plugin_gcc_version *);
/* Function type for the plugin initialization routine. Each plugin module
should define this as an externally-visible function with name
"plugin_init."
PLUGIN_INFO - plugin invocation information.
VERSION - the plugin_gcc_version symbol of GCC.
Returns 0 if initialization finishes successfully. */
typedef int (*plugin_init_func) (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version);
/* Declaration for "plugin_init" function so that it doesn't need to be
duplicated in every plugin. */
extern int plugin_init (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version);
/* Function type for a plugin callback routine.
GCC_DATA - event-specific data provided by GCC
USER_DATA - plugin-specific data provided by the plugin */
typedef void (*plugin_callback_func) (void *gcc_data, void *user_data);
/* Called from the plugin's initialization code. Register a single callback.
This function can be called multiple times.
PLUGIN_NAME - display name for this plugin
EVENT - which event the callback is for
CALLBACK - the callback to be called at the event
USER_DATA - plugin-provided data.
*/
/* Number of event ids / names registered so far. */
extern int get_event_last (void);
int get_named_event_id (const char *name, enum insert_option insert);
/* This is also called without a callback routine for the
PLUGIN_PASS_MANAGER_SETUP, PLUGIN_INFO and PLUGIN_REGISTER_GGC_ROOTS
pseudo-events, with a specific user_data.
*/
extern void register_callback (const char *plugin_name,
int event,
plugin_callback_func callback,
void *user_data);
extern int unregister_callback (const char *plugin_name, int event);
/* Retrieve the plugin directory name, as returned by the
-fprint-file-name=plugin argument to the gcc program, which is the
-iplugindir program argument to cc1. */
extern const char* default_plugin_dir_name (void);
#ifdef __cplusplus
}
#endif
/* In case the C++ compiler does name mangling for globals, declare
plugin_is_GPL_compatible extern "C" so that a later definition
in a plugin file will have this linkage. */
#ifdef __cplusplus
extern "C" {
#endif
extern int plugin_is_GPL_compatible;
#ifdef __cplusplus
}
#endif
struct attribute_spec;
struct scoped_attributes;
extern void add_new_plugin (const char *);
extern void parse_plugin_arg_opt (const char *);
extern int invoke_plugin_callbacks_full (int, void *);
extern void initialize_plugins (void);
extern bool plugins_active_p (void);
extern void dump_active_plugins (FILE *);
extern void debug_active_plugins (void);
extern void warn_if_plugins (void);
extern void print_plugins_versions (FILE *file, const char *indent);
extern void print_plugins_help (FILE *file, const char *indent);
extern void finalize_plugins (void);
extern bool flag_plugin_added;
/* Called from inside GCC. Invoke all plugin callbacks registered with
the specified event.
Return PLUGEVT_SUCCESS if at least one callback was called,
PLUGEVT_NO_CALLBACK if there was no callback.
EVENT - the event identifier
GCC_DATA - event-specific data provided by the compiler */
static inline int
invoke_plugin_callbacks (int event ATTRIBUTE_UNUSED,
void *gcc_data ATTRIBUTE_UNUSED)
{
#ifdef ENABLE_PLUGIN
/* True iff at least one plugin has been added. */
if (flag_plugin_added)
return invoke_plugin_callbacks_full (event, gcc_data);
#endif
return PLUGEVT_NO_CALLBACK;
}
/* In attribs.c. */
extern void register_attribute (const struct attribute_spec *attr);
/* The default argument for the third parameter is given in attribs.h. */
extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *,
const char *,
bool);
#endif /* PLUGIN_H */