Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp =================================================================== --- clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -239,10 +239,11 @@ finalizeLastError(); std::set 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(); } Index: unittests/clang-tidy/CMakeLists.txt =================================================================== --- unittests/clang-tidy/CMakeLists.txt +++ unittests/clang-tidy/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories(${CLANG_LINT_SOURCE_DIR}) add_extra_unittest(ClangTidyTests + ClangTidyDiagnosticConsumerTest.cpp LLVMModuleTest.cpp GoogleModuleTest.cpp MiscModuleTest.cpp) Index: unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp =================================================================== --- /dev/null +++ unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp @@ -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("var"); + // Add diagnostics in the wrong order. + diag(Var->getLocation(), "variable"); + diag(Var->getTypeSpecStartLoc(), "type specifier"); + } +}; + +TEST(ClangTidyDiagnosticConsumer, SortsErrors) { + SmallVector Errors; + runCheckOnCode("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 Index: unittests/clang-tidy/ClangTidyTest.h =================================================================== --- unittests/clang-tidy/ClangTidyTest.h +++ unittests/clang-tidy/ClangTidyTest.h @@ -39,9 +39,10 @@ ClangTidyContext *Context; }; -template std::string runCheckOnCode(StringRef Code) { +template +std::string runCheckOnCode(StringRef Code, + SmallVectorImpl &Errors) { T Check; - SmallVector Errors; ClangTidyContext Context(&Errors, ".*", ""); ClangTidyDiagnosticConsumer DiagConsumer(Context); Check.setContext(&Context); @@ -65,6 +66,11 @@ return tooling::applyAllReplacements(Code, Fixes); } +template std::string runCheckOnCode(StringRef Code) { + SmallVector Errors; + return runCheckOnCode(Code, Errors); +} + } // namespace test } // namespace tidy } // namespace clang