[compiler-rt] r349077 - Implement a small subset of the C++ `type_traits` header inside sanitizer_common so we can avoid depending on system C++ headers.

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 13 11:55:36 PST 2018


Author: delcypher
Date: Thu Dec 13 11:55:36 2018
New Revision: 349077

URL: http://llvm.org/viewvc/llvm-project?rev=349077&view=rev
Log:
Implement a small subset of the C++ `type_traits` header inside sanitizer_common so we can avoid depending on system C++ headers.

Summary:
In particular we implement the `is_same<T,U>` templated type. This is
useful for doing compile-time comparison of types in `static_assert`s.
The plan is to use this in another patch (
https://reviews.llvm.org/D54904 ).

Reviewers: kcc, dvyukov, vitalybuka, cryptoad, eugenis, kubamracek, george.karpenkov

Subscribers: mgorny, #sanitizers, llvm-commits

Differential Revision: https://reviews.llvm.org/D54951

Added:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt

Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h?rev=349077&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h Thu Dec 13 11:55:36 2018
@@ -0,0 +1,44 @@
+//===-- sanitizer_type_traits.h ---------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements a subset of C++ type traits. This is so we can avoid depending
+// on system C++ headers.
+//
+//===----------------------------------------------------------------------===//
+#ifndef SANITIZER_TYPE_TRAITS_H
+#define SANITIZER_TYPE_TRAITS_H
+
+namespace __sanitizer {
+
+struct true_type {
+  static const bool value = true;
+};
+
+struct false_type {
+  static const bool value = false;
+};
+
+// is_same<T, U>
+//
+// Type trait to compare if types are the same.
+// E.g.
+//
+// ```
+// is_same<int,int>::value - True
+// is_same<int,char>::value - False
+// ```
+template <typename T, typename U>
+struct is_same : public false_type {};
+
+template <typename T>
+struct is_same<T, T> : public true_type {};
+
+};  // namespace __sanitizer
+
+#endif

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt?rev=349077&r1=349076&r2=349077&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt Thu Dec 13 11:55:36 2018
@@ -36,6 +36,7 @@ set(SANITIZER_UNITTESTS
   sanitizer_symbolizer_test.cc
   sanitizer_test_main.cc
   sanitizer_thread_registry_test.cc
+  sanitizer_type_traits_test.cc
   sanitizer_vector_test.cc)
 
 set(SANITIZER_TEST_HEADERS

Added: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc?rev=349077&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc (added)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc Thu Dec 13 11:55:36 2018
@@ -0,0 +1,28 @@
+//===-- sanitizer_type_traits_test.cc -------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
+//
+//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_type_traits.h"
+#include "gtest/gtest.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+using namespace __sanitizer;
+
+TEST(SanitizerCommon, IsSame) {
+  ASSERT_TRUE((is_same<unsigned, unsigned>::value));
+  ASSERT_TRUE((is_same<uptr, uptr>::value));
+  ASSERT_TRUE((is_same<sptr, sptr>::value));
+  ASSERT_TRUE((is_same<const uptr, const uptr>::value));
+
+  ASSERT_FALSE((is_same<unsigned, signed>::value));
+  ASSERT_FALSE((is_same<uptr, sptr>::value));
+  ASSERT_FALSE((is_same<uptr, const uptr>::value));
+}




More information about the llvm-commits mailing list