Output clang-tidy errors in a consistent order.

Summary: Sort errors by path, file offset and message.

Reviewers: djasper, klimek

Reviewed By: djasper

CC: cfe-commits

Differential Revision: http://reviews.llvm.org/D3314

llvm-svn: 205759
This commit is contained in:
Alexander Kornienko 2014-04-08 12:27:49 +00:00
parent 33d07468bc
commit 9eb8c92b52
4 changed files with 46 additions and 5 deletions

View File

@ -239,10 +239,11 @@ void ClangTidyDiagnosticConsumer::finish() {
finalizeLastError();
std::set<const ClangTidyError*, LessClangTidyError> UniqueErrors;
for (const ClangTidyError &Error : Errors) {
if (Context.getChecksFilter().isCheckEnabled(Error.CheckName) &&
UniqueErrors.insert(&Error).second)
Context.storeError(Error);
if (Context.getChecksFilter().isCheckEnabled(Error.CheckName))
UniqueErrors.insert(&Error);
}
for (const ClangTidyError *Error : UniqueErrors)
Context.storeError(*Error);
Errors.clear();
}

View File

@ -7,6 +7,7 @@ get_filename_component(CLANG_LINT_SOURCE_DIR
include_directories(${CLANG_LINT_SOURCE_DIR})
add_extra_unittest(ClangTidyTests
ClangTidyDiagnosticConsumerTest.cpp
LLVMModuleTest.cpp
GoogleModuleTest.cpp
MiscModuleTest.cpp)

View File

@ -0,0 +1,33 @@
#include "ClangTidy.h"
#include "ClangTidyTest.h"
#include "gtest/gtest.h"
namespace clang {
namespace tidy {
namespace test {
class TestCheck : public ClangTidyCheck {
public:
void registerMatchers(ast_matchers::MatchFinder *Finder) override {
Finder->addMatcher(ast_matchers::varDecl().bind("var"), this);
}
void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
const VarDecl *Var = Result.Nodes.getNodeAs<VarDecl>("var");
// Add diagnostics in the wrong order.
diag(Var->getLocation(), "variable");
diag(Var->getTypeSpecStartLoc(), "type specifier");
}
};
TEST(ClangTidyDiagnosticConsumer, SortsErrors) {
SmallVector<ClangTidyError, 8> Errors;
runCheckOnCode<TestCheck>("int a;", Errors);
EXPECT_EQ(2ul, Errors.size());
// FIXME: Remove " []" once the check name is removed from the message text.
EXPECT_EQ("type specifier []", Errors[0].Message.Message);
EXPECT_EQ("variable []", Errors[1].Message.Message);
}
} // namespace test
} // namespace tidy
} // namespace clang

View File

@ -39,9 +39,10 @@ private:
ClangTidyContext *Context;
};
template <typename T> std::string runCheckOnCode(StringRef Code) {
template <typename T>
std::string runCheckOnCode(StringRef Code,
SmallVectorImpl<ClangTidyError> &Errors) {
T Check;
SmallVector<ClangTidyError, 16> Errors;
ClangTidyContext Context(&Errors, ".*", "");
ClangTidyDiagnosticConsumer DiagConsumer(Context);
Check.setContext(&Context);
@ -65,6 +66,11 @@ template <typename T> std::string runCheckOnCode(StringRef Code) {
return tooling::applyAllReplacements(Code, Fixes);
}
template <typename T> std::string runCheckOnCode(StringRef Code) {
SmallVector<ClangTidyError, 16> Errors;
return runCheckOnCode<T>(Code, Errors);
}
} // namespace test
} // namespace tidy
} // namespace clang