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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
//===-- cpp11-migrate/Cpp11Migrate.cpp - Main file C++11 migration tool ---===//
//
// 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 implements the C++11 feature migration tool main function
/// and transformation framework.
///
/// See user documentation for usage instructions.
///
//===----------------------------------------------------------------------===//
#include "Core/Transforms.h"
#include "Core/Transform.h"
#include "Core/PerfSupport.h"
#include "LoopConvert/LoopConvert.h"
#include "UseNullptr/UseNullptr.h"
#include "UseAuto/UseAuto.h"
#include "AddOverride/AddOverride.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/Signals.h"
namespace cl = llvm::cl;
using namespace clang::tooling;
TransformOptions GlobalOptions;
static cl::opt<RiskLevel, /*ExternalStorage=*/true> MaxRiskLevel(
"risk", cl::desc("Select a maximum risk level:"),
cl::values(clEnumValN(RL_Safe, "safe", "Only safe transformations"),
clEnumValN(RL_Reasonable, "reasonable",
"Enable transformations that might change "
"semantics (default)"),
clEnumValN(RL_Risky, "risky",
"Enable transformations that are likely to "
"change semantics"),
clEnumValEnd),
cl::location(GlobalOptions.MaxRiskLevel),
cl::init(RL_Reasonable));
static cl::opt<bool> FinalSyntaxCheck(
"final-syntax-check",
cl::desc("Check for correct syntax after applying transformations"),
cl::init(false));
static cl::opt<bool>
SummaryMode("summary", cl::desc("Print transform summary"),
cl::init(false));
const char NoTiming[] = "no_timing";
static cl::opt<std::string> TimingDirectoryName(
"perf", cl::desc("Capture performance data and output to specified "
"directory. Default: ./migrate_perf"),
cl::init(NoTiming), cl::ValueOptional, cl::value_desc("directory name"));
// TODO: Remove cl::Hidden when functionality for acknowledging include/exclude
// options are implemented in the tool.
static cl::opt<std::string>
IncludePaths("include", cl::Hidden,
cl::desc("Comma seperated list of paths to consider to be "
"transformed"));
static cl::opt<std::string>
ExcludePaths("exclude", cl::Hidden,
cl::desc("Comma seperated list of paths that can not "
"be transformed"));
static cl::opt<std::string>
IncludeFromFile("include-from", cl::Hidden, cl::value_desc("filename"),
cl::desc("File containing a list of paths to consider to "
"be transformed"));
static cl::opt<std::string>
ExcludeFromFile("exclude-from", cl::Hidden, cl::value_desc("filename"),
cl::desc("File containing a list of paths that can not be "
"transforms"));
// Header modifications will probably be always on eventually. For now, they
// need to be explicitly enabled.
static cl::opt<bool, /*ExternalStorage=*/true> EnableHeaderModifications(
"headers",
cl::Hidden, // Experimental feature for now.
cl::desc("Enable modifications to headers"),
cl::location(GlobalOptions.EnableHeaderModifications),
cl::init(false));
class EndSyntaxArgumentsAdjuster : public ArgumentsAdjuster {
CommandLineArguments Adjust(const CommandLineArguments &Args) {
CommandLineArguments AdjustedArgs = Args;
AdjustedArgs.push_back("-fsyntax-only");
AdjustedArgs.push_back("-std=c++11");
return AdjustedArgs;
}
};
int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal();
Transforms TransformManager;
TransformManager.registerTransform(
"loop-convert", "Make use of range-based for loops where possible",
&ConstructTransform<LoopConvertTransform>);
TransformManager.registerTransform(
"use-nullptr", "Make use of nullptr keyword where possible",
&ConstructTransform<UseNullptrTransform>);
TransformManager.registerTransform(
"use-auto", "Use of 'auto' type specifier",
&ConstructTransform<UseAutoTransform>);
TransformManager.registerTransform(
"add-override", "Make use of override specifier where possible",
&ConstructTransform<AddOverrideTransform>);
// Add more transform options here.
// This causes options to be parsed.
CommonOptionsParser OptionsParser(argc, argv);
// Since ExecutionTimeDirectoryName could be an empty string we compare
// against the default value when the command line option is not specified.
GlobalOptions.EnableTiming = (TimingDirectoryName != NoTiming);
// Populate the ModifiableHeaders structure if header modifications are
// enabled.
if (GlobalOptions.EnableHeaderModifications) {
GlobalOptions.ModifiableHeaders
.readListFromString(IncludePaths, ExcludePaths);
GlobalOptions.ModifiableHeaders
.readListFromFile(IncludeFromFile, ExcludeFromFile);
}
TransformManager.createSelectedTransforms(GlobalOptions);
if (TransformManager.begin() == TransformManager.end()) {
llvm::errs() << "No selected transforms\n";
return 1;
}
FileContentsByPath FileStates1, FileStates2,
*InputFileStates = &FileStates1, *OutputFileStates = &FileStates2;
SourcePerfData PerfData;
// Apply transforms.
for (Transforms::const_iterator I = TransformManager.begin(),
E = TransformManager.end();
I != E; ++I) {
if ((*I)->apply(*InputFileStates, OptionsParser.getCompilations(),
OptionsParser.getSourcePathList(), *OutputFileStates) !=
0) {
// FIXME: Improve ClangTool to not abort if just one file fails.
return 1;
}
if (GlobalOptions.EnableTiming)
collectSourcePerfData(**I, PerfData);
if (SummaryMode) {
llvm::outs() << "Transform: " << (*I)->getName()
<< " - Accepted: "
<< (*I)->getAcceptedChanges();
if ((*I)->getChangesNotMade()) {
llvm::outs() << " - Rejected: "
<< (*I)->getRejectedChanges()
<< " - Deferred: "
<< (*I)->getDeferredChanges();
}
llvm::outs() << "\n";
}
std::swap(InputFileStates, OutputFileStates);
OutputFileStates->clear();
}
// Final state of files is pointed at by InputFileStates.
if (FinalSyntaxCheck) {
ClangTool EndSyntaxTool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
// Add c++11 support to clang.
EndSyntaxTool.setArgumentsAdjuster(new EndSyntaxArgumentsAdjuster);
for (FileContentsByPath::const_iterator I = InputFileStates->begin(),
E = InputFileStates->end();
I != E; ++I) {
EndSyntaxTool.mapVirtualFile(I->first, I->second);
}
if (EndSyntaxTool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>())
!= 0) {
return 1;
}
}
// Write results to file.
for (FileContentsByPath::const_iterator I = InputFileStates->begin(),
E = InputFileStates->end();
I != E; ++I) {
std::string ErrorInfo;
llvm::raw_fd_ostream FileStream(I->first.c_str(), ErrorInfo,
llvm::raw_fd_ostream::F_Binary);
FileStream << I->second;
}
// Report execution times.
if (GlobalOptions.EnableTiming && !PerfData.empty()) {
std::string DirectoryName = TimingDirectoryName;
// Use default directory name.
if (DirectoryName.empty())
DirectoryName = "./migrate_perf";
writePerfDataJSON(DirectoryName, PerfData);
}
return 0;
}
|