Skip to content

Commit bab3f3b

Browse files
AaronBallmanIanWood1
authored andcommitted
[C] Add -Wduplicate-decl-specifier to -Wc++-compat (llvm#138012)
The existing diagnostic, already enabled by default in C, will diagnose use of duplicate declaration specifiers (e.g., `const const`). However, the C++ standard claims that is ill-formed, so the diagnostic is now also controlled via -Wc++-compat. Note: Clang treats this as a warning in C++ rather than an error, but GCC does treat this as an error in C++, so the compatibility concerns are minor but do exist.
1 parent e77b126 commit bab3f3b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ C Language Changes
188188
``-Wunterminated-string-initialization``. However, this diagnostic is not
189189
silenced by the ``nonstring`` attribute as these initializations are always
190190
incompatible with C++.
191+
- Added the existing ``-Wduplicate-decl-specifier`` diagnostic, which is on by
192+
default, to ``-Wc++-compat`` because duplicated declaration specifiers are
193+
not valid in C++.
191194

192195
C2y Feature Support
193196
^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def BuiltinRequiresHeader : DiagGroup<"builtin-requires-header">;
156156
def C99Compat : DiagGroup<"c99-compat">;
157157
def C23Compat : DiagGroup<"c23-compat">;
158158
def : DiagGroup<"c2x-compat", [C23Compat]>;
159+
160+
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
159161
def InitStringTooLongMissingNonString :
160162
DiagGroup<"unterminated-string-initialization">;
161163
def InitStringTooLongForCpp :
@@ -168,7 +170,8 @@ def ImplicitIntToEnumCast : DiagGroup<"implicit-int-enum-cast",
168170
[ImplicitEnumEnumCast]>;
169171
def CXXCompat: DiagGroup<"c++-compat", [ImplicitVoidPtrCast, DefaultConstInit,
170172
ImplicitIntToEnumCast, HiddenCppDecl,
171-
InitStringTooLongForCpp]>;
173+
InitStringTooLongForCpp,
174+
DuplicateDeclSpecifier]>;
172175

173176
def ExternCCompat : DiagGroup<"extern-c-compat">;
174177
def KeywordCompat : DiagGroup<"keyword-compat">;
@@ -814,7 +817,6 @@ def TautologicalCompare : DiagGroup<"tautological-compare",
814817
TautologicalObjCBoolCompare,
815818
TautologicalNegationCompare]>;
816819
def HeaderHygiene : DiagGroup<"header-hygiene">;
817-
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
818820
def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
819821
def GNUUnionCast : DiagGroup<"gnu-union-cast">;
820822
def GNUVariableSizedTypeNotAtEnd : DiagGroup<"gnu-variable-sized-type-not-at-end">;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wduplicate-decl-specifier %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-compat %s
3+
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-duplicate-decl-specifier -Wc++-compat %s
4+
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wc++-compat -Wno-duplicate-decl-specifier %s
5+
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-duplicate-decl-specifier %s
6+
// RUN: %clang_cc1 -fsyntax-only -verify -x c++ %s
7+
// good-no-diagnostics
8+
9+
// Note: we treat this as a warning in C++, so you get the same diagnostics in
10+
// either language mode. However, GCC diagnoses this as an error, so the
11+
// compatibility warning has value.
12+
const const int i = 12; // expected-warning {{duplicate 'const' declaration specifier}}
13+
14+
__attribute__((address_space(1)))
15+
__attribute__((address_space(1))) // expected-warning {{multiple identical address spaces specified for type}}
16+
int j = 12;
17+

0 commit comments

Comments
 (0)