1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
4 Sample: List include dependencies of a given source file
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
9 To get a hint which commandline options are supported, call it with the
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 =============================================================================*/
19 #include "list_includes.hpp" // config data
21 ///////////////////////////////////////////////////////////////////////////////
22 // include required boost libraries
23 #include <boost/assert.hpp>
24 #include <boost/program_options.hpp>
26 ///////////////////////////////////////////////////////////////////////////////
27 // Include Wave itself
28 #include <boost/wave.hpp>
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
35 ///////////////////////////////////////////////////////////////////////////////
36 // Include the default context trace policies
37 #include <boost/wave/preprocessing_hooks.hpp>
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>
47 ///////////////////////////////////////////////////////////////////////////////
48 // import required names
49 using namespace boost::spirit::classic
;
59 using std::istreambuf_iterator
;
61 namespace po
= boost::program_options
;
63 ///////////////////////////////////////////////////////////////////////////////
64 namespace cmd_line_util
{
66 // predicate to extract all positional arguments from the command line
68 bool operator()(po::option
const &opt
)
70 return (opt
.position_key
== -1) ? true : false;
74 ///////////////////////////////////////////////////////////////////////////////
77 ///////////////////////////////////////////////////////////////////////////////
78 // print the current version
82 // get time of last compilation of this file
83 boost::wave::util::time_conversion_helper
compilation_time(__DATE__
" " __TIME__
);
85 // calculate the number of days since Jan 29 2003
86 // (the day the list_includes project was started)
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
94 long seconds
= long(std::difftime(compilation_time
.get_time(),
95 std::mktime(&first_day
)));
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
105 ///////////////////////////////////////////////////////////////////////////////
107 struct trace_include_files
108 : public boost::wave::context_policies::default_preprocessing_hooks
110 trace_include_files(set
<string
> &files_
)
111 : files(files_
), include_depth(0)
114 template <typename ContextT
>
116 opened_include_file(ContextT
const& ctx
, std::string
const& relname
,
117 std::string
const& filename
, bool is_system_include
)
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
)
124 cout
<< filename
<< endl
;
126 files
.insert(filename
);
131 template <typename ContextT
>
132 void returning_from_include_file(ContextT
const& ctx
)
138 std::size_t include_depth
;
141 ///////////////////////////////////////////////////////////////////////////////
144 do_actual_work(vector
<string
> const &arguments
, po::variables_map
const &vm
)
146 // current file position is saved for exception handling
147 boost::wave::util::file_position_type current_position
;
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
)
155 ifstream
instream((*file_it
).c_str());
158 if (!instream
.is_open()) {
159 cerr
<< "Could not open input file: " << *file_it
<< endl
;
162 instream
.unsetf(std::ios::skipws
);
163 instring
= string(istreambuf_iterator
<char>(instream
.rdbuf()),
164 istreambuf_iterator
<char>());
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
<> >
171 typedef boost::wave::context
<
172 std::string::iterator
, lex_iterator_type
,
173 boost::wave::iteration_context_policies::load_file_to_string
,
178 trace_include_files
trace(files
);
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
);
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();
196 ctx
.add_include_path((*cit
).c_str());
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();
208 ctx
.add_sysinclude_path((*cit
).c_str());
212 // analyze the actual file
213 context_type::iterator_type first
= ctx
.begin();
214 context_type::iterator_type last
= ctx
.end();
216 cout
<< "Printing dependency information for: "
219 while (first
!= last
) {
220 current_position
= (*first
).get_position();
224 // prepend endl before next file
228 catch (boost::wave::cpp_exception
&e
) {
229 // some preprocessing error
231 << e
.file_name() << "(" << e
.line_no() << "): "
232 << e
.description() << endl
;
235 catch (std::exception
&e
) {
236 // use last recognized token to retrieve the error position
238 << current_position
.get_file()
239 << "(" << current_position
.get_line() << "): "
240 << "exception caught: " << e
.what()
245 // use last recognized token to retrieve the error position
247 << current_position
.get_file()
248 << "(" << current_position
.get_line() << "): "
249 << "unexpected exception caught." << endl
;
255 ///////////////////////////////////////////////////////////////////////////////
258 main (int argc
, char *argv
[])
261 // analyze the command line options and arguments
262 vector
<string
> syspathes
;
263 po::options_description
desc("Usage: list_includes [options] file ...");
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")
274 using namespace boost::program_options::command_line_style
;
276 po::parsed_options opts
= po::parse_command_line(argc
, argv
, desc
, unix_style
);
277 po::variables_map vm
;
282 if (vm
.count("help")) {
283 cout
<< desc
<< endl
;
287 if (vm
.count("version")) {
288 return print_version();
291 // extract the arguments from the parsed command line
292 vector
<po::option
> arguments
;
294 std::remove_copy_if(opts
.options
.begin(), opts
.options
.end(),
295 inserter(arguments
, arguments
.end()), cmd_line_util::is_argument());
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
;
304 // iterate over all given input files
305 return do_actual_work(arguments
[0].value
, vm
);
307 catch (std::exception
&e
) {
308 cout
<< "list_includes: exception caught: " << e
.what() << endl
;
312 cerr
<< "list_includes: unexpected exception caught." << endl
;