553
553
# This is set by --headers flag.
554
554
_hpp_headers = set (['h' ])
555
555
556
+ # Source filename extensions
557
+ _cpp_extensions = set (['cc' , 'mm' ])
558
+
556
559
# {str, bool}: a map from error categories to booleans which indicate if the
557
560
# category should be suppressed for every line.
558
561
_global_error_suppressions = {}
@@ -569,6 +572,15 @@ def ProcessHppHeadersOption(val):
569
572
def IsHeaderExtension (file_extension ):
570
573
return file_extension in _hpp_headers
571
574
575
+ def IsSourceExtension (file_extension ):
576
+ return file_extension in _cpp_extensions
577
+
578
+ def IsSourceFilename (filename ):
579
+ global _cpp_extensions
580
+ ext = os .path .splitext (filename )[- 1 ].lower ()
581
+ ext = ext [1 :] # leading dot
582
+ return IsSourceExtension (ext )
583
+
572
584
def ParseNolintSuppressions (filename , raw_line , linenum , error ):
573
585
"""Updates the global list of line error-suppressions.
574
586
@@ -4579,7 +4591,7 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4579
4591
error (filename , linenum , 'build/include' , 4 ,
4580
4592
'"%s" already included at %s:%s' %
4581
4593
(include , filename , duplicate_line ))
4582
- elif (include . endswith ( '.cc' ) and
4594
+ elif (IsSourceFilename ( include ) and
4583
4595
os .path .dirname (fileinfo .RepositoryName ()) != os .path .dirname (include )):
4584
4596
error (filename , linenum , 'build/include' , 4 ,
4585
4597
'Do not include .cc files from other packages' )
@@ -5390,6 +5402,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
5390
5402
('<map>' , ('map' , 'multimap' ,)),
5391
5403
('<memory>' , ('allocator' , 'make_shared' , 'make_unique' , 'shared_ptr' ,
5392
5404
'unique_ptr' , 'weak_ptr' )),
5405
+ ('<ostream>' , ('ostream' ,)),
5393
5406
('<queue>' , ('queue' , 'priority_queue' ,)),
5394
5407
('<set>' , ('set' , 'multiset' ,)),
5395
5408
('<stack>' , ('stack' ,)),
@@ -5415,6 +5428,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
5415
5428
)
5416
5429
5417
5430
_RE_PATTERN_STRING = re .compile (r'\bstring\b' )
5431
+ _RE_PATTERN_OSTREAM = re .compile (r'\bostream\b' )
5418
5432
5419
5433
_re_pattern_headers_maybe_templates = []
5420
5434
for _header , _templates in _HEADERS_MAYBE_TEMPLATES :
@@ -5553,6 +5567,14 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5553
5567
if prefix .endswith ('std::' ) or not prefix .endswith ('::' ):
5554
5568
required ['<string>' ] = (linenum , 'string' )
5555
5569
5570
+ # Ostream is special too -- also non-templatized
5571
+ matched = _RE_PATTERN_OSTREAM .search (line )
5572
+ if matched :
5573
+ if IsSourceFilename (filename ):
5574
+ required ['<ostream>' ] = (linenum , 'ostream' )
5575
+ else :
5576
+ required ['<iosfwd>' ] = (linenum , 'ostream' )
5577
+
5556
5578
for pattern , template , header in _re_pattern_headers_maybe_templates :
5557
5579
if pattern .search (line ):
5558
5580
required [header ] = (linenum , template )
@@ -5605,7 +5627,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5605
5627
# didn't include it in the .h file.
5606
5628
# TODO(unknown): Do a better job of finding .h files so we are confident that
5607
5629
# not having the .h file means there isn't one.
5608
- if filename . endswith ( '.cc' ) and not header_found :
5630
+ if IsSourceFilename ( filename ) and not header_found :
5609
5631
return
5610
5632
5611
5633
# All the lines have been processed, report the errors found.
0 commit comments