mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
6418b5f4de
* Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit7263f3289e
. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits:bda3d6a428
97062933ac
0f4fb04bac
1b0c19afd1
a5739ea5f0
* Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit6fe1a42035
. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces3c1c0794dd
* AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit2238c4760e
. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
178 lines
5.2 KiB
Plaintext
178 lines
5.2 KiB
Plaintext
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
//
|
|
// This code is licensed under the MIT License (MIT).
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef GSL_CONTRACTS_H
|
|
#define GSL_CONTRACTS_H
|
|
|
|
#include <exception>
|
|
#include <stdexcept> // for logic_error
|
|
|
|
//
|
|
// make suppress attributes parse for some compilers
|
|
// Hopefully temporary until suppression standardization occurs
|
|
//
|
|
#if defined(__clang__)
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
|
#else
|
|
#if defined(_MSC_VER)
|
|
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
|
#else
|
|
#define GSL_SUPPRESS(x)
|
|
#endif // _MSC_VER
|
|
#endif // __clang__
|
|
|
|
//
|
|
// Temporary until MSVC STL supports no-exceptions mode.
|
|
// Currently terminate is a no-op in this mode, so we add termination behavior back
|
|
//
|
|
#if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
|
|
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
|
|
#include <intrin.h>
|
|
#define RANGE_CHECKS_FAILURE 0
|
|
|
|
#if defined(__clang__)
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Winvalid-noreturn"
|
|
#endif
|
|
|
|
#endif
|
|
|
|
//
|
|
// There are three configuration options for this GSL implementation's behavior
|
|
// when pre/post conditions on the GSL types are violated:
|
|
//
|
|
// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
|
|
// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
|
|
// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
|
|
//
|
|
#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) || defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) || \
|
|
defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
|
|
#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
|
#endif
|
|
|
|
#define GSL_STRINGIFY_DETAIL(x) #x
|
|
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
|
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
|
|
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
#else
|
|
#define GSL_LIKELY(x) (!!(x))
|
|
#define GSL_UNLIKELY(x) (!!(x))
|
|
#endif
|
|
|
|
//
|
|
// GSL_ASSUME(cond)
|
|
//
|
|
// Tell the optimizer that the predicate cond must hold. It is unspecified
|
|
// whether or not cond is actually evaluated.
|
|
//
|
|
#ifdef _MSC_VER
|
|
#define GSL_ASSUME(cond) __assume(cond)
|
|
#elif defined(__GNUC__)
|
|
#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
|
|
#else
|
|
#define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
|
|
#endif
|
|
|
|
//
|
|
// GSL.assert: assertions
|
|
//
|
|
|
|
namespace gsl
|
|
{
|
|
struct fail_fast : public std::logic_error
|
|
{
|
|
explicit fail_fast(char const* const message) : std::logic_error(message) {}
|
|
};
|
|
|
|
namespace details
|
|
{
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
|
|
typedef void (__cdecl *terminate_handler)();
|
|
|
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
|
|
[[noreturn]] inline void __cdecl default_terminate_handler()
|
|
{
|
|
__fastfail(RANGE_CHECKS_FAILURE);
|
|
}
|
|
|
|
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
|
|
{
|
|
static terminate_handler handler = &default_terminate_handler;
|
|
return handler;
|
|
}
|
|
|
|
#endif
|
|
|
|
[[noreturn]] inline void terminate() noexcept
|
|
{
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
(*gsl::details::get_terminate_handler())();
|
|
#else
|
|
std::terminate();
|
|
#endif
|
|
}
|
|
|
|
#if defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
|
|
|
template <typename Exception>
|
|
[[noreturn]] void throw_exception(Exception&&) noexcept
|
|
{
|
|
gsl::details::terminate();
|
|
}
|
|
|
|
#else
|
|
|
|
template <typename Exception>
|
|
[[noreturn]] void throw_exception(Exception&& exception)
|
|
{
|
|
throw std::forward<Exception>(exception);
|
|
}
|
|
|
|
#endif // GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
|
|
|
} // namespace details
|
|
} // namespace gsl
|
|
|
|
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
|
(GSL_LIKELY(cond) ? static_cast<void>(0) \
|
|
: gsl::details::throw_exception(gsl::fail_fast( \
|
|
"GSL: " type " failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__))))
|
|
|
|
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
|
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
|
|
|
|
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond)
|
|
|
|
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
|
|
|
|
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
|
|
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
|
|
|
|
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
|
#pragma clang diagnostic pop
|
|
#endif
|
|
|
|
#endif // GSL_CONTRACTS_H
|