cpp11-migrate: Use Replacement serialization from clang::tooling

Serialization of replacements has been moved to clang::tooling.

Differential Revision: http://llvm-reviews.chandlerc.com/D1423

llvm-svn: 188820
This commit is contained in:
Edwin Vane 2013-08-20 19:20:52 +00:00
parent 71a78f962b
commit d2cd2a4e1e
8 changed files with 25 additions and 202 deletions

View File

@ -19,6 +19,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
@ -30,9 +31,9 @@ using namespace clang::tooling;
void HeaderOverride::recordReplacements(
const clang::tooling::Replacements &Replaces) {
MigratorDoc.Replacements.resize(Replaces.size());
Replacements.Replacements.resize(Replaces.size());
std::copy(Replaces.begin(), Replaces.end(),
MigratorDoc.Replacements.begin());
Replacements.Replacements.begin());
}
SourceOverrides::SourceOverrides(llvm::StringRef MainFileName,
@ -93,7 +94,7 @@ void SourceOverrides::applyReplacements(clang::tooling::Replacements &Replaces,
// pipeline.
HeaderOverride &HeaderOv = Headers[FileName];
// "Create" HeaderOverride if not already existing
if (HeaderOv.getFileName().empty())
if (HeaderOv.getHeaderPath().empty())
HeaderOv = HeaderOverride(FileName, MainFileName);
HeaderOv.swapContentOverride(ResultBuf);
@ -150,7 +151,7 @@ void SourceOverrides::applyOverrides(SourceManager &SM) const {
assert(!I->second.getContentOverride().empty() &&
"Header override should not be empty!");
SM.overrideFileContents(
FM.getFile(I->second.getFileName()),
FM.getFile(I->second.getHeaderPath()),
llvm::MemoryBuffer::getMemBuffer(I->second.getContentOverride()));
}
}

View File

@ -16,8 +16,8 @@
#ifndef CPP11_MIGRATE_FILE_OVERRIDES_H
#define CPP11_MIGRATE_FILE_OVERRIDES_H
#include "Core/ReplacementsYaml.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/ADT/StringMap.h"
// Forward Declarations
@ -64,16 +64,15 @@ public:
/// \brief Constructors.
/// @{
HeaderOverride() {}
HeaderOverride(llvm::StringRef TargetFile,
llvm::StringRef MainSourceFile) {
MigratorDoc.TargetFile = TargetFile;
MigratorDoc.MainSourceFile= MainSourceFile;
HeaderOverride(llvm::StringRef HeaderPath,
llvm::StringRef MainSourceFile) : HeaderPath(HeaderPath) {
Replacements.MainSourceFile = MainSourceFile;
}
/// @}
/// \brief Getter for FileName.
llvm::StringRef getFileName() const {
return MigratorDoc.TargetFile;
/// \brief Getter for HeaderPath.
llvm::StringRef getHeaderPath() const {
return HeaderPath;
}
/// \brief Accessor for ContentOverride.
@ -88,9 +87,9 @@ public:
/// \brief Swaps the content of ContentOverride with \p S.
void swapContentOverride(std::string &S) { ContentOverride.swap(S); }
/// \brief Getter for MigratorDoc.
const MigratorDocument &getMigratorDoc() const {
return MigratorDoc;
/// \brief Getter for Replacements.
const clang::tooling::TranslationUnitReplacements &getReplacements() const {
return Replacements;
}
/// \brief Stores the replacements made by a transform to the header this
@ -105,7 +104,8 @@ public:
private:
std::string ContentOverride;
ChangedRanges Changes;
MigratorDocument MigratorDoc;
std::string HeaderPath;
clang::tooling::TranslationUnitReplacements Replacements;
};
/// \brief Container mapping header file names to override information.

View File

@ -47,7 +47,7 @@ void Reformatter::reformatChanges(SourceOverrides &Overrides,
I != E; ++I) {
const HeaderOverride &Header = I->getValue();
const tooling::Replacements &HeaderReplaces =
reformatSingleFile(Header.getFileName(), Header.getChanges(), SM);
reformatSingleFile(Header.getHeaderPath(), Header.getChanges(), SM);
Replaces.insert(HeaderReplaces.begin(), HeaderReplaces.end());
}
Overrides.applyReplacements(Replaces, SM);

View File

@ -1,105 +0,0 @@
//===-- Core/ReplacementsYaml.h ---------------------------------*- 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 provides functionality to serialize replacements for a
/// single file. It is used by the C++11 Migrator to store all the changes made
/// by a single transform to a particular file resulting from migrating a
/// translation unit of a particular main source file.
///
//===----------------------------------------------------------------------===//
#ifndef CPP11_MIGRATE_REPLACEMENTS_YAML_H
#define CPP11_MIGRATE_REPLACEMENTS_YAML_H
#include "clang/Tooling/Refactoring.h"
#include "llvm/Support/YAMLTraits.h"
#include <vector>
#include <string>
/// \brief The top-level YAML document that contains the details for the
/// replacement.
struct MigratorDocument {
std::vector<clang::tooling::Replacement> Replacements;
std::string TargetFile;
std::string MainSourceFile;
};
// FIXME: Put the YAML support for Replacement into clang::tooling. NOTE: The
// implementation below doesn't serialize the filename for Replacements.
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("Offset", Keys->Offset);
Io.mapRequired("Length", Keys->Length);
Io.mapRequired("ReplacementText", Keys->ReplacementText);
}
};
/// \brief Specialized MappingTraits for MigratorDocument to be converted
/// to/from a YAML File.
template <>
struct MappingTraits<MigratorDocument> {
static void mapping(IO &Io, MigratorDocument &TD) {
Io.mapRequired("Replacements", TD.Replacements);
Io.mapRequired("TargetFile", TD.TargetFile);
Io.mapRequired("MainSourceFile", TD.MainSourceFile);
}
};
} // end namespace yaml
} // end namespace llvm
#endif // CPP11_MIGRATE_REPLACEMENTS_YAML_H

