Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
7.22M
        : S(P.getActions()), DiagnosticPool(nullptr) {
54
7.22M
      if (activate) {
55
4.84M
        State = S.PushParsingDeclaration(DiagnosticPool);
56
4.84M
        Active = true;
57
4.84M
      } else {
58
2.38M
        Active = false;
59
2.38M
      }
60
7.22M
    }
61
    SuppressAccessChecks(SuppressAccessChecks &&Other)
62
      : S(Other.S), DiagnosticPool(std::move(Other.DiagnosticPool)),
63
1.34M
        State(Other.State), Active(Other.Active) {
64
1.34M
      Other.Active = false;
65
1.34M
    }
66
    void operator=(SuppressAccessChecks &&Other) = delete;
67
68
4.84M
    void done() {
69
4.84M
      assert(Active && "trying to end an inactive suppression");
70
4.84M
      S.PopParsingDeclaration(State, nullptr);
71
4.84M
      Active = false;
72
4.84M
    }
73
74
4.77M
    void redelay() {
75
4.77M
      assert(!Active && "redelaying without having ended first");
76
4.77M
      if (!DiagnosticPool.pool_empty())
77
1.24k
        S.redelayDiagnostics(DiagnosticPool);
78
4.77M
      assert(DiagnosticPool.pool_empty());
79
4.77M
    }
80
81
8.57M
    ~SuppressAccessChecks() {
82
8.57M
      if (Active) 
done()0
;
83
8.57M
    }
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
19.0M
        : Actions(P.getActions()), DiagnosticPool(nullptr) {
103
19.0M
      push();
104
19.0M
    }
105
106
    /// Creates a RAII object whose pool is optionally parented by another.
107
    ParsingDeclRAIIObject(Parser &P,
108
                          const sema::DelayedDiagnosticPool *parentPool)
109
14.0M
        : Actions(P.getActions()), DiagnosticPool(parentPool) {
110
14.0M
      push();
111
14.0M
    }
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
3.44M
          DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) {
119
3.44M
      if (other) {
120
1.22M
        DiagnosticPool.steal(other->DiagnosticPool);
121
1.22M
        other->abort();
122
1.22M
      }
123
3.44M
      push();
124
3.44M
    }
125
126
36.4M
    ~ParsingDeclRAIIObject() {
127
36.4M
      abort();
128
36.4M
    }
129
130
0
    sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
131
0
      return DiagnosticPool;
132
0
    }
133
14.0M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
134
14.0M
      return DiagnosticPool;
135
14.0M
    }
136
137
    /// Resets the RAII object for a new declaration.
138
238k
    void reset() {
139
238k
      abort();
140
238k
      push();
141
238k
    }
142
143
    /// Signals that the context was completed without an appropriate
144
    /// declaration being parsed.
145
40.1M
    void abort() {
146
40.1M
      pop(nullptr);
147
40.1M
    }
148
149
21.3M
    void complete(Decl *D) {
150
21.3M
      assert(!Popped && "ParsingDeclaration has already been popped!");
151
21.3M
      pop(D);
152
21.3M
    }
153
154
    /// Unregister this object from Sema, but remember all the
155
    /// diagnostics that were emitted into it.
156
0
    void abortAndRemember() {
157
0
      pop(nullptr);
158
0
    }
159
160
  private:
161
36.7M
    void push() {
162
36.7M
      State = Actions.PushParsingDeclaration(DiagnosticPool);
163
36.7M
      Popped = false;
164
36.7M
    }
165
166
61.5M
    void pop(Decl *D) {
167
61.5M
      if (!Popped) {
168
36.7M
        Actions.PopParsingDeclaration(State, D);
169
36.7M
        Popped = true;
170
36.7M
      }
171
61.5M
    }
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
12.1M
        ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
182
    ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
183
      : DeclSpec(P.getAttrFactory()),
184
3.44M
        ParsingRAII(P, RAII) {}
185
186
14.0M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
187
14.0M
      return ParsingRAII.getDelayedDiagnosticPool();
188
14.0M
    }
189
190
1.48M
    void complete(Decl *D) {
191
1.48M
      ParsingRAII.complete(D);
192
1.48M
    }
193
194
2.26M
    void abort() {
195
2.26M
      ParsingRAII.abort();
196
2.26M
    }
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
11.6M
      : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
206
11.6M
    }
207
208
3.33M
    const ParsingDeclSpec &getDeclSpec() const {
209
3.33M
      return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
210
3.33M
    }
211
212
2.16M
    ParsingDeclSpec &getMutableDeclSpec() const {
213
2.16M
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
214
2.16M
    }
215
216
238k
    void clear() {
217
238k
      Declarator::clear();
218
238k
      ParsingRAII.reset();
219
238k
    }
220
221
11.9M
    void complete(Decl *D) {
222
11.9M
      ParsingRAII.complete(D);
223
11.9M
    }
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
2.34M
      : FieldDeclarator(DS), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {
233
2.34M
    }
