r185745 - Sema: Fix a crash when main is redeclared as a function-template.

David Majnemer david.majnemer at gmail.com
Fri Jul 5 19:13:46 PDT 2013


Author: majnemer
Date: Fri Jul  5 21:13:46 2013
New Revision: 185745

URL: http://llvm.org/viewvc/llvm-project?rev=185745&view=rev
Log:
Sema: Fix a crash when main is redeclared as a function-template.

This boils down to us sending invalid function decls to
CheckFunctionDeclaration becauswe we did not consider that CheckMain
could cause the decl to be invalid.  Instead, interogate the new decl's
main-validity and *then* send it over to get CheckFunctionDeclaration'd
if it was still valid after calling CheckMain.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/CXX/basic/basic.start/basic.start.main/p2.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=185745&r1=185744&r2=185745&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jul  5 21:13:46 2013
@@ -6473,12 +6473,12 @@ Sema::ActOnFunctionDeclarator(Scope *S,
   if (!getLangOpts().CPlusPlus) {
     // Perform semantic checking on the function declaration.
     bool isExplicitSpecialization=false;
-    if (!NewFD->isInvalidDecl()) {
-      if (NewFD->isMain())
-        CheckMain(NewFD, D.getDeclSpec());
+    if (!NewFD->isInvalidDecl() && NewFD->isMain())
+      CheckMain(NewFD, D.getDeclSpec());
+
+    if (!NewFD->isInvalidDecl())
       D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous,
                                                   isExplicitSpecialization));
-    }
     // Make graceful recovery from an invalid redeclaration.
     else if (!Previous.empty())
            D.setRedeclaration(true);
@@ -6590,17 +6590,17 @@ Sema::ActOnFunctionDeclarator(Scope *S,
 
     // Perform semantic checking on the function declaration.
     if (!isDependentClassScopeExplicitSpecialization) {
+      if (!NewFD->isInvalidDecl() && NewFD->isMain())
+        CheckMain(NewFD, D.getDeclSpec());
+
       if (NewFD->isInvalidDecl()) {
         // If this is a class member, mark the class invalid immediately.
         // This avoids some consistency errors later.
         if (CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(NewFD))
           methodDecl->getParent()->setInvalidDecl();
-      } else {
-        if (NewFD->isMain()) 
-          CheckMain(NewFD, D.getDeclSpec());
+      } else
         D.setRedeclaration(CheckFunctionDeclaration(S, NewFD, Previous,
                                                     isExplicitSpecialization));
-      }
     }
 
     assert((NewFD->isInvalidDecl() || !D.isRedeclaration() ||

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=185745&r1=185744&r2=185745&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 Fri Jul  5 21:13:46 2013
@@ -15,6 +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
 
 #if TEST1
 
@@ -94,6 +96,13 @@ int main(int, charT* const *) {}
 typedef char charT;
 int main(int, const charT* const *) {}
 
+#elif TEST13
+
+int main(void) {}
+
+template <typename T>
+int main(void); // expected-error{{'main' cannot be a template}}
+
 #else
 
 #error Unknown test mode





More information about the cfe-commits mailing list