]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/wave/samples/list_includes/list_includes.cpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / libs / wave / samples / list_includes / list_includes.cpp
1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3
4 Sample: List include dependencies of a given source file
5
6 The 'list_includes' sample shows a simple way, how to use the Wave C++
7 preprocessor library to extract a list of included files from a given
8 source file.
9 To get a hint which commandline options are supported, call it with the
10 --help option.
11
12 http://www.boost.org/
13
14 Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
15 Software License, Version 1.0. (See accompanying file
16 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
17 =============================================================================*/
18
19 #include "list_includes.hpp" // config data
20
21 ///////////////////////////////////////////////////////////////////////////////
22 // include required boost libraries
23 #include <boost/assert.hpp>
24 #include <boost/program_options.hpp>
25
26 ///////////////////////////////////////////////////////////////////////////////
27 // Include Wave itself
28 #include <boost/wave.hpp>
29
30 ///////////////////////////////////////////////////////////////////////////////
31 // Include the lexer stuff
32 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // standard token type
33 #include "lexertl_iterator.hpp" // lexertl based lexer
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // Include the default context trace policies
37 #include <boost/wave/preprocessing_hooks.hpp>
38
39 #include <iostream>
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // include lexer specifics, import lexer names
43 #if BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION == 0
44 #include <boost/wave/cpplexer/re2clex/cpp_re2c_lexer.hpp>
45 #endif
46
47 ///////////////////////////////////////////////////////////////////////////////
48 // import required names
49 using namespace boost::spirit::classic;
50
51 using std::string;
52 using std::vector;
53 using std::set;
54 using std::cout;
55 using std::cerr;
56 using std::endl;
57 using std::ifstream;
58 using std::ostream;
59 using std::istreambuf_iterator;
60
61 namespace po = boost::program_options;
62
63 ///////////////////////////////////////////////////////////////////////////////
64 namespace cmd_line_util {
65
66 // predicate to extract all positional arguments from the command line
67 struct is_argument {
68 bool operator()(po::option const &opt)
69 {
70 return (opt.position_key == -1) ? true : false;
71 }
72 };
73
74 ///////////////////////////////////////////////////////////////////////////////
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
78 // print the current version
79
80 int print_version()
81 {
82 // get time of last compilation of this file
83 boost::wave::util::time_conversion_helper compilation_time(__DATE__ " " __TIME__);
84
85 // calculate the number of days since Jan 29 2003
86 // (the day the list_includes project was started)
87 std::tm first_day;
88
89 std::memset (&first_day, 0, sizeof(std::tm));
90 first_day.tm_mon = 0; // Jan
91 first_day.tm_mday = 29; // 29
92 first_day.tm_year = 103; // 2003
93
94 long seconds = long(std::difftime(compilation_time.get_time(),
95 std::mktime(&first_day)));
96
97 cout
98 << LIST_INCLUDES_VERSION_MAJOR << '.'
99 << LIST_INCLUDES_VERSION_MINOR << '.'
100 << LIST_INCLUDES_VERSION_SUBMINOR << '.'
101 << seconds/(3600*24); // get number of days from seconds
102 return 1; // exit app
103 }
104
105 ///////////////////////////////////////////////////////////////////////////////
106 // policy class
107 struct trace_include_files
108 : public boost::wave::context_policies::default_preprocessing_hooks
109 {
110 trace_include_files(set<string> &files_)
111 : files(files_), include_depth(0)
112 {}
113
114 template <typename ContextT>
115 void
116 opened_include_file(ContextT const& ctx, std::string const& relname,
117 std::string const& filename, bool is_system_include)
118 {
119 set<string>::iterator it = files.find(filename);
120 if (it == files.end()) {
121 // print indented filename
122 for (std::size_t i = 0; i < include_depth; ++i)
123 cout << " ";
124 cout << filename << endl;
125
126 files.insert(filename);
127 }
128 ++include_depth;
129 }
130
131 template <typename ContextT>
132 void returning_from_include_file(ContextT const& ctx)
133 {
134 --include_depth;
135 }
136
137 set<string> &files;
138 std::size_t include_depth;
139 };
140
141 ///////////////////////////////////////////////////////////////////////////////
142 //
143 int
144 do_actual_work(vector<string> const &arguments, po::variables_map const &vm)
145 {
146 // current file position is saved for exception handling
147 boost::wave::util::file_position_type current_position;
148
149 try {
150 // list the included files for all arguments given
151 vector<string>::const_iterator lastfile = arguments.end();
152 for (vector<string>::const_iterator file_it = arguments.begin();
153 file_it != lastfile; ++file_it)
154 {
155 ifstream instream((*file_it).c_str());
156 string instring;
157
158 if (!instream.is_open()) {
159 cerr << "Could not open input file: " << *file_it << endl;
160 continue;
161 }
162 instream.unsetf(std::ios::skipws);
163 instring = string(istreambuf_iterator<char>(instream.rdbuf()),
164 istreambuf_iterator<char>());
165
166 // The template boost::wave::cpplexer::lex_token<> is the token type to be
167 // used by the Wave library.
168 typedef boost::wave::cpplexer::lexertl::lex_iterator<
169 boost::wave::cpplexer::lex_token<> >
170 lex_iterator_type;
171 typedef boost::wave::context<
172 std::string::iterator, lex_iterator_type,
173 boost::wave::iteration_context_policies::load_file_to_string,
174 trace_include_files
175 > context_type;
176
177 set<string> files;
178 trace_include_files trace(files);
179
180 // The preprocessor iterator shouldn't be constructed directly. It is
181 // to be generated through a wave::context<> object. This wave:context<>
182 // object is additionally to be used to initialize and define different
183 // parameters of the actual preprocessing.
184 // The preprocessing of the input stream is done on the fly behind the
185 // scenes during iteration over the context_type::iterator_type stream.
186 context_type ctx (instring.begin(), instring.end(), (*file_it).c_str(), trace);
187
188 // add include directories to the include path
189 if (vm.count("include")) {
190 vector<string> const &paths =
191 vm["include"].as<vector<string> >();
192 vector<string>::const_iterator end = paths.end();
193 for (vector<string>::const_iterator cit = paths.begin();
194 cit != end; ++cit)
195 {
196 ctx.add_include_path((*cit).c_str());
197 }
198 }
199
200 // add system include directories to the include path
201 if (vm.count("sysinclude")) {
202 vector<string> const &syspaths =
203 vm["sysinclude"].as<vector<string> >();
204 vector<string>::const_iterator end = syspaths.end();
205 for (vector<string>::const_iterator cit = syspaths.begin();
206 cit != end; ++cit)
207 {
208 ctx.add_sysinclude_path((*cit).c_str());
209 }
210 }
211
212 // analyze the actual file
213 context_type::iterator_type first = ctx.begin();
214 context_type::iterator_type last = ctx.end();
215
216 cout << "Printing dependency information for: "
217 << *file_it << endl;
218
219 while (first != last) {
220 current_position = (*first).get_position();
221 ++first;
222 }
223
224 // prepend endl before next file
225 cout << endl;
226 }
227 }
228 catch (boost::wave::cpp_exception &e) {
229 // some preprocessing error
230 cerr
231 << e.file_name() << "(" << e.line_no() << "): "
232 << e.description() << endl;
233 return 2;
234 }
235 catch (std::exception &e) {
236 // use last recognized token to retrieve the error position
237 cerr
238 << current_position.get_file()
239 << "(" << current_position.get_line() << "): "
240 << "exception caught: " << e.what()
241 << endl;
242 return 3;
243 }
244 catch (...) {
245 // use last recognized token to retrieve the error position
246 cerr
247 << current_position.get_file()
248 << "(" << current_position.get_line() << "): "
249 << "unexpected exception caught." << endl;
250 return 4;
251 }
252 return 0;
253 }
254
255 ///////////////////////////////////////////////////////////////////////////////
256 // here we go!
257 int
258 main (int argc, char *argv[])
259 {
260 try {
261 // analyze the command line options and arguments
262 vector<string> syspathes;
263 po::options_description desc("Usage: list_includes [options] file ...");
264
265 desc.add_options()
266 ("help,h", "print out program usage (this message)")
267 ("version,v", "print the version number")
268 ("include,I", po::value<vector<string> >(),
269 "specify additional include directory")
270 ("sysinclude,S", po::value<vector<string> >(),
271 "specify additional system include directory")
272 ;
273
274 using namespace boost::program_options::command_line_style;
275
276 po::parsed_options opts = po::parse_command_line(argc, argv, desc, unix_style);
277 po::variables_map vm;
278
279 po::store(opts, vm);
280 po::notify(vm);
281
282 if (vm.count("help")) {
283 cout << desc << endl;
284 return 1;
285 }
286
287 if (vm.count("version")) {
288 return print_version();
289 }
290
291 // extract the arguments from the parsed command line
292 vector<po::option> arguments;
293
294 std::remove_copy_if(opts.options.begin(), opts.options.end(),
295 inserter(arguments, arguments.end()), cmd_line_util::is_argument());
296
297 // if there is no input file given, then exit
298 if (0 == arguments.size() || 0 == arguments[0].value.size()) {
299 cerr << "list_includes: No input file given. "
300 << "Use --help to get a hint." << endl;
301 return 5;
302 }
303
304 // iterate over all given input files
305 return do_actual_work(arguments[0].value , vm);
306 }
307 catch (std::exception &e) {
308 cout << "list_includes: exception caught: " << e.what() << endl;
309 return 6;
310 }
311 catch (...) {
312 cerr << "list_includes: unexpected exception caught." << endl;
313 return 7;
314 }
315 }
316