View File

@ -379,8 +379,8 @@ int main(int argc, const char **argv) {
continue;
}
llvm::yaml::Output YAML(ReplacementsFile);
YAML << const_cast<MigratorDocument &>(
HeaderI->getValue().getMigratorDoc());
YAML << const_cast<TranslationUnitReplacements &>(
HeaderI->getValue().getReplacements());
} else {
// If -yaml-only was not specified, then change headers on disk.
// FIXME: This is transitional behaviour. Remove this functionality

View File

@ -1,11 +1,12 @@
---
MainSourceFile: "$(path)/main.cpp"
Replacements:
- Offset: 432
- FilePath: "$(path)/common.h"
Offset: 432
Length: 61
ReplacementText: "(auto & elem : C)"
- Offset: 506
- FilePath: "$(path)/common.h"
Offset: 506
Length: 2
ReplacementText: "elem"
TargetFile: "$(path)/common.h"
MainSourceFile: "$(path)/main.cpp"
...

View File

@ -11,7 +11,6 @@ add_extra_unittest(Cpp11MigrateTests
ReformattingTest.cpp
IncludeExcludeTest.cpp
PerfSupportTest.cpp
ReplacementsYamlTest.cpp
TransformTest.cpp
UniqueHeaderNameTest.cpp
)

View File

@ -1,73 +0,0 @@
//===- unittests/cpp11-migrate/ReplacementsYamlTest.cpp -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Tests that change description files can be written and read.
//
//===----------------------------------------------------------------------===//
#include "Utility.h"
#include "Core/FileOverrides.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(ReplacementsYamlTest, writeReadTest) {
using clang::tooling::Replacement;
const std::string TargetFile = "/path/to/common.h";
const std::string MainSourceFile = "/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";
MigratorDocument Doc;
Doc.Replacements.push_back(Replacement(TargetFile, ReplacementOffset1,
ReplacementLength1, ReplacementText1));
Doc.Replacements.push_back(Replacement(TargetFile, ReplacementOffset2,
ReplacementLength2, ReplacementText2));
Doc.TargetFile = TargetFile.c_str();
Doc.MainSourceFile= MainSourceFile.c_str();
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.
{
MigratorDocument DocActual;
yaml::Input YAML(YamlContent);
YAML >> DocActual;
ASSERT_NO_ERROR(YAML.error());
EXPECT_EQ(TargetFile, DocActual.TargetFile);
EXPECT_EQ(MainSourceFile, DocActual.MainSourceFile);
ASSERT_EQ(2u, DocActual.Replacements.size());
EXPECT_EQ(ReplacementOffset1, DocActual.Replacements[0].getOffset());
EXPECT_EQ(ReplacementLength1, DocActual.Replacements[0].getLength());
EXPECT_EQ(ReplacementText1,
DocActual.Replacements[0].getReplacementText().str());
EXPECT_EQ(ReplacementOffset2, DocActual.Replacements[1].getOffset());
EXPECT_EQ(ReplacementLength2, DocActual.Replacements[1].getLength());
EXPECT_EQ(ReplacementText2,
DocActual.Replacements[1].getReplacementText().str());
}
}