Skip to content

Commit 213e369

Browse files
authored
Merge pull request #303 from github/lcartey/func-c-style
__func__ excluded from C-style queries
2 parents a260607 + 02b518d commit 213e369

File tree

5 files changed

+11
-2
lines changed

5 files changed

+11
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Exclude the use of `__func__` from certain queries, as it is the proscribed way to return the name of the current function:
2+
* `A27-0-4` - Use of the value returned by `__func__` is no longer flagged as a use of C-style strings.
3+
* `A18-1-1` - `__func__` is no longer flagged as a declaration of a variable using C-style arrays.

cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ class StaticConstExprArrayDataMember extends MemberVariable {
3030
from Variable v
3131
where
3232
not isExcluded(v, BannedSyntaxPackage::cStyleArraysUsedQuery()) and
33-
exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember)
33+
exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember) and
34+
// Exclude the compiler generated __func__ as it is the only way to access the function name information
35+
not v.getName() = "__func__"
3436
select v, "Variable " + v.getName() + " declares a c-style array."

cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ where
3636
e = any(FunctionCall fc).getArgument(_) and
3737
e.getUnspecifiedType().(PointerType).getBaseType*() instanceof CharType
3838
) and
39-
DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e))
39+
DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e)) and
40+
not cs = any(LocalVariable lv | lv.getName() = "__func__").getInitializer().getExpr()
4041
select cs, "Usage of C-style string in $@.", e, "expression"

cpp/autosar/test/rules/A18-1-1/test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ int test_c_arrays() {
1010

1111
int x[100]; // NON_COMPLIANT
1212
constexpr int a[]{0, 1, 2}; // NON_COMPLIANT
13+
14+
__func__; // COMPLAINT
1315
return 0;
1416
}

cpp/autosar/test/rules/A27-0-4/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ void f2() {
2626
f1(a1);
2727
f1(a2);
2828
f1(s.c_str()); // NON_COMPLIANT
29+
__func__;
2930
}

0 commit comments

Comments
 (0)