diff --git a/change_notes/2022-08-18-dead-code-static-asserts.md b/change_notes/2022-08-18-dead-code-static-asserts.md new file mode 100644 index 0000000000..0e7e8507c8 --- /dev/null +++ b/change_notes/2022-08-18-dead-code-static-asserts.md @@ -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. \ No newline at end of file diff --git a/cpp/autosar/src/rules/M0-1-9/DeadCode.ql b/cpp/autosar/src/rules/M0-1-9/DeadCode.ql index 396fc55238..6d35a019fd 100644 --- a/cpp/autosar/src/rules/M0-1-9/DeadCode.ql +++ b/cpp/autosar/src/rules/M0-1-9/DeadCode.ql @@ -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 @@ -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." diff --git a/cpp/autosar/test/rules/M0-1-9/test.cpp b/cpp/autosar/test/rules/M0-1-9/test.cpp index 57dee51bf6..8dc51c2114 100644 --- a/cpp/autosar/test/rules/M0-1-9/test.cpp +++ b/cpp/autosar/test/rules/M0-1-9/test.cpp @@ -76,5 +76,7 @@ int test_dead_code(int x) { } catch (...) { // NON_COMPLIANT } + static_assert(1); // COMPLIANT + return live5 + live6; // COMPLIANT } \ No newline at end of file