Skip to content

Commit 847c6d9

Browse files
committed
tools: cpplint: fix up build/include_order rule
Change the build/include_order rule to match our preference: project headers before system headers. The rationale is that system headers before project headers makes it easy to slip in bugs where a project header that requires a definition from a system header, forgets to include the system header but still compiles because the source files that include the project header coincidentally include the system header too. A good example is the size_t type. A project header file that needs the definition of size_t should include stddef.h but forgetting to do so will probably go unnoticed for a long time because almost every other system header includes stddef.h (either directly or indirectly) and almost every source file includes one or more system headers. Ergo, project headers before system headers. It's a good thing.
1 parent 58159e3 commit 847c6d9

1 file changed

Lines changed: 29 additions & 30 deletions

File tree

tools/cpplint.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@
280280

281281
# These constants define types of headers for use with
282282
# _IncludeState.CheckNextIncludeOrder().
283-
_C_SYS_HEADER = 1
284-
_CPP_SYS_HEADER = 2
285-
_LIKELY_MY_HEADER = 3
286-
_POSSIBLE_MY_HEADER = 4
287-
_OTHER_HEADER = 5
283+
_LIKELY_MY_HEADER = 1
284+
_POSSIBLE_MY_HEADER = 2
285+
_OTHER_HEADER = 3
286+
_C_SYS_HEADER = 4
287+
_CPP_SYS_HEADER = 5
288288

289289

290290
_regexp_compile_cache = {}
@@ -377,9 +377,9 @@ class _IncludeState(dict):
377377
# needs to move backwards, CheckNextIncludeOrder will raise an error.
378378
_INITIAL_SECTION = 0
379379
_MY_H_SECTION = 1
380-
_C_SECTION = 2
381-
_CPP_SECTION = 3
382-
_OTHER_H_SECTION = 4
380+
_OTHER_H_SECTION = 2
381+
_C_SECTION = 3
382+
_CPP_SECTION = 4
383383

384384
_TYPE_NAMES = {
385385
_C_SYS_HEADER: 'C system header',
@@ -453,33 +453,32 @@ def CheckNextIncludeOrder(self, header_type):
453453

454454
last_section = self._section
455455

456-
if header_type == _C_SYS_HEADER:
457-
if self._section <= self._C_SECTION:
458-
self._section = self._C_SECTION
459-
else:
460-
self._last_header = ''
461-
return error_message
462-
elif header_type == _CPP_SYS_HEADER:
463-
if self._section <= self._CPP_SECTION:
464-
self._section = self._CPP_SECTION
465-
else:
466-
self._last_header = ''
467-
return error_message
468-
elif header_type == _LIKELY_MY_HEADER:
456+
if header_type == _LIKELY_MY_HEADER:
469457
if self._section <= self._MY_H_SECTION:
470458
self._section = self._MY_H_SECTION
471459
else:
472-
self._section = self._OTHER_H_SECTION
460+
self._last_header = ''
461+
return error_message
473462
elif header_type == _POSSIBLE_MY_HEADER:
474463
if self._section <= self._MY_H_SECTION:
475464
self._section = self._MY_H_SECTION
476465
else:
477-
# This will always be the fallback because we're not sure
478-
# enough that the header is associated with this file.
466+
self._last_header = ''
467+
return error_message
468+
elif header_type == _OTHER_HEADER:
469+
if self._section <= self._OTHER_H_SECTION:
479470
self._section = self._OTHER_H_SECTION
471+
else:
472+
self._last_header = ''
473+
return error_message
474+
elif header_type == _C_SYS_HEADER:
475+
if self._section <= self._C_SECTION:
476+
self._section = self._C_SECTION
477+
else:
478+
self._section = self._CPP_SECTION
480479
else:
481-
assert header_type == _OTHER_HEADER
482-
self._section = self._OTHER_H_SECTION
480+
assert header_type == _CPP_SYS_HEADER
481+
self._section = self._CPP_SECTION
483482

484483
if last_section != self._section:
485484
self._last_header = ''
@@ -2312,10 +2311,10 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
23122311
# lower type after that.
23132312
error_message = include_state.CheckNextIncludeOrder(
23142313
_ClassifyInclude(fileinfo, include, is_system))
2315-
# if error_message:
2316-
# error(filename, linenum, 'build/include_order', 4,
2317-
# '%s. Should be: %s.h, c system, c++ system, other.' %
2318-
# (error_message, fileinfo.BaseName()))
2314+
if error_message:
2315+
error(filename, linenum, 'build/include_order', 4,
2316+
'%s. Should be: %s.h, c system, c++ system, other.' %
2317+
(error_message, fileinfo.BaseName()))
23192318
if not include_state.IsInAlphabeticalOrder(include):
23202319
error(filename, linenum, 'build/include_alpha', 4,
23212320
'Include "%s" not in alphabetical order' % include)

0 commit comments

Comments
 (0)