234
235
0
    const ParsingDeclSpec &getDeclSpec() const {
236
0
      return static_cast<const ParsingDeclSpec&>(D.getDeclSpec());
237
0
    }
238
239
0
    ParsingDeclSpec &getMutableDeclSpec() const {
240
0
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
241
0
    }
242
243
2.34M
    void complete(Decl *D) {
244
2.34M
      ParsingRAII.complete(D);
245
2.34M
    }
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
31.5k
    ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
259
31.5k
      Diags.IncrementAllExtensionsSilenced();
260
31.5k
    }
261
262
31.5k
    ~ExtensionRAIIObject() {
263
31.5k
      Diags.DecrementAllExtensionsSilenced();
264
31.5k
    }
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
24.5M
      : P(p), OldVal(P.ColonIsSacred) {
276
24.5M
      P.ColonIsSacred = Value;
277
24.5M
    }
278
279
    /// restore - This can be used to restore the state early, before the dtor
280
    /// is run.
281
30.9M
    void restore() {
282
30.9M
      P.ColonIsSacred = OldVal;
283
30.9M
    }
284
285
24.5M
    ~ColonProtectionRAIIObject() {
286
24.5M
      restore();
287
24.5M
    }
288
  };
289
290
  /// Activates OpenMP parsing mode to preseve OpenMP specific annotation
291
  /// tokens.
292
  class ParsingOpenMPDirectiveRAII {
293
    Parser &P;
294
    bool OldVal;
295
296
  public:
297
    ParsingOpenMPDirectiveRAII(Parser &P)
298
185k
        : P(P), OldVal(P.OpenMPDirectiveParsing) {
299
185k
      P.OpenMPDirectiveParsing = true;
300
185k
    }
301
302
    /// This can be used to restore the state early, before the dtor
303
    /// is run.
304
185k
    void restore() { P.OpenMPDirectiveParsing = OldVal; }
305
306
185k
    ~ParsingOpenMPDirectiveRAII() { restore(); }
307
  };
308
309
  /// RAII object that makes '>' behave either as an operator
310
  /// or as the closing angle bracket for a template argument list.
311
  class GreaterThanIsOperatorScope {
312
    bool &GreaterThanIsOperator;
313
    bool OldGreaterThanIsOperator;
314
  public:
315
    GreaterThanIsOperatorScope(bool &GTIO, bool Val)
316
34.9M
    : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
317
34.9M
      GreaterThanIsOperator = Val;
318
34.9M
    }
319
320
34.9M
    ~GreaterThanIsOperatorScope() {
321
34.9M
      GreaterThanIsOperator = OldGreaterThanIsOperator;
322
34.9M
    }
323
  };
324
325
  class InMessageExpressionRAIIObject {
326
    bool &InMessageExpression;
327
    bool OldValue;
328
329
  public:
330
    InMessageExpressionRAIIObject(Parser &P, bool Value)
331
      : InMessageExpression(P.InMessageExpression),
332
8.44M
        OldValue(P.InMessageExpression) {
333
8.44M
      InMessageExpression = Value;
334
8.44M
    }
335
336
8.44M
    ~InMessageExpressionRAIIObject() {
337
8.44M
      InMessageExpression = OldValue;
338
8.44M
    }
339
  };
340
341
  /// RAII object that makes sure paren/bracket/brace count is correct
342
  /// after declaration/statement parsing, even when there's a parsing error.
343
  class ParenBraceBracketBalancer {
344
    Parser &P;
345
    unsigned short ParenCount, BracketCount, BraceCount;
346
  public:
347
    ParenBraceBracketBalancer(Parser &p)
348
      : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
349
24.5M
        BraceCount(p.BraceCount) { }
350
351
24.5M
    ~ParenBraceBracketBalancer() {
352
24.5M
      P.AngleBrackets.clear(P);
353
24.5M
      P.ParenCount = ParenCount;
354
24.5M
      P.BracketCount = BracketCount;
355
24.5M
      P.BraceCount = BraceCount;
356
24.5M
    }
357
  };
358
359
  class PoisonSEHIdentifiersRAIIObject {
360
    PoisonIdentifierRAIIObject Ident_AbnormalTermination;
361
    PoisonIdentifierRAIIObject Ident_GetExceptionCode;
362
    PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
363
    PoisonIdentifierRAIIObject Ident__abnormal_termination;
364
    PoisonIdentifierRAIIObject Ident__exception_code;
365
    PoisonIdentifierRAIIObject Ident__exception_info;
366
    PoisonIdentifierRAIIObject Ident___abnormal_termination;
367
    PoisonIdentifierRAIIObject Ident___exception_code;
368
    PoisonIdentifierRAIIObject Ident___exception_info;
369
  public:
370
    PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
371
      : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
372
        Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
373
        Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
374
        Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
375
        Ident__exception_code(Self.Ident__exception_code, NewValue),
376
        Ident__exception_info(Self.Ident__exception_info, NewValue),
377
        Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
378
        Ident___exception_code(Self.Ident___exception_code, NewValue),
379
2.29M
        Ident___exception_info(Self.Ident___exception_info, NewValue) {
380
2.29M
    }
381
  };
