njames93 created this revision.
njames93 added a reviewer: aaron.ballman.
Herald added subscribers: usaxena95, kadircet, arphaman, kbarton, xazax.hun, 
nemanjai.
njames93 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Extract the ClangTidyOptionsProvider and ClangTidyContext into their own 
headers.
The main purpose of this change is to reduce Dependancies in ClangTidyCheck.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93861

Files:
  clang-tools-extra/clang-tidy/ClangTidy.cpp
  clang-tools-extra/clang-tidy/ClangTidy.h
  clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
  clang-tools-extra/clang-tidy/ClangTidyCheck.h
  clang-tools-extra/clang-tidy/ClangTidyContext.h
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
  clang-tools-extra/clang-tidy/ClangTidyModule.cpp
  clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
  clang-tools-extra/clang-tidy/ClangTidyOptions.h
  clang-tools-extra/clang-tidy/ClangTidyOptionsProvider.h
  
clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
  clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
  clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
  clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
  clang-tools-extra/clangd/Diagnostics.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/TidyProvider.cpp
  clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
  clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
  clang-tools-extra/unittests/clang-tidy/OptionsProviderTest.cpp

Index: clang-tools-extra/unittests/clang-tidy/OptionsProviderTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-tidy/OptionsProviderTest.cpp
+++ clang-tools-extra/unittests/clang-tidy/OptionsProviderTest.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "ClangTidyOptions.h"
+#include "ClangTidyOptionsProvider.h"
 #include "clang/Basic/LLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/VirtualFileSystem.h"
Index: clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
===================================================================
--- clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
+++ clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
@@ -11,7 +11,9 @@
 
 #include "ClangTidy.h"
 #include "ClangTidyCheck.h"
+#include "ClangTidyContext.h"
 #include "ClangTidyDiagnosticConsumer.h"
+#include "ClangTidyOptionsProvider.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendActions.h"
Index: clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
+++ clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
@@ -1,6 +1,7 @@
-#include "ClangTidyOptions.h"
 #include "ClangTidyCheck.h"
+#include "ClangTidyContext.h"
 #include "ClangTidyDiagnosticConsumer.h"
+#include "ClangTidyOptionsProvider.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Testing/Support/Annotations.h"
Index: clang-tools-extra/clangd/TidyProvider.cpp
===================================================================
--- clang-tools-extra/clangd/TidyProvider.cpp
+++ clang-tools-extra/clangd/TidyProvider.cpp
@@ -8,6 +8,7 @@
 
 #include "TidyProvider.h"
 #include "../clang-tidy/ClangTidyModuleRegistry.h"
+#include "../clang-tidy/ClangTidyOptionsProvider.h"
 #include "Config.h"
 #include "support/FileCache.h"
 #include "support/Logger.h"
Index: clang-tools-extra/clangd/ParsedAST.cpp
===================================================================
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -8,8 +8,10 @@
 
 #include "ParsedAST.h"
 #include "../clang-tidy/ClangTidyCheck.h"
+#include "../clang-tidy/ClangTidyContext.h"
 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
 #include "../clang-tidy/ClangTidyModuleRegistry.h"
+#include "../clang-tidy/ClangTidyOptionsProvider.h"
 #include "AST.h"
 #include "Compiler.h"
 #include "Diagnostics.h"
Index: clang-tools-extra/clangd/Diagnostics.cpp
===================================================================
--- clang-tools-extra/clangd/Diagnostics.cpp
+++ clang-tools-extra/clangd/Diagnostics.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Diagnostics.h"
-#include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
+#include "../clang-tidy/ClangTidyContext.h"
 #include "Compiler.h"
 #include "Protocol.h"
 #include "SourceCode.h"
Index: clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -16,7 +16,9 @@
 
 #include "ClangTidyMain.h"
 #include "../ClangTidy.h"
+#include "../ClangTidyContext.h"
 #include "../ClangTidyForceLinker.h"
+#include "../ClangTidyOptionsProvider.h"
 #include "../GlobList.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "llvm/Support/InitLLVM.h"
