Skip to content

__func__ excluded from C-style queries #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2023
Merged
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/2023-05-02-func-c-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Exclude the use of `__func__` from certain queries, as it is the proscribed way to return the name of the current function:
* `A27-0-4` - Use of the value returned by `__func__` is no longer flagged as a use of C-style strings.
* `A18-1-1` - `__func__` is no longer flagged as a declaration of a variable using C-style arrays.
4 changes: 3 additions & 1 deletion cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ class StaticConstExprArrayDataMember extends MemberVariable {
from Variable v
where
not isExcluded(v, BannedSyntaxPackage::cStyleArraysUsedQuery()) and
exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember)
exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember) and
// Exclude the compiler generated __func__ as it is the only way to access the function name information
not v.getName() = "__func__"
select v, "Variable " + v.getName() + " declares a c-style array."
3 changes: 2 additions & 1 deletion cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ where
e = any(FunctionCall fc).getArgument(_) and
e.getUnspecifiedType().(PointerType).getBaseType*() instanceof CharType
) and
DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e))
DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e)) and
not cs = any(LocalVariable lv | lv.getName() = "__func__").getInitializer().getExpr()
select cs, "Usage of C-style string in $@.", e, "expression"
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/A18-1-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ int test_c_arrays() {

int x[100]; // NON_COMPLIANT
constexpr int a[]{0, 1, 2}; // NON_COMPLIANT

__func__; // COMPLAINT
return 0;
}
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A27-0-4/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ void f2() {
f1(a1);
f1(a2);
f1(s.c_str()); // NON_COMPLIANT
__func__;
}