r214507 - Improving diagnostic source ranges for the nonnull attribute. Now it highlights the attribute and the faulty nonpointer type when possible.

David Blaikie dblaikie at gmail.com
Fri Aug 1 10:01:18 PDT 2014


On Fri, Aug 1, 2014 at 5:58 AM, Aaron Ballman <aaron at aaronballman.com> wrote:
> Author: aaronballman
> Date: Fri Aug  1 07:58:11 2014
> New Revision: 214507
>
> URL: http://llvm.org/viewvc/llvm-project?rev=214507&view=rev
> Log:
> Improving diagnostic source ranges for the nonnull attribute. Now it highlights the attribute and the faulty nonpointer type when possible.
>
> Modified:
>     cfe/trunk/include/clang/AST/DeclObjC.h
>     cfe/trunk/lib/AST/DeclObjC.cpp
>     cfe/trunk/lib/Sema/SemaDeclAttr.cpp
>
> Modified: cfe/trunk/include/clang/AST/DeclObjC.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=214507&r1=214506&r2=214507&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/AST/DeclObjC.h (original)
> +++ cfe/trunk/include/clang/AST/DeclObjC.h Fri Aug  1 07:58:11 2014
> @@ -329,6 +329,7 @@ public:
>
>    QualType getReturnType() const { return MethodDeclType; }
>    void setReturnType(QualType T) { MethodDeclType = T; }
> +  SourceRange getReturnTypeSourceRange() const;
>
>    /// \brief Determine the type of an expression that sends a message to this
>    /// function.
>
> Modified: cfe/trunk/lib/AST/DeclObjC.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=214507&r1=214506&r2=214507&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/DeclObjC.cpp (original)
> +++ cfe/trunk/lib/AST/DeclObjC.cpp Fri Aug  1 07:58:11 2014
> @@ -952,6 +952,13 @@ ObjCInterfaceDecl *ObjCMethodDecl::getCl
>    llvm_unreachable("unknown method context");
>  }
>
> +SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
> +  const auto *TSI = getReturnTypeSourceInfo();
> +  if (TSI)
> +    return TSI->getTypeLoc().getSourceRange();
> +  return SourceRange();
> +}
> +
>  static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
>                                              const ObjCMethodDecl *Method,
>                                 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
>
> Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=214507&r1=214506&r2=214507&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Fri Aug  1 07:58:11 2014
> @@ -88,12 +88,30 @@ static QualType getFunctionOrMethodParam
>    return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
>  }
>
> +static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
> +  if (const auto *FD = dyn_cast<FunctionDecl>(D))
> +    return FD->getParamDecl(Idx)->getSourceRange();
> +  else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))

Drop the else(s) after return.

> +    return MD->parameters()[Idx]->getSourceRange();
> +  else if (const auto *BD = dyn_cast<BlockDecl>(D))
> +    return BD->getParamDecl(Idx)->getSourceRange();
> +  return SourceRange();
> +}
> +
>  static QualType getFunctionOrMethodResultType(const Decl *D) {
>    if (const FunctionType *FnTy = D->getFunctionType())
>      return cast<FunctionType>(FnTy)->getReturnType();
>    return cast<ObjCMethodDecl>(D)->getReturnType();
>  }
>
> +static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
> +  if (const auto *FD = dyn_cast<FunctionDecl>(D))
> +    return FD->getReturnTypeSourceRange();
> +  else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
> +    return MD->getReturnTypeSourceRange();
> +  return SourceRange();
> +}
> +
>  static bool isFunctionOrMethodVariadic(const Decl *D) {
>    if (const FunctionType *FnTy = D->getFunctionType()) {
>      const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
> @@ -1122,15 +1140,17 @@ static void possibleTransparentUnionPoin
>  }
>
>  static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
> -                                SourceRange R, bool isReturnValue = false) {
> +                                SourceRange AttrParmRange,
> +                                SourceRange NonNullTypeRange,
> +                                bool isReturnValue = false) {
>    T = T.getNonReferenceType();
>    possibleTransparentUnionPointerType(T);
>
>    if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
> -    S.Diag(Attr.getLoc(),
> -           isReturnValue ? diag::warn_attribute_return_pointers_only
> -                         : diag::warn_attribute_pointers_only)
> -      << Attr.getName() << R;
> +    S.Diag(Attr.getLoc(), isReturnValue
> +                              ? diag::warn_attribute_return_pointers_only
> +                              : diag::warn_attribute_pointers_only)
> +        << Attr.getName() << AttrParmRange << NonNullTypeRange;
>      return false;
>    }
>    return true;
> @@ -1145,9 +1165,9 @@ static void handleNonNullAttr(Sema &S, D
>        return;
>
>      // Is the function argument a pointer type?
> -    // FIXME: Should also highlight argument in decl in the diagnostic.
>      if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
> -                             Ex->getSourceRange()))
> +                             Ex->getSourceRange(),
> +                             getFunctionOrMethodParamRange(D, Idx)))
>        continue;
>
>      NonNullArgs.push_back(Idx);
> @@ -1194,7 +1214,8 @@ static void handleNonNullAttrParameter(S
>    }
>
>    // Is the argument a pointer type?
> -  if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
> +  if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
> +                           D->getSourceRange()))
>      return;
>
>    D->addAttr(::new (S.Context)
> @@ -1205,7 +1226,8 @@ static void handleNonNullAttrParameter(S
>  static void handleReturnsNonNullAttr(Sema &S, Decl *D,
>                                       const AttributeList &Attr) {
>    QualType ResultType = getFunctionOrMethodResultType(D);
> -  if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
> +  SourceRange SR = getFunctionOrMethodResultSourceRange(D);
> +  if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
>                             /* isReturnValue */ true))
>      return;
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list