Skip to content

Commit 2265ed4

Browse files
Address feedback
1 parent f363371 commit 2265ed4

9 files changed

+21
-62
lines changed

c/common/src/codingstandards/c/Generic.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ParsedGenericMacro extends Macro {
115115
)
116116
}
117117

118-
string getControllingExprString() { result = getSelectionString(1) }
118+
string getControllingExprString() { result = getSelectionString(1).trim() }
119119

120120
bindingset[str, word]
121121
private int countWordInString(string word, string str) {

c/misra/src/rules/RULE-23-1/GenericSelectionDoesntDependOnMacroArgument.ql

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import codingstandards.c.Generic
1919
from ParsedGenericMacro macro, string ctrlExpr
2020
where
2121
not isExcluded(macro, GenericsPackage::genericSelectionDoesntDependOnMacroArgumentQuery()) and
22-
ctrlExpr = macro.getControllingExprString().trim() and
23-
not macro.expansionsInsideControllingExpr(_) > 0
22+
ctrlExpr = macro.getControllingExprString() and
23+
// No parameter exists that is expanded in the controlling expression one or more times
24+
not exists(string parameter | macro.expansionsInsideControllingExpr(parameter) > 0)
2425
select macro,
25-
"Generic macro " + macro.getName() + " uses controlling expr " + ctrlExpr +
26-
", which doesn't match any macro parameter."
26+
"Generic macro " + macro.getName() + " doesn't refer to a macro parameter in controlling expr '" +
27+
ctrlExpr + "'."

c/misra/src/rules/RULE-23-1/GenericSelectionNotExpandedFromAMacro.ql

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ where
1919
not isExcluded(generic, GenericsPackage::genericSelectionNotExpandedFromAMacroQuery()) and
2020
ctrlExpr = generic.getControllingExpr() and
2121
not exists(MacroInvocation mi | mi.getAGeneratedElement() = generic.getExpr())
22-
select generic, "Generic expression with controlling expression $@ is not expanded froma macro",
23-
ctrlExpr, ctrlExpr.toString()
22+
select generic, "$@ in generic expression does not expand a macro parameter.", ctrlExpr,
23+
"Controlling expression"

c/misra/src/rules/RULE-23-2/GenericSelectionNotFromMacroWithSideEffects.ql

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module GenericSideEffectReportConfig implements MacroReportConfigSig<GenericWith
7272
string getMessageNotInMacro(
7373
GenericWithNonMacroSideEffect element, Locatable optLoc1, string optStr1
7474
) {
75+
// Generics which are not expanded from a macro aren't applicable to this rule.
7576
none()
7677
}
7778
}

c/misra/src/rules/RULE-23-3/GenericWithoutNonDefaultAssociation.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class InvalidGeneric extends C11GenericExpr {
2525
}
2626
}
2727

