r224039 - When checking for nonnull parameter attributes, also check the ParmVarDecl since the attribute may reside there, instead of just on the FunctionDecl. Fixes PR21668.

Aaron Ballman aaron at aaronballman.com
Thu Dec 11 11:35:43 PST 2014


Author: aaronballman
Date: Thu Dec 11 13:35:42 2014
New Revision: 224039

URL: http://llvm.org/viewvc/llvm-project?rev=224039&view=rev
Log:
When checking for nonnull parameter attributes, also check the ParmVarDecl since the attribute may reside there, instead of just on the FunctionDecl. Fixes PR21668.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/nonnull.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=224039&r1=224038&r2=224039&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Dec 11 13:35:42 2014
@@ -6766,7 +6766,8 @@ void Sema::DiagnoseAlwaysNonNullPointer(
         }
         if (!AttrNonNull.empty())
           for (unsigned i = 0; i < NumArgs; ++i)
-            if (FD->getParamDecl(i) == PV && AttrNonNull[i]) {
+            if (FD->getParamDecl(i) == PV &&
+                (AttrNonNull[i] || PV->hasAttr<NonNullAttr>())) {
               std::string Str;
               llvm::raw_string_ostream S(Str);
               E->printPretty(S, nullptr, getPrintingPolicy());

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=224039&r1=224038&r2=224039&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Dec 11 13:35:42 2014
@@ -9243,7 +9243,7 @@ static void RecordModifiableNonNullParam
   if (!Param)
     return;
   if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(Param->getDeclContext()))
-    if (!FD->hasAttr<NonNullAttr>())
+    if (!FD->hasAttr<NonNullAttr>() && !Param->hasAttr<NonNullAttr>())
       return;
   if (FunctionScopeInfo *FD = S.getCurFunction())
     if (!FD->ModifiedNonNullParams.count(Param))

Modified: cfe/trunk/test/Sema/nonnull.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/nonnull.c?rev=224039&r1=224038&r2=224039&view=diff
==============================================================================
--- cfe/trunk/test/Sema/nonnull.c (original)
+++ cfe/trunk/test/Sema/nonnull.c Thu Dec 11 13:35:42 2014
@@ -141,3 +141,15 @@ void yet_another_evil_nonnull_func(int*
  }
 }
 
+void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) {
+  if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
+    ;
+  if (s) // No warning
+    ;
+}
+
+void pr21668_2(__attribute__((nonnull)) const char *p) {
+  p = 0;
+  if (p) // No warning
+    ;
+}





More information about the cfe-commits mailing list