diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2017-03-24 09:11:57 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2017-03-24 09:11:57 +0000 |
commit | 45bbe0117bee932c967de6d44984ea7dab8f36b5 (patch) | |
tree | f138defe1ddbfab8edbad2265af359e6339f6289 /clang/lib | |
parent | 0387364c4c00277be875e1544d99492b0ea7820b (diff) | |
download | bcm5719-llvm-45bbe0117bee932c967de6d44984ea7dab8f36b5.tar.gz bcm5719-llvm-45bbe0117bee932c967de6d44984ea7dab8f36b5.zip |
Revert r298491 and r298494 which changed Clang's handling of 'nonnull'
attributes.
These patches don't work because we can't currently access the parameter
information in a reliable way when building attributes. I thought this
would be relatively straightforward to fix, but it seems not to be the
case. Fixing this will requrie a substantial re-plumbing of machinery to
allow attributes to be handled in this location, and several other fixes
to the attribute machinery should probably be made at the same time. All
of this will make the patch .... substantially more complicated.
Reverting for now as there are active miscompiles caused by the current
version.
llvm-svn: 298695
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/ASTContext.cpp | 44 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGCall.cpp | 90 |
2 files changed, 49 insertions, 85 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 1c1f78ad266..071537a051c 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -8499,14 +8499,13 @@ void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, /// to be an Integer Constant Expression. static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, ASTContext::GetBuiltinTypeError &Error, - bool &RequiresICE, bool &OverrideNonnull, + bool &RequiresICE, bool AllowTypeModifiers) { // Modifiers. int HowLong = 0; bool Signed = false, Unsigned = false; RequiresICE = false; - OverrideNonnull = false; - + // Read the prefixed modifiers first. bool Done = false; while (!Done) { @@ -8515,9 +8514,6 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, case 'I': RequiresICE = true; break; - case 'N': - OverrideNonnull = true; - break; case 'S': assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); assert(!Signed && "Can't use 'S' modifier multiple times!"); @@ -8652,8 +8648,8 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, assert(End != Str && "Missing vector size"); Str = End; - QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, - OverrideNonnull, false); + QualType ElementType = DecodeTypeFromStr(Str, Context, Error, + RequiresICE, false); assert(!RequiresICE && "Can't require vector ICE"); // TODO: No way to make AltiVec vectors in builtins yet. @@ -8668,15 +8664,15 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, assert(End != Str && "Missing vector size"); Str = End; - + QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, - OverrideNonnull, false); + false); Type = Context.getExtVectorType(ElementType, NumElements); break; } case 'X': { QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, - OverrideNonnull, false); + false); assert(!RequiresICE && "Can't require complex ICE"); Type = Context.getComplexType(ElementType); break; @@ -8758,37 +8754,27 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, } /// GetBuiltinType - Return the type for the specified builtin. -QualType ASTContext::GetBuiltinType(unsigned Id, GetBuiltinTypeError &Error, - unsigned *IntegerConstantArgs, - bool *OverrideNonnullReturn, - unsigned *OverrideNonnullArgs) const { +QualType ASTContext::GetBuiltinType(unsigned Id, + GetBuiltinTypeError &Error, + unsigned *IntegerConstantArgs) const { const char *TypeStr = BuiltinInfo.getTypeString(Id); SmallVector<QualType, 8> ArgTypes; bool RequiresICE = false; - bool OverrideNonnull = false; Error = GE_None; - QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, - OverrideNonnull, true); + QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, + RequiresICE, true); if (Error != GE_None) return QualType(); - - if (OverrideNonnullReturn) - *OverrideNonnullReturn = OverrideNonnull; + assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); - + while (TypeStr[0] && TypeStr[0] != '.') { - QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, - OverrideNonnull, true); + QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); if (Error != GE_None) return QualType(); - // If this argument should have any nonnull annotations overriden, fill in - // the bitmask. - if (OverrideNonnull && OverrideNonnullArgs) - *OverrideNonnullArgs |= 1 << ArgTypes.size(); - // If this argument is required to be an IntegerConstantExpression and the // caller cares, fill in the bitmask we return. if (RequiresICE && IntegerConstantArgs) diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 7e8d5161808..55191b223d0 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -1759,34 +1759,6 @@ void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) { F.addAttributes(llvm::AttributeList::FunctionIndex, AS); } -/// Returns the attribute (either parameter attribute, or function -/// attribute), which declares argument ArgNo to be non-null. -static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, - QualType ArgType, unsigned ArgNo) { - // FIXME: __attribute__((nonnull)) can also be applied to: - // - references to pointers, where the pointee is known to be - // nonnull (apparently a Clang extension) - // - transparent unions containing pointers - // In the former case, LLVM IR cannot represent the constraint. In - // the latter case, we have no guarantee that the transparent union - // is in fact passed as a pointer. - if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType()) - return nullptr; - // First, check attribute on parameter itself. - if (PVD) { - if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>()) - return ParmNNAttr; - } - // Check function attributes. - if (!FD) - return nullptr; - for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) { - if (NNAttr->isNonNull(ArgNo)) - return NNAttr; - } - return nullptr; -} - void CodeGenModule::ConstructAttributeList( StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo, AttributeListType &PAL, unsigned &CallingConv, bool AttrOnCallSite) { @@ -1803,22 +1775,6 @@ void CodeGenModule::ConstructAttributeList( CalleeInfo.getCalleeFunctionProtoType()); const Decl *TargetDecl = CalleeInfo.getCalleeDecl(); - const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl); - - // Check if this is a builtin function that might override some attributes. - unsigned BuiltinID = FD ? FD->getBuiltinID() : 0; - bool OverrideNonnullReturn = false; - unsigned OverrideNonnullArgs = 0; - if (BuiltinID) { - ASTContext::GetBuiltinTypeError Error; - getContext().GetBuiltinType(BuiltinID, Error, nullptr, - &OverrideNonnullReturn, &OverrideNonnullArgs); - // Note that we can't check the 'Error' here as there may be errors that - // have been diagnosed and reported to the programmer as warnings but - // repaired in the AST such as for implicit declarations of builtin - // functions. None of those impact our usage of this to analyze attributes - // for the builtins. - } bool HasOptnone = false; // FIXME: handle sseregparm someday... @@ -1834,13 +1790,13 @@ void CodeGenModule::ConstructAttributeList( if (TargetDecl->hasAttr<ConvergentAttr>()) FuncAttrs.addAttribute(llvm::Attribute::Convergent); - if (FD) { + if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { AddAttributesFromFunctionProtoType( - getContext(), FuncAttrs, FD->getType()->getAs<FunctionProtoType>()); + getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>()); // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. // These attributes are not inherited by overloads. - const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); - if (FD->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) + const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); + if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) FuncAttrs.addAttribute(llvm::Attribute::NoReturn); } @@ -1857,7 +1813,7 @@ void CodeGenModule::ConstructAttributeList( } if (TargetDecl->hasAttr<RestrictAttr>()) RetAttrs.addAttribute(llvm::Attribute::NoAlias); - if (TargetDecl->hasAttr<ReturnsNonNullAttr>() && !OverrideNonnullReturn) + if (TargetDecl->hasAttr<ReturnsNonNullAttr>()) RetAttrs.addAttribute(llvm::Attribute::NonNull); HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>(); @@ -1889,6 +1845,7 @@ void CodeGenModule::ConstructAttributeList( // we have a decl for the function and it has a target attribute then // parse that and add it to the feature set. StringRef TargetCPU = getTarget().getTargetOpts().CPU; + const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl); if (FD && FD->hasAttr<TargetAttr>()) { llvm::StringMap<bool> FeatureMap; getFunctionFeatureMap(FeatureMap, FD); @@ -2080,13 +2037,6 @@ void CodeGenModule::ConstructAttributeList( continue; } - if (FD && ArgNo < FD->param_size() && ParamType->isPointerType()) { - auto *PVD = FD->getParamDecl(ArgNo); - if (getNonNullAttr(FD, PVD, ParamType, PVD->getFunctionScopeIndex()) && - (!BuiltinID || (OverrideNonnullArgs & (1 << ArgNo)) == 0)) - Attrs.addAttribute(llvm::Attribute::NonNull); - } - if (const auto *RefTy = ParamType->getAs<ReferenceType>()) { QualType PTy = RefTy->getPointeeType(); if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) @@ -2166,6 +2116,34 @@ static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); } +/// Returns the attribute (either parameter attribute, or function +/// attribute), which declares argument ArgNo to be non-null. +static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, + QualType ArgType, unsigned ArgNo) { + // FIXME: __attribute__((nonnull)) can also be applied to: + // - references to pointers, where the pointee is known to be + // nonnull (apparently a Clang extension) + // - transparent unions containing pointers + // In the former case, LLVM IR cannot represent the constraint. In + // the latter case, we have no guarantee that the transparent union + // is in fact passed as a pointer. + if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType()) + return nullptr; + // First, check attribute on parameter itself. + if (PVD) { + if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>()) + return ParmNNAttr; + } + // Check function attributes. + if (!FD) + return nullptr; + for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) { + if (NNAttr->isNonNull(ArgNo)) + return NNAttr; + } + return nullptr; +} + namespace { struct CopyBackSwiftError final : EHScopeStack::Cleanup { Address Temp; |