Skip to content

Commit

Permalink
Adds a tool for inspect that checks for character limits
Browse files Browse the repository at this point in the history
Created to default to hard limit of 90, excludes documented files, CMakeLists.txt, and #error.
  • Loading branch information
Bcorde5 committed Jul 23, 2015
1 parent 1f14217 commit 084b912
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/inspect/inspect.cpp
Expand Up @@ -27,6 +27,7 @@ const char* hpx_no_inspect = "hpx-" "no-inspect";
#include <cstring>
#include <iostream>
#include <fstream>
#include <limits>

#include "boost/shared_ptr.hpp"
#include "boost/lexical_cast.hpp"
Expand Down Expand Up @@ -62,6 +63,7 @@ const char* hpx_no_inspect = "hpx-" "no-inspect";
#include "minmax_check.hpp"
#include "unnamed_namespace_check.hpp"
#include "endline_whitespace_check.hpp"
#include "length_check.hpp"

//#include "cvs_iterator.hpp"

Expand Down Expand Up @@ -816,6 +818,7 @@ int cpp_main( int argc_param, char * argv_param[] )
bool minmax_ck = false;
bool unnamed_ck = false;
bool whitespace_ck = false;
bool length_ck = false;

desc_commandline.add_options()
("help,h", "print some command line help")
Expand Down Expand Up @@ -853,6 +856,8 @@ int cpp_main( int argc_param, char * argv_param[] )
"check for unnamed namespace usage violations (default: off)")
("whitespace", value<bool>(&whitespace_ck)->implicit_value(false),
"check for endline whitespace violations (default: off)")
("length", value<bool>(&length_ck)->implicit_value(false),
"check for exceeding character limit (default: off)")

("all,a", "check for all violations (default: no checks are performed)")
;
Expand Down Expand Up @@ -916,12 +921,36 @@ int cpp_main( int argc_param, char * argv_param[] )
minmax_ck = true;
unnamed_ck = true;
whitespace_ck = true;
length_ck = true;
}

std::string output_path("-");
if (vm.count("output"))
output_path = vm["output"].as<std::string>();

//// configure length limit
size_t limit;

if (length_ck)
{
std::cout << "What is the character limit for each line? ";
std::cout << "Entering a number less than 1 sets a default to 90" << std::endl;
int length = 0;
while (!(std::cin >> length)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Try again: ";
}
if (length < 1)
{
limit = 90;
}
else
{
limit = length;
}
}

// since this is in its own block; reporting will happen
// automatically, from each registered inspector, when
// leaving, due to destruction of the inspector_list object
Expand Down Expand Up @@ -955,6 +984,8 @@ int cpp_main( int argc_param, char * argv_param[] )
inspectors.push_back( inspector_element( new boost::inspect::unnamed_namespace_check ) );
if ( whitespace_ck )
inspectors.push_back( inspector_element( new boost::inspect::whitespace_check) );
if ( length_ck)
inspectors.push_back(inspector_element ( new boost::inspect::length_check(limit)) );

//// perform the actual inspection, using the requested type of iteration
for(auto const& search_root: search_roots)
Expand Down
131 changes: 131 additions & 0 deletions tools/inspect/length_check.cpp
@@ -0,0 +1,131 @@
// character_length_check implementation ----------------------------------//

// Copyright (c) 2015 Brandon Cordes
// 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)

#include "length_check.hpp"
#include <iostream>
#include <functional>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include "boost/regex.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/filesystem/operations.hpp"

using namespace std;
namespace fs = boost::filesystem;

namespace boost
{
namespace inspect
{
size_t limit;

length_check::length_check(size_t setting)
: m_files_with_errors(0)

{
register_signature(".c");
register_signature(".cpp");
register_signature(".css");
register_signature(".cxx");
register_signature(".h");
register_signature(".hpp");
register_signature(".hxx");
register_signature(".inc");
register_signature(".ipp");
register_signature(".txt");

limit = setting;
}

void length_check::inspect(
const string & library_name,
const path & full_path, // ex: c:/foo/boost/filesystem/path.hpp
const string & contents) // contents of file to be inspected
{
if (contents.find("hpxinspect:" "length") != string::npos)
return;
string pathname = full_path.string();
if (pathname.find("CMakeLists.txt") != string::npos)
return;
//Temporary, until we are ready to format documentation files in this limitation.
if (library_name.find(".qbk") != string::npos)
return;

string total, linenum;
long errors = 0, currline = 0;
size_t p = 0, extend = 0;
vector<string> someline, lineorder;

char_separator<char> sep("\n", "", boost::keep_empty_tokens);
tokenizer<char_separator<char>> tokens(contents, sep);
for (const auto& t : tokens) {
size_t rend = t.find_first_of("\r"), size = t.size();
if (rend == size - 1)
{
someline.push_back(t);
}
else
{
char_separator<char> sep2("\r", "", boost::keep_empty_tokens);
tokenizer<char_separator<char>> tokens2(t, sep2);
for (const auto& u : tokens2) {
someline.push_back(u);
}
}
}
while (p < someline.size())
{
currline++;
size_t rend = someline[p].find_last_of("\r");
boost::regex error_note;
error_note = "\\s*#\\s*error";
boost::smatch m;
if (boost::regex_match(someline[p], m, error_note))
{
if (m.position() == 0)
{
return;
}
}
if (rend != string::npos)
{
extend = limit + 2;
}
else
{
extend = limit;
}
size_t size = someline[p].size();
if (size > extend)
{
errors++;
linenum = to_string(currline);
lineorder.push_back(linenum);
}
p++;
}
p = 0;
while (p < lineorder.size())
{
total += lineorder[p];
if (p < lineorder.size() - 1)
{
total += ", ";
}
p++;
}
if (errors > 0)
{
string errored = "Character Limit*: " + total;
error(library_name, full_path, errored);
++m_files_with_errors;
}
}
} // namespace inspect
} // namespace boost
46 changes: 46 additions & 0 deletions tools/inspect/length_check.hpp
@@ -0,0 +1,46 @@
// character_length_check header ------------------------------------------//

// Copyright (c) 2015 Brandon Cordes
// 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_EIGHTY_CHECK_HPP
#define BOOST_EIGHTY_CHECK_HPP

#include "inspector.hpp"

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

length_check(size_t setting);

std::string a = "*Character Limit*";
std::string b = "The line is larger than the character limit";
virtual const char * name() const { return a.c_str(); }
virtual const char * desc() const { return b.c_str(); }

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

virtual void print_summary(std::ostream& out)
{
string c = " files exceeding the character limit";
out << " " << m_files_with_errors << c << line_break();
}

virtual ~length_check() {}
};
}
}

#endif // BOOST_EXTRA_WHITESPACE_CHECK_HPP

0 comments on commit 084b912

Please sign in to comment.