| 1 |
typedef enum { |
| 2 |
BadFirst, |
| 3 |
BadSecond, |
| 4 |
BadThird |
| 5 |
} BadEnum; |
| 6 |
|
| 7 |
typedef enum { |
| 8 |
NestedFirst, |
| 9 |
NestedSecond |
| 10 |
} NestedBadEnum; |
| 11 |
|
| 12 |
typedef enum { |
| 13 |
GoodFirst, |
| 14 |
GoodSecond, |
| 15 |
GoodLast |
| 16 |
} GoodEnum; |
| 17 |
|
| 18 |
enum RawEnum { |
| 19 |
RawFirst, |
| 20 |
RawLast |
| 21 |
}; |
| 22 |
|
| 23 |
enum class ClassEnum { |
| 24 |
ClassFirst, |
| 25 |
ClassLast |
| 26 |
}; |
| 27 |
|
| 28 |
template <class P> struct ParamTraits; |
| 29 |
|
| 30 |
// Simplified EnumSerializer etc. from IPCMessageUtils.h |
| 31 |
template <typename E, typename EnumValidator> |
| 32 |
struct EnumSerializer { |
| 33 |
typedef E paramType; |
| 34 |
}; |
| 35 |
|
| 36 |
template <typename E, |
| 37 |
E MinLegal, |
| 38 |
E HighBound> |
| 39 |
class ContiguousEnumValidator |
| 40 |
{}; |
| 41 |
|
| 42 |
template <typename E, |
| 43 |
E MinLegal, |
| 44 |
E HighBound> |
| 45 |
struct ContiguousEnumSerializer |
| 46 |
: EnumSerializer<E, |
| 47 |
ContiguousEnumValidator<E, MinLegal, HighBound>> |
| 48 |
{}; |
| 49 |
|
| 50 |
// Typical ParamTraits implementation that should be avoided |
| 51 |
template<> |
| 52 |
struct ParamTraits<ClassEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}} |
| 53 |
{ |
| 54 |
typedef ClassEnum paramType; |
| 55 |
}; |
| 56 |
|
| 57 |
template<> |
| 58 |
struct ParamTraits<enum RawEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}} |
| 59 |
{ |
| 60 |
typedef enum RawEnum paramType; |
| 61 |
}; |
| 62 |
|
| 63 |
template<> |
| 64 |
struct ParamTraits<BadEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}} |
| 65 |
{ |
| 66 |
typedef BadEnum paramType; |
| 67 |
}; |
| 68 |
|
| 69 |
// Make sure the analysis catches nested typedefs |
| 70 |
typedef NestedBadEnum NestedDefLevel1; |
| 71 |
typedef NestedDefLevel1 NestedDefLevel2; |
| 72 |
|
| 73 |
template<> |
| 74 |
struct ParamTraits<NestedDefLevel2> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}} |
| 75 |
{ |
| 76 |
typedef NestedDefLevel2 paramType; |
| 77 |
}; |
| 78 |
|
| 79 |
// Make sure a non enum typedef is not accidentally flagged |
| 80 |
typedef int IntTypedef; |
| 81 |
|
| 82 |
template<> |
| 83 |
struct ParamTraits<IntTypedef> |
| 84 |
{ |
| 85 |
typedef IntTypedef paramType; |
| 86 |
}; |
| 87 |
|
| 88 |
// Make sure ParamTraits using helper classes are not flagged |
| 89 |
template<> |
| 90 |
struct ParamTraits<GoodEnum> |
| 91 |
: public ContiguousEnumSerializer<GoodEnum, |
| 92 |
GoodEnum::GoodFirst, |
| 93 |
GoodEnum::GoodLast> |
| 94 |
{}; |