Debian Codegraph @

clang_delta/ClangDelta.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2016 The University of Utah
4 // All rights reserved.
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See the file COPYING for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #if HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 #include <string>
16 #include <sstream>
17 #include <cstdlib>
18
19 #include "llvm/Support/raw_ostream.h"
20 #include "TransformationManager.h"
21 #include "git_version.h"
22
23 static TransformationManager *TransMgr;
24 static int ErrorCode = -1;
25
26 static void PrintVersion()
27 {
28 llvm::outs() << "clang_delta " << PACKAGE_VERSION << "\n";
29 llvm::outs() << "Git version: " << git_version << "\n";
30 // XXX print copyright, contact info, etc.?
31 }
32
33 static void PrintHelpMessage()
34 {
35 PrintVersion();
36 llvm::outs() << "\n";
37 llvm::outs() << "Usage: \n";
38 llvm::outs() << " clang_delta ";
39 llvm::outs() << "--transformation=<name> ";
40 llvm::outs() << "--counter=<number> ";
41 llvm::outs() << "--output=<output_filename> ";
42 llvm::outs() << "<source_filename>\n\n";
43
44 llvm::outs() << "clang_delta options:\n";
45
46 llvm::outs() << " --help: ";
47 llvm::outs() << "print this message\n";
48 llvm::outs() << " --version: ";
49 llvm::outs() << "print the program version number\n";
50
51 llvm::outs() << " --verbose-transformations: ";
52 llvm::outs() << "print verbose description messages for all ";
53 llvm::outs() << "transformations\n";
54
55 llvm::outs() << " --transformation=<name>: ";
56 llvm::outs() << "specify the transformation\n";
57
58 llvm::outs() << " --transformations: ";
59 llvm::outs() << "print the names of all available transformations\n";
60
61 llvm::outs() << " --query-instances=<name>: ";
62 llvm::outs() << "query available transformation instances for a given ";
63 llvm::outs() << "transformation\n";
64
65 llvm::outs() << " --counter=<number>: ";
66 llvm::outs() << "specify the instance of the transformation to perform\n";
67
68 llvm::outs() << " --to-counter=<number>: ";
69 llvm::outs() << "specify the ending instance of the transformation to ";
70 llvm::outs() << "perform (when this option is given, clang_delta will ";
71 llvm::outs() << "rewrite multiple instances [counter,to-counter] ";
72 llvm::outs() << "simultaneously. Note that currently only ";
73 llvm::outs() << "replace-function-def-with-decl supports this feature.)\n";
74
75 llvm::outs() << " --replacement=<string>: ";
76 llvm::outs() << "instead of performing normal rewriting, the candidate ";
77 llvm::outs() << "pointed by the counter will be replaced by the passed ";
78 llvm::outs() << "\"string\". Currently, this option works only with ";
79 llvm::outs() << "transformation expression-detector.\n";
80
81 llvm::outs() << " --check-reference=<value>: ";
82 llvm::outs() << "insert code to check if the candidate designated by the ";
83 llvm::outs() << "counter equals to the reference value or not. Currently, ";
84 llvm::outs() << "this option works only with transformation ";
85 llvm::outs() << "expression-detector.\n";
86
87 llvm::outs() << " --output=<filename>: ";
88 llvm::outs() << "specify where to output the transformed source code ";
89 llvm::outs() << "(default: stdout)\n";
90 llvm::outs() << "\n";
91 }
92
93 static void DieOnBadCmdArg(const std::string &ArgStr)
94 {
95 llvm::outs() << "Error: Bad command line option `" << ArgStr << "`\n";
96 llvm::outs() << "\n";
97 PrintHelpMessage();
98 TransformationManager::Finalize();
99 exit(-1);
100 }
101
102 static void Die(const std::string &Message)
103 {
104 llvm::outs() << "Error: " << Message << "\n";
105 TransformationManager::Finalize();
106 exit(ErrorCode);
107 }
108
109 static void HandleOneArgValue(const std::string &ArgValueStr, size_t SepPos)
110 {
111 if ((SepPos < 1) || (SepPos >= ArgValueStr.length())) {
112 DieOnBadCmdArg("--" + ArgValueStr);
113 }
114
115 std::string ArgName, ArgValue;
116
117 ArgName = ArgValueStr.substr(0, SepPos);
118 ArgValue = ArgValueStr.substr(SepPos+1);
119
120 if (!ArgName.compare("transformation")) {
121 if (TransMgr->setTransformation(ArgValue)) {
122 Die("Invalid transformation[" + ArgValue + "]");
123 }
124 }
125 else if (!ArgName.compare("query-instances")) {
126 if (TransMgr->setTransformation(ArgValue)) {
127 Die("Invalid transformation[" + ArgValue + "]");
128 }
129 TransMgr->setQueryInstanceFlag(true);
130 TransMgr->setTransformationCounter(1);
131 }
132 else if (!ArgName.compare("counter")) {
133 int Val;
134 std::stringstream TmpSS(ArgValue);
135
136 if (!(TmpSS >> Val)) {
137 ErrorCode = TransformationManager::ErrorInvalidCounter;
138 Die("Invalid counter[" + ArgValueStr + "]");
139 }
140
141 TransMgr->setTransformationCounter(Val);
142 }
143 else if (!ArgName.compare("to-counter")) {
144 int Val;
145 std::stringstream TmpSS(ArgValue);
146
147 if (!(TmpSS >> Val)) {
148 ErrorCode = TransformationManager::ErrorInvalidCounter;
149 Die("Invalid to-counter[" + ArgValueStr + "]");
150 }
151
152 TransMgr->setToCounter(Val);
153 }
154 else if (!ArgName.compare("output")) {
155 TransMgr->setOutputFileName(ArgValue);
156 }
157 else if (!ArgName.compare("replacement")) {
158 TransMgr->setReplacement(ArgValue);
159 }
160 else if (!ArgName.compare("check-reference")) {
161 TransMgr->setReferenceValue(ArgValue);
162 }
163 else {
164 DieOnBadCmdArg("--" + ArgValueStr);
165 }
166 }
167
168 static void HandleOneNoneValueArg(const std::string &ArgStr)
169 {
170 if (!ArgStr.compare("help")) {
171 PrintHelpMessage();
172 exit(0);
173 }
174 else if (!ArgStr.compare("version")) {
175 PrintVersion();
176 exit(0);
177 }
178 else if (!ArgStr.compare("transformations")) {
179 TransMgr->printTransformationNames();
180 exit(0);
181 }
182 else if (!ArgStr.compare("verbose-transformations")) {
183 TransMgr->printTransformations();
184 exit(0);
185 }
186 else {
187 DieOnBadCmdArg(ArgStr);
188 }
189 }
190
191 static void HandleOneArg(const char *Arg)
192 {
193 std::string ArgStr(Arg);
194
195 if (!ArgStr.compare(0, 2, "--")) {
196 std::string SubArgStr = ArgStr.substr(2);
197 if (!SubArgStr.length())
198 DieOnBadCmdArg(ArgStr);
199
200 size_t found;
201 found = SubArgStr.find('=');
202 if (found != std::string::npos) {
203 HandleOneArgValue(SubArgStr, found);
204 }
205 else {
206 HandleOneNoneValueArg(SubArgStr);
207 }
208 }
209 else {
210 TransMgr->setSrcFileName(ArgStr);
211 }
212 }
213
214 int main(int argc, char **argv)
215 {
216 TransMgr = TransformationManager::GetInstance();
217 for (int i = 1; i < argc; i++) {
218 HandleOneArg(argv[i]);
219 }
220
221 std::string ErrorMsg;
222 if (!TransMgr->verify(ErrorMsg, ErrorCode))
223 Die(ErrorMsg);
224
225 if (!TransMgr->initializeCompilerInstance(ErrorMsg))
226 Die(ErrorMsg);
227
228 if (!TransMgr->doTransformation(ErrorMsg, ErrorCode)) {
229 // fail to do transformation
230 Die(ErrorMsg);
231 }
232
233 if (TransMgr->getQueryInstanceFlag())
234 TransMgr->outputNumTransformationInstances();
235
236 TransformationManager::Finalize();
237 return 0;
238 }
239