Skip to content

A16-2-2: Exclude reporting of nested redundant includes #550

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 3 commits into from
Mar 5, 2024
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/2024-03-04-fix-fp-a16-2-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A16-2-2` - `UnusedIncludeDirectives.ql`:
- Address FP reported in #453. Exclude reporting of redundant include directives indirectly included by included files.
15 changes: 12 additions & 3 deletions cpp/autosar/src/rules/A16-2-2/UnusedIncludeDirectives.ql
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,19 @@ private predicate firstReliableProvide(File f, File g, int line) {

cached
predicate mayProvideFirst(IncludeDepends i, File g) {
// i may provide g and does not come after a reliable include of g.
// i may provide g
i.provides(g) and
not exists(int line | firstReliableProvide(i.getFile(), g, line) |
line < i.getLocation().getStartLine()
(
// and does not come after a reliable include of g.
not exists(int line | firstReliableProvide(i.getFile(), g, line) |
line < i.getLocation().getStartLine()
)
or
// or it comes after a reliable include of g, and although redundant,
// is not necessarily an issue e.g. in the case of libraries with
// public header forwards to an internal header.
// therefore, hold for transitive includes as well to exclude those results.
not i.getIncludedFile() = g
)
}

Expand Down
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A16-2-2/internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void f();
4 changes: 3 additions & 1 deletion cpp/autosar/test/rules/A16-2-2/test.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
void f();
#include "z.h"

void g() { f(); }
7 changes: 7 additions & 0 deletions cpp/autosar/test/rules/A16-2-2/test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "test.hpp" // COMPLIANT
#include "z.h" // COMPLIANT

void test() {
f();
g();
}
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A16-2-2/z.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "internal.h"