Skip to content

Commit 85aaaf6

Browse files
authored
[clang-tidy] EndSourceFile() for preprocessor before diagnostic client (#145784)
The comment for `DiagnosticConsumer::BeginSourceFile()` states that "diagnostics with source range information are required to only be emitted in between BeginSourceFile() and EndSourceFile().". While working on some upcoming changes to the static analyzer, we hit some crashes when diagnostics were reported from the `EndOfMainFile` callback in the preprocessor. This turned out to be because `FrontEndAction::EndSourceFile()` notifies the diagnostic clients of the end of the source file before it notifies the preprocessor. Thus, the diagnostics from the preprocessor callback are reported when the diagnostic client is no longer expecting any diagnostics. The fix is to swap the order of the `EndSourceFile()` calls so that the preprocessor is notified first. I've added asserts to the `ClangTidyDiagnosticConsumer` to catch unexpected diagnostics outside of a source file. Before swapping the order of the calls as described above, this causes several failures in the clang-tidy regression tests. With the swap, there are no failures in `check-all`. rdar://141230583
1 parent 8c9e0c6 commit 85aaaf6

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,27 @@ getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix) {
358358

359359
} // namespace clang::tidy
360360

361+
void ClangTidyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
362+
const Preprocessor *PP) {
363+
DiagnosticConsumer::BeginSourceFile(LangOpts, PP);
364+
365+
assert(!InSourceFile);
366+
InSourceFile = true;
367+
}
368+
369+
void ClangTidyDiagnosticConsumer::EndSourceFile() {
370+
assert(InSourceFile);
371+
InSourceFile = false;
372+
373+
DiagnosticConsumer::EndSourceFile();
374+
}
375+
361376
void ClangTidyDiagnosticConsumer::HandleDiagnostic(
362377
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
378+
// A diagnostic should not be reported outside of a
379+
// BeginSourceFile()/EndSourceFile() pair if it has a source location.
380+
assert(InSourceFile || Info.getLocation().isInvalid());
381+
363382
if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
364383
return;
365384

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
292292
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
293293
const Diagnostic &Info) override;
294294

295+
void BeginSourceFile(const LangOptions &LangOpts,
296+
const Preprocessor *PP = nullptr) override;
297+
298+
void EndSourceFile() override;
299+
295300
// Retrieve the diagnostics that were captured.
296301
std::vector<ClangTidyError> take();
297302

@@ -326,6 +331,11 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
326331
bool LastErrorRelatesToUserCode = false;
327332
bool LastErrorPassesLineFilter = false;
328333
bool LastErrorWasIgnored = false;
334+
/// Tracks whether we're currently inside a
335+
/// `BeginSourceFile()/EndSourceFile()` pair. Outside of a source file, we
336+
/// should only receive diagnostics that have to source location, such as
337+
/// command-line warnings.
338+
bool InSourceFile = false;
329339
};
330340

331341
} // end namespace tidy

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,13 +1243,15 @@ llvm::Error FrontendAction::Execute() {
12431243
void FrontendAction::EndSourceFile() {
12441244
CompilerInstance &CI = getCompilerInstance();
12451245

1246-
// Inform the diagnostic client we are done with this source file.
1247-
CI.getDiagnosticClient().EndSourceFile();
1248-
12491246
// Inform the preprocessor we are done.
12501247
if (CI.hasPreprocessor())
12511248
CI.getPreprocessor().EndSourceFile();
12521249

1250+
// Inform the diagnostic client we are done with this source file.
1251+
// Do this after notifying the preprocessor, so that end-of-file preprocessor
1252+
// callbacks can report diagnostics.
1253+
CI.getDiagnosticClient().EndSourceFile();
1254+
12531255
// Finalize the action.
12541256
EndSourceFileAction();
12551257

0 commit comments

Comments
 (0)