Skip to content

[Clang] Implement CWG3005 Function parameters should never be name-independent #138245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Resolutions to C++ Defect Reports
- Bumped the ``__cpp_constexpr`` feature-test macro to ``202002L`` in C++20 mode as indicated in
`P2493R0 <https://wg21.link/P2493R0>`_.

- Implemented `CWG3005 Function parameters should never be name-independent <https://wg21.link/CWG3005>`_.

C Language Changes
------------------

Expand Down
10 changes: 7 additions & 3 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7542,16 +7542,20 @@ NamedDecl *Sema::ActOnVariableDeclarator(

DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();
StorageClass SC = StorageClassSpecToVarDeclStorageClass(D.getDeclSpec());

if (LangOpts.CPlusPlus && (DC->isClosure() || DC->isFunctionOrMethod()) &&
SC != SC_Static && SC != SC_Extern && II && II->isPlaceholder()) {

IsPlaceholderVariable = true;

if (!Previous.empty()) {
NamedDecl *PrevDecl = *Previous.begin();
bool SameDC = PrevDecl->getDeclContext()->getRedeclContext()->Equals(
DC->getRedeclContext());
if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false))
DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false)) {
IsPlaceholderVariable = !isa<ParmVarDecl>(PrevDecl);
if (IsPlaceholderVariable)
DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions clang/test/CXX/drs/cwg30xx.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to bother you with the description of what I want to see in this file. Here you go:

// RUN: %clang_cc1 -std=c++98 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++14 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++17 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++23 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++2c -pedantic-errors -verify=expected %s


namespace cwg3005 { // cwg3005: 21 open 2025-03-10

void f(
  int _, // #cwg3005-first-param
  int _)
  // expected-error@-1 {{redefinition of parameter '_'}}
  //   expected-note@#cwg3005-first-param {{previous declaration is here}}
{
  int _;
  // expected-error@-1 {{redefinition of '_'}}
  //   expected-note@#cwg3005-first-param {{previous declaration is here}}
}

} // namespace cwg3005

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clang_cc1 -std=c++98 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++14 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++17 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++23 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++2c -pedantic-errors -verify=expected %s


namespace cwg3005 { // cwg3005: 21 open 2025-03-10

void f(
int _, // #cwg3005-first-param
int _)
// expected-error@-1 {{redefinition of parameter '_'}}
// expected-note@#cwg3005-first-param {{previous definition is here}}
{
int _;
// expected-error@-1 {{redefinition of '_'}}
// expected-note@#cwg3005-first-param {{previous declaration is here}}
}

} // namespace cwg3005
6 changes: 5 additions & 1 deletion clang/www/cxx_dr_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -17890,7 +17890,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/3005.html">3005</a></td>
<td>open</td>
<td>Function parameters should never be name-independent</td>
<td align="center">Not resolved</td>
<td align="center">
<details>
<summary>Not resolved</summary>
Clang 21 implements 2025-03-10 resolution
</details></td>
</tr>
<tr class="open" id="3006">
<td><a href="https://cplusplus.github.io/CWG/issues/3006.html">3006</a></td>
Expand Down