[clang-tools-extra] r205759 - Output clang-tidy errors in a consistent order.

Alexander Kornienko alexfh at google.com
Tue Apr 8 05:27:50 PDT 2014


Author: alexfh
Date: Tue Apr  8 07:27:49 2014
New Revision: 205759

URL: http://llvm.org/viewvc/llvm-project?rev=205759&view=rev
Log:
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

Added:
    clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/unittests/clang-tidy/CMakeLists.txt
    clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=205759&r1=205758&r2=205759&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Tue Apr  8 07:27:49 2014
@@ -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();
 }
 

Modified: clang-tools-extra/trunk/unittests/clang-tidy/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/CMakeLists.txt?rev=205759&r1=205758&r2=205759&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/CMakeLists.txt Tue Apr  8 07:27:49 2014
@@ -7,6 +7,7 @@ get_filename_component(CLANG_LINT_SOURCE
 include_directories(${CLANG_LINT_SOURCE_DIR})
 
 add_extra_unittest(ClangTidyTests
+  ClangTidyDiagnosticConsumerTest.cpp
   LLVMModuleTest.cpp
   GoogleModuleTest.cpp
   MiscModuleTest.cpp)

Added: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp?rev=205759&view=auto
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp (added)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp Tue Apr  8 07:27:49 2014
@@ -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

Modified: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h?rev=205759&r1=205758&r2=205759&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h Tue Apr  8 07:27:49 2014
@@ -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 runChe
   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





More information about the cfe-commits mailing list