Coverage Report

Created: 2019-03-22 08:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Sema/Designator.h
Line
Count
Source (jump to first uncovered line)
1
//===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines interfaces used to represent designators (a la
10
// C99 designated initializers) during parsing.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SEMA_DESIGNATOR_H
15
#define LLVM_CLANG_SEMA_DESIGNATOR_H
16
17
#include "clang/Basic/SourceLocation.h"
18
#include "llvm/ADT/SmallVector.h"
19
20
namespace clang {
21
22
class Expr;
23
class IdentifierInfo;
24
class Sema;
25
26
/// Designator - A designator in a C99 designated initializer.
27
///
28
/// This class is a discriminated union which holds the various
29
/// different sorts of designators possible.  A Designation is an array of
30
/// these.  An example of a designator are things like this:
31
///     [8] .field [47]        // C99 designation: 3 designators
32
///     [8 ... 47]  field:     // GNU extensions: 2 designators
33
/// These occur in initializers, e.g.:
34
///  int a[10] = {2, 4, [8]=9, 10};
35
///
36
class Designator {
37
public:
38
  enum DesignatorKind {
39
    FieldDesignator, ArrayDesignator, ArrayRangeDesignator
40
  };
41
private:
42
  DesignatorKind Kind;
43
44
  struct FieldDesignatorInfo {
45
    const IdentifierInfo *II;
46
    unsigned DotLoc;
47
    unsigned NameLoc;
48
  };
49
  struct ArrayDesignatorInfo {
50
    Expr *Index;
51
    unsigned LBracketLoc;
52
    mutable unsigned  RBracketLoc;
53
  };
54
  struct ArrayRangeDesignatorInfo {
55
    Expr *Start, *End;
56
    unsigned LBracketLoc, EllipsisLoc;
57
    mutable unsigned RBracketLoc;
58
  };
59
60
  union {
61
    FieldDesignatorInfo FieldInfo;
62
    ArrayDesignatorInfo ArrayInfo;
63
    ArrayRangeDesignatorInfo ArrayRangeInfo;
64
  };
65
66
public:
67
68
2.86k
  DesignatorKind getKind() const { return Kind; }
69
  bool isFieldDesignator() const { return Kind == FieldDesignator; }
70
887
  bool isArrayDesignator() const { return Kind == ArrayDesignator; }
71
4
  bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
72
73
2.56k
  const IdentifierInfo *getField() const {
74
2.56k
    assert(isFieldDesignator() && "Invalid accessor");
75
2.56k
    return FieldInfo.II;
76
2.56k
  }
77
78
2.56k
  SourceLocation getDotLoc() const {
79
2.56k
    assert(isFieldDesignator() && "Invalid accessor");
80
2.56k
    return SourceLocation::getFromRawEncoding(FieldInfo.DotLoc);
81
2.56k
  }
82
83
2.56k
  SourceLocation getFieldLoc() const {
84
2.56k
    assert(isFieldDesignator() && "Invalid accessor");
85
2.56k
    return SourceLocation::getFromRawEncoding(FieldInfo.NameLoc);
86
2.56k
  }
87
88
273
  Expr *getArrayIndex() const {
89
273
    assert(isArrayDesignator() && "Invalid accessor");
90
273
    return ArrayInfo.Index;
91
273
  }
92
93
27
  Expr *getArrayRangeStart() const {
94
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
95
27
    return ArrayRangeInfo.Start;
96
27
  }
97
27
  Expr *getArrayRangeEnd() const {
98
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
99
27
    return ArrayRangeInfo.End;
100
27
  }
101
102
295
  SourceLocation getLBracketLoc() const {
103
295
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
104
295
           "Invalid accessor");
105
295
    if (isArrayDesignator())
106
269
      return SourceLocation::getFromRawEncoding(ArrayInfo.LBracketLoc);
107
26
    else
108
26
      return SourceLocation::getFromRawEncoding(ArrayRangeInfo.LBracketLoc);
109
295
  }
110
111
295
  SourceLocation getRBracketLoc() const {
112
295
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
113
295
           "Invalid accessor");
114
295
    if (isArrayDesignator())
115
269
      return SourceLocation::getFromRawEncoding(ArrayInfo.RBracketLoc);
116
26
    else
117
26
      return SourceLocation::getFromRawEncoding(ArrayRangeInfo.RBracketLoc);
118
295
  }
