comparison extern/vera/src/legacy_main.cpp @ 548:a7c0eb100760

CMake: import vera++ 1.3.0, closes #729
author David Demelier <markand@malikania.fr>
date Wed, 22 Nov 2017 20:10:03 +0100
parents
children
comparison
equal deleted inserted replaced
547:a95954e53589 548:a7c0eb100760
1 //
2 // Copyright (C) 2006-2007 Maciej Sobczak
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7
8 #include "config.h"
9 #include "structures/SourceFiles.h"
10 #include "plugins/Profiles.h"
11 #include "plugins/Rules.h"
12 #include "plugins/Exclusions.h"
13 #include "plugins/Transformations.h"
14 #include "plugins/Parameters.h"
15 #include "plugins/Reports.h"
16 #include "plugins/RootDirectory.h"
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include <vector>
21 #include <cstdlib>
22 #include <sys/stat.h>
23 #include "get_vera_root_default.h"
24
25 namespace // unnamed
26 {
27
28 // helper function that checks whether the given file name names the C or C++ source file
29 bool isSourceFileName(const Vera::Structures::SourceFiles::FileName & name)
30 {
31 const std::string suffixes[] =
32 { ".cpp", ".cxx", ".cc", ".c", ".C", ".h", ".hh", ".hpp", ".hxx", ".ipp" };
33
34 const int numOfSuffixes = sizeof(suffixes) / sizeof(std::string);
35 for (int i = 0; i != numOfSuffixes; ++i)
36 {
37 const std::string suf = suffixes[i];
38 const Vera::Structures::SourceFiles::FileName::size_type pos = name.rfind(suf);
39
40 if (pos != Vera::Structures::SourceFiles::FileName::npos &&
41 pos == name.size() - suf.size())
42 {
43 return true;
44 }
45 }
46
47 return false;
48 }
49
50 } // unnamed namespace
51
52
53 int legacy_main(int argc, char * argv[], bool silent = false)
54 {
55 int exitCodeOnFailure = EXIT_FAILURE;
56
57 try
58 {
59 Vera::Plugins::Profiles::ProfileName profile("default");
60
61 // the directory containing the profile and rule definitions
62 // by default it is (in this order, first has highest precedence):
63 // - VERA_ROOT (if VERA_ROOT is defined)
64 // - HOME/.vera++ (if HOME is defined)
65 // - current directory (if scripts and profile are present)
66 // - /usr/lib/vera++/ default debian directory
67
68 Vera::Plugins::RootDirectory::DirectoryName veraRoot(get_vera_root_default(argv[0]));
69
70 struct stat St;
71 bool isInCurrent = ( (stat( "./scripts", &St ) == 0 || stat( "./rules", &St ) == 0
72 || stat( "./transforms", &St ) == 0)
73 && stat( "./profiles", &St ) == 0 );
74
75 // scripts and profiles folders are inside current directory
76 if (isInCurrent)
77 {
78 // we can override /usr/lib/vera++
79 veraRoot = ".";
80 }
81 char * veraRootEnv = getenv("HOME");
82 if (veraRootEnv != NULL)
83 {
84 Vera::Plugins::RootDirectory::DirectoryName veraRootTmp(veraRootEnv);
85 veraRootTmp += "/.vera++";
86 bool isInHome = ( stat( veraRootTmp.c_str(), &St ) == 0 );
87 if (isInHome)
88 {
89 // We assume that if the user has a .vera++ folder in
90 // their home then we can override the current
91 // directory
92 // We don't want to override the current directory
93 // only because $HOME is defined.
94 veraRoot = veraRootEnv;
95 veraRoot += "/.vera++";
96 }
97 }
98 veraRootEnv = getenv("VERA_ROOT");
99 if (veraRootEnv != NULL)
100 {
101 veraRoot = veraRootEnv;
102 }
103
104 Vera::Plugins::RootDirectory::setRootDirectory(veraRoot);
105
106 // collect all source file names and interpret options
107
108 Vera::Plugins::Rules::RuleName singleRule;
109 Vera::Plugins::Transformations::TransformationName singleTransformation;
110
111 bool omitDuplicates = false;
112
113 int i = 1;
114 while (i != argc)
115 {
116 const std::string arg(argv[i]);
117
118 if (arg == "-help")
119 {
120 std::cout << "vera++ [options] [list-of-files]\n\n"
121 "This command line interface is deprecated and is planned to be removed.\n\n"
122 "Recognized options:\n\n"
123 "- (a single minus sign) indicates that the list of\n"
124 " source file names will be provided on the stdin.\n\n"
125 "-exclusions file read exclusions from file\n\n"
126 "-help print this message\n\n"
127 "-nofail do not fail even when finding rule violations\n\n"
128 "-nodup do not duplicate messages if a single rule is violated\n"
129 " many times in a single line of code\n\n"
130 "-profile name execute all rules from the given profile\n\n"
131 "-param name=value provide parameters to scripts (can be used many times)\n\n"
132 "-paramfile file read parameters from file\n\n"
133 "-rule name execute the given rule\n"
134 " (note: the .tcl extension is added automatically)\n\n"
135 "-showrules include rule name in each report\n\n"
136 "-vcformat report in Visual C++ format\n\n"
137 "-xmlreport produce report in the XML format\n\n"
138 "-transform name execute the given transformation\n\n"
139 "-version print version number\n\n";
140 exit(EXIT_SUCCESS);
141 }
142 else if (arg == "-version")
143 {
144 std::cout << VERA_VERSION << '\n';
145 exit(EXIT_SUCCESS);
146 }
147 else if (arg == "-nofail")
148 {
149 exitCodeOnFailure = EXIT_SUCCESS;
150 }
151 else if (arg == "-nodup")
152 {
153 omitDuplicates = true;
154 }
155 else if (arg == "-")
156 {
157 // list of source files is provided on stdin
158 Vera::Structures::SourceFiles::FileName name;
159 while (std::cin >> name)
160 {
161 Vera::Structures::SourceFiles::addFileName(name);
162 }
163 }
164 else if (arg == "-showrules")
165 {
166 Vera::Plugins::Reports::setShowRules(true);
167 }
168 else if (arg == "-xmlreport")
169 {
170 Vera::Plugins::Reports::setXMLReport(true);
171 }
172 else if (arg == "-vcformat")
173 {
174 Vera::Plugins::Reports::setVCFormat(true);
175 }
176 else if (arg == "-rule")
177 {
178 ++i;
179 if (argv[i] != NULL)
180 {
181 singleRule = argv[i];
182 }
183 else
184 {
185 if (silent == false)
186 {
187 std::cerr << "error: option -rule provided with no rule name\n";
188 }
189 return exitCodeOnFailure;
190 }
191 }
192 else if (arg == "-profile")
193 {
194 ++i;
195 if (argv[i] != NULL)
196 {
197 profile = argv[i];
198 }
199 else
200 {
201 if (silent == false)
202 {
203 std::cerr << "error: option -profile provided with no profile name\n";
204 }
205 return exitCodeOnFailure;
206 }
207 }
208 else if (arg == "-exclusions")
209 {
210 ++i;
211 if (argv[i] != NULL)
212 {
213 Vera::Plugins::Exclusions::ExclusionFileName file(argv[i]);
214 Vera::Plugins::Exclusions::setExclusions(file);
215 }
216 else
217 {
218 if (silent == false)
219 {
220 std::cerr << "error: option -exclusions provided without name of file\n";
221 }
222 return exitCodeOnFailure;
223 }
224 }
225 else if (arg == "-param")
226 {
227 ++i;
228 if (argv[i] != NULL)
229 {
230 Vera::Plugins::Parameters::ParamAssoc assoc(argv[i]);
231 Vera::Plugins::Parameters::set(assoc);
232 }
233 else
234 {
235 if (silent == false)
236 {
237 std::cerr << "error: option -param provided without name and value\n";
238 }
239 return exitCodeOnFailure;
240 }
241 }
242 else if (arg == "-paramfile")
243 {
244 ++i;
245 if (argv[i] != NULL)
246 {
247 Vera::Plugins::Parameters::FileName file(argv[i]);
248 Vera::Plugins::Parameters::readFromFile(file);
249 }
250 else
251 {
252 if (silent == false)
253 {
254 std::cerr << "error: option -paramfile provided without name of file\n";
255 }
256 return exitCodeOnFailure;
257 }
258 }
259 else if (arg == "-transform")
260 {
261 ++i;
262 if (argv[i] != NULL)
263 {
264 singleTransformation = argv[i];
265 }
266 else
267 {
268 if (silent == false)
269 {
270 std::cerr
271 << "error: option -transform provided without name of transformation\n";
272 }
273 return exitCodeOnFailure;
274 }
275 }
276 else if (isSourceFileName(arg))
277 {
278 Vera::Structures::SourceFiles::addFileName(arg);
279 }
280 else
281 {
282 // the option was not recognized as a name of the source file
283 // or a vera-specific option
284 if (silent == false)
285 {
286 std::cerr << "error: option " << arg << " not recognized\n";
287 }
288 return EXIT_FAILURE;
289 }
290
291 ++i;
292 }
293
294 if (Vera::Structures::SourceFiles::empty())
295 {
296 if (silent == false)
297 {
298 std::cerr << "vera++: no input files\n";
299 }
300 return exitCodeOnFailure;
301 }
302
303 if (singleTransformation.empty() == false)
304 {
305 if (singleRule.empty() == false || profile != "default")
306 {
307 if (silent == false)
308 {
309 std::cerr << "error: "
310 "transformation cannot be specified together with rules or profiles\n";
311 }
312 return exitCodeOnFailure;
313 }
314
315 Vera::Plugins::Transformations::executeTransformation(singleTransformation);
316 }
317 else if (singleRule.empty() == false)
318 {
319 // single rule requested
320 Vera::Plugins::Rules::executeRule(singleRule);
321 }
322 else
323 {
324 Vera::Plugins::Profiles::executeProfile(profile);
325 }
326
327 Vera::Plugins::Reports::dumpAll(std::cerr, omitDuplicates);
328 }
329 catch (const std::exception & e)
330 {
331 std::cerr << "error: " << e.what() << '\n';
332 exit(exitCodeOnFailure);
333 }
334 return 0;
335 }