compositeprimes updated this revision to Diff 245717.
compositeprimes marked an inline comment as done.
compositeprimes added a comment.

Added tests for Yaml serialization


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69782/new/

https://reviews.llvm.org/D69782

Files:
  include/clang/Tooling/Core/Diagnostic.h
  include/clang/Tooling/DiagnosticsYaml.h
  lib/Tooling/Core/Diagnostic.cpp
  unittests/Tooling/DiagnosticsYamlTest.cpp

Index: unittests/Tooling/DiagnosticsYamlTest.cpp
===================================================================
--- unittests/Tooling/DiagnosticsYamlTest.cpp
+++ unittests/Tooling/DiagnosticsYamlTest.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Tooling/DiagnosticsYaml.h"
+#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/SmallVector.h"
 #include "clang/Tooling/Core/Diagnostic.h"
 #include "clang/Tooling/ReplacementsYaml.h"
 #include "gtest/gtest.h"
@@ -30,13 +31,24 @@
   return DiagMessage;
 }
 
+static FileByteRange makeByteRange(int FileOffset,
+                                   int Length,
+                                   const std::string &FilePath) {
+  FileByteRange Range;
+  Range.FileOffset = FileOffset;
+  Range.Length = Length;
+  Range.FilePath = FilePath;
+  return Range;
+}
+
 static Diagnostic makeDiagnostic(StringRef DiagnosticName,
                                  const std::string &Message, int FileOffset,
                                  const std::string &FilePath,
-                                 const StringMap<Replacements> &Fix) {
+                                 const StringMap<Replacements> &Fix,
+                                 const SmallVector<FileByteRange, 1> &Ranges) {
   return Diagnostic(DiagnosticName,
                     makeMessage(Message, FileOffset, FilePath, Fix), {},
-                    Diagnostic::Warning, "path/to/build/directory");
+                    Diagnostic::Warning, "path/to/build/directory", Ranges);
 }
 
 static const char *YAMLContent =
@@ -63,6 +75,10 @@
     "          Offset:          62\n"
     "          Length:          2\n"
     "          ReplacementText: 'replacement #2'\n"
+    "    Ranges:\n"
+    "      - FilePath:        'path/to/source.cpp'\n"
+    "        FileOffset:      10\n"
+    "        Length:          10\n"
     "  - DiagnosticName:  'diagnostic#3'\n"
     "    DiagnosticMessage:\n"
     "      Message:         'message #3'\n"
@@ -88,16 +104,18 @@
       {"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));
+                                           "path/to/source.cpp", Fix1, {}));
 
   StringMap<Replacements> Fix2 = {
       {"path/to/header.h",
        Replacements({"path/to/header.h", 62, 2, "replacement #2"})}};
+  SmallVector<FileByteRange, 1> Ranges2 =
+      {makeByteRange(10, 10, "path/to/source.cpp")};
   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#2", "message #2", 60,
-                                           "path/to/header.h", Fix2));
+                                           "path/to/header.h", Fix2, Ranges2));
 
   TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
-                                           "path/to/source2.cpp", {}));
+                                           "path/to/source2.cpp", {}, {}));
   TUD.Diagnostics.back().Notes.push_back(
       makeMessage("Note1", 88, "path/to/note1.cpp", {}));
   TUD.Diagnostics.back().Notes.push_back(
@@ -142,6 +160,7 @@
   EXPECT_EQ(100u, Fixes1[0].getOffset());
   EXPECT_EQ(12u, Fixes1[0].getLength());
   EXPECT_EQ("replacement #1", Fixes1[0].getReplacementText());
+  EXPECT_TRUE(D1.Ranges.empty());
 
   Diagnostic D2 = TUDActual.Diagnostics[1];
   EXPECT_EQ("diagnostic#2", D2.DiagnosticName);
@@ -154,6 +173,10 @@
   EXPECT_EQ(62u, Fixes2[0].getOffset());
   EXPECT_EQ(2u, Fixes2[0].getLength());
   EXPECT_EQ("replacement #2", Fixes2[0].getReplacementText());
+  EXPECT_EQ(1, D2.Ranges.size());
+  EXPECT_EQ("path/to/source.cpp", D2.Ranges[0].FilePath);
+  EXPECT_EQ(10, D2.Ranges[0].FileOffset);
+  EXPECT_EQ(10, D2.Ranges[0].Length);
 
   Diagnostic D3 = TUDActual.Diagnostics[2];
   EXPECT_EQ("diagnostic#3", D3.DiagnosticName);
@@ -169,4 +192,5 @@
   EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath);
   std::vector<Replacement> Fixes3 = getFixes(D3.Message.Fix);
   EXPECT_TRUE(Fixes3.empty());
+  EXPECT_TRUE(D3.Ranges.empty());
 }
