From e6202c43db54569c344e8dff98fdb399ef023c82 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 12 Sep 2019 18:26:34 +0000 Subject: [PATCH] [MS] Warn when shadowing template parameters under -fms-compatibility Summary: C++ does not allow shadowing template parameters, but previously we allowed it under -fms-extensions. Now this behavior is controlled by -fms-compatibility, and we emit a -Wmicrosoft-template warning when it happens. Fixes PR43265 Reviewers: thakis, hans Subscribers: amccarth, rsmith, STL_MSFT, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D67463 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371753 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaTemplate.cpp | 11 +++++------ test/Parser/DelayedTemplateParsing.cpp | 16 ---------------- test/SemaCXX/MicrosoftCompatibility.cpp | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 22596950e2..46af14503e 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -4000,6 +4000,8 @@ def err_ovl_no_viable_literal_operator : Error< // C++ Template Declarations def err_template_param_shadow : Error< "declaration of %0 shadows template parameter">; +def ext_template_param_shadow : ExtWarn< + err_template_param_shadow.Text>, InGroup; def note_template_param_here : Note<"template parameter is declared here">; def warn_template_export_unsupported : Warning< "exported templates are unsupported">; diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 001e433ad6..91466fa904 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -849,15 +849,14 @@ bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); - // Microsoft Visual C++ permits template parameters to be shadowed. - if (getLangOpts().MicrosoftExt) - return; - // C++ [temp.local]p4: // A template-parameter shall not be redeclared within its // scope (including nested scopes). - Diag(Loc, diag::err_template_param_shadow) - << cast(PrevDecl)->getDeclName(); + // + // Make this a warning when MSVC compatibility is requested. + unsigned DiagId = getLangOpts().MSVCCompat ? diag::ext_template_param_shadow + : diag::err_template_param_shadow; + Diag(Loc, DiagId) << cast(PrevDecl)->getDeclName(); Diag(PrevDecl->getLocation(), diag::note_template_param_here); } diff --git a/test/Parser/DelayedTemplateParsing.cpp b/test/Parser/DelayedTemplateParsing.cpp index c65e80b1f7..bcd286ae04 100644 --- a/test/Parser/DelayedTemplateParsing.cpp +++ b/test/Parser/DelayedTemplateParsing.cpp @@ -48,22 +48,6 @@ template void foo5() {} // expected-error {{redefinition of 'foo5'}} -namespace Inner_Outer_same_template_param_name { - -template -class Outmost { -public: - template - class Inner { - public: - void f() { - T* var; - } - }; -}; - -} - namespace PR11931 { diff --git a/test/SemaCXX/MicrosoftCompatibility.cpp b/test/SemaCXX/MicrosoftCompatibility.cpp index bfdf441092..453f78ad43 100644 --- a/test/SemaCXX/MicrosoftCompatibility.cpp +++ b/test/SemaCXX/MicrosoftCompatibility.cpp @@ -366,3 +366,21 @@ struct S { int S::fn() { return 0; } // expected-warning {{is missing exception specification}} } +namespace PR43265 { +template // expected-note {{template parameter is declared here}} +struct Foo { + static const int N = 42; // expected-warning {{declaration of 'N' shadows template parameter}} +}; +} + +namespace Inner_Outer_same_template_param_name { +template // expected-note {{template parameter is declared here}} +struct Outmost { + template // expected-warning {{declaration of 'T' shadows template parameter}} + struct Inner { + void f() { + T *var; + } + }; +}; +} -- 2.40.0