Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Parse/RAIIObjectsForParser.h
Line
Count
Source (jump to first uncovered line)
1
//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- 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 and implements the some simple RAII objects that are used
10
// by the parser to manage bits in recursion.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H
15
#define LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H
16
17
#include "clang/Parse/ParseDiagnostic.h"
18
#include "clang/Parse/Parser.h"
19
#include "clang/Sema/DelayedDiagnostic.h"
20
#include "clang/Sema/ParsedTemplate.h"
21
#include "clang/Sema/Sema.h"
22
23
namespace clang {
24
  // TODO: move ParsingClassDefinition here.
25
  // TODO: move TentativeParsingAction here.
26
27
  /// A RAII object used to temporarily suppress access-like
28
  /// checking.  Access-like checks are those associated with
29
  /// controlling the use of a declaration, like C++ access control
30
  /// errors and deprecation warnings.  They are contextually
31
  /// dependent, in that they can only be resolved with full
32
  /// information about what's being declared.  They are also
33
  /// suppressed in certain contexts, like the template arguments of
34
  /// an explicit instantiation.  However, those suppression contexts
35
  /// cannot necessarily be fully determined in advance;  for
36
  /// example, something starting like this:
37
  ///   template <> class std::vector<A::PrivateType>
38
  /// might be the entirety of an explicit instantiation:
39
  ///   template <> class std::vector<A::PrivateType>;
40
  /// or just an elaborated type specifier:
41
  ///   template <> class std::vector<A::PrivateType> make_vector<>();
42
  /// Therefore this class collects all the diagnostics and permits
43
  /// them to be re-delayed in a new context.
44
  class SuppressAccessChecks {
45
    Sema &S;
46
    sema::DelayedDiagnosticPool DiagnosticPool;
47
    Sema::ParsingDeclState State;
48
    bool Active;
49
50
  public:
51
    /// Begin suppressing access-like checks
52
    SuppressAccessChecks(Parser &P, bool activate = true)
53
1.78M
        : S(P.getActions()), DiagnosticPool(nullptr) {
54
1.78M
      if (activate) {
55
429k
        State = S.PushParsingDeclaration(DiagnosticPool);
56
429k
        Active = true;
57
1.35M
      } else {
58
1.35M
        Active = false;
59
1.35M
      }
60
1.78M
    }
61
    SuppressAccessChecks(SuppressAccessChecks &&Other)
62
      : S(Other.S), DiagnosticPool(std::move(Other.DiagnosticPool)),
63
170k
        State(Other.State), Active(Other.Active) {
64
170k
      Other.Active = false;
65
170k
    }
66
    void operator=(SuppressAccessChecks &&Other) = delete;
67
68
429k
    void done() {
69
429k
      assert(Active && "trying to end an inactive suppression");
70
429k
      S.PopParsingDeclaration(State, nullptr);
71
429k
      Active = false;
72
429k
    }
73
74
357k
    void redelay() {
75
357k
      assert(!Active && "redelaying without having ended first");
76
357k
      if (!DiagnosticPool.pool_empty())
77
760
        S.redelayDiagnostics(DiagnosticPool);
78
357k
      assert(DiagnosticPool.pool_empty());
79
357k
    }
80
81
1.95M
    ~SuppressAccessChecks() {
82
1.95M
      if (Active) 
done()0
;
83
1.95M
    }
84
  };
85
86
  /// RAII object used to inform the actions that we're
87
  /// currently parsing a declaration.  This is active when parsing a
88
  /// variable's initializer, but not when parsing the body of a
89
  /// class or function definition.
90
  class ParsingDeclRAIIObject {
91
    Sema &Actions;
92
    sema::DelayedDiagnosticPool DiagnosticPool;
93
    Sema::ParsingDeclState State;
94
    bool Popped;
95
96
    ParsingDeclRAIIObject(const ParsingDeclRAIIObject &) = delete;
97
    void operator=(const ParsingDeclRAIIObject &) = delete;
98
99
  public:
100
    enum NoParent_t { NoParent };
101
    ParsingDeclRAIIObject(Parser &P, NoParent_t _)
102
9.35M
        : Actions(P.getActions()), DiagnosticPool(nullptr) {
103
9.35M
      push();
104
9.35M
    }
105
106
    /// Creates a RAII object whose pool is optionally parented by another.
107
    ParsingDeclRAIIObject(Parser &P,
108
                          const sema::DelayedDiagnosticPool *parentPool)
109
11.3M
        : Actions(P.getActions()), DiagnosticPool(parentPool) {
110
11.3M
      push();
111
11.3M
    }
112
113
    /// Creates a RAII object and, optionally, initialize its
114
    /// diagnostics pool by stealing the diagnostics from another
115
    /// RAII object (which is assumed to be the current top pool).
116
    ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other)
117
        : Actions(P.getActions()),
118
4.73M
          DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) {
119
4.73M
      if (other) {
120
1.26M
        DiagnosticPool.steal(other->DiagnosticPool);
121
1.26M
        other->abort();
122
1.26M
      }
123
4.73M
      push();
124
4.73M
    }
