Coverage Report

Created: 2022-07-16 07:03

/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_PARSE_RAIIOBJECTSFORPARSER_H
15
#define LLVM_CLANG_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
42.2M
        : S(P.getActions()), DiagnosticPool(nullptr) {
54
42.2M
      if (activate) {
55
4.52M
        State = S.PushParsingDeclaration(DiagnosticPool);
56
4.52M
        Active = true;
57
37.6M
      } else {
58
37.6M
        Active = false;
59
37.6M
      }
60
42.2M
    }
61
    SuppressAccessChecks(SuppressAccessChecks &&Other)
62
      : S(Other.S), DiagnosticPool(std::move(Other.DiagnosticPool)),
63
1.04M
        State(Other.State), Active(Other.Active) {
64
1.04M
      Other.Active = false;
65
1.04M
    }
66
    void operator=(SuppressAccessChecks &&Other) = delete;
67
68
4.52M
    void done() {
69
4.52M
      assert(Active && "trying to end an inactive suppression");
70
0
      S.PopParsingDeclaration(State, nullptr);
71
4.52M
      Active = false;
72
4.52M
    }
73
74
3.84M
    void redelay() {
75
3.84M
      assert(!Active && "redelaying without having ended first");
76
3.84M
      if (!DiagnosticPool.pool_empty())
77
1.43k
        S.redelayDiagnostics(DiagnosticPool);
78
3.84M
      assert(DiagnosticPool.pool_empty());
79
3.84M
    }
80
81
43.2M
    ~SuppressAccessChecks() {
82
43.2M
      if (Active) 
done()18
;
83
43.2M
    }
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
36.4M
        : Actions(P.getActions()), DiagnosticPool(nullptr) {
103
36.4M
      push();
104
36.4M
    }
105
106
    /// Creates a RAII object whose pool is optionally parented by another.
107
    ParsingDeclRAIIObject(Parser &P,
108
                          const sema::DelayedDiagnosticPool *parentPool)
109
33.1M
        : Actions(P.getActions()), DiagnosticPool(parentPool) {
110
33.1M
      push();
111
33.1M
    }
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.42M
          DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) {
119
4.42M
      if (other) {
120
1.56M
        DiagnosticPool.steal(other->DiagnosticPool);
121
1.56M
        other->abort();
122
1.56M
      }
123
4.42M
      push();
124
4.42M
    }
125
126
73.9M
    ~ParsingDeclRAIIObject() {
127
73.9M
      abort();
128
73.9M
    }
129
130
0
    sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() {
131
0
      return DiagnosticPool;
132
0
    }
133
33.1M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
134
33.1M
      return DiagnosticPool;
135
33.1M
    }
136
137
    /// Resets the RAII object for a new declaration.
138
198k
    void reset() {
139
198k
      abort();
140
198k
      push();
141
198k
    }
142
143
    /// Signals that the context was completed without an appropriate
144
    /// declaration being parsed.
145
78.8M
    void abort() {
146
78.8M
      pop(nullptr);
147
78.8M
    }
148
149
39.3M
    void complete(Decl *D) {
150
39.3M
      assert(!Popped && "ParsingDeclaration has already been popped!");
151
0
      pop(D);
152
39.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
74.1M
    void push() {
162
74.1M
      State = Actions.PushParsingDeclaration(DiagnosticPool);
163
74.1M
      Popped = false;
164
74.1M
    }
165
166
118M
    void pop(Decl *D) {
167
118M
      if (!Popped) {
168
74.1M
        Actions.PopParsingDeclaration(State, D);
169
74.1M
        Popped = true;
170
74.1M
      }
171
118M
    }
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
30.3M
        ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {}
182
    ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII)
183
      : DeclSpec(P.getAttrFactory()),
184
4.42M
        ParsingRAII(P, RAII) {}
185
186
33.1M
    const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const {
187
33.1M
      return ParsingRAII.getDelayedDiagnosticPool();
188
33.1M
    }
189
190
1.50M
    void complete(Decl *D) {
191
1.50M
      ParsingRAII.complete(D);
192
1.50M
    }
193
194
3.10M
    void abort() {
195
3.10M
      ParsingRAII.abort();
196
3.10M
    }
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,
205
                      const ParsedAttributes &DeclarationAttrs,
206
                      DeclaratorContext C)
207
        : Declarator(DS, DeclarationAttrs, C),
208
30.8M
          ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {}
209
210
4.58M
    const ParsingDeclSpec &getDeclSpec() const {
211
4.58M
      return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec());
212
4.58M
    }
213
214
2.99M
    ParsingDeclSpec &getMutableDeclSpec() const {
215
2.99M
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
216
2.99M
    }
217
218
198k
    void clear() {
219
198k
      Declarator::clear();
220
198k
      ParsingRAII.reset();
221
198k
    }
222
223
31.0M
    void complete(Decl *D) {
224
31.0M
      ParsingRAII.complete(D);
225
31.0M
    }
226
  };
227
228
  /// A class for parsing a field declarator.
