Skip to content

Commit e92d31e

Browse files
author
git apple-llvm automerger
committed
Merge commit 'ca4c4a6758d1' from llvm.org/main into next
2 parents 7a635a9 + ca4c4a6 commit e92d31e

20 files changed

+29
-225
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,6 @@ Attribute Changes in Clang
204204
and each must be a positive integer when provided. The parameter ``x`` is required, while ``y`` and
205205
``z`` are optional with default value of 1.
206206

207-
- The ``_Nullable`` and ``_Nonnull`` family of type attributes can now apply
208-
to certain C++ class types, such as smart pointers:
209-
``void useObject(std::unique_ptr<Object> _Nonnull obj);``.
210-
211-
This works for standard library types including ``unique_ptr``, ``shared_ptr``,
212-
and ``function``. See
213-
`the attribute reference documentation <https://llvm.org/docs/AttributeReference.html#nullability-attributes>`_
214-
for the full list.
215-
216-
- The ``_Nullable`` attribute can be applied to C++ class declarations:
217-
``template <class T> class _Nullable MySmartPointer {};``.
218-
219-
This allows the ``_Nullable`` and ``_Nonnull`` family of type attributes to
220-
apply to this class.
221-
222207
Improvements to Clang's diagnostics
223208
-----------------------------------
224209
- Clang now applies syntax highlighting to the code snippets it

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,10 +2188,9 @@ def TypeNonNull : TypeAttr {
21882188
let Documentation = [TypeNonNullDocs];
21892189
}
21902190

2191-
def TypeNullable : DeclOrTypeAttr {
2191+
def TypeNullable : TypeAttr {
21922192
let Spellings = [CustomKeyword<"_Nullable">];
21932193
let Documentation = [TypeNullableDocs];
2194-
// let Subjects = SubjectList<[CXXRecord], ErrorDiag>;
21952194
}
21962195

21972196
def TypeNullableResult : TypeAttr {

clang/include/clang/Basic/AttrDocs.td

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,20 +4310,6 @@ non-underscored keywords. For example:
43104310
@property (assign, nullable) NSView *superview;
43114311
@property (readonly, nonnull) NSArray *subviews;
43124312
@end
4313-
4314-
As well as built-in pointer types, the nullability attributes can be attached
4315-
to C++ classes marked with the ``_Nullable`` attribute.
4316-
4317-
The following C++ standard library types are considered nullable:
4318-
``unique_ptr``, ``shared_ptr``, ``auto_ptr``, ``exception_ptr``, ``function``,
4319-
``move_only_function`` and ``coroutine_handle``.
4320-
4321-
Types should be marked nullable only where the type itself leaves nullability
4322-
ambiguous. For example, ``std::optional`` is not marked ``_Nullable``, because
4323-
``optional<int> _Nullable`` is redundant and ``optional<int> _Nonnull`` is
4324-
not a useful type. ``std::weak_ptr`` is not nullable, because its nullability
4325-
can change with no visible modification, so static annotation is unlikely to be
4326-
unhelpful.
43274313
}];
43284314
}
43294315

@@ -4358,17 +4344,6 @@ The ``_Nullable`` nullability qualifier indicates that a value of the
43584344
int fetch_or_zero(int * _Nullable ptr);
43594345

43604346
a caller of ``fetch_or_zero`` can provide null.
4361-
4362-
The ``_Nullable`` attribute on classes indicates that the given class can
4363-
represent null values, and so the ``_Nullable``, ``_Nonnull`` etc qualifiers
4364-
make sense for this type. For example:
4365-
4366-
.. code-block:: c
4367-
4368-
class _Nullable ArenaPointer { ... };
4369-
4370-
ArenaPointer _Nonnull x = ...;
4371-
ArenaPointer _Nullable y = nullptr;
43724347
}];
43734348
}
43744349

