Skip to content

M0-1-9: Exclude more compiler generated statements #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2022-08-18-dead-code-static-asserts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M0-1-9` - `DeadCode.ql`:
- More compiler generated statements are now excluded from being reported as dead code, including compiler generated statements for `static_assert` calls.
8 changes: 6 additions & 2 deletions cpp/autosar/src/rules/M0-1-9/DeadCode.ql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ predicate isDeadStmt(Stmt s) {
// - The initializers for each of the variables are pure
exists(DeclStmt ds |
ds = s and
forall(Declaration d | d = ds.getADeclaration() |
// Use forex so that we don't flag "fake" generated `DeclStmt`s (e.g. those generated by the
// extractor for static_asserts) with no actual declarations
forex(Declaration d | d = ds.getADeclaration() |
exists(LocalScopeVariable v |
d = v and
v.getInitializer().getExpr().isPure() and
Expand Down Expand Up @@ -123,5 +125,7 @@ where
// output". We therefore exclude unreachable statements as they are, by definition, not executed.
not s.getBasicBlock() = any(UnreachableBasicBlock ubb).getABasicBlock() and
// Exclude code generated by macros, because the code may be "live" in other instantiations
not s.isAffectedByMacro()
not s.isAffectedByMacro() and
// Exclude compiler generated statements
not s.isCompilerGenerated()
select s, "This statement is dead code."
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/M0-1-9/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ int test_dead_code(int x) {
} catch (...) { // NON_COMPLIANT
}

static_assert(1); // COMPLIANT

return live5 + live6; // COMPLIANT
}