From a31043191688f3dce6f7f24908cc260965de47d1 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Tue, 9 Feb 2016 18:52:09 +0000 Subject: Simplify EnterTokenStream API to make it more robust for memory management While this won't help fix things like the bug that r260219 addressed, it seems like good tidy up to have anyway. (it might be nice if "makeArrayRef" always produced a MutableArrayRef & let it decay to an ArrayRef when needed - then I'd use that for the MutableArrayRefs in this patch) If we had std::dynarray I'd use that instead of unique_ptr+size_t, ideally (but then it'd have to be threaded down through the Preprocessor all the way - no idea how painful that would be) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260246 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/Preprocessor.h | 12 ++- lib/Frontend/PrintPreprocessedOutput.cpp | 7 +- lib/Lex/PPDirectives.cpp | 8 +- lib/Lex/PPMacroExpansion.cpp | 11 ++- lib/Lex/Pragma.cpp | 12 +-- lib/Parse/ParseCXXInlineMethods.cpp | 14 ++-- lib/Parse/ParseDecl.cpp | 2 +- lib/Parse/ParseExprCXX.cpp | 3 +- lib/Parse/ParseObjc.cpp | 4 +- lib/Parse/ParsePragma.cpp | 138 +++++++++++++------------------ lib/Parse/ParseStmtAsm.cpp | 4 +- lib/Parse/ParseTemplate.cpp | 2 +- lib/Rewrite/HTMLRewrite.cpp | 2 +- 13 files changed, 99 insertions(+), 120 deletions(-) diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 5ffa48277f..1031cdac9f 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -1018,10 +1018,20 @@ public: /// If \p OwnsTokens is false, this method assumes that the specified stream /// of tokens has a permanent owner somewhere, so they do not need to be /// copied. If it is true, it assumes the array of tokens is allocated with - /// \c new[] and must be freed. + /// \c new[] and the Preprocessor will delete[] it. +private: void EnterTokenStream(const Token *Toks, unsigned NumToks, bool DisableMacroExpansion, bool OwnsTokens); +public: + void EnterTokenStream(std::unique_ptr Toks, unsigned NumToks, + bool DisableMacroExpansion) { + EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true); + } + void EnterTokenStream(ArrayRef Toks, bool DisableMacroExpansion) { + EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false); + } + /// \brief Pop the current lexer/macro exp off the top of the lexer stack. /// /// This should only be used in situations where the current state of the diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index eee9da8594..8a90b56ff3 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -580,11 +580,10 @@ struct UnknownPragmaHandler : public PragmaHandler { if (ShouldExpandTokens) { // The first token does not have expanded macros. Expand them, if // required. - Token *Toks = new Token[1]; + auto Toks = llvm::make_unique(1); Toks[0] = PragmaTok; - PP.EnterTokenStream(Toks, /*NumToks=*/1, - /*DisableMacroExpansion=*/false, - /*OwnsTokens=*/true); + PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1, + /*DisableMacroExpansion=*/false); PP.Lex(PragmaTok); } Token PrevToken; diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index c02a0cb8d3..1c74a1702a 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -907,7 +907,7 @@ void Preprocessor::HandleDirective(Token &Result) { // various pseudo-ops. Just return the # token and push back the following // token to be lexed next time. if (getLangOpts().AsmPreprocessor) { - Token *Toks = new Token[2]; + auto Toks = llvm::make_unique(2); // Return the # and the token after it. Toks[0] = SavedHash; Toks[1] = Result; @@ -920,7 +920,7 @@ void Preprocessor::HandleDirective(Token &Result) { // Enter this token stream so that we re-lex the tokens. Make sure to // enable macro expansion, in case the token after the # is an identifier // that is expanded. - EnterTokenStream(Toks, 2, false, true); + EnterTokenStream(std::move(Toks), 2, false); return; } @@ -1442,13 +1442,13 @@ static void EnterAnnotationToken(Preprocessor &PP, tok::TokenKind Kind, void *AnnotationVal) { // FIXME: Produce this as the current token directly, rather than // allocating a new token for it. - Token *Tok = new Token[1]; + auto Tok = llvm::make_unique(1); Tok[0].startToken(); Tok[0].setKind(Kind); Tok[0].setLocation(Begin); Tok[0].setAnnotationEndLoc(End); Tok[0].setAnnotationValue(AnnotationVal); - PP.EnterTokenStream(Tok, 1, true, true); + PP.EnterTokenStream(std::move(Tok), 1, true); } /// \brief Produce a diagnostic informing the user that a #include or similar diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index 11b4a0b3d8..69f5bc8ddc 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -755,13 +755,12 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, // Do not lose the EOF/EOD. Return it to the client. MacroName = Tok; return nullptr; - } else { - // Do not lose the EOF/EOD. - Token *Toks = new Token[1]; - Toks[0] = Tok; - EnterTokenStream(Toks, 1, true, true); - break; } + // Do not lose the EOF/EOD. + auto Toks = llvm::make_unique(1); + Toks[0] = Tok; + EnterTokenStream(std::move(Toks), 1, true); + break; } else if (Tok.is(tok::r_paren)) { // If we found the ) token, the macro arg list is done. if (NumParens-- == 0) { diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index afb41a2407..fea66daa5f 100644 --- a/lib/Lex/Pragma.cpp +++ b/lib/Lex/Pragma.cpp @@ -938,13 +938,13 @@ struct PragmaDebugHandler : public PragmaHandler { } SourceLocation NameLoc = Tok.getLocation(); - Token *Toks = PP.getPreprocessorAllocator().Allocate(1); - Toks->startToken(); - Toks->setKind(tok::annot_pragma_captured); - Toks->setLocation(NameLoc); + MutableArrayRef Toks( + PP.getPreprocessorAllocator().Allocate(1), 1); + Toks[0].startToken(); + Toks[0].setKind(tok::annot_pragma_captured); + Toks[0].setLocation(NameLoc); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } // Disable MSVC warning about runtime stack overflow. diff --git a/lib/Parse/ParseCXXInlineMethods.cpp b/lib/Parse/ParseCXXInlineMethods.cpp index e536644d5b..89ef35c88b 100644 --- a/lib/Parse/ParseCXXInlineMethods.cpp +++ b/lib/Parse/ParseCXXInlineMethods.cpp @@ -325,7 +325,7 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { // Parse the default argument from its saved token stream. Toks->push_back(Tok); // So that the current token doesn't get lost - PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); + PP.EnterTokenStream(*Toks, true); // Consume the previously-pushed token. ConsumeAnyToken(); @@ -399,7 +399,7 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { // Parse the default argument from its saved token stream. Toks->push_back(Tok); // So that the current token doesn't get lost - PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); + PP.EnterTokenStream(*Toks, true); // Consume the previously-pushed token. ConsumeAnyToken(); @@ -504,7 +504,7 @@ void Parser::ParseLexedMethodDef(LexedMethod &LM) { // Append the current token at the end of the new token stream so that it // doesn't get lost. LM.Toks.push_back(Tok); - PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); + PP.EnterTokenStream(LM.Toks, true); // Consume the previously pushed token. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); @@ -617,7 +617,7 @@ void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { // Append the current token at the end of the new token stream so that it // doesn't get lost. MI.Toks.push_back(Tok); - PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); + PP.EnterTokenStream(MI.Toks, true); // Consume the previously pushed token. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); @@ -971,10 +971,10 @@ public: // Put back the original tokens. Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch); if (Toks.size()) { - Token *Buffer = new Token[Toks.size()]; - std::copy(Toks.begin() + 1, Toks.end(), Buffer); + auto Buffer = llvm::make_unique(Toks.size()); + std::copy(Toks.begin() + 1, Toks.end(), Buffer.get()); Buffer[Toks.size() - 1] = Self.Tok; - Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true); + Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true); Self.Tok = Toks.front(); } diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 3097d28af1..900c33b791 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -1187,7 +1187,7 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA, // Append the current token at the end of the new token stream so that it // doesn't get lost. LA.Toks.push_back(Tok); - PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); + PP.EnterTokenStream(LA.Toks, true); // Consume the previously pushed token. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp index 760c30b812..558d2371f6 100644 --- a/lib/Parse/ParseExprCXX.cpp +++ b/lib/Parse/ParseExprCXX.cpp @@ -3093,8 +3093,7 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, Toks.push_back(Tok); // Re-enter the stored parenthesized tokens into the token stream, so we may // parse them now. - PP.EnterTokenStream(Toks.data(), Toks.size(), - true/*DisableMacroExpansion*/, false/*OwnsTokens*/); + PP.EnterTokenStream(Toks, true /*DisableMacroExpansion*/); // Drop the current token and bring the first cached one. It's the same token // as when we entered this function. ConsumeAnyToken(); diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp index 5cd7e7ab7e..6a680907cc 100644 --- a/lib/Parse/ParseObjc.cpp +++ b/lib/Parse/ParseObjc.cpp @@ -3585,8 +3585,8 @@ void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) { // Append the current token at the end of the new token stream so that it // doesn't get lost. LM.Toks.push_back(Tok); - PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); - + PP.EnterTokenStream(LM.Toks, true); + // Consume the previously pushed token. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp index bc70942851..42f8b518b1 100644 --- a/lib/Parse/ParsePragma.cpp +++ b/lib/Parse/ParsePragma.cpp @@ -507,8 +507,9 @@ void Parser::HandlePragmaMSVtorDisp() { void Parser::HandlePragmaMSPragma() { assert(Tok.is(tok::annot_pragma_ms_pragma)); // Grab the tokens out of the annotation and enter them into the stream. - auto TheTokens = (std::pair *)Tok.getAnnotationValue(); - PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true); + auto TheTokens = + (std::pair, size_t> *)Tok.getAnnotationValue(); + PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true); SourceLocation PragmaLocation = ConsumeToken(); // The annotation token. assert(Tok.isAnyIdentifier()); StringRef PragmaName = Tok.getIdentifierInfo()->getName(); @@ -798,14 +799,13 @@ bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { Hint.OptionLoc = IdentifierLoc::create( Actions.Context, Info->Option.getLocation(), OptionInfo); - const Token *Toks = Info->Toks.data(); - size_t TokSize = Info->Toks.size(); + llvm::ArrayRef Toks = Info->Toks; // Return a valid hint if pragma unroll or nounroll were specified // without an argument. bool PragmaUnroll = PragmaNameInfo->getName() == "unroll"; bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll"; - if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) { + if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) { ConsumeToken(); // The annotation token. Hint.Range = Info->PragmaName.getLocation(); return true; @@ -813,7 +813,7 @@ bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { // The constant expression is always followed by an eof token, which increases // the TokSize by 1. - assert(TokSize > 0 && + assert(!Toks.empty() && "PragmaLoopHintInfo::Toks must contain at least one token."); // If no option is specified the argument is assumed to be a constant expr. @@ -849,14 +849,13 @@ bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { << /*FullKeyword=*/OptionUnroll; return false; } - if (TokSize > 2) + if (Toks.size() > 2) Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << PragmaLoopHintString(Info->PragmaName, Info->Option); Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo); } else { // Enter constant expression including eof terminator into token stream. - PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false); ConsumeToken(); // The annotation token. ExprResult R = ParseConstantExpression(); @@ -881,7 +880,7 @@ bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { } Hint.Range = SourceRange(Info->PragmaName.getLocation(), - Info->Toks[TokSize - 1].getLocation()); + Info->Toks.back().getLocation()); return true; } @@ -934,15 +933,14 @@ void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, return; } - Token *Toks = new Token[1]; + auto Toks = llvm::make_unique(1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_vis); Toks[0].setLocation(VisLoc); Toks[0].setAnnotationEndLoc(EndLoc); Toks[0].setAnnotationValue( const_cast(static_cast(VisType))); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/true); + PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true); } // #pragma pack(...) comes in the following delicious flavors: @@ -1041,27 +1039,22 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP, return; } - PragmaPackInfo *Info = - (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate( - sizeof(PragmaPackInfo), llvm::alignOf()); - new (Info) PragmaPackInfo(); + PragmaPackInfo *Info = + PP.getPreprocessorAllocator().Allocate(1); Info->Kind = Kind; Info->Name = Name; Info->Alignment = Alignment; Info->LParenLoc = LParenLoc; Info->RParenLoc = RParenLoc; - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 1, llvm::alignOf()); - new (Toks) Token(); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(1), + 1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_pack); Toks[0].setLocation(PackLoc); Toks[0].setAnnotationEndLoc(RParenLoc); Toks[0].setAnnotationValue(static_cast(Info)); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } // #pragma ms_struct on @@ -1096,18 +1089,15 @@ void PragmaMSStructHandler::HandlePragma(Preprocessor &PP, return; } - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 1, llvm::alignOf()); - new (Toks) Token(); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(1), + 1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_msstruct); Toks[0].setLocation(MSStructTok.getLocation()); Toks[0].setAnnotationEndLoc(EndLoc); Toks[0].setAnnotationValue(reinterpret_cast( static_cast(Kind))); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} @@ -1167,18 +1157,15 @@ static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok, return; } - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 1, llvm::alignOf()); - new (Toks) Token(); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(1), + 1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_align); Toks[0].setLocation(FirstTok.getLocation()); Toks[0].setAnnotationEndLoc(EndLoc); Toks[0].setAnnotationValue(reinterpret_cast( static_cast(Kind))); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } void PragmaAlignHandler::HandlePragma(Preprocessor &PP, @@ -1260,9 +1247,9 @@ void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, // This allows us to cache a "#pragma unused" that occurs inside an inline // C++ member function. - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf()); + MutableArrayRef Toks( + PP.getPreprocessorAllocator().Allocate(2 * Identifiers.size()), + 2 * Identifiers.size()); for (unsigned i=0; i != Identifiers.size(); i++) { Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1]; pragmaUnusedTok.startToken(); @@ -1270,8 +1257,7 @@ void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, pragmaUnusedTok.setLocation(UnusedLoc); idTok = Identifiers[i]; } - PP.EnterTokenStream(Toks, 2*Identifiers.size(), - /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } // #pragma weak identifier @@ -1311,9 +1297,8 @@ void PragmaWeakHandler::HandlePragma(Preprocessor &PP, } if (HasAlias) { - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 3, llvm::alignOf()); + MutableArrayRef Toks( + PP.getPreprocessorAllocator().Allocate(3), 3); Token &pragmaUnusedTok = Toks[0]; pragmaUnusedTok.startToken(); pragmaUnusedTok.setKind(tok::annot_pragma_weakalias); @@ -1321,20 +1306,17 @@ void PragmaWeakHandler::HandlePragma(Preprocessor &PP, pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation()); Toks[1] = WeakName; Toks[2] = AliasName; - PP.EnterTokenStream(Toks, 3, - /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } else { - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 2, llvm::alignOf()); + MutableArrayRef Toks( + PP.getPreprocessorAllocator().Allocate(2), 2); Token &pragmaUnusedTok = Toks[0]; pragmaUnusedTok.startToken(); pragmaUnusedTok.setKind(tok::annot_pragma_weak); pragmaUnusedTok.setLocation(WeakLoc); pragmaUnusedTok.setAnnotationEndLoc(WeakLoc); Toks[1] = WeakName; - PP.EnterTokenStream(Toks, 2, - /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } } @@ -1370,9 +1352,8 @@ void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, return; } - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 3, llvm::alignOf()); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(3), + 3); Token &pragmaRedefTok = Toks[0]; pragmaRedefTok.startToken(); pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname); @@ -1380,8 +1361,7 @@ void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation()); Toks[1] = RedefName; Toks[2] = AliasName; - PP.EnterTokenStream(Toks, 3, - /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } @@ -1393,18 +1373,15 @@ PragmaFPContractHandler::HandlePragma(Preprocessor &PP, if (PP.LexOnOffSwitch(OOS)) return; - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 1, llvm::alignOf()); - new (Toks) Token(); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(1), + 1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_fp_contract); Toks[0].setLocation(Tok.getLocation()); Toks[0].setAnnotationEndLoc(Tok.getLocation()); Toks[0].setAnnotationValue(reinterpret_cast( static_cast(OOS))); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); } void @@ -1452,17 +1429,14 @@ PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP, } OpenCLExtData data(ename, state); - Token *Toks = - (Token*) PP.getPreprocessorAllocator().Allocate( - sizeof(Token) * 1, llvm::alignOf()); - new (Toks) Token(); + MutableArrayRef Toks(PP.getPreprocessorAllocator().Allocate(1), + 1); Toks[0].startToken(); Toks[0].setKind(tok::annot_pragma_opencl_extension); Toks[0].setLocation(NameLoc); Toks[0].setAnnotationValue(data.getOpaqueValue()); Toks[0].setAnnotationEndLoc(StateLoc); - PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, - /*OwnsTokens=*/false); + PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); if (PP.getPPCallbacks()) PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename, @@ -1506,10 +1480,10 @@ PragmaOpenMPHandler::HandlePragma(Preprocessor &PP, Tok.setLocation(EodLoc); Pragma.push_back(Tok); - Token *Toks = new Token[Pragma.size()]; - std::copy(Pragma.begin(), Pragma.end(), Toks); - PP.EnterTokenStream(Toks, Pragma.size(), - /*DisableMacroExpansion=*/false, /*OwnsTokens=*/true); + auto Toks = llvm::make_unique(Pragma.size()); + std::copy(Pragma.begin(), Pragma.end(), Toks.get()); + PP.EnterTokenStream(std::move(Toks), Pragma.size(), + /*DisableMacroExpansion=*/false); } /// \brief Handle '#pragma pointers_to_members' @@ -1725,10 +1699,11 @@ void PragmaMSPragma::HandlePragma(Preprocessor &PP, TokenVector.push_back(EoF); // We must allocate this array with new because EnterTokenStream is going to // delete it later. - Token *TokenArray = new Token[TokenVector.size()]; - std::copy(TokenVector.begin(), TokenVector.end(), TokenArray); + auto TokenArray = llvm::make_unique(TokenVector.size()); + std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get()); auto Value = new (PP.getPreprocessorAllocator()) - std::pair(std::make_pair(TokenArray, TokenVector.size())); + std::pair, size_t>(std::move(TokenArray), + TokenVector.size()); AnnotTok.setAnnotationValue(Value); PP.EnterToken(AnnotTok); } @@ -2059,12 +2034,11 @@ void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP, return; } - Token *TokenArray = new Token[TokenList.size()]; - std::copy(TokenList.begin(), TokenList.end(), TokenArray); + auto TokenArray = llvm::make_unique(TokenList.size()); + std::copy(TokenList.begin(), TokenList.end(), TokenArray.get()); - PP.EnterTokenStream(TokenArray, TokenList.size(), - /*DisableMacroExpansion=*/false, - /*OwnsTokens=*/true); + PP.EnterTokenStream(std::move(TokenArray), TokenList.size(), + /*DisableMacroExpansion=*/false); } /// \brief Handle the loop unroll optimization pragmas. @@ -2127,12 +2101,12 @@ void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP, } // Generate the hint token. - Token *TokenArray = new Token[1]; + auto TokenArray = llvm::make_unique(1); TokenArray[0].startToken(); TokenArray[0].setKind(tok::annot_pragma_loop_hint); TokenArray[0].setLocation(PragmaName.getLocation()); TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation()); TokenArray[0].setAnnotationValue(static_cast(Info)); - PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false, - /*OwnsTokens=*/true); + PP.EnterTokenStream(std::move(TokenArray), 1, + /*DisableMacroExpansion=*/false); } diff --git a/lib/Parse/ParseStmtAsm.cpp b/lib/Parse/ParseStmtAsm.cpp index d3a86362c8..3a23622670 100644 --- a/lib/Parse/ParseStmtAsm.cpp +++ b/lib/Parse/ParseStmtAsm.cpp @@ -200,9 +200,7 @@ ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl &LineToks, // Also copy the current token over. LineToks.push_back(Tok); - PP.EnterTokenStream(LineToks.begin(), LineToks.size(), - /*disable macros*/ true, - /*owns tokens*/ false); + PP.EnterTokenStream(LineToks, /*DisableMacroExpansions*/ true); // Clear the current token and advance to the first token in LineToks. ConsumeAnyToken(); diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index e6ce30a659..185497a8be 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -1369,7 +1369,7 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { // Append the current token at the end of the new token stream so that it // doesn't get lost. LPT.Toks.push_back(Tok); - PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false); + PP.EnterTokenStream(LPT.Toks, true); // Consume the previously pushed token. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); diff --git a/lib/Rewrite/HTMLRewrite.cpp b/lib/Rewrite/HTMLRewrite.cpp index 275fbd0ebc..2d82d8fd4b 100644 --- a/lib/Rewrite/HTMLRewrite.cpp +++ b/lib/Rewrite/HTMLRewrite.cpp @@ -502,7 +502,7 @@ void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) { // Enter the tokens we just lexed. This will cause them to be macro expanded // but won't enter sub-files (because we removed #'s). - TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false); + TmpPP.EnterTokenStream(TokenStream, false); TokenConcatenation ConcatInfo(TmpPP); -- cgit v1.2.3