@@ -123,6 +123,13 @@ AST_MATCHER(EnumDecl, hasSequentialInitialValues) {
123
123
return !AllEnumeratorsArePowersOfTwo;
124
124
}
125
125
126
+ std::string getName (const EnumDecl *Decl) {
127
+ if (!Decl->getDeclName ())
128
+ return " <unnamed>" ;
129
+
130
+ return Decl->getQualifiedNameAsString ();
131
+ }
132
+
126
133
} // namespace
127
134
128
135
EnumInitialValueCheck::EnumInitialValueCheck (StringRef Name,
@@ -158,12 +165,16 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
158
165
}
159
166
160
167
void EnumInitialValueCheck::check (const MatchFinder::MatchResult &Result) {
168
+ PrintingPolicy PP = Result.Context ->getPrintingPolicy ();
169
+ PP.AnonymousTagLocations = false ;
170
+
161
171
if (const auto *Enum = Result.Nodes .getNodeAs <EnumDecl>(" inconsistent" )) {
162
172
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;
173
+ diag (
174
+ Enum->getBeginLoc (),
175
+ " initial values in enum '%0' are not consistent, consider explicit "
176
+ " initialization of all, none or only the first enumerator" )
177
+ << getName (Enum);
167
178
for (const EnumConstantDecl *ECD : Enum->enumerators ())
168
179
if (ECD->getInitExpr () == nullptr ) {
169
180
const SourceLocation EndLoc = Lexer::getLocForEndOfToken (
@@ -183,16 +194,16 @@ void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
183
194
if (Loc.isInvalid () || Loc.isMacroID ())
184
195
return ;
185
196
DiagnosticBuilder Diag = diag (Loc, " zero initial value for the first "
186
- " enumerator in %0 can be disregarded" )
187
- << Enum;
197
+ " enumerator in '%0' can be disregarded" )
198
+ << getName ( Enum) ;
188
199
cleanInitialValue (Diag, ECD, *Result.SourceManager , getLangOpts ());
189
200
return ;
190
201
}
191
202
if (const auto *Enum = Result.Nodes .getNodeAs <EnumDecl>(" sequential" )) {
192
203
DiagnosticBuilder Diag =
193
204
diag (Enum->getBeginLoc (),
194
- " sequential initial value in %0 can be ignored" )
195
- << Enum;
205
+ " sequential initial value in '%0' can be ignored" )
206
+ << getName ( Enum) ;
196
207
for (const EnumConstantDecl *ECD : llvm::drop_begin (Enum->enumerators ()))
197
208
cleanInitialValue (Diag, ECD, *Result.SourceManager , getLangOpts ());
198
209
return ;
0 commit comments