|
| 1 | +/** |
| 2 | + * A library for handling "predicate" types, which are function parameters in the standard library. |
| 3 | + * |
| 4 | + * For example, `std::sort` takes a predicate as its third argument, and `std::set` takes a |
| 5 | + * predicate as its second template parameter. Predicates are always template parameters, and we |
| 6 | + * can identify them by their name -- this is what MISRA expects us to do. |
| 7 | + */ |
| 8 | + |
| 9 | +import cpp |
| 10 | +private import codingstandards.cpp.StdNamespace |
| 11 | +private import codingstandards.cpp.ast.Templates |
| 12 | +private import codingstandards.cpp.types.Templates |
| 13 | +private import semmle.code.cpp.dataflow.new.DataFlow |
| 14 | + |
| 15 | +/** |
| 16 | + * A "predicate" type parameter as defined by MISRA, which is a standard library function named |
| 17 | + * "Compare" or "Predicate" or "BinaryPredicate" in the standard library. |
| 18 | + * |
| 19 | + * To be more widely useful, we match more flexibly on the name, as `_Compare` is also common, etc. |
| 20 | + * |
| 21 | + * To get a particular `Type` that is _used_ as a predicate type, rather than the type parameter |
| 22 | + * itself, see `getASubstitutedType()`. |
| 23 | + */ |
| 24 | +class PredicateType extends TypeTemplateParameter { |
| 25 | + PredicateType() { |
| 26 | + this.getName().matches(["%Compare%", "%Predicate%"]) and |
| 27 | + getTemplateParameterNamespace(this) instanceof StdNS |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get a type that is used (anywhere, via some substitution) as this predicate type parameter. |
| 32 | + * |
| 33 | + * For example, `std::sort(..., ..., [](int a, int b) { return a < b; })` creates a `Closure` |
| 34 | + * type, and substitutes that closure type for the predicate type parameter of `std::sort`. |
| 35 | + */ |
| 36 | + Type getASubstitutedType(Substitution sub) { result = sub.getSubstitutedTypeForParameter(this) } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A class type that has been substituted for a predicate type parameter, and has an `operator()` |
| 41 | + * member function. |
| 42 | + * |
| 43 | + * For example, the closure type in `std::sort(..., ..., [](int a, int b) { return a < b; })` is a |
| 44 | + * `PredicateFunctionObject`, and so is any `std::less<int>` that is used as a predicate type |
| 45 | + * parameter, etc. |
| 46 | + * |
| 47 | + * This does not cover function pointer types, as these are not class types. |
| 48 | + */ |
| 49 | +class PredicateFunctionObject extends Class { |
| 50 | + PredicateType pred; |
| 51 | + Function operator; |
| 52 | + Substitution sub; |
| 53 | + |
| 54 | + PredicateFunctionObject() { |
| 55 | + this = pred.getASubstitutedType(sub) and |
| 56 | + operator.getDeclaringType() = this and |
| 57 | + operator.getName() = "operator()" |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the predicate type parameter that this function object is being substituted for. |
| 62 | + */ |
| 63 | + PredicateType getPredicateType() { result = pred } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the `operator()` function that this function object defines. This is the function that will |
| 67 | + * be invoked and essentially defines the predicate behavior. |
| 68 | + */ |
| 69 | + Function getCallOperator() { result = operator } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the `Substitution` object that makes this type a `PredicateFunctionObject`. |
| 73 | + * |
| 74 | + * This is a particular instantiation of some template that contains a predicate type parameter, |
| 75 | + * which is substituted by this type in that instantiation. The `Substitution` object may refer |
| 76 | + * to a `TemplateClass`, `TemplateVariable`, or `TemplateFunction`. |
| 77 | + */ |
| 78 | + Substitution getSubstitution() { result = sub } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Gets a function access where the purpose of that access is to pass the accessed function as a |
| 83 | + * predicate argument to a standard library template. |
| 84 | + * |
| 85 | + * For example, in `std::sort(..., ..., myCompare)`, where `myCompare` is a function, then |
| 86 | + * `myCompare` will be converted into a function pointer and that function pointer will be used as |
| 87 | + * a predicate in that `std::sort` call. |
| 88 | + * |
| 89 | + * This is more complex to identify than `PredicateFunctionObject` because the addressee of the |
| 90 | + * function pointer is not necessarily statically known. |
| 91 | + */ |
| 92 | +class PredicateFunctionPointerUse extends FunctionAccess { |
| 93 | + Expr functionPointerArgument; |
| 94 | + FunctionCall templateFunctionCall; |
| 95 | + FunctionTemplateInstantiation instantiation; |
| 96 | + Substitution sub; |
| 97 | + PredicateType pred; |
| 98 | + Parameter parameter; |
| 99 | + int index; |
| 100 | + |
| 101 | + PredicateFunctionPointerUse() { |
| 102 | + functionPointerArgument = templateFunctionCall.getArgument(index) and |
| 103 | + templateFunctionCall.getTarget() = instantiation and |
| 104 | + parameter = instantiation.getParameter(index) and |
| 105 | + sub.asFunctionSubstitution() = instantiation and |
| 106 | + parameter.getType() = sub.getSubstitutedTypeForParameter(pred) and |
| 107 | + exists(DataFlow::Node func, DataFlow::Node arg | |
| 108 | + func.asExpr() = this and |
| 109 | + arg.asExpr() = functionPointerArgument and |
| 110 | + DataFlow::localFlow(func, arg) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the predicate type parameter that this function pointer is being substituted for. |
| 116 | + */ |
| 117 | + PredicateType getPredicateType() { result = pred } |
| 118 | +} |
0 commit comments