diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt b/clang-tools-extra/clang-tidy/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/CMakeLists.txt @@ -117,7 +117,6 @@ # Other subtargets. These may reference ALL_CLANG_TIDY_CHECKS # and must be below its definition. -add_subdirectory(plugin) add_subdirectory(tool) add_subdirectory(utils) diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -542,7 +542,7 @@ Context.setEnableProfiling(EnableCheckProfile); Context.setProfileStoragePrefix(StoreCheckProfile); - ClangTidyDiagnosticConsumer DiagConsumer(Context, nullptr, true, ApplyAnyFix); + ClangTidyDiagnosticConsumer DiagConsumer(Context, true, ApplyAnyFix); DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions(), &DiagConsumer, /*ShouldOwnClient=*/false); Context.setDiagnosticsEngine(&DE); diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -250,7 +250,6 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer { public: ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, - DiagnosticsEngine *ExternalDiagEngine = nullptr, bool RemoveIncompatibleErrors = true, bool GetFixesFromNotes = false, bool EnableNolintBlocks = true); @@ -278,10 +277,7 @@ 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; bool GetFixesFromNotes; bool EnableNolintBlocks; diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -273,14 +273,12 @@ } ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer( - ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine, - bool RemoveIncompatibleErrors, bool GetFixesFromNotes, - bool EnableNolintBlocks) - : Context(Ctx), ExternalDiagEngine(ExternalDiagEngine), - RemoveIncompatibleErrors(RemoveIncompatibleErrors), - GetFixesFromNotes(GetFixesFromNotes), - EnableNolintBlocks(EnableNolintBlocks), LastErrorRelatesToUserCode(false), - LastErrorPassesLineFilter(false), LastErrorWasIgnored(false) {} + ClangTidyContext &Ctx, bool RemoveIncompatibleErrors, + bool GetFixesFromNotes, bool EnableNolintBlocks) + : Context(Ctx), RemoveIncompatibleErrors(RemoveIncompatibleErrors), + GetFixesFromNotes(GetFixesFromNotes), EnableNolintBlocks(EnableNolintBlocks), + LastErrorRelatesToUserCode(false), LastErrorPassesLineFilter(false), + LastErrorWasIgnored(false) {} void ClangTidyDiagnosticConsumer::finalizeLastError() { if (!Errors.empty()) { @@ -600,22 +598,16 @@ IsWarningAsError); } - if (ExternalDiagEngine) { - // If there is an external diagnostics engine, like in the - // ClangTidyPluginAction case, forward the diagnostics to it. - forwardDiagnostic(Info); - } else { - ClangTidyDiagnosticRenderer Converter( - Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(), - Errors.back()); - SmallString<100> Message; - Info.FormatDiagnostic(Message); - FullSourceLoc Loc; - if (Info.getLocation().isValid() && Info.hasSourceManager()) + ClangTidyDiagnosticRenderer Converter( + Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(), + Errors.back()); + SmallString<100> Message; + Info.FormatDiagnostic(Message); + FullSourceLoc Loc; + if (Info.getLocation().isValid() && Info.hasSourceManager()) Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); - Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(), - Info.getFixItHints()); - } + Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(), + Info.getFixItHints()); if (Info.hasSourceManager()) checkFilters(Info.getLocation(), Info.getSourceManager()); @@ -643,71 +635,6 @@ return false; } -void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) { - // Acquire a diagnostic ID also in the external diagnostics engine. - auto DiagLevelAndFormatString = - Context.getDiagLevelAndFormatString(Info.getID(), Info.getLocation()); - unsigned ExternalID = ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID( - DiagLevelAndFormatString.first, DiagLevelAndFormatString.second); - - // Forward the details. - auto Builder = ExternalDiagEngine->Report(Info.getLocation(), ExternalID); - for (auto Hint : Info.getFixItHints()) - Builder << Hint; - for (auto Range : Info.getRanges()) - Builder << Range; - for (unsigned Index = 0; Index < Info.getNumArgs(); ++Index) { - DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index); - switch (Kind) { - case clang::DiagnosticsEngine::ak_std_string: - Builder << Info.getArgStdStr(Index); - break; - case clang::DiagnosticsEngine::ak_c_string: - Builder << Info.getArgCStr(Index); - break; - case clang::DiagnosticsEngine::ak_sint: - Builder << Info.getArgSInt(Index); - break; - case clang::DiagnosticsEngine::ak_uint: - Builder << Info.getArgUInt(Index); - break; - case clang::DiagnosticsEngine::ak_tokenkind: - Builder << static_cast(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_identifierinfo: - Builder << Info.getArgIdentifier(Index); - break; - case clang::DiagnosticsEngine::ak_qual: - Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_qualtype: - Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_declarationname: - Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_nameddecl: - Builder << reinterpret_cast(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_nestednamespec: - Builder << reinterpret_cast(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_declcontext: - Builder << reinterpret_cast(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_qualtype_pair: - assert(false); // This one is not passed around. - break; - case clang::DiagnosticsEngine::ak_attr: - Builder << reinterpret_cast(Info.getRawArg(Index)); - break; - case clang::DiagnosticsEngine::ak_addrspace: - Builder << static_cast(Info.getRawArg(Index)); - break; - } - } -} - void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location, const SourceManager &Sources) { // Invalid location may mean a diagnostic in a command line, don't skip these. diff --git a/clang-tools-extra/clang-tidy/plugin/CMakeLists.txt b/clang-tools-extra/clang-tidy/plugin/CMakeLists.txt deleted file mode 100644 --- a/clang-tools-extra/clang-tidy/plugin/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -add_clang_library(clangTidyPlugin - ClangTidyPlugin.cpp - - LINK_LIBS - clangTidy - ${ALL_CLANG_TIDY_CHECKS} - - DEPENDS - omp_gen - ) - -clang_target_link_libraries(clangTidyPlugin - PRIVATE - clangAST - clangASTMatchers - clangBasic - clangFrontend - clangSema - clangTooling - ) diff --git a/clang-tools-extra/clang-tidy/plugin/ClangTidyPlugin.cpp b/clang-tools-extra/clang-tidy/plugin/ClangTidyPlugin.cpp deleted file mode 100644 --- a/clang-tools-extra/clang-tidy/plugin/ClangTidyPlugin.cpp +++ /dev/null @@ -1,86 +0,0 @@ -//===- ClangTidyPlugin.cpp - clang-tidy as a clang plugin -----------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "../ClangTidy.h" -#include "../ClangTidyDiagnosticConsumer.h" -#include "../ClangTidyForceLinker.h" -#include "../ClangTidyModule.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FrontendPluginRegistry.h" -#include "clang/Frontend/MultiplexConsumer.h" - -namespace clang { -namespace tidy { - -/// The core clang tidy plugin action. This just provides the AST consumer and -/// command line flag parsing for using clang-tidy as a clang plugin. -class ClangTidyPluginAction : public PluginASTAction { - /// Wrapper to grant the context and diagnostics engine the same lifetime as - /// the action. - /// We use MultiplexConsumer to avoid writing out all the forwarding methods. - class WrapConsumer : public MultiplexConsumer { - std::unique_ptr Context; - std::unique_ptr DiagEngine; - - public: - WrapConsumer(std::unique_ptr Context, - std::unique_ptr DiagEngine, - std::vector> Consumer) - : MultiplexConsumer(std::move(Consumer)), Context(std::move(Context)), - DiagEngine(std::move(DiagEngine)) {} - }; - -public: - std::unique_ptr CreateASTConsumer(CompilerInstance &Compiler, - StringRef File) override { - // Create and set diagnostics engine - auto *DiagConsumer = - new ClangTidyDiagnosticConsumer(*Context, &Compiler.getDiagnostics()); - auto DiagEngine = std::make_unique( - new DiagnosticIDs, new DiagnosticOptions, DiagConsumer); - Context->setDiagnosticsEngine(DiagEngine.get()); - - // Create the AST consumer. - ClangTidyASTConsumerFactory Factory(*Context); - std::vector> Vec; - Vec.push_back(Factory.createASTConsumer(Compiler, File)); - - return std::make_unique( - std::move(Context), std::move(DiagEngine), std::move(Vec)); - } - - bool ParseArgs(const CompilerInstance &, - const std::vector &Args) override { - ClangTidyGlobalOptions GlobalOptions; - ClangTidyOptions DefaultOptions; - ClangTidyOptions OverrideOptions; - - // Parse the extra command line args. - // FIXME: This is very limited at the moment. - for (StringRef Arg : Args) - if (Arg.startswith("-checks=")) - OverrideOptions.Checks = std::string(Arg.substr(strlen("-checks="))); - - auto Options = std::make_unique( - GlobalOptions, DefaultOptions, OverrideOptions); - Context = std::make_unique(std::move(Options)); - return true; - } - -private: - std::unique_ptr Context; -}; -} // namespace tidy -} // namespace clang - -// This anchor is used to force the linker to link in the generated object file -// and thus register the clang-tidy plugin. -volatile int ClangTidyPluginAnchorSource = 0; - -static clang::FrontendPluginRegistry::Add - X("clang-tidy", "clang-tidy");