From 3a2476a34219361e039e226b1e91e6f6e7f6a49d Mon Sep 17 00:00:00 2001 From: "Tareq A. Siraj" Date: Fri, 26 Jul 2013 14:11:45 +0000 Subject: cpp11-migrate: Write header replacements to disk Header replacements are now written to disk in YAML format for an external tool to merge. A unique file will be created in the same directory as the header with all replacements that came from a source file that included the header file. The YAML file will have: - Name of the file - Transform ID that generated the replacement - Offset - Length - Replacement text Any tool reading these replacements should read them using the TransformDocument struct. Differential Revision: http://llvm-reviews.chandlerc.com/D1142 llvm-svn: 187204 --- .../cpp11-migrate/Core/ReplacementsYaml.h | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h (limited to 'clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h') diff --git a/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h b/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h new file mode 100644 index 00000000000..0374aae9aaf --- /dev/null +++ b/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h @@ -0,0 +1,115 @@ +//===-- 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 structs to store the migrator specific header +/// replacements and the YAML traits for Replacements. +/// +//===----------------------------------------------------------------------===// + +#ifndef CPP11_MIGRATE_REPLACEMENTS_YAML_H +#define CPP11_MIGRATE_REPLACEMENTS_YAML_H + +#include "clang/Tooling/Refactoring.h" +#include "llvm/Support/YAMLTraits.h" +#include +#include + +/// \brief A replacement struct to store the transform ID and the replacement. +struct TransformReplacements { + std::string TransformID; + std::vector GeneratedReplacements; +}; + +/// \brief The top-level YAML document that contains the name of the file and +/// the TransformReplacements. +struct TransformDocument { + std::string FileName; + std::vector Replacements; +}; + +LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement) +LLVM_YAML_IS_SEQUENCE_VECTOR(TransformReplacements) + +namespace llvm { +namespace yaml { + +/// \brief ScalarTraits to read/write std::string objects. +template <> +struct ScalarTraits { + 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 { + /// \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 + Keys(Io, R); + Io.mapRequired("Offset", Keys->Offset); + Io.mapRequired("Length", Keys->Length); + Io.mapRequired("ReplacementText", Keys->ReplacementText); + } +}; + +/// \brief Specialized MappingTraits for TransformRepleacements to be converted +/// to/from a YAML File. +template <> +struct MappingTraits { + static void mapping(IO &Io, TransformReplacements &R) { + Io.mapRequired("TransformID", R.TransformID); + Io.mapRequired("Replacements", R.GeneratedReplacements); + } +}; + +/// \brief Specialized MappingTraits for TransformDocument to be converted +/// to/from a YAML File. +template <> +struct MappingTraits { + static void mapping(IO &Io, TransformDocument &TD) { + Io.mapRequired("FileName", TD.FileName); + Io.mapRequired("Transforms", TD.Replacements); + } +}; +} // end namespace yaml +} // end namespace llvm + +#endif // CPP11_MIGRATE_REPLACEMENTS_YAML_H -- cgit v1.2.3