2013-07-29 16:19:24 +08:00
|
|
|
//===--- tools/extra/clang-tidy/ClangTidy.cpp - Clang tidy tool -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file This file implements a clang-tidy tool.
|
|
|
|
///
|
|
|
|
/// This tool uses the Clang Tooling infrastructure, see
|
|
|
|
/// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
|
|
|
|
/// for details on setting it up with LLVM source tree.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangTidy.h"
|
|
|
|
#include "ClangTidyDiagnosticConsumer.h"
|
|
|
|
#include "ClangTidyModuleRegistry.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2016-10-18 01:25:02 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2014-03-27 18:24:11 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2014-01-03 17:31:57 +08:00
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "clang/Rewrite/Frontend/FixItRewriter.h"
|
|
|
|
#include "clang/Rewrite/Frontend/FrontendActions.h"
|
2016-07-19 03:21:22 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
2014-01-04 01:24:20 +08:00
|
|
|
#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "clang/Tooling/Refactoring.h"
|
2014-09-04 18:31:23 +08:00
|
|
|
#include "clang/Tooling/ReplacementsYaml.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
2014-02-06 22:50:10 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
#include <algorithm>
|
2014-03-27 18:24:11 +08:00
|
|
|
#include <utility>
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-08-05 19:01:08 +08:00
|
|
|
LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
|
2014-07-03 22:12:47 +08:00
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2013-11-14 23:49:44 +08:00
|
|
|
|
2014-01-03 17:31:57 +08:00
|
|
|
namespace {
|
2014-02-06 22:50:10 +08:00
|
|
|
static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
|
2013-11-14 23:49:44 +08:00
|
|
|
|
2014-02-06 22:50:10 +08:00
|
|
|
class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
|
|
|
|
public:
|
|
|
|
AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
|
|
|
|
|
2014-02-27 21:14:51 +08:00
|
|
|
void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
|
2014-03-02 18:20:11 +08:00
|
|
|
FilesMade *filesMade) override {
|
2014-03-06 18:17:46 +08:00
|
|
|
for (const ento::PathDiagnostic *PD : Diags) {
|
2014-02-12 17:52:07 +08:00
|
|
|
SmallString<64> CheckName(AnalyzerCheckNamePrefix);
|
|
|
|
CheckName += PD->getCheckName();
|
2014-03-06 21:24:28 +08:00
|
|
|
Context.diag(CheckName, PD->getLocation().asLocation(),
|
|
|
|
PD->getShortDescription())
|
|
|
|
<< PD->path.back()->getRanges();
|
2014-02-06 22:50:10 +08:00
|
|
|
|
2014-03-06 18:17:46 +08:00
|
|
|
for (const auto &DiagPiece :
|
|
|
|
PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
|
2014-03-06 21:24:28 +08:00
|
|
|
Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
|
|
|
|
DiagPiece->getString(), DiagnosticIDs::Note)
|
|
|
|
<< DiagPiece->getRanges();
|
2014-02-06 22:50:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 18:20:11 +08:00
|
|
|
StringRef getName() const override { return "ClangTidyDiags"; }
|
|
|
|
bool supportsLogicalOpControlFlow() const override { return true; }
|
|
|
|
bool supportsCrossFileDiagnostics() const override { return true; }
|
2014-02-06 22:50:10 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyContext &Context;
|
|
|
|
};
|
|
|
|
|
2014-03-27 18:24:11 +08:00
|
|
|
class ErrorReporter {
|
|
|
|
public:
|
2016-12-01 02:06:42 +08:00
|
|
|
ErrorReporter(bool ApplyFixes, StringRef FormatStyle)
|
2014-03-27 18:24:11 +08:00
|
|
|
: Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()),
|
|
|
|
DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
|
|
|
|
Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
|
|
|
|
DiagPrinter),
|
2016-10-18 01:25:02 +08:00
|
|
|
SourceMgr(Diags, Files), ApplyFixes(ApplyFixes), TotalFixes(0),
|
2016-12-01 02:06:42 +08:00
|
|
|
AppliedFixes(0), WarningsAsErrors(0), FormatStyle(FormatStyle) {
|
2014-03-27 18:24:11 +08:00
|
|
|
DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
|
|
|
|
DiagPrinter->BeginSourceFile(LangOpts);
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:07:51 +08:00
|
|
|
SourceManager &getSourceManager() { return SourceMgr; }
|
2016-02-26 17:19:33 +08:00
|
|
|
|
2014-07-02 23:05:04 +08:00
|
|
|
void reportDiagnostic(const ClangTidyError &Error) {
|
|
|
|
const ClangTidyMessage &Message = Error.Message;
|
2014-03-27 18:24:11 +08:00
|
|
|
SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
|
|
|
|
// Contains a pair for each attempted fix: location and whether the fix was
|
|
|
|
// applied successfully.
|
|
|
|
SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
|
|
|
|
{
|
2014-07-02 23:05:04 +08:00
|
|
|
auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
|
2016-01-14 01:36:41 +08:00
|
|
|
std::string Name = Error.CheckName;
|
|
|
|
if (Error.IsWarningAsError) {
|
|
|
|
Name += ",-warnings-as-errors";
|
|
|
|
Level = DiagnosticsEngine::Error;
|
|
|
|
WarningsAsErrors++;
|
|
|
|
}
|
2015-11-16 21:06:15 +08:00
|
|
|
auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
|
2016-01-14 01:36:41 +08:00
|
|
|
<< Message.Message << Name;
|
2016-08-09 15:54:49 +08:00
|
|
|
for (const auto &FileAndReplacements : Error.Fix) {
|
2016-10-18 01:25:02 +08:00
|
|
|
for (const auto &Repl : FileAndReplacements.second) {
|
2016-08-09 15:54:49 +08:00
|
|
|
// Retrieve the source range for applicable fixes. Macro definitions
|
|
|
|
// on the command line have locations in a virtual buffer and don't
|
|
|
|
// have valid file paths and are therefore not applicable.
|
|
|
|
SourceRange Range;
|
|
|
|
SourceLocation FixLoc;
|
2016-10-18 01:25:02 +08:00
|
|
|
++TotalFixes;
|
|
|
|
bool CanBeApplied = false;
|
|
|
|
if (Repl.isApplicable()) {
|
|
|
|
SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
|
2016-08-09 15:54:49 +08:00
|
|
|
Files.makeAbsolutePath(FixAbsoluteFilePath);
|
2016-10-18 01:25:02 +08:00
|
|
|
if (ApplyFixes) {
|
|
|
|
tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
|
|
|
|
Repl.getLength(),
|
|
|
|
Repl.getReplacementText());
|
|
|
|
Replacements &Replacements = FileReplacements[R.getFilePath()];
|
|
|
|
llvm::Error Err = Replacements.add(R);
|
|
|
|
if (Err) {
|
|
|
|
// FIXME: Implement better conflict handling.
|
|
|
|
llvm::errs() << "Trying to resolve conflict: "
|
|
|
|
<< llvm::toString(std::move(Err)) << "\n";
|
|
|
|
unsigned NewOffset =
|
|
|
|
Replacements.getShiftedCodePosition(R.getOffset());
|
|
|
|
unsigned NewLength = Replacements.getShiftedCodePosition(
|
|
|
|
R.getOffset() + R.getLength()) -
|
|
|
|
NewOffset;
|
|
|
|
if (NewLength == R.getLength()) {
|
|
|
|
R = Replacement(R.getFilePath(), NewOffset, NewLength,
|
|
|
|
R.getReplacementText());
|
|
|
|
Replacements = Replacements.merge(tooling::Replacements(R));
|
|
|
|
CanBeApplied = true;
|
|
|
|
++AppliedFixes;
|
|
|
|
} else {
|
|
|
|
llvm::errs()
|
|
|
|
<< "Can't resolve conflict, skipping the replacement.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
CanBeApplied = true;
|
|
|
|
++AppliedFixes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
|
2016-08-09 15:54:49 +08:00
|
|
|
SourceLocation FixEndLoc =
|
2016-10-18 01:25:02 +08:00
|
|
|
FixLoc.getLocWithOffset(Repl.getLength());
|
2016-08-09 15:54:49 +08:00
|
|
|
Range = SourceRange(FixLoc, FixEndLoc);
|
2016-10-18 01:25:02 +08:00
|
|
|
Diag << FixItHint::CreateReplacement(Range,
|
|
|
|
Repl.getReplacementText());
|
2016-08-09 15:54:49 +08:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:25:02 +08:00
|
|
|
if (ApplyFixes)
|
|
|
|
FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
|
2014-03-27 18:24:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto Fix : FixLocations) {
|
|
|
|
Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
|
|
|
|
: diag::note_fixit_failed);
|
|
|
|
}
|
2014-07-02 23:05:04 +08:00
|
|
|
for (const ClangTidyMessage &Note : Error.Notes)
|
|
|
|
reportNote(Note);
|
2014-03-27 18:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Finish() {
|
|
|
|
// FIXME: Run clang-format on changes.
|
|
|
|
if (ApplyFixes && TotalFixes > 0) {
|
2016-10-18 01:25:02 +08:00
|
|
|
Rewriter Rewrite(SourceMgr, LangOpts);
|
|
|
|
for (const auto &FileAndReplacements : FileReplacements) {
|
|
|
|
StringRef File = FileAndReplacements.first();
|
|
|
|
llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
|
|
|
SourceMgr.getFileManager().getBufferForFile(File);
|
|
|
|
if (!Buffer) {
|
|
|
|
llvm::errs() << "Can't get buffer for file " << File << ": "
|
|
|
|
<< Buffer.getError().message() << "\n";
|
|
|
|
// FIXME: Maybe don't apply fixes for other files as well.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StringRef Code = Buffer.get()->getBuffer();
|
2016-12-01 02:06:42 +08:00
|
|
|
format::FormatStyle Style = format::getStyle("file", File, FormatStyle);
|
2016-10-18 01:25:02 +08:00
|
|
|
llvm::Expected<Replacements> CleanReplacements =
|
|
|
|
format::cleanupAroundReplacements(Code, FileAndReplacements.second,
|
|
|
|
Style);
|
|
|
|
if (!CleanReplacements) {
|
|
|
|
llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!tooling::applyAllReplacements(CleanReplacements.get(), Rewrite)) {
|
|
|
|
llvm::errs() << "Can't apply replacements for file " << File << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Rewrite.overwriteChangedFiles()) {
|
|
|
|
llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "clang-tidy applied " << AppliedFixes << " of "
|
|
|
|
<< TotalFixes << " suggested fixes.\n";
|
|
|
|
}
|
2014-03-27 18:24:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:36:41 +08:00
|
|
|
unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
|
|
|
|
|
2014-03-27 18:24:11 +08:00
|
|
|
private:
|
|
|
|
SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
|
|
|
|
if (FilePath.empty())
|
|
|
|
return SourceLocation();
|
|
|
|
|
|
|
|
const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
|
|
|
|
FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
|
|
|
|
return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
|
|
|
|
}
|
|
|
|
|
2014-07-02 23:05:04 +08:00
|
|
|
void reportNote(const ClangTidyMessage &Message) {
|
|
|
|
SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
|
|
|
|
DiagnosticBuilder Diag =
|
|
|
|
Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
|
|
|
|
<< Message.Message;
|
|
|
|
}
|
|
|
|
|
2014-03-27 18:24:11 +08:00
|
|
|
FileManager Files;
|
|
|
|
LangOptions LangOpts; // FIXME: use langopts from each original file
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
|
|
|
|
DiagnosticConsumer *DiagPrinter;
|
|
|
|
DiagnosticsEngine Diags;
|
|
|
|
SourceManager SourceMgr;
|
2016-10-18 01:25:02 +08:00
|
|
|
llvm::StringMap<Replacements> FileReplacements;
|
2014-03-27 18:24:11 +08:00
|
|
|
bool ApplyFixes;
|
2014-03-27 22:53:37 +08:00
|
|
|
unsigned TotalFixes;
|
|
|
|
unsigned AppliedFixes;
|
2016-01-14 01:36:41 +08:00
|
|
|
unsigned WarningsAsErrors;
|
2016-12-01 02:06:42 +08:00
|
|
|
StringRef FormatStyle;
|
2014-03-27 18:24:11 +08:00
|
|
|
};
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
class ClangTidyASTConsumer : public MultiplexConsumer {
|
|
|
|
public:
|
2014-08-11 03:56:59 +08:00
|
|
|
ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
|
2014-06-05 21:31:45 +08:00
|
|
|
std::unique_ptr<ast_matchers::MatchFinder> Finder,
|
|
|
|
std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
|
2014-08-11 03:56:59 +08:00
|
|
|
: MultiplexConsumer(std::move(Consumers)), Finder(std::move(Finder)),
|
2014-06-05 21:31:45 +08:00
|
|
|
Checks(std::move(Checks)) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<ast_matchers::MatchFinder> Finder;
|
|
|
|
std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
|
|
|
|
};
|
|
|
|
|
2014-01-03 17:31:57 +08:00
|
|
|
} // namespace
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
|
2014-01-03 17:31:57 +08:00
|
|
|
ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
|
2014-06-05 21:31:45 +08:00
|
|
|
ClangTidyContext &Context)
|
|
|
|
: Context(Context), CheckFactories(new ClangTidyCheckFactories) {
|
2014-01-03 17:31:57 +08:00
|
|
|
for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
|
|
|
|
E = ClangTidyModuleRegistry::end();
|
|
|
|
I != E; ++I) {
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<ClangTidyModule> Module(I->instantiate());
|
2014-01-03 17:31:57 +08:00
|
|
|
Module->addCheckFactories(*CheckFactories);
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 23:49:44 +08:00
|
|
|
|
2015-03-12 01:25:22 +08:00
|
|
|
static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
|
|
|
|
AnalyzerOptionsRef AnalyzerOptions) {
|
|
|
|
StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
|
|
|
|
for (const auto &Opt : Opts.CheckOptions) {
|
|
|
|
StringRef OptName(Opt.first);
|
|
|
|
if (!OptName.startswith(AnalyzerPrefix))
|
|
|
|
continue;
|
|
|
|
AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:43:42 +08:00
|
|
|
typedef std::vector<std::pair<std::string, bool>> CheckersList;
|
|
|
|
|
|
|
|
static CheckersList getCheckersControlList(GlobList &Filter) {
|
|
|
|
CheckersList List;
|
|
|
|
|
|
|
|
const auto &RegisteredCheckers =
|
|
|
|
AnalyzerOptions::getRegisteredCheckers(/*IncludeExperimental=*/false);
|
|
|
|
bool AnalyzerChecksEnabled = false;
|
|
|
|
for (StringRef CheckName : RegisteredCheckers) {
|
|
|
|
std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
|
|
|
|
AnalyzerChecksEnabled |= Filter.contains(ClangTidyCheckName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AnalyzerChecksEnabled)
|
|
|
|
return List;
|
|
|
|
|
|
|
|
// List all static analyzer checkers that our filter enables.
|
|
|
|
//
|
|
|
|
// Always add all core checkers if any other static analyzer check is enabled.
|
|
|
|
// This is currently necessary, as other path sensitive checks rely on the
|
|
|
|
// core checkers.
|
|
|
|
for (StringRef CheckName : RegisteredCheckers) {
|
|
|
|
std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
|
|
|
|
|
|
|
|
if (CheckName.startswith("core") || Filter.contains(ClangTidyCheckName))
|
|
|
|
List.emplace_back(CheckName, true);
|
|
|
|
}
|
|
|
|
return List;
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
ClangTidyASTConsumerFactory::CreateASTConsumer(
|
2014-01-03 17:31:57 +08:00
|
|
|
clang::CompilerInstance &Compiler, StringRef File) {
|
|
|
|
// FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
|
|
|
|
// modify Compiler.
|
|
|
|
Context.setSourceManager(&Compiler.getSourceManager());
|
2014-06-05 21:31:45 +08:00
|
|
|
Context.setCurrentFile(File);
|
2014-07-14 22:10:03 +08:00
|
|
|
Context.setASTContext(&Compiler.getASTContext());
|
2014-06-05 21:31:45 +08:00
|
|
|
|
2016-02-26 17:19:33 +08:00
|
|
|
auto WorkingDir = Compiler.getSourceManager()
|
|
|
|
.getFileManager()
|
|
|
|
.getVirtualFileSystem()
|
|
|
|
->getCurrentWorkingDirectory();
|
|
|
|
if (WorkingDir)
|
|
|
|
Context.setCurrentBuildDirectory(WorkingDir.get());
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
|
2014-09-12 16:53:36 +08:00
|
|
|
CheckFactories->createChecks(&Context, Checks);
|
2014-06-05 21:31:45 +08:00
|
|
|
|
2014-10-24 01:23:20 +08:00
|
|
|
ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
|
|
|
|
if (auto *P = Context.getCheckProfileData())
|
|
|
|
FinderOptions.CheckProfiling.emplace(P->Records);
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
std::unique_ptr<ast_matchers::MatchFinder> Finder(
|
2014-10-24 01:23:20 +08:00
|
|
|
new ast_matchers::MatchFinder(std::move(FinderOptions)));
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
for (auto &Check : Checks) {
|
|
|
|
Check->registerMatchers(&*Finder);
|
2014-03-06 18:17:46 +08:00
|
|
|
Check->registerPPCallbacks(Compiler);
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
2014-01-03 17:31:57 +08:00
|
|
|
|
2014-08-11 03:56:59 +08:00
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
2014-06-05 21:31:45 +08:00
|
|
|
if (!Checks.empty())
|
|
|
|
Consumers.push_back(Finder->newASTConsumer());
|
2014-02-14 00:10:47 +08:00
|
|
|
|
2014-04-30 22:09:24 +08:00
|
|
|
AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
|
|
|
|
// FIXME: Remove this option once clang's cfg-temporary-dtors option defaults
|
|
|
|
// to true.
|
|
|
|
AnalyzerOptions->Config["cfg-temporary-dtors"] =
|
2014-06-05 21:31:45 +08:00
|
|
|
Context.getOptions().AnalyzeTemporaryDtors ? "true" : "false";
|
2014-04-30 22:09:24 +08:00
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
GlobList &Filter = Context.getChecksFilter();
|
2014-06-05 21:31:45 +08:00
|
|
|
AnalyzerOptions->CheckersControlList = getCheckersControlList(Filter);
|
2014-04-30 22:09:24 +08:00
|
|
|
if (!AnalyzerOptions->CheckersControlList.empty()) {
|
2015-03-12 01:25:22 +08:00
|
|
|
setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
|
2014-04-30 22:09:24 +08:00
|
|
|
AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
|
|
|
|
AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
|
|
|
|
AnalyzerOptions->AnalyzeNestedBlocks = true;
|
|
|
|
AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
|
2014-08-27 23:14:47 +08:00
|
|
|
ento::CreateAnalysisConsumer(Compiler);
|
2014-02-14 00:10:47 +08:00
|
|
|
AnalysisConsumer->AddDiagnosticConsumer(
|
|
|
|
new AnalyzerDiagnosticConsumer(Context));
|
2014-08-11 03:56:59 +08:00
|
|
|
Consumers.push_back(std::move(AnalysisConsumer));
|
2014-02-14 00:10:47 +08:00
|
|
|
}
|
2014-08-11 03:56:59 +08:00
|
|
|
return llvm::make_unique<ClangTidyASTConsumer>(
|
|
|
|
std::move(Consumers), std::move(Finder), std::move(Checks));
|
2014-01-03 17:31:57 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
|
2014-01-03 17:31:57 +08:00
|
|
|
std::vector<std::string> CheckNames;
|
2014-09-12 16:53:36 +08:00
|
|
|
GlobList &Filter = Context.getChecksFilter();
|
2014-03-06 18:17:46 +08:00
|
|
|
for (const auto &CheckFactory : *CheckFactories) {
|
2014-08-06 19:49:10 +08:00
|
|
|
if (Filter.contains(CheckFactory.first))
|
2014-03-06 18:17:46 +08:00
|
|
|
CheckNames.push_back(CheckFactory.first);
|
2013-07-29 16:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
for (const auto &AnalyzerCheck : getCheckersControlList(Filter))
|
2014-03-06 18:17:46 +08:00
|
|
|
CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-01-03 17:31:57 +08:00
|
|
|
std::sort(CheckNames.begin(), CheckNames.end());
|
|
|
|
return CheckNames;
|
|
|
|
}
|
2013-11-14 23:49:44 +08:00
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
ClangTidyOptions::OptionMap ClangTidyASTConsumerFactory::getCheckOptions() {
|
|
|
|
ClangTidyOptions::OptionMap Options;
|
|
|
|
std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
|
|
|
|
CheckFactories->createChecks(&Context, Checks);
|
|
|
|
for (const auto &Check : Checks)
|
|
|
|
Check->storeOptions(Options);
|
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:34:48 +08:00
|
|
|
DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
|
|
|
|
DiagnosticIDs::Level Level) {
|
|
|
|
return Context->diag(CheckName, Loc, Message, Level);
|
2014-01-13 18:50:51 +08:00
|
|
|
}
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
|
|
|
|
Context->setSourceManager(Result.SourceManager);
|
|
|
|
check(Result);
|
|
|
|
}
|
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
OptionsView::OptionsView(StringRef CheckName,
|
|
|
|
const ClangTidyOptions::OptionMap &CheckOptions)
|
|
|
|
: NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
|
|
|
|
|
2016-02-05 19:23:59 +08:00
|
|
|
std::string OptionsView::get(StringRef LocalName, StringRef Default) const {
|
2014-09-12 16:53:36 +08:00
|
|
|
const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
|
|
|
|
if (Iter != CheckOptions.end())
|
|
|
|
return Iter->second;
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2016-02-05 19:23:59 +08:00
|
|
|
std::string OptionsView::getLocalOrGlobal(StringRef LocalName,
|
|
|
|
StringRef Default) const {
|
|
|
|
auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
|
|
|
|
if (Iter != CheckOptions.end())
|
|
|
|
return Iter->second;
|
|
|
|
// Fallback to global setting, if present.
|
|
|
|
Iter = CheckOptions.find(LocalName.str());
|
|
|
|
if (Iter != CheckOptions.end())
|
|
|
|
return Iter->second;
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
void OptionsView::store(ClangTidyOptions::OptionMap &Options,
|
|
|
|
StringRef LocalName, StringRef Value) const {
|
|
|
|
Options[NamePrefix + LocalName.str()] = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsView::store(ClangTidyOptions::OptionMap &Options,
|
|
|
|
StringRef LocalName, int64_t Value) const {
|
|
|
|
store(Options, LocalName, llvm::itostr(Value));
|
2014-01-13 18:50:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-29 23:20:10 +08:00
|
|
|
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
|
2014-06-05 21:31:45 +08:00
|
|
|
clang::tidy::ClangTidyContext Context(
|
2014-09-04 22:23:36 +08:00
|
|
|
llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
|
|
|
|
Options));
|
2014-06-05 21:31:45 +08:00
|
|
|
ClangTidyASTConsumerFactory Factory(Context);
|
2014-09-12 16:53:36 +08:00
|
|
|
return Factory.getCheckNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options) {
|
|
|
|
clang::tidy::ClangTidyContext Context(
|
|
|
|
llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
|
|
|
|
Options));
|
|
|
|
ClangTidyASTConsumerFactory Factory(Context);
|
|
|
|
return Factory.getCheckOptions();
|
2013-11-14 23:49:44 +08:00
|
|
|
}
|
|
|
|
|
2014-09-04 22:23:36 +08:00
|
|
|
ClangTidyStats
|
|
|
|
runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
|
2016-10-18 01:25:02 +08:00
|
|
|
const CompilationDatabase &Compilations,
|
2014-09-04 22:23:36 +08:00
|
|
|
ArrayRef<std::string> InputFiles,
|
2014-10-24 01:23:20 +08:00
|
|
|
std::vector<ClangTidyError> *Errors, ProfileData *Profile) {
|
2014-05-28 23:21:14 +08:00
|
|
|
ClangTool Tool(Compilations, InputFiles);
|
2014-09-04 22:23:36 +08:00
|
|
|
clang::tidy::ClangTidyContext Context(std::move(OptionsProvider));
|
2016-04-06 22:07:51 +08:00
|
|
|
|
|
|
|
// Add extra arguments passed by the clang-tidy command-line.
|
|
|
|
ArgumentsAdjuster PerFileExtraArgumentsInserter =
|
|
|
|
[&Context](const CommandLineArguments &Args, StringRef Filename) {
|
|
|
|
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
|
2016-08-23 22:13:31 +08:00
|
|
|
CommandLineArguments AdjustedArgs = Args;
|
|
|
|
if (Opts.ExtraArgsBefore) {
|
|
|
|
auto I = AdjustedArgs.begin();
|
|
|
|
if (I != AdjustedArgs.end() && !StringRef(*I).startswith("-"))
|
|
|
|
++I; // Skip compiler binary name, if it is there.
|
|
|
|
AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
|
|
|
|
Opts.ExtraArgsBefore->end());
|
|
|
|
}
|
2016-04-06 22:07:51 +08:00
|
|
|
if (Opts.ExtraArgs)
|
|
|
|
AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
|
|
|
|
Opts.ExtraArgs->end());
|
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove plugins arguments.
|
|
|
|
ArgumentsAdjuster PluginArgumentsRemover =
|
|
|
|
[&Context](const CommandLineArguments &Args, StringRef Filename) {
|
|
|
|
CommandLineArguments AdjustedArgs;
|
|
|
|
for (size_t I = 0, E = Args.size(); I < E; ++I) {
|
|
|
|
if (I + 4 < Args.size() && Args[I] == "-Xclang" &&
|
|
|
|
(Args[I + 1] == "-load" || Args[I + 1] == "-add-plugin" ||
|
|
|
|
StringRef(Args[I + 1]).startswith("-plugin-arg-")) &&
|
|
|
|
Args[I + 2] == "-Xclang") {
|
|
|
|
I += 3;
|
|
|
|
} else
|
|
|
|
AdjustedArgs.push_back(Args[I]);
|
|
|
|
}
|
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
|
|
|
|
2015-11-10 00:28:11 +08:00
|
|
|
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
|
2016-04-06 22:07:51 +08:00
|
|
|
Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
|
2014-10-24 01:23:20 +08:00
|
|
|
if (Profile)
|
|
|
|
Context.setCheckProfileData(Profile);
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
ClangTidyDiagnosticConsumer DiagConsumer(Context);
|
|
|
|
|
2013-11-14 23:49:44 +08:00
|
|
|
Tool.setDiagnosticConsumer(&DiagConsumer);
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
class ActionFactory : public FrontendActionFactory {
|
|
|
|
public:
|
2014-07-24 18:23:33 +08:00
|
|
|
ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {}
|
|
|
|
FrontendAction *create() override { return new Action(&ConsumerFactory); }
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
class Action : public ASTFrontendAction {
|
|
|
|
public:
|
|
|
|
Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
|
|
|
|
StringRef File) override {
|
2014-01-03 17:31:57 +08:00
|
|
|
return Factory->CreateASTConsumer(Compiler, File);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyASTConsumerFactory *Factory;
|
|
|
|
};
|
|
|
|
|
2014-07-24 18:23:33 +08:00
|
|
|
ClangTidyASTConsumerFactory ConsumerFactory;
|
2014-01-03 17:31:57 +08:00
|
|
|
};
|
|
|
|
|
2014-07-24 18:23:33 +08:00
|
|
|
ActionFactory Factory(Context);
|
|
|
|
Tool.run(&Factory);
|
2014-05-09 20:24:09 +08:00
|
|
|
*Errors = Context.getErrors();
|
2014-05-07 17:06:53 +08:00
|
|
|
return Context.getStats();
|
2013-11-14 23:49:44 +08:00
|
|
|
}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2016-01-14 01:36:41 +08:00
|
|
|
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
|
2016-12-01 02:06:42 +08:00
|
|
|
StringRef FormatStyle, unsigned &WarningsAsErrorsCount) {
|
|
|
|
ErrorReporter Reporter(Fix, FormatStyle);
|
2016-02-26 17:19:33 +08:00
|
|
|
vfs::FileSystem &FileSystem =
|
|
|
|
*Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
|
|
|
|
auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
|
|
|
|
if (!InitialWorkingDir)
|
|
|
|
llvm::report_fatal_error("Cannot get current working path.");
|
|
|
|
|
|
|
|
for (const ClangTidyError &Error : Errors) {
|
|
|
|
if (!Error.BuildDirectory.empty()) {
|
|
|
|
// By default, the working directory of file system is the current
|
|
|
|
// clang-tidy running directory.
|
|
|
|
//
|
|
|
|
// Change the directory to the one used during the analysis.
|
|
|
|
FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
|
|
|
|
}
|
2014-07-02 23:05:04 +08:00
|
|
|
Reporter.reportDiagnostic(Error);
|
2016-02-26 17:19:33 +08:00
|
|
|
// Return to the initial directory to correctly resolve next Error.
|
|
|
|
FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
|
|
|
|
}
|
2014-03-27 18:24:11 +08:00
|
|
|
Reporter.Finish();
|
2016-01-14 01:36:41 +08:00
|
|
|
WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
|
2013-07-29 16:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-09-04 18:31:23 +08:00
|
|
|
void exportReplacements(const std::vector<ClangTidyError> &Errors,
|
|
|
|
raw_ostream &OS) {
|
2016-10-18 01:25:02 +08:00
|
|
|
TranslationUnitReplacements TUR;
|
2016-08-09 15:54:49 +08:00
|
|
|
for (const ClangTidyError &Error : Errors) {
|
|
|
|
for (const auto &FileAndFixes : Error.Fix)
|
|
|
|
TUR.Replacements.insert(TUR.Replacements.end(),
|
|
|
|
FileAndFixes.second.begin(),
|
|
|
|
FileAndFixes.second.end());
|
|
|
|
}
|
2014-09-04 18:31:23 +08:00
|
|
|
|
|
|
|
yaml::Output YAML(OS);
|
|
|
|
YAML << TUR;
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|