Skip to content

Commit 6d5df14

Browse files
committed
C++: Remove arguable FPs re: sizeof/decltype
1 parent 592bc18 commit 6d5df14

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import cpp
1212
import semmle.code.cpp.commons.Exclusions
1313

14+
/** Gets the sub-expression of 'e' with the earliest-starting Location */
1415
Expr normalizeExpr(Expr e) {
1516
if exists(e.(Call).getQualifier())
1617
then result = normalizeExpr(e.(Call).getQualifier())
@@ -26,6 +27,12 @@ predicate isInLoopHead(CommaExpr ce) {
2627
ce.getEnclosingStmt() = any(ForStmt f).getInitialization()
2728
}
2829

30+
predicate isInDecltypeOrSizeof(CommaExpr ce) {
31+
ce.getParent*() instanceof SizeofExprOperator
32+
or
33+
ce.getParent*() = any(Decltype d).getExpr()
34+
}
35+
2936
from CommaExpr ce, Expr left, Expr right, Location leftLoc, Location rightLoc
3037
where
3138
ce.fromSource() and
@@ -34,7 +41,8 @@ where
3441
right = normalizeExpr(ce.getRightOperand()) and
3542
leftLoc = left.getLocation() and
3643
rightLoc = right.getLocation() and
37-
not isInLoopHead(ce) and // HACK to reduce FPs in loop heads; assumption: unlikely to be misread due to '(', ')' delimiters
44+
not isInLoopHead(ce) and // <- HACK to reduce FPs in loop heads; assumption: unlikely to be misread due to '(', ')' delimiters
45+
not isInDecltypeOrSizeof(ce) and // <- Removes arguable FPs since, like function calls (and loop heads), these Exprs have clear delimiters.
3846
leftLoc.getEndLine() < rightLoc.getStartLine() and
3947
leftLoc.getStartColumn() > rightLoc.getStartColumn()
4048
select right, "The indentation after the comma may be misleading (for some tab sizes)."

cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
7777
j++);
7878
(void)sizeof(x.foo(i++), // GOOD
7979
j++);
80+
using U = decltype(x.foo(i++), // GOOD? Unlikely to be misread
81+
j++);
82+
(void)sizeof(x.foo(i++), // GOOD? Unlikely to be misread
83+
j++);
8084

8185
// Comma in loops
8286

@@ -86,6 +90,11 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
8690
i = j = i + j;
8791
}
8892

93+
while (i = foo(j++), // GOOD??? Currently ignoring loop heads
94+
i != j && i != 42 && !foo(j)) {
95+
i = j = i + j;
96+
}
97+
8998
for (i = 0, // GOOD? Currently ignoring loop heads.
9099
j = 1;
91100
i + j < 10;

0 commit comments

Comments
 (0)