1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3 Sample demonstrating the usage of advanced preprocessor hooks.
4
5 http://www.boost.org/
6
7 Copyright (c) 2001-2010 Hartmut Kaiser.
8 Copyright (c) 2020 Jeff Trull. Distributed under the Boost
9 Software License, Version 1.0. (See accompanying file
10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 =============================================================================*/
12
13 #include "check_macro_naming.hpp"
14 #include "libs/filesystem/include/boost/filesystem/file_status.hpp"
15
16 ///////////////////////////////////////////////////////////////////////////////
17 // Utilities from the rest of Boost
18 #include <boost/program_options.hpp>
19 #include <boost/filesystem.hpp>
20 #include <boost/regex.hpp>
21 #include <boost/bind/bind.hpp>
22
23 ///////////////////////////////////////////////////////////////////////////////
24 // Wave itself
25 #include <boost/wave.hpp>
26
27 ///////////////////////////////////////////////////////////////////////////////
28 // Include the lexer stuff
29 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
30 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
31
32 #include <iostream>
33 #include <string>
34 #include <set>
35 #include <algorithm>
36
process_header(std::string const & filename,boost::regex const & re)37 void process_header(std::string const & filename,
38 boost::regex const & re) {
39 using namespace boost::wave;
40
41 // data to collect
42 std::string include_guard; // macro that protects this file, if any
43 std::set<std::string> bad_macros; // misnamed macros in this file
44
45 try {
46 // create a fake main program in memory to include our header from
47 std::string fake_main("#include \"");
48 fake_main += filename;
49 fake_main += "\"\n";
50
51 typedef cpplexer::lex_token<> token_type;
52 typedef cpplexer::lex_iterator<token_type> lex_iterator_type;
53 typedef context<std::string::iterator, lex_iterator_type,
54 iteration_context_policies::load_file_to_string,
55 macroname_preprocessing_hooks>
56 context_type;
57
58 context_type ctx (fake_main.begin(), fake_main.end(), "in-memory.cpp",
59 macroname_preprocessing_hooks(re, bad_macros,
60 include_guard));
61
62 // consume input, letting the hooks do the work
63 context_type::iterator_type last = ctx.end();
64 for (context_type::iterator_type it = ctx.begin(); it != last; ++it);
65
66 std::set<std::string>::const_iterator beg = bad_macros.begin();
67 std::set<std::string>::const_iterator end = bad_macros.end();
68 if (beg != end) {
69 // we have some macros that don't follow convention
70 for (std::set<std::string>::const_iterator it = beg;
71 it != end; ++it) {
72 std::cout << *it << " " << filename;
73 if (*it == include_guard)
74 std::cout << " (guard)\n";
75 else
76 std::cout << "\n";
77 }
78 }
79 } catch (preprocess_exception const& e) {
80 // some preprocessing error
81 std::cerr
82 << e.file_name() << "(" << e.line_no() << "): "
83 << e.description() << std::endl;
84 } catch (cpplexer::lexing_exception const& e) {
85 std::cerr
86 << e.file_name() << "(" << e.line_no() << "): "
87 << e.description() << std::endl;
88 }
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92 // Main entry point
93 //
94 // This sample shows how to check macros defined in header files to ensure
95 // they conform to a standard naming convention. It uses hooks for
96 // macro definition and include guard events to collect and report results.
97
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100
101 // argument processing
102 namespace po = boost::program_options;
103 po::options_description visible("Usage: check_macro_naming [options] directory");
104 // named arguments
105 visible.add_options()
106 ("help,h", "print out options")
107 ("match", po::value<std::string>()->default_value("^BOOST_.*"),
108 "pattern defined macros must match")
109 ("exclude", po::value<std::vector<std::string> >(), "subdirectory to skip");
110
111 // positional arguments
112 po::positional_options_description p;
113 p.add("dirname", 1);
114 // this positional option should not be displayed as a named, so we separate it:
115 po::options_description hidden;
116 hidden.add_options()("dirname", po::value<std::string>());
117
118 // combine visible and hidden for parsing:
119 po::options_description desc;
120 desc.add(visible).add(hidden);
121
122 po::variables_map vm;
123 po::store(po::command_line_parser(argc, argv)
124 .options(desc)
125 .positional(p)
126 .run(),
127 vm);
128 po::notify(vm);
129
130 if (vm.count("help") || (vm.count("dirname") == 0)) {
131 std::cerr << visible << "\n";
132 std::cerr << "recursively traverse directory, reporting macro definitions ";
133 std::cerr << "that do not conform to the supplied pattern\n";
134 return 1;
135 }
136
137 // get named parameters
138 boost::regex macro_regex(vm["match"].as<std::string>());
139 std::vector<std::string> exclude_dirnames;
140 if (vm.count("exclude"))
141 exclude_dirnames = vm["exclude"].as<std::vector<std::string> >();
142
143 // get our single positional parameter - the directory to process
144 std::string dirname = vm["dirname"].as<std::string>();
145
146 // directory traversal logic
147 static const boost::regex header_regex(".*\\.(hh|hpp|h)$");
148 namespace fs = boost::filesystem;
149 std::vector<fs::path> exclude_dirs(exclude_dirnames.begin(),
150 exclude_dirnames.end());
151 // canonicalize exclude directories for comparison vs.
152 // search directories - either may be relative
153 typedef fs::path (*canonicalizer)(fs::path const&, fs::path const&);
154 using namespace boost::placeholders;
155 std::transform(exclude_dirs.begin(), exclude_dirs.end(),
156 exclude_dirs.begin(),
157 boost::bind(static_cast<canonicalizer>(&fs::canonical),
158 _1, fs::current_path()));
159
160 fs::recursive_directory_iterator dir_end;
161 fs::recursive_directory_iterator dir_beg(dirname);
162 for (fs::recursive_directory_iterator it = dir_beg; it != dir_end; ++it) {
163 if (it->status().type() == fs::regular_file) {
164 std::string fn = it->path().string();
165 if (regex_match(fn, header_regex))
166 process_header(fn, macro_regex);
167 }
168 if ((it->status().type() == fs::directory_file) &&
169 (std::find(exclude_dirs.begin(),
170 exclude_dirs.end(),
171 fs::canonical(it->path())) != exclude_dirs.end())) {
172 // skip recursion here
173 it.no_push();
174 }
175 }
176
177 return 0;
178 }
179