From: Francois Pichet Date: Thu, 22 Sep 2011 22:14:56 +0000 (+0000) Subject: [microsoft] Fix a bug in -fdelayed-template-parsing mode where we were not reentering... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fdde47061d8ff0b481414ccf69ebdc47c2216274;p=clang [microsoft] Fix a bug in -fdelayed-template-parsing mode where we were not reentering the delayed function context correctly. The problem was that all template params were reintroduced inside the same scope. So if we had a situation where we had 2 template params with the same name at different scope then clang would generate an error about ambiguous name. The solution is to create a new ParseScope(Scope::TemplateParamScope) for each template scope that we want to reenter. (from the outmost to the innermost scope) This fixes some errors when parsing MFC code with clang. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140344 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index f9fe3563b4..955f61c492 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -1159,6 +1159,7 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) { FD = cast(LMT.D); // Reinject the template parameters. + SmallVector TemplateParamScopeStack; DeclaratorDecl* Declarator = dyn_cast(FD); if (Declarator && Declarator->getNumTemplateParameterLists() != 0) { Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator); @@ -1166,17 +1167,31 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) { } else { Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D); + // Get the list of DeclContext to reenter. + SmallVector DeclContextToReenter; DeclContext *DD = FD->getLexicalParent(); while (DD && DD->isRecord()) { - if (ClassTemplatePartialSpecializationDecl* MD = - dyn_cast_or_null(DD)) - Actions.ActOnReenterTemplateScope(getCurScope(), MD); - else if (CXXRecordDecl* MD = dyn_cast_or_null(DD)) - Actions.ActOnReenterTemplateScope(getCurScope(), - MD->getDescribedClassTemplate()); - + DeclContextToReenter.push_back(DD); DD = DD->getLexicalParent(); } + + // Reenter the DeclContext from outmost to innermost. + SmallVector::reverse_iterator II = + DeclContextToReenter.rbegin(); + for (; II != DeclContextToReenter.rend(); ++II) { + if (ClassTemplatePartialSpecializationDecl* MD = + dyn_cast_or_null(*II)) { + TemplateParamScopeStack.push_back(new ParseScope(this, + Scope::TemplateParamScope)); + Actions.ActOnReenterTemplateScope(getCurScope(), MD); + } else if (CXXRecordDecl* MD = dyn_cast_or_null(*II)) { + TemplateParamScopeStack.push_back(new ParseScope(this, + Scope::TemplateParamScope, + MD->getDescribedClassTemplate() != 0 )); + Actions.ActOnReenterTemplateScope(getCurScope(), + MD->getDescribedClassTemplate()); + } + } } assert(!LMT.Toks.empty() && "Empty body!"); @@ -1207,21 +1222,25 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) { if (Tok.is(tok::kw_try)) { ParseFunctionTryBlock(LMT.D, FnScope); - return; - } - if (Tok.is(tok::colon)) { - ParseConstructorInitializer(LMT.D); + } else { + if (Tok.is(tok::colon)) + ParseConstructorInitializer(LMT.D); + else + Actions.ActOnDefaultCtorInitializers(LMT.D); - // Error recovery. - if (!Tok.is(tok::l_brace)) { + if (Tok.is(tok::l_brace)) { + ParseFunctionStatementBody(LMT.D, FnScope); + Actions.MarkAsLateParsedTemplate(FD, false); + } else Actions.ActOnFinishFunctionBody(LMT.D, 0); - return; - } - } else - Actions.ActOnDefaultCtorInitializers(LMT.D); + } - ParseFunctionStatementBody(LMT.D, FnScope); - Actions.MarkAsLateParsedTemplate(FD, false); + // Exit scopes. + FnScope.Exit(); + SmallVector::reverse_iterator I = + TemplateParamScopeStack.rbegin(); + for (; I != TemplateParamScopeStack.rend(); ++I) + delete *I; DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D); if (grp) diff --git a/test/Parser/DelayedTemplateParsing.cpp b/test/Parser/DelayedTemplateParsing.cpp index b447fff2f1..b02c4026c0 100644 --- a/test/Parser/DelayedTemplateParsing.cpp +++ b/test/Parser/DelayedTemplateParsing.cpp @@ -1,11 +1,11 @@ -// RUN: %clang_cc1 -fdelayed-template-parsing -fsyntax-only -verify %s +// RUN: %clang_cc1 -fms-extensions -fdelayed-template-parsing -fsyntax-only -verify %s template class A { void foo() { undeclared(); } - void foo2(); + void foo2(); }; template @@ -40,3 +40,22 @@ void undeclared() template void foo5() {} //expected-note {{previous definition is here}} template void foo5() {} // expected-error {{redefinition of 'foo5'}} + + + +namespace Inner_Outer_same_template_param_name { + +template +class Outmost { +public: + template + class Inner { + public: + void f() { + T* var; + } + }; +}; + +} +