diff options
author | Vassil Vassilev <v.g.vassilev@gmail.com> | 2017-03-23 15:11:07 +0000 |
---|---|---|
committer | Vassil Vassilev <v.g.vassilev@gmail.com> | 2017-03-23 15:11:07 +0000 |
commit | dc408464136bcd47b1cca596e6c4513bd5153e38 (patch) | |
tree | e9fbb34602db367877f45d2e80bed6aeb6f34221 /lib/Parse | |
parent | aa59bd4807e5129200864caedac0a6d22a8c8185 (diff) |
Publish RAIIObjectsForParser.h for external usage.
Some clients (eg the cling interpreter) need to recover their parser from
errors.
Patch by Axel Naumann (D31190)!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@298606 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse')
-rw-r--r-- | lib/Parse/ParseCXXInlineMethods.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseDeclCXX.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseExpr.cpp | 4 | ||||
-rw-r--r-- | lib/Parse/ParseExprCXX.cpp | 4 | ||||
-rw-r--r-- | lib/Parse/ParseInit.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseObjc.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseOpenMP.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParsePragma.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseStmt.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseStmtAsm.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/ParseTemplate.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/Parser.cpp | 2 | ||||
-rw-r--r-- | lib/Parse/RAIIObjectsForParser.h | 447 |
14 files changed, 15 insertions, 462 deletions
diff --git a/lib/Parse/ParseCXXInlineMethods.cpp b/lib/Parse/ParseCXXInlineMethods.cpp index c52b61e7e9..5034f28f8c 100644 --- a/lib/Parse/ParseCXXInlineMethods.cpp +++ b/lib/Parse/ParseCXXInlineMethods.cpp @@ -12,9 +12,9 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/DeclTemplate.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/Scope.h" using namespace clang; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 226e2610ee..3531ca4724 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -12,7 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/Basic/AddressSpaces.h" diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 87facc449f..343b5f1a17 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -12,7 +12,6 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/Basic/Attributes.h" @@ -20,6 +19,7 @@ #include "clang/Basic/OperatorKinds.h" #include "clang/Basic/TargetInfo.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/PrettyDeclStackTrace.h" diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index 56c7685d93..4dcdfbf993 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -21,10 +21,10 @@ /// //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" +#include "clang/Parse/Parser.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/PrettyStackTrace.h" -#include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp index 10339fc96b..b549f60ce0 100644 --- a/lib/Parse/ParseExprCXX.cpp +++ b/lib/Parse/ParseExprCXX.cpp @@ -10,13 +10,13 @@ // This file implements the Expression parsing implementation for C++. // //===----------------------------------------------------------------------===// +#include "clang/Parse/Parser.h" #include "clang/AST/ASTContext.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/DeclTemplate.h" #include "clang/Basic/PrettyStackTrace.h" #include "clang/Lex/LiteralSupport.h" #include "clang/Parse/ParseDiagnostic.h" -#include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" diff --git a/lib/Parse/ParseInit.cpp b/lib/Parse/ParseInit.cpp index fa6b75daed..f48d01e0f6 100644 --- a/lib/Parse/ParseInit.cpp +++ b/lib/Parse/ParseInit.cpp @@ -11,9 +11,9 @@ // //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/Designator.h" #include "clang/Sema/Scope.h" #include "llvm/ADT/SmallString.h" diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp index 47674738d1..9a677d1b40 100644 --- a/lib/Parse/ParseObjc.cpp +++ b/lib/Parse/ParseObjc.cpp @@ -12,10 +12,10 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/CharInfo.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/PrettyDeclStackTrace.h" #include "clang/Sema/Scope.h" diff --git a/lib/Parse/ParseOpenMP.cpp b/lib/Parse/ParseOpenMP.cpp index 8138a0ac9a..dfb0438ba8 100644 --- a/lib/Parse/ParseOpenMP.cpp +++ b/lib/Parse/ParseOpenMP.cpp @@ -11,11 +11,11 @@ /// //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/StmtOpenMP.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/Scope.h" #include "llvm/ADT/PointerIntPair.h" diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp index 89733237c1..c97df85ae1 100644 --- a/lib/Parse/ParsePragma.cpp +++ b/lib/Parse/ParsePragma.cpp @@ -11,13 +11,13 @@ // //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/PragmaKinds.h" #include "clang/Basic/TargetInfo.h" #include "clang/Lex/Preprocessor.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/LoopHint.h" #include "clang/Sema/Scope.h" #include "llvm/ADT/StringSwitch.h" diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index 522cab1c1e..05c22b04d8 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -12,10 +12,10 @@ // //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" #include "clang/Basic/Attributes.h" #include "clang/Basic/PrettyStackTrace.h" #include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/LoopHint.h" #include "clang/Sema/PrettyDeclStackTrace.h" diff --git a/lib/Parse/ParseStmtAsm.cpp b/lib/Parse/ParseStmtAsm.cpp index 85cd22fef4..9903c89fa9 100644 --- a/lib/Parse/ParseStmtAsm.cpp +++ b/lib/Parse/ParseStmtAsm.cpp @@ -12,10 +12,10 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/TargetInfo.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/MC/MCAsmInfo.h" diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index f12e3f55e5..428ff38829 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -11,11 +11,11 @@ // //===----------------------------------------------------------------------===// -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp index 7df2e3288c..c938cc9b5e 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -12,11 +12,11 @@ //===----------------------------------------------------------------------===// #include "clang/Parse/Parser.h" -#include "RAIIObjectsForParser.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/Parse/ParseDiagnostic.h" +#include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" diff --git a/lib/Parse/RAIIObjectsForParser.h b/lib/Parse/RAIIObjectsForParser.h deleted file mode 100644 index 36d87ebd8a..0000000000 --- a/lib/Parse/RAIIObjectsForParser.h +++ /dev/null @@ -1,447 +0,0 @@ -//===--- RAIIObjectsForParser.h - RAII helpers for the parser ---*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines and implements the some simple RAII objects that are used -// by the parser to manage bits in recursion. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H -#define LLVM_CLANG_LIB_PARSE_RAIIOBJECTSFORPARSER_H - -#include "clang/Parse/ParseDiagnostic.h" -#include "clang/Parse/Parser.h" -#include "clang/Sema/DelayedDiagnostic.h" -#include "clang/Sema/Sema.h" - -namespace clang { - // TODO: move ParsingClassDefinition here. - // TODO: move TentativeParsingAction here. - - /// \brief A RAII object used to temporarily suppress access-like - /// checking. Access-like checks are those associated with - /// controlling the use of a declaration, like C++ access control - /// errors and deprecation warnings. They are contextually - /// dependent, in that they can only be resolved with full - /// information about what's being declared. They are also - /// suppressed in certain contexts, like the template arguments of - /// an explicit instantiation. However, those suppression contexts - /// cannot necessarily be fully determined in advance; for - /// example, something starting like this: - /// template <> class std::vector<A::PrivateType> - /// might be the entirety of an explicit instantiation: - /// template <> class std::vector<A::PrivateType>; - /// or just an elaborated type specifier: - /// template <> class std::vector<A::PrivateType> make_vector<>(); - /// Therefore this class collects all the diagnostics and permits - /// them to be re-delayed in a new context. - class SuppressAccessChecks { - Sema &S; - sema::DelayedDiagnosticPool DiagnosticPool; - Sema::ParsingDeclState State; - bool Active; - - public: - /// Begin suppressing access-like checks - SuppressAccessChecks(Parser &P, bool activate = true) - : S(P.getActions()), DiagnosticPool(nullptr) { - if (activate) { - State = S.PushParsingDeclaration(DiagnosticPool); - Active = true; - } else { - Active = false; - } - } - SuppressAccessChecks(SuppressAccessChecks &&Other) - : S(Other.S), DiagnosticPool(std::move(Other.DiagnosticPool)), - State(Other.State), Active(Other.Active) { - Other.Active = false; - } - void operator=(SuppressAccessChecks &&Other) = delete; - - void done() { - assert(Active && "trying to end an inactive suppression"); - S.PopParsingDeclaration(State, nullptr); - Active = false; - } - - void redelay() { - assert(!Active && "redelaying without having ended first"); - if (!DiagnosticPool.pool_empty()) - S.redelayDiagnostics(DiagnosticPool); - assert(DiagnosticPool.pool_empty()); - } - - ~SuppressAccessChecks() { - if (Active) done(); - } - }; - - /// \brief RAII object used to inform the actions that we're - /// currently parsing a declaration. This is active when parsing a - /// variable's initializer, but not when parsing the body of a - /// class or function definition. - class ParsingDeclRAIIObject { - Sema &Actions; - sema::DelayedDiagnosticPool DiagnosticPool; - Sema::ParsingDeclState State; - bool Popped; - - ParsingDeclRAIIObject(const ParsingDeclRAIIObject &) = delete; - void operator=(const ParsingDeclRAIIObject &) = delete; - - public: - enum NoParent_t { NoParent }; - ParsingDeclRAIIObject(Parser &P, NoParent_t _) - : Actions(P.getActions()), DiagnosticPool(nullptr) { - push(); - } - - /// Creates a RAII object whose pool is optionally parented by another. - ParsingDeclRAIIObject(Parser &P, - const sema::DelayedDiagnosticPool *parentPool) - : Actions(P.getActions()), DiagnosticPool(parentPool) { - push(); - } - - /// Creates a RAII object and, optionally, initialize its - /// diagnostics pool by stealing the diagnostics from another - /// RAII object (which is assumed to be the current top pool). - ParsingDeclRAIIObject(Parser &P, ParsingDeclRAIIObject *other) - : Actions(P.getActions()), - DiagnosticPool(other ? other->DiagnosticPool.getParent() : nullptr) { - if (other) { - DiagnosticPool.steal(other->DiagnosticPool); - other->abort(); - } - push(); - } - - ~ParsingDeclRAIIObject() { - abort(); - } - - sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() { - return DiagnosticPool; - } - const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const { - return DiagnosticPool; - } - - /// Resets the RAII object for a new declaration. - void reset() { - abort(); - push(); - } - - /// Signals that the context was completed without an appropriate - /// declaration being parsed. - void abort() { - pop(nullptr); - } - - void complete(Decl *D) { - assert(!Popped && "ParsingDeclaration has already been popped!"); - pop(D); - } - - /// Unregister this object from Sema, but remember all the - /// diagnostics that were emitted into it. - void abortAndRemember() { - pop(nullptr); - } - - private: - void push() { - State = Actions.PushParsingDeclaration(DiagnosticPool); - Popped = false; - } - - void pop(Decl *D) { - if (!Popped) { - Actions.PopParsingDeclaration(State, D); - Popped = true; - } - } - }; - - /// A class for parsing a DeclSpec. - class ParsingDeclSpec : public DeclSpec { - ParsingDeclRAIIObject ParsingRAII; - - public: - ParsingDeclSpec(Parser &P) - : DeclSpec(P.getAttrFactory()), - ParsingRAII(P, ParsingDeclRAIIObject::NoParent) {} - ParsingDeclSpec(Parser &P, ParsingDeclRAIIObject *RAII) - : DeclSpec(P.getAttrFactory()), - ParsingRAII(P, RAII) {} - - const sema::DelayedDiagnosticPool &getDelayedDiagnosticPool() const { - return ParsingRAII.getDelayedDiagnosticPool(); - } - - void complete(Decl *D) { - ParsingRAII.complete(D); - } - - void abort() { - ParsingRAII.abort(); - } - }; - - /// A class for parsing a declarator. - class ParsingDeclarator : public Declarator { - ParsingDeclRAIIObject ParsingRAII; - - public: - ParsingDeclarator(Parser &P, const ParsingDeclSpec &DS, TheContext C) - : Declarator(DS, C), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) { - } - - const ParsingDeclSpec &getDeclSpec() const { - return static_cast<const ParsingDeclSpec&>(Declarator::getDeclSpec()); - } - - ParsingDeclSpec &getMutableDeclSpec() const { - return const_cast<ParsingDeclSpec&>(getDeclSpec()); - } - - void clear() { - Declarator::clear(); - ParsingRAII.reset(); - } - - void complete(Decl *D) { - ParsingRAII.complete(D); - } - }; - - /// A class for parsing a field declarator. - class ParsingFieldDeclarator : public FieldDeclarator { - ParsingDeclRAIIObject ParsingRAII; - - public: - ParsingFieldDeclarator(Parser &P, const ParsingDeclSpec &DS) - : FieldDeclarator(DS), ParsingRAII(P, &DS.getDelayedDiagnosticPool()) { - } - - const ParsingDeclSpec &getDeclSpec() const { - return static_cast<const ParsingDeclSpec&>(D.getDeclSpec()); - } - - ParsingDeclSpec &getMutableDeclSpec() const { - return const_cast<ParsingDeclSpec&>(getDeclSpec()); - } - - void complete(Decl *D) { - ParsingRAII.complete(D); - } - }; - - /// ExtensionRAIIObject - This saves the state of extension warnings when - /// constructed and disables them. When destructed, it restores them back to - /// the way they used to be. This is used to handle __extension__ in the - /// parser. - class ExtensionRAIIObject { - ExtensionRAIIObject(const ExtensionRAIIObject &) = delete; - void operator=(const ExtensionRAIIObject &) = delete; - - DiagnosticsEngine &Diags; - public: - ExtensionRAIIObject(DiagnosticsEngine &diags) : Diags(diags) { - Diags.IncrementAllExtensionsSilenced(); - } - - ~ExtensionRAIIObject() { - Diags.DecrementAllExtensionsSilenced(); - } - }; - - /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and - /// restores it when destroyed. This says that "foo:" should not be - /// considered a possible typo for "foo::" for error recovery purposes. - class ColonProtectionRAIIObject { - Parser &P; - bool OldVal; - public: - ColonProtectionRAIIObject(Parser &p, bool Value = true) - : P(p), OldVal(P.ColonIsSacred) { - P.ColonIsSacred = Value; - } - - /// restore - This can be used to restore the state early, before the dtor - /// is run. - void restore() { - P.ColonIsSacred = OldVal; - } - - ~ColonProtectionRAIIObject() { - restore(); - } - }; - - /// \brief RAII object that makes '>' behave either as an operator - /// or as the closing angle bracket for a template argument list. - class GreaterThanIsOperatorScope { - bool &GreaterThanIsOperator; - bool OldGreaterThanIsOperator; - public: - GreaterThanIsOperatorScope(bool >IO, bool Val) - : GreaterThanIsOperator(GTIO), OldGreaterThanIsOperator(GTIO) { - GreaterThanIsOperator = Val; - } - - ~GreaterThanIsOperatorScope() { - GreaterThanIsOperator = OldGreaterThanIsOperator; - } - }; - - class InMessageExpressionRAIIObject { - bool &InMessageExpression; - bool OldValue; - - public: - InMessageExpressionRAIIObject(Parser &P, bool Value) - : InMessageExpression(P.InMessageExpression), - OldValue(P.InMessageExpression) { - InMessageExpression = Value; - } - - ~InMessageExpressionRAIIObject() { - InMessageExpression = OldValue; - } - }; - - /// \brief RAII object that makes sure paren/bracket/brace count is correct - /// after declaration/statement parsing, even when there's a parsing error. - class ParenBraceBracketBalancer { - Parser &P; - unsigned short ParenCount, BracketCount, BraceCount; - public: - ParenBraceBracketBalancer(Parser &p) - : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount), - BraceCount(p.BraceCount) { } - - ~ParenBraceBracketBalancer() { - P.ParenCount = ParenCount; - P.BracketCount = BracketCount; - P.BraceCount = BraceCount; - } - }; - - class PoisonSEHIdentifiersRAIIObject { - PoisonIdentifierRAIIObject Ident_AbnormalTermination; - PoisonIdentifierRAIIObject Ident_GetExceptionCode; - PoisonIdentifierRAIIObject Ident_GetExceptionInfo; - PoisonIdentifierRAIIObject Ident__abnormal_termination; - PoisonIdentifierRAIIObject Ident__exception_code; - PoisonIdentifierRAIIObject Ident__exception_info; - PoisonIdentifierRAIIObject Ident___abnormal_termination; - PoisonIdentifierRAIIObject Ident___exception_code; - PoisonIdentifierRAIIObject Ident___exception_info; - public: - PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue) - : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue), - Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue), - Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue), - Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue), - Ident__exception_code(Self.Ident__exception_code, NewValue), - Ident__exception_info(Self.Ident__exception_info, NewValue), - Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue), - Ident___exception_code(Self.Ident___exception_code, NewValue), - Ident___exception_info(Self.Ident___exception_info, NewValue) { - } - }; - - /// \brief RAII class that helps handle the parsing of an open/close delimiter - /// pair, such as braces { ... } or parentheses ( ... ). - class BalancedDelimiterTracker : public GreaterThanIsOperatorScope { - Parser& P; - tok::TokenKind Kind, Close, FinalToken; - SourceLocation (Parser::*Consumer)(); - SourceLocation LOpen, LClose; - - unsigned short &getDepth() { - switch (Kind) { - case tok::l_brace: return P.BraceCount; - case tok::l_square: return P.BracketCount; - case tok::l_paren: return P.ParenCount; - default: llvm_unreachable("Wrong token kind"); - } - } - - enum { MaxDepth = 256 }; - - bool diagnoseOverflow(); - bool diagnoseMissingClose(); - - public: - BalancedDelimiterTracker(Parser& p, tok::TokenKind k, - tok::TokenKind FinalToken = tok::semi) - : GreaterThanIsOperatorScope(p.GreaterThanIsOperator, true), - P(p), Kind(k), FinalToken(FinalToken) - { - switch (Kind) { - default: llvm_unreachable("Unexpected balanced token"); - case tok::l_brace: - Close = tok::r_brace; - Consumer = &Parser::ConsumeBrace; - break; - case tok::l_paren: - Close = tok::r_paren; - Consumer = &Parser::ConsumeParen; - break; - - case tok::l_square: - Close = tok::r_square; - Consumer = &Parser::ConsumeBracket; - break; - } - } - - SourceLocation getOpenLocation() const { return LOpen; } - SourceLocation getCloseLocation() const { return LClose; } - SourceRange getRange() const { return SourceRange(LOpen, LClose); } - - bool consumeOpen() { - if (!P.Tok.is(Kind)) - return true; - - if (getDepth() < P.getLangOpts().BracketDepth) { - LOpen = (P.*Consumer)(); - return false; - } - - return diagnoseOverflow(); - } - - bool expectAndConsume(unsigned DiagID = diag::err_expected, - const char *Msg = "", - tok::TokenKind SkipToTok = tok::unknown); - bool consumeClose() { - if (P.Tok.is(Close)) { - LClose = (P.*Consumer)(); - return false; - } else if (P.Tok.is(tok::semi) && P.NextToken().is(Close)) { - SourceLocation SemiLoc = P.ConsumeToken(); - P.Diag(SemiLoc, diag::err_unexpected_semi) - << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc)); - LClose = (P.*Consumer)(); - return false; - } - - return diagnoseMissingClose(); - } - void skipToEnd(); - }; - -} // end namespace clang - -#endif |