229
  class ParsingFieldDeclarator : public FieldDeclarator {
230
    ParsingDeclRAIIObject ParsingRAII;
231
232
  public:
233
    ParsingFieldDeclarator(Parser &P, const ParsingDeclSpec &DS,
234
                           const ParsedAttributes &DeclarationAttrs)
235
        : FieldDeclarator(DS, DeclarationAttrs),
236
2.32M
          ParsingRAII(P, &DS.getDelayedDiagnosticPool()) {}
237
238
0
    const ParsingDeclSpec &getDeclSpec() const {
239
0
      return static_cast<const ParsingDeclSpec&>(D.getDeclSpec());
240
0
    }
241
242
0
    ParsingDeclSpec &getMutableDeclSpec() const {
243
0
      return const_cast<ParsingDeclSpec&>(getDeclSpec());
244
0
    }
245
246
2.32M
    void complete(Decl *D) {
247
2.32M
      ParsingRAII.complete(D);
248
2.32M
    }
249
  };
250
251
  /// ExtensionRAIIObject - This saves the state of extension warnings when
252
  /// constructed and disables them.  When destructed, it restores them back to
253
  /// the way they used to be.  This is used to handle __extension__ in the
254
  /// parser.
255
  class ExtensionRAIIObject {
256
    ExtensionRAIIObject(const ExtensionRAIIObject &) = delete;
257
    void operator=(const ExtensionRAIIObject &) = delete;
258
259
    DiagnosticsEngine &Diags;
260
  public:
261
36.7k
    ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) {
262
36.7k
      Diags.IncrementAllExtensionsSilenced();
263
36.7k
    }
264
265
36.7k
    ~ExtensionRAIIObject() {
266
36.7k
      Diags.DecrementAllExtensionsSilenced();
267
36.7k
    }
268
  };
269
270
  /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
271
  /// restores it when destroyed.  This says that "foo:" should not be
272
  /// considered a possible typo for "foo::" for error recovery purposes.
273
  class ColonProtectionRAIIObject {
274
    Parser &P;
275
    bool OldVal;
276
  public:
277
    ColonProtectionRAIIObject(Parser &p, bool Value = true)
278
60.3M
      : P(p), OldVal(P.ColonIsSacred) {
279
60.3M
      P.ColonIsSacred = Value;
280
60.3M
    }
281
282
    /// restore - This can be used to restore the state early, before the dtor
283
    /// is run.
284
68.3M
    void restore() {
285
68.3M
      P.ColonIsSacred = OldVal;
286
68.3M
    }
287
288
60.3M
    ~ColonProtectionRAIIObject() {
289
60.3M
      restore();
290
60.3M
    }
291
  };
292
293
  /// Activates OpenMP parsing mode to preseve OpenMP specific annotation
294
  /// tokens.
295
  class ParsingOpenMPDirectiveRAII {
296
    Parser &P;
297
    bool OldVal;
298
299
  public:
300
    ParsingOpenMPDirectiveRAII(Parser &P, bool Value = true)
301
526k
        : P(P), OldVal(P.OpenMPDirectiveParsing) {
302
526k
      P.OpenMPDirectiveParsing = Value;
303
526k
    }
304
305
    /// This can be used to restore the state early, before the dtor
306
    /// is run.
307
526k
    void restore() { P.OpenMPDirectiveParsing = OldVal; }
308
309
526k
    ~ParsingOpenMPDirectiveRAII() { restore(); }
310
  };
311
312
  /// RAII object that makes '>' behave either as an operator
313
  /// or as the closing angle bracket for a template argument list.
314
  class GreaterThanIsOperatorScope {
315
    bool &GreaterThanIsOperator;
316
    bool OldGreaterThanIsOperator;
317
  public:
318
    GreaterThanIsOperatorScope(bool &GTIO, bool Val)
319
59.4M
    : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) {
320
59.4M
      GreaterThanIsOperator = Val;
321
59.4M
    }
322
323
59.4M
    ~GreaterThanIsOperatorScope() {
324
59.4M
      GreaterThanIsOperator = OldGreaterThanIsOperator;
325
59.4M
    }
326
  };
327
328
  class InMessageExpressionRAIIObject {
329
    bool &InMessageExpression;
330
    bool OldValue;
331
332
  public:
333
    InMessageExpressionRAIIObject(Parser &P, bool Value)
334
      : InMessageExpression(P.InMessageExpression),
335
11.7M
        OldValue(P.InMessageExpression) {
336
11.7M
      InMessageExpression = Value;
337
11.7M
    }
338
339
11.7M
    ~InMessageExpressionRAIIObject() {
340
11.7M
      InMessageExpression = OldValue;
341
11.7M
    }
342
  };
343
344
  /// RAII object that makes sure paren/bracket/brace count is correct
345
  /// after declaration/statement parsing, even when there's a parsing error.
346
  class ParenBraceBracketBalancer {
347
    Parser &P;
348
    unsigned short ParenCount, BracketCount, BraceCount;
349
  public:
350
    ParenBraceBracketBalancer(Parser &p)
351
      : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
352
48.7M
        BraceCount(p.BraceCount) { }
353
354
48.7M
    ~ParenBraceBracketBalancer() {
355
48.7M
      P.AngleBrackets.clear(P);
356
48.7M
      P.ParenCount = ParenCount;
357
48.7M
      P.BracketCount = BracketCount;
358
48.7M
      P.BraceCount = BraceCount;
359
48.7M
    }
360
  };
