From 4d871f99c796c0cf26f68380fe97c6e13b249607 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 12 Aug 2015 13:16:41 +0000 Subject: [PATCH] Lazily initialize HeaderFilter in ClangTidyDiagnosticConsumer. This removes a corner case in tests that don't set the diagnostic consumer. In tests, it is good, not to set the diagnostic consumer so that Clang's parsing diagnostics are still displayed in the test output and only ClangTidy's output is analyzed differently. llvm-svn: 244745 --- .../clang-tidy/ClangTidyDiagnosticConsumer.cpp | 18 ++++++++---------- .../clang-tidy/ClangTidyDiagnosticConsumer.h | 8 ++++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index ad341ce..c383440 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -336,13 +336,6 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic( checkFilters(Info.getLocation()); } -void ClangTidyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, - const Preprocessor *PP) { - // Before the first translation unit we don't need HeaderFilter, as we - // shouldn't get valid source locations in diagnostics. - HeaderFilter.reset(new llvm::Regex(*Context.getOptions().HeaderFilterRegex)); -} - bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName, unsigned LineNumber) const { if (Context.getGlobalOptions().LineFilter.empty()) @@ -389,17 +382,22 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location) { } StringRef FileName(File->getName()); - assert(LastErrorRelatesToUserCode || Sources.isInMainFile(Location) || - HeaderFilter != nullptr); LastErrorRelatesToUserCode = LastErrorRelatesToUserCode || Sources.isInMainFile(Location) || - HeaderFilter->match(FileName); + getHeaderFilter()->match(FileName); unsigned LineNumber = Sources.getExpansionLineNumber(Location); LastErrorPassesLineFilter = LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber); } +llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() { + if (!HeaderFilter) + HeaderFilter.reset( + new llvm::Regex(*Context.getOptions().HeaderFilterRegex)); + return HeaderFilter.get(); +} + namespace { struct LessClangTidyError { bool operator()(const ClangTidyError *LHS, const ClangTidyError *RHS) const { diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h index 67ef840..f1ab494 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -220,16 +220,16 @@ public: void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override; - /// \brief Sets \c HeaderFilter to the value configured for this file. - void BeginSourceFile(const LangOptions &LangOpts, - const Preprocessor *PP) override; - /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext. void finish() override; private: void finalizeLastError(); + /// \brief Returns the \c HeaderFilter constructed for the options set in the + /// context. + llvm::Regex* getHeaderFilter(); + /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter /// according to the diagnostic \p Location. void checkFilters(SourceLocation Location); -- 2.7.4