[llvm-branch-commits] [cfe-branch] r218644 - Merging r216619:

Hans Wennborg hans at hanshq.net
Mon Sep 29 16:33:05 PDT 2014


Author: hans
Date: Mon Sep 29 18:33:05 2014
New Revision: 218644

URL: http://llvm.org/viewvc/llvm-project?rev=218644&view=rev
Log:
Merging r216619:
------------------------------------------------------------------------
r216619 | hans | 2014-08-27 14:27:40 -0700 (Wed, 27 Aug 2014) | 7 lines

Allow adding dll attributes on certain redecls with a warning if the decl hasn't been used yet (PR20746)

This shouldn't really be allowed, but it comes up in real code (see PR). As
long as the decl hasn't been used there's no technical difficulty in supporting
it, so downgrade the error to a warning.

Differential Revision: http://reviews.llvm.org/D5087
------------------------------------------------------------------------

Modified:
    cfe/branches/release_35/   (props changed)
    cfe/branches/release_35/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/branches/release_35/lib/Sema/SemaDecl.cpp
    cfe/branches/release_35/test/Analysis/MismatchedDeallocator-checker-test.mm   (props changed)
    cfe/branches/release_35/test/Analysis/NewDelete-checker-test.cpp   (props changed)
    cfe/branches/release_35/test/Sema/dllexport.c
    cfe/branches/release_35/test/Sema/dllimport.c
    cfe/branches/release_35/test/SemaCXX/dllexport.cpp
    cfe/branches/release_35/test/SemaCXX/dllimport.cpp
    cfe/branches/release_35/test/SemaCXX/warn-unreachable.cpp   (props changed)

Propchange: cfe/branches/release_35/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Sep 29 18:33:05 2014
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:213609,213611,213613,213741,213834,213840,213902,213912-213913,213993,213998-213999,214008,214050,214060,214119,214208,214222,214369,214390,214471,214734-214735,214777,215046,215229,215245,215609,215618,215806-215808,216531,216572
+/cfe/trunk:213609,213611,213613,213741,213834,213840,213902,213912-213913,213993,213998-213999,214008,214050,214060,214119,214208,214222,214369,214390,214471,214734-214735,214777,215046,215229,215245,215609,215618,215806-215808,216531,216572,216619
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_35/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/include/clang/Basic/DiagnosticSemaKinds.td?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/branches/release_35/include/clang/Basic/DiagnosticSemaKinds.td Mon Sep 29 18:33:05 2014
@@ -2115,6 +2115,9 @@ def warn_attribute_invalid_on_definition
   InGroup<IgnoredAttributes>;
 def err_attribute_dll_redeclaration : Error<
   "redeclaration of %q0 cannot add %q1 attribute">;
+def warn_attribute_dll_redeclaration : Warning<
+  "redeclaration of %q0 should not add %q1 attribute">,
+  InGroup<DiagGroup<"dll-attribute-on-redeclaration">>;
 def err_attribute_dllimport_function_definition : Error<
   "dllimport cannot be applied to non-inline function definition">;
 def err_attribute_dll_deleted : Error<

Modified: cfe/branches/release_35/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/lib/Sema/SemaDecl.cpp?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/lib/Sema/SemaDecl.cpp (original)
+++ cfe/branches/release_35/lib/Sema/SemaDecl.cpp Mon Sep 29 18:33:05 2014
@@ -5020,7 +5020,7 @@ static void checkDLLAttributeRedeclarati
     NewDecl = NewTD->getTemplatedDecl();
 
   if (!OldDecl || !NewDecl)
-      return;
+    return;
 
   const DLLImportAttr *OldImportAttr = OldDecl->getAttr<DLLImportAttr>();
   const DLLExportAttr *OldExportAttr = OldDecl->getAttr<DLLExportAttr>();
@@ -5037,13 +5037,30 @@ static void checkDLLAttributeRedeclarati
   // Implicitly generated declarations are also excluded for now because there
   // is no other way to switch these to use dllimport or dllexport.
   bool AddsAttr = !(OldImportAttr || OldExportAttr) && HasNewAttr;
