@@ -1547,34 +1547,28 @@ def IsTemplateArgumentList_DB(clean_lines, linenum, pos):
1547
1547
1548
1548
return True
1549
1549
1550
- def OpenExpression (clean_lines , linenum , pos ):
1551
- """If input points to ) or } or ] or >, finds the position that opens it.
1552
-
1553
- If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1554
- linenum/pos that correspond to the closing of the expression.
1555
-
1556
- Essentially a mirror of what CloseExpression does
1550
+ def ForceOpenExpression (clean_lines , linenum , pos , bracket ):
1551
+ """Find an opening bracket matching the specified closing bracket
1557
1552
1558
- TODO(tkiley): could probably be merged with CloseExpression
1553
+ Search starting at the position for a matching closing bracket of the
1554
+ same type as bracket.
1559
1555
1560
1556
Args:
1561
1557
clean_lines: A CleansedLines instance containing the file.
1562
1558
linenum: The number of the line to check.
1563
1559
pos: A position on the line.
1560
+ bracket: The style of bracket to match
1564
1561
1565
1562
Returns:
1566
1563
A tuple (line, linenum, pos) pointer *to* the closing brace, or
1567
1564
(line, len(lines), -1) if we never find a close. Note we ignore
1568
1565
strings and comments when matching; and the line we return is the
1569
1566
'cleansed' line at linenum.
1570
1567
"""
1571
-
1572
1568
line = clean_lines .elided [linenum ]
1573
- if (line [pos ] not in ')}]>' ):
1574
- return (line , clean_lines .NumLines (), - 1 )
1575
1569
1576
1570
# Check first line
1577
- (end_pos , stack ) = FindStartOfExpressionInLine (line , pos , [])
1571
+ (end_pos , stack ) = FindStartOfExpressionInLine (line , pos , [bracket ])
1578
1572
if end_pos > - 1 :
1579
1573
return (line , linenum , end_pos )
1580
1574
@@ -1589,6 +1583,37 @@ def OpenExpression(clean_lines, linenum, pos):
1589
1583
# Did not find end of expression before end of file, give up
1590
1584
return (line , clean_lines .NumLines (), - 1 )
1591
1585
1586
+ def OpenExpression (clean_lines , linenum , pos ):
1587
+ """If input points to ) or } or ] or >, finds the position that opens it.
1588
+
1589
+ If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1590
+ linenum/pos that correspond to the closing of the expression.
1591
+
1592
+ Essentially a mirror of what CloseExpression does
1593
+
1594
+ Calls ForceOpenExpression with the bracket type pointed at
1595
+
1596
+ TODO(tkiley): could probably be merged with CloseExpression
1597
+
1598
+ Args:
1599
+ clean_lines: A CleansedLines instance containing the file.
1600
+ linenum: The number of the line to check.
1601
+ pos: A position on the line.
1602
+
1603
+ Returns:
1604
+ A tuple (line, linenum, pos) pointer *to* the closing brace, or
1605
+ (line, len(lines), -1) if we never find a close. Note we ignore
1606
+ strings and comments when matching; and the line we return is the
1607
+ 'cleansed' line at linenum.
1608
+ """
1609
+
1610
+ line = clean_lines .elided [linenum ]
1611
+ if (line [pos ] not in ')}]>' ):
1612
+ return (line , clean_lines .NumLines (), - 1 )
1613
+ else :
1614
+ return ForceOpenExpression (clean_lines , linenum , pos - 1 , line [pos ])
1615
+
1616
+
1592
1617
def FindEndOfExpressionInLine (line , startpos , stack ):
1593
1618
"""Find the position just after the end of current parenthesized expression.
1594
1619
@@ -3656,6 +3681,48 @@ def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3656
3681
error (filename , linenum , 'whitespace/operators' , 4 ,
3657
3682
'Extra space for operator %s' % match .group (1 ))
3658
3683
3684
+ def CheckPointerReferenceSpacing (filename , clean_lines , linenum , error ):
3685
+ """Checks the */& are attached to variable names rather than types
3686
+
3687
+ A pointer or reference type should have the */& attached to the variable
3688
+ name rather than the type. I.e. a pointer to an int should be:
3689
+
3690
+ int *var_name;
3691
+
3692
+ Args:
3693
+ filename: The name of the current file.
3694
+ clean_lines: A CleansedLines instance containing the file.
3695
+ linenum: The number of the line to check.
3696
+ error: The function to call with any errors found.
3697
+ """
3698
+ line = clean_lines .elided [linenum ]
3699
+
3700
+ # Find types by looking for word_names that are at the start of the line
3701
+ # If there are followed by a * or & (after an optional ' const')
3702
+ # then they appear to be a reference/pointer type with it attached to
3703
+ # the type rather than the variable
3704
+ wrong_type_match = Search (r'^\s*([\w_])+( const)?(?P<type>[&\*])\s*\w' , line )
3705
+
3706
+ if wrong_type_match :
3707
+ # This still could be a false positive, we must
3708
+ # check that we are not inside brackets as then could be just be
3709
+ # operators (multiply and logical AND)
3710
+ pos = wrong_type_match .start (1 )
3711
+ _ , _ , opening_pos = ForceOpenExpression (clean_lines , linenum , pos , ')' )
3712
+
3713
+ # If we don't find a matching close brace then we aren't in brackets
3714
+ # so can assume this is a type with the * or & attached to it
3715
+ if opening_pos == - 1 :
3716
+ op_type = wrong_type_match .group ('type' )
3717
+ op_word = ""
3718
+ if op_type == '*' :
3719
+ op_word = 'Pointer'
3720
+ else :
3721
+ op_word = 'Reference'
3722
+ error (filename , linenum , 'whitespace/operators' , 4 ,
3723
+ op_word + ' type name must have ' + op_type + ' attached to the type name' )
3724
+
3725
+
3659
3726
3660
3727
def CheckParenthesisSpacing (filename , clean_lines , linenum , error ):
3661
3728
"""Checks for horizontal spacing around parentheses.
@@ -4671,6 +4738,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4671
4738
CheckAccess (filename , clean_lines , linenum , nesting_state , error )
4672
4739
CheckSpacing (filename , clean_lines , linenum , nesting_state , error )
4673
4740
CheckOperatorSpacing (filename , clean_lines , linenum , error )
4741
+ CheckPointerReferenceSpacing (filename , clean_lines , linenum , error )
4674
4742
CheckParenthesisSpacing (filename , clean_lines , linenum , error )
4675
4743
CheckCommaSpacing (filename , clean_lines , linenum , error )
4676
4744
CheckBracesSpacing (filename , clean_lines , linenum , nesting_state , error )
0 commit comments