1 #include "ClangTidy.h"
2 #include "ClangTidyTest.h"
3 #include "gtest/gtest.h"
4
5 namespace clang {
6 namespace tidy {
7 namespace test {
8
9 namespace {
10 class TestCheck : public ClangTidyCheck {
11 public:
TestCheck(StringRef Name,ClangTidyContext * Context)12 TestCheck(StringRef Name, ClangTidyContext *Context)
13 : ClangTidyCheck(Name, Context) {
14 diag("DiagWithNoLoc");
15 }
registerMatchers(ast_matchers::MatchFinder * Finder)16 void registerMatchers(ast_matchers::MatchFinder *Finder) override {
17 Finder->addMatcher(ast_matchers::varDecl().bind("var"), this);
18 }
check(const ast_matchers::MatchFinder::MatchResult & Result)19 void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
20 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
21 // Add diagnostics in the wrong order.
22 diag(Var->getLocation(), "variable");
23 diag(Var->getTypeSpecStartLoc(), "type specifier");
24 }
25 };
26 } // namespace
27
TEST(ClangTidyDiagnosticConsumer,SortsErrors)28 TEST(ClangTidyDiagnosticConsumer, SortsErrors) {
29 std::vector<ClangTidyError> Errors;
30 runCheckOnCode<TestCheck>("int a;", &Errors);
31 EXPECT_EQ(3ul, Errors.size());
32 EXPECT_EQ("DiagWithNoLoc", Errors[0].Message.Message);
33 EXPECT_EQ("type specifier", Errors[1].Message.Message);
34 EXPECT_EQ("variable", Errors[2].Message.Message);
35 }
36
37 } // namespace test
38 } // namespace tidy
39 } // namespace clang
40