From: DeLesley Hutchins Date: Wed, 26 Sep 2012 17:57:31 +0000 (+0000) Subject: Fix template instantiation of attributes. More specifically, fix the case X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f39c0c2487d518037e9b4b1cd3b913b2f9ec098e;p=platform%2Fupstream%2Fllvm.git Fix template instantiation of attributes. More specifically, fix the case where an attribute is attached to a forward declaration of a template function, and refers to parameters of that declaration, but is then inherited by the definition of that function. When the definition is instantiated, the parameter references need to be remapped. llvm-svn: 164710 --- diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 621f92b..665dd07 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -2635,8 +2635,25 @@ bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); } + +static const Decl* getCanonicalParmVarDecl(const Decl *D) { + // When storing ParmVarDecls in the local instantiation scope, we always + // want to use the ParmVarDecl from the canonical function declaration, + // since the map is then valid for any redeclaration or definition of that + // function. + if (const ParmVarDecl *PV = dyn_cast(D)) { + if (const FunctionDecl *FD = dyn_cast(PV->getDeclContext())) { + unsigned i = PV->getFunctionScopeIndex(); + return FD->getCanonicalDecl()->getParamDecl(i); + } + } + return D; +} + + llvm::PointerUnion * LocalInstantiationScope::findInstantiationOf(const Decl *D) { + D = getCanonicalParmVarDecl(D); for (LocalInstantiationScope *Current = this; Current; Current = Current->Outer) { @@ -2668,6 +2685,7 @@ LocalInstantiationScope::findInstantiationOf(const Decl *D) { } void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { + D = getCanonicalParmVarDecl(D); llvm::PointerUnion &Stored = LocalDecls[D]; if (Stored.isNull()) Stored = Inst; @@ -2680,11 +2698,13 @@ void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, Decl *Inst) { + D = getCanonicalParmVarDecl(D); DeclArgumentPack *Pack = LocalDecls[D].get(); Pack->push_back(Inst); } void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { + D = getCanonicalParmVarDecl(D); llvm::PointerUnion &Stored = LocalDecls[D]; assert(Stored.isNull() && "Already instantiated this local"); DeclArgumentPack *Pack = new DeclArgumentPack; diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp index fd3577c..a82d265 100644 --- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -3524,3 +3524,82 @@ void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) { } // end namespace VirtualMethodCanonicalizationTest + +namespace TemplateFunctionParamRemapTest { + +template +struct Cell { + T dummy_; + Mutex* mu_; +}; + +class Foo { +public: + template + void elr(Cell* c) __attribute__((exclusive_locks_required(c->mu_))); + + void test(); +}; + +template +void Foo::elr(Cell* c1) { } + +void Foo::test() { + Cell cell; + elr(&cell); // \ + // expected-warning {{calling function 'elr' requires exclusive lock on 'cell.mu_'}} +} + + +template +void globalELR(Cell* c) __attribute__((exclusive_locks_required(c->mu_))); + +template +void globalELR(Cell* c1) { } + +void globalTest() { + Cell cell; + globalELR(&cell); // \ + // expected-warning {{calling function 'globalELR' requires exclusive lock on 'cell.mu_'}} +} + + +template +void globalELR2(Cell* c) __attribute__((exclusive_locks_required(c->mu_))); + +// second declaration +template +void globalELR2(Cell* c2); + +template +void globalELR2(Cell* c3) { } + +// re-declaration after definition +template +void globalELR2(Cell* c4); + +void globalTest2() { + Cell cell; + globalELR2(&cell); // \ + // expected-warning {{calling function 'globalELR2' requires exclusive lock on 'cell.mu_'}} +} + + +template +class FooT { +public: + void elr(Cell* c) __attribute__((exclusive_locks_required(c->mu_))); +}; + +template +void FooT::elr(Cell* c1) { } + +void testFooT() { + Cell cell; + FooT foo; + foo.elr(&cell); // \ + // expected-warning {{calling function 'elr' requires exclusive lock on 'cell.mu_'}} +} + +} // end namespace TemplateFunctionParamRemapTest +