r308014 - [Clang-Tidy] Preserve Message, FileOffset, FilePath in Clang-Tidy YAML output

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 14 03:37:44 PDT 2017


Author: alexfh
Date: Fri Jul 14 03:37:44 2017
New Revision: 308014

URL: http://llvm.org/viewvc/llvm-project?rev=308014&view=rev
Log:
[Clang-Tidy] Preserve Message, FileOffset, FilePath in Clang-Tidy YAML output

Summary:
To get properly integration Clang-Tidy with CLion IDE, next things were implemented:
1) Preserve `Message`, `FileOffset`, `FilePath` in the clang-tidy output.
2) Export all diagnostics, not just the ones with fixes
3) Test-cases

Reviewers: klimek, ilya-biryukov, alexfh

Reviewed By: alexfh

Subscribers: alexfh, JDevlieghere, mgorny, xazax.hun, cfe-commits, klimek

Tags: #clang-tools-extra

Patch by Vladimir Plyashkun!

Differential Revision: https://reviews.llvm.org/D34404

Added:
    cfe/trunk/unittests/Tooling/DiagnosticsYamlTest.cpp
Modified:
    cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h
    cfe/trunk/unittests/Tooling/CMakeLists.txt

Modified: cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h?rev=308014&r1=308013&r2=308014&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h (original)
+++ cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h Fri Jul 14 03:37:44 2017
@@ -56,6 +56,9 @@ template <> struct MappingTraits<clang::
     MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
         Io, D);
     Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
+    Io.mapRequired("Message", Keys->Message.Message);
+    Io.mapRequired("FileOffset", Keys->Message.FileOffset);
+    Io.mapRequired("FilePath", Keys->Message.FilePath);
 
     // FIXME: Export properly all the different fields.
 
@@ -82,17 +85,7 @@ template <> struct MappingTraits<clang::
 template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
   static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
     Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
