1 // assert_macro_check implementation ------------------------------------------------//
3 // Copyright Eric Niebler 2010.
4 // Based on the assert_macro_check checker by Marshall Clow
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 #include "assert_macro_check.hpp"
12 #include "boost/regex.hpp"
13 #include "boost/lexical_cast.hpp"
14 #include "boost/filesystem/operations.hpp"
16 namespace fs
= boost::filesystem
;
20 boost::regex
assert_macro_regex(
22 "^\\s*#\\s*undef\\s*" // # undef
23 "\\b(assert)\\b" // followed by assert macro, whole word
27 "//[^\\n]*" // single line comments (//)
29 "/\\*.*?\\*/" // multi line comments (/**/)
31 "\"(?:\\\\\\\\|\\\\\"|[^\"])*\"" // string literals
35 "\\b(assert)\\b" // assert macro, whole word
36 "\\s*\\(" // followed by 0 or more spaces and an opening paren
38 , boost::regex::normal
);
40 } // unnamed namespace
47 assert_macro_check::assert_macro_check()
48 : m_files_with_errors(0)
50 fs::exists(search_root_path() / "boost") &&
51 fs::exists(search_root_path() / "libs"))
53 register_signature( ".c" );
54 register_signature( ".cpp" );
55 register_signature( ".cxx" );
56 register_signature( ".h" );
57 register_signature( ".hpp" );
58 register_signature( ".hxx" );
59 register_signature( ".ipp" );
62 void assert_macro_check::inspect(
63 const string
& library_name
,
64 const path
& full_path
, // example: c:/foo/boost/filesystem/path.hpp
65 const string
& contents
) // contents of file to be inspected
67 if (contents
.find( "boostinspect:" "naassert_macro" ) != string::npos
)
70 // Check files iff (a) they are in the boost directory, or (b) they
71 // are in the src directory under libs.
72 if (m_from_boost_root
) {
73 path
relative( relative_to( full_path
, search_root_path() ) );
74 path::const_iterator pbeg
= relative
.begin(), pend
= relative
.end();
75 if (pbeg
!= std::find(pbeg
, pend
, "boost") &&
76 !(pbeg
== std::find(pbeg
, pend
, "libs") && pend
!= std::find(pbeg
, pend
, "src")))
81 boost::sregex_iterator
cur(contents
.begin(), contents
.end(), assert_macro_regex
), end
;
82 for( ; cur
!= end
; ++cur
)
84 if(!(*cur
)[3].matched
)
86 string::const_iterator it
= contents
.begin();
87 string::const_iterator match_it
= (*cur
)[0].first
;
89 string::const_iterator line_start
= it
;
91 string::size_type line_number
= 1;
92 for ( ; it
!= match_it
; ++it
) {
93 if (string::traits_type::eq(*it
, '\n')) {
95 line_start
= it
+ 1; // could be end()
100 error( library_name
, full_path
, "C-style assert macro on line "
101 + boost::lexical_cast
<string
>( line_number
) );
105 ++m_files_with_errors
;
107 } // namespace inspect