Hi klimek, djasper, silvas,
Adding a YAML document definition for serializing a vector of
Replacements. Document is basically just a sequence of Replacements but
has an optional 'Context' field a producer may provide to annotate the
Replacements serialized in a single document.
Tests added.
http://llvm-reviews.chandlerc.com/D1422
Files:
include/clang/Tooling/ReplacementsYaml.h
unittests/Tooling/CMakeLists.txt
unittests/Tooling/ReplacementsYamlTest.cpp
Index: include/clang/Tooling/ReplacementsYaml.h
===================================================================
--- /dev/null
+++ include/clang/Tooling/ReplacementsYaml.h
@@ -0,0 +1,107 @@
+//===-- ReplacementsYaml.h -- Serialiazation for Replacements ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file defines the structure of a YAML document for serializing
+/// replacements.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
+#define LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
+
+#include "clang/Tooling/Refactoring.h"
+#include "llvm/Support/YAMLTraits.h"
+#include <vector>
+#include <string>
+
+namespace clang {
+namespace tooling {
+
+/// \brief The top-level YAML document that contains all Replacements.
+struct ReplacementsDocument {
+ /// A freeform chunk of text to describe the context of the replacements in
+ /// this document.
+ std::string Context;
+
+ std::vector<Replacement> Replacements;
+};
+
+} // end namespace tooling
+} // end namespace clang
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement)
+
+namespace llvm {
+namespace yaml {
+
+/// \brief ScalarTraits to read/write std::string objects.
+template <>
+struct ScalarTraits<std::string> {
+ static void output(const std::string &Val, void *, llvm::raw_ostream &Out) {
+ // We need to put quotes around the string to make sure special characters
+ // in the string is not treated as YAML tokens.
+ std::string NormalizedVal = std::string("\"") + Val + std::string("\"");
+ Out << NormalizedVal;
+ }
+
+ static StringRef input(StringRef Scalar, void *, std::string &Val) {
+ Val = Scalar;
+ return StringRef();
+ }
+};
+
+/// \brief Specialized MappingTraits for Repleacements to be converted to/from
+/// a YAML File.
+template <>
+struct MappingTraits<clang::tooling::Replacement> {
+ /// \brief Normalize clang::tooling::Replacement to provide direct access to
+ /// its members.
+ struct NormalizedReplacement {
+ NormalizedReplacement(const IO &)
+ : FilePath(""), Offset(0), Length(0), ReplacementText("") {}
+
+ NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
+ : FilePath(R.getFilePath()), Offset(R.getOffset()),
+ Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
+
+ clang::tooling::Replacement denormalize(const IO &) {
+ return clang::tooling::Replacement(FilePath, Offset, Length,
+ ReplacementText);
+ }
+
+ std::string FilePath;
+ unsigned int Offset;
+ unsigned int Length;
+ std::string ReplacementText;
+ };
+
+ static void mapping(IO &Io, clang::tooling::Replacement &R) {
+ MappingNormalization<NormalizedReplacement, clang::tooling::Replacement>
+ Keys(Io, R);
+ Io.mapRequired("FilePath", Keys->FilePath);
+ Io.mapRequired("Offset", Keys->Offset);
+ Io.mapRequired("Length", Keys->Length);
+ Io.mapRequired("ReplacementText", Keys->ReplacementText);
+ }
+};
+
+/// \brief Specialized MappingTraits for ReplacementsDocument to be converted
+/// to/from a YAML File.
+template <>
+struct MappingTraits<clang::tooling::ReplacementsDocument> {
+ static void mapping(IO &Io, clang::tooling::ReplacementsDocument &Doc) {
+ Io.mapOptional("Context", Doc.Context, std::string());
+ Io.mapRequired("Replacements", Doc.Replacements);
+ }
+};
+} // end namespace yaml
+} // end namespace llvm
+
+#endif // LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
Index: unittests/Tooling/CMakeLists.txt
===================================================================
--- unittests/Tooling/CMakeLists.txt
+++ unittests/Tooling/CMakeLists.txt
@@ -14,6 +14,7 @@
RefactoringTest.cpp
RewriterTest.cpp
RefactoringCallbacksTest.cpp
+ ReplacementsYamlTest.cpp
)
target_link_libraries(ToolingTests
Index: unittests/Tooling/ReplacementsYamlTest.cpp
===================================================================
--- /dev/null
+++ unittests/Tooling/ReplacementsYamlTest.cpp
@@ -0,0 +1,116 @@
+//===- unittests/Tooling/ReplacementsYamlTest.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 Replacements.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "gtest/gtest.h"
+
+// FIXME: Copied from llvm/unittests/Support/Path.cpp
+#define ASSERT_NO_ERROR(x) \
+ if (error_code ASSERT_NO_ERROR_ec = x) { \
+ SmallString<128> MessageStorage; \
+ raw_svector_ostream Message(MessageStorage); \
+ Message << #x ": did not return errc::success.\n" \
+ << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
+ << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
+ GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
+ } else {}
+
+using namespace llvm;
+using namespace clang::tooling;
+
+TEST(ReplacementsYamlTest, writeReadTest) {
+
+ const std::string TargetFile = "/path/to/common.h";
+ const std::string Context = "/path/to/source.cpp";
+ const unsigned int ReplacementOffset1 = 232;
+ const unsigned int ReplacementLength1 = 56;
+ const std::string ReplacementText1 = "(auto & elem : V)";
+ const unsigned int ReplacementOffset2 = 301;
+ const unsigned int ReplacementLength2 = 2;
+ const std::string ReplacementText2 = "elem";
+
+ ReplacementsDocument Doc;
+ Doc.Replacements.push_back(Replacement(TargetFile, ReplacementOffset1,
+ ReplacementLength1, ReplacementText1));
+ Doc.Replacements.push_back(Replacement(TargetFile, ReplacementOffset2,
+ ReplacementLength2, ReplacementText2));
+
+ Doc.Context = Context;
+
+ std::string YamlContent;
+ llvm::raw_string_ostream YamlContentStream(YamlContent);
+
+ // Write to the YAML file.
+ {
+ yaml::Output YAML(YamlContentStream);
+ YAML << Doc;
+ YamlContentStream.str();
+ ASSERT_NE(YamlContent.length(), 0u);
+ }
+
+ // Read from the YAML file and verify that what was written is exactly what
+ // we read back.
+ {
+ ReplacementsDocument DocActual;
+ yaml::Input YAML(YamlContent);
+ YAML >> DocActual;
+ ASSERT_NO_ERROR(YAML.error());
+ EXPECT_EQ(Context, DocActual.Context);
+ ASSERT_EQ(2u, DocActual.Replacements.size());
+
+ EXPECT_EQ(TargetFile, DocActual.Replacements[0].getFilePath());
+ EXPECT_EQ(ReplacementOffset1, DocActual.Replacements[0].getOffset());
+ EXPECT_EQ(ReplacementLength1, DocActual.Replacements[0].getLength());
+ EXPECT_EQ(ReplacementText1,
+ DocActual.Replacements[0].getReplacementText().str());
+
+ EXPECT_EQ(TargetFile, DocActual.Replacements[1].getFilePath());
+ EXPECT_EQ(ReplacementOffset2, DocActual.Replacements[1].getOffset());
+ EXPECT_EQ(ReplacementLength2, DocActual.Replacements[1].getLength());
+ EXPECT_EQ(ReplacementText2,
+ DocActual.Replacements[1].getReplacementText().str());
+ }
+}
+
+TEST(ReplacementsYamlTest, optionalContextWrite) {
+
+ ReplacementsDocument Doc;
+ Doc.Replacements.push_back(Replacement(/*FilePath=*/"target_file.h",
+ /*Offset=*/1,
+ /*Length=*/10,
+ /*ReplacementText=*/"replacement"));
+ std::string YamlContent;
+ llvm::raw_string_ostream YamlContentStream(YamlContent);
+
+ // Make sure a doc can be written without the context field.
+ yaml::Output YAML(YamlContentStream);
+ YAML << Doc;
+ YamlContentStream.str();
+ ASSERT_NE(YamlContent.length(), 0u);
+ ASSERT_EQ(std::string::npos, YamlContent.find("Context:"));
+}
+
+TEST(ReplacementsYamlTest, optionalContextRead) {
+ // Make sure a doc can be read without the context field.
+ std::string YamlContent = "---\n"
+ "Replacements:\n"
+ " - FilePath: \"target_file.h\"\n"
+ " Offset: 1\n"
+ " Length: 10\n"
+ " ReplacementText: \"replacement\"\n"
+ "...\n";
+ ReplacementsDocument DocActual;
+ yaml::Input YAML(YamlContent);
+ YAML >> DocActual;
+ ASSERT_NO_ERROR(YAML.error());
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits