Skip to content

Commit e51d411

Browse files
committed
EndSourceFile() for preprocessor before diagnstic client
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, which catches the problem in current tests.
1 parent c72507d commit e51d411

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix) {
360360

361361
void ClangTidyDiagnosticConsumer::HandleDiagnostic(
362362
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
363+
assert(InSourceFile || Info.getLocation().isInvalid()); // A diagnostic should not be reported outside of a BeginSourceFile()/EndSourceFile() pair if it has a source location.
364+
363365
if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
364366
return;
365367

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ 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+
DiagnosticConsumer::BeginSourceFile(LangOpts, PP);
298+
299+
assert(!InSourceFile);
300+
InSourceFile = true;
301+
}
302+
303+
void EndSourceFile() override {
304+
assert(InSourceFile);
305+
InSourceFile = false;
306+
307+
DiagnosticConsumer::EndSourceFile();
308+
}
309+
295310
// Retrieve the diagnostics that were captured.
296311
std::vector<ClangTidyError> take();
297312

@@ -326,6 +341,7 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
326341
bool LastErrorRelatesToUserCode = false;
327342
bool LastErrorPassesLineFilter = false;
328343
bool LastErrorWasIgnored = false;
344+
bool InSourceFile = false;
329345
};
330346

331347
} // 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)