-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathExternalReplacement.cpp
More file actions
196 lines (166 loc) · 6.44 KB
/
ExternalReplacement.cpp
File metadata and controls
196 lines (166 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//===--------------- ExternalReplacement.cpp ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This file target to process replacement based operation:
// -Save replacement to external (disk file)
// -Load replacement from external (disk file)
// -Merge replacement in current migration with previous migration.
#include "ExternalReplacement.h"
#include "AnalysisInfo.h"
#include "IncMigration/IncrementalMigrationUtility.h"
#include "Utility.h"
#include "clang/Tooling/Core/Diagnostic.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_os_ostream.h"
#include <algorithm>
#include <cassert>
#include <fstream>
using namespace llvm;
namespace path = llvm::sys::path;
namespace fs = llvm::sys::fs;
using clang::tooling::Replacements;
namespace clang {
namespace dpct {
int save2Yaml(
clang::tooling::UnifiedPath &YamlFile,
clang::tooling::UnifiedPath &SrcFileName,
const std::vector<clang::tooling::Replacement> &Replaces,
const std::vector<clang::tooling::MainSourceFileInfo> &MainSrcFilesDigest,
const std::map<clang::tooling::UnifiedPath,
std::vector<clang::tooling::CompilationInfo>>
&CompileTargets) {
std::string YamlContent;
llvm::raw_string_ostream YamlContentStream(YamlContent);
llvm::yaml::Output YAMLOut(YamlContentStream);
// list all the replacement.
clang::tooling::TranslationUnitReplacements TUR;
TUR.MainSourceFile = SrcFileName.getCanonicalPath();
TUR.Replacements.insert(TUR.Replacements.end(), Replaces.begin(),
Replaces.end());
TUR.MainSourceFilesDigest = MainSrcFilesDigest;
for (const auto &Entry : CompileTargets) {
TUR.CompileTargets[Entry.first.getCanonicalPath().str()] = Entry.second;
}
TUR.DpctVersion = clang::dpct::getDpctVersionStr();
auto Ver = clang::getCudaVersionPair(DpctGlobalInfo::getSDKVersion());
TUR.SDKVersionMajor = std::to_string(Ver.first);
TUR.SDKVersionMinor = std::to_string(Ver.second);
TUR.OptionMap = clang::dpct::DpctGlobalInfo::getCurrentOptMap();
YAMLOut << TUR;
YamlContentStream.flush();
clang::dpct::writeDataToFile(YamlFile.getCanonicalPath().str(), YamlContent);
return 0;
}
template <class T>
static int loadFromYaml(const clang::tooling::UnifiedPath &Input, T &Content) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
llvm::MemoryBuffer::getFile(Input.getCanonicalPath());
if (!Buffer) {
llvm::errs() << "error: failed to read " << Input.getCanonicalPath() << ": "
<< Buffer.getError().message() << "\n";
return -1;
}
llvm::yaml::Input YAMLIn(Buffer.get()->getBuffer());
YAMLIn >> Content;
if (YAMLIn.error()) {
Content = T();
return -1;
}
return 0;
}
int loadTUFromYaml(const clang::tooling::UnifiedPath &Input,
clang::tooling::TranslationUnitReplacements &TU) {
int Status = loadFromYaml(Input, TU);
if (Status)
return Status;
bool IsSrcFileChanged = false;
for (const auto &digest : TU.MainSourceFilesDigest) {
auto Hash = llvm::sys::fs::md5_contents(digest.MainSourceFile);
if (Hash && Hash->digest().c_str() != digest.Digest) {
llvm::errs() << "Warning: The file '" << digest.MainSourceFile
<< "' has been changed during incremental migration.\n";
IsSrcFileChanged = true;
}
}
if (IsSrcFileChanged && !DpctGlobalInfo::useReMigration()) {
// File doesn't appear to be a header change description. Ignore it.
TU = clang::tooling::TranslationUnitReplacements();
return -1;
}
return 0;
}
void loadGDCFromYaml(const clang::tooling::UnifiedPath &Input,
clang::dpct::GitDiffChanges &GDC) {
int status = loadFromYaml(Input, GDC);
if (status) {
llvm::errs() << "Failed to load git diff Changes from "
<< Input.getCanonicalPath() << "\n";
GDC = clang::dpct::GitDiffChanges();
}
}
void mergeAndUniqueReps(
Replacements &Replaces,
const std::vector<clang::tooling::Replacement> &PreRepls) {
bool DupFlag = false;
for (const auto &OldR : PreRepls) {
DupFlag = false;
for (const auto &CurrR : Replaces) {
if (CurrR.getFilePath() != OldR.getFilePath()) {
llvm::errs() << "Ignore " << OldR.getFilePath().str()
<< " for differnt path!\n";
return;
}
if ((CurrR.getFilePath() == OldR.getFilePath()) &&
(CurrR.getOffset() == OldR.getOffset()) &&
(CurrR.getLength() == OldR.getLength()) &&
(CurrR.getReplacementText() == OldR.getReplacementText())) {
DupFlag = true;
break;
}
}
if (DupFlag == false) {
if (auto Err = Replaces.add(OldR)) {
llvm::dbgs() << "Adding replacement when merging previous "
"replacement: Error occured!\n"
<< Err << "\n";
}
}
}
}
int mergeExternalReps(clang::tooling::UnifiedPath InRootSrcFilePath,
clang::tooling::UnifiedPath OutRootSrcFilePath,
Replacements &Replaces) {
clang::tooling::UnifiedPath YamlFile =
OutRootSrcFilePath.getCanonicalPath() + ".yaml";
auto PreTU =
clang::dpct::DpctGlobalInfo::getInstance()
.getReplInfoFromYAMLSavedInFileInfo(std::move(InRootSrcFilePath));
if (PreTU) {
llvm::errs() << YamlFile << " exist, try to merge it.\n";
mergeAndUniqueReps(Replaces, (*PreTU).Replacements);
}
llvm::errs() << "Saved new version of " << YamlFile << " file\n";
std::vector<clang::tooling::Replacement> Repls(Replaces.begin(),
Replaces.end());
// For header file, its hash content digest and HasCUDASytax field is not
// registed.
std::vector<clang::tooling::MainSourceFileInfo> MsfInfo(1);
std::map<clang::tooling::UnifiedPath,
std::vector<clang::tooling::CompilationInfo>>
CompileTargets;
save2Yaml(YamlFile, OutRootSrcFilePath, Repls, MsfInfo, CompileTargets);
return 0;
}
} // namespace dpct
} // namespace clang