ReplacementsYaml.h revision 5321e94281f4944d05ab5f90208933afc1e7cae8
1//===-- ReplacementsYaml.h -- Serialiazation for Replacements ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file defines the structure of a YAML document for serializing
12/// replacements.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
17#define LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
18
19#include "clang/Tooling/Refactoring.h"
20#include "llvm/Support/YAMLTraits.h"
21#include <vector>
22#include <string>
23
24LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement)
25
26namespace llvm {
27namespace yaml {
28
29/// \brief ScalarTraits to read/write std::string objects.
30template <> struct ScalarTraits<std::string> {
31  static void output(const std::string &Val, void *, llvm::raw_ostream &Out) {
32    // We need to put quotes around the string to make sure special characters
33    // in the string is not treated as YAML tokens.
34    std::string NormalizedVal = std::string("\"") + Val + std::string("\"");
35    Out << NormalizedVal;
36  }
37
38  static StringRef input(StringRef Scalar, void *, std::string &Val) {
39    Val = Scalar;
40    return StringRef();
41  }
42};
43
44/// \brief Specialized MappingTraits to describe how a Replacement is
45/// (de)serialized.
46template <> struct MappingTraits<clang::tooling::Replacement> {
47  /// \brief Helper to (de)serialize a Replacement since we don't have direct
48  /// access to its data members.
49  struct NormalizedReplacement {
50    NormalizedReplacement(const IO &)
51        : FilePath(""), Offset(0), Length(0), ReplacementText("") {}
52
53    NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
54        : FilePath(R.getFilePath()), Offset(R.getOffset()),
55          Length(R.getLength()), ReplacementText(R.getReplacementText()) {}
56
57    clang::tooling::Replacement denormalize(const IO &) {
58      return clang::tooling::Replacement(FilePath, Offset, Length,
59                                         ReplacementText);
60    }
61
62    std::string FilePath;
63    unsigned int Offset;
64    unsigned int Length;
65    std::string ReplacementText;
66  };
67
68  static void mapping(IO &Io, clang::tooling::Replacement &R) {
69    MappingNormalization<NormalizedReplacement, clang::tooling::Replacement>
70    Keys(Io, R);
71    Io.mapRequired("FilePath", Keys->FilePath);
72    Io.mapRequired("Offset", Keys->Offset);
73    Io.mapRequired("Length", Keys->Length);
74    Io.mapRequired("ReplacementText", Keys->ReplacementText);
75  }
76};
77
78/// \brief Specialized MappingTraits to describe how a
79/// TranslationUnitReplacements is (de)serialized.
80template <> struct MappingTraits<clang::tooling::TranslationUnitReplacements> {
81  static void mapping(IO &Io,
82                      clang::tooling::TranslationUnitReplacements &Doc) {
83    Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
84    Io.mapOptional("Context", Doc.Context, std::string());
85    Io.mapRequired("Replacements", Doc.Replacements);
86  }
87};
88} // end namespace yaml
89} // end namespace llvm
90
91#endif // LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H
92