Using llvm::OwningPtr<> for CurLexer and CurTokenLexer. This makes both the ownership semantics of these objects explicit within the Preprocessor and also tightens up the code (explicit deletes not needed).

llvm-svn: 59249
This commit is contained in:
Ted Kremenek 2008-11-13 17:11:24 +00:00
parent 7c1e61d78b
commit a0d2a1661a
3 changed files with 21 additions and 27 deletions

View File

@ -20,6 +20,7 @@
#include "clang/Basic/IdentifierTable.h" #include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/OwningPtr.h"
namespace clang { namespace clang {
@ -93,7 +94,7 @@ class Preprocessor {
/// CurLexer - This is the current top of the stack that we're lexing from if /// CurLexer - This is the current top of the stack that we're lexing from if
/// not expanding a macro. One of CurLexer and CurTokenLexer must be null. /// not expanding a macro. One of CurLexer and CurTokenLexer must be null.
Lexer *CurLexer; llvm::OwningPtr<Lexer> CurLexer;
/// CurLookup - The DirectoryLookup structure used to find the current /// CurLookup - The DirectoryLookup structure used to find the current
/// FileEntry, if CurLexer is non-null and if applicable. This allows us to /// FileEntry, if CurLexer is non-null and if applicable. This allows us to
@ -102,7 +103,7 @@ class Preprocessor {
/// CurTokenLexer - This is the current macro we are expanding, if we are /// CurTokenLexer - This is the current macro we are expanding, if we are
/// expanding a macro. One of CurLexer and CurTokenLexer must be null. /// expanding a macro. One of CurLexer and CurTokenLexer must be null.
TokenLexer *CurTokenLexer; llvm::OwningPtr<TokenLexer> CurTokenLexer;
/// IncludeMacroStack - This keeps track of the stack of files currently /// IncludeMacroStack - This keeps track of the stack of files currently
/// #included, and macros currently being expanded from, not counting /// #included, and macros currently being expanded from, not counting
@ -191,7 +192,7 @@ public:
/// isCurrentLexer - Return true if we are lexing directly from the specified /// isCurrentLexer - Return true if we are lexing directly from the specified
/// lexer. /// lexer.
bool isCurrentLexer(const Lexer *L) const { bool isCurrentLexer(const Lexer *L) const {
return CurLexer == L; return CurLexer.get() == L;
} }
/// getCurrentLexer - Return the current file lexer being lexed from. Note /// getCurrentLexer - Return the current file lexer being lexed from. Note
@ -484,16 +485,14 @@ public:
private: private:
void PushIncludeMacroStack() { void PushIncludeMacroStack() {
IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, IncludeMacroStack.push_back(IncludeStackInfo(CurLexer.take(), CurDirLookup,
CurTokenLexer)); CurTokenLexer.take()));
CurLexer = 0;
CurTokenLexer = 0;
} }
void PopIncludeMacroStack() { void PopIncludeMacroStack() {
CurLexer = IncludeMacroStack.back().TheLexer; CurLexer.reset(IncludeMacroStack.back().TheLexer);
CurDirLookup = IncludeMacroStack.back().TheDirLookup; CurDirLookup = IncludeMacroStack.back().TheDirLookup;
CurTokenLexer = IncludeMacroStack.back().TheTokenLexer; CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
IncludeMacroStack.pop_back(); IncludeMacroStack.pop_back();
} }

View File

@ -48,7 +48,7 @@ bool Preprocessor::isInPrimaryFile() const {
/// that this ignores any potentially active macro expansions and _Pragma /// that this ignores any potentially active macro expansions and _Pragma
/// expansions going on at the time. /// expansions going on at the time.
Lexer *Preprocessor::getCurrentFileLexer() const { Lexer *Preprocessor::getCurrentFileLexer() const {
if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer.get();
// Look for a stacked lexer. // Look for a stacked lexer.
for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
@ -88,7 +88,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
if (CurLexer || CurTokenLexer) if (CurLexer || CurTokenLexer)
PushIncludeMacroStack(); PushIncludeMacroStack();
CurLexer = TheLexer; CurLexer.reset(TheLexer);
CurDirLookup = CurDir; CurDirLookup = CurDir;
// Notify the client, if desired, that we are in a new source file. // Notify the client, if desired, that we are in a new source file.
@ -110,9 +110,9 @@ void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
CurDirLookup = 0; CurDirLookup = 0;
if (NumCachedTokenLexers == 0) { if (NumCachedTokenLexers == 0) {
CurTokenLexer = new TokenLexer(Tok, Args, *this); CurTokenLexer.reset(new TokenLexer(Tok, Args, *this));
} else { } else {
CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
CurTokenLexer->Init(Tok, Args); CurTokenLexer->Init(Tok, Args);
} }
} }
@ -138,10 +138,10 @@ void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
// Create a macro expander to expand from the specified token stream. // Create a macro expander to expand from the specified token stream.
if (NumCachedTokenLexers == 0) { if (NumCachedTokenLexers == 0) {
CurTokenLexer = new TokenLexer(Toks, NumToks, DisableMacroExpansion, CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
OwnsTokens, *this); OwnsTokens, *this));
} else { } else {
CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
} }
} }
@ -204,8 +204,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
// We're done with the #included file. // We're done with the #included file.
delete CurLexer; CurLexer.reset();
CurLexer = 0;
// This is the end of the top-level file. If the diag::pp_macro_not_used // This is the end of the top-level file. If the diag::pp_macro_not_used
// diagnostic is enabled, look for macros that have not been used. // diagnostic is enabled, look for macros that have not been used.
@ -227,12 +226,11 @@ bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
// Delete or cache the now-dead macro expander. // Delete or cache the now-dead macro expander.
if (NumCachedTokenLexers == TokenLexerCacheSize) if (NumCachedTokenLexers == TokenLexerCacheSize)
delete CurTokenLexer; CurTokenLexer.reset();
else else
TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
// Handle this like a #include file being popped off the stack. // Handle this like a #include file being popped off the stack.
CurTokenLexer = 0;
return HandleEndOfFile(Result, true); return HandleEndOfFile(Result, true);
} }
@ -245,11 +243,11 @@ void Preprocessor::RemoveTopOfLexerStack() {
if (CurTokenLexer) { if (CurTokenLexer) {
// Delete or cache the now-dead macro expander. // Delete or cache the now-dead macro expander.
if (NumCachedTokenLexers == TokenLexerCacheSize) if (NumCachedTokenLexers == TokenLexerCacheSize)
delete CurTokenLexer; CurTokenLexer.reset();
else else
TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
} else { } else {
delete CurLexer; CurLexer.reset();
} }
PopIncludeMacroStack(); PopIncludeMacroStack();

View File

@ -87,9 +87,6 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
Preprocessor::~Preprocessor() { Preprocessor::~Preprocessor() {
assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
// Free any active lexers.
delete CurLexer;
while (!IncludeMacroStack.empty()) { while (!IncludeMacroStack.empty()) {
delete IncludeMacroStack.back().TheLexer; delete IncludeMacroStack.back().TheLexer;
delete IncludeMacroStack.back().TheTokenLexer; delete IncludeMacroStack.back().TheTokenLexer;