From 28606c561d6e69d1d2a93bb6ddc35174bf632bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nora=20Dimitrijevi=C4=87?= Date: Fri, 30 Sep 2022 14:35:54 +0200 Subject: [PATCH] C++: Simplify normalizeExpr This has a comparable but different set of FPs as the previous version. But arguably it's an improvement. --- .../CommaBeforeMisleadingIndentation.ql | 31 +++++-------------- .../CommaBeforeMisleadingIndentation.expected | 2 +- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql b/cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql index 66446a5dce8..35ab00a8fdd 100644 --- a/cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql +++ b/cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql @@ -12,32 +12,15 @@ import cpp import semmle.code.cpp.commons.Exclusions -/** - * Holds if `te` is an implicit `this`. - * - * ThisExpr.isCompilerGenerated() is currently not being extracted, so use a heuristic. - */ -predicate isCompilerGenerated(ThisExpr te) { - exists(int line, int colStart, int colEnd | - te.getLocation().hasLocationInfo(_, line, colStart, line, colEnd) - | - colStart = colEnd - or - exists(Call c | c.getQualifier() = te | c.getLocation() = te.getLocation()) - ) -} - /** Gets the sub-expression of 'e' with the earliest-starting Location */ Expr normalizeExpr(Expr e) { - if forex(Expr q | q = e.(Call).getQualifier() | not isCompilerGenerated(q)) - then result = normalizeExpr(e.(Call).getQualifier()) - else - if forex(Expr q | q = e.(FieldAccess).getQualifier() | not isCompilerGenerated(q)) - then result = normalizeExpr(e.(FieldAccess).getQualifier()) - else - if e.hasExplicitConversion() - then result = normalizeExpr(e.getFullyConverted()) - else result = e + result = + min(Expr child | + child.getParentWithConversions*() = e.getFullyConverted() and + not child.getParentWithConversions*() = any(Call c).getAnArgument() + | + child order by child.getLocation().getStartColumn(), count(child.getParentWithConversions*()) + ) } predicate isParenthesized(CommaExpr ce) { diff --git a/cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/CommaBeforeMisleadingIndentation.expected b/cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/CommaBeforeMisleadingIndentation.expected index 93911c1d9bb..0cc6e7f4fc4 100644 --- a/cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/CommaBeforeMisleadingIndentation.expected +++ b/cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/CommaBeforeMisleadingIndentation.expected @@ -1,5 +1,5 @@ | test.cpp:49:2:49:8 | (void)... | The indentation level may be misleading (for some tab sizes). | -| test.cpp:52:8:52:8 | x | The indentation level may be misleading (for some tab sizes). | +| test.cpp:52:2:52:15 | (void)... | The indentation level may be misleading (for some tab sizes). | | test.cpp:160:3:160:9 | (void)... | The indentation level may be misleading (for some tab sizes). | | test.cpp:166:5:166:7 | ... ++ | The indentation level may be misleading (for some tab sizes). | | test.cpp:176:6:178:6 | ... ? ... : ... | The indentation level may be misleading (for some tab sizes). |