Index: include/clang/AST/Type.h =================================================================== --- include/clang/AST/Type.h +++ include/clang/AST/Type.h @@ -1752,6 +1752,7 @@ bool isTemplateTypeParmType() const; // C++ template type parameter bool isNullPtrType() const; // C++11 std::nullptr_t bool isAlignValT() const; // C++17 std::align_val_t + bool isStdByteType() const; // C++17 std::byte bool isAtomicType() const; // C11 _Atomic() #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ Index: lib/AST/Type.cpp =================================================================== --- lib/AST/Type.cpp +++ lib/AST/Type.cpp @@ -2313,6 +2313,15 @@ return false; } +bool Type::isStdByteType() const { + if (auto *ET = getAs()) { + auto *II = ET->getDecl()->getIdentifier(); + if (II && II->isStr("byte") && ET->getDecl()->isInStdNamespace()) + return true; + } + return false; +} + bool Type::isPromotableIntegerType() const { if (const BuiltinType *BT = getAs()) switch (BT->getKind()) { Index: lib/CodeGen/CodeGenTBAA.cpp =================================================================== --- lib/CodeGen/CodeGenTBAA.cpp +++ lib/CodeGen/CodeGenTBAA.cpp @@ -85,6 +85,12 @@ return TypeHasMayAlias(TTy->desugar()); } + // C++1z [intro.object]p3: If a complete object is created in storage + // associated with another object e of type [...] "array of N std::byte", that + // array provides storage for the created object [...]. + if (QTy->isStdByteType()) + return true; + return false; } Index: test/CodeGenCXX/std-byte.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/std-byte.cpp @@ -0,0 +1,41 @@ +// RUN: %clang_cc1 -std=c++1z -Werror -triple i386-unknown-unknown -emit-llvm -O1 -disable-llvm-passes -o - %s | FileCheck %s + +// std::byte should be considered equivalent to char for aliasing. + +namespace std { +enum byte : unsigned char {}; +} + +// CHECK-LABEL: define void @test0( +extern "C" void test0(std::byte *sb, int *i) { + // CHECK: store i8 0, i8* %{{.*}} !tbaa [[TAG_CHAR:!.*]] + *sb = std::byte{0}; + + // CHECK: store i32 1, i32* %{{.*}} !tbaa [[TAG_INT:!.*]] + *i = 1; +} + +enum byte : unsigned char {}; +namespace my { +enum byte : unsigned char {}; +namespace std { +enum byte : unsigned char {}; +} // namespace std +} // namespace my + +// Make sure we don't get confused with other enums named 'byte'. + +// CHECK-LABEL: define void @test1( +extern "C" void test1(::byte *b, ::my::byte *mb, ::my::std::byte *msb) { + *b = ::byte{0}; + *mb = ::my::byte{0}; + *msb = ::my::std::byte{0}; + // CHECK-NOT: store i8 0, i8* %{{.*}} !tbaa [[TAG_CHAR]] +} + +// CHECK: !"any pointer", [[TYPE_CHAR:!.*]], +// CHECK: [[TYPE_CHAR]] = !{!"omnipotent char", [[TAG_CXX_TBAA:!.*]], +// CHECK: [[TAG_CXX_TBAA]] = !{!"Simple C++ TBAA"} +// CHECK: [[TAG_CHAR]] = !{[[TYPE_CHAR:!.*]], [[TYPE_CHAR]], i64 0} +// CHECK: [[TAG_INT]] = !{[[TYPE_INT:!.*]], [[TYPE_INT]], i64 0} +// CHECK: [[TYPE_INT]] = !{!"int", [[TYPE_CHAR]]