@@ -109,8 +109,7 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
109
109
EndIfs[Ifndefs[MacroEntry.first .getIdentifierInfo ()].first ];
110
110
111
111
// If the macro Name is not equal to what we can compute, correct it in
112
- // the
113
- // #ifndef and #define.
112
+ // the #ifndef and #define.
114
113
StringRef CurHeaderGuard =
115
114
MacroEntry.first .getIdentifierInfo ()->getName ();
116
115
std::string NewGuard = checkHeaderGuardDefinition (
@@ -119,6 +118,22 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
119
118
// Now look at the #endif. We want a comment with the header guard. Fix it
120
119
// at the slightest deviation.
121
120
checkEndifComment (FileName, EndIf, NewGuard);
121
+
122
+ // Bundle all fix-its into one warning. The message depends on whether we
123
+ // changed the header guard or not.
124
+ if (!FixIts.empty ()) {
125
+ if (CurHeaderGuard != NewGuard) {
126
+ auto D = Check->diag (Ifndef,
127
+ " header guard does not follow preferred style" );
128
+ for (const FixItHint Fix : FixIts)
129
+ D.AddFixItHint (Fix);
130
+ } else {
131
+ auto D = Check->diag (EndIf, " #endif for a header guard should "
132
+ " reference the guard macro in a comment" );
133
+ for (const FixItHint Fix : FixIts)
134
+ D.AddFixItHint (Fix);
135
+ }
136
+ }
122
137
}
123
138
124
139
// Emit warnings for headers that are missing guards.
@@ -129,6 +144,7 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
129
144
Files.clear ();
130
145
Ifndefs.clear ();
131
146
EndIfs.clear ();
147
+ FixIts.clear ();
132
148
}
133
149
134
150
bool wouldFixEndifComment (StringRef FileName, SourceLocation EndIf,
@@ -170,15 +186,14 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
170
186
if (Ifndef.isValid () && CurHeaderGuard != CPPVar &&
171
187
(CurHeaderGuard != CPPVarUnder ||
172
188
wouldFixEndifComment (FileName, EndIf, CurHeaderGuard))) {
173
- Check->diag (Ifndef, " header guard does not follow preferred style" )
174
- << FixItHint::CreateReplacement (
175
- CharSourceRange::getTokenRange (
176
- Ifndef, Ifndef.getLocWithOffset (CurHeaderGuard.size ())),
177
- CPPVar)
178
- << FixItHint::CreateReplacement (
179
- CharSourceRange::getTokenRange (
180
- Define, Define.getLocWithOffset (CurHeaderGuard.size ())),
181
- CPPVar);
189
+ FixIts.push_back (FixItHint::CreateReplacement (
190
+ CharSourceRange::getTokenRange (
191
+ Ifndef, Ifndef.getLocWithOffset (CurHeaderGuard.size ())),
192
+ CPPVar));
193
+ FixIts.push_back (FixItHint::CreateReplacement (
194
+ CharSourceRange::getTokenRange (
195
+ Define, Define.getLocWithOffset (CurHeaderGuard.size ())),
196
+ CPPVar));
182
197
return CPPVar;
183
198
}
184
199
return CurHeaderGuard;
@@ -191,12 +206,10 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
191
206
size_t EndIfLen;
192
207
if (wouldFixEndifComment (FileName, EndIf, HeaderGuard, &EndIfLen)) {
193
208
std::string Correct = " endif // " + HeaderGuard.str ();
194
- Check->diag (EndIf, " #endif for a header guard should reference the "
195
- " guard macro in a comment" )
196
- << FixItHint::CreateReplacement (
197
- CharSourceRange::getCharRange (EndIf,
198
- EndIf.getLocWithOffset (EndIfLen)),
199
- Correct);
209
+ FixIts.push_back (FixItHint::CreateReplacement (
210
+ CharSourceRange::getCharRange (EndIf,
211
+ EndIf.getLocWithOffset (EndIfLen)),
212
+ Correct));
200
213
}
201
214
}
202
215
@@ -257,6 +270,7 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
257
270
std::map<const IdentifierInfo *, std::pair<SourceLocation, SourceLocation>>
258
271
Ifndefs;
259
272
std::map<SourceLocation, SourceLocation> EndIfs;
273
+ std::vector<FixItHint> FixIts;
260
274
261
275
Preprocessor *PP;
262
276
HeaderGuardCheck *Check;
0 commit comments