[PATCH] Output clang-tidy errors in a consistent order.
Alexander Kornienko
alexfh at google.com
Tue Apr 8 04:14:05 PDT 2014
Hi djasper, klimek,
Sort errors by path, file offset and message.
http://reviews.llvm.org/D3314
Files:
clang-tidy/ClangTidyDiagnosticConsumer.cpp
unittests/clang-tidy/CMakeLists.txt
unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp
unittests/clang-tidy/ClangTidyTest.h
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -239,10 +239,11 @@
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();
}
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<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
Index: unittests/clang-tidy/ClangTidyTest.h
===================================================================
--- unittests/clang-tidy/ClangTidyTest.h
+++ unittests/clang-tidy/ClangTidyTest.h
@@ -39,9 +39,10 @@
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 @@
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3314.1.patch
Type: text/x-patch
Size: 3383 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140408/d0416c19/attachment.bin>
More information about the cfe-commits
mailing list