vladimir.plyashkun updated this revision to Diff 105078.
vladimir.plyashkun added a comment.
Herald added a subscriber: JDevlieghere.

- fixed `No newline at end of file` problem
- provided test-case to check that diagnostics with no fixes will be applied 
correctly


Repository:
  rL LLVM

https://reviews.llvm.org/D34404

Files:
  include/clang/Tooling/DiagnosticsYaml.h
  unittests/Tooling/CMakeLists.txt
  unittests/Tooling/DiagnosticsYamlTest.cpp
  unittests/clang-apply-replacements/ApplyReplacementsTest.cpp
  unittests/clang-apply-replacements/CMakeLists.txt

Index: unittests/Tooling/DiagnosticsYamlTest.cpp
===================================================================
--- /dev/null
+++ unittests/Tooling/DiagnosticsYamlTest.cpp
@@ -0,0 +1,183 @@
+//===- 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/Core/Diagnostic.h"
+#include "clang/Tooling/DiagnosticsYaml.h"
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang::tooling;
+
+TEST(DiagnosticsYamlTest, SerializesDiagnostics) {
+  TranslationUnitDiagnostics TUD;
+  TUD.MainSourceFile = "path/to/source.cpp";
+  DiagnosticMessage Message1;
+  Message1.Message = "message #1";
+  Message1.FileOffset = 55;
+  Message1.FilePath = "path/to/source.cpp";
+  StringMap<Replacements> Fix1{
+    { "path/to/source.cpp", Replacements(Replacement("path/to/source.cpp", 100,
+    12, "replacement #1")) } };
+  DiagnosticMessage Message2;
+  Message2.Message = "message #2";
+  Message2.FileOffset = 60;
+  Message2.FilePath = "path/to/header.h";
+  StringMap<Replacements> Fix2{
+    { "path/to/header.h",
+    Replacements(Replacement("path/to/header.h", 62, 2, "replacement #2")) } };
+  SmallVector<DiagnosticMessage, 1> EmptyNotes;
+  TUD.Diagnostics.emplace_back("diagnostic#1", Message1, Fix1, EmptyNotes,
+                               Diagnostic::Warning, "path/to/build/directory");
+  TUD.Diagnostics.emplace_back("diagnostic#2", Message2, Fix2, EmptyNotes,
+                               Diagnostic::Error, "path/to/another/build/directory");
+
+  std::string YamlContent;
+  raw_string_ostream YamlContentStream(YamlContent);
+
+  yaml::Output YAML(YamlContentStream);
+  YAML << TUD;
+
+  ASSERT_STREQ("---\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"
+    "...\n",
+      YamlContentStream.str().c_str());
+}
+
+TEST(DiagnosticsYamlTest, SerializesDiagnosticWithNoFix) {
+  TranslationUnitDiagnostics TUD;
+  TUD.MainSourceFile = "path/to/source.cpp";
+  DiagnosticMessage Message;
+  Message.Message = "message #1";
+  Message.FileOffset = 55;
+  Message.FilePath = "path/to/source.cpp";
+  StringMap<Replacements> EmptyReplacements;
+  SmallVector<DiagnosticMessage, 1> EmptyNotes;
+  TUD.Diagnostics.emplace_back("diagnostic#1", Message, EmptyReplacements,
+                               EmptyNotes, Diagnostic::Warning,
+                               "path/to/build/directory");
+
+  std::string YamlContent;
+  raw_string_ostream YamlContentStream(YamlContent);
+
+  yaml::Output YAML(YamlContentStream);
+  YAML << TUD;
+
+  ASSERT_STREQ("---\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"
+    "...\n",
+      YamlContentStream.str().c_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());
+  ASSERT_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];
+  ASSERT_EQ("diagnostic#1", D1.DiagnosticName);
+  ASSERT_EQ("message #1", D1.Message.Message);
+  ASSERT_EQ(55u, D1.Message.FileOffset);
+  ASSERT_EQ("path/to/source.cpp", D1.Message.FilePath);
+  std::vector<Replacement> Fixes1 = getFixes(D1.Fix);
+  ASSERT_EQ(1u, Fixes1.size());
+  ASSERT_EQ("path/to/source.cpp", Fixes1[0].getFilePath());
+  ASSERT_EQ(100, Fixes1[0].getOffset());
+  ASSERT_EQ(12, Fixes1[0].getLength());
+  ASSERT_EQ("replacement #1", Fixes1[0].getReplacementText());
+
+  Diagnostic D2 = TUDActual.Diagnostics[1];
+  ASSERT_EQ("diagnostic#2", D2.DiagnosticName);
+  ASSERT_EQ("message #2", D2.Message.Message);
+  ASSERT_EQ(60, D2.Message.FileOffset);
+  ASSERT_EQ("path/to/header.h", D2.Message.FilePath);
+  std::vector<Replacement> Fixes2 = getFixes(D2.Fix);
+  ASSERT_EQ(1u, Fixes2.size());
+  ASSERT_EQ("path/to/header.h", Fixes2[0].getFilePath());
+  ASSERT_EQ(62, Fixes2[0].getOffset());
+  ASSERT_EQ(2, Fixes2[0].getLength());
+  ASSERT_EQ("replacement #2", Fixes2[0].getReplacementText());
+
+  Diagnostic D3 = TUDActual.Diagnostics[2];
+  ASSERT_EQ("diagnostic#3", D3.DiagnosticName);
+  ASSERT_EQ("message #3", D3.Message.Message);
+  ASSERT_EQ(98, D3.Message.FileOffset);
+  ASSERT_EQ("path/to/source.cpp", D3.Message.FilePath);
+  std::vector<Replacement> Fixes3 = getFixes(D3.Fix);
+  ASSERT_TRUE(Fixes3.empty());
+}
Index: unittests/clang-apply-replacements/CMakeLists.txt
===================================================================
--- unittests/clang-apply-replacements/CMakeLists.txt
+++ unittests/clang-apply-replacements/CMakeLists.txt
@@ -8,6 +8,7 @@
   )
 
 add_extra_unittest(ClangApplyReplacementsTests
+  ApplyReplacementsTest.cpp
   ReformattingTest.cpp
   )
 