125
126
25.4M
    ~ParsingDeclRAIIObject() {
127
25.4M
      abort();
128
25.4M
    }
129
130
0
    sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
131
0
      return DiagnosticPool;
132
0
    }
133
11.3M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
134
11.3M
      return DiagnosticPool;
135
11.3M
    }
136
137
    /// Resets the RAII object for a new declaration.
138
64.5k
    void reset() {
139
64.5k
      abort();
140
64.5k
      push();
141
64.5k
    }
142
143
    /// Signals that the context was completed without an appropriate
144
    /// declaration being parsed.
145
28.7M
    void abort() {
146
28.7M
      pop(nullptr);
147
28.7M
    }
148
149
12.8M
    void complete(Decl *D) {
150
12.8M
      assert(!Popped && "ParsingDeclaration has already been popped!");
151
12.8M
      pop(D);
152
12.8M
    }
153
154
    /// Unregister this object from Sema, but remember all the
155
    /// diagnostics that were emitted into it.
156
    void abortAndRemember() {
157
      pop(nullptr);
158
    }
159
160
  private:
161
25.5M
    void push() {
162
25.5M
      State = Actions.PushParsingDeclaration(DiagnosticPool);
163
25.5M
      Popped = false;
164
25.5M
    }
165
166
41.6M
    void pop(Decl *D) {
167
41.6M
      if (!Popped) {
168
25.5M
        Actions.PopParsingDeclaration(State, D);
169
25.5M
        Popped = true;
170
25.5M
      }
171
41.6M
    }
172
  };
173
174
  /// A class for parsing a DeclSpec.
175
  class ParsingDeclSpec : public DeclSpec {
176
    ParsingDeclRAIIObject ParsingRAII;
177
178
  public:
179
    ParsingDeclSpec(Parser &P)
180
      : DeclSpec(P.getAttrFactory()),
181
7.65M
        ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
182
    ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
183
      : DeclSpec(P.getAttrFactory()),
184
4.73M
        ParsingRAII(P, RAII) {}
185
186
11.3M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
187
11.3M
      return ParsingRAII.getDelayedDiagnosticPool();
188
11.3M
    }
189
190
948k
    void complete(Decl *D) {
191
948k
      ParsingRAII.complete(D);
192
948k
    }
193
194
1.96M
    void abort() {
195
1.96M
      ParsingRAII.abort();
196
1.96M
    }
197
  };
198
199
  /// A class for parsing a declarator.
200
  class ParsingDeclarator : public Declarator {
201
    ParsingDeclRAIIObject ParsingRAII;
202
203
  public:
204
    ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, DeclaratorContext C)
205
10.6M
      : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
206
10.6M
    }
207
208
3.81M
    const ParsingDeclSpec &getDeclSpec() const {
209
3.81M
      return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
210
3.81M
    }
211
212
1.91M
    ParsingDeclSpec &getMutableDeclSpec() const {
213
1.91M
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
214
1.91M
    }
215
216
64.5k
    void clear() {
217
64.5k
      Declarator::clear();
218
64.5k
      ParsingRAII.reset();
219
64.5k
    }
220
221
10.6M
    void complete(Decl *D) {
222
10.6M
      ParsingRAII.complete(D);
223
10.6M
    }
224
  };
225
226
  /// A class for parsing a field declarator.
227
  class ParsingFieldDeclarator : public FieldDeclarator {
228
    ParsingDeclRAIIObject ParsingRAII;
229
230
  public:
231
    ParsingFieldDeclarator(Parser &P, const ParsingDeclSpec &DS)
232
766k
      : FieldDeclarator(DS), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
233
766k
    }
234
235
    const ParsingDeclSpec &getDeclSpec() const {
236
      return static_cast<const ParsingDeclSpec&>(D.getDeclSpec());
237
    }
238
239
    ParsingDeclSpec &getMutableDeclSpec() const {
240
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
241
    }
242
243
766k
    void complete(Decl *D) {
244
766k
      ParsingRAII.complete(D);
245
766k
    }
246
  };
247
248
  /// ExtensionRAIIObject - This saves the state of extension warnings when
249
  /// constructed and disables them.  When destructed, it restores them back to