clang/include/clang/Basic/Features.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ FEATURE(enumerator_attributes, true)
9696
FEATURE(generalized_swift_name, true)
9797
FEATURE(nullability, true)
9898
FEATURE(nullability_on_arrays, true)
99-
FEATURE(nullability_on_classes, true)
10099
FEATURE(nullability_nullable_result, true)
101100
FEATURE(memory_sanitizer,
102101
LangOpts.Sanitize.hasOneOf(SanitizerKind::Memory |

clang/include/clang/Parse/Parser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,6 @@ class Parser : public CodeCompletionHandler {
30143014
void DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
30153015
SourceLocation SkipExtendedMicrosoftTypeAttributes();
30163016
void ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs);
3017-
void ParseNullabilityClassAttributes(ParsedAttributes &attrs);
30183017
void ParseBorlandTypeAttributes(ParsedAttributes &attrs);
30193018
void ParseOpenCLKernelAttributes(ParsedAttributes &attrs);
30203019
void ParseOpenCLQualifiers(ParsedAttributes &Attrs);

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,9 +1655,6 @@ class Sema final {
16551655
/// Add [[gsl::Pointer]] attributes for std:: types.
16561656
void inferGslPointerAttribute(TypedefNameDecl *TD);
16571657

1658-
/// Add _Nullable attributes for std:: types.
1659-
void inferNullableClassAttribute(CXXRecordDecl *CRD);
1660-
16611658
enum PragmaOptionsAlignKind {
16621659
POAK_Native, // #pragma options align=native
16631660
POAK_Natural, // #pragma options align=natural

clang/lib/AST/Type.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,15 +4555,16 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
45554555
case Type::Auto:
45564556
return ResultIfUnknown;
45574557

4558-
// Dependent template specializations could instantiate to pointer types.
4558+
// Dependent template specializations can instantiate to pointer
4559+
// types unless they're known to be specializations of a class
4560+
// template.
45594561
case Type::TemplateSpecialization:
4560-
// If it's a known class template, we can already check if it's nullable.
4561-
if (TemplateDecl *templateDecl =
4562-
cast<TemplateSpecializationType>(type.getTypePtr())
4563-
->getTemplateName()
4564-
.getAsTemplateDecl())
4565-
if (auto *CTD = dyn_cast<ClassTemplateDecl>(templateDecl))
4566-
return CTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
4562+
if (TemplateDecl *templateDecl
4563+
= cast<TemplateSpecializationType>(type.getTypePtr())
4564+
->getTemplateName().getAsTemplateDecl()) {
4565+
if (isa<ClassTemplateDecl>(templateDecl))
4566+
return false;
4567+
}
45674568
return ResultIfUnknown;
45684569

45694570
case Type::Builtin:
@@ -4620,17 +4621,6 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
46204621
}
46214622
llvm_unreachable("unknown builtin type");
46224623

4623-
case Type::Record: {
4624-
const RecordDecl *RD = cast<RecordType>(type)->getDecl();
4625-
// For template specializations, look only at primary template attributes.
4626-
// This is a consistent regardless of whether the instantiation is known.
4627-
if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
4628-
return CTSD->getSpecializedTemplate()
4629-
->getTemplatedDecl()
4630-
->hasAttr<TypeNullableAttr>();
4631-
return RD->hasAttr<TypeNullableAttr>();
4632-
}
4633-
46344624
// Non-pointer types.
46354625
case Type::Complex:
46364626
case Type::LValueReference:
@@ -4648,6 +4638,7 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
46484638
case Type::DependentAddressSpace:
46494639
case Type::FunctionProto:
46504640
case Type::FunctionNoProto:
4641+
case Type::Record:
46514642
case Type::DeducedTemplateSpecialization:
46524643
case Type::Enum:
46534644
case Type::InjectedClassName:

clang/lib/CodeGen/CGCall.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,8 +4380,7 @@ void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
43804380
NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
43814381

43824382
bool CanCheckNullability = false;
4383-
if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD &&
4384-
!PVD->getType()->isRecordType()) {
4383+
if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
43854384
auto Nullability = PVD->getType()->getNullability();
43864385
CanCheckNullability = Nullability &&
43874386
*Nullability == NullabilityKind::NonNull &&

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
990990
// return value. Initialize the flag to 'true' and refine it in EmitParmDecl.
991991
if (SanOpts.has(SanitizerKind::NullabilityReturn)) {
992992
auto Nullability = FnRetTy->getNullability();
993-
if (Nullability && *Nullability == NullabilityKind::NonNull &&
994-
!FnRetTy->isRecordType()) {
993+
if (Nullability && *Nullability == NullabilityKind::NonNull) {
995994
if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) &&
996995
CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>()))
997996
RetValNullabilityPrecondition =

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,15 +1494,6 @@ void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
14941494
}
14951495
}
14961496

1497-
void Parser::ParseNullabilityClassAttributes(ParsedAttributes &attrs) {
1498-
while (Tok.is(tok::kw__Nullable)) {
1499-
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1500-
auto Kind = Tok.getKind();
1501-
SourceLocation AttrNameLoc = ConsumeToken();
1502-
attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, Kind);
1503-
}
1504-
}
1505-
15061497
/// Determine whether the following tokens are valid after a type-specifier
15071498
/// which could be a standalone declaration. This will conservatively return
15081499
/// true if there's any doubt, and is appropriate for insert-';' fixits.
@@ -1684,21 +1675,15 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
16841675

