diff options
author | Douglas Gregor <dgregor@apple.com> | 2008-05-07 04:49:29 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2008-05-07 04:49:29 +0000 |
commit | caa8acebe738aaa17fcbaa99b3f39da7874a6063 (patch) | |
tree | 3783ae6afce1b29c8f024d726bb41ca7951f57a7 /clang/lib | |
parent | 9930bd8c4f778145c97a8baa2e6d0311cfa89e2e (diff) | |
download | bcm5719-llvm-caa8acebe738aaa17fcbaa99b3f39da7874a6063.tar.gz bcm5719-llvm-caa8acebe738aaa17fcbaa99b3f39da7874a6063.zip |
Diagnose attempts to use C++ default arguments outside of a function declaration
llvm-svn: 50799
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/Sema.h | 1 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 14 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 36 | ||||
-rw-r--r-- | clang/lib/Sema/SemaType.cpp | 4 |
4 files changed, 46 insertions, 9 deletions
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 27eca89ab2b..e5aab61bb22 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -279,6 +279,7 @@ private: ParmVarDecl *CreateImplicitParameter(Scope *S, IdentifierInfo *Id, SourceLocation IdLoc, QualType Type); void CheckCXXDefaultArguments(FunctionDecl *FD); + void CheckExtraCXXDefaultArguments(Declarator &D); /// More parsing and symbol table subroutines... Decl *LookupDecl(const IdentifierInfo *II, unsigned NSI, Scope *S, diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 2624f5019db..9e9509adaa8 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -785,6 +785,10 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { + // Check that there are no default arguments (C++ only). + if (getLangOptions().CPlusPlus) + CheckExtraCXXDefaultArguments(D); + TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); if (!NewTD) return 0; @@ -889,6 +893,10 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { if (getLangOptions().CPlusPlus) CheckCXXDefaultArguments(NewFD); } else { + // Check that there are no default arguments (C++ only). + if (getLangOptions().CPlusPlus) + CheckExtraCXXDefaultArguments(D); + if (R.getTypePtr()->isObjCInterfaceType()) { Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, D.getIdentifier()->getName()); @@ -1108,7 +1116,11 @@ Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { DS.ClearStorageClassSpecs(); } - + // Check that there are no default arguments inside the type of this + // parameter (C++ only). + if (getLangOptions().CPlusPlus) + CheckExtraCXXDefaultArguments(D); + // In this context, we *do not* check D.getInvalidType(). If the declarator // type was invalid, GetTypeForDeclarator() still returns a "valid" type, // though it will not reflect the user specified type. diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 827c737ad4a..c3b6bce447a 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -134,14 +134,6 @@ Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, return; } - // FIXME: C++ [dcl.fct.default]p3 - // A default argument expression shall be specified only in the - // parameter-declaration-clause of a function declaration or in a - // template-parameter (14.1). It shall not be specified for a - // parameter pack. If it is specified in a - // parameter-declaration-clause, it shall not occur within a - // declarator or abstract-declarator of a parameter-declaration. - // Check that the default argument is well-formed CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); if (DefaultArgChecker.Visit(DefaultArg.get())) @@ -151,6 +143,34 @@ Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, Param->setDefaultArg(DefaultArg.take()); } +/// CheckExtraCXXDefaultArguments - Check for any extra default +/// arguments in the declarator, which is not a function declaration +/// or definition and therefore is not permitted to have default +/// arguments. This routine should be invoked for every declarator +/// that is not a function declaration or definition. +void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { + // C++ [dcl.fct.default]p3 + // A default argument expression shall be specified only in the + // parameter-declaration-clause of a function declaration or in a + // template-parameter (14.1). It shall not be specified for a + // parameter pack. If it is specified in a + // parameter-declaration-clause, it shall not occur within a + // declarator or abstract-declarator of a parameter-declaration. + for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) { + DeclaratorChunk &chunk = D.getTypeObject(i); + if (chunk.Kind == DeclaratorChunk::Function) { + for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) { + ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param; + if (Param->getDefaultArg()) { + Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc, + Param->getDefaultArg()->getSourceRange()); + Param->setDefaultArg(0); + } + } + } + } +} + // MergeCXXFunctionDecl - Merge two declarations of the same C++ // function, once we already know that they have the same // type. Subroutine of MergeFunctionDecl. diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 33797c42555..85ff96111a5 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -501,6 +501,10 @@ Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); + // Check that there are no default arguments (C++ only). + if (getLangOptions().CPlusPlus) + CheckExtraCXXDefaultArguments(D); + // In this context, we *do not* check D.getInvalidType(). If the declarator // type was invalid, GetTypeForDeclarator() still returns a "valid" type, // though it will not reflect the user specified type. |