Skip to content

Commit

Permalink
inspect tool: check for C-style assert macro instead of BOOST_ASSERT
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.boost.org/svn/boost/trunk@66502 b8fc166d-592f-0410-95f2-cb63ce0dd405
  • Loading branch information
eric_niebler committed Nov 11, 2010
1 parent 2dda859 commit be79ba8
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 3 deletions.
98 changes: 98 additions & 0 deletions tools/inspect/assert_macro_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// assert_macro_check implementation ------------------------------------------------//

// Copyright Eric Niebler 2010.
// Based on the assert_macro_check checker by Marshall Clow
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "assert_macro_check.hpp"
#include <functional>
#include "boost/regex.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/filesystem/operations.hpp"

namespace fs = boost::filesystem;

namespace
{
boost::regex assert_macro_regex(
"("
"^\\s*#\\s*undef\\s*" // # undef
"\\b(assert)\\b" // followed by assert macro, whole word
")"
"|" // or (ignored)
"("
"//[^\\n]*" // single line comments (//)
"|"
"/\\*.*?\\*/" // multi line comments (/**/)
"|"
"\"(?:\\\\\\\\|\\\\\"|[^\"])*\"" // string literals
")"
"|" // or
"("
"\\b(assert)\\b" // assert macro, whole word
"\\s*\\(" // followed by 0 or more spaces and an opening paren
")"
, boost::regex::normal);

} // unnamed namespace


namespace boost
{
namespace inspect
{
assert_macro_check::assert_macro_check() : m_files_with_errors(0)
{
register_signature( ".c" );
register_signature( ".cpp" );
register_signature( ".cxx" );
register_signature( ".h" );
register_signature( ".hpp" );
register_signature( ".hxx" );
register_signature( ".ipp" );
}

void assert_macro_check::inspect(
const string & library_name,
const path & full_path, // example: c:/foo/boost/filesystem/path.hpp
const string & contents ) // contents of file to be inspected
{
if (contents.find( "boostinspect:" "naassert_macro" ) != string::npos) return;

boost::sregex_iterator cur(contents.begin(), contents.end(), assert_macro_regex), end;

long errors = 0;

for( ; cur != end; ++cur /*, ++m_files_with_errors*/ )
{

if(!(*cur)[3].matched)
{
string::const_iterator it = contents.begin();
string::const_iterator match_it = (*cur)[0].first;

string::const_iterator line_start = it;

string::size_type line_number = 1;
for ( ; it != match_it; ++it) {
if (string::traits_type::eq(*it, '\n')) {
++line_number;
line_start = it + 1; // could be end()
}
}

++errors;
error( library_name, full_path, "C-style assert macro", line_number );
}
}
if(errors > 0) {
++m_files_with_errors;
}
}
} // namespace inspect
} // namespace boost


39 changes: 39 additions & 0 deletions tools/inspect/assert_macro_check.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// assert_macro_check header --------------------------------------------------------//

// Copyright Eric Niebler 2010.
// Based on the apple_macro_check checker by Marshall Clow
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_ASSERT_MACRO_CHECK_HPP
#define BOOST_ASSERT_MACRO_CHECK_HPP

#include "inspector.hpp"


namespace boost
{
namespace inspect
{
class assert_macro_check : public inspector
{
long m_files_with_errors;
public:

assert_macro_check();
virtual const char * name() const { return "*ASSERT-MACROS*"; }
virtual const char * desc() const { return "presence of C-style assert macro in file (use BOOST_ASSERT instead)"; }

virtual void inspect(
const std::string & library_name,
const path & full_path,
const std::string & contents );

virtual ~assert_macro_check()
{ std::cout << " " << m_files_with_errors << " files with a C-style assert macro" << line_break(); }
};
}
}

