diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-05-09 03:59:21 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-05-09 03:59:21 +0000 |
commit | 7a6d690bb52df60e979a7c7f9d03a774009bb443 (patch) | |
tree | d7e8e354720db8f569f17d421af83536ecfb4ebe /lib/Parse/ParseDecl.cpp | |
parent | b14b99abefb0dd40b13dd70c99d87d04a66b3236 (diff) | |
download | clang-7a6d690bb52df60e979a7c7f9d03a774009bb443.tar.gz |
[c++20] Add support for explicit(bool), as described in P0892R2.
Patch by Tyker!
Differential Revision: https://reviews.llvm.org/D60934
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@360311 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseDecl.cpp')
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 62 |
1 files changed, 49 insertions, 13 deletions
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 9c9a9f4679..7b4ca124dc 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2480,12 +2480,12 @@ void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); if (DS.isVirtualSpecified()) Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); - if (DS.isExplicitSpecified()) + if (DS.hasExplicitSpecifier()) Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); DS.ClearFunctionSpecs(); } - // Issue diagnostic and remove constexpr specfier if present. + // Issue diagnostic and remove constexpr specifier if present. if (DS.isConstexprSpecified() && DSC != DeclSpecContext::DSC_condition) { Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); DS.ClearConstexprSpec(); @@ -3004,9 +3004,13 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, while (1) { bool isInvalid = false; bool isStorageClass = false; + bool isAlreadyConsumed = false; const char *PrevSpec = nullptr; unsigned DiagID = 0; + // This value need to be set when isAlreadyConsumed is set to true. + SourceLocation RangeEnd; + // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL // implementation for VS2013 uses _Atomic as an identifier for one of the // classes in <atomic>. @@ -3558,9 +3562,34 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); } break; - case tok::kw_explicit: - isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); + case tok::kw_explicit: { + SourceLocation ExplicitLoc = Loc; + SourceLocation CloseParenLoc; + ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue); + isAlreadyConsumed = true; + RangeEnd = ExplicitLoc; + ConsumeToken(); // kw_explicit + if (Tok.is(tok::l_paren)) { + if (getLangOpts().CPlusPlus2a) { + ExprResult ExplicitExpr(static_cast<Expr *>(nullptr)); + BalancedDelimiterTracker Tracker(*this, tok::l_paren); + Tracker.consumeOpen(); + ExplicitExpr = ParseConstantExpression(); + RangeEnd = Tok.getLocation(); + if (ExplicitExpr.isUsable()) { + CloseParenLoc = Tok.getLocation(); + Tracker.consumeClose(); + ExplicitSpec = + Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get()); + } else + Tracker.skipToEnd(); + } else + Diag(Tok.getLocation(), diag::warn_cxx2a_compat_explicit_bool); + } + isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID, + ExplicitSpec, CloseParenLoc); break; + } case tok::kw__Noreturn: if (!getLangOpts().C11) Diag(Loc, diag::ext_c11_noreturn); @@ -3904,25 +3933,32 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, // If a type specifier follows, it will be diagnosed elsewhere. continue; } + + assert(!isAlreadyConsumed || RangeEnd != SourceLocation() && + "both or neither of isAlreadyConsumed and " + "RangeEnd needs to be set"); + DS.SetRangeEnd(isAlreadyConsumed ? RangeEnd : Tok.getLocation()); + // If the specifier wasn't legal, issue a diagnostic. if (isInvalid) { assert(PrevSpec && "Method did not return previous specifier!"); assert(DiagID); if (DiagID == diag::ext_duplicate_declspec || - DiagID == diag::ext_warn_duplicate_declspec) - Diag(Tok, DiagID) - << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); + DiagID == diag::ext_warn_duplicate_declspec || + DiagID == diag::err_duplicate_declspec) + Diag(Loc, DiagID) << PrevSpec + << FixItHint::CreateRemoval( + SourceRange(Loc, DS.getEndLoc())); else if (DiagID == diag::err_opencl_unknown_type_specifier) { - Diag(Tok, DiagID) << getLangOpts().OpenCLCPlusPlus - << getLangOpts().getOpenCLVersionTuple().getAsString() - << PrevSpec << isStorageClass; + Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus + << getLangOpts().getOpenCLVersionTuple().getAsString() + << PrevSpec << isStorageClass; } else - Diag(Tok, DiagID) << PrevSpec; + Diag(Loc, DiagID) << PrevSpec; } - DS.SetRangeEnd(Tok.getLocation()); - if (DiagID != diag::err_bool_redeclaration) + if (DiagID != diag::err_bool_redeclaration && !isAlreadyConsumed) // After an error the next token can be an annotation token. ConsumeAnyToken(); |