From 632e9dfa03cf0cc7e53b0e131c3e0f01e33b308f Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 09:51:41 +0100 Subject: [PATCH 1/3] DeadCode: Add failing test case for static_asserts static_assert generates a DeclStmt which is considered dead. --- cpp/autosar/test/rules/M0-1-9/test.cpp | 2 ++ 1 file changed, 2 insertions(+) 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 From c24c9a9e0802bc22139868f16a30bd3e2e3c90b8 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 09:52:25 +0100 Subject: [PATCH 2/3] DeadCode: Exclude more generated statements M0-1-9 now excludes more generated statements, including those marked as compiler generated, and DeclStmts generated from static_asserts. --- cpp/autosar/src/rules/M0-1-9/DeadCode.ql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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." From dbe4bdeedd6dab957e73dd7b3eff3ea06c613d46 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 09:57:14 +0100 Subject: [PATCH 3/3] DeadCode: Change note for excluding compiler generated statements --- change_notes/2022-08-18-dead-code-static-asserts.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 change_notes/2022-08-18-dead-code-static-asserts.md 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