From 5d84f5961b8e28e802f600bb2d2c6903e219092e Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 22 Feb 2017 17:37:09 +0000 Subject: [PATCH] Add ATTRIBUTE_FALLTHROUGH for switch cases without break In GCC 7 there is a new warning triggered when a switch case has a conditional statement (eg if ... else...) and some of the code paths fallthrough to the next switch statement. e.g. conf/domain_conf.c: In function 'virDomainChrEquals': conf/domain_conf.c:14926:12: error: this statement may fall through [-Werror=implicit-fallthrough=] if (src->targetTypeAttr != tgt->targetTypeAttr) ^ conf/domain_conf.c:14928:5: note: here case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE: ^~~~ conf/domain_conf.c: In function 'virDomainChrDefFormat': conf/domain_conf.c:22143:12: error: this statement may fall through [-Werror=implicit-fallthrough=] if (def->targetTypeAttr) { ^ conf/domain_conf.c:22151:5: note: here default: ^~~~~~~ GCC introduced a __attribute__((fallthrough)) to let you indicate that this is intentionale behaviour rather than a bug. Signed-off-by: Daniel P. Berrange --- src/conf/domain_conf.c | 5 +++++ src/conf/network_conf.c | 3 ++- src/internal.h | 12 ++++++++++++ src/lxc/lxc_container.c | 2 +- src/network/bridge_driver.c | 6 ++++++ tools/virsh-edit.c | 2 +- 6 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b7b5b7e7d7..97d42fe993 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14935,6 +14935,9 @@ virDomainChrEquals(virDomainChrDefPtr src, case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL: if (src->targetTypeAttr != tgt->targetTypeAttr) return false; + + ATTRIBUTE_FALLTHROUGH; + case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE: case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL: return src->target.port == tgt->target.port; @@ -22158,6 +22161,8 @@ virDomainChrDefFormat(virBufferPtr buf, def->target.port); break; } + ATTRIBUTE_FALLTHROUGH; + default: virBufferAsprintf(buf, "\n", def->target.port); diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 0e20dac074..48e0001899 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2442,7 +2442,8 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt) def->name); goto error; } - /* fall through to next case */ + ATTRIBUTE_FALLTHROUGH; + case VIR_NETWORK_FORWARD_BRIDGE: if (def->delay || stp) { virReportError(VIR_ERR_XML_ERROR, diff --git a/src/internal.h b/src/internal.h index 334659d329..d64be93b34 100644 --- a/src/internal.h +++ b/src/internal.h @@ -218,6 +218,14 @@ # endif # endif +# ifndef ATTRIBUTE_FALLTHROUGH +# if __GNUC_PREREQ (7, 0) +# define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough)) +# else +# define ATTRIBUTE_FALLTHROUGH do {} while(0) +# endif +# endif + # else # ifndef ATTRIBUTE_UNUSED # define ATTRIBUTE_UNUSED @@ -228,6 +236,10 @@ # ifndef ATTRIBUTE_RETURN_CHECK # define ATTRIBUTE_RETURN_CHECK # endif +# +# ifndef ATTRIBUTE_FALLTHROUGH +# define ATTRIBUTE_FALLTHROUGH do {} while(0) +# endif # endif /* __GNUC__ */ diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 601b9b0357..99bd7e9446 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -2042,7 +2042,7 @@ static int lxcContainerDropCapabilities(virDomainDefPtr def, default: /* User specified capabilities to drop */ toDrop = (state == VIR_TRISTATE_SWITCH_OFF); } - /* Fallthrough */ + ATTRIBUTE_FALLTHROUGH; case VIR_DOMAIN_CAPABILITIES_POLICY_ALLOW: if (policy == VIR_DOMAIN_CAPABILITIES_POLICY_ALLOW) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 06759c6431..c5ec2823d3 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -2715,6 +2715,8 @@ networkStartNetwork(virNetworkDriverStatePtr driver, * VIR_NETWORK_FORWARD_BRIDGE with no bridge device defined * (since that is macvtap bridge mode). */ + ATTRIBUTE_FALLTHROUGH; + case VIR_NETWORK_FORWARD_PRIVATE: case VIR_NETWORK_FORWARD_VEPA: case VIR_NETWORK_FORWARD_PASSTHROUGH: @@ -2792,6 +2794,8 @@ networkShutdownNetwork(virNetworkDriverStatePtr driver, * VIR_NETWORK_FORWARD_BRIDGE with no bridge device defined * (since that is macvtap bridge mode). */ + ATTRIBUTE_FALLTHROUGH; + case VIR_NETWORK_FORWARD_PRIVATE: case VIR_NETWORK_FORWARD_VEPA: case VIR_NETWORK_FORWARD_PASSTHROUGH: @@ -4974,6 +4978,8 @@ networkGetNetworkAddress(const char *netname, char **netaddr) * fall through if netdef->bridge wasn't set, since that is * macvtap bridge mode network. */ + ATTRIBUTE_FALLTHROUGH; + case VIR_NETWORK_FORWARD_PRIVATE: case VIR_NETWORK_FORWARD_VEPA: case VIR_NETWORK_FORWARD_PASSTHROUGH: diff --git a/tools/virsh-edit.c b/tools/virsh-edit.c index 16d6705fbb..92a00b7f9a 100644 --- a/tools/virsh-edit.c +++ b/tools/virsh-edit.c @@ -140,7 +140,7 @@ do { goto redefine; break; } - /* fall-through */ + ATTRIBUTE_FALLTHROUGH; #endif default: -- GitLab