+
   if (AddsAttr && !IsSpecialization && !OldDecl->isImplicit()) {
-    S.Diag(NewDecl->getLocation(), diag::err_attribute_dll_redeclaration)
-      << NewDecl
-      << (NewImportAttr ? (const Attr *)NewImportAttr : NewExportAttr);
+    // If the declaration hasn't been used yet, allow with a warning for
+    // free functions and global variables.
+    bool JustWarn = false;
+    if (!OldDecl->isUsed() && OldDecl->getDeclContext()->isFileContext()) {
+      auto *VD = dyn_cast<VarDecl>(OldDecl);
+      if (VD && !VD->getDescribedVarTemplate())
+        JustWarn = true;
+      auto *FD = dyn_cast<FunctionDecl>(OldDecl);
+      if (FD && FD->getTemplatedKind() == FunctionDecl::TK_NonTemplate)
+        JustWarn = true;
+    }
+
+    unsigned DiagID = JustWarn ? diag::warn_attribute_dll_redeclaration
+                               : diag::err_attribute_dll_redeclaration;
+    S.Diag(NewDecl->getLocation(), DiagID)
+        << NewDecl
+        << (NewImportAttr ? (const Attr *)NewImportAttr : NewExportAttr);
     S.Diag(OldDecl->getLocation(), diag::note_previous_declaration);
-    NewDecl->setInvalidDecl();
-    return;
+    if (!JustWarn) {
+      NewDecl->setInvalidDecl();
+      return;
+    }
   }
 
   // A redeclaration is not allowed to drop a dllimport attribute, the only

Propchange: cfe/branches/release_35/test/Analysis/MismatchedDeallocator-checker-test.mm
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Sep 29 18:33:05 2014
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite/test/Analysis/alloc-match-dealloc.mm:134693-134817
-/cfe/trunk/test/Analysis/MismatchedDeallocator-checker-test.mm:214777
+/cfe/trunk/test/Analysis/MismatchedDeallocator-checker-test.mm:214777,216619
 /cfe/trunk/test/SemaTemplate/test/Analysis/alloc-match-dealloc.mm:126920
 /cfe/trunk/test/test/Analysis/alloc-match-dealloc.mm:170344

Propchange: cfe/branches/release_35/test/Analysis/NewDelete-checker-test.cpp
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Sep 29 18:33:05 2014
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite/test/Analysis/NewDelete-checker-test.mm:134693-134817
-/cfe/trunk/test/Analysis/NewDelete-checker-test.cpp:214777
+/cfe/trunk/test/Analysis/NewDelete-checker-test.cpp:214777,216619
 /cfe/trunk/test/SemaTemplate/test/Analysis/NewDelete-checker-test.mm:126920
 /cfe/trunk/test/test/Analysis/NewDelete-checker-test.mm:170344

Modified: cfe/branches/release_35/test/Sema/dllexport.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/test/Sema/dllexport.c?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/test/Sema/dllexport.c (original)
+++ cfe/branches/release_35/test/Sema/dllexport.c Mon Sep 29 18:33:05 2014
@@ -39,8 +39,13 @@ __declspec(dllexport) extern int GlobalR
                              int GlobalRedecl2;
 
                       extern int GlobalRedecl3; // expected-note{{previous declaration is here}}
+int useGlobalRedecl3() { return GlobalRedecl3; }
 __declspec(dllexport) extern int GlobalRedecl3; // expected-error{{redeclaration of 'GlobalRedecl3' cannot add 'dllexport' attribute}}
 
