/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */1/*2* Copyright (c) 1993, 1994, 1995, 1996, 19973* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. All advertising materials mentioning features or use of this software14* must display the following acknowledgement:15* This product includes software developed by the Computer Systems16* Engineering Group at Lawrence Berkeley Laboratory.17* 4. Neither the name of the University nor of the Laboratory may be used18* to endorse or promote products derived from this software without19* specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#ifndef _diag_control_h35#define _diag_control_h3637#include "pcap/compiler-tests.h"3839#if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8) || \40PCAP_IS_AT_LEAST_GNUC_VERSION(4,6) || \41PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)42/*43* All these compilers support this way of putting pragmas into #defines.44* We use it only if we have a compiler that supports it; see below45* for the code that uses it and the #defines that control whether46* that code is used.47*/48#define PCAP_DO_PRAGMA(x) _Pragma (#x)49#endif5051/*52* Suppress "enum value not explicitly handled in switch" warnings.53* We may have to build on multiple different Windows SDKs, so we54* may not be able to include all enum values in a switch, as they55* won't necessarily be defined on all the SDKs, and, unlike56* #defines, there's no easy way to test whether a given enum has57* a given value. It *could* be done by the configure script or58* CMake tests.59*/60#if defined(_MSC_VER)61#define DIAG_OFF_ENUM_SWITCH \62__pragma(warning(push)) \63__pragma(warning(disable:4061))64#define DIAG_ON_ENUM_SWITCH \65__pragma(warning(pop))66#endif6768/*69* Suppress "switch statement has only a default case" warnings.70* There's a switch in bpf_filter.c that only has additional71* cases on Linux.72*/73#if defined(_MSC_VER)74#define DIAG_OFF_DEFAULT_ONLY_SWITCH \75__pragma(warning(push)) \76__pragma(warning(disable:4065))77#define DIAG_ON_DEFAULT_ONLY_SWITCH \78__pragma(warning(pop))79#endif8081/*82* Suppress Flex, narrowing, and deprecation warnings.83*/84#if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)85/*86* This is Clang 2.8 or later; we can use "clang diagnostic87* ignored -Wxxx" and "clang diagnostic push/pop".88*89* Suppress -Wdocumentation warnings; GCC doesn't support -Wdocumentation,90* at least according to the GCC 7.3 documentation. Apparently, Flex91* generates code that upsets at least some versions of Clang's92* -Wdocumentation.93*94* (This could be clang-cl, which defines _MSC_VER, so test this95* before testing _MSC_VER.)96*/97#define DIAG_OFF_FLEX \98PCAP_DO_PRAGMA(clang diagnostic push) \99PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare") \100PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation") \101PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32") \102PCAP_DO_PRAGMA(clang diagnostic ignored "-Wmissing-noreturn") \103PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunused-parameter") \104PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")105#define DIAG_ON_FLEX \106PCAP_DO_PRAGMA(clang diagnostic pop)107108/*109* Suppress the only narrowing warnings you get from Clang.110*/111#define DIAG_OFF_NARROWING \112PCAP_DO_PRAGMA(clang diagnostic push) \113PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32")114115#define DIAG_ON_NARROWING \116PCAP_DO_PRAGMA(clang diagnostic pop)117118/*119* Suppress deprecation warnings.120*/121#define DIAG_OFF_DEPRECATION \122PCAP_DO_PRAGMA(clang diagnostic push) \123PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")124#define DIAG_ON_DEPRECATION \125PCAP_DO_PRAGMA(clang diagnostic pop)126127/*128* When Clang correctly detects an old-style function prototype after129* preprocessing, the warning can be irrelevant to this source tree because130* the prototype comes from a system header macro.131*/132#if PCAP_IS_AT_LEAST_CLANG_VERSION(5,0)133#define DIAG_OFF_STRICT_PROTOTYPES \134PCAP_DO_PRAGMA(clang diagnostic push) \135PCAP_DO_PRAGMA(clang diagnostic ignored "-Wstrict-prototypes")136#define DIAG_ON_STRICT_PROTOTYPES \137PCAP_DO_PRAGMA(clang diagnostic pop)138#endif139140#define DIAG_OFF_DOCUMENTATION \141PCAP_DO_PRAGMA(clang diagnostic push) \142PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation")143#define DIAG_ON_DOCUMENTATION \144PCAP_DO_PRAGMA(clang diagnostic pop)145146#define DIAG_OFF_SIGN_COMPARE \147PCAP_DO_PRAGMA(clang diagnostic push) \148PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare")149#define DIAG_ON_SIGN_COMPARE \150PCAP_DO_PRAGMA(clang diagnostic pop)151#elif defined(_MSC_VER)152/*153* This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))154* and __pragma(warning(push/pop)).155*156* Suppress signed-vs-unsigned comparison, narrowing, and unreachable157* code warnings.158*/159#define DIAG_OFF_FLEX \160__pragma(warning(push)) \161__pragma(warning(disable:4127)) \162__pragma(warning(disable:4242)) \163__pragma(warning(disable:4244)) \164__pragma(warning(disable:4702))165#define DIAG_ON_FLEX \166__pragma(warning(pop))167168/*169* Suppress narrowing warnings.170*/171#define DIAG_OFF_NARROWING \172__pragma(warning(push)) \173__pragma(warning(disable:4242)) \174__pragma(warning(disable:4311))175#define DIAG_ON_NARROWING \176__pragma(warning(pop))177178/*179* Suppress deprecation warnings.180*/181#define DIAG_OFF_DEPRECATION \182__pragma(warning(push)) \183__pragma(warning(disable:4996))184#define DIAG_ON_DEPRECATION \185__pragma(warning(pop))186#elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)187/*188* This is GCC 4.6 or later, or a compiler claiming to be that.189* We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)190* and "GCC diagnostic push/pop" (introduced in 4.6).191*/192#define DIAG_OFF_FLEX \193PCAP_DO_PRAGMA(GCC diagnostic push) \194PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \195PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \196PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")197#define DIAG_ON_FLEX \198PCAP_DO_PRAGMA(GCC diagnostic pop)199200/*201* GCC currently doesn't issue any narrowing warnings.202*/203204/*205* Suppress deprecation warnings.206*/207#define DIAG_OFF_DEPRECATION \208PCAP_DO_PRAGMA(GCC diagnostic push) \209PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")210#define DIAG_ON_DEPRECATION \211PCAP_DO_PRAGMA(GCC diagnostic pop)212213/*214* Suppress format-truncation= warnings.215* GCC 7.1 had introduced this warning option. Earlier versions (at least216* one particular copy of GCC 4.6.4) treat the request as a warning.217*/218#if PCAP_IS_AT_LEAST_GNUC_VERSION(7,1)219#define DIAG_OFF_FORMAT_TRUNCATION \220PCAP_DO_PRAGMA(GCC diagnostic push) \221PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wformat-truncation=")222#define DIAG_ON_FORMAT_TRUNCATION \223PCAP_DO_PRAGMA(GCC diagnostic pop)224#endif225#elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)226/*227* Sun C compiler version 5.5 (Studio version 8) and later supports "#pragma228* error_messages()".229*/230#define DIAG_OFF_FLEX \231PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))232#define DIAG_ON_FLEX \233PCAP_DO_PRAGMA(error_messages(default,E_STATEMENT_NOT_REACHED))234#endif235236#ifdef YYBYACC237/*238* Berkeley YACC.239*240* It generates a global declaration of yylval, or the appropriately241* prefixed version of yylval, in grammar.h, *even though it's been242* told to generate a pure parser, meaning it doesn't have any global243* variables*. Bison doesn't do this.244*245* That causes a warning due to the local declaration in the parser246* shadowing the global declaration.247*248* So, if the compiler warns about that, we turn off -Wshadow warnings.249*250* In addition, the generated code may have functions with unreachable251* code, so suppress warnings about those.252*/253#if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)254/*255* This is Clang 2.8 or later (including clang-cl, so test this256* before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".257*/258#define DIAG_OFF_BISON_BYACC \259PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \260PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")261#elif defined(_MSC_VER)262/*263* This is Microsoft Visual Studio; we can use264* __pragma(warning(disable:XXXX)).265*/266#define DIAG_OFF_BISON_BYACC \267__pragma(warning(disable:4702))268#elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)269/*270* This is GCC 4.6 or later, or a compiler claiming to be that.271* We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,272* but it may not actually work very well prior to 4.6).273*/274#define DIAG_OFF_BISON_BYACC \275PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \276PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")277#endif278#else279/*280* Bison.281*282* The generated code may have functions with unreachable code and283* switches with only a default case, so suppress warnings about those.284*/285#if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)286/*287* This is Clang 2.8 or later (including clang-cl, so test this288* before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".289*/290#define DIAG_OFF_BISON_BYACC \291PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")292#elif defined(_MSC_VER)293/*294* This is Microsoft Visual Studio; we can use295* __pragma(warning(disable:XXXX)).296*297* Suppress some /Wall warnings.298*/299#define DIAG_OFF_BISON_BYACC \300__pragma(warning(disable:4065)) \301__pragma(warning(disable:4127)) \302__pragma(warning(disable:4242)) \303__pragma(warning(disable:4244)) \304__pragma(warning(disable:4702))305#elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)306/*307* This is GCC 4.6 or later, or a compiler claiming to be that.308* We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,309* but it may not actually work very well prior to 4.6).310*/311#define DIAG_OFF_BISON_BYACC \312PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")313#elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)314/*315* Same as for DIAG_OFF_FLEX above.316*/317#define DIAG_OFF_BISON_BYACC \318PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))319#endif320#endif321322#if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)323/*324* Clang appears to let you ignore a result without a warning by325* casting the function result to void, so we don't appear to326* need this for Clang.327*/328#elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,5)329/*330* GCC warns about unused return values if a function is marked as331* "warn about ignoring this function's return value".332*/333#define DIAG_OFF_WARN_UNUSED_RESULT \334PCAP_DO_PRAGMA(GCC diagnostic push) \335PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-result")336#define DIAG_ON_WARN_UNUSED_RESULT \337PCAP_DO_PRAGMA(GCC diagnostic pop)338339/*340* GCC does not currently generate any -Wstrict-prototypes warnings that341* would need silencing as is done for Clang above.342*/343#endif344345/*346* GCC needs this on AIX for longjmp().347*/348#if PCAP_IS_AT_LEAST_GNUC_VERSION(5,1)349/*350* Beware that the effect of this builtin is more than just squelching the351* warning! GCC trusts it enough for the process to segfault if the control352* flow reaches the builtin (an infinite empty loop in the same context would353* squelch the warning and ruin the process too, albeit in a different way).354* So please remember to use this very carefully.355*/356#define PCAP_UNREACHABLE __builtin_unreachable();357#endif358359#ifndef DIAG_OFF_ENUM_SWITCH360#define DIAG_OFF_ENUM_SWITCH361#endif362#ifndef DIAG_ON_ENUM_SWITCH363#define DIAG_ON_ENUM_SWITCH364#endif365#ifndef DIAG_OFF_DEFAULT_ONLY_SWITCH366#define DIAG_OFF_DEFAULT_ONLY_SWITCH367#endif368#ifndef DIAG_ON_DEFAULT_ONLY_SWITCH369#define DIAG_ON_DEFAULT_ONLY_SWITCH370#endif371#ifndef DIAG_OFF_FLEX372#define DIAG_OFF_FLEX373#endif374#ifndef DIAG_ON_FLEX375#define DIAG_ON_FLEX376#endif377#ifndef DIAG_OFF_NARROWING378#define DIAG_OFF_NARROWING379#endif380#ifndef DIAG_ON_NARROWING381#define DIAG_ON_NARROWING382#endif383#ifndef DIAG_OFF_DEPRECATION384#define DIAG_OFF_DEPRECATION385#endif386#ifndef DIAG_ON_DEPRECATION387#define DIAG_ON_DEPRECATION388#endif389#ifndef DIAG_OFF_FORMAT_TRUNCATION390#define DIAG_OFF_FORMAT_TRUNCATION391#endif392#ifndef DIAG_ON_FORMAT_TRUNCATION393#define DIAG_ON_FORMAT_TRUNCATION394#endif395#ifndef DIAG_OFF_BISON_BYACC396#define DIAG_OFF_BISON_BYACC397#endif398//399// DIAG_ON_BISON_BYACC does not need to be defined.400//401#ifndef DIAG_OFF_WARN_UNUSED_RESULT402#define DIAG_OFF_WARN_UNUSED_RESULT403#endif404#ifndef DIAG_ON_WARN_UNUSED_RESULT405#define DIAG_ON_WARN_UNUSED_RESULT406#endif407#ifndef DIAG_OFF_STRICT_PROTOTYPES408#define DIAG_OFF_STRICT_PROTOTYPES409#endif410#ifndef DIAG_ON_STRICT_PROTOTYPES411#define DIAG_ON_STRICT_PROTOTYPES412#endif413#ifndef DIAG_OFF_DOCUMENTATION414#define DIAG_OFF_DOCUMENTATION415#endif416#ifndef DIAG_ON_DOCUMENTATION417#define DIAG_ON_DOCUMENTATION418#endif419#ifndef DIAG_OFF_SIGN_COMPARE420#define DIAG_OFF_SIGN_COMPARE421#endif422#ifndef DIAG_ON_SIGN_COMPARE423#define DIAG_ON_SIGN_COMPARE424#endif425#ifndef PCAP_UNREACHABLE426#define PCAP_UNREACHABLE427#endif428429#endif /* _diag_control_h */430431432