Skip to content

Commit f5ff3a5

Browse files
authored
[clang-tidy] Do not emit file path for anonymous enums in readability-enum-initial-value check (#112496)
Previously the name of anonymous enums in the check were `enum 'enum (unnamed at /full/path/to/file.c:1:1)'`, which breaks reproducibility of clang-tidy reports when the analyzed project is in a different folder.
1 parent 6414894 commit f5ff3a5

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ AST_MATCHER(EnumDecl, hasSequentialInitialValues) {
123123
return !AllEnumeratorsArePowersOfTwo;
124124
}
125125

126+
std::string getName(const EnumDecl *Decl) {
127+
if (!Decl->getDeclName())
128+
return "<unnamed>";
129+
130+
return Decl->getQualifiedNameAsString();
131+
}
132+
126133
} // namespace
127134

128135
EnumInitialValueCheck::EnumInitialValueCheck(StringRef Name,
@@ -160,10 +167,11 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
160167
void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
161168
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
162169
DiagnosticBuilder Diag =
163-
diag(Enum->getBeginLoc(),
164-
"initial values in enum %0 are not consistent, consider explicit "
165-
"initialization of all, none or only the first enumerator")
166-
<< Enum;
170+
diag(
171+
Enum->getBeginLoc(),
172+
"initial values in enum '%0' are not consistent, consider explicit "
173+
"initialization of all, none or only the first enumerator")
174+
<< getName(Enum);
167175
for (const EnumConstantDecl *ECD : Enum->enumerators())
168176
if (ECD->getInitExpr() == nullptr) {
169177
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
@@ -183,16 +191,16 @@ void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
183191
if (Loc.isInvalid() || Loc.isMacroID())
184192
return;
185193
DiagnosticBuilder Diag = diag(Loc, "zero initial value for the first "
186-
"enumerator in %0 can be disregarded")
187-
<< Enum;
194+
"enumerator in '%0' can be disregarded")
195+
<< getName(Enum);
188196
cleanInitialValue(Diag, ECD, *Result.SourceManager, getLangOpts());
189197
return;
190198
}
191199
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("sequential")) {
192200
DiagnosticBuilder Diag =
193201
diag(Enum->getBeginLoc(),
194-
"sequential initial value in %0 can be ignored")
195-
<< Enum;
202+
"sequential initial value in '%0' can be ignored")
203+
<< getName(Enum);
196204
for (const EnumConstantDecl *ECD : llvm::drop_begin(Enum->enumerators()))
197205
cleanInitialValue(Diag, ECD, *Result.SourceManager, getLangOpts());
198206
return;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ Changes in existing checks
249249

250250
- Improved :doc:`readability-enum-initial-value
251251
<clang-tidy/checks/readability/enum-initial-value>` check by only issuing
252-
diagnostics for the definition of an ``enum``, and by fixing a typo in the
252+
diagnostics for the definition of an ``enum``, by not emitting a redundant
253+
file path for anonymous enums in the diagnostic, and by fixing a typo in the
253254
diagnostic.
254255

255256
- Improved :doc:`readability-implicit-bool-conversion

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ enum EMacro2 {
5353
// CHECK-FIXES: EMacro2_c = 3,
5454
};
5555

56+
57+
enum {
58+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum '<unnamed>' are not consistent
59+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum '<unnamed>' are not consistent
60+
EAnonymous_a = 1,
61+
EAnonymous_b,
62+
// CHECK-FIXES: EAnonymous_b = 2,
63+
EAnonymous_c = 3,
64+
};
65+
66+
5667
enum EnumZeroFirstInitialValue {
5768
EnumZeroFirstInitialValue_0 = 0,
5869
// CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:3: warning: zero initial value for the first enumerator in 'EnumZeroFirstInitialValue' can be disregarded
@@ -114,4 +125,3 @@ enum WithFwdDeclSequential : int {
114125
EFS2 = 4,
115126
// CHECK-FIXES-ENABLE: EFS2 ,
116127
};
117-

0 commit comments

Comments
 (0)