2014-01-10 00:31:25 +08:00
|
|
|
//===--- tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp ----------=== //
|
|
|
|
//
|
|
|
|
// 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 ClangTidyDiagnosticConsumer, ClangTidyMessage,
|
|
|
|
/// ClangTidyContext and ClangTidyError classes.
|
|
|
|
///
|
|
|
|
/// 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 "ClangTidyDiagnosticConsumer.h"
|
2014-04-29 23:20:10 +08:00
|
|
|
#include "ClangTidyOptions.h"
|
2014-07-14 22:10:03 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
2014-05-11 00:32:07 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2014-03-10 21:11:17 +08:00
|
|
|
#include "clang/Frontend/DiagnosticRenderer.h"
|
2014-01-10 00:31:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-03-10 17:45:49 +08:00
|
|
|
#include <tuple>
|
2015-10-16 19:43:49 +08:00
|
|
|
#include <vector>
|
2014-05-11 00:32:07 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace tidy;
|
2014-03-10 17:45:49 +08:00
|
|
|
|
2014-05-11 00:32:07 +08:00
|
|
|
namespace {
|
2014-03-10 21:11:17 +08:00
|
|
|
class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
|
|
|
|
public:
|
|
|
|
ClangTidyDiagnosticRenderer(const LangOptions &LangOpts,
|
|
|
|
DiagnosticOptions *DiagOpts,
|
|
|
|
ClangTidyError &Error)
|
|
|
|
: DiagnosticRenderer(LangOpts, DiagOpts), Error(Error) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
DiagnosticsEngine::Level Level, StringRef Message,
|
|
|
|
ArrayRef<CharSourceRange> Ranges,
|
|
|
|
const SourceManager *SM,
|
|
|
|
DiagOrStoredDiag Info) override {
|
2014-07-02 23:05:04 +08:00
|
|
|
// Remove check name from the message.
|
|
|
|
// FIXME: Remove this once there's a better way to pass check names than
|
|
|
|
// appending the check name to the message in ClangTidyContext::diag and
|
|
|
|
// using getCustomDiagID.
|
|
|
|
std::string CheckNameInMessage = " [" + Error.CheckName + "]";
|
|
|
|
if (Message.endswith(CheckNameInMessage))
|
|
|
|
Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
|
|
|
|
|
2014-03-10 21:11:17 +08:00
|
|
|
ClangTidyMessage TidyMessage = Loc.isValid()
|
|
|
|
? ClangTidyMessage(Message, *SM, Loc)
|
|
|
|
: ClangTidyMessage(Message);
|
|
|
|
if (Level == DiagnosticsEngine::Note) {
|
|
|
|
Error.Notes.push_back(TidyMessage);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 16:05:12 +08:00
|
|
|
assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
|
2014-03-10 21:11:17 +08:00
|
|
|
Error.Message = TidyMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
DiagnosticsEngine::Level Level,
|
|
|
|
ArrayRef<CharSourceRange> Ranges,
|
|
|
|
const SourceManager &SM) override {}
|
|
|
|
|
|
|
|
void emitCodeContext(SourceLocation Loc, DiagnosticsEngine::Level Level,
|
|
|
|
SmallVectorImpl<CharSourceRange> &Ranges,
|
|
|
|
ArrayRef<FixItHint> Hints,
|
|
|
|
const SourceManager &SM) override {
|
|
|
|
assert(Loc.isValid());
|
|
|
|
for (const auto &FixIt : Hints) {
|
|
|
|
CharSourceRange Range = FixIt.RemoveRange;
|
|
|
|
assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
|
|
|
|
"Invalid range in the fix-it hint.");
|
|
|
|
assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
|
|
|
|
"Only file locations supported in fix-it hints.");
|
|
|
|
|
2016-08-09 15:54:49 +08:00
|
|
|
tooling::Replacement Replacement(SM, Range, FixIt.CodeToInsert);
|
|
|
|
llvm::Error Err = Error.Fix[Replacement.getFilePath()].add(Replacement);
|
2016-10-18 01:25:02 +08:00
|
|
|
// FIXME: better error handling (at least, don't let other replacements be
|
|
|
|
// applied).
|
2016-08-01 18:16:39 +08:00
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Fix conflicts with existing fix! "
|
2016-11-08 15:50:19 +08:00
|
|
|
<< llvm::toString(std::move(Err)) << "\n";
|
2016-10-18 01:25:02 +08:00
|
|
|
assert(false && "Fix conflicts with existing fix!");
|
2016-08-01 18:16:39 +08:00
|
|
|
}
|
2014-03-10 21:11:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
const SourceManager &SM) override {}
|
|
|
|
|
|
|
|
void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
StringRef ModuleName,
|
|
|
|
const SourceManager &SM) override {}
|
|
|
|
|
|
|
|
void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
StringRef ModuleName,
|
|
|
|
const SourceManager &SM) override {}
|
|
|
|
|
|
|
|
void endDiagnostic(DiagOrStoredDiag D,
|
|
|
|
DiagnosticsEngine::Level Level) override {
|
|
|
|
assert(!Error.Message.Message.empty() && "Message has not been set");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyError &Error;
|
|
|
|
};
|
2014-05-11 00:32:07 +08:00
|
|
|
} // end anonymous namespace
|
2014-03-10 21:11:17 +08:00
|
|
|
|
2014-06-30 22:29:03 +08:00
|
|
|
ClangTidyMessage::ClangTidyMessage(StringRef Message)
|
|
|
|
: Message(Message), FileOffset(0) {}
|
2014-01-10 00:31:25 +08:00
|
|
|
|
|
|
|
ClangTidyMessage::ClangTidyMessage(StringRef Message,
|
|
|
|
const SourceManager &Sources,
|
|
|
|
SourceLocation Loc)
|
|
|
|
: Message(Message) {
|
2014-03-10 21:11:17 +08:00
|
|
|
assert(Loc.isValid() && Loc.isFileID());
|
2014-01-10 00:31:25 +08:00
|
|
|
FilePath = Sources.getFilename(Loc);
|
|
|
|
FileOffset = Sources.getFileOffset(Loc);
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:44:32 +08:00
|
|
|
ClangTidyError::ClangTidyError(StringRef CheckName,
|
2016-01-14 01:36:41 +08:00
|
|
|
ClangTidyError::Level DiagLevel,
|
2016-11-08 15:50:19 +08:00
|
|
|
bool IsWarningAsError, StringRef BuildDirectory)
|
|
|
|
: CheckName(CheckName), BuildDirectory(BuildDirectory),
|
|
|
|
DiagLevel(DiagLevel), IsWarningAsError(IsWarningAsError) {}
|
2014-01-10 00:31:25 +08:00
|
|
|
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
// Returns true if GlobList starts with the negative indicator ('-'), removes it
|
|
|
|
// from the GlobList.
|
|
|
|
static bool ConsumeNegativeIndicator(StringRef &GlobList) {
|
|
|
|
if (GlobList.startswith("-")) {
|
|
|
|
GlobList = GlobList.substr(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Converts first glob from the comma-separated list of globs to Regex and
|
|
|
|
// removes it and the trailing comma from the GlobList.
|
|
|
|
static llvm::Regex ConsumeGlob(StringRef &GlobList) {
|
2015-09-17 00:16:53 +08:00
|
|
|
StringRef Glob = GlobList.substr(0, GlobList.find(',')).trim();
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
GlobList = GlobList.substr(Glob.size() + 1);
|
2015-11-16 21:06:15 +08:00
|
|
|
SmallString<128> RegexText("^");
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
StringRef MetaChars("()^$|*+?.[]\\{}");
|
|
|
|
for (char C : Glob) {
|
|
|
|
if (C == '*')
|
|
|
|
RegexText.push_back('.');
|
2014-05-15 23:56:58 +08:00
|
|
|
else if (MetaChars.find(C) != StringRef::npos)
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
RegexText.push_back('\\');
|
|
|
|
RegexText.push_back(C);
|
|
|
|
}
|
|
|
|
RegexText.push_back('$');
|
|
|
|
return llvm::Regex(RegexText);
|
|
|
|
}
|
|
|
|
|
2014-08-06 19:49:10 +08:00
|
|
|
GlobList::GlobList(StringRef Globs)
|
2015-10-16 19:43:49 +08:00
|
|
|
: Positive(!ConsumeNegativeIndicator(Globs)), Regex(ConsumeGlob(Globs)),
|
2014-08-06 19:49:10 +08:00
|
|
|
NextGlob(Globs.empty() ? nullptr : new GlobList(Globs)) {}
|
|
|
|
|
|
|
|
bool GlobList::contains(StringRef S, bool Contains) {
|
|
|
|
if (Regex.match(S))
|
|
|
|
Contains = Positive;
|
|
|
|
|
|
|
|
if (NextGlob)
|
|
|
|
Contains = NextGlob->contains(S, Contains);
|
|
|
|
return Contains;
|
2014-03-20 17:38:22 +08:00
|
|
|
}
|
|
|
|
|
2014-09-04 22:23:36 +08:00
|
|
|
ClangTidyContext::ClangTidyContext(
|
|
|
|
std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider)
|
2014-10-24 01:23:20 +08:00
|
|
|
: DiagEngine(nullptr), OptionsProvider(std::move(OptionsProvider)),
|
|
|
|
Profile(nullptr) {
|
2014-06-05 21:31:45 +08:00
|
|
|
// Before the first translation unit we can get errors related to command-line
|
|
|
|
// parsing, use empty string for the file name in this case.
|
|
|
|
setCurrentFile("");
|
|
|
|
}
|
2014-03-20 17:38:22 +08:00
|
|
|
|
2014-02-06 22:50:10 +08:00
|
|
|
DiagnosticBuilder ClangTidyContext::diag(
|
|
|
|
StringRef CheckName, SourceLocation Loc, StringRef Description,
|
2014-02-28 08:27:50 +08:00
|
|
|
DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
|
|
|
|
assert(Loc.isValid());
|
2014-01-26 16:36:03 +08:00
|
|
|
unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
|
2014-02-06 22:50:10 +08:00
|
|
|
Level, (Description + " [" + CheckName + "]").str());
|
2016-07-21 22:13:45 +08:00
|
|
|
CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
|
2014-01-13 18:50:51 +08:00
|
|
|
return DiagEngine->Report(Loc, ID);
|
2014-01-10 00:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClangTidyContext::setDiagnosticsEngine(DiagnosticsEngine *Engine) {
|
|
|
|
DiagEngine = Engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
|
|
|
|
DiagEngine->setSourceManager(SourceMgr);
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
void ClangTidyContext::setCurrentFile(StringRef File) {
|
|
|
|
CurrentFile = File;
|
2015-11-10 00:28:11 +08:00
|
|
|
CurrentOptions = getOptionsForFile(CurrentFile);
|
2014-09-04 22:23:36 +08:00
|
|
|
CheckFilter.reset(new GlobList(*getOptions().Checks));
|
2016-01-14 01:36:41 +08:00
|
|
|
WarningAsErrorFilter.reset(new GlobList(*getOptions().WarningsAsErrors));
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:10:03 +08:00
|
|
|
void ClangTidyContext::setASTContext(ASTContext *Context) {
|
|
|
|
DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
|
2015-08-28 21:20:46 +08:00
|
|
|
LangOpts = Context->getLangOpts();
|
2014-07-14 22:10:03 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
const ClangTidyGlobalOptions &ClangTidyContext::getGlobalOptions() const {
|
|
|
|
return OptionsProvider->getGlobalOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClangTidyOptions &ClangTidyContext::getOptions() const {
|
2014-09-04 22:23:36 +08:00
|
|
|
return CurrentOptions;
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 00:28:11 +08:00
|
|
|
ClangTidyOptions ClangTidyContext::getOptionsForFile(StringRef File) const {
|
|
|
|
// Merge options on top of getDefaults() as a safeguard against options with
|
|
|
|
// unset values.
|
|
|
|
return ClangTidyOptions::getDefaults().mergeWith(
|
2016-01-16 00:16:47 +08:00
|
|
|
OptionsProvider->getOptions(File));
|
2015-11-10 00:28:11 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 19:43:49 +08:00
|
|
|
void ClangTidyContext::setCheckProfileData(ProfileData *P) { Profile = P; }
|
2014-10-24 01:23:20 +08:00
|
|
|
|
2014-08-06 19:49:10 +08:00
|
|
|
GlobList &ClangTidyContext::getChecksFilter() {
|
2014-06-05 21:31:45 +08:00
|
|
|
assert(CheckFilter != nullptr);
|
|
|
|
return *CheckFilter;
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:36:41 +08:00
|
|
|
GlobList &ClangTidyContext::getWarningAsErrorFilter() {
|
|
|
|
assert(WarningAsErrorFilter != nullptr);
|
|
|
|
return *WarningAsErrorFilter;
|
|
|
|
}
|
|
|
|
|
2014-01-10 00:31:25 +08:00
|
|
|
/// \brief Store a \c ClangTidyError.
|
|
|
|
void ClangTidyContext::storeError(const ClangTidyError &Error) {
|
2014-05-09 20:24:09 +08:00
|
|
|
Errors.push_back(Error);
|
2014-01-10 00:31:25 +08:00
|
|
|
}
|
|
|
|
|
2014-01-13 18:50:51 +08:00
|
|
|
StringRef ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
|
|
|
|
llvm::DenseMap<unsigned, std::string>::const_iterator I =
|
|
|
|
CheckNamesByDiagnosticID.find(DiagnosticID);
|
|
|
|
if (I != CheckNamesByDiagnosticID.end())
|
|
|
|
return I->second;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-01-10 00:31:25 +08:00
|
|
|
ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx)
|
2014-06-05 21:31:45 +08:00
|
|
|
: Context(Ctx), LastErrorRelatesToUserCode(false),
|
2016-11-03 05:14:22 +08:00
|
|
|
LastErrorPassesLineFilter(false), LastErrorWasIgnored(false) {
|
2014-01-10 00:31:25 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
Diags.reset(new DiagnosticsEngine(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
|
|
|
|
/*ShouldOwnClient=*/false));
|
|
|
|
Context.setDiagnosticsEngine(Diags.get());
|
|
|
|
}
|
|
|
|
|
2014-02-06 22:50:10 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::finalizeLastError() {
|
2014-05-07 17:06:53 +08:00
|
|
|
if (!Errors.empty()) {
|
|
|
|
ClangTidyError &Error = Errors.back();
|
2014-08-06 19:49:10 +08:00
|
|
|
if (!Context.getChecksFilter().contains(Error.CheckName) &&
|
2014-06-03 04:44:32 +08:00
|
|
|
Error.DiagLevel != ClangTidyError::Error) {
|
2014-05-07 17:06:53 +08:00
|
|
|
++Context.Stats.ErrorsIgnoredCheckFilter;
|
|
|
|
Errors.pop_back();
|
|
|
|
} else if (!LastErrorRelatesToUserCode) {
|
|
|
|
++Context.Stats.ErrorsIgnoredNonUserCode;
|
|
|
|
Errors.pop_back();
|
2014-05-23 00:07:11 +08:00
|
|
|
} else if (!LastErrorPassesLineFilter) {
|
|
|
|
++Context.Stats.ErrorsIgnoredLineFilter;
|
|
|
|
Errors.pop_back();
|
2014-05-07 17:06:53 +08:00
|
|
|
} else {
|
|
|
|
++Context.Stats.ErrorsDisplayed;
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 22:50:10 +08:00
|
|
|
LastErrorRelatesToUserCode = false;
|
2014-05-23 00:07:11 +08:00
|
|
|
LastErrorPassesLineFilter = false;
|
2014-02-06 22:50:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
static bool LineIsMarkedWithNOLINT(SourceManager &SM, SourceLocation Loc) {
|
2016-05-05 05:18:31 +08:00
|
|
|
bool Invalid;
|
|
|
|
const char *CharacterData = SM.getCharacterData(Loc, &Invalid);
|
|
|
|
if (!Invalid) {
|
|
|
|
const char *P = CharacterData;
|
|
|
|
while (*P != '\0' && *P != '\r' && *P != '\n')
|
|
|
|
++P;
|
|
|
|
StringRef RestOfLine(CharacterData, P - CharacterData + 1);
|
|
|
|
// FIXME: Handle /\bNOLINT\b(\([^)]*\))?/ as cpplint.py does.
|
|
|
|
if (RestOfLine.find("NOLINT") != StringRef::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-25 00:06:53 +08:00
|
|
|
static bool LineIsMarkedWithNOLINTinMacro(SourceManager &SM,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
while (true) {
|
|
|
|
if (LineIsMarkedWithNOLINT(SM, Loc))
|
|
|
|
return true;
|
|
|
|
if (!Loc.isMacroID())
|
|
|
|
return false;
|
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).first;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-10 00:31:25 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::HandleDiagnostic(
|
|
|
|
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
|
2016-11-03 05:14:22 +08:00
|
|
|
if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
|
|
|
|
return;
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
if (Info.getLocation().isValid() && DiagLevel != DiagnosticsEngine::Error &&
|
2016-05-05 05:18:31 +08:00
|
|
|
DiagLevel != DiagnosticsEngine::Fatal &&
|
2016-11-08 15:50:19 +08:00
|
|
|
LineIsMarkedWithNOLINTinMacro(Diags->getSourceManager(),
|
|
|
|
Info.getLocation())) {
|
2016-05-05 05:18:31 +08:00
|
|
|
++Context.Stats.ErrorsIgnoredNOLINT;
|
2016-11-03 05:14:22 +08:00
|
|
|
// Ignored a warning, should ignore related notes as well
|
|
|
|
LastErrorWasIgnored = true;
|
2016-05-05 05:18:31 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-03 05:14:22 +08:00
|
|
|
|
|
|
|
LastErrorWasIgnored = false;
|
2014-11-20 20:05:51 +08:00
|
|
|
// Count warnings/errors.
|
|
|
|
DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
|
|
|
|
|
2014-02-06 22:50:10 +08:00
|
|
|
if (DiagLevel == DiagnosticsEngine::Note) {
|
2014-01-10 00:31:25 +08:00
|
|
|
assert(!Errors.empty() &&
|
|
|
|
"A diagnostic note can only be appended to a message.");
|
2014-02-06 22:50:10 +08:00
|
|
|
} else {
|
|
|
|
finalizeLastError();
|
2014-03-20 17:38:22 +08:00
|
|
|
StringRef WarningOption =
|
|
|
|
Context.DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(
|
|
|
|
Info.getID());
|
|
|
|
std::string CheckName = !WarningOption.empty()
|
|
|
|
? ("clang-diagnostic-" + WarningOption).str()
|
|
|
|
: Context.getCheckName(Info.getID()).str();
|
|
|
|
|
2014-07-02 23:05:04 +08:00
|
|
|
if (CheckName.empty()) {
|
|
|
|
// This is a compiler diagnostic without a warning option. Assign check
|
|
|
|
// name based on its level.
|
|
|
|
switch (DiagLevel) {
|
2015-10-16 19:43:49 +08:00
|
|
|
case DiagnosticsEngine::Error:
|
|
|
|
case DiagnosticsEngine::Fatal:
|
|
|
|
CheckName = "clang-diagnostic-error";
|
|
|
|
break;
|
|
|
|
case DiagnosticsEngine::Warning:
|
|
|
|
CheckName = "clang-diagnostic-warning";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CheckName = "clang-diagnostic-unknown";
|
|
|
|
break;
|
2014-07-02 23:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:44:32 +08:00
|
|
|
ClangTidyError::Level Level = ClangTidyError::Warning;
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Error ||
|
|
|
|
DiagLevel == DiagnosticsEngine::Fatal) {
|
2014-07-02 23:05:04 +08:00
|
|
|
// Force reporting of Clang errors regardless of filters and non-user
|
|
|
|
// code.
|
2014-06-03 04:44:32 +08:00
|
|
|
Level = ClangTidyError::Error;
|
|
|
|
LastErrorRelatesToUserCode = true;
|
|
|
|
LastErrorPassesLineFilter = true;
|
|
|
|
}
|
2016-01-14 01:36:41 +08:00
|
|
|
bool IsWarningAsError =
|
|
|
|
DiagLevel == DiagnosticsEngine::Warning &&
|
|
|
|
Context.getWarningAsErrorFilter().contains(CheckName);
|
2016-02-26 17:19:33 +08:00
|
|
|
Errors.push_back(ClangTidyError(CheckName, Level, IsWarningAsError,
|
|
|
|
Context.getCurrentBuildDirectory()));
|
2014-01-10 00:31:25 +08:00
|
|
|
}
|
2014-03-10 21:11:17 +08:00
|
|
|
|
|
|
|
ClangTidyDiagnosticRenderer Converter(
|
2016-02-26 07:57:23 +08:00
|
|
|
Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(),
|
|
|
|
Errors.back());
|
2014-03-10 21:11:17 +08:00
|
|
|
SmallString<100> Message;
|
|
|
|
Info.FormatDiagnostic(Message);
|
|
|
|
SourceManager *Sources = nullptr;
|
|
|
|
if (Info.hasSourceManager())
|
|
|
|
Sources = &Info.getSourceManager();
|
2014-05-23 04:19:46 +08:00
|
|
|
Converter.emitDiagnostic(Info.getLocation(), DiagLevel, Message,
|
|
|
|
Info.getRanges(), Info.getFixItHints(), Sources);
|
2014-02-06 22:50:10 +08:00
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
checkFilters(Info.getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
|
|
|
|
unsigned LineNumber) const {
|
2014-06-05 21:31:45 +08:00
|
|
|
if (Context.getGlobalOptions().LineFilter.empty())
|
2014-05-23 00:07:11 +08:00
|
|
|
return true;
|
2015-10-16 19:43:49 +08:00
|
|
|
for (const FileFilter &Filter : Context.getGlobalOptions().LineFilter) {
|
2014-05-23 00:07:11 +08:00
|
|
|
if (FileName.endswith(Filter.Name)) {
|
|
|
|
if (Filter.LineRanges.empty())
|
|
|
|
return true;
|
|
|
|
for (const FileFilter::LineRange &Range : Filter.LineRanges) {
|
|
|
|
if (Range.first <= LineNumber && LineNumber <= Range.second)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-06 22:50:10 +08:00
|
|
|
}
|
2014-05-23 00:07:11 +08:00
|
|
|
return false;
|
2014-01-10 00:31:25 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location) {
|
2014-05-05 22:54:47 +08:00
|
|
|
// Invalid location may mean a diagnostic in a command line, don't skip these.
|
2014-05-23 00:07:11 +08:00
|
|
|
if (!Location.isValid()) {
|
|
|
|
LastErrorRelatesToUserCode = true;
|
|
|
|
LastErrorPassesLineFilter = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-05-05 22:54:47 +08:00
|
|
|
|
|
|
|
const SourceManager &Sources = Diags->getSourceManager();
|
2014-10-29 06:16:13 +08:00
|
|
|
if (!*Context.getOptions().SystemHeaders &&
|
|
|
|
Sources.isInSystemHeader(Location))
|
2014-05-23 00:07:11 +08:00
|
|
|
return;
|
2014-05-05 22:54:47 +08:00
|
|
|
|
|
|
|
// FIXME: We start with a conservative approach here, but the actual type of
|
|
|
|
// location needed depends on the check (in particular, where this check wants
|
|
|
|
// to apply fixes).
|
|
|
|
FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
|
2014-05-06 16:10:00 +08:00
|
|
|
const FileEntry *File = Sources.getFileEntryForID(FID);
|
2014-05-23 00:07:11 +08:00
|
|
|
|
2014-05-06 16:10:00 +08:00
|
|
|
// -DMACRO definitions on the command line have locations in a virtual buffer
|
|
|
|
// that doesn't have a FileEntry. Don't skip these as well.
|
2014-05-23 00:07:11 +08:00
|
|
|
if (!File) {
|
|
|
|
LastErrorRelatesToUserCode = true;
|
|
|
|
LastErrorPassesLineFilter = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef FileName(File->getName());
|
2014-06-05 21:31:45 +08:00
|
|
|
LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
|
|
|
|
Sources.isInMainFile(Location) ||
|
2015-08-12 21:16:41 +08:00
|
|
|
getHeaderFilter()->match(FileName);
|
2014-06-05 21:31:45 +08:00
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
unsigned LineNumber = Sources.getExpansionLineNumber(Location);
|
2014-06-05 21:31:45 +08:00
|
|
|
LastErrorPassesLineFilter =
|
|
|
|
LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
|
2014-05-05 22:54:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-12 21:16:41 +08:00
|
|
|
llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
|
|
|
|
if (!HeaderFilter)
|
|
|
|
HeaderFilter.reset(
|
|
|
|
new llvm::Regex(*Context.getOptions().HeaderFilterRegex));
|
|
|
|
return HeaderFilter.get();
|
|
|
|
}
|
|
|
|
|
2015-10-16 19:43:49 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::removeIncompatibleErrors(
|
|
|
|
SmallVectorImpl<ClangTidyError> &Errors) const {
|
|
|
|
// Each error is modelled as the set of intervals in which it applies
|
|
|
|
// replacements. To detect overlapping replacements, we use a sweep line
|
|
|
|
// algorithm over these sets of intervals.
|
|
|
|
// An event here consists of the opening or closing of an interval. During the
|
2016-07-29 13:26:58 +08:00
|
|
|
// process, we maintain a counter with the amount of open intervals. If we
|
2015-10-16 19:43:49 +08:00
|
|
|
// find an endpoint of an interval and this counter is different from 0, it
|
|
|
|
// means that this interval overlaps with another one, so we set it as
|
|
|
|
// inapplicable.
|
|
|
|
struct Event {
|
|
|
|
// An event can be either the begin or the end of an interval.
|
|
|
|
enum EventType {
|
|
|
|
ET_Begin = 1,
|
|
|
|
ET_End = -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
Event(unsigned Begin, unsigned End, EventType Type, unsigned ErrorId,
|
|
|
|
unsigned ErrorSize)
|
|
|
|
: Type(Type), ErrorId(ErrorId) {
|
|
|
|
// The events are going to be sorted by their position. In case of draw:
|
|
|
|
//
|
|
|
|
// * If an interval ends at the same position at which other interval
|
|
|
|
// begins, this is not an overlapping, so we want to remove the ending
|
|
|
|
// interval before adding the starting one: end events have higher
|
|
|
|
// priority than begin events.
|
|
|
|
//
|
|
|
|
// * If we have several begin points at the same position, we will mark as
|
2016-07-29 13:26:58 +08:00
|
|
|
// inapplicable the ones that we process later, so the first one has to
|
2015-10-16 19:43:49 +08:00
|
|
|
// be the one with the latest end point, because this one will contain
|
|
|
|
// all the other intervals. For the same reason, if we have several end
|
|
|
|
// points in the same position, the last one has to be the one with the
|
|
|
|
// earliest begin point. In both cases, we sort non-increasingly by the
|
|
|
|
// position of the complementary.
|
|
|
|
//
|
|
|
|
// * In case of two equal intervals, the one whose error is bigger can
|
2016-07-29 13:26:58 +08:00
|
|
|
// potentially contain the other one, so we want to process its begin
|
2015-10-16 19:43:49 +08:00
|
|
|
// points before and its end points later.
|
|
|
|
//
|
|
|
|
// * Finally, if we have two equal intervals whose errors have the same
|
|
|
|
// size, none of them will be strictly contained inside the other.
|
|
|
|
// Sorting by ErrorId will guarantee that the begin point of the first
|
2016-07-29 13:26:58 +08:00
|
|
|
// one will be processed before, disallowing the second one, and the
|
|
|
|
// end point of the first one will also be processed before,
|
2015-10-16 19:43:49 +08:00
|
|
|
// disallowing the first one.
|
|
|
|
if (Type == ET_Begin)
|
|
|
|
Priority = std::make_tuple(Begin, Type, -End, -ErrorSize, ErrorId);
|
|
|
|
else
|
|
|
|
Priority = std::make_tuple(End, Type, -Begin, ErrorSize, ErrorId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Event &Other) const {
|
|
|
|
return Priority < Other.Priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines if this event is the begin or the end of an interval.
|
|
|
|
EventType Type;
|
|
|
|
// The index of the error to which the interval that generated this event
|
|
|
|
// belongs.
|
|
|
|
unsigned ErrorId;
|
|
|
|
// The events will be sorted based on this field.
|
|
|
|
std::tuple<unsigned, EventType, int, int, unsigned> Priority;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compute error sizes.
|
|
|
|
std::vector<int> Sizes;
|
|
|
|
for (const auto &Error : Errors) {
|
|
|
|
int Size = 0;
|
2016-08-09 15:54:49 +08:00
|
|
|
for (const auto &FileAndReplaces : Error.Fix) {
|
|
|
|
for (const auto &Replace : FileAndReplaces.second)
|
|
|
|
Size += Replace.getLength();
|
|
|
|
}
|
2015-10-16 19:43:49 +08:00
|
|
|
Sizes.push_back(Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build events from error intervals.
|
2015-10-17 00:15:27 +08:00
|
|
|
std::map<std::string, std::vector<Event>> FileEvents;
|
2015-10-16 19:43:49 +08:00
|
|
|
for (unsigned I = 0; I < Errors.size(); ++I) {
|
2016-08-09 15:54:49 +08:00
|
|
|
for (const auto &FileAndReplace : Errors[I].Fix) {
|
|
|
|
for (const auto &Replace : FileAndReplace.second) {
|
|
|
|
unsigned Begin = Replace.getOffset();
|
|
|
|
unsigned End = Begin + Replace.getLength();
|
|
|
|
const std::string &FilePath = Replace.getFilePath();
|
|
|
|
// FIXME: Handle empty intervals, such as those from insertions.
|
|
|
|
if (Begin == End)
|
|
|
|
continue;
|
|
|
|
FileEvents[FilePath].push_back(
|
|
|
|
Event(Begin, End, Event::ET_Begin, I, Sizes[I]));
|
|
|
|
FileEvents[FilePath].push_back(
|
|
|
|
Event(Begin, End, Event::ET_End, I, Sizes[I]));
|
|
|
|
}
|
2015-10-16 19:43:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<bool> Apply(Errors.size(), true);
|
2015-10-17 00:15:27 +08:00
|
|
|
for (auto &FileAndEvents : FileEvents) {
|
|
|
|
std::vector<Event> &Events = FileAndEvents.second;
|
|
|
|
// Sweep.
|
|
|
|
std::sort(Events.begin(), Events.end());
|
|
|
|
int OpenIntervals = 0;
|
|
|
|
for (const auto &Event : Events) {
|
|
|
|
if (Event.Type == Event::ET_End)
|
|
|
|
--OpenIntervals;
|
|
|
|
// This has to be checked after removing the interval from the count if it
|
|
|
|
// is an end event, or before adding it if it is a begin event.
|
|
|
|
if (OpenIntervals != 0)
|
|
|
|
Apply[Event.ErrorId] = false;
|
|
|
|
if (Event.Type == Event::ET_Begin)
|
|
|
|
++OpenIntervals;
|
|
|
|
}
|
|
|
|
assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
|
2015-10-16 19:43:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0; I < Errors.size(); ++I) {
|
|
|
|
if (!Apply[I]) {
|
|
|
|
Errors[I].Fix.clear();
|
|
|
|
Errors[I].Notes.push_back(
|
|
|
|
ClangTidyMessage("this fix will not be applied because"
|
|
|
|
" it overlaps with another fix"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 00:32:07 +08:00
|
|
|
namespace {
|
2014-03-10 17:45:49 +08:00
|
|
|
struct LessClangTidyError {
|
2015-10-16 19:43:49 +08:00
|
|
|
bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
|
|
|
|
const ClangTidyMessage &M1 = LHS.Message;
|
|
|
|
const ClangTidyMessage &M2 = RHS.Message;
|
2014-03-10 17:45:49 +08:00
|
|
|
|
|
|
|
return std::tie(M1.FilePath, M1.FileOffset, M1.Message) <
|
|
|
|
std::tie(M2.FilePath, M2.FileOffset, M2.Message);
|
|
|
|
}
|
|
|
|
};
|
2015-10-16 19:43:49 +08:00
|
|
|
struct EqualClangTidyError {
|
|
|
|
bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
|
2015-10-16 20:11:15 +08:00
|
|
|
LessClangTidyError Less;
|
2015-10-16 19:43:49 +08:00
|
|
|
return !Less(LHS, RHS) && !Less(RHS, LHS);
|
|
|
|
}
|
|
|
|
};
|
2014-05-11 00:32:07 +08:00
|
|
|
} // end anonymous namespace
|
2014-03-10 17:45:49 +08:00
|
|
|
|
2014-01-10 00:31:25 +08:00
|
|
|
// Flushes the internal diagnostics buffer to the ClangTidyContext.
|
|
|
|
void ClangTidyDiagnosticConsumer::finish() {
|
2014-02-06 22:50:10 +08:00
|
|
|
finalizeLastError();
|
2014-05-07 17:06:53 +08:00
|
|
|
|
2015-10-16 19:43:49 +08:00
|
|
|
std::sort(Errors.begin(), Errors.end(), LessClangTidyError());
|
|
|
|
Errors.erase(std::unique(Errors.begin(), Errors.end(), EqualClangTidyError()),
|
|
|
|
Errors.end());
|
|
|
|
removeIncompatibleErrors(Errors);
|
|
|
|
|
|
|
|
for (const ClangTidyError &Error : Errors)
|
|
|
|
Context.storeError(Error);
|
2014-01-10 00:31:25 +08:00
|
|
|
Errors.clear();
|
|
|
|
}
|