361
362
  class PoisonSEHIdentifiersRAIIObject {
363
    PoisonIdentifierRAIIObject Ident_AbnormalTermination;
364
    PoisonIdentifierRAIIObject Ident_GetExceptionCode;
365
    PoisonIdentifierRAIIObject Ident_GetExceptionInfo;
366
    PoisonIdentifierRAIIObject Ident__abnormal_termination;
367
    PoisonIdentifierRAIIObject Ident__exception_code;
368
    PoisonIdentifierRAIIObject Ident__exception_info;
369
    PoisonIdentifierRAIIObject Ident___abnormal_termination;
370
    PoisonIdentifierRAIIObject Ident___exception_code;
371
    PoisonIdentifierRAIIObject Ident___exception_info;
372
  public:
373
    PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue)
374
      : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue),
375
        Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue),
376
        Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue),
377
        Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue),
378
        Ident__exception_code(Self.Ident__exception_code, NewValue),
379
        Ident__exception_info(Self.Ident__exception_info, NewValue),
380
        Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue),
381
        Ident___exception_code(Self.Ident___exception_code, NewValue),
382
3.19M
        Ident___exception_info(Self.Ident___exception_info, NewValue) {
383
3.19M
    }
384
  };
385
386
  /// RAII class that helps handle the parsing of an open/close delimiter
387
  /// pair, such as braces { ... } or parentheses ( ... ).
388
  class BalancedDelimiterTracker : public GreaterThanIsOperatorScope {
389
    Parser& P;
390
    tok::TokenKind Kind, Close, FinalToken;
391
    SourceLocation (Parser::*Consumer)();
392
    SourceLocation LOpen, LClose;
393
394
55.5M
    unsigned short &getDepth() {
395
55.5M
      switch (Kind) {
396
6.73M
        case tok::l_brace: return P.BraceCount;
397
669k
        case tok::l_square: return P.BracketCount;
398
48.1M
        case tok::l_paren: return P.ParenCount;
399
0
        default: llvm_unreachable("Wrong token kind");
400
55.5M
      }
401
55.5M
    }
402
403
    bool diagnoseOverflow();
404
    bool diagnoseMissingClose();
405
406
  public:
407
    BalancedDelimiterTracker(Parser& p, tok::TokenKind k,
408
                             tok::TokenKind FinalToken = tok::semi)
409
      : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true),
410
        P(p), Kind(k), FinalToken(FinalToken)
411
55.6M
    {
412
55.6M
      switch (Kind) {
413
0
        default: llvm_unreachable("Unexpected balanced token");
414
6.73M
        case tok::l_brace:
415
6.73M
          Close = tok::r_brace;
416
6.73M
          Consumer = &Parser::ConsumeBrace;
417
6.73M
          break;
418
48.2M
        case tok::l_paren:
419
48.2M
          Close = tok::r_paren;
420
48.2M
          Consumer = &Parser::ConsumeParen;
421
48.2M
          break;
422
423
669k
        case tok::l_square:
424
669k
          Close = tok::r_square;
425
669k
          Consumer = &Parser::ConsumeBracket;
426
669k
          break;
427
55.6M
      }
428
55.6M
    }
429
430
47.3M
    SourceLocation getOpenLocation() const { return LOpen; }
431
58.1M
    SourceLocation getCloseLocation() const { return LClose; }
432
2.19M
    SourceRange getRange() const { return SourceRange(LOpen, LClose); }
433
434
55.0M
    bool consumeOpen() {
435
55.0M
      if (!P.Tok.is(Kind))
436
12.2k
        return true;
437
438
55.0M
      if (getDepth() < P.getLangOpts().BracketDepth) {
439
55.0M
        LOpen = (P.*Consumer)();
440
55.0M
        return false;
441
55.0M
      }
442
443
4
      return diagnoseOverflow();
444
55.0M
    }
445
446
    bool expectAndConsume(unsigned DiagID = diag::err_expected,
447
                          const char *Msg = "",
448
                          tok::TokenKind SkipToTok = tok::unknown);
449
55.5M
    bool consumeClose() {
450
55.5M
      if (P.Tok.is(Close)) {
451
55.4M
        LClose = (P.*Consumer)();
452
55.4M
        return false;
453
55.4M
      } else 
if (23.5k
P.Tok.is(tok::semi)23.5k
&&
P.NextToken().is(Close)37
) {
454
17
        SourceLocation SemiLoc = P.ConsumeToken();
455
17
        P.Diag(SemiLoc, diag::err_unexpected_semi)
456
17
            << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc));
457
17
        LClose = (P.*Consumer)();
458
17
        return false;
459
17
      }
460
461
23.5k
      return diagnoseMissingClose();
462
55.5M
    }
463
    void skipToEnd();
464
  };
465
} // end namespace clang
466
467
#endif