Skip to content

Commit 7a1eeb2

Browse files
committed
M17-0-3: Migrate to StandardNaming
1 parent 740c76d commit 7a1eeb2

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

change_notes/2025-08-28-naming.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `M17-0-3` - `NameOfStandardLibraryFunctionIsOverridden.ql`:
2+
- The list of standard library names has been refined.
3+
- Only names defined in the global namespace by the C++ standard are now reported.

cpp/autosar/src/rules/M17-0-3/NameOfStandardLibraryFunctionIsOverridden.ql

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,26 @@
1515

1616
import cpp
1717
import codingstandards.cpp.autosar
18-
import codingstandards.cpp.Naming
18+
import codingstandards.cpp.StandardLibraryNames
1919

20-
predicate isGlobal(Function f) { f.hasGlobalName(_) }
21-
22-
from TopLevelFunction f
20+
from TopLevelFunction f, string functionName, string header
2321
where
2422
not isExcluded(f, NamingPackage::nameOfStandardLibraryFunctionIsOverriddenQuery()) and
25-
Naming::Cpp14::hasStandardLibraryFunctionName(f.getName()) and
26-
f.fromSource() and
27-
isGlobal(f)
23+
f.hasGlobalName(functionName) and
24+
/*
25+
* The rule does not define what it means by "standard library function", but we will
26+
* assume that it is a non-member function which is declared in the global namespace.
27+
* Functions declared in `std` only can't be easily "replaced" in the way the rule is
28+
* concerned about (without extending 'std'), and so are excluded.
29+
*/
30+
31+
(
32+
// Directly defined by C++
33+
CppStandardLibrary::Cpp14::hasFunctionName(header, "", "", functionName, _, _, _)
34+
or
35+
// Defined by C and inherited by C++
36+
CStandardLibrary::C11::hasFunctionName(header, "", "", functionName, _, _, _)
37+
) and
38+
f.fromSource()
2839
select f,
29-
"Function " + f.getName() + " overrides standard library function " +
30-
Naming::Cpp14::getQualifiedStandardLibraryFunctionName(f.getName()) + "."
40+
"Function " + functionName + " overrides a standard library function from '" + header + "'."
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
| test.cpp:1:6:1:11 | malloc | Function malloc overrides standard library function malloc. |
2-
| test.cpp:3:6:3:11 | printf | Function printf overrides standard library function printf. |
3-
| test.cpp:5:6:5:11 | strlen | Function strlen overrides standard library function strlen. |
4-
| test.cpp:7:6:7:8 | end | Function end overrides standard library function std::__1::end. |
5-
| test.cpp:7:6:7:8 | end | Function end overrides standard library function std::end. |
1+
| test.cpp:1:6:1:11 | malloc | Function malloc overrides a standard library function from 'stdlib.h'. |
2+
| test.cpp:3:6:3:11 | printf | Function printf overrides a standard library function from 'stdio.h'. |
3+
| test.cpp:5:6:5:11 | strlen | Function strlen overrides a standard library function from 'string.h'. |

cpp/autosar/test/rules/M17-0-3/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ void printf() {} // NON_COMPLIANT
44

55
void strlen() {} // NON_COMPLIANT
66

7-
void end() {} // NON_COMPLIANT
7+
void end() {} // COMPLIANT - end is in the std namespace

rule_packages/cpp/Naming.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@
268268
"tags": [
269269
"readability",
270270
"maintainability"
271-
]
271+
],
272+
"implementation_scope": {
273+
"description": "This implementation reports all functions with the same name as a function declared in the global namespace in the C++ standard."
274+
}
272275
}
273276
],
274277
"title": "The names of standard library functions shall not be overridden."

0 commit comments

Comments
 (0)