Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
be7f810
Add module for names from the C/C++ library
lcartey Mar 8, 2024
1554d78
Add mad generator for the C Standard Library
lcartey Mar 8, 2024
057c684
Add mad generator for the C++ Standard Library
lcartey Mar 8, 2024
bf715b1
Fix output locations
lcartey Mar 8, 2024
af00c9f
Update models file
lcartey Mar 8, 2024
ea7e3b5
C: Support pointer return types in library generator
lcartey Mar 12, 2024
e21f6d3
Add missing update
lcartey Mar 12, 2024
cae5660
Add manual member variable models for C11 and C99.
lcartey Mar 12, 2024
189d66f
Expose libraryMemberVariableModels
lcartey Mar 13, 2024
fcbe4e7
Adopt StandardLibraryNames in the C DeclaredAReservedIdentifier query
lcartey Mar 13, 2024
5f738b3
Update message, add additional tests
lcartey Mar 13, 2024
221c0c1
Refine detection of reserved identifiers
lcartey Mar 13, 2024
ca51fda
Correctly reflect tag and typedef name spaces
lcartey Mar 13, 2024
122a020
Exclude identifiers generated from library macros
lcartey Mar 13, 2024
172378f
Only include macro names when the relevant header is included
lcartey Mar 13, 2024
b91d527
Improve performance on large databases
lcartey Mar 13, 2024
1fd72e2
Migrate STR32-C to new naming library
lcartey Mar 14, 2024
f5210ac
Extract ReservedNames library.
lcartey Mar 14, 2024
92cfb2f
Exclude NDEBUG, regenerate
lcartey Mar 14, 2024
6998ed0
Migrate Rule 21.2 to the ReservedNames library
lcartey Mar 14, 2024
927640b
Unshared Rule 21.2, and implement with MISRA rules
lcartey Mar 14, 2024
a821de9
Replace Naming with StandardLibraryNames
lcartey Mar 14, 2024
7c94c8c
Migrate A17-1-1 to use StandardLibraryNames
lcartey Mar 15, 2024
c3977f2
Migrate M17-0-2 to StandardLibraryNames.
lcartey Mar 15, 2024
b2ea30a
C++: More accurately report declaring header
lcartey Mar 25, 2024
d18ada0
C++: Remove unnamed and specialization types
lcartey Mar 25, 2024
a9a4613
C++: Exclude operator bool from the reserved names
lcartey Mar 25, 2024
bbd56e0
C++: Exclude member names unless the declaring type is visible
lcartey Mar 26, 2024
384b245
C++: Exclude name-header mappings for specializations
lcartey Mar 26, 2024
6ca9153
Add a predicate for identifying any name in a standard
lcartey Mar 28, 2024
cb5ddf5
Support C++ in reserved names
lcartey Mar 28, 2024
740c76d
Migrate A17-0-1 to use ReservedName
lcartey Mar 28, 2024
7a1eeb2
M17-0-3: Migrate to StandardNaming
lcartey Aug 28, 2025
deb849b
A17-0-1: Accept output
lcartey Aug 28, 2025
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
Prev Previous commit
Next Next commit
Migrate Rule 21.2 to the ReservedNames library
  • Loading branch information
lcartey committed Aug 27, 2025
commit 6998ed05d9cfe5d0023dc497282476dd90b8d26c
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,10 @@

import cpp
import codingstandards.c.misra
import codingstandards.cpp.Naming
import codingstandards.cpp.ReservedNames

from PreprocessorDirective p, string name
from PreprocessorDirective p, string message
where
not isExcluded(p, Preprocessor4Package::defineAndUndefUsedOnReservedIdentifierOrMacroNameQuery()) and
(
p.(Macro).hasName(name)
or
p.(PreprocessorUndef).getName() = name
) and
(
Naming::Cpp14::hasStandardLibraryMacroName(name)
or
Naming::Cpp14::hasStandardLibraryObjectName(name)
or
name.regexpMatch("_.*")
or
name = "defined"
)
select p, "Reserved identifier '" + name + "' has been undefined or redefined."
ReservedNames::C11::isAReservedIdentifier(p, message, false)
select p, message
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
| test.c:1:1:1:17 | #define _NOT_OKAY | Reserved identifier '_NOT_OKAY' has been undefined or redefined. |
| test.c:2:1:2:16 | #undef _NOT_OKAY | Reserved identifier '_NOT_OKAY' has been undefined or redefined. |
| test.c:5:1:5:13 | #define errno | Reserved identifier 'errno' has been undefined or redefined. |
| test.c:1:1:1:17 | #define _NOT_OKAY | Macro '_NOT_OKAY' declares a reserved name beginning _ followed by an uppercase letter. |
| test.c:2:1:2:16 | #undef _NOT_OKAY | Undef '_NOT_OKAY' declares a reserved name beginning _ followed by an uppercase letter. |
| test.c:5:1:5:13 | #define errno | Macro 'errno' declares a name reserved for a macro from the C11 standard library header 'errno.h'. |
21 changes: 16 additions & 5 deletions cpp/common/src/codingstandards/cpp/ReservedNames.qll
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ module ReservedNames {
scope = MacroScope() and
cNameSpace = MacroNameSpace() and
identifierDescription = "Macro parameter"
or
e.(PreprocessorUndef).getName() = identifierName and
scope = MacroScope() and
cNameSpace = MacroNameSpace() and
identifierDescription = "Undef"
)
}

Expand All @@ -148,7 +153,13 @@ module ReservedNames {

module TargetedCLibrary = CStandardLibrary::C11;

predicate isAReservedIdentifier(Element m, string message) {
/**
* Holds if the given C program element is a reserved identifier according to the C11 standard or MISRA.
*
* @param requireHeaderIncluded false if we don't require
*/
predicate isAReservedIdentifier(Element m, string message, boolean requireHeaderIncluded) {
requireHeaderIncluded = [true, false] and
exists(
string name, Scope scope, CNameSpace cNameSpace, string reason, string identifierDescription
|
Expand Down Expand Up @@ -187,8 +198,8 @@ module ReservedNames {
// > unless explicitly stated otherwise (see 7.1.4).
exists(string header |
TargetedCLibrary::hasMacroName(header, name, _) and
// The relevant header is included directly or transitively by the file
m.getFile().getAnIncludedFile*().getBaseName() = header and
// The relevant header is included directly or transitively by the file, or we don't apply that requirement
(m.getFile().getAnIncludedFile*().getBaseName() = header or requireHeaderIncluded = false) and
reason =
"declares a name reserved for a macro from the " + TargetedCLibrary::getName() +
" standard library header '" + header + "'"
Expand Down Expand Up @@ -272,8 +283,8 @@ module ReservedNames {
or
scope = MacroScope()
) and
// The relevant header is included directly or transitively by the file
m.getFile().getAnIncludedFile*().getBaseName() = header and
// The relevant header is included directly or transitively by the file, or we don't apply that requirement
(m.getFile().getAnIncludedFile*().getBaseName() = header or requireHeaderIncluded = false) and
reason =
"declares a reserved name from the " + TargetedCLibrary::getName() +
" standard library header '" + header +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ Query getQuery() { result instanceof DeclaredAReservedIdentifierSharedQuery }

query predicate problems(Element m, string message) {
not isExcluded(m, getQuery()) and
ReservedNames::C11::isAReservedIdentifier(m, message)
ReservedNames::C11::isAReservedIdentifier(m, message, true) and
// Not covered by this rule
not m instanceof PreprocessorUndef
}