Index: clang-tidy/ClangTidy.h =================================================================== --- clang-tidy/ClangTidy.h +++ clang-tidy/ClangTidy.h @@ -6,11 +6,14 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// +// This file defines the public ClangTidy API exposed to checks and modules. +// +// It should remain as stable as possible, as many out-of-tree checks exist. +//===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H -#include "ClangTidyDiagnosticConsumer.h" #include "ClangTidyOptions.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/Diagnostic.h" @@ -30,6 +33,8 @@ } namespace tidy { +// Checks should treat ClangTidyContext as opaque. +class ClangTidyContext; /// \brief Provides access to the ``ClangTidyCheck`` options via check-local /// names. @@ -131,12 +136,7 @@ /// Derived classes must implement the constructor with this signature or /// delegate it. If a check needs to read options, it can do this in the /// constructor using the Options.get() methods below. - ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context) - : CheckName(CheckName), Context(Context), - Options(CheckName, Context->getOptions().CheckOptions) { - assert(Context != nullptr); - assert(!CheckName.empty()); - } + ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context); /// \brief Override this to register ``PPCallbacks`` with ``Compiler``. /// @@ -181,10 +181,10 @@ protected: OptionsView Options; - /// \brief Returns the main file name of the current translation unit. - StringRef getCurrentMainFile() const { return Context->getCurrentFile(); } - /// \brief Returns the language options from the context. - LangOptions getLangOpts() const { return Context->getLangOpts(); } + /// Returns the main file name of the current translation unit. + StringRef getCurrentMainFile() const; + /// Returns the language options from the current translation unit. + LangOptions getLangOpts() const; }; class ClangTidyCheckFactories; @@ -247,12 +247,6 @@ unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr BaseFS); -/// \brief Serializes replacements into YAML and writes them to the specified -/// output stream. -void exportReplacements(StringRef MainFilePath, - const std::vector &Errors, - raw_ostream &OS); - } // end namespace tidy } // end namespace clang Index: clang-tidy/ClangTidy.cpp =================================================================== --- clang-tidy/ClangTidy.cpp +++ clang-tidy/ClangTidy.cpp @@ -39,9 +39,7 @@ #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" #endif // CLANG_ENABLE_STATIC_ANALYZER -#include "clang/Tooling/DiagnosticsYaml.h" #include "clang/Tooling/Refactoring.h" -#include "clang/Tooling/ReplacementsYaml.h" #include "clang/Tooling/Tooling.h" #include "llvm/Support/Process.h" #include "llvm/Support/Signals.h" @@ -364,7 +362,9 @@ Context.setCurrentBuildDirectory(WorkingDir.get()); std::vector> Checks; - CheckFactories->createChecks(&Context, Checks); + CheckFactories->createChecks( + [this](StringRef Name) { return Context.isCheckEnabled(Name); }, &Context, + Checks); ast_matchers::MatchFinder::MatchFinderOptions FinderOptions; @@ -429,12 +429,21 @@ ClangTidyOptions::OptionMap ClangTidyASTConsumerFactory::getCheckOptions() { ClangTidyOptions::OptionMap Options; std::vector> Checks; - CheckFactories->createChecks(&Context, Checks); + CheckFactories->createChecks( + [this](StringRef Name) { return Context.isCheckEnabled(Name); }, &Context, + Checks); for (const auto &Check : Checks) Check->storeOptions(Options); return Options; } +ClangTidyCheck::ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context) + : CheckName(CheckName), Context(Context), + Options(CheckName, Context->getOptions().CheckOptions) { + assert(Context != nullptr); + assert(!CheckName.empty()); +} + DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level) { return Context->diag(CheckName, Loc, Message, Level); @@ -445,6 +454,14 @@ check(Result); } +StringRef ClangTidyCheck::getCurrentMainFile() const { + return Context->getCurrentFile(); +} + +LangOptions ClangTidyCheck::getLangOpts() const { + return Context->getLangOpts(); +} + OptionsView::OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions) : NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {} @@ -614,19 +631,5 @@ WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount(); } -void exportReplacements(const llvm::StringRef MainFilePath, - const std::vector &Errors, - raw_ostream &OS) { - TranslationUnitDiagnostics TUD; - TUD.MainSourceFile = MainFilePath; - for (const auto &Error : Errors) { - tooling::Diagnostic Diag = Error; - TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag); - } - - yaml::Output YAML(OS); - YAML << TUD; -} - } // namespace tidy } // namespace clang Index: clang-tidy/ClangTidyDiagnosticConsumer.h =================================================================== --- clang-tidy/ClangTidyDiagnosticConsumer.h +++ clang-tidy/ClangTidyDiagnosticConsumer.h @@ -6,6 +6,9 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// +// This file contains private APIs that checks should not depend on. +// FIXME: the contents have diverged significantly from the name, split this up. +//===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H @@ -48,6 +51,11 @@ bool IsWarningAsError; }; +/// Serializes replacements into YAML and writes them to the specified stream. +void exportReplacements(StringRef MainFilePath, + const std::vector &Errors, + raw_ostream &OS); + /// \brief Read-only set of strings represented as a list of positive and /// negative globs. Positive globs add all matched strings to the set, negative /// globs remove them in the order of appearance in the list. Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp =================================================================== --- clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -21,6 +21,8 @@ #include "clang/AST/ASTDiagnostic.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Frontend/DiagnosticRenderer.h" +#include "clang/Tooling/DiagnosticsYaml.h" +#include "clang/Tooling/ReplacementsYaml.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include @@ -113,6 +115,20 @@ : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory), IsWarningAsError(IsWarningAsError) {} +void tidy::exportReplacements(const llvm::StringRef MainFilePath, + const std::vector &Errors, + raw_ostream &OS) { + clang::tooling::TranslationUnitDiagnostics TUD; + TUD.MainSourceFile = MainFilePath; + for (const auto &Error : Errors) { + tooling::Diagnostic Diag = Error; + TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag); + } + + llvm::yaml::Output YAML(OS); + YAML << TUD; +} + // Returns true if GlobList starts with the negative indicator ('-'), removes it // from the GlobList. static bool ConsumeNegativeIndicator(StringRef &GlobList) { Index: clang-tidy/ClangTidyModule.h =================================================================== --- clang-tidy/ClangTidyModule.h +++ clang-tidy/ClangTidyModule.h @@ -63,11 +63,11 @@ }); } - /// \brief Create instances of all checks matching \p CheckRegexString and - /// store them in \p Checks. + /// Create instances of all checks that match the filter and store them. /// /// The caller takes ownership of the return \c ClangTidyChecks. - void createChecks(ClangTidyContext *Context, + void createChecks(llvm::function_ref Filter, + ClangTidyContext *Context, std::vector> &Checks); typedef std::map FactoryMap; Index: clang-tidy/ClangTidyModule.cpp =================================================================== --- clang-tidy/ClangTidyModule.cpp +++ clang-tidy/ClangTidyModule.cpp @@ -22,10 +22,11 @@ } void ClangTidyCheckFactories::createChecks( + llvm::function_ref Filter, ClangTidyContext *Context, std::vector> &Checks) { for (const auto &Factory : Factories) { - if (Context->isCheckEnabled(Factory.first)) + if (Filter(Factory.first)) Checks.emplace_back(Factory.second(Factory.first, Context)); } } Index: clang-tidy/fuchsia/RestrictSystemIncludesCheck.h =================================================================== --- clang-tidy/fuchsia/RestrictSystemIncludesCheck.h +++ clang-tidy/fuchsia/RestrictSystemIncludesCheck.h @@ -11,6 +11,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H #include "../ClangTidy.h" +// FIXME: stop using GlobList, it's not part of the public interface. #include "../ClangTidyDiagnosticConsumer.h" #include "../utils/OptionsUtils.h" Index: clang-tidy/google/TodoCommentCheck.cpp =================================================================== --- clang-tidy/google/TodoCommentCheck.cpp +++ clang-tidy/google/TodoCommentCheck.cpp @@ -8,6 +8,8 @@ //===----------------------------------------------------------------------===// #include "TodoCommentCheck.h" +// FIXME: provide some other way to get at the username. +#include "../ClangTidyDiagnosticConsumer.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/Preprocessor.h" @@ -52,9 +54,10 @@ }; TodoCommentCheck::TodoCommentCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context), - Handler(llvm::make_unique( - *this, Context->getOptions().User)) {} + : ClangTidyCheck(Name, Context) { + Handler = + llvm::make_unique(*this, Context->getOptions().User); +} void TodoCommentCheck::registerPPCallbacks(CompilerInstance &Compiler) { Compiler.getPreprocessor().addCommentHandler(Handler.get()); Index: clang-tidy/plugin/ClangTidyPlugin.cpp =================================================================== --- clang-tidy/plugin/ClangTidyPlugin.cpp +++ clang-tidy/plugin/ClangTidyPlugin.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "../ClangTidy.h" +#include "../ClangTidyDiagnosticConsumer.h" #include "../ClangTidyModule.h" #include "clang/Config/config.h" #include "clang/Frontend/CompilerInstance.h" Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -16,6 +16,7 @@ //===----------------------------------------------------------------------===// #include "../ClangTidy.h" +#include "../ClangTidyDiagnosticConsumer.h" #include "clang/Config/config.h" #include "clang/Tooling/CommonOptionsParser.h" #include "llvm/Support/Process.h"