From: Francois Pichet Date: Sun, 10 Apr 2011 03:03:52 +0000 (+0000) Subject: MSVC accepts that default parameters be redefined for member functions X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8d051e00ad674754d476cc1fa0442da0bc47b2c8;p=clang MSVC accepts that default parameters be redefined for member functions of template class. The new value is ignored. This fixes 1 error when parsing MSVC 2010 header files with clang. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129240 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 92d5dd9e61..edcf4ef14f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1301,6 +1301,8 @@ def err_param_default_argument : Error< "C does not support default arguments">; def err_param_default_argument_redefinition : Error< "redefinition of default argument">; +def war_param_default_argument_redefinition : ExtWarn< + "redefinition of default argument">; def err_param_default_argument_missing : Error< "missing default argument on parameter">; def err_param_default_argument_missing_name : Error< diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 6d50267289..9dbca64639 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -293,12 +293,24 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { // for NewParam to find the last source location in the type... but it // isn't worth the effort right now. This is the kind of test case that // is hard to get right: + unsigned DiagDefaultParamID = + diag::err_param_default_argument_redefinition; + + // MSVC accepts that default parameters be redefined for member functions + // of template class. The new default parameter's value is ignored. + Invalid = true; + if (getLangOptions().Microsoft) { + CXXMethodDecl* MD = dyn_cast(New); + if (MD && MD->getParent()->getDescribedClassTemplate()) { + DiagDefaultParamID = diag::war_param_default_argument_redefinition; + Invalid = false; + } + } // int f(int); // void g(int (*fp)(int) = f); // void g(int (*fp)(int) = &f); - Diag(NewParam->getLocation(), - diag::err_param_default_argument_redefinition) + Diag(NewParam->getLocation(), DiagDefaultParamID) << NewParam->getDefaultArgRange(); // Look for the function declaration where the default argument was @@ -313,7 +325,6 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { Diag(OldParam->getLocation(), diag::note_previous_definition) << OldParam->getDefaultArgRange(); - Invalid = true; } else if (OldParam->hasDefaultArg()) { // Merge the old default argument into the new parameter. // It's important to use getInit() here; getDefaultArg() diff --git a/test/SemaCXX/MicrosoftExtensions.cpp b/test/SemaCXX/MicrosoftExtensions.cpp index 24c24aaf33..2287d26fa8 100644 --- a/test/SemaCXX/MicrosoftExtensions.cpp +++ b/test/SemaCXX/MicrosoftExtensions.cpp @@ -127,3 +127,12 @@ __declspec(dllimport) void AAA::f2(void) { // expected-error {{dllimport attribu +template +class BB { +public: + void f(int g = 10 ); // expected-note {{previous definition is here}} +}; + +template +void BB::f(int g = 0) { } // expected-warning {{redefinition of default argument}} +