Skip to content

Commit 1eec327

Browse files
5chmidtitmsri
authored andcommitted
[clang-tidy] only diagnose definitions in readability-enum-initial-value (llvm#107652)
With the `isDefinition` matcher, the analysis and diagnostics will be constrained to definitions only. Previously forward declarations were diagnosed as well. Fixes llvm#107590
1 parent bcdbe51 commit 1eec327

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ void EnumInitialValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
141141
}
142142

143143
void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
144-
Finder->addMatcher(
145-
enumDecl(unless(isMacro()), unless(hasConsistentInitialValues()))
146-
.bind("inconsistent"),
147-
this);
144+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
145+
unless(hasConsistentInitialValues()))
146+
.bind("inconsistent"),
147+
this);
148148
if (!AllowExplicitZeroFirstInitialValue)
149149
Finder->addMatcher(
150-
enumDecl(hasZeroInitialValueForFirstEnumerator()).bind("zero_first"),
150+
enumDecl(isDefinition(), hasZeroInitialValueForFirstEnumerator())
151+
.bind("zero_first"),
151152
this);
152153
if (!AllowExplicitSequentialInitialValues)
153-
Finder->addMatcher(enumDecl(unless(isMacro()), hasSequentialInitialValues())
154+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
155+
hasSequentialInitialValues())
154156
.bind("sequential"),
155157
this);
156158
}
@@ -159,7 +161,7 @@ void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
159161
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
160162
DiagnosticBuilder Diag =
161163
diag(Enum->getBeginLoc(),
162-
"inital values in enum %0 are not consistent, consider explicit "
164+
"initial values in enum %0 are not consistent, consider explicit "
163165
"initialization of all, none or only the first enumerator")
164166
<< Enum;
165167
for (const EnumConstantDecl *ECD : Enum->enumerators())

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ Changes in existing checks
145145
<clang-tidy/checks/modernize/use-std-print>` check to support replacing
146146
member function calls too.
147147

148+
- Improved :doc:`readability-enum-initial-value
149+
<clang-tidy/checks/readability/enum-initial-value>` check by only issuing
150+
diagnostics for the definition of an ``enum``, and by fixing a typo in the
151+
diagnostic.
152+
148153
- Improved :doc:`performance-avoid-endl
149154
<clang-tidy/checks/performance/avoid-endl>` check to use ``std::endl`` as
150155
placeholder when lexer cannot get source text.

clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// RUN: }}'
77

88
enum EError {
9-
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 'EError' are not consistent
10-
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: inital values in enum 'EError' are not consistent
9+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EError' are not consistent
10+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EError' are not consistent
1111
EError_a = 1,
1212
EError_b,
1313
// CHECK-FIXES: EError_b = 2,
@@ -34,8 +34,8 @@ enum EAll {
3434

3535
#define ENUMERATOR_1 EMacro1_b
3636
enum EMacro1 {
37-
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 'EMacro1' are not consistent
38-
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: inital values in enum 'EMacro1' are not consistent
37+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EMacro1' are not consistent
38+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EMacro1' are not consistent
3939
EMacro1_a = 1,
4040
ENUMERATOR_1,
4141
// CHECK-FIXES: ENUMERATOR_1 = 2,
@@ -45,8 +45,8 @@ enum EMacro1 {
4545

4646
#define ENUMERATOR_2 EMacro2_b = 2
4747
enum EMacro2 {
48-
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 'EMacro2' are not consistent
49-
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: inital values in enum 'EMacro2' are not consistent
48+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EMacro2' are not consistent
49+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EMacro2' are not consistent
5050
EMacro2_a = 1,
5151
ENUMERATOR_2,
5252
EMacro2_c,
@@ -78,3 +78,40 @@ enum EnumSequentialInitialValue {
7878
EnumSequentialInitialValue_2 = 4,
7979
// CHECK-FIXES-ENABLE: EnumSequentialInitialValue_2 ,
8080
};
81+
82+
// gh107590
83+
enum WithFwdDeclInconsistent : int;
84+
85+
enum WithFwdDeclInconsistent : int {
86+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'WithFwdDeclInconsistent' are not consistent
87+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'WithFwdDeclInconsistent' are not consistent
88+
EFI0,
89+
// CHECK-FIXES: EFI0 = 0,
90+
EFI1 = 1,
91+
EFI2,
92+
// CHECK-FIXES: EFI2 = 2,
93+
};
94+
95+
enum WithFwdDeclZeroFirst : int;
96+
97+
enum WithFwdDeclZeroFirst : int {
98+
// CHECK-MESSAGES-ENABLE: :[[@LINE+1]]:3: warning: zero initial value for the first enumerator in 'WithFwdDeclZeroFirst' can be disregarded
99+
EFZ0 = 0,
100+
// CHECK-FIXES-ENABLE: EFZ0 ,
101+
EFZ1,
102+
EFZ2,
103+
};
104+
105+
106+
enum WithFwdDeclSequential : int;
107+
108+
enum WithFwdDeclSequential : int {
109+
// CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:1: warning: sequential initial value in 'WithFwdDeclSequential' can be ignored
110+
EFS0 = 2,
111+
// CHECK-FIXES-ENABLE: EFS0 = 2,
112+
EFS1 = 3,
113+
// CHECK-FIXES-ENABLE: EFS1 ,
114+
EFS2 = 4,
115+
// CHECK-FIXES-ENABLE: EFS2 ,
116+
};
117+

clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy %s readability-enum-initial-value %t
22

33
enum class EError {
4-
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 'EError' are not consistent
4+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EError' are not consistent
55
EError_a = 1,
66
EError_b,
77
// CHECK-FIXES: EError_b = 2,

0 commit comments

Comments
 (0)