16851676
ParsedAttributes attrs(AttrFactory);
16861677
// If attributes exist after tag, parse them.
1687-
for (;;) {
1688-
MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
1689-
// Parse inheritance specifiers.
1690-
if (Tok.isOneOf(tok::kw___single_inheritance,
1691-
tok::kw___multiple_inheritance,
1692-
tok::kw___virtual_inheritance)) {
1693-
ParseMicrosoftInheritanceClassAttributes(attrs);
1694-
continue;
1695-
}
1696-
if (Tok.is(tok::kw__Nullable)) {
1697-
ParseNullabilityClassAttributes(attrs);
1698-
continue;
1699-
}
1700-
break;
1701-
}
1678+
MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
1679+
1680+
// Parse inheritance specifiers.
1681+
if (Tok.isOneOf(tok::kw___single_inheritance, tok::kw___multiple_inheritance,
1682+
tok::kw___virtual_inheritance))
1683+
ParseMicrosoftInheritanceClassAttributes(attrs);
1684+
1685+
// Allow attributes to precede or succeed the inheritance specifiers.
1686+
MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
17021687

17031688
// Source location used by FIXIT to insert misplaced
17041689
// C++11 attributes

clang/lib/Sema/SemaAttr.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,6 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
215215
inferGslPointerAttribute(Record, Record);
216216
}
217217

218-
void Sema::inferNullableClassAttribute(CXXRecordDecl *CRD) {
219-
static llvm::StringSet<> Nullable{
220-
"auto_ptr", "shared_ptr", "unique_ptr", "exception_ptr",
221-
"coroutine_handle", "function", "move_only_function",
222-
};
223-
224-
if (CRD->isInStdNamespace() && Nullable.count(CRD->getName()) &&
225-
!CRD->hasAttr<TypeNullableAttr>())
226-
for (Decl *Redecl : CRD->redecls())
227-
Redecl->addAttr(TypeNullableAttr::CreateImplicit(Context));
228-
}
229-
230218
void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
231219
SourceLocation PragmaLoc) {
232220
PragmaMsStackAction Action = Sema::PSK_Reset;

clang/lib/Sema/SemaChecking.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "clang/AST/ExprObjC.h"
2828
#include "clang/AST/ExprOpenMP.h"
2929
#include "clang/AST/FormatString.h"
30-
#include "clang/AST/IgnoreExpr.h"
3130
#include "clang/AST/NSAPI.h"
3231
#include "clang/AST/NonTrivialTypeVisitor.h"
3332
#include "clang/AST/OperationKinds.h"
@@ -7670,14 +7669,6 @@ bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
76707669
///
76717670
/// Returns true if the value evaluates to null.
76727671
static bool CheckNonNullExpr(Sema &S, const Expr *Expr) {
7673-
// Treat (smart) pointers constructed from nullptr as null, whether we can
7674-
// const-evaluate them or not.
7675-
// This must happen first: the smart pointer expr might have _Nonnull type!
7676-
if (isa<CXXNullPtrLiteralExpr>(
7677-
IgnoreExprNodes(Expr, IgnoreImplicitAsWrittenSingleStep,
7678-
IgnoreElidableImplicitConstructorSingleStep)))
7679-
return true;
7680-
76817672
// If the expression has non-null type, it doesn't evaluate to null.
76827673
if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) {
76837674
if (*nullability == NullabilityKind::NonNull)

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18248,10 +18248,8 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
1824818248
if (PrevDecl)
1824918249
mergeDeclAttributes(New, PrevDecl);
1825018250

18251-
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(New)) {
18251+
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(New))
1825218252
inferGslOwnerPointerAttribute(CXXRD);
18253-
inferNullableClassAttribute(CXXRD);
18254-
}
1825518253

1825618254
// If there's a #pragma GCC visibility in scope, set the visibility of this
1825718255
// record.

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,20 +5985,6 @@ static void handleBuiltinAliasAttr(Sema &S, Decl *D,
59855985
D->addAttr(::new (S.Context) BuiltinAliasAttr(S.Context, AL, Ident));
59865986
}
59875987

5988-
static void handleNullableTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5989-
if (AL.isUsedAsTypeAttr())
5990-
return;
5991-
5992-
if (auto *CRD = dyn_cast<CXXRecordDecl>(D);
5993-
!CRD || !(CRD->isClass() || CRD->isStruct())) {
5994-
S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type_str)
5995-
<< AL << AL.isRegularKeywordAttribute() << "classes";
5996-
return;
5997-
}
5998-
5999-
handleSimpleAttribute<TypeNullableAttr>(S, D, AL);
6000-
}
6001-
60025988
static void handlePreferredTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
60035989
if (!AL.hasParsedType()) {
60045990
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
@@ -9982,10 +9968,6 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
99829968
case ParsedAttr::AT_UsingIfExists:
99839969
handleSimpleAttribute<UsingIfExistsAttr>(S, D, AL);
99849970
break;
9985-
9986-
case ParsedAttr::AT_TypeNullable:
9987-
handleNullableTypeAttr(S, D, AL);
9988-
break;
99899971
}
99909972
}
99919973