382
383
  /// RAII class that helps handle the parsing of an open/close delimiter
384
  /// pair, such as braces { ... } or parentheses ( ... ).
385
  class BalancedDelimiterTracker : public GreaterThanIsOperatorScope {
386
    Parser& P;
387
    tok::TokenKind Kind, Close, FinalToken;
388
    SourceLocation (Parser::*Consumer)();
389
    SourceLocation LOpen, LClose;
390
391
32.1M
    unsigned short &getDepth() {
392
32.1M
      switch (Kind) {
393
4.97M
        case tok::l_brace: return P.BraceCount;
394
581k
        case tok::l_square: return P.BracketCount;
395
26.5M
        case tok::l_paren: return P.ParenCount;
396
0
        default: llvm_unreachable("Wrong token kind");
397
32.1M
      }
398
32.1M
    }
399
400
    bool diagnoseOverflow();
401
    bool diagnoseMissingClose();
402
403
  public:
404
    BalancedDelimiterTracker(Parser& p, tok::TokenKind k,
405
                             tok::TokenKind FinalToken = tok::semi)
406
      : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true),
407
        P(p), Kind(k), FinalToken(FinalToken)
408
32.1M
    {
409
32.1M
      switch (Kind) {
410
0
        default: llvm_unreachable("Unexpected balanced token");
411
4.97M
        case tok::l_brace:
412
4.97M
          Close = tok::r_brace;
413
4.97M
          Consumer = &Parser::ConsumeBrace;
414
4.97M
          break;
415
26.6M
        case tok::l_paren:
416
26.6M
          Close = tok::r_paren;
417
26.6M
          Consumer = &Parser::ConsumeParen;
418
26.6M
          break;
419
0
420
581k
        case tok::l_square:
421
581k
          Close = tok::r_square;
422
581k
          Consumer = &Parser::ConsumeBracket;
423
581k
          break;
424
32.1M
      }
425
32.1M
    }
426
427
22.7M
    SourceLocation getOpenLocation() const { return LOpen; }
428
29.5M
    SourceLocation getCloseLocation() const { return LClose; }
429
2.19M
    SourceRange getRange() const { return SourceRange(LOpen, LClose); }
430
431
31.7M
    bool consumeOpen() {
432
31.7M
      if (!P.Tok.is(Kind))
433
1.75k
        return true;
434
31.7M
435
31.7M
      if (getDepth() < P.getLangOpts().BracketDepth) {
436
31.7M
        LOpen = (P.*Consumer)();
437
31.7M
        return false;
438
31.7M
      }
439
3
440
3
      return diagnoseOverflow();
441
3
    }
442
443
    bool expectAndConsume(unsigned DiagID = diag::err_expected,
444
                          const char *Msg = "",
445
                          tok::TokenKind SkipToTok = tok::unknown);
446
32.0M
    bool consumeClose() {
447
32.0M
      if (P.Tok.is(Close)) {
448
32.0M
        LClose = (P.*Consumer)();
449
32.0M
        return false;
450
32.0M
      } else 
if (17.2k
P.Tok.is(tok::semi)17.2k
&&
P.NextToken().is(Close)32
) {
451
17
        SourceLocation SemiLoc = P.ConsumeToken();
452
17
        P.Diag(SemiLoc, diag::err_unexpected_semi)
453
17
            << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc));
454
17
        LClose = (P.*Consumer)();
455
17
        return false;
456
17
      }
457
17.2k
458
17.2k
      return diagnoseMissingClose();
459
17.2k
    }
460
    void skipToEnd();
461
  };
462
463
  /// RAIIObject to destroy the contents of a SmallVector of
464
  /// TemplateIdAnnotation pointers and clear the vector.
465
  class DestroyTemplateIdAnnotationsRAIIObj {
466
    SmallVectorImpl<TemplateIdAnnotation *> &Container;
467
468
  public:
469
    DestroyTemplateIdAnnotationsRAIIObj(
470
        SmallVectorImpl<TemplateIdAnnotation *> &Container)
471
18.1M
        : Container(Container) {}
472
473
18.1M
    ~DestroyTemplateIdAnnotationsRAIIObj() {
474
18.1M
      for (SmallVectorImpl<TemplateIdAnnotation *>::iterator I =
475
18.1M
               Container.begin(),
476
18.1M
             E = Container.end();
477
20.8M
           I != E; 
++I2.69M
)
478
2.69M
        (*I)->Destroy();
479
18.1M
      Container.clear();
480
18.1M
    }
481
  };
482
} // end namespace clang
483
484
#endif