1 // inspect program -------------------------------------------------------------------//
3 // Copyright Beman Dawes 2002.
4 // Copyright Rene Rivera 2004-2006.
5 // Copyright Gennaro Prota 2006.
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 // This program recurses through sub-directories looking for various problems.
12 // It contains some Boost specific features, like ignoring "bin",
13 // and the code that identifies library names assumes the Boost directory
16 // See http://www.boost.org/tools/inspect/ for more information.
18 const char* boost_no_inspect = "boost-" "no-inspect";
20 // Directories with a file name of the boost_no_inspect value are not inspected.
21 // Files that contain the boost_no_inspect value are not inspected.
29 #include "boost/shared_ptr.hpp"
30 #include "boost/lexical_cast.hpp"
31 #include "boost/filesystem/operations.hpp"
32 #include "boost/filesystem/fstream.hpp"
34 #include <stdio.h> // for popen, pclose
37 # define PCLOSE _pclose
40 # define PCLOSE pclose
43 #include "time_string.hpp"
45 #include "inspector.hpp"
48 #include "copyright_check.hpp"
49 #include "crlf_check.hpp"
50 #include "end_check.hpp"
51 #include "license_check.hpp"
52 #include "link_check.hpp"
53 #include "path_name_check.hpp"
54 #include "tab_check.hpp"
55 #include "ascii_check.hpp"
56 #include "apple_macro_check.hpp"
57 #include "assert_macro_check.hpp"
58 #include "deprecated_macro_check.hpp"
59 #include "minmax_check.hpp"
60 #include "unnamed_namespace_check.hpp"
62 #if !defined(INSPECT_USE_BOOST_TEST)
63 #define INSPECT_USE_BOOST_TEST 0
66 #if INSPECT_USE_BOOST_TEST
67 #include "boost/test/included/prg_exec_monitor.hpp"
70 namespace fs = boost::filesystem;
72 using namespace boost::inspect;
76 fs::path search_root = fs::initial_path();
78 class inspector_element
80 typedef boost::shared_ptr< boost::inspect::inspector > inspector_ptr;
83 inspector_ptr inspector;
85 inspector_element( boost::inspect::inspector * p ) : inspector(p) {}
88 typedef std::list< inspector_element > inspector_list;
91 long directory_count = 0;
93 const int max_offenders = 5; // maximum "worst offenders" to display
95 boost::inspect::string_set content_signatures;
104 bool operator<( const error_msg & rhs ) const
106 if ( library < rhs.library ) return true;
107 if ( library > rhs.library ) return false;
108 if ( rel_path < rhs.rel_path ) return true;
109 if ( rel_path > rhs.rel_path ) return false;
110 if ( line_number < rhs.line_number ) return true;
111 if ( line_number > rhs.line_number ) return false;
112 return msg < rhs.msg;
116 typedef std::vector< error_msg > error_msg_vector;
117 error_msg_vector msgs;
119 struct lib_error_count
124 bool operator<( const lib_error_count & rhs ) const
126 return error_count > rhs.error_count;
130 typedef std::vector< lib_error_count > lib_error_count_vector;
131 lib_error_count_vector libs;
133 // visit_predicate (determines which directories are visited) --------------//
135 typedef bool(*pred_type)(const path&);
137 bool visit_predicate( const path & pth )
139 string local( boost::inspect::relative_to( pth, search_root_path() ) );
140 string leaf( pth.leaf().string() );
141 if (leaf[0] == '.') // ignore hidden by convention directories such as
142 return false; // .htaccess, .git, .svn, .bzr, .DS_Store, etc.
145 // don't look at binaries
148 // no point in checking doxygen xml output
149 && local.find("doc/xml") != 0
150 && local.find("doc\\xml") != 0
151 // ignore if tag file present
152 && !boost::filesystem::exists(pth / boost_no_inspect)
156 // library_from_content ----------------------------------------------------//
158 string library_from_content( const string & content )
160 const string unknown_library ( "unknown" );
161 const string lib_root ( "www.boost.org/libs/" );
162 string::size_type pos( content.find( lib_root ) );
164 string lib = unknown_library;
166 if ( pos != string::npos ) {
168 pos += lib_root.length();
170 const char delims[] = " " // space and...
173 string::size_type n = content.find_first_of( string(delims), pos );
174 if (n != string::npos)
175 lib = string(content, pos, n - pos);
182 // find_signature ----------------------------------------------------------//
184 bool find_signature( const path & file_path,
185 const boost::inspect::string_set & signatures )
187 string name( file_path.leaf().string() );
188 if ( signatures.find( name ) == signatures.end() )
190 string::size_type pos( name.rfind( '.' ) );
191 if ( pos == string::npos
192 || signatures.find( name.substr( pos ) )
193 == signatures.end() ) return false;
198 // load_content ------------------------------------------------------------//
200 void load_content( const path & file_path, string & target )
204 if ( !find_signature( file_path, content_signatures ) ) return;
206 fs::ifstream fin( file_path, std::ios_base::in|std::ios_base::binary );
208 throw string( "could not open input file: " ) + file_path.string();
209 std::getline( fin, target, '\0' ); // read the whole file
212 // check -------------------------------------------------------------------//
214 void check( const string & lib,
215 const path & pth, const string & content, const inspector_list & insp_list )
217 // invoke each inspector
218 for ( inspector_list::const_iterator itr = insp_list.begin();
219 itr != insp_list.end(); ++itr )
221 itr->inspector->inspect( lib, pth ); // always call two-argument form
222 if ( find_signature( pth, itr->inspector->signatures() ) )
224 itr->inspector->inspect( lib, pth, content );
229 // visit_all ---------------------------------------------------------------//
231 template< class DirectoryIterator >
232 void visit_all( const string & lib,
233 const path & dir_path, const inspector_list & insps )
235 static DirectoryIterator end_itr;
238 for ( DirectoryIterator itr( dir_path ); itr != end_itr; ++itr )
240 if ( fs::is_directory( *itr ) )
242 if ( visit_predicate( *itr ) )
244 string cur_lib( boost::inspect::impute_library( *itr ) );
245 check( cur_lib, *itr, "", insps );
246 visit_all<DirectoryIterator>( cur_lib, *itr, insps );
249 else if (itr->path().leaf().string()[0] != '.') // ignore if hidden
253 load_content( *itr, content );
254 if (content.find(boost_no_inspect) == string::npos)
255 check( lib.empty() ? library_from_content( content ) : lib,
256 *itr, content, insps );
261 // display -----------------------------------------------------------------//
263 enum display_format_type
265 display_html, display_text
267 display_format = display_html;
269 enum display_mode_type
271 display_full, display_brief
273 display_mode = display_full;
275 // display_summary_helper --------------------------------------------------//
277 void display_summary_helper( const string & current_library, int err_count )
279 if (display_format == display_text)
281 std::cout << " " << current_library << " (" << err_count << ")\n";
287 << current_library // what about malformed for URI refs? [gps]
288 << "\">" << current_library
290 << err_count << ")<br>\n";
294 // display_summary ---------------------------------------------------------//
296 void display_summary()
298 if (display_format == display_text)
300 std::cout << "Summary:\n";
310 string current_library( msgs.begin()->library );
312 for ( error_msg_vector::iterator itr ( msgs.begin() );
313 itr != msgs.end(); ++itr )
315 if ( current_library != itr->library )
317 display_summary_helper( current_library, err_count );
318 current_library = itr->library;
323 display_summary_helper( current_library, err_count );
325 if (display_format == display_text)
328 std::cout << "</blockquote>\n";
331 // html_encode -------------------------------------------------------------//
333 std::string html_encode(std::string const& text)
337 for(std::string::const_iterator it = text.begin(),
338 end = text.end(); it != end; ++it)
358 // display_details ---------------------------------------------------------//
360 void display_details()
362 if (display_format == display_text)
364 // display error messages with group indication
367 for ( error_msg_vector::iterator itr ( msgs.begin() );
368 itr != msgs.end(); ++itr )
370 if ( current.library != itr->library )
372 if ( display_full == display_mode )
373 std::cout << "\n|" << itr->library << "|\n";
375 std::cout << "\n\n|" << itr->library << '|';
378 if ( current.library != itr->library
379 || current.rel_path != itr->rel_path )
381 if ( display_full == display_mode )
383 std::cout << " " << itr->rel_path << ":\n";
387 path current_rel_path(current.rel_path);
388 path this_rel_path(itr->rel_path);
389 if (current_rel_path.branch_path() != this_rel_path.branch_path())
391 std::cout << "\n " << this_rel_path.branch_path().string() << '/';
393 std::cout << "\n " << this_rel_path.leaf() << ':';
396 if ( current.library != itr->library
397 || current.rel_path != itr->rel_path
398 || current.msg != itr->msg )
400 const string m = itr->msg;
402 if ( display_full == display_mode )
403 std::cout << " " << m << '\n';
405 std::cout << ' ' << m;
407 current.library = itr->library;
408 current.rel_path = itr->rel_path;
409 current.msg = itr->msg;
415 // display error messages with group indication
417 bool first_sep = true;
419 for ( error_msg_vector::iterator itr ( msgs.begin() );
420 itr != msgs.end(); ++itr )
422 if ( current.library != itr->library )
424 if ( !first ) std::cout << "</pre>\n";
425 std::cout << "\n<h3><a name=\"" << itr->library
426 << "\">" << itr->library << "</a></h3>\n<pre>";
428 if ( current.library != itr->library
429 || current.rel_path != itr->rel_path )
432 std::cout << itr->rel_path;
435 if ( current.library != itr->library
436 || current.rel_path != itr->rel_path
437 || current.msg != itr->msg )
441 if (itr->line_number) sep = ":<br> ";
444 if (itr->line_number) sep = "<br> ";
448 if (itr->line_number)
449 std::cout << sep << "(line " << itr->line_number << ") " << html_encode(itr->msg);
450 else std::cout << sep << html_encode(itr->msg);
454 current.library = itr->library;
455 current.rel_path = itr->rel_path;
456 current.msg = itr->msg;
459 std::cout << "</pre>\n";
464 // worst_offenders_count_helper --------------------------------------------------//
466 void worst_offenders_count_helper( const string & current_library, int err_count )
469 lec.library = current_library;
470 lec.error_count = err_count;
471 libs.push_back( lec );
473 // worst_offenders_count -----------------------------------------------------//
475 void worst_offenders_count()
481 string current_library( msgs.begin()->library );
483 for ( error_msg_vector::iterator itr ( msgs.begin() );
484 itr != msgs.end(); ++itr )
486 if ( current_library != itr->library )
488 worst_offenders_count_helper( current_library, err_count );
489 current_library = itr->library;
494 worst_offenders_count_helper( current_library, err_count );
497 // display_worst_offenders -------------------------------------------------//
499 void display_worst_offenders()
501 if (display_mode == display_brief)
503 if (display_format == display_text)
505 std::cout << "Worst Offenders:\n";
510 "<h2>Worst Offenders</h2>\n"
515 int display_count = 0;
516 int last_error_count = 0;
517 for ( lib_error_count_vector::iterator itr ( libs.begin() );
519 && (display_count < max_offenders
520 || itr->error_count == last_error_count);
521 ++itr, ++display_count )
523 if (display_format == display_text)
525 std::cout << itr->library << " " << itr->error_count << "\n";
532 << "\">" << itr->library
534 << itr->error_count << ")<br>\n";
536 last_error_count = itr->error_count;
539 if (display_format == display_text)
542 std::cout << "</blockquote>\n";
546 const char * options()
549 " Output Options:\n\n"
552 " -version-string <version message>\n"
565 " -deprecated_macro\n"
568 " -version-string <version message>\n"
569 " default is all checks on; otherwise options specify desired checks"
573 const char * doctype_declaration()
576 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
577 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
581 std::string validator_link(const std::string & text)
584 // with link to validation service
585 "<a href=\"http://validator.w3.org/check?uri=referer\">"
591 } // unnamed namespace
598 // line_break --------------------------------------------------------------//
600 const char * line_break()
602 return display_format ? "\n" : "<br>\n";
605 // search_root_path --------------------------------------------------------//
607 path search_root_path()
612 // register_signature ------------------------------------------------------//
614 void inspector::register_signature( const string & signature )
616 m_signatures.insert( signature );
617 content_signatures.insert( signature );
620 // error -------------------------------------------------------------------//
622 void inspector::error( const string & library_name,
623 const path & full_path, const string & msg, int line_number )
627 err_msg.library = library_name;
628 err_msg.rel_path = relative_to( full_path, search_root_path() );
630 err_msg.line_number = line_number;
631 msgs.push_back( err_msg );
633 // std::cout << library_name << ": "
634 // << full_path.string() << ": "
639 source_inspector::source_inspector()
641 // C/C++ source code...
642 register_signature( ".c" );
643 register_signature( ".cpp" );
644 register_signature( ".css" );
645 register_signature( ".cxx" );
646 register_signature( ".h" );
647 register_signature( ".hpp" );
648 register_signature( ".hxx" );
649 register_signature( ".inc" );
650 register_signature( ".ipp" );
652 // Boost.Build BJam source code...
653 register_signature( "Jamfile" );
654 register_signature( ".jam" );
655 register_signature( ".v2" );
657 // Other scripts; Python, shell, autoconfig, etc.
658 register_signature( "configure.in" );
659 register_signature( "GNUmakefile" );
660 register_signature( "Makefile" );
661 register_signature( ".bat" );
662 register_signature( ".mak" );
663 register_signature( ".pl" );
664 register_signature( ".py" );
665 register_signature( ".sh" );
667 // Hypertext, Boost.Book, and other text...
668 register_signature( "news" );
669 register_signature( "readme" );
670 register_signature( "todo" );
671 register_signature( "NEWS" );
672 register_signature( "README" );
673 register_signature( "TODO" );
674 register_signature( ".boostbook" );
675 register_signature( ".htm" );
676 register_signature( ".html" );
677 register_signature( ".rst" );
678 register_signature( ".sgml" );
679 register_signature( ".shtml" );
680 register_signature( ".txt" );
681 register_signature( ".xml" );
682 register_signature( ".xsd" );
683 register_signature( ".xsl" );
684 register_signature( ".qbk" );
687 hypertext_inspector::hypertext_inspector()
689 register_signature( ".htm" );
690 register_signature( ".html" );
691 register_signature( ".shtml" );
694 // impute_library ----------------------------------------------------------//
696 // may return an empty string [gps]
697 string impute_library( const path & full_dir_path )
699 path relative( relative_to( full_dir_path, search_root_path() ) );
700 if ( relative.empty() ) return "boost-root";
701 string first( (*relative.begin()).string() );
702 string second = // borland 5.61 requires op=
703 ++relative.begin() == relative.end()
704 ? string() : (*++relative.begin()).string();
706 if ( first == "boost" )
709 return (( first == "libs" || first == "tools" ) && !second.empty())
713 } // namespace inspect
716 // cpp_main() --------------------------------------------------------------//
718 #if !INSPECT_USE_BOOST_TEST
719 int main( int argc_param, char * argv_param[] )
721 int cpp_main( int argc_param, char * argv_param[] )
724 // <hack> for the moment, let's be on the safe side
725 // and ensure we don't modify anything being pointed to;
726 // then we'll do some cleanup here
727 int argc = argc_param;
728 const char* const * argv = &argv_param[0];
730 if ( argc > 1 && (std::strcmp( argv[1], "-help" ) == 0
731 || std::strcmp( argv[1], "--help" ) == 0 ) )
733 std::clog << "Usage: inspect [search-root] [options...]\n\n"
734 " search-root default is the current directory (i.e. '.')\n\n"
735 << options() << '\n';
739 bool options_not_set = true;
740 bool license_ck = false;
741 bool copyright_ck = false;
742 bool crlf_ck = false;
744 bool link_ck = false;
745 bool path_name_ck = false;
747 bool ascii_ck = false;
748 bool apple_ck = false;
749 bool assert_ck = false;
750 bool deprecated_ck = false;
751 bool minmax_ck = false;
752 bool unnamed_ck = false;
753 const char* version_string = 0;
755 if ( argc > 1 && *argv[1] != '-' )
757 search_root = fs::canonical(fs::absolute(argv[1], fs::initial_path()));
761 bool invalid_options = false;
762 for(; argc > 1; --argc, ++argv )
764 if ( std::strcmp( argv[1], "-license" ) == 0 ) {
765 options_not_set = false;
768 else if ( std::strcmp( argv[1], "-copyright" ) == 0 ) {
769 options_not_set = false;
772 else if ( std::strcmp( argv[1], "-crlf" ) == 0 ) {
773 options_not_set = false;
776 else if ( std::strcmp( argv[1], "-end" ) == 0 ) {
777 options_not_set = false;
780 else if ( std::strcmp( argv[1], "-link" ) == 0 ) {
781 options_not_set = false;
784 else if ( std::strcmp( argv[1], "-path_name" ) == 0 ) {
785 options_not_set = false;
788 else if ( std::strcmp( argv[1], "-tab" ) == 0 ) {
789 options_not_set = false;
792 else if ( std::strcmp( argv[1], "-ascii" ) == 0 ) {
793 options_not_set = false;
796 else if ( std::strcmp( argv[1], "-apple_macro" ) == 0 ) {
797 options_not_set = false;
800 else if ( std::strcmp( argv[1], "-assert_macro" ) == 0 ) {
801 options_not_set = false;
804 else if ( std::strcmp( argv[1], "-deprecated_macro" ) == 0 ) {
805 options_not_set = false;
806 deprecated_ck = true;
808 else if ( std::strcmp( argv[1], "-minmax" ) == 0 ) {
809 options_not_set = false;
812 else if ( std::strcmp( argv[1], "-unnamed" ) == 0 ) {
813 options_not_set = false;
816 else if ( argc > 1 && std::strcmp( argv[1], "-text" ) == 0 )
818 display_format = display_text;
820 else if ( argc > 1 && std::strcmp( argv[1], "-brief" ) == 0 )
822 display_mode = display_brief;
824 else if ( std::strcmp( argv[1], "-version-string" ) == 0 ) {
825 if (argc == 2 || argv[2][0] == '-') {
826 std::cerr << "Missing value for -version-string.\n";
827 invalid_options = true;
831 version_string = argv[1];
836 std::cerr << "unknown option: " << argv[1] << '\n';
837 invalid_options = true;
840 if ( invalid_options ) {
841 std::cerr << "\nvalid options are:\n"
846 if (options_not_set) {
857 deprecated_ck = true;
862 string inspector_keys;
864 { // begin reporting block
866 // since this is in its own block; reporting will happen
867 // automatically, from each registered inspector, when
868 // leaving, due to destruction of the inspector_list object
869 inspector_list inspectors;
872 inspectors.push_back( inspector_element( new boost::inspect::license_check ) );
874 inspectors.push_back( inspector_element( new boost::inspect::copyright_check ) );
876 inspectors.push_back( inspector_element( new boost::inspect::crlf_check ) );
878 inspectors.push_back( inspector_element( new boost::inspect::end_check ) );
880 inspectors.push_back( inspector_element( new boost::inspect::link_check ) );
882 inspectors.push_back( inspector_element( new boost::inspect::file_name_check ) );
884 inspectors.push_back( inspector_element( new boost::inspect::tab_check ) );
886 inspectors.push_back( inspector_element( new boost::inspect::ascii_check ) );
888 inspectors.push_back( inspector_element( new boost::inspect::apple_macro_check ) );
890 inspectors.push_back( inspector_element( new boost::inspect::assert_macro_check ) );
892 inspectors.push_back( inspector_element( new boost::inspect::deprecated_macro_check ) );
894 inspectors.push_back( inspector_element( new boost::inspect::minmax_check ) );
896 inspectors.push_back( inspector_element( new boost::inspect::unnamed_namespace_check ) );
898 visit_all<fs::directory_iterator>( search_root.leaf().string(),
899 search_root, inspectors );
902 for ( inspector_list::iterator itr = inspectors.begin();
903 itr != inspectors.end(); ++itr )
905 itr->inspector->close();
908 string run_date ( "n/a" );
909 boost::time_string( run_date );
911 if (display_format == display_text)
915 "Boost Inspection Report\n"
916 "Run Date: " << run_date << "\n"
920 if (version_string) {
922 << "The files checked were from "
929 << " " << file_count << " files scanned\n"
930 << " " << directory_count << " directories scanned (including root)\n"
931 << " " << error_count << " problems reported\n"
938 std::cout << doctype_declaration() << '\n';
943 "<style> body { font-family: sans-serif; } </style>\n"
944 "<title>Boost Inspection Report</title>\n"
948 // we should not use a table, of course [gps]
951 "<td><img src=\"http://www.boost.org/boost.png\" alt=\"Boost logo\" />"
954 "<h1>Boost Inspection Report</h1>\n"
955 "<b>Run Date:</b> " << run_date << "\n"
956 //" / " << validator_link( "validate me" ) << " /\n"
961 "<p>This report is generated by an <a href=\"http://www.boost.org/tools/inspect/index.html\">inspection\n"
962 "program</a> that checks files for the problems noted below.</p>\n"
964 if (version_string) {
966 << "<p>The files checked were from "
967 << html_encode(version_string)
973 << "<h2>Totals</h2>\n"
974 << file_count << " files scanned<br>\n"
975 << directory_count << " directories scanned (including root)<br>\n"
976 << error_count << " problems reported\n<p>";
979 for ( inspector_list::iterator itr = inspectors.begin();
980 itr != inspectors.end(); ++itr )
983 inspector_keys += static_cast<string>(" ")
984 + itr->inspector->name()
985 + ' ' + itr->inspector->desc()
990 if (display_format == display_text)
991 std::cout << "\nProblem counts:\n";
993 std::cout << "\n<h2>Problem counts</h2>\n<blockquote><p>\n" ;
995 } // end of block: starts reporting
997 if (display_format == display_text)
1000 std::cout << "</blockquote>\n";
1002 std::sort( msgs.begin(), msgs.end() );
1004 worst_offenders_count();
1005 std::stable_sort( libs.begin(), libs.end() );
1007 if ( !libs.empty() && display_mode != display_brief)
1008 display_worst_offenders();
1010 if ( !msgs.empty() )
1014 if (display_format == display_text)
1016 std::cout << "Details:\n" << inspector_keys;
1017 std::cout << "\nDirectories with a file named \"" << boost_no_inspect << "\" will not be inspected.\n"
1018 "Files containing \"" << boost_no_inspect << "\" will not be inspected.\n";
1022 std::cout << "<h2>Details</h2>\n" << inspector_keys;
1023 std::cout << "\n<p>Directories with a file named \"" << boost_no_inspect << "\" will not be inspected.<br>\n"
1024 "Files containing \"" << boost_no_inspect << "\" will not be inspected.</p>\n";
1029 if (display_format == display_text)
1031 std::cout << "\n\n" ;
1040 return error_count ? 1 : 0;