Skip to content

[C] Add -Wduplicate-decl-specifier to -Wc++-compat #138012

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 1 commit into from
May 1, 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
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ C Language Changes
``-Wunterminated-string-initialization``. However, this diagnostic is not
silenced by the ``nonstring`` attribute as these initializations are always
incompatible with C++.
- Added the existing ``-Wduplicate-decl-specifier`` diagnostic, which is on by
default, to ``-Wc++-compat`` because duplicated declaration specifiers are
not valid in C++.

C2y Feature Support
^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def BuiltinRequiresHeader : DiagGroup<"builtin-requires-header">;
def C99Compat : DiagGroup<"c99-compat">;
def C23Compat : DiagGroup<"c23-compat">;
def : DiagGroup<"c2x-compat", [C23Compat]>;

def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
def InitStringTooLongMissingNonString :
DiagGroup<"unterminated-string-initialization">;
def InitStringTooLongForCpp :
Expand All @@ -168,7 +170,8 @@ def ImplicitIntToEnumCast : DiagGroup<"implicit-int-enum-cast",
[ImplicitEnumEnumCast]>;
def CXXCompat: DiagGroup<"c++-compat", [ImplicitVoidPtrCast, DefaultConstInit,
ImplicitIntToEnumCast, HiddenCppDecl,
InitStringTooLongForCpp]>;
InitStringTooLongForCpp,
DuplicateDeclSpecifier]>;

def ExternCCompat : DiagGroup<"extern-c-compat">;
def KeywordCompat : DiagGroup<"keyword-compat">;
Expand Down Expand Up @@ -814,7 +817,6 @@ def TautologicalCompare : DiagGroup<"tautological-compare",
TautologicalObjCBoolCompare,
TautologicalNegationCompare]>;
def HeaderHygiene : DiagGroup<"header-hygiene">;
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
def GNUUnionCast : DiagGroup<"gnu-union-cast">;
def GNUVariableSizedTypeNotAtEnd : DiagGroup<"gnu-variable-sized-type-not-at-end">;
Expand Down
17 changes: 17 additions & 0 deletions clang/test/Sema/warn-duplicate-decl-specifier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 -fsyntax-only -verify -Wduplicate-decl-specifier %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-compat %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-duplicate-decl-specifier -Wc++-compat %s
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wc++-compat -Wno-duplicate-decl-specifier %s
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-duplicate-decl-specifier %s
// RUN: %clang_cc1 -fsyntax-only -verify -x c++ %s
// good-no-diagnostics

// Note: we treat this as a warning in C++, so you get the same diagnostics in
// either language mode. However, GCC diagnoses this as an error, so the
// compatibility warning has value.
const const int i = 12; // expected-warning {{duplicate 'const' declaration specifier}}

__attribute__((address_space(1)))
__attribute__((address_space(1))) // expected-warning {{multiple identical address spaces specified for type}}
int j = 12;