clang/lib/Sema/SemaInit.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7075,11 +7075,6 @@ PerformConstructorInitialization(Sema &S,
70757075
hasCopyOrMoveCtorParam(S.Context,
70767076
getConstructorInfo(Step.Function.FoundDecl));
70777077

7078-
// A smart pointer constructed from a nullable pointer is nullable.
7079-
if (NumArgs == 1 && !Kind.isExplicitCast())
7080-
S.diagnoseNullableToNonnullConversion(
7081-
Entity.getType(), Args.front()->getType(), Kind.getLocation());
7082-
70837078
// Determine the arguments required to actually perform the constructor
70847079
// call.
70857080
if (S.CompleteConstructorCall(Constructor, Step.Type, Args, Loc,

clang/lib/Sema/SemaOverload.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14808,13 +14808,6 @@ ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc,
1480814808
}
1480914809
}
1481014810

14811-
// Check for nonnull = nullable.
14812-
// This won't be caught in the arg's initialization: the parameter to
14813-
// the assignment operator is not marked nonnull.
14814-
if (Op == OO_Equal)
14815-
diagnoseNullableToNonnullConversion(Args[0]->getType(),
14816-
Args[1]->getType(), OpLoc);
14817-
1481814811
// Convert the arguments.
1481914812
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) {
1482014813
// Best->Access is only meaningful for class members.

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,6 @@ DeclResult Sema::CheckClassTemplate(
21712171

21722172
AddPushedVisibilityAttribute(NewClass);
21732173
inferGslOwnerPointerAttribute(NewClass);
2174-
inferNullableClassAttribute(NewClass);
21752174

21762175
if (TUK != TUK_Friend) {
21772176
// Per C++ [basic.scope.temp]p2, skip the template parameter scopes.

clang/lib/Sema/SemaType.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,18 +4707,6 @@ static bool DiagnoseMultipleAddrSpaceAttributes(Sema &S, LangAS ASOld,
47074707
return false;
47084708
}
47094709

4710-
// Whether this is a type broadly expected to have nullability attached.
4711-
// These types are affected by `#pragma assume_nonnull`, and missing nullability
4712-
// will be diagnosed with -Wnullability-completeness.
4713-
static bool shouldHaveNullability(QualType T) {
4714-
return T->canHaveNullability(/*ResultIfUnknown=*/false) &&
4715-
// For now, do not infer/require nullability on C++ smart pointers.
4716-
// It's unclear whether the pragma's behavior is useful for C++.
4717-
// e.g. treating type-aliases and template-type-parameters differently
4718-
// from types of declarations can be surprising.
4719-
!isa<RecordType>(T);
4720-
}
4721-
47224710
static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
47234711
QualType declSpecType,
47244712
TypeSourceInfo *TInfo) {
@@ -4837,7 +4825,8 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
48374825
// inner pointers.
48384826
complainAboutMissingNullability = CAMN_InnerPointers;
48394827

4840-
if (shouldHaveNullability(T) && !T->getNullability()) {
4828+
if (T->canHaveNullability(/*ResultIfUnknown*/ false) &&
4829+
!T->getNullability()) {
48414830
// Note that we allow but don't require nullability on dependent types.
48424831
++NumPointersRemaining;
48434832
}
@@ -5060,7 +5049,8 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
50605049
// If the type itself could have nullability but does not, infer pointer
50615050
// nullability and perform consistency checking.
50625051
if (S.CodeSynthesisContexts.empty()) {
5063-
if (shouldHaveNullability(T) && !T->getNullability()) {
5052+
if (T->canHaveNullability(/*ResultIfUnknown*/ false) &&
5053+
!T->getNullability()) {
50645054
if (isVaList(T)) {
50655055
// Record that we've seen a pointer, but do nothing else.
50665056
if (NumPointersRemaining > 0)

clang/test/Sema/nullability.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,3 @@ void arraysInBlocks(void) {
248248
void (^withTypedefBad)(INTS _Nonnull [2]) = // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}
249249
^(INTS _Nonnull x[2]) {}; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}
250250
}
251-
252-
struct _Nullable NotCplusplusClass {}; // expected-error {{'_Nullable' attribute only applies to classes}}

0 commit comments

Comments
 (0)