Skip to content

Commit 2eabcc9

Browse files
committed
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) llvm-svn: 260246
1 parent cb1175c commit 2eabcc9

File tree

13 files changed

+99
-120
lines changed

13 files changed

+99
-120
lines changed

clang/include/clang/Lex/Preprocessor.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,20 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
10181018
/// If \p OwnsTokens is false, this method assumes that the specified stream
10191019
/// of tokens has a permanent owner somewhere, so they do not need to be
10201020
/// copied. If it is true, it assumes the array of tokens is allocated with
1021-
/// \c new[] and must be freed.
1021+
/// \c new[] and the Preprocessor will delete[] it.
1022+
private:
10221023
void EnterTokenStream(const Token *Toks, unsigned NumToks,
10231024
bool DisableMacroExpansion, bool OwnsTokens);
10241025

1026+
public:
1027+
void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1028+
bool DisableMacroExpansion) {
1029+
EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true);
1030+
}
1031+
void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion) {
1032+
EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false);
1033+
}
1034+
10251035
/// \brief Pop the current lexer/macro exp off the top of the lexer stack.
10261036
///
10271037
/// This should only be used in situations where the current state of the

clang/lib/Frontend/PrintPreprocessedOutput.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,10 @@ struct UnknownPragmaHandler : public PragmaHandler {
580580
if (ShouldExpandTokens) {
581581
// The first token does not have expanded macros. Expand them, if
582582
// required.
583-
Token *Toks = new Token[1];
583+
auto Toks = llvm::make_unique<Token[]>(1);
584584
Toks[0] = PragmaTok;
585-
PP.EnterTokenStream(Toks, /*NumToks=*/1,
586-
/*DisableMacroExpansion=*/false,
587-
/*OwnsTokens=*/true);
585+
PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1,
586+
/*DisableMacroExpansion=*/false);
588587
PP.Lex(PragmaTok);
589588
}
590589
Token PrevToken;

