1 //===-- ReplacementsYaml.h -- Serialiazation for Replacements ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file defines the structure of a YAML document for serializing
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H
16 #define LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H
18 #include "clang/Tooling/Refactoring.h"
19 #include "llvm/Support/YAMLTraits.h"
22 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement)
27 /// Specialized MappingTraits to describe how a Replacement is
29 template <> struct MappingTraits<clang::tooling::Replacement> {
30 /// Helper to (de)serialize a Replacement since we don't have direct
31 /// access to its data members.
32 struct NormalizedReplacement {
33 NormalizedReplacement(const IO &)
34 : FilePath(""), Offset(0), Length(0), ReplacementText("") {}
36 NormalizedReplacement(const IO &, const clang::tooling::Replacement &R)
37 : FilePath(R.getFilePath()), Offset(R.getOffset()),
38 Length(R.getLength()), ReplacementText(R.getReplacementText()) {
39 size_t lineBreakPos = ReplacementText.find('\n');
40 while (lineBreakPos != std::string::npos) {
41 ReplacementText.replace(lineBreakPos, 1, "\n\n");
42 lineBreakPos = ReplacementText.find('\n', lineBreakPos + 2);
46 clang::tooling::Replacement denormalize(const IO &) {
47 return clang::tooling::Replacement(FilePath, Offset, Length,
54 std::string ReplacementText;
57 static void mapping(IO &Io, clang::tooling::Replacement &R) {
58 MappingNormalization<NormalizedReplacement, clang::tooling::Replacement>
60 Io.mapRequired("FilePath", Keys->FilePath);
61 Io.mapRequired("Offset", Keys->Offset);
62 Io.mapRequired("Length", Keys->Length);
63 Io.mapRequired("ReplacementText", Keys->ReplacementText);
67 /// Specialized MappingTraits to describe how a
68 /// TranslationUnitReplacements is (de)serialized.
69 template <> struct MappingTraits<clang::tooling::TranslationUnitReplacements> {
70 static void mapping(IO &Io,
71 clang::tooling::TranslationUnitReplacements &Doc) {
72 Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
73 Io.mapRequired("Replacements", Doc.Replacements);
76 } // end namespace yaml
77 } // end namespace llvm