From a70a52b29e9c951cc0a94f22e6653522f49b5d2d Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Fri, 29 Aug 2014 19:36:52 +0000 Subject: [PATCH] unique_ptrify Preprocessor's TokenLexerCache git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@216756 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/Preprocessor.h | 2 +- lib/Lex/PPLexerChange.cpp | 22 +++++++++++----------- lib/Lex/Preprocessor.cpp | 3 +-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index e1896e4789..8e352b78e1 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -395,7 +395,7 @@ class Preprocessor : public RefCountedBase { /// \brief Cache of macro expanders to reduce malloc traffic. enum { TokenLexerCacheSize = 8 }; unsigned NumCachedTokenLexers; - TokenLexer *TokenLexerCache[TokenLexerCacheSize]; + std::unique_ptr TokenLexerCache[TokenLexerCacheSize]; /// \} /// \brief Keeps macro expanded tokens for TokenLexers. diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp index 22ee971f48..1ce3796aa7 100644 --- a/lib/Lex/PPLexerChange.cpp +++ b/lib/Lex/PPLexerChange.cpp @@ -160,17 +160,17 @@ void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, /// tokens from it instead of the current buffer. void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args) { - TokenLexer *TokLexer; + std::unique_ptr TokLexer; if (NumCachedTokenLexers == 0) { - TokLexer = new TokenLexer(Tok, ILEnd, Macro, Args, *this); + TokLexer = llvm::make_unique(Tok, ILEnd, Macro, Args, *this); } else { - TokLexer = TokenLexerCache[--NumCachedTokenLexers]; + TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); TokLexer->Init(Tok, ILEnd, Macro, Args); } PushIncludeMacroStack(); CurDirLookup = nullptr; - CurTokenLexer.reset(TokLexer); + CurTokenLexer = std::move(TokLexer); if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_TokenLexer; } @@ -191,19 +191,19 @@ void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, bool DisableMacroExpansion, bool OwnsTokens) { // Create a macro expander to expand from the specified token stream. - TokenLexer *TokLexer; + std::unique_ptr TokLexer; if (NumCachedTokenLexers == 0) { - TokLexer = new TokenLexer(Toks, NumToks, DisableMacroExpansion, - OwnsTokens, *this); + TokLexer = llvm::make_unique( + Toks, NumToks, DisableMacroExpansion, OwnsTokens, *this); } else { - TokLexer = TokenLexerCache[--NumCachedTokenLexers]; + TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); } // Save our current state. PushIncludeMacroStack(); CurDirLookup = nullptr; - CurTokenLexer.reset(TokLexer); + CurTokenLexer = std::move(TokLexer); if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_TokenLexer; } @@ -526,7 +526,7 @@ bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { if (NumCachedTokenLexers == TokenLexerCacheSize) CurTokenLexer.reset(); else - TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.release(); + TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); // Handle this like a #include file being popped off the stack. return HandleEndOfFile(Result, true); @@ -543,7 +543,7 @@ void Preprocessor::RemoveTopOfLexerStack() { if (NumCachedTokenLexers == TokenLexerCacheSize) CurTokenLexer.reset(); else - TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.release(); + TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); } PopIncludeMacroStack(); diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index faf5bbb38b..2c23d68282 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -151,8 +151,7 @@ Preprocessor::~Preprocessor() { // Free any cached macro expanders. // This populates MacroArgCache, so all TokenLexers need to be destroyed // before the code below that frees up the MacroArgCache list. - for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) - delete TokenLexerCache[i]; + std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr); CurTokenLexer.reset(); while (DeserializedMacroInfoChain *I = DeserialMIChainHead) { -- 2.40.0