summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/tests/TestParamTraitsEnum.cpp
blob: a250250bfe49041470ea87578b081dc2f51cb363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
typedef enum {
  BadFirst,
  BadSecond,
  BadThird
} BadEnum;

typedef enum {
  NestedFirst,
  NestedSecond
} NestedBadEnum;

typedef enum {
  GoodFirst,
  GoodSecond,
  GoodLast
} GoodEnum;

enum RawEnum {
  RawFirst,
  RawLast
};

enum class ClassEnum {
  ClassFirst,
  ClassLast
};

template <class P> struct ParamTraits;

// Simplified EnumSerializer etc. from IPCMessageUtils.h
template <typename E, typename EnumValidator>
struct EnumSerializer {
  typedef E paramType;
};

template <typename E,
          E MinLegal,
          E HighBound>
class ContiguousEnumValidator
{};

template <typename E,
          E MinLegal,
          E HighBound>
struct ContiguousEnumSerializer
  : EnumSerializer<E,
                   ContiguousEnumValidator<E, MinLegal, HighBound>>
{};

// Typical ParamTraits implementation that should be avoided
template<>
struct ParamTraits<ClassEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}}
{
  typedef ClassEnum paramType;
};

template<>
struct ParamTraits<enum RawEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}}
{
  typedef enum RawEnum paramType;
};

template<>
struct ParamTraits<BadEnum> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}}
{
  typedef BadEnum paramType;
};

// Make sure the analysis catches nested typedefs
typedef NestedBadEnum NestedDefLevel1;
typedef NestedDefLevel1 NestedDefLevel2;

template<>
struct ParamTraits<NestedDefLevel2> // expected-error {{Custom ParamTraits implementation for an enum type}} expected-note {{Please use a helper class for example ContiguousEnumSerializer}}
{
  typedef NestedDefLevel2 paramType;
};

// Make sure a non enum typedef is not accidentally flagged
typedef int IntTypedef;

template<>
struct ParamTraits<IntTypedef>
{
  typedef IntTypedef paramType;
};

// Make sure ParamTraits using helper classes are not flagged
template<>
struct ParamTraits<GoodEnum>
: public ContiguousEnumSerializer<GoodEnum,
                                  GoodEnum::GoodFirst,
                                  GoodEnum::GoodLast>
{};