Index: lib/Tooling/Core/Diagnostic.cpp
===================================================================
--- lib/Tooling/Core/Diagnostic.cpp
+++ lib/Tooling/Core/Diagnostic.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Tooling/Core/Diagnostic.h"
+#include "third_party/llvm/llvm-project/clang/include/clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "llvm/ADT/STLExtras.h"
 
@@ -34,6 +35,16 @@
     FileOffset = Sources.getFileOffset(Loc);
 }
 
+FileByteRange::FileByteRange(
+    const SourceManager &Sources, CharSourceRange Range)
+    : FileOffset(0), Length(0) {
+  FilePath = std::string(Sources.getFilename(Range.getBegin()));
+  if (!FilePath.empty()) {
+    FileOffset = Sources.getFileOffset(Range.getBegin());
+    Length = Sources.getFileOffset(Range.getEnd()) - FileOffset;
+  }
+}
+
 Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
                        Diagnostic::Level DiagLevel, StringRef BuildDirectory)
     : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
@@ -42,9 +53,10 @@
 Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
                        const DiagnosticMessage &Message,
                        const SmallVector<DiagnosticMessage, 1> &Notes,
-                       Level DiagLevel, llvm::StringRef BuildDirectory)
+                       Level DiagLevel, llvm::StringRef BuildDirectory,
+                       const SmallVector<FileByteRange, 1> &Ranges)
     : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes),
-      DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
+      DiagLevel(DiagLevel), BuildDirectory(BuildDirectory), Ranges(Ranges) {}
 
 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) {
    if (!D.Message.Fix.empty())
Index: include/clang/Tooling/DiagnosticsYaml.h
===================================================================
--- include/clang/Tooling/DiagnosticsYaml.h
+++ include/clang/Tooling/DiagnosticsYaml.h
@@ -22,10 +22,19 @@
 
 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
+LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::FileByteRange)
 
 namespace llvm {
 namespace yaml {
 
+template <> struct MappingTraits<clang::tooling::FileByteRange> {
+  static void mapping(IO &Io, clang::tooling::FileByteRange &R) {
+    Io.mapRequired("FilePath", R.FilePath);
+    Io.mapRequired("FileOffset", R.FileOffset);
+    Io.mapRequired("Length", R.Length);
+  }
+};
+
 template <> struct MappingTraits<clang::tooling::DiagnosticMessage> {
   static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) {
     Io.mapRequired("Message", M.Message);
@@ -58,11 +67,12 @@
 
     NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
         : DiagnosticName(D.DiagnosticName), Message(D.Message), Notes(D.Notes),
-          DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory) {}
+          DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory),
+          Ranges(D.Ranges) {}
 
     clang::tooling::Diagnostic denormalize(const IO &) {
       return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
-                                        DiagLevel, BuildDirectory);
+                                        DiagLevel, BuildDirectory, Ranges);
     }
 
     std::string DiagnosticName;
@@ -71,6 +81,7 @@
     SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
     clang::tooling::Diagnostic::Level DiagLevel;
     std::string BuildDirectory;
+    SmallVector<clang::tooling::FileByteRange, 1> Ranges;
   };
 
   static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
@@ -79,6 +90,7 @@
     Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
     Io.mapRequired("DiagnosticMessage", Keys->Message);
     Io.mapOptional("Notes", Keys->Notes);
+    Io.mapOptional("Ranges", Keys->Ranges);
 
     // FIXME: Export properly all the different fields.
   }
Index: include/clang/Tooling/Core/Diagnostic.h
===================================================================
--- include/clang/Tooling/Core/Diagnostic.h
+++ include/clang/Tooling/Core/Diagnostic.h
@@ -47,6 +47,17 @@
   llvm::StringMap<Replacements> Fix;
 };
 
+/// Represents a range within a specific source file.
+struct FileByteRange {
+  FileByteRange() = default;
+
+  FileByteRange(const SourceManager &Sources, CharSourceRange Range);
+
+  std::string FilePath;
+  unsigned FileOffset;
+  unsigned Length;
+};
+
 /// Represents the diagnostic with the level of severity and possible
 /// fixes to be applied.
 struct Diagnostic {
@@ -62,7 +73,8 @@
 
   Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message,
              const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
-             llvm::StringRef BuildDirectory);
+             llvm::StringRef BuildDirectory,
+             const SmallVector<FileByteRange, 1> &Ranges);
 
   /// Name identifying the Diagnostic.
   std::string DiagnosticName;
@@ -84,6 +96,10 @@
   ///
   /// Note: it is empty in unittest.
   std::string BuildDirectory;
+
+  /// Extra source ranges associated with the diagnostic (in addition to the
+  /// location of the Message above).
+  SmallVector<FileByteRange, 1> Ranges;
 };
 
 /// Collection of Diagnostics generated from a single translation unit.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to