@@ -1384,12 +1384,61 @@ def FindNextMultiLineCommentEnd(lines, lineix):
1384
1384
return len (lines )
1385
1385
1386
1386
1387
- def RemoveMultiLineCommentsFromRange (lines , begin , end ):
1388
- """Clears a range of lines for multi-line comments."""
1389
- # Having // dummy comments makes the lines non-empty, so we will not get
1390
- # unnecessary blank line warnings later in the code.
1387
+ def ReplaceMultiLineCommentsInRange (filename , lines , begin , end , error ):
1388
+ """Replaces multi-line C-style comments with line-by-line C++ comments.
1389
+ This enables the existing line-by-line comment linting to take effect.
1390
+ Retains empty lines, line length, and indentation to the extent practical.
1391
+ """
1392
+ assert begin + 1 <= end - 1 , "The range must be multi-line"
1393
+ # Find the common whitespace prefix for all lines.
1394
+ prefix , _ = lines [begin ].split ('/*' , 2 )
1395
+ if len (prefix .strip ()) > 0 :
1396
+ error (filename , i , 'readability/multiline_comment' , 5 ,
1397
+ 'Multi-line comments should start on a dedicated line' )
1398
+ return
1399
+ # Rewrite each line to C++ comment instead of C comment.
1391
1400
for i in range (begin , end ):
1392
- lines [i ] = '/**/'
1401
+ line = lines [i ]
1402
+ # Handle the first line specially.
1403
+ # Change "/* Foo" to "// Foo", "/** Bar" to "/// Bar".
1404
+ if i == begin :
1405
+ lines [i ] = line .replace ('*' , '/' )
1406
+ continue
1407
+ # Leave interior blank lines untouched.
1408
+ if len (line ) == 0 :
1409
+ continue
1410
+ # Check for the common whitespace prefix.
1411
+ if not line .startswith (prefix ):
1412
+ error (filename , i , 'readability/multiline_comment' , 5 ,
1413
+ 'Inconsistent indentation on multi-line comment' )
1414
+ continue
1415
+ suffix = line [len (prefix ):]
1416
+ # Handle the last line specially.
1417
+ if i == (end - 1 ):
1418
+ # Check that there is nothing after the comment close.
1419
+ _ , after = suffix .split ('*/' , 2 )
1420
+ if after :
1421
+ error (filename , i , 'readability/multiline_comment' , 5 ,
1422
+ 'There should not be anything after the */ that closes a '
1423
+ 'multi-line comment' )
1424
+ continue
1425
+ # Change "Foo */" to "Foo ./" and then treat it like a middle line.
1426
+ suffix = suffix .replace ('*' , '.' )
1427
+ # Add a leading '//' to non-initial lines.
1428
+ if len (prefix ) > 4 :
1429
+ # We can overwrite the prefix while retaining 2-space indent.
1430
+ lines [i ] = prefix [:- 4 ] + '// ' + suffix
1431
+ elif len (suffix ) < 3 :
1432
+ # The line is trivially short. Not much we can do.
1433
+ lines [i ] = '/' * len (line )
1434
+ else :
1435
+ # Shorten the comment by deleting 3 letters in the middle. This
1436
+ # preserves the end of line (for trailing whitespace complaints)
1437
+ # and the start of line (for http URLs to still look like URLs).
1438
+ mid = len (suffix ) // 2
1439
+ new_suffix = suffix [:mid - 2 ] + suffix [mid + 1 :]
1440
+ # Insert the comment marker and then the trimmed suffix.
1441
+ lines [i ] = prefix + '// ' + new_suffix
1393
1442
1394
1443
1395
1444
def RemoveMultiLineComments (filename , lines , error ):
@@ -1404,7 +1453,8 @@ def RemoveMultiLineComments(filename, lines, error):
1404
1453
error (filename , lineix_begin + 1 , 'readability/multiline_comment' , 5 ,
1405
1454
'Could not find end of multi-line comment' )
1406
1455
return
1407
- RemoveMultiLineCommentsFromRange (lines , lineix_begin , lineix_end + 1 )
1456
+ ReplaceMultiLineCommentsInRange (
1457
+ filename , lines , lineix_begin , lineix_end + 1 , error )
1408
1458
lineix = lineix_end + 1
1409
1459
1410
1460
@@ -4418,11 +4468,15 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4418
4468
# URLs can be long too. It's possible to split these, but it makes them
4419
4469
# harder to cut&paste.
4420
4470
#
4471
+ # Tables of data (1.0 | 2.0 | 3.0 | ...) are sometimes easier grok as long
4472
+ # lines, so we pass any comment with at least two " | ".
4473
+ #
4421
4474
# The "$Id:...$" comment may also get very long without it being the
4422
4475
# developers fault.
4423
4476
if (not line .startswith ('#include' ) and not is_header_guard and
4424
4477
not Match (r'^\s*//.*http(s?)://\S*$' , line ) and
4425
4478
not Match (r'^\s*//\s*[^\s]*$' , line ) and
4479
+ not Match (r'^\s*//(.* \| ){2,}' , line ) and
4426
4480
not Match (r'^// \$Id:.*#[0-9]+ \$$' , line )):
4427
4481
line_width = GetLineWidth (line )
4428
4482
if line_width > _line_length :
0 commit comments