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 <set>
|
|
|
|
#include <tuple>
|
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;
|
|
|
|
}
|
|
|
|
assert(Error.Message.Message.empty() &&
|
|
|
|
"Overwriting a diagnostic message");
|
|
|
|
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.");
|
|
|
|
|
|
|
|
Error.Fix.insert(tooling::Replacement(SM, Range, FixIt.CodeToInsert));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
ClangTidyError::Level DiagLevel)
|
|
|
|
: CheckName(CheckName), DiagLevel(DiagLevel) {}
|
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) {
|
|
|
|
StringRef Glob = GlobList.substr(0, GlobList.find(','));
|
|
|
|
GlobList = GlobList.substr(Glob.size() + 1);
|
|
|
|
llvm::SmallString<128> RegexText("^");
|
|
|
|
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)
|
|
|
|
: Positive(!ConsumeNegativeIndicator(Globs)),
|
|
|
|
Regex(ConsumeGlob(Globs)),
|
|
|
|
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());
|
|
|
|
bool Invalid;
|
|
|
|
const char *CharacterData =
|
|
|
|
DiagEngine->getSourceManager().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.
|
2014-05-07 17:06:53 +08:00
|
|
|
if (RestOfLine.find("NOLINT") != StringRef::npos) {
|
2014-02-28 08:27:50 +08:00
|
|
|
Level = DiagnosticIDs::Ignored;
|
2014-05-07 17:06:53 +08:00
|
|
|
++Stats.ErrorsIgnoredNOLINT;
|
|
|
|
}
|
2014-02-28 08:27:50 +08:00
|
|
|
}
|
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());
|
2014-01-13 18:50:51 +08:00
|
|
|
if (CheckNamesByDiagnosticID.count(ID) == 0)
|
|
|
|
CheckNamesByDiagnosticID.insert(std::make_pair(ID, CheckName.str()));
|
|
|
|
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;
|
2014-09-04 22:23:36 +08:00
|
|
|
// Safeguard against options with unset values.
|
|
|
|
CurrentOptions = ClangTidyOptions::getDefaults().mergeWith(
|
|
|
|
OptionsProvider->getOptions(CurrentFile));
|
|
|
|
CheckFilter.reset(new GlobList(*getOptions().Checks));
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-24 01:23:20 +08:00
|
|
|
void ClangTidyContext::setCheckProfileData(ProfileData *P) {
|
|
|
|
Profile = P;
|
|
|
|
}
|
|
|
|
|
2014-08-06 19:49:10 +08:00
|
|
|
GlobList &ClangTidyContext::getChecksFilter() {
|
2014-06-05 21:31:45 +08:00
|
|
|
assert(CheckFilter != nullptr);
|
|
|
|
return *CheckFilter;
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
LastErrorPassesLineFilter(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
|
|
|
}
|
|
|
|
|
2014-01-10 00:31:25 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::HandleDiagnostic(
|
|
|
|
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
|
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) {
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
Errors.push_back(ClangTidyError(CheckName, Level));
|
2014-01-10 00:31:25 +08:00
|
|
|
}
|
2014-03-10 21:11:17 +08:00
|
|
|
|
|
|
|
// FIXME: Provide correct LangOptions for each file.
|
|
|
|
LangOptions LangOpts;
|
|
|
|
ClangTidyDiagnosticRenderer Converter(
|
|
|
|
LangOpts, &Context.DiagEngine->getDiagnosticOptions(), Errors.back());
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
void ClangTidyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
|
|
|
|
const Preprocessor *PP) {
|
|
|
|
// Before the first translation unit we don't need HeaderFilter, as we
|
|
|
|
// shouldn't get valid source locations in diagnostics.
|
2014-09-04 22:23:36 +08:00
|
|
|
HeaderFilter.reset(new llvm::Regex(*Context.getOptions().HeaderFilterRegex));
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
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;
|
2014-06-05 21:31:45 +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
|
|
|
assert(LastErrorRelatesToUserCode || Sources.isInMainFile(Location) ||
|
|
|
|
HeaderFilter != nullptr);
|
|
|
|
LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
|
|
|
|
Sources.isInMainFile(Location) ||
|
|
|
|
HeaderFilter->match(FileName);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-11 00:32:07 +08:00
|
|
|
namespace {
|
2014-03-10 17:45:49 +08:00
|
|
|
struct LessClangTidyError {
|
|
|
|
bool operator()(const ClangTidyError *LHS, const ClangTidyError *RHS) const {
|
|
|
|
const ClangTidyMessage &M1 = LHS->Message;
|
|
|
|
const ClangTidyMessage &M2 = RHS->Message;
|
|
|
|
|
|
|
|
return std::tie(M1.FilePath, M1.FileOffset, M1.Message) <
|
|
|
|
std::tie(M2.FilePath, M2.FileOffset, M2.Message);
|
|
|
|
}
|
|
|
|
};
|
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-03-10 17:45:49 +08:00
|
|
|
std::set<const ClangTidyError*, LessClangTidyError> UniqueErrors;
|
2014-05-07 17:06:53 +08:00
|
|
|
for (const ClangTidyError &Error : Errors)
|
|
|
|
UniqueErrors.insert(&Error);
|
|
|
|
|
2014-04-08 20:27:49 +08:00
|
|
|
for (const ClangTidyError *Error : UniqueErrors)
|
|
|
|
Context.storeError(*Error);
|
2014-01-10 00:31:25 +08:00
|
|
|
Errors.clear();
|
|
|
|
}
|