gamesh411 created this revision.
Herald added subscribers: steakhal, manas, ASDenysPetrov, martong, dkrupp, 
donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, 
xazax.hun.
Herald added a reviewer: Szelethus.
Herald added a project: All.
gamesh411 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

In C++17 the initialization rules for enum classes are relaxed.
See: https://en.cppreference.com/w/cpp/language/enum#enum_relaxed_init_cpp17
EnumCastOutOfRangeChecker now correctly recognizes this, and no longer gives 
false positive reports.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150647

Files:
  clang/test/Analysis/Inputs/system-header-simulator-cxx.h
  clang/test/Analysis/enum-cast-out-of-range-c++17.cpp


Index: clang/test/Analysis/enum-cast-out-of-range-c++17.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/enum-cast-out-of-range-c++17.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_analyze_cc1 \
+// RUN:   -analyzer-checker=core,alpha.cplusplus.EnumCastOutOfRange \
+// RUN:   -std=c++17 -verify %s
+//
+// Test relaxed enum class initialization.
+//
+
+// expected-no-diagnostics
+
+#include "Inputs/system-header-simulator-cxx.h"
+
+void test_direct_list_init() {
+  std::byte b{0};       // OK
+  (void)b;
+}
+
+void test_copy_init() {
+  std::byte b = std::byte{0}; // OK
+  (void)b;
+}
+
+struct A { std::byte b; };
+void test_aggregate_copy_init() {
+  A a = {std::byte{42}}; // OK
+  (void)a;
+}
Index: clang/test/Analysis/Inputs/system-header-simulator-cxx.h
===================================================================
--- clang/test/Analysis/Inputs/system-header-simulator-cxx.h
+++ clang/test/Analysis/Inputs/system-header-simulator-cxx.h
@@ -1253,3 +1253,11 @@
 };
 
 } // namespace std
+
+// C++17 std::byte
+#if __cplusplus >= 201703L
+namespace std {
+  enum class byte : unsigned char {};
+} // namespace std
+#endif
+


Index: clang/test/Analysis/enum-cast-out-of-range-c++17.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/enum-cast-out-of-range-c++17.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_analyze_cc1 \
+// RUN:   -analyzer-checker=core,alpha.cplusplus.EnumCastOutOfRange \
+// RUN:   -std=c++17 -verify %s
+//
+// Test relaxed enum class initialization.
+//
+
+// expected-no-diagnostics
+
+#include "Inputs/system-header-simulator-cxx.h"
+
+void test_direct_list_init() {
+  std::byte b{0};       // OK
+  (void)b;
+}
+
+void test_copy_init() {
+  std::byte b = std::byte{0}; // OK
+  (void)b;
+}
+
+struct A { std::byte b; };
+void test_aggregate_copy_init() {
+  A a = {std::byte{42}}; // OK
+  (void)a;
+}
Index: clang/test/Analysis/Inputs/system-header-simulator-cxx.h
===================================================================
--- clang/test/Analysis/Inputs/system-header-simulator-cxx.h
+++ clang/test/Analysis/Inputs/system-header-simulator-cxx.h
@@ -1253,3 +1253,11 @@
 };
 
 } // namespace std
+
+// C++17 std::byte
+#if __cplusplus >= 201703L
+namespace std {
+  enum class byte : unsigned char {};
+} // namespace std
+#endif
+
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to