250
  /// the way they used to be.  This is used to handle __extension__ in the
251
  /// parser.
252
  class ExtensionRAIIObject {
253
    ExtensionRAIIObject(const ExtensionRAIIObject &) = delete;
254
    void operator=(const ExtensionRAIIObject &) = delete;
255
256
    DiagnosticsEngine &Diags;
257
  public:
258
22.9k
    ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
259
22.9k
      Diags.IncrementAllExtensionsSilenced();
260
22.9k
    }
261
262
22.9k
    ~ExtensionRAIIObject() {
263
22.9k
      Diags.DecrementAllExtensionsSilenced();
264
22.9k
    }
265
  };
266
267
  /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
268
  /// restores it when destroyed.  This says that "foo:" should not be
269
  /// considered a possible typo for "foo::" for error recovery purposes.
270
  class ColonProtectionRAIIObject {
271
    Parser &P;
272
    bool OldVal;
273
  public:
274
    ColonProtectionRAIIObject(Parser &p, bool Value = true)
275
27.9M
      : P(p), OldVal(P.ColonIsSacred) {
276
27.9M
      P.ColonIsSacred = Value;
277
27.9M
    }
278
279
    /// restore - This can be used to restore the state early, before the dtor
280
    /// is run.
281
35.0M
    void restore() {
282
35.0M
      P.ColonIsSacred = OldVal;
283
35.0M
    }
284
285
27.9M
    ~ColonProtectionRAIIObject() {
286
27.9M
      restore();
287
27.9M
    }
288
  };
289
290
  /// RAII object that makes '>' behave either as an operator
291
  /// or as the closing angle bracket for a template argument list.
292
  class GreaterThanIsOperatorScope {
293
    bool &GreaterThanIsOperator;
294
    bool OldGreaterThanIsOperator;
295
  public:
296
    GreaterThanIsOperatorScope(bool &GTIO, bool Val)
297
28.9M
    : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
298
28.9M
      GreaterThanIsOperator = Val;
299
28.9M
    }
300
301
28.9M
    ~GreaterThanIsOperatorScope() {
302
28.9M
      GreaterThanIsOperator = OldGreaterThanIsOperator;
303
28.9M
    }
304
  };
305
306
  class InMessageExpressionRAIIObject {
307
    bool &InMessageExpression;
308
    bool OldValue;
309
310
  public:
311
    InMessageExpressionRAIIObject(Parser &P, bool Value)
312
      : InMessageExpression(P.InMessageExpression),
313
11.1M
        OldValue(P.InMessageExpression) {
314
11.1M
      InMessageExpression = Value;
315
11.1M
    }
316
317
11.1M
    ~InMessageExpressionRAIIObject() {
318
11.1M
      InMessageExpression = OldValue;
319
11.1M
    }
320
  };
321
322
  /// RAII object that makes sure paren/bracket/brace count is correct
323
  /// after declaration/statement parsing, even when there's a parsing error.
324
  class ParenBraceBracketBalancer {
325
    Parser &P;
326
    unsigned short ParenCount, BracketCount, BraceCount;
327
  public:
328
    ParenBraceBracketBalancer(Parser &p)
329
      : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
330
26.2M
        BraceCount(p.BraceCount) { }
331
332
26.2M
    ~ParenBraceBracketBalancer() {
333
26.2M
      P.AngleBrackets.clear(P);
334
26.2M
      P.ParenCount = ParenCount;
335
26.2M
      P.BracketCount = BracketCount;
336
26.2M
      P.BraceCount = BraceCount;
337
26.2M
    }
338
  };
339
340
  class PoisonSEHIdentifiersRAIIObject {
341
    PoisonIdentifierRAIIObject Ident_AbnormalTermination;
342
    PoisonIdentifierRAIIObject Ident_GetExceptionCode;
343
    PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
344
    PoisonIdentifierRAIIObject Ident__abnormal_termination;
345
    PoisonIdentifierRAIIObject Ident__exception_code;
346
    PoisonIdentifierRAIIObject Ident__exception_info;
347
    PoisonIdentifierRAIIObject Ident___abnormal_termination;
348
    PoisonIdentifierRAIIObject Ident___exception_code;
349
    PoisonIdentifierRAIIObject Ident___exception_info;
350
  public:
351
    PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
352
      : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
353
        Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
354
        Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
355
        Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
356
        Ident__exception_code(Self.Ident__exception_code, NewValue),
357
        Ident__exception_info(Self.Ident__exception_info, NewValue),
358
        Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
359
        Ident___exception_code(Self.Ident___exception_code, NewValue),
360
2.07M
        Ident___exception_info(Self.Ident___exception_info, NewValue) {
361
2.07M
    }
362
  };
