Skip to content

Commit 95f439d

Browse files
mbaludaCopilot
andcommitted
DIR-4-10: Report include guards reused across header files
MISRA C:2025 promotes the guard-uniqueness note of Dir 4.10 to a normative amplification. Report header files sharing an include-guard name, even across translation units or file extensions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bc8a0eb commit 95f439d

7 files changed

Lines changed: 44 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `DIR-4-10` (`PrecautionIncludeGuardsNotProvided`), `M16-2-3` (`IncludeGuardsNotProvided`), `RULE-19-2-1` (`NoValidIfdefGuardInHeader`):
2+
- The queries now report header files that reuse an include-guard name, even when they have
3+
different file extensions or are included in separate translation units.

‎cpp/common/src/codingstandards/cpp/rules/includeguardsnotused/IncludeGuardsNotUsed.qll‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provides a library which includes a `problems` predicate for reporting header files without an include guard.
2+
* Provides a `problems` predicate for reporting missing or non-unique header include guards.
33
*/
44

55
import cpp
@@ -11,6 +11,9 @@ abstract class IncludeGuardsNotUsedSharedQuery extends Query { }
1111

1212
Query getQuery() { result instanceof IncludeGuardsNotUsedSharedQuery }
1313

14+
/**
15+
* A header file whose include guard is blocked by another header file.
16+
*/
1417
class BlockedIncludeGuard extends HeaderFile {
1518
CorrectIncludeGuard other;
1619

@@ -20,10 +23,33 @@ class BlockedIncludeGuard extends HeaderFile {
2023
)
2124
}
2225

26+
/** Gets the header file whose include guard prevents this header from being included. */
2327
CorrectIncludeGuard getBlockingIncludeGuard() { result = other }
2428
}
2529

30+
/** A header file that shares its include guard name with another header file. */
31+
class DuplicateIncludeGuard extends CorrectIncludeGuard {
32+
/** Gets the first other header file, by path, that uses the same include guard name. */
33+
CorrectIncludeGuard getDuplicate() {
34+
result =
35+
min(CorrectIncludeGuard o |
36+
o != this and o.getIncludeGuardName() = this.getIncludeGuardName()
37+
|
38+
o order by o.getAbsolutePath()
39+
)
40+
}
41+
}
42+
43+
/**
44+
* Holds if `file` has a missing or non-unique include guard.
45+
* `message` is the alert message, and `other` and `name` identify the linked element.
46+
*/
2647
query predicate problems(HeaderFile file, string message, HeaderFile other, string name) {
48+
not isExcluded(file, getQuery()) and
49+
other = file.(DuplicateIncludeGuard).getDuplicate() and
50+
message = "Header file '" + file.getBaseName() + "' uses an include guard also used by $@." and
51+
name = "header file"
52+
or
2753
not isExcluded(file, getQuery()) and
2854
not file instanceof CorrectIncludeGuard and
2955
if file instanceof BlockedIncludeGuard

‎cpp/common/test/rules/includeguardsnotused/IncludeGuardsNotUsed.expected‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| 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 |
2+
| 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 |
13
| 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 | |
24
| 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 | |
35
| 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 | |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef DIR_4_10_DUPLICATE_GUARD
2+
#define DIR_4_10_DUPLICATE_GUARD
3+
int g8;
4+
#endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef DIR_4_10_DUPLICATE_GUARD
2+
#define DIR_4_10_DUPLICATE_GUARD
3+
int g9;
4+
#endif

‎cpp/common/test/rules/includeguardsnotused/test.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313

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

16-
#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation
16+
#include "headers/excluded/test7.hpp" // COMPLIANT - excluded via deviation
17+
18+
#include "headers/dir410_a.h" // NON_COMPLIANT - shares a guard across translation units
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "headers/dir410_b.hpp" // NON_COMPLIANT - shares a guard across translation units

0 commit comments

Comments
 (0)