Index: clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -8,6 +8,7 @@
 
 #include "IdentifierNamingCheck.h"
 
+#include "../ClangTidyContext.h"
 #include "../GlobList.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
Index: clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
+++ clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "TodoCommentCheck.h"
+#include "../ClangTidyContext.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Preprocessor.h"
 
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "PreferMemberInitializerCheck.h"
+#include "../ClangTidyContext.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Lexer.h"
Index: clang-tools-extra/clang-tidy/ClangTidyOptionsProvider.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyOptionsProvider.h
+++ clang-tools-extra/clang-tidy/ClangTidyOptionsProvider.h
@@ -1,4 +1,4 @@
-//===--- ClangTidyOptions.h - clang-tidy ------------------------*- C++ -*-===//
+//===--- ClangTidyOptionsProvider.h - clang-tidy ----------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,138 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONSPROVIDER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONSPROVIDER_H
 
+#include "ClangTidyOptions.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/ADT/Optional.h"
-#include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/ErrorOr.h"
-#include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/Support/VirtualFileSystem.h"
-#include <functional>
-#include <string>
-#include <system_error>
-#include <utility>
-#include <vector>
 
 namespace clang {
 namespace tidy {
-
-/// Contains a list of line ranges in a single file.
-struct FileFilter {
-  /// File name.
-  std::string Name;
-
-  /// LineRange is a pair<start, end> (inclusive).
-  typedef std::pair<unsigned, unsigned> LineRange;
-
-  /// A list of line ranges in this file, for which we show warnings.
-  std::vector<LineRange> LineRanges;
-};
-
-/// Global options. These options are neither stored nor read from
-/// configuration files.
-struct ClangTidyGlobalOptions {
-  /// Output warnings from certain line ranges of certain files only.
-  /// If empty, no warnings will be filtered.
-  std::vector<FileFilter> LineFilter;
-};
-
-/// Contains options for clang-tidy. These options may be read from
-/// configuration files, and may be different for different translation units.
-struct ClangTidyOptions {
-  /// These options are used for all settings that haven't been
-  /// overridden by the \c OptionsProvider.
-  ///
-  /// Allow no checks and no headers by default. This method initializes
-  /// check-specific options by calling \c ClangTidyModule::getModuleOptions()
-  /// of each registered \c ClangTidyModule.
-  static ClangTidyOptions getDefaults();
-
-  /// Overwrites all fields in here by the fields of \p Other that have a value.
-  /// \p Order specifies precedence of \p Other option.
-  ClangTidyOptions &mergeWith(const ClangTidyOptions &Other, unsigned Order);
-
-  /// Creates a new \c ClangTidyOptions instance combined from all fields
-  /// of this instance overridden by the fields of \p Other that have a value.
-  /// \p Order specifies precedence of \p Other option.
-  LLVM_NODISCARD ClangTidyOptions merge(const ClangTidyOptions &Other,
-                                        unsigned Order) const;
-
-  /// Checks filter.
-  llvm::Optional<std::string> Checks;
-
-  /// WarningsAsErrors filter.
-  llvm::Optional<std::string> WarningsAsErrors;
-
-  /// Output warnings from headers matching this filter. Warnings from
-  /// main files will always be displayed.
-  llvm::Optional<std::string> HeaderFilterRegex;
-
-  /// Output warnings from system headers matching \c HeaderFilterRegex.
-  llvm::Optional<bool> SystemHeaders;
-
-  /// Format code around applied fixes with clang-format using this
-  /// style.
-  ///
-  /// Can be one of:
-  ///   * 'none' - don't format code around applied fixes;
-  ///   * 'llvm', 'google', 'mozilla' or other predefined clang-format style
-  ///     names;
-  ///   * 'file' - use the .clang-format file in the closest parent directory of
-  ///     each source file;
-  ///   * '{inline-formatting-style-in-yaml-format}'.
-  ///
-  /// See clang-format documentation for more about configuring format style.
-  llvm::Optional<std::string> FormatStyle;
-
-  /// Specifies the name or e-mail of the user running clang-tidy.
-  ///
-  /// This option is used, for example, to place the correct user name in TODO()
-  /// comments in the relevant check.
-  llvm::Optional<std::string> User;
-
-  /// Helper structure for storing option value with priority of the value.
-  struct ClangTidyValue {
-    ClangTidyValue() : Value(), Priority(0) {}
-    ClangTidyValue(const char *Value) : Value(Value), Priority(0) {}
-    ClangTidyValue(llvm::StringRef Value, unsigned Priority = 0)
-        : Value(Value), Priority(Priority) {}
-
-    std::string Value;
-    /// Priority stores relative precedence of the value loaded from config
-    /// files to disambigute local vs global value from different levels.
-    unsigned Priority;
-  };
-  typedef std::pair<std::string, std::string> StringPair;
-  typedef llvm::StringMap<ClangTidyValue> OptionMap;
-
-  /// Key-value mapping used to store check-specific options.
-  OptionMap CheckOptions;
-
-  typedef std::vector<std::string> ArgList;
-
-  /// Add extra compilation arguments to the end of the list.
-  llvm::Optional<ArgList> ExtraArgs;
-
-  /// Add extra compilation arguments to the start of the list.
-  llvm::Optional<ArgList> ExtraArgsBefore;
-
-  /// Only used in the FileOptionsProvider and ConfigOptionsProvider. If true
-  /// and using a FileOptionsProvider, it will take a configuration file in the
-  /// parent directory (if any exists) and apply this config file on top of the
-  /// parent one. IF true and using a ConfigOptionsProvider, it will apply this
-  /// config on top of any configuation file it finds in the directory using the
-  /// same logic as FileOptionsProvider. If false or missing, only this
-  /// configuration file will be used.
-  llvm::Optional<bool> InheritParentConfig;
-
-  /// Use colors in diagnostics. If missing, it will be auto detected.
-  llvm::Optional<bool> UseColor;
-};
-
 /// Abstract interface for retrieving various ClangTidy options.
 class ClangTidyOptionsProvider {
 public:
@@ -319,8 +196,7 @@
 
 /// Serializes configuration to a YAML-encoded string.
 std::string configurationAsText(const ClangTidyOptions &Options);
+} // namespace tidy
+} // namespace clang
 
-} // end namespace tidy
-} // end namespace clang
-
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONSPROVIDER_H
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/ClangTidyOptions.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyOptions.h
+++ clang-tools-extra/clang-tidy/ClangTidyOptions.h
@@ -9,16 +9,11 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
 
