/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE2 is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/3940#ifndef PCRE2_UTIL_H_IDEMPOTENT_GUARD41#define PCRE2_UTIL_H_IDEMPOTENT_GUARD4243/* Assertion macros */4445#ifdef PCRE2_DEBUG4647#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)48#include <assert.h>49#endif5051/* PCRE2_ASSERT(x) can be used to inject an assert() for conditions52that the code below doesn't support. It is a NOP for non debug builds53but in debug builds will print information about the location of the54code where it triggered and crash.5556It is meant to work like assert(), and therefore the expression used57should indicate what the expected state is, and shouldn't have any58side-effects. */5960#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)61#define PCRE2_ASSERT(x) assert(x)62#else63#define PCRE2_ASSERT(x) do \64{ \65if (!(x)) \66{ \67fprintf(stderr, "Assertion failed at " __FILE__ ":%d\n", __LINE__); \68abort(); \69} \70} while(0)71#endif7273/* LCOV_EXCL_START */7475/* PCRE2_UNREACHABLE() can be used to mark locations on the code that76shouldn't be reached. In non debug builds is defined as a hint for77the compiler to eliminate any code after it, so it is useful also for78performance reasons, but should be used with care because if it is79ever reached will trigger Undefined Behaviour and if you are lucky a80crash. In debug builds it will report the location where it was triggered81and crash. One important point to consider when using this macro, is82that it is only implemented for a few compilers, and therefore can't83be relied on to always be active either, so if it is followed by some84code it is important to make sure that the whole thing is safe to85use even if the macro is not there (ex: make sure there is a `break`86after it if used at the end of a `case`) and to test your code also87with a configuration where the macro will be a NOP. */8889#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)90#define PCRE2_UNREACHABLE() \91assert(((void)"Execution reached unexpected point", 0))92#else93#define PCRE2_UNREACHABLE() do \94{ \95fprintf(stderr, "Execution reached unexpected point at " __FILE__ \96":%d\n", __LINE__); \97abort(); \98} while(0)99#endif100101/* PCRE2_DEBUG_UNREACHABLE() is a debug only version of the previous102macro. It is meant to be used in places where the code is handling103an error situation in code that shouldn't be reached, but that has104some sort of fallback code to normally handle the error. When in105doubt you should use this instead of the previous macro. Like in106the previous case, it is a good idea to document as much as possible107the reason and the actions that should be taken if it ever triggers. */108109#define PCRE2_DEBUG_UNREACHABLE() PCRE2_UNREACHABLE()110111/* LCOV_EXCL_STOP */112113#endif /* PCRE2_DEBUG */114115#ifndef PCRE2_ASSERT116#define PCRE2_ASSERT(x) do {} while(0)117#endif118119/* LCOV_EXCL_START */120121#ifndef PCRE2_DEBUG_UNREACHABLE122#define PCRE2_DEBUG_UNREACHABLE() do {} while(0)123#endif124125#ifndef PCRE2_UNREACHABLE126#ifdef HAVE_BUILTIN_UNREACHABLE127#define PCRE2_UNREACHABLE() __builtin_unreachable()128#elif defined(HAVE_BUILTIN_ASSUME)129#define PCRE2_UNREACHABLE() __assume(0)130#else131#define PCRE2_UNREACHABLE() do {} while(0)132#endif133#endif /* !PCRE2_UNREACHABLE */134135/* LCOV_EXCL_STOP */136137/* We define this fallthrough macro for suppressing -Wimplicit-fallthrough warnings.138Clang only allows this via an attribute, whereas other compilers (eg. GCC) match attributes139and also specially-formatted comments.140141This macro should be used with no following semicolon, and ideally with a comment: */142143// PCRE2_FALLTHROUGH /* Fall through */144145#ifndef PCRE2_FALLTHROUGH146147#if defined(__cplusplus) && __cplusplus >= 202002L && \148defined(__has_cpp_attribute)149/* Standards-compatible C++ variant. */150#if __has_cpp_attribute(fallthrough)151#define PCRE2_FALLTHROUGH [[fallthrough]];152#endif153#elif !defined(__cplusplus) && \154defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L && \155defined(__has_c_attribute)156/* Standards-compatible C variant. */157#if __has_c_attribute(fallthrough)158#define PCRE2_FALLTHROUGH [[fallthrough]];159#endif160#elif ((defined(__clang__) && __clang_major__ >= 10) || \161(defined(__GNUC__) && __GNUC__ >= 7)) && \162defined(__has_attribute)163/* Clang and GCC syntax. Rule out old versions because apparently Clang at164least has a broken implementation of __has_attribute. */165#if __has_attribute(fallthrough)166#define PCRE2_FALLTHROUGH __attribute__((fallthrough));167#endif168#endif169170#endif /* !PCRE2_FALLTHROUGH */171172#ifndef PCRE2_FALLTHROUGH173#define PCRE2_FALLTHROUGH174#endif175176#endif /* PCRE2_UTIL_H_IDEMPOTENT_GUARD */177178/* End of pcre2_util.h */179180181