r365017 - [clang-tidy] Fix the YAML created for checks like modernize-pass-by-value

Ivan Donchevskii via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 3 03:21:51 PDT 2019


Author: yvvan
Date: Wed Jul  3 03:21:50 2019
New Revision: 365017

URL: http://llvm.org/viewvc/llvm-project?rev=365017&view=rev
Log:
[clang-tidy] Fix the YAML created for checks like modernize-pass-by-value

Currently this check generates the replacement with the newline in the end.
The proper way to export it to YAML is to have two \n\n instead of one.
Without this fix clients should reinterpret the replacement as
"#include <utility> " instead of "#include <utility>\n"

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

Modified:
    cfe/trunk/include/clang/Tooling/ReplacementsYaml.h
    cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp

Modified: cfe/trunk/include/clang/Tooling/ReplacementsYaml.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/ReplacementsYaml.h?rev=365017&r1=365016&r2=365017&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/ReplacementsYaml.h (original)
+++ cfe/trunk/include/clang/Tooling/ReplacementsYaml.h Wed Jul  3 03:21:50 2019
@@ -35,7 +35,13 @@ template <> struct MappingTraits<clang::
 
     NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
         : FilePath(R.getFilePath()), Offset(R.getOffset()),
-          Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
+          Length(R.getLength()), ReplacementText(R.getReplacementText()) {
+      size_t lineBreakPos = ReplacementText.find('\n');
+      while (lineBreakPos != std::string::npos) {
+        ReplacementText.replace(lineBreakPos, 1, "\n\n");
+        lineBreakPos = ReplacementText.find('\n', lineBreakPos + 2);
+      }
+    }
 
     clang::tooling::Replacement denormalize(const IO &) {
       return clang::tooling::Replacement(FilePath, Offset, Length,

Modified: cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp?rev=365017&r1=365016&r2=365017&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp Wed Jul  3 03:21:50 2019
@@ -46,6 +46,30 @@ TEST(ReplacementsYamlTest, serializesRep
                YamlContentStream.str().c_str());
 }
 
+TEST(ReplacementsYamlTest, serializesNewLines) {
+  TranslationUnitReplacements Doc;
+
+  Doc.MainSourceFile = "/path/to/source.cpp";
+  Doc.Replacements.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");
+
+  std::string YamlContent;
+  llvm::raw_string_ostream YamlContentStream(YamlContent);
+
+  yaml::Output YAML(YamlContentStream);
+  YAML << Doc;
+
+  // NOTE: If this test starts to fail for no obvious reason, check whitespace.
+  ASSERT_STREQ("---\n"
+               "MainSourceFile:  '/path/to/source.cpp'\n"
+               "Replacements:    \n" // Extra whitespace here!
+               "  - FilePath:        '/path/to/file1.h'\n"
+               "    Offset:          0\n"
+               "    Length:          0\n"
+               "    ReplacementText: '#include <utility>\n\n'\n"
+               "...\n",
+               YamlContentStream.str().c_str());
+}
+
 TEST(ReplacementsYamlTest, deserializesReplacements) {
   std::string YamlContent = "---\n"
                             "MainSourceFile:      /path/to/source.cpp\n"




More information about the cfe-commits mailing list