-#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
-#include "llvm/Support/ErrorOr.h"
-#include "llvm/Support/MemoryBufferRef.h"
-#include "llvm/Support/VirtualFileSystem.h"
-#include <functional>
+#include "llvm/Support/Compiler.h"
 #include <string>
-#include <system_error>
 #include <utility>
 #include <vector>
 
@@ -138,188 +133,6 @@
   llvm::Optional<bool> UseColor;
 };
 
-/// Abstract interface for retrieving various ClangTidy options.
-class ClangTidyOptionsProvider {
-public:
-  static const char OptionsSourceTypeDefaultBinary[];
-  static const char OptionsSourceTypeCheckCommandLineOption[];
-  static const char OptionsSourceTypeConfigCommandLineOption[];
-
-  virtual ~ClangTidyOptionsProvider() {}
-
-  /// Returns global options, which are independent of the file.
-  virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
-
-  /// ClangTidyOptions and its source.
-  //
-  /// clang-tidy has 3 types of the sources in order of increasing priority:
-  ///    * clang-tidy binary.
-  ///    * '-config' commandline option or a specific configuration file. If the
-  ///       commandline option is specified, clang-tidy will ignore the
-  ///       configuration file.
-  ///    * '-checks' commandline option.
-  typedef std::pair<ClangTidyOptions, std::string> OptionsSource;
-
-  /// Returns an ordered vector of OptionsSources, in order of increasing
-  /// priority.
-  virtual std::vector<OptionsSource>
-  getRawOptions(llvm::StringRef FileName) = 0;
-
-  /// Returns options applying to a specific translation unit with the
-  /// specified \p FileName.
-  ClangTidyOptions getOptions(llvm::StringRef FileName);
-};
-
-/// Implementation of the \c ClangTidyOptionsProvider interface, which
-/// returns the same options for all files.
-class DefaultOptionsProvider : public ClangTidyOptionsProvider {
-public:
-  DefaultOptionsProvider(ClangTidyGlobalOptions GlobalOptions,
-                         ClangTidyOptions Options)
-      : GlobalOptions(std::move(GlobalOptions)),
-        DefaultOptions(std::move(Options)) {}
-  const ClangTidyGlobalOptions &getGlobalOptions() override {
-    return GlobalOptions;
-  }
-  std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
-
-private:
-  ClangTidyGlobalOptions GlobalOptions;
-  ClangTidyOptions DefaultOptions;
-};
-
-class FileOptionsBaseProvider : public DefaultOptionsProvider {
-protected:
-  // A pair of configuration file base name and a function parsing
-  // configuration from text in the corresponding format.
-  typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
-                                     llvm::MemoryBufferRef)>>
-      ConfigFileHandler;
-
-  /// Configuration file handlers listed in the order of priority.
-  ///
-  /// Custom configuration file formats can be supported by constructing the
-  /// list of handlers and passing it to the appropriate \c FileOptionsProvider
-  /// constructor. E.g. initialization of a \c FileOptionsProvider with support
-  /// of a custom configuration file format for files named ".my-tidy-config"
-  /// could look similar to this:
-  /// \code
-  /// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
-  /// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
-  /// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
-  /// return std::make_unique<FileOptionsProvider>(
-  ///     GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
-  /// \endcode
-  ///
-  /// With the order of handlers shown above, the ".my-tidy-config" file would
-  /// take precedence over ".clang-tidy" if both reside in the same directory.
-  typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
-
-  FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
-                          ClangTidyOptions DefaultOptions,
-                          ClangTidyOptions OverrideOptions,
-                          llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
-
-  FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
-                          ClangTidyOptions DefaultOptions,
-                          ClangTidyOptions OverrideOptions,
-                          ConfigFileHandlers ConfigHandlers);
-
-protected:
-  void addRawFileOptions(llvm::StringRef AbsolutePath,
-                         std::vector<OptionsSource> &CurOptions);
-
-  /// Try to read configuration files from \p Directory using registered
-  /// \c ConfigHandlers.
-  llvm::Optional<OptionsSource> tryReadConfigFile(llvm::StringRef Directory);
-
-  llvm::StringMap<OptionsSource> CachedOptions;
-  ClangTidyOptions OverrideOptions;
-  ConfigFileHandlers ConfigHandlers;
-  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
-};
-
-/// Implementation of ClangTidyOptions interface, which is used for
-/// '-config' command-line option.
-class ConfigOptionsProvider : public FileOptionsBaseProvider {
-public:
-  ConfigOptionsProvider(
-      ClangTidyGlobalOptions GlobalOptions, ClangTidyOptions DefaultOptions,
-      ClangTidyOptions ConfigOptions, ClangTidyOptions OverrideOptions,
-      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
-  std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
-
-private:
-  ClangTidyOptions ConfigOptions;
-};
-
-/// Implementation of the \c ClangTidyOptionsProvider interface, which
-/// tries to find a configuration file in the closest parent directory of each
-/// source file.
-///
-/// By default, files named ".clang-tidy" will be considered, and the
-/// \c clang::tidy::parseConfiguration function will be used for parsing, but a
-/// custom set of configuration file names and parsing functions can be
-/// specified using the appropriate constructor.
-class FileOptionsProvider : public FileOptionsBaseProvider {
-public:
-  /// Initializes the \c FileOptionsProvider instance.
-  ///
-  /// \param GlobalOptions are just stored and returned to the caller of
-  /// \c getGlobalOptions.
-  ///
-  /// \param DefaultOptions are used for all settings not specified in a
-  /// configuration file.
-  ///
-  /// If any of the \param OverrideOptions fields are set, they will override
-  /// whatever options are read from the configuration file.
-  FileOptionsProvider(
-      ClangTidyGlobalOptions GlobalOptions, ClangTidyOptions DefaultOptions,
-      ClangTidyOptions OverrideOptions,
-      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
-
-  /// Initializes the \c FileOptionsProvider instance with a custom set
-  /// of configuration file handlers.
-  ///
-  /// \param GlobalOptions are just stored and returned to the caller of
-  /// \c getGlobalOptions.
-  ///
-  /// \param DefaultOptions are used for all settings not specified in a
-  /// configuration file.
-  ///
-  /// If any of the \param OverrideOptions fields are set, they will override
-  /// whatever options are read from the configuration file.
-  ///
-  /// \param ConfigHandlers specifies a custom set of configuration file
-  /// handlers. Each handler is a pair of configuration file name and a function
-  /// that can parse configuration from this file type. The configuration files
-  /// in each directory are searched for in the order of appearance in
-  /// \p ConfigHandlers.
-  FileOptionsProvider(ClangTidyGlobalOptions GlobalOptions,
-                      ClangTidyOptions DefaultOptions,
-                      ClangTidyOptions OverrideOptions,
-                      ConfigFileHandlers ConfigHandlers);
-
-  std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
-};
-
-/// Parses LineFilter from JSON and stores it to the \p Options.
-std::error_code parseLineFilter(llvm::StringRef LineFilter,
-                                ClangTidyGlobalOptions &Options);
-
-/// Parses configuration from JSON and returns \c ClangTidyOptions or an
-/// error.
-llvm::ErrorOr<ClangTidyOptions>
-parseConfiguration(llvm::MemoryBufferRef Config);
-
-using DiagCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;
-
-llvm::ErrorOr<ClangTidyOptions>
-parseConfigurationWithDiags(llvm::MemoryBufferRef Config, DiagCallback Handler);
-
-/// Serializes configuration to a YAML-encoded string.
-std::string configurationAsText(const ClangTidyOptions &Options);
-
 } // end namespace tidy
 } // end namespace clang
 