28-
from C11GenericExpr generic, Element primaryElement
28+
from InvalidGeneric generic, Element primaryElement
2929
where
3030
not isExcluded(primaryElement, GenericsPackage::genericWithoutNonDefaultAssociationQuery()) and
3131
not exists(Type t |
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.c:2:1:2:31 | #define M1 _Generic(1, int : 1) | Generic macro M1 uses controlling expr 1, which doesn't match any macro parameter. |
2-
| test.c:4:1:4:34 | #define M2(X) _Generic(1, int : X) | Generic macro M2 uses controlling expr 1, which doesn't match any macro parameter. |
3-
| test.c:18:1:18:39 | #define M9(X) g(_Generic((Y), int : 1)) | Generic macro M9 uses controlling expr (Y), which doesn't match any macro parameter. |
1+
| test.c:2:1:2:31 | #define M1 _Generic(1, int : 1) | Generic macro M1 doesn't refer to a macro parameter in controlling expr '1'. |
2+
| test.c:4:1:4:34 | #define M2(X) _Generic(1, int : X) | Generic macro M2 doesn't refer to a macro parameter in controlling expr '1'. |
3+
| test.c:18:1:18:39 | #define M9(X) g(_Generic((Y), int : 1)) | Generic macro M9 doesn't refer to a macro parameter in controlling expr '(Y)'. |
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.c:21:3:21:22 | _Generic | Generic expression with controlling expression $@ is not expanded froma macro | test.c:21:12:21:12 | 1 | 1 |
1+
| test.c:21:3:21:22 | _Generic | $@ in generic expression does not expand a macro parameter. | test.c:21:12:21:12 | 1 | Controlling expression |

cpp/common/src/codingstandards/cpp/types/Compatible.qll

+1-50
Original file line numberDiff line numberDiff line change
@@ -369,53 +369,4 @@ private class FunctionType extends Type {
369369
result = this.(RoutineType).getParameterType(i) or
370370
result = this.(FunctionPointerIshType).getParameterType(i)
371371
}
372-
}
373-
/*
374-
* predicate typesCompatibleImpl(Type t1, Type t2) {
375-
* // A type is compatible with itself
376-
* t1 = t2
377-
* or
378-
* // All specifiers must match, but the order does not matter:
379-
* (
380-
* t1 instanceof SpecifiedType and
381-
* t2 instanceof SpecifiedType
382-
* ) and
383-
* specifiersMatchExactly(t1, t2) and
384-
* typesCompatibleImpl(t1.stripTopLevelSpecifiers(), t2.stripTopLevelSpecifiers())
385-
* or
386-
* // Identically qualified pointers are compatible if they point to compatible types.
387-
* typesCompatibleImpl(t1.(PointerType).getBaseType(), t2.(PointerType).getBaseType())
388-
* or
389-
* // Array objects are compatible if they have a compatible element type. If both have a constant
390-
* // size then that size must match.
391-
* typesCompatibleImpl(t1.(ArrayType).getBaseType(), t2.(ArrayType).getBaseType()) and
392-
* count(int i | i = [t1, t2].(ArrayType).getSize()) < 2
393-
* or
394-
* // Enum types are compatible with one of char, int, or signed int, but the implementation
395-
* // decides.
396-
* [t1, t2] instanceof Enum and
397-
* ([t1, t2] instanceof CharType or [t1, t2] instanceof IntType)
398-
* or
399-
* // `int` is the same as `signed`, `signed int`, while `unsigned` is the same as `unsigned int`.
400-
* t1.(IntegralType).getCanonicalArithmeticType() = t2.(IntegralType).getCanonicalArithmeticType()
401-
* or
402-
* // Function types are compatible if they have the same return type and compatible parameters.
403-
* // Technically, variadic functions are have special behavior not covered here.
404-
* exists(RoutineType f1, RoutineType f2 | f1 = t1 and f2 = t2 |
405-
* typesCompatibleImpl(f1.getReturnType(), f2.getReturnType()) and
406-
* forall(int i | exists([f1, f2].getParameterType(i)) |
407-
* typesCompatibleImpl(f1.getParameterType(i), f2.getParameterType(i))
408-
* )
409-
* )
410-
* or
411-
* // Function pointer types should be covered by `PointerType` and `RoutineType` above, but that is
412-
* // not how they are implemented in CodeQL.
413-
* exists(FunctionPointerIshType f1, FunctionPointerIshType f2 | f1 = t1 and f2 = t2 |
414-
* typesCompatibleImpl(f1.getReturnType(), f2.getReturnType()) and
415-
* forall(int i | exists([f1, f2].getParameterType(i)) |
416-
* typesCompatibleImpl(f1.getParameterType(i), f2.getParameterType(i))
417-
* )
418-
* )
419-
* }
420-
*/
421-
372+
}

cpp/common/src/codingstandards/cpp/types/SimpleAssignment.qll

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Helper predicates related to C11/C17 constraints on simple assignment between two types.
3+
*
4+
* Currently only a subset of the constraints are implemented, specifically those
5+
* related to pointer types.
6+
*/
17
import codingstandards.cpp.types.LvalueConversion
28
import codingstandards.cpp.types.Compatible
39

0 commit comments

Comments
 (0)