diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -4557,6 +4557,7 @@ AddCV, AddConst, AddVolatile, + RemoveConst, }; private: diff --git a/clang/include/clang/Basic/Specifiers.h b/clang/include/clang/Basic/Specifiers.h --- a/clang/include/clang/Basic/Specifiers.h +++ b/clang/include/clang/Basic/Specifiers.h @@ -86,6 +86,7 @@ TST_add_const, // __add_const extension TST_add_cv, // __add_cv extension TST_add_volatile, // __add_volatile extension + TST_remove_const, // __remove_const extension TST_auto, // C++11 auto TST_decltype_auto, // C++1y decltype(auto) TST_auto_type, // __auto_type extension diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def --- a/clang/include/clang/Basic/TokenKinds.def +++ b/clang/include/clang/Basic/TokenKinds.def @@ -511,6 +511,7 @@ KEYWORD(__add_const, KEYCXX) KEYWORD(__add_cv, KEYCXX) KEYWORD(__add_volatile, KEYCXX) +KEYWORD(__remove_const, KEYCXX) // Clang-only C++ Type Traits TYPE_TRAIT_2(__reference_binds_to_temporary, ReferenceBindsToTemporary, KEYCXX) diff --git a/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h --- a/clang/include/clang/Sema/DeclSpec.h +++ b/clang/include/clang/Sema/DeclSpec.h @@ -294,6 +294,7 @@ static const TST TST_add_const = clang::TST_add_const; static const TST TST_add_cv = clang::TST_add_cv; static const TST TST_add_volatile = clang::TST_add_volatile; + static const TST TST_remove_const = clang::TST_remove_const; static const TST TST_auto = clang::TST_auto; static const TST TST_auto_type = clang::TST_auto_type; static const TST TST_unknown_anytype = clang::TST_unknown_anytype; diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -3934,6 +3934,9 @@ case UnaryTransformType::AddVolatile: Out << "3av"; break; + case UnaryTransformType::RemoveConst: + Out << "3rc"; + break; } } diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp --- a/clang/lib/AST/JSONNodeDumper.cpp +++ b/clang/lib/AST/JSONNodeDumper.cpp @@ -674,6 +674,9 @@ case UnaryTransformType::AddVolatile: JOS.attribute("transformedKind", "add_volatile"); break; + case UnaryTransformType::RemoveConst: + JOS.attribute("transformedKind", "remove_const"); + break; } } diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1122,9 +1122,11 @@ case UnaryTransformType::EnumUnderlyingType: case UnaryTransformType::AddConst: case UnaryTransformType::AddCV: + case UnaryTransformType::RemoveConst: case UnaryTransformType::AddVolatile: { - constexpr std::array Transformation = { - "__underlying_type", "__add_const", "__add_cv", "__add_volatile"}; + constexpr std::array Transformation = { + "__underlying_type", "__add_const", "__add_cv", "__add_volatile", + "__remove_const"}; OS << Transformation[T->getUTTKind()] << '('; print(T->getBaseType(), OS, StringRef()); OS << ')'; @@ -1145,6 +1147,7 @@ case UnaryTransformType::AddConst: case UnaryTransformType::AddCV: case UnaryTransformType::AddVolatile: + case UnaryTransformType::RemoveConst: return; } diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -60,6 +60,7 @@ case tok::kw___add_const: case tok::kw___add_cv: case tok::kw___add_volatile: + case tok::kw___remove_const: case tok::annot_typename: case tok::kw_char8_t: case tok::kw_char16_t: diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1661,6 +1661,7 @@ .Case("__add_const", true) .Case("__add_cv", true) .Case("__add_volatile", true) + .Case("__remove_const", true) .Default(false); } else { return llvm::StringSwitch(II->getName()) diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -4118,6 +4118,7 @@ case tok::kw___add_const: case tok::kw___add_cv: case tok::kw___add_volatile: + case tok::kw___remove_const: ParseTypeTransformTypeSpecifier(DS); continue; @@ -4229,6 +4230,8 @@ return DeclSpec::TST_add_cv; case tok::kw___add_volatile: return DeclSpec::TST_add_volatile; + case tok::kw___remove_const: + return DeclSpec::TST_remove_const; } __builtin_unreachable(); diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1534,7 +1534,7 @@ tok::kw___is_trivially_assignable, tok::kw___is_trivially_constructible, tok::kw___is_trivially_copyable, tok::kw___is_union, tok::kw___is_unsigned, tok::kw___is_void, - tok::kw___is_volatile)) + tok::kw___is_volatile, tok::kw___remove_const)) // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the // name of struct templates, but some are keywords in GCC >= 4.3 // and Clang. Therefore, when we see the token sequence "struct diff --git a/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp --- a/clang/lib/Sema/DeclSpec.cpp +++ b/clang/lib/Sema/DeclSpec.cpp @@ -18,6 +18,7 @@ #include "clang/AST/TypeLoc.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceManager.h" +#include "clang/Basic/Specifiers.h" #include "clang/Basic/TargetInfo.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Sema.h" @@ -393,6 +394,7 @@ case TST_add_const: case TST_add_cv: case TST_add_volatile: + case TST_remove_const: case TST_typename: case TST_typeofType: { QualType QT = DS.getRepAsType().get(); @@ -584,6 +586,8 @@ return "__add_const"; case DeclSpec::TST_add_cv: return "__add_cv"; + case DeclSpec::TST_remove_const: + return "__remove_const"; case DeclSpec::TST_add_volatile: return "__add_volatile"; case DeclSpec::TST_unknown_anytype: return "__unknown_anytype"; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -147,6 +147,7 @@ case tok::kw___underlying_type: case tok::kw___add_const: case tok::kw___add_cv: + case tok::kw___remove_const: case tok::kw___add_volatile: case tok::kw___auto_type: return true; @@ -5604,6 +5605,7 @@ case DeclSpec::TST_underlyingType: case DeclSpec::TST_add_const: case DeclSpec::TST_add_cv: + case DeclSpec::TST_remove_const: case DeclSpec::TST_add_volatile: case DeclSpec::TST_atomic: { // Grab the type from the parser. diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp --- a/clang/lib/Sema/SemaTemplateVariadic.cpp +++ b/clang/lib/Sema/SemaTemplateVariadic.cpp @@ -863,6 +863,7 @@ case TST_underlyingType: case TST_add_const: case TST_add_cv: + case TST_remove_const: case TST_add_volatile: case TST_atomic: { QualType T = DS.getRepAsType().get(); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -22,6 +22,7 @@ #include "clang/AST/TypeLoc.h" #include "clang/AST/TypeLocVisitor.h" #include "clang/Basic/PartialDiagnostic.h" +#include "clang/Basic/Specifiers.h" #include "clang/Basic/TargetInfo.h" #include "clang/Lex/Preprocessor.h" #include "clang/Sema/DeclSpec.h" @@ -1266,6 +1267,8 @@ return UnaryTransformType::AddCV; case TST_add_volatile: return UnaryTransformType::AddVolatile; + case TST_remove_const: + return UnaryTransformType::RemoveConst; case TST_underlyingType: return UnaryTransformType::EnumUnderlyingType; default: @@ -1659,6 +1662,7 @@ case DeclSpec::TST_add_const: case DeclSpec::TST_add_cv: case DeclSpec::TST_add_volatile: + case DeclSpec::TST_remove_const: Result = S.GetTypeFromParser(DS.getRepAsType()); assert(!Result.isNull() && "Didn't get a type for the transformation?"); Result = S.BuildUnaryTransformType( @@ -5996,7 +6000,7 @@ void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { // Make sure it is a unary transform type assert(DS.getTypeSpecType() >= DeclSpec::TST_underlyingType && - DS.getTypeSpecType() <= DeclSpec::TST_add_volatile); + DS.getTypeSpecType() <= DeclSpec::TST_remove_const); TL.setKWLoc(DS.getTypeSpecTypeLoc()); TL.setParensRange(DS.getTypeofParensRange()); assert(DS.getRepAsType()); @@ -9088,7 +9092,8 @@ } case UnaryTransformType::AddConst: case UnaryTransformType::AddCV: - case UnaryTransformType::AddVolatile: { + case UnaryTransformType::AddVolatile: + case UnaryTransformType::RemoveConst: { SplitQualType Split = BaseType.getSplitUnqualifiedType(); Qualifiers Quals = BaseType.getQualifiers(); if (BaseType->isReferenceType() || BaseType->isFunctionType()) @@ -9102,6 +9107,10 @@ [[fallthrough]]; case UnaryTransformType::AddVolatile: Quals.addVolatile(); + break; + case UnaryTransformType::RemoveConst: + Quals.removeConst(); + break; } QualType Underlying(Split.Ty, Quals.getAsOpaqueValue()); return Context.getUnaryTransformType(BaseType, Underlying, UKind); diff --git a/clang/test/SemaCXX/remove_cvref.cpp b/clang/test/SemaCXX/remove_cvref.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/remove_cvref.cpp @@ -0,0 +1,40 @@ +// RUN: %clang_cc1 -std=c++11 %s + +template +constexpr bool check_remove_qualifiers() { + static_assert(__is_same(__remove_const(const T), T), ""); + return true; +} + +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); + +struct S {}; +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), ""); +static_assert(check_remove_qualifiers(), "");