[cfe-commits] r139302 - in /cfe/trunk: include/clang/AST/DeclBase.h include/clang/Sema/Sema.h lib/AST/DeclBase.cpp lib/Parse/ParseDecl.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/SemaCXX/warn-thread-safety-parsing.cpp
Caitlin Sadowski
supertri at google.com
Thu Sep 8 10:42:31 PDT 2011
Author: supertri
Date: Thu Sep 8 12:42:31 2011
New Revision: 139302
URL: http://llvm.org/viewvc/llvm-project?rev=139302&view=rev
Log:
Thread safety: added support for function scopes in attribute arguments.
This patch was written by DeLesley Hutchins.
Modified:
cfe/trunk/include/clang/AST/DeclBase.h
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/AST/DeclBase.cpp
cfe/trunk/lib/Parse/ParseDecl.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp
Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Thu Sep 8 12:42:31 2011
@@ -671,6 +671,9 @@
/// \brief Whether this declaration is a parameter pack.
bool isParameterPack() const;
+ /// \brief returns true if this declaration is a template
+ bool isTemplateDecl() const;
+
/// \brief Whether this declaration is a function or function template.
bool isFunctionOrFunctionTemplate() const;
Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Sep 8 12:42:31 2011
@@ -1244,6 +1244,10 @@
void EnterDeclaratorContext(Scope *S, DeclContext *DC);
void ExitDeclaratorContext(Scope *S);
+ /// Push the parameters of D, which must be a function, into scope.
+ void ActOnReenterFunctionContext(Scope* S, Decl* D);
+ void ActOnExitFunctionContext() { PopDeclContext(); }
+
DeclContext *getFunctionLevelDeclContext();
/// getCurFunctionDecl - If inside of a function body, this returns a pointer
Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Thu Sep 8 12:42:31 2011
@@ -133,6 +133,10 @@
return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
}
+bool Decl::isTemplateDecl() const {
+ return isa<TemplateDecl>(this);
+}
+
bool Decl::isDefinedOutsideFunctionOrMethod() const {
for (const DeclContext *DC = getDeclContext();
DC && !DC->isTranslationUnit();
Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Thu Sep 8 12:42:31 2011
@@ -759,8 +759,28 @@
ParsedAttributes Attrs(AttrFactory);
SourceLocation endLoc;
+ // If the Decl is templatized, add template parameters to scope.
+ bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
+ ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
+ if (HasTemplateScope)
+ Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
+
+ // If the Decl is on a function, add function parameters to the scope.
+ bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
+ ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
+ if (HasFunctionScope)
+ Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
+
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
+ if (HasFunctionScope) {
+ Actions.ActOnExitFunctionContext();
+ FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
+ }
+ if (HasTemplateScope) {
+ TempScope.Exit();
+ }
+
// Late parsed attributes must be attached to Decls by hand. If the
// LA.D is not set, then this was not done properly.
assert(LA.D && "No decl attached to late parsed attribute");
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Sep 8 12:42:31 2011
@@ -782,6 +782,29 @@
// disappear.
}
+
+void Sema::ActOnReenterFunctionContext(Scope* S, Decl *D) {
+ FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
+ if (FunctionTemplateDecl *TFD = dyn_cast_or_null<FunctionTemplateDecl>(D)) {
+ // We assume that the caller has already called
+ // ActOnReenterTemplateScope
+ FD = TFD->getTemplatedDecl();
+ }
+ if (!FD)
+ return;
+
+ PushDeclContext(S, FD);
+ for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) {
+ ParmVarDecl *Param = FD->getParamDecl(P);
+ // If the parameter has an identifier, then add it to the scope
+ if (Param->getIdentifier()) {
+ S->AddDecl(Param);
+ IdResolver.AddDecl(Param);
+ }
+ }
+}
+
+
/// \brief Determine whether we allow overloading of the function
/// PrevDecl with another declaration.
///
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Thu Sep 8 12:42:31 2011
@@ -304,8 +304,11 @@
for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Expr *ArgExp = Attr.getArg(Idx);
- if (ArgExp->isTypeDependent())
- continue;
+ if (ArgExp->isTypeDependent()) {
+ // FIXME -- need to processs this again on template instantiation
+ Args.push_back(ArgExp);
+ continue;
+ }
QualType ArgTy = ArgExp->getType();
@@ -390,6 +393,7 @@
return;
if (Arg->isTypeDependent())
+ // FIXME: handle attributes with dependent types
return;
// check that the argument is lockable object
Modified: cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp?rev=139302&r1=139301&r2=139302&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp Thu Sep 8 12:42:31 2011
@@ -1219,3 +1219,37 @@
};
+//-----------------------------------------------------
+// Parsing of member variables and function parameters
+//------------------------------------------------------
+
+Mu gmu;
+
+class StaticMu {
+ static Mu statmu;
+};
+
+class FooLate {
+public:
+ void foo1() __attribute__((exclusive_locks_required(gmu))) { }
+ void foo2() __attribute__((exclusive_locks_required(mu))) { }
+ void foo3(Mu *m) __attribute__((exclusive_locks_required(m))) { }
+ void foo3(FooLate *f) __attribute__((exclusive_locks_required(f->mu))) { }
+ void foo4(FooLate *f) __attribute__((exclusive_locks_required(f->mu)));
+
+ static void foo5() __attribute__((exclusive_locks_required(mu))); // \
+ expected-error {{invalid use of member 'mu' in static member function}}
+
+ template <class T>
+ void foo6() __attribute__((exclusive_locks_required(T::statmu))) { }
+
+ template <class T>
+ void foo7(T* f) __attribute__((exclusive_locks_required(f->mu))) { }
+
+ int a __attribute__((guarded_by(gmu)));
+ int b __attribute__((guarded_by(mu)));
+ int c __attribute__((guarded_by(this->mu)));
+
+ Mu mu;
+};
+
More information about the cfe-commits
mailing list