diff options
author | Chris Lattner <sabre@nondot.org> | 2008-04-12 23:52:44 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-04-12 23:52:44 +0000 |
commit | 9e979557eea3875c9e3d100c68188233dd7f46c0 (patch) | |
tree | 2576060186b3181b93b67c0565e7eb6d79d474e2 /lib/Sema/SemaDeclCXX.cpp | |
parent | 22cb282b29deb9a7b919a339d450d36a3fc205a6 (diff) |
Default argument cleanups and minor improvements, patch by
Doug Gregor!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49598 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDeclCXX.cpp')
-rw-r--r-- | lib/Sema/SemaDeclCXX.cpp | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 3211e22890..89440c0cf4 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -25,63 +25,68 @@ using namespace clang; // CheckDefaultArgumentVisitor //===----------------------------------------------------------------------===// -/// CheckDefaultArgumentVisitor - Traverses the default argument of a -/// parameter to determine whether it contains any ill-formed -/// subexpressions. For example, this will diagnose the use of local -/// variables or parameters within the default argument expression. -class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor - : public StmtVisitor<CheckDefaultArgumentVisitor, bool> -{ - Sema *S; - -public: - explicit CheckDefaultArgumentVisitor(Sema *s) : S(s) {} - - bool VisitExpr(Expr *Node); - bool VisitDeclRefExpr(DeclRefExpr *DRE); -}; - -/// VisitExpr - Visit all of the children of this expression. -bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { - bool IsInvalid = false; - for (Stmt::child_iterator first = Node->child_begin(), - last = Node->child_end(); - first != last; ++first) - IsInvalid |= Visit(*first); - - return IsInvalid; -} - -/// VisitDeclRefExpr - Visit a reference to a declaration, to -/// determine whether this declaration can be used in the default -/// argument expression. -bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { - ValueDecl *Decl = DRE->getDecl(); - if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { - // C++ [dcl.fct.default]p9 - // Default arguments are evaluated each time the function is - // called. The order of evaluation of function arguments is - // unspecified. Consequently, parameters of a function shall not - // be used in default argument expressions, even if they are not - // evaluated. Parameters of a function declared before a default - // argument expression are in scope and can hide namespace and - // class member names. - return S->Diag(DRE->getSourceRange().getBegin(), - diag::err_param_default_argument_references_param, - Param->getName()); - } else if (BlockVarDecl *BlockVar = dyn_cast<BlockVarDecl>(Decl)) { - // C++ [dcl.fct.default]p7 - // Local variables shall not be used in default argument - // expressions. - return S->Diag(DRE->getSourceRange().getBegin(), - diag::err_param_default_argument_references_local, - BlockVar->getName()); +namespace { + /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses + /// the default argument of a parameter to determine whether it + /// contains any ill-formed subexpressions. For example, this will + /// diagnose the use of local variables or parameters within the + /// default argument expression. + class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor + : public StmtVisitor<CheckDefaultArgumentVisitor, bool> + { + Expr *DefaultArg; + Sema *S; + + public: + CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) + : DefaultArg(defarg), S(s) {} + + bool VisitExpr(Expr *Node); + bool VisitDeclRefExpr(DeclRefExpr *DRE); + }; + + /// VisitExpr - Visit all of the children of this expression. + bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { + bool IsInvalid = false; + for (Stmt::child_iterator first = Node->child_begin(), + last = Node->child_end(); + first != last; ++first) + IsInvalid |= Visit(*first); + + return IsInvalid; } - // FIXME: when Clang has support for member functions, "this" - // will also need to be diagnosted. + /// VisitDeclRefExpr - Visit a reference to a declaration, to + /// determine whether this declaration can be used in the default + /// argument expression. + bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { + ValueDecl *Decl = DRE->getDecl(); + if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { + // C++ [dcl.fct.default]p9 + // Default arguments are evaluated each time the function is + // called. The order of evaluation of function arguments is + // unspecified. Consequently, parameters of a function shall not + // be used in default argument expressions, even if they are not + // evaluated. Parameters of a function declared before a default + // argument expression are in scope and can hide namespace and + // class member names. + return S->Diag(DRE->getSourceRange().getBegin(), + diag::err_param_default_argument_references_param, + Param->getName(), DefaultArg->getSourceRange()); + } else if (BlockVarDecl *BlockVar = dyn_cast<BlockVarDecl>(Decl)) { + // C++ [dcl.fct.default]p7 + // Local variables shall not be used in default argument + // expressions. + return S->Diag(DRE->getSourceRange().getBegin(), + diag::err_param_default_argument_references_local, + BlockVar->getName(), DefaultArg->getSourceRange()); + } - return false; + // FIXME: when Clang has support for member functions, "this" + // will also need to be diagnosed. + + return false; + } } /// ActOnParamDefaultArgument - Check whether the default argument @@ -135,7 +140,7 @@ Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, // declarator or abstract-declarator of a parameter-declaration. // Check that the default argument is well-formed - CheckDefaultArgumentVisitor DefaultArgChecker(this); + CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); if (DefaultArgChecker.Visit(DefaultArg.get())) return; |