]> granicus.if.org Git - clang/commitdiff
Rework the way we handle constructor decls to be less hacky and fix PR3948 completely.
authorAnders Carlsson <andersca@mac.com>
Thu, 30 Apr 2009 22:41:11 +0000 (22:41 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 30 Apr 2009 22:41:11 +0000 (22:41 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70516 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseDecl.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/constructor.cpp

index 59e9ed6acbefc509e466a378a6ee21d086071bfb..8b81c312d2e015e9c8e0f3104d93b849620f0b3d 100644 (file)
@@ -1982,7 +1982,17 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
 
       if (Tok.is(tok::identifier)) {
         assert(Tok.getIdentifierInfo() && "Not an identifier?");
-        D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
+
+        // If this identifier is the name of the current class, it's a
+        // constructor name. 
+        if (!D.getDeclSpec().hasTypeSpecifier() &&
+            Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
+          D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
+                                               Tok.getLocation(), CurScope),
+                           Tok.getLocation());
+        // This is a normal identifier.
+        } else
+          D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
         ConsumeToken();
         goto PastIdentifier;
       } else if (Tok.is(tok::annot_template_id)) {
@@ -2092,15 +2102,6 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
           break;
       }
       ParseFunctionDeclarator(ConsumeParen(), D);
-      
-      // If this identifier is the name of the current class, it's a
-      // constructor name. 
-      if (IdentifierInfo *II = D.getIdentifier()) {
-        if (Actions.isCurrentClassName(*II, CurScope))
-        D.setConstructor(Actions.getTypeName(*II, D.getIdentifierLoc(), 
-                                             CurScope), 
-                         D.getIdentifierLoc());
-      } 
     } else if (Tok.is(tok::l_square)) {
       ParseBracketDeclarator(D);
     } else {
index 2381c62575a5ca8caf06475b7d218fb170fd5036..37f4985ebe8f31ab9b15ce0dd8c22274384d6d40 100644 (file)
@@ -2035,6 +2035,18 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
       
     isVirtualOkay = true;
   } else if (DC->isRecord()) {
+    // If the of the function is the same as the name of the record, then this
+    // must be an invalid constructor that has a return type. 
+    // (The parser checks for a return type and makes the declarator a 
+    // constructor if it has no return type).
+    // must have an invalid constructor that has a return type 
+    if (Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){
+      Diag(D.getIdentifierLoc(), diag::err_constructor_return_type)
+        << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc())
+        << SourceRange(D.getIdentifierLoc());
+      return 0;
+    }
+    
     // This is a C++ method declaration.
     NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC),
                                   D.getIdentifierLoc(), Name, R,
index e9b7fb04ceb67294ab4a398a4aecdc3c5bd20640..6e00488c509f1d681c1fbc7235393e099753ccd8 100644 (file)
@@ -1294,19 +1294,6 @@ QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R,
     D.setInvalidType();
     SC = FunctionDecl::None;
   }
-  if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) {
-    // Constructors don't have return types, but the parser will
-    // happily parse something like:
-    //
-    //   class X {
-    //     float X(float);
-    //   };
-    //
-    // The return type will be eliminated later.
-    Diag(D.getIdentifierLoc(), diag::err_constructor_return_type)
-      << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc())
-      << SourceRange(D.getIdentifierLoc());
-  }
   
   DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
   if (FTI.TypeQuals != 0) {
index 1429cc64527797774de01a4e41edec9c0d3caaa5..8f289a2b1e961149b54590d117c8cba50aea3308 100644 (file)
@@ -45,3 +45,16 @@ struct Length {
 struct mmst_reg{
  char mmst_reg[10];
 };
+
+// PR3948
+namespace PR3948 {
+// PR3948
+class a {
+  public:
+  int b(int a());
+};
+int x();
+void y() {
+  a z; z.b(x);
+}
+}