Index: unittests/clang-apply-replacements/ApplyReplacementsTest.cpp
===================================================================
--- /dev/null
+++ unittests/clang-apply-replacements/ApplyReplacementsTest.cpp
@@ -0,0 +1,58 @@
+//===- clang-apply-replacements/ApplyReplacementsTest.cpp ----------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+using namespace clang::tooling;
+using namespace clang::replace;
+using namespace llvm;
+
+static tooling::Diagnostic makeDiagnostic(const StringRef DiagnosticName,
+                                          DiagnosticMessage &Message,
+                                          StringMap<Replacements> &Replacements,
+                                          StringRef BuildDirectory) {
+  SmallVector<DiagnosticMessage, 1> EmptyNotes;
+  return tooling::Diagnostic(DiagnosticName, Message, Replacements, EmptyNotes,
+                             tooling::Diagnostic::Warning, BuildDirectory);
+}
+
+static TUDiagnostics makeTUDiagnostics(const std::string &MainSourceFile,
+                                       const StringRef DiagnosticName,
+                                       DiagnosticMessage &Message,
+                                       StringMap<Replacements> &Replacements,
+                                       StringRef BuildDirectory) {
+  TUDiagnostics TUs;
+  TranslationUnitDiagnostics TU;
+  TU.MainSourceFile = MainSourceFile;
+  TU.Diagnostics.push_back(
+	  makeDiagnostic(DiagnosticName, Message, Replacements, BuildDirectory));
+  TUs.push_back(TU);
+  return TUs;
+}
+
+// Test to ensure diagnostics with no fixes, will be merged correctly 
+// before applying.
+TEST(ApplyReplacementsTest, mergeDiagnosticsWithNoFixes) {
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
+  DiagnosticsEngine Diagnostics(
+	  IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), DiagOpts.get());
+  FileManager Files((FileSystemOptions()));
+  SourceManager SM(Diagnostics, Files);
+  StringMap<Replacements> EmptyReplacements;
+  DiagnosticMessage Message;
+  TUDiagnostics TUs = makeTUDiagnostics("path/to/source.cpp", "diagnostic",
+	                                    Message, EmptyReplacements, "path/to");
+  FileToReplacementsMap ReplacementsMap;
+  bool success = mergeAndDeduplicate(TUs, ReplacementsMap, SM);
+
+  EXPECT_TRUE(success);
+  EXPECT_TRUE(ReplacementsMap.empty());
+}
Index: unittests/Tooling/CMakeLists.txt
===================================================================
--- unittests/Tooling/CMakeLists.txt
+++ unittests/Tooling/CMakeLists.txt
@@ -14,6 +14,7 @@
   CastExprTest.cpp
   CommentHandlerTest.cpp
   CompilationDatabaseTest.cpp
+  DiagnosticsYamlTest.cpp
   FixItTest.cpp
   LookupTest.cpp
   QualTypeNamesTest.cpp
Index: include/clang/Tooling/DiagnosticsYaml.h
===================================================================
--- include/clang/Tooling/DiagnosticsYaml.h
+++ include/clang/Tooling/DiagnosticsYaml.h
@@ -56,6 +56,9 @@
     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::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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to