1616import cpp
1717import semmle.code.cpp.commons.Exclusions
1818
19- /** Gets the sub-expression of 'e' with the earliest-starting Location */
19+ /**
20+ * Gets a child of `e`, including conversions but excluding call arguments.
21+ */
22+ pragma [ inline]
23+ Expr getAChildWithConversions ( Expr e ) {
24+ result .getParentWithConversions ( ) = e and
25+ not result = any ( Call c ) .getAnArgument ( )
26+ }
27+
28+ /**
29+ * Gets the left-most column position of any transitive child of `e` (including
30+ * conversions but excluding call arguments).
31+ */
32+ int getCandidateColumn ( Expr e ) {
33+ result = e .getLocation ( ) .getStartColumn ( ) or
34+ result = getCandidateColumn ( getAChildWithConversions ( e ) )
35+ }
36+
37+ /**
38+ * Gets the transitive child of `e` (including conversions but excluding call
39+ * arguments) at the left-most column position, preferring less deeply nested
40+ * expressions if there is a choice.
41+ */
2042Expr normalizeExpr ( Expr e ) {
21- result =
22- min ( Expr child |
23- child .getParentWithConversions * ( ) = e .getFullyConverted ( ) and
24- not child .getParentWithConversions * ( ) = any ( Call c ) .getAnArgument ( )
25- |
26- child order by child .getLocation ( ) .getStartColumn ( ) , count ( child .getParentWithConversions * ( ) )
27- )
43+ e .getLocation ( ) .getStartColumn ( ) = min ( getCandidateColumn ( e ) ) and
44+ result = e
45+ or
46+ not e .getLocation ( ) .getStartColumn ( ) = min ( getCandidateColumn ( e ) ) and
47+ result = normalizeExpr ( getAChildWithConversions ( e ) ) and
48+ result .getLocation ( ) .getStartColumn ( ) = min ( getCandidateColumn ( e ) )
2849}
2950
3051predicate isParenthesized ( CommaExpr ce ) {
@@ -43,8 +64,8 @@ from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
4364where
4465 ce .fromSource ( ) and
4566 not isFromMacroDefinition ( ce ) and
46- left = normalizeExpr ( ce .getLeftOperand ( ) ) and
47- right = normalizeExpr ( ce .getRightOperand ( ) ) and
67+ left = normalizeExpr ( ce .getLeftOperand ( ) . getFullyConverted ( ) ) and
68+ right = normalizeExpr ( ce .getRightOperand ( ) . getFullyConverted ( ) ) and
4869 leftLoc = left .getLocation ( ) and
4970 rightLoc = right .getLocation ( ) and
5071 not isParenthesized ( ce ) and
0 commit comments