Skip to content
Draft
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
3 changes: 3 additions & 0 deletions change_notes/2026-10-19-expand-dir-4-10.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 | |
Expand Down
4 changes: 4 additions & 0 deletions cpp/common/test/rules/includeguardsnotused/headers/dir410_a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef DIR_4_10_DUPLICATE_GUARD
#define DIR_4_10_DUPLICATE_GUARD
int g8;
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef DIR_4_10_DUPLICATE_GUARD
#define DIR_4_10_DUPLICATE_GUARD
int g9;
#endif
4 changes: 3 additions & 1 deletion cpp/common/test/rules/includeguardsnotused/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@

#include "headers/test7.hpp" //NON_COMPLIANT - missing include guard, not excluded

#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation
#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation

#include "headers/dir410_a.h" // NON_COMPLIANT - shares a guard across translation units
1 change: 1 addition & 0 deletions cpp/common/test/rules/includeguardsnotused/test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "headers/dir410_b.hpp" // NON_COMPLIANT - shares a guard across translation units
Loading