Index: clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
@@ -8,6 +8,7 @@
 
 #include "ClangTidyOptions.h"
 #include "ClangTidyModuleRegistry.h"
+#include "ClangTidyOptionsProvider.h"
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Debug.h"
Index: clang-tools-extra/clang-tidy/ClangTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyModule.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyModule.cpp
@@ -12,6 +12,7 @@
 
 #include "ClangTidyModule.h"
 #include "ClangTidyCheck.h"
+#include "ClangTidyContext.h"
 
 namespace clang {
 namespace tidy {
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -16,7 +16,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "ClangTidyDiagnosticConsumer.h"
+#include "ClangTidyContext.h"
 #include "ClangTidyOptions.h"
+#include "ClangTidyOptionsProvider.h"
 #include "GlobList.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTDiagnostic.h"
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -9,11 +9,9 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
 
-#include "ClangTidyOptions.h"
 #include "ClangTidyProfiling.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Tooling/Core/Diagnostic.h"
-#include "llvm/ADT/DenseMap.h"
 #include "llvm/Support/Regex.h"
 
 namespace clang {
@@ -30,6 +28,8 @@
 
 namespace tidy {
 
+class ClangTidyContext;
+
 /// A detected error complete with information to display diagnostic and
 /// automatic fix.
 ///
@@ -45,173 +45,6 @@
   std::vector<std::string> EnabledDiagnosticAliases;
 };
 
-/// Contains displayed and ignored diagnostic counters for a ClangTidy
-/// run.
-struct ClangTidyStats {
-  ClangTidyStats()
-      : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
-        ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
-
-  unsigned ErrorsDisplayed;
-  unsigned ErrorsIgnoredCheckFilter;
-  unsigned ErrorsIgnoredNOLINT;
-  unsigned ErrorsIgnoredNonUserCode;
-  unsigned ErrorsIgnoredLineFilter;
-
-  unsigned errorsIgnored() const {
-    return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
-           ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
-  }
-};
-
-/// Every \c ClangTidyCheck reports errors through a \c DiagnosticsEngine
-/// provided by this context.
-///
-/// A \c ClangTidyCheck always has access to the active context to report
-/// warnings like:
-/// \code
-/// Context->Diag(Loc, "Single-argument constructors must be explicit")
-///     << FixItHint::CreateInsertion(Loc, "explicit ");
-/// \endcode
-class ClangTidyContext {
-public:
-  /// Initializes \c ClangTidyContext instance.
-  ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
-                   bool AllowEnablingAnalyzerAlphaCheckers = false);
-  /// Sets the DiagnosticsEngine that diag() will emit diagnostics to.
-  // FIXME: this is required initialization, and should be a constructor param.
-  // Fix the context -> diag engine -> consumer -> context initialization cycle.
-  void setDiagnosticsEngine(DiagnosticsEngine *DiagEngine) {
-    this->DiagEngine = DiagEngine;
-  }
-
-  ~ClangTidyContext();
-
-  /// Report any errors detected using this method.
-  ///
-  /// This is still under heavy development and will likely change towards using
-  /// tablegen'd diagnostic IDs.
-  /// FIXME: Figure out a way to manage ID spaces.
-  DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
-                         StringRef Message,
-                         DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
-
-  DiagnosticBuilder diag(StringRef CheckName, StringRef Message,
-                         DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
-
-  /// Report any errors to do with reading the configuration using this method.
-  DiagnosticBuilder
-  configurationDiag(StringRef Message,
-                    DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
-
-  /// Sets the \c SourceManager of the used \c DiagnosticsEngine.
-  ///
-  /// This is called from the \c ClangTidyCheck base class.
-  void setSourceManager(SourceManager *SourceMgr);
-
-  /// Should be called when starting to process new translation unit.
-  void setCurrentFile(StringRef File);
-
-  /// Returns the main file name of the current translation unit.
-  StringRef getCurrentFile() const { return CurrentFile; }
-
-  /// Sets ASTContext for the current translation unit.
-  void setASTContext(ASTContext *Context);
-
-  /// Gets the language options from the AST context.
-  const LangOptions &getLangOpts() const { return LangOpts; }
-
-  /// Returns the name of the clang-tidy check which produced this
-  /// diagnostic ID.
-  std::string getCheckName(unsigned DiagnosticID) const;
-
-  /// Returns \c true if the check is enabled for the \c CurrentFile.
-  ///
-  /// The \c CurrentFile can be changed using \c setCurrentFile.
-  bool isCheckEnabled(StringRef CheckName) const;
-
-  /// Returns \c true if the check should be upgraded to error for the
-  /// \c CurrentFile.
-  bool treatAsError(StringRef CheckName) const;
-
-  /// Returns global options.
-  const ClangTidyGlobalOptions &getGlobalOptions() const;
-
-  /// Returns options for \c CurrentFile.
-  ///
-  /// The \c CurrentFile can be changed using \c setCurrentFile.
-  const ClangTidyOptions &getOptions() const;
-
-  /// Returns options for \c File. Does not change or depend on
-  /// \c CurrentFile.
-  ClangTidyOptions getOptionsForFile(StringRef File) const;
-
-  /// Returns \c ClangTidyStats containing issued and ignored diagnostic
-  /// counters.
-  const ClangTidyStats &getStats() const { return Stats; }
-
-  /// Control profile collection in clang-tidy.
-  void setEnableProfiling(bool Profile);
-  bool getEnableProfiling() const { return Profile; }
-
-  /// Control storage of profile date.
-  void setProfileStoragePrefix(StringRef ProfilePrefix);
-  llvm::Optional<ClangTidyProfiling::StorageParams>
-  getProfileStorageParams() const;
-
-  /// Should be called when starting to process new translation unit.
-  void setCurrentBuildDirectory(StringRef BuildDirectory) {
-    CurrentBuildDirectory = std::string(BuildDirectory);
-  }
-
-  /// Returns build directory of the current translation unit.
-  const std::string &getCurrentBuildDirectory() {
-    return CurrentBuildDirectory;
-  }
-
-  /// If the experimental alpha checkers from the static analyzer can be
-  /// enabled.
-  bool canEnableAnalyzerAlphaCheckers() const {
-    return AllowEnablingAnalyzerAlphaCheckers;
-  }
-
-  using DiagLevelAndFormatString = std::pair<DiagnosticIDs::Level, std::string>;
-  DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID,
-                                                       SourceLocation Loc) {
-    return DiagLevelAndFormatString(
-        static_cast<DiagnosticIDs::Level>(
-            DiagEngine->getDiagnosticLevel(DiagnosticID, Loc)),
-        std::string(
-            DiagEngine->getDiagnosticIDs()->getDescription(DiagnosticID)));
-  }
-
-private:
-  // Writes to Stats.
-  friend class ClangTidyDiagnosticConsumer;
-
-  DiagnosticsEngine *DiagEngine;
-  std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
-
-  std::string CurrentFile;
-  ClangTidyOptions CurrentOptions;
-  class CachedGlobList;
-  std::unique_ptr<CachedGlobList> CheckFilter;
-  std::unique_ptr<CachedGlobList> WarningAsErrorFilter;
-
-  LangOptions LangOpts;
-
-  ClangTidyStats Stats;
-
-  std::string CurrentBuildDirectory;
-
-  llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
-
-  bool Profile;
-  std::string ProfilePrefix;
-
-  bool AllowEnablingAnalyzerAlphaCheckers;
-};
-
 /// Check whether a given diagnostic should be suppressed due to the presence
 /// of a "NOLINT" suppression comment.
 /// This is exposed so that other tools that present clang-tidy diagnostics
Index: clang-tools-extra/clang-tidy/ClangTidyContext.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyContext.h
+++ clang-tools-extra/clang-tidy/ClangTidyContext.h
@@ -1,4 +1,4 @@
-//===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- C++ -*-===//
+//===--- ClangTidyContext.h - clang-tidy ------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,44 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
-
 #include "ClangTidyOptions.h"
 #include "ClangTidyProfiling.h"
 #include "clang/Basic/Diagnostic.h"
-#include "clang/Tooling/Core/Diagnostic.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/Support/Regex.h"
+#include <memory>
+#include <string>
+#include <utility>
 
 namespace clang {
-
 class ASTContext;
-class CompilerInstance;
-class SourceManager;
-namespace ast_matchers {
-class MatchFinder;
-}
-namespace tooling {
-class CompilationDatabase;
-}
-
 namespace tidy {
 
-/// A detected error complete with information to display diagnostic and
-/// automatic fix.
-///
-/// This is used as an intermediate format to transport Diagnostics without a
-/// dependency on a SourceManager.
-///
-/// FIXME: Make Diagnostics flexible enough to support this directly.
-struct ClangTidyError : tooling::Diagnostic {
-  ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory,
-                 bool IsWarningAsError);
-
-  bool IsWarningAsError;
-  std::vector<std::string> EnabledDiagnosticAliases;
-};
+class ClangTidyOptionsProvider;
 
 /// Contains displayed and ignored diagnostic counters for a ClangTidy
 /// run.
@@ -211,68 +185,5 @@
 
   bool AllowEnablingAnalyzerAlphaCheckers;
 };
-
-/// Check whether a given diagnostic should be suppressed due to the presence
-/// of a "NOLINT" suppression comment.
-/// This is exposed so that other tools that present clang-tidy diagnostics
-/// (such as clangd) can respect the same suppression rules as clang-tidy.
-/// This does not handle suppression of notes following a suppressed diagnostic;
-/// that is left to the caller is it requires maintaining state in between calls
-/// to this function.
-/// If `AllowIO` is false, the function does not attempt to read source files
-/// from disk which are not already mapped into memory; such files are treated
-/// as not containing a suppression comment.
-bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel,
-                              const Diagnostic &Info, ClangTidyContext &Context,
-                              bool AllowIO = true);
-
-/// A diagnostic consumer that turns each \c Diagnostic into a
-/// \c SourceManager-independent \c ClangTidyError.
-//
-// FIXME: If we move away from unit-tests, this can be moved to a private
-// implementation file.
-class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
-public:
-  ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx,
-                              DiagnosticsEngine *ExternalDiagEngine = nullptr,
-                              bool RemoveIncompatibleErrors = true);
-
-  // FIXME: The concept of converting between FixItHints and Replacements is
-  // more generic and should be pulled out into a more useful Diagnostics
-  // library.
-  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
-                        const Diagnostic &Info) override;
-
-  // Retrieve the diagnostics that were captured.
-  std::vector<ClangTidyError> take();
-
-private:
-  void finalizeLastError();
-  void removeIncompatibleErrors();
-  void removeDuplicatedDiagnosticsOfAliasCheckers();
-
-  /// Returns the \c HeaderFilter constructed for the options set in the
-  /// context.
-  llvm::Regex *getHeaderFilter();
-
-  /// Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
-  /// according to the diagnostic \p Location.
-  void checkFilters(SourceLocation Location, const SourceManager &Sources);
-  bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
-
-  void forwardDiagnostic(const Diagnostic &Info);
-
-  ClangTidyContext &Context;
-  DiagnosticsEngine *ExternalDiagEngine;
-  bool RemoveIncompatibleErrors;
-  std::vector<ClangTidyError> Errors;
-  std::unique_ptr<llvm::Regex> HeaderFilter;
-  bool LastErrorRelatesToUserCode;
-  bool LastErrorPassesLineFilter;
-  bool LastErrorWasIgnored;
-};
-
-} // end namespace tidy
-} // end namespace clang
-
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
+} // namespace tidy
+} // namespace clang
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/ClangTidyCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyCheck.h
+++ clang-tools-extra/clang-tidy/ClangTidyCheck.h
@@ -9,7 +9,6 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYCHECK_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYCHECK_H
 