#endif // BOOST_ASSERT_MACRO_CHECK_HPP
2 changes: 1 addition & 1 deletion tools/inspect/build/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ project
exe inspect
:
inspect.cpp license_check.cpp link_check.cpp path_name_check.cpp tab_check.cpp crlf_check.cpp end_check.cpp unnamed_namespace_check.cpp ascii_check.cpp
copyright_check.cpp minmax_check.cpp apple_macro_check.cpp
copyright_check.cpp minmax_check.cpp apple_macro_check.cpp assert_macro_check.cpp
/boost//filesystem/<link>static
/boost//regex/<link>static
:
Expand Down
4 changes: 4 additions & 0 deletions tools/inspect/build/msvc/boost_inspect.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@
RelativePath="..\..\ascii_check.cpp"
>
</File>
<File
RelativePath="..\..\assert_macro_check.cpp"
>
</File>
<File
RelativePath="..\..\copyright_check.cpp"
>
Expand Down
20 changes: 18 additions & 2 deletions tools/inspect/doc/inspect.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ Usage: inspect \[-cvs\] \[-text\] \[-brief\] \[options...\]
-license
-copyright
-crlf
-end
-link
-long_name
-path_name
-tab
-ascii
-apple_macro
-assert_macro
-minmax
-unnamed
default is all checks on; otherwise options specify desired checks
Expand Down Expand Up @@ -86,15 +90,27 @@ There are two types of options allowed, ones that control general operation and
[ [[^-crlf]]
[Checks that files use consistent EOL chanracters.] ]

[ [[^-end]]
[Checks that files end with a newline character.] ]

[ [[^-link]]
[Checks the validity of URIs in HTML files.] ]

[ [[^-long_name]]
[ [[^-path_name]]
[Checks for long names, and a variety of other file name problems that inhibit portability of files.] ]

[ [[^-tab]]
[Checks for files with tab characters.] ]

[ [[^-ascii]]
[Checks for files with non-ASCII characters.] ]

[ [[^-apple_macro]]
[Checks for conflicts with to Apple's unfortunately named debugging macros.] ]

[ [[^-assert_macro]]
[Checks for presence of C-style assert macros (instead of BOOST_ASSERT).] ]

[ [[^-minmax]]
[Checks for violations of the Boost min/max quidelines.] ]

Expand Down
8 changes: 8 additions & 0 deletions tools/inspect/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "tab_check.hpp"
#include "ascii_check.hpp"
#include "apple_macro_check.hpp"
#include "assert_macro_check.hpp"
#include "minmax_check.hpp"
#include "unnamed_namespace_check.hpp"

Expand Down Expand Up @@ -547,6 +548,7 @@ namespace
" -tab\n"
" -ascii\n"
" -apple_macro\n"
" -assert_macro\n"
" -minmax\n"
" -unnamed\n"
" default is all checks on; otherwise options specify desired checks"
Expand Down Expand Up @@ -719,6 +721,7 @@ int cpp_main( int argc_param, char * argv_param[] )
bool tab_ck = true;
bool ascii_ck = true;
bool apple_ok = true;
bool assert_ok = true;
bool minmax_ck = true;
bool unnamed_ck = true;
bool cvs = false;
Expand Down Expand Up @@ -752,6 +755,7 @@ int cpp_main( int argc_param, char * argv_param[] )
tab_ck = false;
ascii_ck = false;
apple_ok = false;
assert_ok = false;
minmax_ck = false;
unnamed_ck = false;
}
Expand All @@ -777,6 +781,8 @@ int cpp_main( int argc_param, char * argv_param[] )
ascii_ck = true;
else if ( std::strcmp( argv[1], "-apple_macro" ) == 0 )
apple_ok = true;
else if ( std::strcmp( argv[1], "-assert_macro" ) == 0 )
assert_ok = true;
else if ( std::strcmp( argv[1], "-minmax" ) == 0 )
minmax_ck = true;
else if ( std::strcmp( argv[1], "-unnamed" ) == 0 )
Expand Down Expand Up @@ -822,6 +828,8 @@ int cpp_main( int argc_param, char * argv_param[] )
inspectors.push_back( inspector_element( new boost::inspect::ascii_check ) );
if ( apple_ok )
inspectors.push_back( inspector_element( new boost::inspect::apple_macro_check ) );
if ( assert_ok )
inspectors.push_back( inspector_element( new boost::inspect::assert_macro_check ) );
if ( minmax_ck )
inspectors.push_back( inspector_element( new boost::inspect::minmax_check ) );
if ( unnamed_ck )
Expand Down

0 comments on commit be79ba8

Please sign in to comment.