[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-commits
Subject: [PATCH] D19763: Functions declared in a scope should not hide previous declaration in earlier scopes
From: Olivier Goffart via cfe-commits <cfe-commits () lists ! llvm ! org>
Date: 2016-04-30 11:42:37
Message-ID: differential-rev-PHID-DREV-kklxgn4k4umrcvjzyks3-req () reviews ! llvm ! org
[Download RAW message or body]
ogoffart created this revision.
ogoffart added reviewers: cfe-commits, rsmith.
This code should be an error:
void foo(int);
void f3() {
int foo(float);
{
float foo(int); // expected-error {{functions that differ only in their return \
type cannot be overloaded}} }
}
the foo(float) function declared at function scope should not hide the float(int) \
while trying to redeclare functions.
http://reviews.llvm.org/D19763
Files:
lib/Sema/SemaLookup.cpp
test/SemaCXX/function-redecl.cpp
Index: test/SemaCXX/function-redecl.cpp
===================================================================
--- test/SemaCXX/function-redecl.cpp
+++ test/SemaCXX/function-redecl.cpp
@@ -7,7 +7,7 @@
void bar(int); // expected-note 2{{previous declaration is here}}
}
- void foo(int); // expected-note 2{{previous declaration is here}}
+ void foo(int); // expected-note 3{{previous declaration is here}}
void f2() {
int foo(int); // expected-error {{functions that differ only in their return \
type cannot be overloaded}} @@ -25,6 +25,13 @@
}
}
}
+
+ void f3() {
+ int foo(float);
+ {
+ float foo(int); // expected-error {{functions that differ only in their return \
type cannot be overloaded}} + }
+ }
}
class A {
Index: lib/Sema/SemaLookup.cpp
===================================================================
--- lib/Sema/SemaLookup.cpp
+++ lib/Sema/SemaLookup.cpp
@@ -1091,23 +1091,25 @@
bool Found = false;
for (; I != IEnd && S->isDeclScope(*I); ++I) {
if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
- if (NameKind == LookupRedeclarationWithLinkage) {
- // Determine whether this (or a previous) declaration is
- // out-of-scope.
- if (!LeftStartingScope && !Initial->isDeclScope(*I))
- LeftStartingScope = true;
-
- // If we found something outside of our starting scope that
- // does not have linkage, skip it. If it's a template parameter,
+ if (NameKind == LookupRedeclarationWithLinkage &&
+ !(*I)->isTemplateParameter()) {
+ // If it's a template parameter,
// we still find it, so we can diagnose the invalid redeclaration.
- if (LeftStartingScope && !((*I)->hasLinkage()) &&
- !(*I)->isTemplateParameter()) {
+
+ // Determine whether this (or a previous) declaration is
+ // out-of-scope.
+ if (!LeftStartingScope && !Initial->isDeclScope(*I))
+ LeftStartingScope = true;
+
+ // If we found something outside of our starting scope that
+ // does not have linkage, skip it.
+ if (LeftStartingScope && !((*I)->hasLinkage())) {
R.setShadowed();
continue;
}
+ } else {
+ Found = true;
}
-
- Found = true;
R.addDecl(ND);
}
}
["D19763.55712.patch" (text/x-patch)]
Index: test/SemaCXX/function-redecl.cpp
===================================================================
--- test/SemaCXX/function-redecl.cpp
+++ test/SemaCXX/function-redecl.cpp
@@ -7,7 +7,7 @@
void bar(int); // expected-note 2{{previous declaration is here}}
}
- void foo(int); // expected-note 2{{previous declaration is here}}
+ void foo(int); // expected-note 3{{previous declaration is here}}
void f2() {
int foo(int); // expected-error {{functions that differ only in their return \
type cannot be overloaded}} @@ -25,6 +25,13 @@
}
}
}
+
+ void f3() {
+ int foo(float);
+ {
+ float foo(int); // expected-error {{functions that differ only in their return \
type cannot be overloaded}} + }
+ }
}
class A {
Index: lib/Sema/SemaLookup.cpp
===================================================================
--- lib/Sema/SemaLookup.cpp
+++ lib/Sema/SemaLookup.cpp
@@ -1091,23 +1091,25 @@
bool Found = false;
for (; I != IEnd && S->isDeclScope(*I); ++I) {
if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
- if (NameKind == LookupRedeclarationWithLinkage) {
- // Determine whether this (or a previous) declaration is
- // out-of-scope.
- if (!LeftStartingScope && !Initial->isDeclScope(*I))
- LeftStartingScope = true;
-
- // If we found something outside of our starting scope that
- // does not have linkage, skip it. If it's a template parameter,
+ if (NameKind == LookupRedeclarationWithLinkage &&
+ !(*I)->isTemplateParameter()) {
+ // If it's a template parameter,
// we still find it, so we can diagnose the invalid redeclaration.
- if (LeftStartingScope && !((*I)->hasLinkage()) &&
- !(*I)->isTemplateParameter()) {
+
+ // Determine whether this (or a previous) declaration is
+ // out-of-scope.
+ if (!LeftStartingScope && !Initial->isDeclScope(*I))
+ LeftStartingScope = true;
+
+ // If we found something outside of our starting scope that
+ // does not have linkage, skip it.
+ if (LeftStartingScope && !((*I)->hasLinkage())) {
R.setShadowed();
continue;
}
+ } else {
+ Found = true;
}
-
- Found = true;
R.addDecl(ND);
}
}
[Attachment #4 (text/plain)]
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic