diff --git a/change_notes/2024-03-04-fix-fp-a16-2-2.md b/change_notes/2024-03-04-fix-fp-a16-2-2.md new file mode 100644 index 0000000000..79ff54eea1 --- /dev/null +++ b/change_notes/2024-03-04-fix-fp-a16-2-2.md @@ -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. diff --git a/cpp/autosar/src/rules/A16-2-2/UnusedIncludeDirectives.ql b/cpp/autosar/src/rules/A16-2-2/UnusedIncludeDirectives.ql index 9b536b78b3..ce51602fd2 100644 --- a/cpp/autosar/src/rules/A16-2-2/UnusedIncludeDirectives.ql +++ b/cpp/autosar/src/rules/A16-2-2/UnusedIncludeDirectives.ql @@ -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 ) } diff --git a/cpp/autosar/test/rules/A16-2-2/internal.h b/cpp/autosar/test/rules/A16-2-2/internal.h new file mode 100644 index 0000000000..6eb06a9a27 --- /dev/null +++ b/cpp/autosar/test/rules/A16-2-2/internal.h @@ -0,0 +1 @@ +void f(); \ No newline at end of file diff --git a/cpp/autosar/test/rules/A16-2-2/test.hpp b/cpp/autosar/test/rules/A16-2-2/test.hpp index 6eb06a9a27..a6c63f5413 100644 --- a/cpp/autosar/test/rules/A16-2-2/test.hpp +++ b/cpp/autosar/test/rules/A16-2-2/test.hpp @@ -1 +1,3 @@ -void f(); \ No newline at end of file +#include "z.h" + +void g() { f(); } \ No newline at end of file diff --git a/cpp/autosar/test/rules/A16-2-2/test2.cpp b/cpp/autosar/test/rules/A16-2-2/test2.cpp new file mode 100644 index 0000000000..6a4e01987d --- /dev/null +++ b/cpp/autosar/test/rules/A16-2-2/test2.cpp @@ -0,0 +1,7 @@ +#include "test.hpp" // COMPLIANT +#include "z.h" // COMPLIANT + +void test() { + f(); + g(); +} \ No newline at end of file diff --git a/cpp/autosar/test/rules/A16-2-2/z.h b/cpp/autosar/test/rules/A16-2-2/z.h new file mode 100644 index 0000000000..22080b7884 --- /dev/null +++ b/cpp/autosar/test/rules/A16-2-2/z.h @@ -0,0 +1 @@ +#include "internal.h" \ No newline at end of file