merrymeerkat created this revision. Herald added subscribers: martong, kadircet, arphaman, xazax.hun. Herald added a project: All. merrymeerkat requested review of this revision. Herald added projects: clang, clang-tools-extra. Herald added a subscriber: cfe-commits.
This change will allow users to call getNullability() without providing an ASTContext. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D140104 Files: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp clang/include/clang/AST/ASTContext.h clang/include/clang/AST/Type.h clang/lib/AST/ASTContext.cpp clang/lib/AST/Type.cpp clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDecl.cpp clang/lib/CodeGen/CodeGenFunction.cpp clang/lib/Sema/Sema.cpp clang/lib/Sema/SemaChecking.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclObjC.cpp clang/lib/Sema/SemaExpr.cpp clang/lib/Sema/SemaExprObjC.cpp clang/lib/Sema/SemaLambda.cpp clang/lib/Sema/SemaObjCProperty.cpp clang/lib/Sema/SemaType.cpp clang/tools/libclang/CXType.cpp
Index: clang/tools/libclang/CXType.cpp =================================================================== --- clang/tools/libclang/CXType.cpp +++ clang/tools/libclang/CXType.cpp @@ -1330,8 +1330,7 @@ if (T.isNull()) return CXTypeNullability_Invalid; - ASTContext &Ctx = cxtu::getASTUnit(GetTU(CT))->getASTContext(); - if (auto nullability = T->getNullability(Ctx)) { + if (auto nullability = T->getNullability()) { switch (*nullability) { case NullabilityKind::NonNull: return CXTypeNullability_NonNull; Index: clang/lib/Sema/SemaType.cpp =================================================================== --- clang/lib/Sema/SemaType.cpp +++ clang/lib/Sema/SemaType.cpp @@ -4701,8 +4701,8 @@ // inner pointers. complainAboutMissingNullability = CAMN_InnerPointers; - if (T->canHaveNullability(/*ResultIfUnknown*/false) && - !T->getNullability(S.Context)) { + if (T->canHaveNullability(/*ResultIfUnknown*/ false) && + !T->getNullability()) { // Note that we allow but don't require nullability on dependent types. ++NumPointersRemaining; } @@ -4923,8 +4923,8 @@ // If the type itself could have nullability but does not, infer pointer // nullability and perform consistency checking. if (S.CodeSynthesisContexts.empty()) { - if (T->canHaveNullability(/*ResultIfUnknown*/false) && - !T->getNullability(S.Context)) { + if (T->canHaveNullability(/*ResultIfUnknown*/ false) && + !T->getNullability()) { if (isVaList(T)) { // Record that we've seen a pointer, but do nothing else. if (NumPointersRemaining > 0) @@ -4947,9 +4947,8 @@ } } - if (complainAboutMissingNullability == CAMN_Yes && - T->isArrayType() && !T->getNullability(S.Context) && !isVaList(T) && - D.isPrototypeContext() && + if (complainAboutMissingNullability == CAMN_Yes && T->isArrayType() && + !T->getNullability() && !isVaList(T) && D.isPrototypeContext() && !hasOuterPointerLikeChunk(D, D.getNumTypeObjects())) { checkNullabilityConsistency(S, SimplePointerKind::Array, D.getDeclSpec().getTypeSpecTypeLoc()); @@ -7389,7 +7388,7 @@ // This (unlike the code above) looks through typedefs that might // have nullability specifiers on them, which means we cannot // provide a useful Fix-It. - if (auto existingNullability = desugared->getNullability(S.Context)) { + if (auto existingNullability = desugared->getNullability()) { if (nullability != *existingNullability) { S.Diag(nullabilityLoc, diag::err_nullability_conflicting) << DiagNullabilityKind(nullability, isContextSensitive) @@ -7488,7 +7487,7 @@ // If we started with an object pointer type, rebuild it. if (ptrType) { equivType = S.Context.getObjCObjectPointerType(equivType); - if (auto nullability = type->getNullability(S.Context)) { + if (auto nullability = type->getNullability()) { // We create a nullability attribute from the __kindof attribute. // Make sure that will make sense. assert(attr.getAttributeSpellingListIndex() == 0 && Index: clang/lib/Sema/SemaObjCProperty.cpp =================================================================== --- clang/lib/Sema/SemaObjCProperty.cpp +++ clang/lib/Sema/SemaObjCProperty.cpp @@ -2754,7 +2754,7 @@ if (Attributes & ObjCPropertyAttribute::kind_weak) { // 'weak' and 'nonnull' are mutually exclusive. - if (auto nullability = PropertyTy->getNullability(Context)) { + if (auto nullability = PropertyTy->getNullability()) { if (*nullability == NullabilityKind::NonNull) Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "nonnull" << "weak"; Index: clang/lib/Sema/SemaLambda.cpp =================================================================== --- clang/lib/Sema/SemaLambda.cpp +++ clang/lib/Sema/SemaLambda.cpp @@ -775,8 +775,8 @@ if (Context.getCanonicalFunctionResultType(ReturnType) == Context.getCanonicalFunctionResultType(CSI.ReturnType)) { // Use the return type with the strictest possible nullability annotation. - auto RetTyNullability = ReturnType->getNullability(Ctx); - auto BlockNullability = CSI.ReturnType->getNullability(Ctx); + auto RetTyNullability = ReturnType->getNullability(); + auto BlockNullability = CSI.ReturnType->getNullability(); if (BlockNullability && (!RetTyNullability || hasWeakerNullability(*RetTyNullability, *BlockNullability))) Index: clang/lib/Sema/SemaExprObjC.cpp =================================================================== --- clang/lib/Sema/SemaExprObjC.cpp +++ clang/lib/Sema/SemaExprObjC.cpp @@ -592,7 +592,7 @@ BoxedType = NSStringPointer; // Transfer the nullability from method's return type. Optional<NullabilityKind> Nullability = - BoxingMethod->getReturnType()->getNullability(Context); + BoxingMethod->getReturnType()->getNullability(); if (Nullability) BoxedType = Context.getAttributedType( AttributedType::getNullabilityAttrKind(*Nullability), BoxedType, @@ -1466,8 +1466,8 @@ // result type to the returned result. auto transferNullability = [&](QualType type) -> QualType { // If the method's result type has nullability, extract it. - if (auto nullability = Method->getSendResultType(ReceiverType) - ->getNullability(Context)){ + if (auto nullability = + Method->getSendResultType(ReceiverType)->getNullability()) { // Strip off any outer nullability sugar from the provided type. (void)AttributedType::stripOuterNullability(type); @@ -1546,7 +1546,7 @@ assert(MD->isClassMethod() && "expected a class method"); QualType NewResultType = Context.getObjCObjectPointerType( Context.getObjCInterfaceType(MD->getClassInterface())); - if (auto Nullability = resultType->getNullability(Context)) + if (auto Nullability = resultType->getNullability()) NewResultType = Context.getAttributedType( AttributedType::getNullabilityAttrKind(*Nullability), NewResultType, NewResultType); @@ -1563,16 +1563,14 @@ // Map the nullability of the result into a table index. unsigned receiverNullabilityIdx = 0; - if (Optional<NullabilityKind> nullability = - ReceiverType->getNullability(Context)) { + if (Optional<NullabilityKind> nullability = ReceiverType->getNullability()) { if (*nullability == NullabilityKind::NullableResult) nullability = NullabilityKind::Nullable; receiverNullabilityIdx = 1 + static_cast<unsigned>(*nullability); } unsigned resultNullabilityIdx = 0; - if (Optional<NullabilityKind> nullability = - resultType->getNullability(Context)) { + if (Optional<NullabilityKind> nullability = resultType->getNullability()) { if (*nullability == NullabilityKind::NullableResult) nullability = NullabilityKind::Nullable; resultNullabilityIdx = 1 + static_cast<unsigned>(*nullability); @@ -1605,7 +1603,7 @@ } else { resultType = resultType.getDesugaredType(Context); } - } while (resultType->getNullability(Context)); + } while (resultType->getNullability()); // Add nullability back if needed. if (newResultNullabilityIdx > 0) { Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -9070,7 +9070,7 @@ return ResTy; auto GetNullability = [&Ctx](QualType Ty) { - Optional<NullabilityKind> Kind = Ty->getNullability(Ctx); + Optional<NullabilityKind> Kind = Ty->getNullability(); if (Kind) { // For our purposes, treat _Nullable_result as _Nullable. if (*Kind == NullabilityKind::NullableResult) @@ -9107,7 +9107,7 @@ return ResTy; // Strip all nullability from ResTy. - while (ResTy->getNullability(Ctx)) + while (ResTy->getNullability()) ResTy = ResTy.getSingleStepDesugaredType(Ctx); // Create a new AttributedType with the new nullability kind. Index: clang/lib/Sema/SemaDeclObjC.cpp =================================================================== --- clang/lib/Sema/SemaDeclObjC.cpp +++ clang/lib/Sema/SemaDeclObjC.cpp @@ -2361,21 +2361,17 @@ !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), MethodDecl->getReturnType(), false)) { - auto nullabilityMethodImpl = - *MethodImpl->getReturnType()->getNullability(S.Context); - auto nullabilityMethodDecl = - *MethodDecl->getReturnType()->getNullability(S.Context); - S.Diag(MethodImpl->getLocation(), - diag::warn_conflicting_nullability_attr_overriding_ret_types) - << DiagNullabilityKind( - nullabilityMethodImpl, - ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) - != 0)) - << DiagNullabilityKind( - nullabilityMethodDecl, - ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) - != 0)); - S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); + auto nullabilityMethodImpl = *MethodImpl->getReturnType()->getNullability(); + auto nullabilityMethodDecl = *MethodDecl->getReturnType()->getNullability(); + S.Diag(MethodImpl->getLocation(), + diag::warn_conflicting_nullability_attr_overriding_ret_types) + << DiagNullabilityKind(nullabilityMethodImpl, + ((MethodImpl->getObjCDeclQualifier() & + Decl::OBJC_TQ_CSNullability) != 0)) + << DiagNullabilityKind(nullabilityMethodDecl, + ((MethodDecl->getObjCDeclQualifier() & + Decl::OBJC_TQ_CSNullability) != 0)); + S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); } if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), @@ -2453,14 +2449,12 @@ !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { S.Diag(ImplVar->getLocation(), diag::warn_conflicting_nullability_attr_overriding_param_types) - << DiagNullabilityKind( - *ImplTy->getNullability(S.Context), - ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) - != 0)) - << DiagNullabilityKind( - *IfaceTy->getNullability(S.Context), - ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) - != 0)); + << DiagNullabilityKind(*ImplTy->getNullability(), + ((ImplVar->getObjCDeclQualifier() & + Decl::OBJC_TQ_CSNullability) != 0)) + << DiagNullabilityKind(*IfaceTy->getNullability(), + ((IfaceVar->getObjCDeclQualifier() & + Decl::OBJC_TQ_CSNullability) != 0)); S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); } if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) @@ -4543,8 +4537,8 @@ QualType prevType, bool prevUsesCSKeyword) { // Determine the nullability of both types. - auto nullability = type->getNullability(S.Context); - auto prevNullability = prevType->getNullability(S.Context); + auto nullability = type->getNullability(); + auto prevNullability = prevType->getNullability(); // Easy case: both have nullability. if (nullability.has_value() == prevNullability.has_value()) { Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -3398,8 +3398,8 @@ static void mergeParamDeclTypes(ParmVarDecl *NewParam, const ParmVarDecl *OldParam, Sema &S) { - if (auto Oldnullability = OldParam->getType()->getNullability(S.Context)) { - if (auto Newnullability = NewParam->getType()->getNullability(S.Context)) { + if (auto Oldnullability = OldParam->getType()->getNullability()) { + if (auto Newnullability = NewParam->getType()->getNullability()) { if (*Oldnullability != *Newnullability) { S.Diag(NewParam->getLocation(), diag::warn_mismatched_nullability_attr) << DiagNullabilityKind( Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -5544,8 +5544,7 @@ /// Returns true if the value evaluates to null. static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { // If the expression has non-null type, it doesn't evaluate to null. - if (auto nullability - = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) { + if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) { if (*nullability == NullabilityKind::NonNull) return false; } @@ -5629,8 +5628,8 @@ } /// Determine whether the given type has a non-null nullability annotation. -static bool isNonNullType(ASTContext &ctx, QualType type) { - if (auto nullability = type->getNullability(ctx)) +static bool isNonNullType(QualType type) { + if (auto nullability = type->getNullability()) return *nullability == NullabilityKind::NonNull; return false; @@ -5683,8 +5682,7 @@ for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); I != E; ++I, ++ParamIndex) { const ParmVarDecl *PVD = *I; - if (PVD->hasAttr<NonNullAttr>() || - isNonNullType(S.Context, PVD->getType())) { + if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) { if (NonNullArgs.empty()) NonNullArgs.resize(Args.size()); @@ -5713,7 +5711,7 @@ if (Proto) { unsigned Index = 0; for (auto paramType : Proto->getParamTypes()) { - if (isNonNullType(S.Context, paramType)) { + if (isNonNullType(paramType)) { if (NonNullArgs.empty()) NonNullArgs.resize(Args.size()); @@ -12026,7 +12024,7 @@ const FunctionDecl *FD) { // Check if the return value is null but should not be. if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || - (!isObjCMethod && isNonNullType(Context, lhsType))) && + (!isObjCMethod && isNonNullType(lhsType))) && CheckNonNullExpr(*this, RetValExp)) Diag(ReturnLoc, diag::warn_null_ret) << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ clang/lib/Sema/Sema.cpp @@ -564,12 +564,12 @@ void Sema::diagnoseNullableToNonnullConversion(QualType DstType, QualType SrcType, SourceLocation Loc) { - Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); + Optional<NullabilityKind> ExprNullability = SrcType->getNullability(); if (!ExprNullability || (*ExprNullability != NullabilityKind::Nullable && *ExprNullability != NullabilityKind::NullableResult)) return; - Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); + Optional<NullabilityKind> TypeNullability = DstType->getNullability(); if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) return; Index: clang/lib/CodeGen/CodeGenFunction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenFunction.cpp +++ clang/lib/CodeGen/CodeGenFunction.cpp @@ -956,7 +956,7 @@ // If we're checking nullability, we need to know whether we can check the // return value. Initialize the flag to 'true' and refine it in EmitParmDecl. if (SanOpts.has(SanitizerKind::NullabilityReturn)) { - auto Nullability = FnRetTy->getNullability(getContext()); + auto Nullability = FnRetTy->getNullability(); if (Nullability && *Nullability == NullabilityKind::NonNull) { if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>())) Index: clang/lib/CodeGen/CGDecl.cpp =================================================================== --- clang/lib/CodeGen/CGDecl.cpp +++ clang/lib/CodeGen/CGDecl.cpp @@ -758,7 +758,7 @@ if (!SanOpts.has(SanitizerKind::NullabilityAssign)) return; - auto Nullability = LHS.getType()->getNullability(getContext()); + auto Nullability = LHS.getType()->getNullability(); if (!Nullability || *Nullability != NullabilityKind::NonNull) return; @@ -2615,7 +2615,7 @@ // function satisfy their nullability preconditions. This makes it necessary // to emit null checks for args in the function body itself. if (requiresReturnValueNullabilityCheck()) { - auto Nullability = Ty->getNullability(getContext()); + auto Nullability = Ty->getNullability(); if (Nullability && *Nullability == NullabilityKind::NonNull) { SanitizerScope SanScope(this); RetValNullabilityPrecondition = Index: clang/lib/CodeGen/CGCall.cpp =================================================================== --- clang/lib/CodeGen/CGCall.cpp +++ clang/lib/CodeGen/CGCall.cpp @@ -4126,7 +4126,7 @@ bool CanCheckNullability = false; if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) { - auto Nullability = PVD->getType()->getNullability(getContext()); + auto Nullability = PVD->getType()->getNullability(); CanCheckNullability = Nullability && *Nullability == NullabilityKind::NonNull && PVD->getTypeSourceInfo(); Index: clang/lib/AST/Type.cpp =================================================================== --- clang/lib/AST/Type.cpp +++ clang/lib/AST/Type.cpp @@ -4141,8 +4141,7 @@ return LinkageComputer{}.getTypeLinkageAndVisibility(this); } -Optional<NullabilityKind> -Type::getNullability(const ASTContext &Context) const { +Optional<NullabilityKind> Type::getNullability() const { QualType Type(this, 0); while (const auto *AT = Type->getAs<AttributedType>()) { // Check whether this is an attributed type with nullability Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -6975,7 +6975,7 @@ PrettyArrayType->getIndexTypeQualifiers()); // int x[_Nullable] -> int * _Nullable - if (auto Nullability = Ty->getNullability(*this)) { + if (auto Nullability = Ty->getNullability()) { Result = const_cast<ASTContext *>(this)->getAttributedType( AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); } Index: clang/include/clang/AST/Type.h =================================================================== --- clang/include/clang/AST/Type.h +++ clang/include/clang/AST/Type.h @@ -2547,7 +2547,7 @@ /// Note that nullability is only captured as sugar within the type /// system, not as part of the canonical type, so nullability will /// be lost by canonicalization and desugaring. - Optional<NullabilityKind> getNullability(const ASTContext &context) const; + Optional<NullabilityKind> getNullability() const; /// Determine whether the given type can have a nullability /// specifier applied to it, i.e., if it is any kind of pointer type. Index: clang/include/clang/AST/ASTContext.h =================================================================== --- clang/include/clang/AST/ASTContext.h +++ clang/include/clang/AST/ASTContext.h @@ -2548,8 +2548,8 @@ bool hasSameNullabilityTypeQualifier(QualType SubT, QualType SuperT, bool IsParam) const { - auto SubTnullability = SubT->getNullability(*this); - auto SuperTnullability = SuperT->getNullability(*this); + auto SubTnullability = SubT->getNullability(); + auto SuperTnullability = SuperT->getNullability(); if (SubTnullability.has_value() == SuperTnullability.has_value()) { // Neither has nullability; return true if (!SubTnullability) Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -874,7 +874,7 @@ auto AST = TU.build(); EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); const auto *X = cast<FunctionDecl>(findDecl(AST, "foo")).getParamDecl(0); - ASSERT_TRUE(X->getOriginalType()->getNullability(X->getASTContext()) == + ASSERT_TRUE(X->getOriginalType()->getNullability() == NullabilityKind::NonNull); } @@ -892,10 +892,10 @@ EXPECT_THAT(*AST.getDiagnostics(), ElementsAre(diagName("pp_eof_in_assume_nonnull"))); const auto *X = cast<FunctionDecl>(findDecl(AST, "foo")).getParamDecl(0); - ASSERT_TRUE(X->getOriginalType()->getNullability(X->getASTContext()) == + ASSERT_TRUE(X->getOriginalType()->getNullability() == NullabilityKind::NonNull); const auto *Y = cast<FunctionDecl>(findDecl(AST, "bar")).getParamDecl(0); - ASSERT_FALSE(Y->getOriginalType()->getNullability(X->getASTContext())); + ASSERT_FALSE(Y->getOriginalType()->getNullability()); } TEST(DiagnosticsTest, InsideMacros) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits