Lazily initialize HeaderFilter in ClangTidyDiagnosticConsumer. This
authorDaniel Jasper <djasper@google.com>
Wed, 12 Aug 2015 13:16:41 +0000 (13:16 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 12 Aug 2015 13:16:41 +0000 (13:16 +0000)
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-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

index ad341ce..c383440 100644 (file)
@@ -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 {
index 67ef840..f1ab494 100644 (file)
@@ -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);