-
-    std::vector<clang::tooling::Diagnostic> Diagnostics;
-    for (auto &Diagnostic : Doc.Diagnostics) {
-      // FIXME: Export all diagnostics, not just the ones with fixes.
-      // Update MappingTraits<clang::tooling::Diagnostic>::mapping.
-      if (Diagnostic.Fix.size() > 0) {
-        Diagnostics.push_back(Diagnostic);
-      }
-    }
-    Io.mapRequired("Diagnostics", Diagnostics);
-    Doc.Diagnostics = Diagnostics;
+    Io.mapRequired("Diagnostics", Doc.Diagnostics);
   }
 };
 } // end namespace yaml

Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=308014&r1=308013&r2=308014&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Tooling/CMakeLists.txt Fri Jul 14 03:37:44 2017
@@ -14,6 +14,7 @@ add_clang_unittest(ToolingTests
   CastExprTest.cpp
   CommentHandlerTest.cpp
   CompilationDatabaseTest.cpp
+  DiagnosticsYamlTest.cpp
   FixItTest.cpp
   LookupTest.cpp
   QualTypeNamesTest.cpp

Added: cfe/trunk/unittests/Tooling/DiagnosticsYamlTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/DiagnosticsYamlTest.cpp?rev=308014&view=auto
==============================================================================
--- cfe/trunk/unittests/Tooling/DiagnosticsYamlTest.cpp (added)
+++ cfe/trunk/unittests/Tooling/DiagnosticsYamlTest.cpp Fri Jul 14 03:37:44 2017
@@ -0,0 +1,166 @@
+//===- unittests/Tooling/DiagnosticsYamlTest.cpp - Serialization tests ---===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for serialization of Diagnostics.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/DiagnosticsYaml.h"
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang::tooling;
+
+static Diagnostic makeDiagnostic(StringRef DiagnosticName,
+                                 const std::string &Message, int FileOffset,
+                                 const std::string &FilePath,
+                                 const StringMap<Replacements> &Fix) {
+  DiagnosticMessage DiagMessage;
+  DiagMessage.Message = Message;
+  DiagMessage.FileOffset = FileOffset;
+  DiagMessage.FilePath = FilePath;
+  return Diagnostic(DiagnosticName, DiagMessage, Fix, {}, Diagnostic::Warning,
+                    "path/to/build/directory");
+}
+
+TEST(DiagnosticsYamlTest, serializesDiagnostics) {
+  TranslationUnitDiagnostics TUD;
+  TUD.MainSourceFile = "path/to/source.cpp";
+
+  StringMap<Replacements> Fix1 = {
+      {"path/to/source.cpp",
+       Replacements({"path/to/source.cpp", 100, 12, "replacement #1"})}};
+  TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#1", "message #1", 55,
+                                           "path/to/source.cpp", Fix1));
+
+  StringMap<Replacements> Fix2 = {
+      {"path/to/header.h",
+       Replacements({"path/to/header.h", 62, 2, "replacement #2"})}};
+  TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60,
+                                           "path/to/header.h", Fix2));
+
+  TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
+                                           "path/to/source2.cpp", {}));
+
+  std::string YamlContent;
+  raw_string_ostream YamlContentStream(YamlContent);
+
+  yaml::Output YAML(YamlContentStream);
+  YAML << TUD;
+
+  EXPECT_EQ("---\n"
+            "MainSourceFile:  path/to/source.cpp\n"
+            "Diagnostics:     \n"
+            "  - DiagnosticName:  'diagnostic#1\'\n"
+            "    Message:         'message #1'\n"
+            "    FileOffset:      55\n"
+            "    FilePath:        path/to/source.cpp\n"
+            "    Replacements:    \n"
+            "      - FilePath:        path/to/source.cpp\n"
+            "        Offset:          100\n"
+            "        Length:          12\n"
+            "        ReplacementText: 'replacement #1'\n"
+            "  - DiagnosticName:  'diagnostic#2'\n"
+            "    Message:         'message #2'\n"
+            "    FileOffset:      60\n"
+            "    FilePath:        path/to/header.h\n"
+            "    Replacements:    \n"
+            "      - FilePath:        path/to/header.h\n"
+            "        Offset:          62\n"
+            "        Length:          2\n"
+            "        ReplacementText: 'replacement #2'\n"
+            "  - DiagnosticName:  'diagnostic#3'\n"
+            "    Message:         'message #3'\n"
+            "    FileOffset:      72\n"
+            "    FilePath:        path/to/source2.cpp\n"
+            "    Replacements:    \n"
+            "...\n",
+            YamlContentStream.str());
+}
+
+TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
+  std::string YamlContent = "---\n"
+                            "MainSourceFile:  path/to/source.cpp\n"
+                            "Diagnostics:     \n"
+                            "  - DiagnosticName:  'diagnostic#1'\n"
+                            "    Message:         'message #1'\n"
+                            "    FileOffset:      55\n"
+                            "    FilePath:        path/to/source.cpp\n"
+                            "    Replacements:    \n"
+                            "      - FilePath:        path/to/source.cpp\n"
+                            "        Offset:          100\n"
+                            "        Length:          12\n"
+                            "        ReplacementText: 'replacement #1'\n"
+                            "  - DiagnosticName:  'diagnostic#2'\n"
+                            "    Message:         'message #2'\n"
+                            "    FileOffset:      60\n"
+                            "    FilePath:        path/to/header.h\n"
+                            "    Replacements:    \n"
+                            "      - FilePath:        path/to/header.h\n"
+                            "        Offset:          62\n"
+                            "        Length:          2\n"
+                            "        ReplacementText: 'replacement #2'\n"
+                            "  - DiagnosticName:  'diagnostic#3'\n"
+                            "    Message:         'message #3'\n"
+                            "    FileOffset:      98\n"
+                            "    FilePath:        path/to/source.cpp\n"
+                            "    Replacements:    \n"
+                            "...\n";
+  TranslationUnitDiagnostics TUDActual;
+  yaml::Input YAML(YamlContent);
+  YAML >> TUDActual;
+
+  ASSERT_FALSE(YAML.error());
+  ASSERT_EQ(3u, TUDActual.Diagnostics.size());
+  EXPECT_EQ("path/to/source.cpp", TUDActual.MainSourceFile);
+
+  auto getFixes = [](const StringMap<Replacements> &Fix) {
+    std::vector<Replacement> Fixes;
+    for (auto &Replacements : Fix) {
+      for (auto &Replacement : Replacements.second) {
+        Fixes.push_back(Replacement);
+      }
+    }
+    return Fixes;
+  };
+
+  Diagnostic D1 = TUDActual.Diagnostics[0];
+  EXPECT_EQ("diagnostic#1", D1.DiagnosticName);
+  EXPECT_EQ("message #1", D1.Message.Message);
+  EXPECT_EQ(55u, D1.Message.FileOffset);
+  EXPECT_EQ("path/to/source.cpp", D1.Message.FilePath);
+  std::vector<Replacement> Fixes1 = getFixes(D1.Fix);
+  ASSERT_EQ(1u, Fixes1.size());
+  EXPECT_EQ("path/to/source.cpp", Fixes1[0].getFilePath());
+  EXPECT_EQ(100u, Fixes1[0].getOffset());
+  EXPECT_EQ(12u, Fixes1[0].getLength());
+  EXPECT_EQ("replacement #1", Fixes1[0].getReplacementText());
+
+  Diagnostic D2 = TUDActual.Diagnostics[1];
+  EXPECT_EQ("diagnostic#2", D2.DiagnosticName);
+  EXPECT_EQ("message #2", D2.Message.Message);
+  EXPECT_EQ(60u, D2.Message.FileOffset);
+  EXPECT_EQ("path/to/header.h", D2.Message.FilePath);
+  std::vector<Replacement> Fixes2 = getFixes(D2.Fix);
+  ASSERT_EQ(1u, Fixes2.size());
+  EXPECT_EQ("path/to/header.h", Fixes2[0].getFilePath());
+  EXPECT_EQ(62u, Fixes2[0].getOffset());
+  EXPECT_EQ(2u, Fixes2[0].getLength());
+  EXPECT_EQ("replacement #2", Fixes2[0].getReplacementText());
+
+  Diagnostic D3 = TUDActual.Diagnostics[2];
+  EXPECT_EQ("diagnostic#3", D3.DiagnosticName);
+  EXPECT_EQ("message #3", D3.Message.Message);
+  EXPECT_EQ(98u, D3.Message.FileOffset);
+  EXPECT_EQ("path/to/source.cpp", D3.Message.FilePath);
+  std::vector<Replacement> Fixes3 = getFixes(D3.Fix);
+  EXPECT_TRUE(Fixes3.empty());
+}




More information about the cfe-commits mailing list