clang/lib/Lex/PPDirectives.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ void Preprocessor::HandleDirective(Token &Result) {
907907
// various pseudo-ops. Just return the # token and push back the following
908908
// token to be lexed next time.
909909
if (getLangOpts().AsmPreprocessor) {
910-
Token *Toks = new Token[2];
910+
auto Toks = llvm::make_unique<Token[]>(2);
911911
// Return the # and the token after it.
912912
Toks[0] = SavedHash;
913913
Toks[1] = Result;
@@ -920,7 +920,7 @@ void Preprocessor::HandleDirective(Token &Result) {
920920
// Enter this token stream so that we re-lex the tokens. Make sure to
921921
// enable macro expansion, in case the token after the # is an identifier
922922
// that is expanded.
923-
EnterTokenStream(Toks, 2, false, true);
923+
EnterTokenStream(std::move(Toks), 2, false);
924924
return;
925925
}
926926

@@ -1442,13 +1442,13 @@ static void EnterAnnotationToken(Preprocessor &PP,
14421442
tok::TokenKind Kind, void *AnnotationVal) {
14431443
// FIXME: Produce this as the current token directly, rather than
14441444
// allocating a new token for it.
1445-
Token *Tok = new Token[1];
1445+
auto Tok = llvm::make_unique<Token[]>(1);
14461446
Tok[0].startToken();
14471447
Tok[0].setKind(Kind);
14481448
Tok[0].setLocation(Begin);
14491449
Tok[0].setAnnotationEndLoc(End);
14501450
Tok[0].setAnnotationValue(AnnotationVal);
1451-
PP.EnterTokenStream(Tok, 1, true, true);
1451+
PP.EnterTokenStream(std::move(Tok), 1, true);
14521452
}
14531453

14541454
/// \brief Produce a diagnostic informing the user that a #include or similar

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,12 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
755755
// Do not lose the EOF/EOD. Return it to the client.
756756
MacroName = Tok;
757757
return nullptr;
758-
} else {
759-
// Do not lose the EOF/EOD.
760-
Token *Toks = new Token[1];
761-
Toks[0] = Tok;
762-
EnterTokenStream(Toks, 1, true, true);
763-
break;
764758
}
759+
// Do not lose the EOF/EOD.
760+
auto Toks = llvm::make_unique<Token[]>(1);
761+
Toks[0] = Tok;
762+
EnterTokenStream(std::move(Toks), 1, true);
763+
break;
765764
} else if (Tok.is(tok::r_paren)) {
766765
// If we found the ) token, the macro arg list is done.
767766
if (NumParens-- == 0) {

clang/lib/Lex/Pragma.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,13 @@ struct PragmaDebugHandler : public PragmaHandler {
938938
}
939939

940940
SourceLocation NameLoc = Tok.getLocation();
941-
Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
942-
Toks->startToken();
943-
Toks->setKind(tok::annot_pragma_captured);
944-
Toks->setLocation(NameLoc);
941+
MutableArrayRef<Token> Toks(
942+
PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
943+
Toks[0].startToken();
944+
Toks[0].setKind(tok::annot_pragma_captured);
945+
Toks[0].setLocation(NameLoc);
945946

946-
PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
947-
/*OwnsTokens=*/false);
947+
PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
948948
}
949949

950950
// Disable MSVC warning about runtime stack overflow.

clang/lib/Parse/ParseCXXInlineMethods.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
325325

326326
// Parse the default argument from its saved token stream.
327327
Toks->push_back(Tok); // So that the current token doesn't get lost
328-
PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
328+
PP.EnterTokenStream(*Toks, true);
329329

330330
// Consume the previously-pushed token.
331331
ConsumeAnyToken();
@@ -399,7 +399,7 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
399399

400400
// Parse the default argument from its saved token stream.
401401
Toks->push_back(Tok); // So that the current token doesn't get lost
402-
PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
402+
PP.EnterTokenStream(*Toks, true);
403403

404404
// Consume the previously-pushed token.
405405
ConsumeAnyToken();
@@ -504,7 +504,7 @@ void Parser::ParseLexedMethodDef(LexedMethod &LM) {
504504
// Append the current token at the end of the new token stream so that it
505505
// doesn't get lost.
506506
LM.Toks.push_back(Tok);
507-
PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
507+
PP.EnterTokenStream(LM.Toks, true);
508508

509509
// Consume the previously pushed token.
510510
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
@@ -617,7 +617,7 @@ void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
617617
// Append the current token at the end of the new token stream so that it
618618
// doesn't get lost.
619619
MI.Toks.push_back(Tok);
620-
PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
620+
PP.EnterTokenStream(MI.Toks, true);
621621

622622
// Consume the previously pushed token.
623623
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
@@ -971,10 +971,10 @@ class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction
971971
// Put back the original tokens.
972972
Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
973973
if (Toks.size()) {
974-
Token *Buffer = new Token[Toks.size()];
975-
std::copy(Toks.begin() + 1, Toks.end(), Buffer);
974+
auto Buffer = llvm::make_unique<Token[]>(Toks.size());
975+
std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
976976
Buffer[Toks.size() - 1] = Self.Tok;
977-
Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true);
977+
Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true);
978978

979979
Self.Tok = Toks.front();
980980
}

clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
11871187
// Append the current token at the end of the new token stream so that it
11881188
// doesn't get lost.
11891189
LA.Toks.push_back(Tok);
1190-
PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1190+
PP.EnterTokenStream(LA.Toks, true);
11911191
// Consume the previously pushed token.
11921192
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
11931193

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,8 +3093,7 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
30933093
Toks.push_back(Tok);
30943094
// Re-enter the stored parenthesized tokens into the token stream, so we may
30953095
// parse them now.
3096-
PP.EnterTokenStream(Toks.data(), Toks.size(),
3097-
true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
3096+
PP.EnterTokenStream(Toks, true /*DisableMacroExpansion*/);
30983097
// Drop the current token and bring the first cached one. It's the same token
30993098
// as when we entered this function.
31003099
ConsumeAnyToken();

clang/lib/Parse/ParseObjc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,8 +3585,8 @@ void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
35853585
// Append the current token at the end of the new token stream so that it
35863586
// doesn't get lost.
35873587
LM.Toks.push_back(Tok);
3588-
PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3589-
3588+
PP.EnterTokenStream(LM.Toks, true);
3589+
35903590
// Consume the previously pushed token.
35913591
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
35923592

0 commit comments

Comments
 (0)