[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 76ee59d 2/3: Detect missorted #include direct
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 76ee59d 2/3: Detect missorted #include directives |
Date: |
Sun, 19 Feb 2017 11:39:13 -0500 (EST) |
branch: master
commit 76ee59d0f819a0b65c3be8ffb4206b2c8a068b28
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Detect missorted #include directives
---
Makefile.am | 1 +
objects.make | 1 +
test_coding_rules.cpp | 29 ++++++++++++++++++++++++++++-
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/Makefile.am b/Makefile.am
index ccbfe59..53a9c67 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -515,6 +515,7 @@ test_coding_rules_SOURCES = \
fenv_lmi.cpp \
main_common.cpp \
main_common_non_wx.cpp \
+ miscellany.cpp \
my_test_coding_rules.cpp \
sigfpe.cpp \
test_coding_rules.cpp
diff --git a/objects.make b/objects.make
index 2010d2d..9d980d5 100644
--- a/objects.make
+++ b/objects.make
@@ -1002,6 +1002,7 @@ test_coding_rules$(EXEEXT): \
$(boost_filesystem_objects) \
$(boost_regex_objects) \
$(main_auxiliary_common_objects) \
+ miscellany.o \
my_test_coding_rules.o \
test_coding_rules.o \
diff --git a/test_coding_rules.cpp b/test_coding_rules.cpp
index 368d9c0..333bba5 100644
--- a/test_coding_rules.cpp
+++ b/test_coding_rules.cpp
@@ -24,7 +24,7 @@
#include "handle_exceptions.hpp"
#include "istream_to_string.hpp"
#include "main_common.hpp"
-#include "miscellany.hpp" // lmi_array_size()
+#include "miscellany.hpp" // lmi_array_size(), split_into_lines()
#include "obstruct_slicing.hpp"
#include "uncopyable_lmi.hpp"
@@ -34,6 +34,7 @@
#include <boost/filesystem/path.hpp>
#include <boost/regex.hpp>
+#include <algorithm> // std::is_sorted()
#include <cstddef> // std::size_t
#include <ctime>
#include <iomanip>
@@ -708,6 +709,31 @@ void check_include_guards(file const& f)
require(f, guards, "lacks canonical header guards.");
}
+void check_inclusion_order(file const& f)
+{
+ if(!f.is_of_phylum(e_c_or_cxx))
+ {
+ return;
+ }
+
+ static boost::regex const r(R"((?<=\n\n)(# *include *[<"][^\n]*\n)+\n)");
+ boost::sregex_iterator i(f.data().begin(), f.data().end(), r);
+ boost::sregex_iterator const omega;
+ for(; i != omega; ++i)
+ {
+ boost::smatch const& z(*i);
+ std::string s = z[0];
+ rtrim(s, "\n");
+ std::vector<std::string> v = split_into_lines(s);
+ if(!std::is_sorted(v.begin(), v.end()))
+ {
+ std::ostringstream oss;
+ oss << "has missorted #include directives:\n" << s;
+ complain(f, oss.str());
+ }
+ }
+}
+
void check_label_indentation(file const& f)
{
if(!f.is_of_phylum(e_c_or_cxx))
@@ -1149,6 +1175,7 @@ statistics process_file(std::string const& file_path)
check_cxx (f);
check_defect_markers (f);
check_include_guards (f);
+ check_inclusion_order (f);
check_label_indentation (f);
check_logs (f);
check_preamble (f);