119
120
27
  SourceLocation getEllipsisLoc() const {
121
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
122
27
    return SourceLocation::getFromRawEncoding(ArrayRangeInfo.EllipsisLoc);
123
27
  }
124
125
  static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc,
126
2.57k
                             SourceLocation NameLoc) {
127
2.57k
    Designator D;
128
2.57k
    D.Kind = FieldDesignator;
129
2.57k
    D.FieldInfo.II = II;
130
2.57k
    D.FieldInfo.DotLoc = DotLoc.getRawEncoding();
131
2.57k
    D.FieldInfo.NameLoc = NameLoc.getRawEncoding();
132
2.57k
    return D;
133
2.57k
  }
134
135
  static Designator getArray(Expr *Index,
136
283
                             SourceLocation LBracketLoc) {
137
283
    Designator D;
138
283
    D.Kind = ArrayDesignator;
139
283
    D.ArrayInfo.Index = Index;
140
283
    D.ArrayInfo.LBracketLoc = LBracketLoc.getRawEncoding();
141
283
    D.ArrayInfo.RBracketLoc = 0;
142
283
    return D;
143
283
  }
144
145
  static Designator getArrayRange(Expr *Start,
146
                                  Expr *End,
147
                                  SourceLocation LBracketLoc,
148
27
                                  SourceLocation EllipsisLoc) {
149
27
    Designator D;
150
27
    D.Kind = ArrayRangeDesignator;
151
27
    D.ArrayRangeInfo.Start = Start;
152
27
    D.ArrayRangeInfo.End = End;
153
27
    D.ArrayRangeInfo.LBracketLoc = LBracketLoc.getRawEncoding();
154
27
    D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc.getRawEncoding();
155
27
    D.ArrayRangeInfo.RBracketLoc = 0;
156
27
    return D;
157
27
  }
158
159
278
  void setRBracketLoc(SourceLocation RBracketLoc) const {
160
278
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
161
278
           "Invalid accessor");
162
278
    if (isArrayDesignator())
163
259
      ArrayInfo.RBracketLoc = RBracketLoc.getRawEncoding();
164
19
    else
165
19
      ArrayRangeInfo.RBracketLoc = RBracketLoc.getRawEncoding();
166
278
  }
167
168
  /// ClearExprs - Null out any expression references, which prevents
169
  /// them from being 'delete'd later.
170
  void ClearExprs(Sema &Actions) {}
171
172
  /// FreeExprs - Release any unclaimed memory for the expressions in
173
  /// this designator.
174
  void FreeExprs(Sema &Actions) {}
175
};
176
177
178
/// Designation - Represent a full designation, which is a sequence of
179
/// designators.  This class is mostly a helper for InitListDesignations.
180
class Designation {
181
  /// Designators - The actual designators for this initializer.
182
  SmallVector<Designator, 2> Designators;
183
184
public:
185
  /// AddDesignator - Add a designator to the end of this list.
186
2.88k
  void AddDesignator(Designator D) {
187
2.88k
    Designators.push_back(D);
188
2.88k
  }
189
190
0
  bool empty() const { return Designators.empty(); }
191
192
5.93k
  unsigned getNumDesignators() const { return Designators.size(); }
193
3.16k
  const Designator &getDesignator(unsigned Idx) const {
194
3.16k
    assert(Idx < Designators.size());
195
3.16k
    return Designators[Idx];
196
3.16k
  }
197
198
  /// ClearExprs - Null out any expression references, which prevents them from
199
  /// being 'delete'd later.
200
2.64k
  void ClearExprs(Sema &Actions) {}
201
202
  /// FreeExprs - Release any unclaimed memory for the expressions in this
203
  /// designation.
204
0
  void FreeExprs(Sema &Actions) {}
205
};
206
207
} // end namespace clang
208
209
#endif