+                      extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
+__declspec(dllexport) extern int GlobalRedecl4; // expected-warning{{redeclaration of 'GlobalRedecl4' should not add 'dllexport' attribute}}
+
+
 // External linkage is required.
 __declspec(dllexport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllexport'}}
 
@@ -86,11 +91,18 @@ __declspec(dllexport) void redecl3();
                       void redecl3() {}
 
                       void redecl4(); // expected-note{{previous declaration is here}}
+void useRedecl4() { redecl4(); }
 __declspec(dllexport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllexport' attribute}}
 
                       void redecl5(); // expected-note{{previous declaration is here}}
+void useRedecl5() { redecl5(); }
 __declspec(dllexport) inline void redecl5() {} // expected-error{{redeclaration of 'redecl5' cannot add 'dllexport' attribute}}
 
+// Allow with a warning if the decl hasn't been used yet.
+                      void redecl6(); // expected-note{{previous declaration is here}}
+__declspec(dllexport) void redecl6(); // expected-warning{{redeclaration of 'redecl6' should not add 'dllexport' attribute}}
+
+
 // External linkage is required.
 __declspec(dllexport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllexport'}}
 

Modified: cfe/branches/release_35/test/Sema/dllimport.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/test/Sema/dllimport.c?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/test/Sema/dllimport.c (original)
+++ cfe/branches/release_35/test/Sema/dllimport.c Mon Sep 29 18:33:05 2014
@@ -64,9 +64,16 @@ int GlobalRedecl2c __attribute__((dllimp
 __declspec(dllimport) extern int GlobalRedecl3; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
                       extern int GlobalRedecl3; // expected-warning{{'GlobalRedecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
 
+// Adding an attribute on redeclaration.
                       extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
+int useGlobalRedecl4() { return GlobalRedecl4; }
 __declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
 
+// Allow with a warning if the decl hasn't been used yet.
+                      extern int GlobalRedecl5; // expected-note{{previous declaration is here}}
+__declspec(dllimport) extern int GlobalRedecl5; // expected-warning{{redeclaration of 'GlobalRedecl5' should not add 'dllimport' attribute}}
+
+
 // External linkage is required.
 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
 
@@ -124,14 +131,20 @@ __declspec(dllimport) void redecl3(); //
                       void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
 
                       void redecl4(); // expected-note{{previous declaration is here}}
+void useRedecl4() { redecl4(); }
 __declspec(dllimport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllimport' attribute}}
 
+// Allow with a warning if the decl hasn't been used yet.
+                      void redecl5(); // expected-note{{previous declaration is here}}
+__declspec(dllimport) void redecl5(); // expected-warning{{redeclaration of 'redecl5' should not add 'dllimport' attribute}}
+
+
 // Inline redeclarations are fine.
-__declspec(dllimport) void redecl5();
-                      inline void redecl5() {}
+__declspec(dllimport) void redecl6();
+                      inline void redecl6() {}
 
-                      void redecl6(); // expected-note{{previous declaration is here}}
-__declspec(dllimport) inline void redecl6() {} // expected-error{{redeclaration of 'redecl6' cannot add 'dllimport' attribute}}
+                      void redecl7(); // expected-note{{previous declaration is here}}
+__declspec(dllimport) inline void redecl7() {} // expected-warning{{redeclaration of 'redecl7' should not add 'dllimport' attribute}}
 
 // External linkage is required.
 __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}

Modified: cfe/branches/release_35/test/SemaCXX/dllexport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/test/SemaCXX/dllexport.cpp?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/test/SemaCXX/dllexport.cpp (original)
+++ cfe/branches/release_35/test/SemaCXX/dllexport.cpp Mon Sep 29 18:33:05 2014
@@ -53,7 +53,7 @@ __declspec(dllexport) extern int GlobalR
                              int GlobalRedecl2;
 
                       extern int GlobalRedecl3; // expected-note{{previous declaration is here}}
-__declspec(dllexport) extern int GlobalRedecl3; // expected-error{{redeclaration of 'GlobalRedecl3' cannot add 'dllexport' attribute}}
+__declspec(dllexport) extern int GlobalRedecl3; // expected-warning{{redeclaration of 'GlobalRedecl3' should not add 'dllexport' attribute}}
 
 // External linkage is required.
 __declspec(dllexport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllexport'}}
@@ -186,10 +186,10 @@ __declspec(dllexport) void redecl2();
                       void redecl2() {}
 
                       void redecl3(); // expected-note{{previous declaration is here}}
-__declspec(dllexport) void redecl3(); // expected-error{{redeclaration of 'redecl3' cannot add 'dllexport' attribute}}
+__declspec(dllexport) void redecl3(); // expected-warning{{redeclaration of 'redecl3' should not add 'dllexport' attribute}}
 
                       void redecl4(); // expected-note{{previous declaration is here}}
-__declspec(dllexport) inline void redecl4() {} // expected-error{{redeclaration of 'redecl4' cannot add 'dllexport' attribute}}
+__declspec(dllexport) inline void redecl4() {} // expected-warning{{redeclaration of 'redecl4' should not add 'dllexport' attribute}}
 
 // Friend functions
 struct FuncFriend {
@@ -200,8 +200,8 @@ struct FuncFriend {
 };
 __declspec(dllexport) void friend1() {}
                       void friend2() {}
-__declspec(dllexport) void friend3() {} // expected-error{{redeclaration of 'friend3' cannot add 'dllexport' attribute}}
-__declspec(dllexport) inline void friend4() {} // expected-error{{redeclaration of 'friend4' cannot add 'dllexport' attribute}}
+__declspec(dllexport) void friend3() {} // expected-warning{{redeclaration of 'friend3' should not add 'dllexport' attribute}}
+__declspec(dllexport) inline void friend4() {} // expected-warning{{redeclaration of 'friend4' should not add 'dllexport' attribute}}
 
 // Implicit declarations can be redeclared with dllexport.
 __declspec(dllexport) void* operator new(__SIZE_TYPE__ n);

Modified: cfe/branches/release_35/test/SemaCXX/dllimport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_35/test/SemaCXX/dllimport.cpp?rev=218644&r1=218643&r2=218644&view=diff
==============================================================================
--- cfe/branches/release_35/test/SemaCXX/dllimport.cpp (original)
+++ cfe/branches/release_35/test/SemaCXX/dllimport.cpp Mon Sep 29 18:33:05 2014
@@ -75,7 +75,7 @@ __declspec(dllimport) extern int GlobalR
                       extern int GlobalRedecl3; // expected-warning{{'GlobalRedecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
 
                       extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
-__declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
+__declspec(dllimport) extern int GlobalRedecl4; // expected-warning{{redeclaration of 'GlobalRedecl4' should not add 'dllimport' attribute}}
 
 // External linkage is required.
 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
@@ -224,10 +224,10 @@ __declspec(dllimport) void redecl3(); //
                       void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
 
                       void redecl4(); // expected-note{{previous declaration is here}}
-__declspec(dllimport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllimport' attribute}}
+__declspec(dllimport) void redecl4(); // expected-warning{{redeclaration of 'redecl4' should not add 'dllimport' attribute}}
 
                       void redecl5(); // expected-note{{previous declaration is here}}
-__declspec(dllimport) inline void redecl5() {} // expected-error{{redeclaration of 'redecl5' cannot add 'dllimport' attribute}}
+__declspec(dllimport) inline void redecl5() {} // expected-warning{{redeclaration of 'redecl5' should not add 'dllimport' attribute}}
 
 // Friend functions
 struct FuncFriend {
@@ -240,8 +240,8 @@ struct FuncFriend {
 __declspec(dllimport) void friend1();
                       void friend2(); // expected-warning{{'friend2' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
                       void friend3() {} // expected-warning{{'friend3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
-__declspec(dllimport) void friend4(); // expected-error{{redeclaration of 'friend4' cannot add 'dllimport' attribute}}
-__declspec(dllimport) inline void friend5() {} // expected-error{{redeclaration of 'friend5' cannot add 'dllimport' attribute}}
+__declspec(dllimport) void friend4(); // expected-warning{{redeclaration of 'friend4' should not add 'dllimport' attribute}}
+__declspec(dllimport) inline void friend5() {} // expected-warning{{redeclaration of 'friend5' should not add 'dllimport' attribute}}
 
 // Implicit declarations can be redeclared with dllimport.
 __declspec(dllimport) void* operator new(__SIZE_TYPE__ n);

Propchange: cfe/branches/release_35/test/SemaCXX/warn-unreachable.cpp
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Sep 29 18:33:05 2014
@@ -1,2 +1,2 @@
 /cfe/branches/type-system-rewrite/test/SemaCXX/warn-unreachable.cpp:134693-134817
-/cfe/trunk/test/SemaCXX/warn-unreachable.cpp:121961,214777
+/cfe/trunk/test/SemaCXX/warn-unreachable.cpp:121961,214777,216619





More information about the llvm-branch-commits mailing list