]> granicus.if.org Git - clang/commitdiff
Sema: Do not merge new decls with invalid, old decls
authorDavid Majnemer <david.majnemer@gmail.com>
Sun, 7 Jul 2013 23:49:50 +0000 (23:49 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Sun, 7 Jul 2013 23:49:50 +0000 (23:49 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185779 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2024acc97558b28e7f59087cdb3953ab2f2ec280..5abacca732c126beab7de3336c58a937d77e6b4c 100644 (file)
@@ -2305,6 +2305,10 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, Scope *S) {
     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;
index cd912b834d7b932d655e53307d4f0becd55163e1..5c7d60c1df4c3396c642550f5703b44e9790dc25 100644 (file)
@@ -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
index 77b47239f4c8baae8e9c64bacc05576f65525935..3e429d411621b8ed16bbc3975e8c6586a4ecf986 100644 (file)
@@ -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>
index b67f96125d4aaaccbae65db16525d7336dae0a58..58a6dfde108da90b706b7ebb5dc3a601795e9c5e 100644 (file)
@@ -14,16 +14,14 @@ static int main() {
   return 0;
 }
 
-// expected-error@+3 {{redefinition of 'main'}}
-// expected-error@+2 {{'main' is not allowed to be declared inline}}
-// expected-note@+1 {{previous definition is here}}
+// expected-error@+2 {{redefinition of 'main'}}
+// expected-error@+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@+6 {{function 'main' declared 'noreturn' should not return}}
-// expected-error@+3 {{redefinition of 'main'}}
+// expected-warning@+5 {{function 'main' declared 'noreturn' should not return}}
 // expected-warning@+2 {{'main' is not allowed to be declared _Noreturn}}
 // expected-note@+1 {{remove '_Noreturn'}}
 _Noreturn int main() {
index 7582df5e66aad116bf82577fa14a3dc41c573623..4770c4fa3ed6b0577346772f32a0e58aa12ab864 100644 (file)
@@ -22,14 +22,10 @@ void f0a(T x, __typeof__(f(N)) y) { } // expected-note{{previous}}
 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}}