diff --git a/change_notes/2026-10-19-expand-dir-4-10.md b/change_notes/2026-10-19-expand-dir-4-10.md new file mode 100644 index 000000000..376701220 --- /dev/null +++ b/change_notes/2026-10-19-expand-dir-4-10.md @@ -0,0 +1,3 @@ +- `DIR-4-10` (`PrecautionIncludeGuardsNotProvided`), `M16-2-3` (`IncludeGuardsNotProvided`), `RULE-19-2-1` (`NoValidIfdefGuardInHeader`): + - The queries now report header files that reuse an include-guard name, even when they have + different file extensions or are included in separate translation units. diff --git a/cpp/common/src/codingstandards/cpp/rules/includeguardsnotused/IncludeGuardsNotUsed.qll b/cpp/common/src/codingstandards/cpp/rules/includeguardsnotused/IncludeGuardsNotUsed.qll index fefe57b31..2e31f872a 100644 --- a/cpp/common/src/codingstandards/cpp/rules/includeguardsnotused/IncludeGuardsNotUsed.qll +++ b/cpp/common/src/codingstandards/cpp/rules/includeguardsnotused/IncludeGuardsNotUsed.qll @@ -1,5 +1,5 @@ /** - * Provides a library which includes a `problems` predicate for reporting header files without an include guard. + * Provides a `problems` predicate for reporting missing or non-unique header include guards. */ import cpp @@ -11,6 +11,9 @@ abstract class IncludeGuardsNotUsedSharedQuery extends Query { } Query getQuery() { result instanceof IncludeGuardsNotUsedSharedQuery } +/** + * A header file whose include guard is blocked by another header file. + */ class BlockedIncludeGuard extends HeaderFile { CorrectIncludeGuard other; @@ -20,10 +23,33 @@ class BlockedIncludeGuard extends HeaderFile { ) } + /** Gets the header file whose include guard prevents this header from being included. */ CorrectIncludeGuard getBlockingIncludeGuard() { result = other } } +/** A header file that shares its include guard name with another header file. */ +class DuplicateIncludeGuard extends CorrectIncludeGuard { + /** Gets the first other header file, by path, that uses the same include guard name. */ + CorrectIncludeGuard getDuplicate() { + result = + min(CorrectIncludeGuard o | + o != this and o.getIncludeGuardName() = this.getIncludeGuardName() + | + o order by o.getAbsolutePath() + ) + } +} + +/** + * Holds if `file` has a missing or non-unique include guard. + * `message` is the alert message, and `other` and `name` identify the linked element. + */ query predicate problems(HeaderFile file, string message, HeaderFile other, string name) { + not isExcluded(file, getQuery()) and + other = file.(DuplicateIncludeGuard).getDuplicate() and + message = "Header file '" + file.getBaseName() + "' uses an include guard also used by $@." and + name = "header file" + or not isExcluded(file, getQuery()) and not file instanceof CorrectIncludeGuard and if file instanceof BlockedIncludeGuard diff --git a/cpp/common/test/rules/includeguardsnotused/IncludeGuardsNotUsed.expected b/cpp/common/test/rules/includeguardsnotused/IncludeGuardsNotUsed.expected index dda204cd1..0d100de9c 100644 --- a/cpp/common/test/rules/includeguardsnotused/IncludeGuardsNotUsed.expected +++ b/cpp/common/test/rules/includeguardsnotused/IncludeGuardsNotUsed.expected @@ -1,3 +1,5 @@ +| headers/dir410_a.h:0:0:0:0 | headers/dir410_a.h | Header file 'dir410_a.h' uses an include guard also used by $@. | headers/dir410_b.hpp:0:0:0:0 | headers/dir410_b.hpp | header file | +| headers/dir410_b.hpp:0:0:0:0 | headers/dir410_b.hpp | Header file 'dir410_b.hpp' uses an include guard also used by $@. | headers/dir410_a.h:0:0:0:0 | headers/dir410_a.h | header file | | headers/test3.hpp:0:0:0:0 | headers/test3.hpp | Header file test3.hpp is missing expected include guard. | headers/test3.hpp:0:0:0:0 | headers/test3.hpp | | | headers/test4.hpp:0:0:0:0 | headers/test4.hpp | Header file test4.hpp is missing expected include guard. | headers/test4.hpp:0:0:0:0 | headers/test4.hpp | | | headers/test5.hpp:0:0:0:0 | headers/test5.hpp | Header file test5.hpp is missing expected include guard. | headers/test5.hpp:0:0:0:0 | headers/test5.hpp | | diff --git a/cpp/common/test/rules/includeguardsnotused/headers/dir410_a.h b/cpp/common/test/rules/includeguardsnotused/headers/dir410_a.h new file mode 100644 index 000000000..3e2222679 --- /dev/null +++ b/cpp/common/test/rules/includeguardsnotused/headers/dir410_a.h @@ -0,0 +1,4 @@ +#ifndef DIR_4_10_DUPLICATE_GUARD +#define DIR_4_10_DUPLICATE_GUARD +int g8; +#endif diff --git a/cpp/common/test/rules/includeguardsnotused/headers/dir410_b.hpp b/cpp/common/test/rules/includeguardsnotused/headers/dir410_b.hpp new file mode 100644 index 000000000..5905c4bfe --- /dev/null +++ b/cpp/common/test/rules/includeguardsnotused/headers/dir410_b.hpp @@ -0,0 +1,4 @@ +#ifndef DIR_4_10_DUPLICATE_GUARD +#define DIR_4_10_DUPLICATE_GUARD +int g9; +#endif diff --git a/cpp/common/test/rules/includeguardsnotused/test.cpp b/cpp/common/test/rules/includeguardsnotused/test.cpp index 82170197f..7a8b69f15 100644 --- a/cpp/common/test/rules/includeguardsnotused/test.cpp +++ b/cpp/common/test/rules/includeguardsnotused/test.cpp @@ -13,4 +13,6 @@ #include "headers/test7.hpp" //NON_COMPLIANT - missing include guard, not excluded -#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation \ No newline at end of file +#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation + +#include "headers/dir410_a.h" // NON_COMPLIANT - shares a guard across translation units \ No newline at end of file diff --git a/cpp/common/test/rules/includeguardsnotused/test2.cpp b/cpp/common/test/rules/includeguardsnotused/test2.cpp new file mode 100644 index 000000000..31a2fafbd --- /dev/null +++ b/cpp/common/test/rules/includeguardsnotused/test2.cpp @@ -0,0 +1 @@ +#include "headers/dir410_b.hpp" // NON_COMPLIANT - shares a guard across translation units