Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix) {

void ClangTidyDiagnosticConsumer::HandleDiagnostic(
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
// A diagnostic should not be reported outside of a
// BeginSourceFile()/EndSourceFile() pair if it has a source location.
assert(InSourceFile || Info.getLocation().isInvalid());

if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
return;

Expand Down
16 changes: 16 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override;

void BeginSourceFile(const LangOptions &LangOpts,
const Preprocessor *PP = nullptr) override {
DiagnosticConsumer::BeginSourceFile(LangOpts, PP);

assert(!InSourceFile);
InSourceFile = true;
}

void EndSourceFile() override {
assert(InSourceFile);
InSourceFile = false;

DiagnosticConsumer::EndSourceFile();
}

// Retrieve the diagnostics that were captured.
std::vector<ClangTidyError> take();

Expand Down Expand Up @@ -326,6 +341,7 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
bool LastErrorRelatesToUserCode = false;
bool LastErrorPassesLineFilter = false;
bool LastErrorWasIgnored = false;
bool InSourceFile = false;
};

} // end namespace tidy
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,13 +1243,15 @@ llvm::Error FrontendAction::Execute() {
void FrontendAction::EndSourceFile() {
CompilerInstance &CI = getCompilerInstance();

// Inform the diagnostic client we are done with this source file.
CI.getDiagnosticClient().EndSourceFile();

// Inform the preprocessor we are done.
if (CI.hasPreprocessor())
CI.getPreprocessor().EndSourceFile();

// Inform the diagnostic client we are done with this source file.
// Do this after notifying the preprocessor, so that end-of-file preprocessor
// callbacks can report diagnostics.
CI.getDiagnosticClient().EndSourceFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unittest to check this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already covered by several existing clang-tidy tests. Any check that fires a warning from the EndOfMainFile preprocessor event will trigger the assert that I added. These tests fail if I remove the change to FrontendAction.cpp.


// Finalize the action.
EndSourceFileAction();

Expand Down