forked from STEllAR-GROUP/hpx
/
endline_whitespace_check.cpp
115 lines (105 loc) · 3.77 KB
/
endline_whitespace_check.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// extra_whitespace_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 "endline_whitespace_check.hpp"
#include "function_hyper.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
{
whitespace_check::whitespace_check()
: 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");
}
void whitespace_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:" "endlinewhitespace") != string::npos)
return;
string whitespace(" \t\f\v\r\n"), 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");
size_t found = someline[p].find_last_not_of(whitespace);
if (rend != string::npos)
{
extend = 2;
}
else
{
extend = 1;
}
size_t size = someline[p].size();
if (found < size - extend || (found == someline[p].npos && size > 1))
{
errors++;
linenum = to_string(currline);
lineorder.push_back(linenum);
}
p++;
}
p = 0;
while (p < lineorder.size())
{
total += linelink(full_path, lineorder[p]); //linelink is located in function_hyper.hpp
if (p < lineorder.size() - 1)
{
total += ", ";
}
p++;
}
if (errors > 0)
{
string errored = "*Endline Whitespace*: " + total;
error(library_name, full_path, errored);
++m_files_with_errors;
}
}
} // namespace inspect
} // namespace boost