r185779 - Sema: Do not merge new decls with invalid, old decls

David Majnemer david.majnemer at gmail.com
Sun Jul 7 16:49:51 PDT 2013


Author: majnemer
Date: Sun Jul  7 18:49:50 2013
New Revision: 185779

URL: http://llvm.org/viewvc/llvm-project?rev=185779&view=rev
Log:
Sema: Do not merge new decls with invalid, old decls

Sema::MergeFunctionDecl attempts merging two decls even if the old decl
is invalid.  This can lead to interesting circumstances where we
successfully merge the decls but the result makes no sense.

Take the following for example:

template <typename T>
int main(void);

int main(void);

Sema will not consider these to be overloads of the same name because
main can't be overloaded, which means that this must be a redeclaration.

In this case the templated decl is compatible with the non-templated
decl allowing the Sema::CheckFunctionDeclaration machinery to move on
and do bizarre things like setting the previous decl of a non-templated
decl to a templated decl!

The way I see it, we should just bail from MergeFunctionDecl if the old
decl is invalid.

This fixes PR16531.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp
    cfe/trunk/test/Parser/DelayedTemplateParsing.cpp
    cfe/trunk/test/Sema/warn-main.c
    cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=185779&r1=185778&r2=185779&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sun Jul  7 18:49:50 2013
@@ -2305,6 +2305,10 @@ bool Sema::MergeFunctionDecl(FunctionDec
     return true;
   }
 
+  // If the old declaration is invalid, just give up here.
+  if (Old->isInvalidDecl())
+    return true;
+
   // Determine whether the previous declaration was a definition,
   // implicit declaration, or a declaration.
   diag::kind PrevDiag;

Modified: cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp?rev=185779&r1=185778&r2=185779&view=diff
==============================================================================
--- cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp (original)
+++ cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp Sun Jul  7 18:49:50 2013
@@ -15,8 +15,8 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST10
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST11
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST12
-// RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST12
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST13
+// RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST14
 
 #if TEST1
 
@@ -103,6 +103,13 @@ int main(void) {}
 template <typename T>
 int main(void); // expected-error{{'main' cannot be a template}}
 
+#elif TEST14
+
+template <typename T>
+int main(void); // expected-error{{'main' cannot be a template}}
+
+int main(void) {}
+
 #else
 
 #error Unknown test mode

Modified: cfe/trunk/test/Parser/DelayedTemplateParsing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/DelayedTemplateParsing.cpp?rev=185779&r1=185778&r2=185779&view=diff
==============================================================================
--- cfe/trunk/test/Parser/DelayedTemplateParsing.cpp (original)
+++ cfe/trunk/test/Parser/DelayedTemplateParsing.cpp Sun Jul  7 18:49:50 2013
@@ -11,7 +11,8 @@ class A {
 template <class T>
 class B {
    void foo4() { } // expected-note {{previous definition is here}}  expected-note {{previous definition is here}}
-   void foo4() { } // expected-error {{class member cannot be redeclared}} expected-error {{redefinition of 'foo4'}}  expected-note {{previous definition is here}}
+   void foo4() { } // expected-error {{class member cannot be redeclared}} expected-error {{redefinition of 'foo4'}}
+   void foo5() { } // expected-note {{previous definition is here}}
 
    friend void foo3() {
        undeclared();
@@ -20,7 +21,7 @@ class B {
 
 
 template <class T>
-void B<T>::foo4() {// expected-error {{redefinition of 'foo4'}}
+void B<T>::foo5() { // expected-error {{redefinition of 'foo5'}}
 }
 
 template <class T>

Modified: cfe/trunk/test/Sema/warn-main.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-main.c?rev=185779&r1=185778&r2=185779&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-main.c (original)
+++ cfe/trunk/test/Sema/warn-main.c Sun Jul  7 18:49:50 2013
@@ -14,16 +14,14 @@ static int main() {
   return 0;
 }
 
-// expected-error at +3 {{redefinition of 'main'}}
-// expected-error at +2 {{'main' is not allowed to be declared inline}}
-// expected-note at +1 {{previous definition is here}}
+// expected-error at +2 {{redefinition of 'main'}}
+// expected-error at +1 {{'main' is not allowed to be declared inline}}
 inline int main() {
 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:1-[[@LINE-1]]:8}:""
   return 0;
 }
 
-// expected-warning at +6 {{function 'main' declared 'noreturn' should not return}}
-// expected-error at +3 {{redefinition of 'main'}}
+// expected-warning at +5 {{function 'main' declared 'noreturn' should not return}}
 // expected-warning at +2 {{'main' is not allowed to be declared _Noreturn}}
 // expected-note at +1 {{remove '_Noreturn'}}
 _Noreturn int main() {

Modified: cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp?rev=185779&r1=185778&r2=185779&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp (original)
+++ cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp Sun Jul  7 18:49:50 2013
@@ -22,14 +22,10 @@ void f0a(T x, __typeof__(f(N)) y) { } //
 void f(int);
 
 template<typename T, T N>
-void f0a(T x, __typeof__(f(N)) y) { } // expected-error{{redefinition}} \
-                                      // expected-note{{previous}}
+void f0a(T x, __typeof__(f(N)) y) { } // expected-error{{redefinition}}
 
 void f(float);
 
-template<typename T, T N>
-void f0a(T x, __typeof__(f(N)) y) { } // expected-error{{redefinition}}
-
 // Test dependently-sized array canonicalization
 template<typename T, int N, int M>
 void f1(T (&array)[N + M]) { } // expected-note{{previous}}





More information about the cfe-commits mailing list