Sema: An extern declaration can't be a redeclaration of a parameter

In the following:
void f(int x) { extern int x; }

The second declaration of 'x' shouldn't be considered a redeclaration of
the parameter.

This is a different approach to r225780.

llvm-svn: 225875
This commit is contained in:
David Majnemer 2015-01-14 00:31:13 +00:00
parent 11f5032368
commit a3b04cea04
3 changed files with 11 additions and 2 deletions

View File

@ -906,6 +906,11 @@ public:
return false;
}
/// \brief Similar to isLocalVarDecl but also includes parameters.
bool isLocalVarDeclOrParm() const {
return isLocalVarDecl() || getKind() == Decl::ParmVar;
}
/// isFunctionOrMethodVarDecl - Similar to isLocalVarDecl, but
/// excludes variables declared in blocks.
bool isFunctionOrMethodVarDecl() const {

View File

@ -3285,12 +3285,12 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
// Check if extern is followed by non-extern and vice-versa.
if (New->hasExternalStorage() &&
!Old->hasLinkage() && Old->isLocalVarDecl()) {
!Old->hasLinkage() && Old->isLocalVarDeclOrParm()) {
Diag(New->getLocation(), diag::err_extern_non_extern) << New->getDeclName();
Diag(OldLocation, PrevDiag);
return New->setInvalidDecl();
}
if (Old->hasLinkage() && New->isLocalVarDecl() &&
if (Old->hasLinkage() && New->isLocalVarDeclOrParm() &&
!New->hasExternalStorage()) {
Diag(New->getLocation(), diag::err_non_extern_extern) << New->getDeclName();
Diag(OldLocation, PrevDiag);

View File

@ -60,3 +60,7 @@ int *p=&g19; // expected-error{{use of undeclared identifier 'g19'}} \
static int a;
extern int a; // expected-note {{previous declaration is here}}
int a; // expected-error {{non-static declaration of 'a' follows static declaration}}
void f(int x) { // expected-note {{previous definition is here}}
extern int x; // expected-error {{extern declaration of 'x' follows non-extern declaration}}
}