Skip to content

[clang-tidy] EndSourceFile() for preprocessor before diagnostic client #145784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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();

// Finalize the action.
EndSourceFileAction();

Expand Down