[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
hamzasood updated this revision to Diff 156857. hamzasood added a comment. - Made the suggested test changes. The compiler is meant to create these; Clang doesn't do this yet but I'm working on the implementation. I was under the impression that the compiler support would require the library stuff to land first, but I just checked Clang's coroutine tests and it looks like they include their own coroutine header so it doesn't depend on the library. I suppose the same thing can happen here, in which case I agree it's probably best to put this on hold in the meantime. https://reviews.llvm.org/D49647 Files: include/contract include/module.modulemap test/libcxx/double_include.sh.cpp test/libcxx/language.support/support.contract/version.pass.cpp test/std/language.support/support.contract/contract_violation.pass.cpp Index: test/std/language.support/support.contract/contract_violation.pass.cpp === --- test/std/language.support/support.contract/contract_violation.pass.cpp +++ test/std/language.support/support.contract/contract_violation.pass.cpp @@ -0,0 +1,44 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// +// class contract_violation + +#include +#include +#include +#include +#include + +using CV = std::contract_violation; + +// Check for noexcept. +static_assert(noexcept(std::declval().line_number())); +static_assert(noexcept(std::declval().file_name())); +static_assert(noexcept(std::declval().function_name())); +static_assert(noexcept(std::declval().comment())); +static_assert(noexcept(std::declval().assertion_level())); + +// Check return types. +static_assert(std::is_same_v().line_number()), + std::uint_least32_t>); +static_assert(std::is_same_v().file_name()), + std::string_view>); +static_assert(std::is_same_v().function_name()), + std::string_view>); +static_assert(std::is_same_v().comment()), + std::string_view>); +static_assert(std::is_same_v().assertion_level()), + std::string_view>); + +int main() +{ +} Index: test/libcxx/language.support/support.contract/version.pass.cpp === --- test/libcxx/language.support/support.contract/version.pass.cpp +++ test/libcxx/language.support/support.contract/version.pass.cpp @@ -0,0 +1,22 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +#include + +#ifndef _LIBCPP_VERSION +#error _LIBCPP_VERSION not defined +#endif + +int main() +{ +} Index: test/libcxx/double_include.sh.cpp === --- test/libcxx/double_include.sh.cpp +++ test/libcxx/double_include.sh.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include Index: include/module.modulemap === --- include/module.modulemap +++ include/module.modulemap @@ -255,6 +255,10 @@ header "condition_variable" export * } + module contract { +header "contract" +export * + } module deque { header "deque" export initializer_list Index: include/contract === --- include/contract +++ include/contract @@ -0,0 +1,67 @@ +// -*- C++ -*- +//===-- contract --===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef _LIBCPP_CONTRACT +#define _LIBCPP_CONTRACT + +/* +contract synopsis + +namespace std { + class contract_violation { + public: +uint_least32_t line_number() const noexcept; +string_view file_name() const noexcept; +string_view function_name() const noexcept; +string_view comment() const noexcept; +string_view assertion_level() const noexcept; + }; +} +*/ + +#include <__config> +#include +#include + +#ifndef
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
mclow.lists added inline comments. Comment at: test/libcxx/language.support/support.contract/version.pass.cpp:9 +//===--===// + +// All the tests need `// UNSUPPORTED: c++03, c++11, c++14, c++17` https://reviews.llvm.org/D49647 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
mclow.lists added a comment. I'd like to be clear that I'm not against using `ASSERT_NOEXCEPT` or `ASSERT_SAME_TYPE` - that's fine. But I'd rather you use `declval<>` - which is only useful in an unevaluated context, rather than having a function that takes a parameter - which someone might actually call. Also, then you can move all the tests into `main` and get rid of the function. https://reviews.llvm.org/D49647 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
mclow.lists added a comment. > Testing whether it's actually hooked up correctly is quite problematic; it > isn't meant to be instantiable. What's the best way to proceed with this? The compiler is supposed to create these - right? Does clang currently do this? - If not, we should wait for them to catch up. - If so, then we should have tests that have a contract violation, and pass these around. Since the method for hooking up a CV proc is implementation-defined, those tests will probably have to go into `test/libcxx`. Comment at: test/std/language.support/support.contract/contract_violation.pass.cpp:27 + + ASSERT_NOEXCEPT(violation.line_number()); + ASSERT_NOEXCEPT(violation.file_name()); You can do this better with static_assert and declval: static_assert(noexcept(std::declval().line_number()), ""); and same with the types: static_assert(std::is_same_v().line_number()>, std::uint_least32_t>, ""); (It's shorter to write `using CV = const contract_violation;` and then `declval()`) https://reviews.llvm.org/D49647 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
hamzasood updated this revision to Diff 156808. hamzasood added a comment. - Added a missing visibility attribute. - Added a simple test that type-checks the expected public interface. Testing whether it's actually hooked up correctly is quite problematic; it isn't meant to be instantiable. What's the best way to proceed with this? https://reviews.llvm.org/D49647 Files: include/contract include/module.modulemap test/libcxx/double_include.sh.cpp test/libcxx/language.support/support.contract/version.pass.cpp test/std/language.support/support.contract/contract_violation.pass.cpp Index: test/std/language.support/support.contract/contract_violation.pass.cpp === --- test/std/language.support/support.contract/contract_violation.pass.cpp +++ test/std/language.support/support.contract/contract_violation.pass.cpp @@ -0,0 +1,42 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// +// class contract_violation + +#include +#include +#include + +#include "test_macros.h" + +// Check that the expected public members exist and have the required type. +// The contract_violation is taken as a const reference, which ensures that the +// member functions we access are marked as const. +void check_public_member_types(const std::contract_violation ) { + (void)violation; + + ASSERT_NOEXCEPT(violation.line_number()); + ASSERT_NOEXCEPT(violation.file_name()); + ASSERT_NOEXCEPT(violation.function_name()); + ASSERT_NOEXCEPT(violation.comment()); + ASSERT_NOEXCEPT(violation.assertion_level()); + + ASSERT_SAME_TYPE(decltype(violation.line_number()), std::uint_least32_t); + ASSERT_SAME_TYPE(decltype(violation.file_name()), std::string_view); + ASSERT_SAME_TYPE(decltype(violation.function_name()), std::string_view); + ASSERT_SAME_TYPE(decltype(violation.comment()), std::string_view); + ASSERT_SAME_TYPE(decltype(violation.assertion_level()), std::string_view); +} + +int main() +{ +} Index: test/libcxx/language.support/support.contract/version.pass.cpp === --- test/libcxx/language.support/support.contract/version.pass.cpp +++ test/libcxx/language.support/support.contract/version.pass.cpp @@ -0,0 +1,20 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// + +#include + +#ifndef _LIBCPP_VERSION +#error _LIBCPP_VERSION not defined +#endif + +int main() +{ +} Index: test/libcxx/double_include.sh.cpp === --- test/libcxx/double_include.sh.cpp +++ test/libcxx/double_include.sh.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include Index: include/module.modulemap === --- include/module.modulemap +++ include/module.modulemap @@ -255,6 +255,10 @@ header "condition_variable" export * } + module contract { +header "contract" +export * + } module deque { header "deque" export initializer_list Index: include/contract === --- include/contract +++ include/contract @@ -0,0 +1,67 @@ +// -*- C++ -*- +//===-- contract --===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef _LIBCPP_CONTRACT +#define _LIBCPP_CONTRACT + +/* +contract synopsis + +namespace std { + class contract_violation { + public: +uint_least32_t line_number() const noexcept; +string_view file_name() const noexcept; +string_view function_name() const noexcept; +string_view comment() const noexcept; +string_view assertion_level() const noexcept; + }; +} +*/ + +#include <__config> +#include +#include + +#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 + +class _LIBCPP_TYPE_VIS contract_violation { +public: + _LIBCPP_INLINE_VISIBILITY uint_least32_t line_number() const noexcept {
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
mclow.lists added a comment. I get that lib++ will not make these things, but we still need some tests showing that the whole machinery works. Repository: rCXX libc++ https://reviews.llvm.org/D49647 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
mclow.lists added a comment. Thanks for doing this, but it needs tests before it can land. If I were to define `class contract_violation {};` that would pass the tests you've provided. (Heck, if I removed the entire definition of `contract_violation`, it would still pass.) Comment at: include/contract:41 + +class contract_violation { +public: Needs `_LIBCPP_TYPE_VIS` Repository: rCXX libc++ https://reviews.llvm.org/D49647 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49647: [libcxx] Library support for contracts (C++2a)
hamzasood created this revision. hamzasood added reviewers: EricWF, mclow.lists, rsmith. Herald added subscribers: cfe-commits, ldionne, christof. This patch adds the library components needed for contracts in C++2a. The wording says that a contract_violation object is populated in an implementation-defined manner. A private constructor that the compiler can call seems like the most sensible (and obvious) solution. Repository: rCXX libc++ https://reviews.llvm.org/D49647 Files: include/contract include/module.modulemap test/libcxx/double_include.sh.cpp test/libcxx/language.support/support.contract/version.pass.cpp Index: test/libcxx/language.support/support.contract/version.pass.cpp === --- test/libcxx/language.support/support.contract/version.pass.cpp +++ test/libcxx/language.support/support.contract/version.pass.cpp @@ -0,0 +1,20 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// + +#include + +#ifndef _LIBCPP_VERSION +#error _LIBCPP_VERSION not defined +#endif + +int main() +{ +} Index: test/libcxx/double_include.sh.cpp === --- test/libcxx/double_include.sh.cpp +++ test/libcxx/double_include.sh.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include Index: include/module.modulemap === --- include/module.modulemap +++ include/module.modulemap @@ -255,6 +255,10 @@ header "condition_variable" export * } + module contract { +header "contract" +export * + } module deque { header "deque" export initializer_list Index: include/contract === --- include/contract +++ include/contract @@ -0,0 +1,67 @@ +// -*- C++ -*- +//===-- contract --===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef _LIBCPP_CONTRACT +#define _LIBCPP_CONTRACT + +/* +contract synopsis + +namespace std { + class contract_violation { + public: +uint_least32_t line_number() const noexcept; +string_view file_name() const noexcept; +string_view function_name() const noexcept; +string_view comment() const noexcept; +string_view assertion_level() const noexcept; + }; +} +*/ + +#include <__config> +#include +#include + +#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 + +class contract_violation { +public: + _LIBCPP_INLINE_VISIBILITY uint_least32_t line_number() const noexcept { return __line_number; } + _LIBCPP_INLINE_VISIBILITY string_view file_name() const noexcept { return __file_name; } + _LIBCPP_INLINE_VISIBILITY string_view function_name() const noexcept { return __function_name; } + _LIBCPP_INLINE_VISIBILITY string_view comment() const noexcept { return __comment; } + _LIBCPP_INLINE_VISIBILITY string_view assertion_level() const noexcept { return __assertion_level; } + +private: + _LIBCPP_INLINE_VISIBILITY + constexpr contract_violation(uint_least32_t __line, string_view __file, string_view __fn, + string_view __comment, string_view __lvl) noexcept + : __line_number(__line), __file_name(__file), __function_name(__fn), +__comment(__comment), __assertion_level(__lvl) {} + + uint_least32_t __line_number; + string_view __file_name; + string_view __function_name; + string_view __comment; + string_view __assertion_level; +}; + +#endif // _LIBCPP_STD_VER > 17 + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_CONTRACT ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits