r216638 - Properly handle multiple nonnull attributes in CodeGen

Alexey Samsonov vonosmas at gmail.com
Wed Aug 27 17:53:21 PDT 2014


Author: samsonov
Date: Wed Aug 27 19:53:20 2014
New Revision: 216638

URL: http://llvm.org/viewvc/llvm-project?rev=216638&view=rev
Log:
Properly handle multiple nonnull attributes in CodeGen

Modified:
    cfe/trunk/lib/CodeGen/CGCall.cpp
    cfe/trunk/test/CodeGen/nonnull.c

Modified: cfe/trunk/lib/CodeGen/CGCall.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=216638&r1=216637&r2=216638&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCall.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCall.cpp Wed Aug 27 19:53:20 2014
@@ -1419,6 +1419,30 @@ static llvm::Value *emitArgumentDemotion
   return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
 }
 
+static bool shouldAddNonNullAttr(const Decl *FD, const ParmVarDecl *PVD) {
+  // 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 (!PVD->getType()->isAnyPointerType() &&
+      !PVD->getType()->isBlockPointerType())
+    return false;
+  // First, check attribute on parameter itself.
+  if (PVD->hasAttr<NonNullAttr>())
+    return true;
+  // Check function attributes.
+  if (!FD)
+    return false;
+  for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
+    if (NNAttr->isNonNull(PVD->getFunctionScopeIndex()))
+      return true;
+  }
+  return false;
+}
+
 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
                                          llvm::Function *Fn,
                                          const FunctionArgList &Args) {
@@ -1463,10 +1487,6 @@ void CodeGenFunction::EmitFunctionProlog
                                         llvm::Attribute::NoAlias));
   }
 
-  // Get the function-level nonnull attribute if it exists.
-  const NonNullAttr *NNAtt =
-    CurCodeDecl ? CurCodeDecl->getAttr<NonNullAttr>() : nullptr;
-
   // Track if we received the parameter as a pointer (indirect, byval, or
   // inalloca).  If already have a pointer, EmitParmDecl doesn't need to copy it
   // into a local alloca for us.
@@ -1557,17 +1577,7 @@ void CodeGenFunction::EmitFunctionProlog
         llvm::Value *V = AI;
 
         if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
-          // 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 (((NNAtt && NNAtt->isNonNull(PVD->getFunctionScopeIndex())) ||
-               PVD->hasAttr<NonNullAttr>()) &&
-              (PVD->getType()->isAnyPointerType() ||
-               PVD->getType()->isBlockPointerType()))
+          if (shouldAddNonNullAttr(CurCodeDecl, PVD))
             AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
                                                 AI->getArgNo() + 1,
                                                 llvm::Attribute::NonNull));

Modified: cfe/trunk/test/CodeGen/nonnull.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/nonnull.c?rev=216638&r1=216637&r2=216638&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/nonnull.c (original)
+++ cfe/trunk/test/CodeGen/nonnull.c Wed Aug 27 19:53:20 2014
@@ -41,3 +41,11 @@ typedef union {
 int bar6(TransparentUnion tu) __attribute__((nonnull(1))) {
   return *tu.p;
 }
+
+// CHECK: define void @bar7(i32* nonnull %a, i32* nonnull %b)
+void bar7(int *a, int *b) __attribute__((nonnull(1)))
+__attribute__((nonnull(2))) {}
+
+// CHECK: define void @bar8(i32* nonnull %a, i32* nonnull %b)
+void bar8(int *a, int *b) __attribute__((nonnull))
+__attribute__((nonnull(1))) {}





More information about the cfe-commits mailing list