16
16
import cpp
17
17
import semmle.code.cpp.commons.Exclusions
18
18
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
+ */
20
42
Expr 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 ) )
28
49
}
29
50
30
51
predicate isParenthesized ( CommaExpr ce ) {
@@ -43,8 +64,8 @@ from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
43
64
where
44
65
ce .fromSource ( ) and
45
66
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
48
69
leftLoc = left .getLocation ( ) and
49
70
rightLoc = right .getLocation ( ) and
50
71
not isParenthesized ( ce ) and
0 commit comments