-#include "ClangTidyDiagnosticConsumer.h"
 #include "ClangTidyOptions.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Basic/Diagnostic.h"
@@ -26,6 +25,8 @@
 
 namespace tidy {
 
+class ClangTidyContext;
+
 /// This class should be specialized by any enum type that needs to be converted
 /// to and from an \ref llvm::StringRef.
 template <class T> struct OptionEnumMapping {
@@ -508,9 +509,9 @@
 protected:
   OptionsView Options;
   /// Returns the main file name of the current translation unit.
-  StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
+  StringRef getCurrentMainFile() const;
   /// Returns the language options from the context.
-  const LangOptions &getLangOpts() const { return Context->getLangOpts(); }
+  const LangOptions &getLangOpts() const;
 };
 
 /// Read a named option from the ``Context`` and parse it as a bool.
Index: clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ClangTidyCheck.h"
+#include "ClangTidyContext.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
@@ -240,5 +241,11 @@
   return llvm::None;
 }
 
+llvm::StringRef ClangTidyCheck::getCurrentMainFile() const {
+  return Context->getCurrentFile();
+}
+const LangOptions &ClangTidyCheck::getLangOpts() const {
+  return Context->getLangOpts();
+}
 } // namespace tidy
 } // namespace clang
Index: clang-tools-extra/clang-tidy/ClangTidy.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.h
+++ clang-tools-extra/clang-tidy/ClangTidy.h
@@ -11,6 +11,7 @@
 
 #include "ClangTidyDiagnosticConsumer.h"
 #include "ClangTidyOptions.h"
+#include "llvm/Support/VirtualFileSystem.h"
 #include <memory>
 #include <vector>
 
Index: clang-tools-extra/clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -16,8 +16,10 @@
 
 #include "ClangTidy.h"
 #include "ClangTidyCheck.h"
+#include "ClangTidyContext.h"
 #include "ClangTidyDiagnosticConsumer.h"
 #include "ClangTidyModuleRegistry.h"
+#include "ClangTidyOptionsProvider.h"
 #include "ClangTidyProfiling.h"
 #include "ExpandModularHeadersPPCallbacks.h"
 #include "clang-tidy-config.h"
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to