363
364
  /// RAII class that helps handle the parsing of an open/close delimiter
365
  /// pair, such as braces { ... } or parentheses ( ... ).
366
  class BalancedDelimiterTracker : public GreaterThanIsOperatorScope {
367
    Parser& P;
368
    tok::TokenKind Kind, Close, FinalToken;
369
    SourceLocation (Parser::*Consumer)();
370
    SourceLocation LOpen, LClose;
371
372
26.1M
    unsigned short &getDepth() {
373
26.1M
      switch (Kind) {
374
26.1M
        
case tok::l_brace: return P.BraceCount5.00M
;
375
26.1M
        
case tok::l_square: return P.BracketCount1.01M
;
376
26.1M
        
case tok::l_paren: return P.ParenCount20.1M
;
377
26.1M
        
default: 0
llvm_unreachable0
("Wrong token kind");
378
26.1M
      }
379
26.1M
    }
380
381
    bool diagnoseOverflow();
382
    bool diagnoseMissingClose();
383
384
  public:
385
    BalancedDelimiterTracker(Parser& p, tok::TokenKind k,
386
                             tok::TokenKind FinalToken = tok::semi)
387
      : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true),
388
        P(p), Kind(k), FinalToken(FinalToken)
389
26.2M
    {
390
26.2M
      switch (Kind) {
391
26.2M
        
default: 0
llvm_unreachable0
("Unexpected balanced token");
392
26.2M
        case tok::l_brace:
393
5.00M
          Close = tok::r_brace;
394
5.00M
          Consumer = &Parser::ConsumeBrace;
395
5.00M
          break;
396
26.2M
        case tok::l_paren:
397
20.2M
          Close = tok::r_paren;
398
20.2M
          Consumer = &Parser::ConsumeParen;
399
20.2M
          break;
400
26.2M
401
26.2M
        case tok::l_square:
402
1.01M
          Close = tok::r_square;
403
1.01M
          Consumer = &Parser::ConsumeBracket;
404
1.01M
          break;
405
26.2M
      }
406
26.2M
    }
407
408
24.6M
    SourceLocation getOpenLocation() const { return LOpen; }
409
20.3M
    SourceLocation getCloseLocation() const { return LClose; }
410
1.05M
    SourceRange getRange() const { return SourceRange(LOpen, LClose); }
411
412
25.8M
    bool consumeOpen() {
413
25.8M
      if (!P.Tok.is(Kind))
414
617
        return true;
415
25.8M
416
25.8M
      if (getDepth() < P.getLangOpts().BracketDepth) {
417
25.8M
        LOpen = (P.*Consumer)();
418
25.8M
        return false;
419
25.8M
      }
420
2
421
2
      return diagnoseOverflow();
422
2
    }
423
424
    bool expectAndConsume(unsigned DiagID = diag::err_expected,
425
                          const char *Msg = "",
426
                          tok::TokenKind SkipToTok = tok::unknown);
427
26.1M
    bool consumeClose() {
428
26.1M
      if (P.Tok.is(Close)) {
429
26.1M
        LClose = (P.*Consumer)();
430
26.1M
        return false;
431
26.1M
      } else 
if (11.7k
P.Tok.is(tok::semi)11.7k
&&
P.NextToken().is(Close)32
) {
432
17
        SourceLocation SemiLoc = P.ConsumeToken();
433
17
        P.Diag(SemiLoc, diag::err_unexpected_semi)
434
17
            << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc));
435
17
        LClose = (P.*Consumer)();
436
17
        return false;
437
17
      }
438
11.6k
439
11.6k
      return diagnoseMissingClose();
440
11.6k
    }
441
    void skipToEnd();
442
  };
443
444
  /// RAIIObject to destroy the contents of a SmallVector of
445
  /// TemplateIdAnnotation pointers and clear the vector.
446
  class DestroyTemplateIdAnnotationsRAIIObj {
447
    SmallVectorImpl<TemplateIdAnnotation *> &Container;
448
449
  public:
450
    DestroyTemplateIdAnnotationsRAIIObj(
451
        SmallVectorImpl<TemplateIdAnnotation *> &Container)
452
10.2M
        : Container(Container) {}
453
454
10.2M
    ~DestroyTemplateIdAnnotationsRAIIObj() {
455
10.2M
      for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
456
10.2M
               Container.begin(),
457
10.2M
             E = Container.end();
458
12.9M
           I != E; 
++I2.67M
)
459
2.67M
        (*I)->Destroy();
460
10.2M
      Container.clear();
461
10.2M
    }
462
  };
463
} // end namespace clang
464
465
#endif