[cfe-commits] r58382 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/fntype-decl.cpp test/SemaCXX/overload-call.cpp

Douglas Gregor doug.gregor at gmail.com
Wed Oct 29 08:10:40 PDT 2008


Author: dgregor
Date: Wed Oct 29 10:10:40 2008
New Revision: 58382

URL: http://llvm.org/viewvc/llvm-project?rev=58382&view=rev
Log:
Simplify and correct the check for function redefinitions. This does two things:

  - Allows definitions of overloaded functions :)
  - Eliminates extraneous error messages when we have a definition of a 
    function that isn't an overload but doesn't have exactly the same type
    as the original.
  


Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/fntype-decl.cpp
    cfe/trunk/test/SemaCXX/overload-call.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=58382&r1=58381&r2=58382&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Oct 29 10:10:40 2008
@@ -1761,20 +1761,6 @@
   
   Scope *GlobalScope = FnBodyScope->getParent();
 
-  // See if this is a redefinition.
-  Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
-                             GlobalScope);
-  if (PrevDcl && isDeclInScope(PrevDcl, CurContext)) {
-    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
-      const FunctionDecl *Definition;
-      if (FD->getBody(Definition)) {
-        Diag(D.getIdentifierLoc(), diag::err_redefinition, 
-             D.getIdentifier()->getName());
-        Diag(Definition->getLocation(), diag::err_previous_definition);
-      }
-    }
-  }
-
   return ActOnStartOfFunctionDef(FnBodyScope,
                                  ActOnDeclarator(GlobalScope, D, 0));
 }
@@ -1782,6 +1768,15 @@
 Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) {
   Decl *decl = static_cast<Decl*>(D);
   FunctionDecl *FD = cast<FunctionDecl>(decl);
+
+  // See if this is a redefinition.
+  const FunctionDecl *Definition;
+  if (FD->getBody(Definition)) {
+    Diag(FD->getLocation(), diag::err_redefinition, 
+         FD->getName());
+    Diag(Definition->getLocation(), diag::err_previous_definition);
+  }
+
   PushDeclContext(FD);
     
   // Check the validity of our function parameters

Modified: cfe/trunk/test/SemaCXX/fntype-decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/fntype-decl.cpp?rev=58382&r1=58381&r2=58382&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/fntype-decl.cpp (original)
+++ cfe/trunk/test/SemaCXX/fntype-decl.cpp Wed Oct 29 10:10:40 2008
@@ -2,7 +2,7 @@
 
 // PR2942
 typedef void fn(int);
-fn f;
+fn f; // expected-error{{previous declaration is here}}
 
 int g(int x, int y);
 int g(int x, int y = 2);
@@ -10,6 +10,11 @@
 typedef int g_type(int, int);
 g_type g;
 
-int h(int x) {
+int h(int x) { // expected-error{{previous definition is here}}
   return g(x);
 }
+
+float f(int) { } // expected-error{{functions that differ only in their return type cannot be overloaded}}
+
+int h(int) { } // expected-error{{redefinition of 'h'}}
+

Modified: cfe/trunk/test/SemaCXX/overload-call.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overload-call.cpp?rev=58382&r1=58381&r2=58382&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/overload-call.cpp (original)
+++ cfe/trunk/test/SemaCXX/overload-call.cpp Wed Oct 29 10:10:40 2008
@@ -1,6 +1,6 @@
 // RUN: clang -fsyntax-only -pedantic -verify %s 
-int* f(int);
-float* f(float);
+int* f(int) { return 0; }
+float* f(float) { return 0; }
 void f();
 
 void test_f(int iv, float fv) {





More information about the cfe-commits mailing list