From: Manuel Klimek Date: Mon, 3 Dec 2012 20:55:42 +0000 (+0000) Subject: Fixes a compile warning and crash in the tests. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0be4b36830a26026726d05aae5434c6658abc65f;p=clang Fixes a compile warning and crash in the tests. The necessity of this fix points to a problem with the design of the addToken during the optimiation phase, which we need to address in a much more principled way. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169151 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a967768eec..4104bd3e6c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -167,13 +167,14 @@ private: State.Column = State.Column - Previous.Tok.getLength(); else if (Previous.Tok.is(tok::equal) && ParenLevel != 0) State.Column = State.Indent[ParenLevel] + 4; - else + else if (ParenLevel < State.Indent.size()) State.Column = State.Indent[ParenLevel]; if (!DryRun) replaceWhitespace(Current, 1, State.Column); State.Column += Current.Tok.getLength(); - State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; + if (ParenLevel < State.LastSpace.size()) + State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; if (Current.Tok.is(tok::colon) && Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) { State.Indent[ParenLevel] += 2; @@ -189,20 +190,23 @@ private: if (Previous.Tok.is(tok::l_paren)) State.Indent[ParenLevel] = State.Column; if (Previous.Tok.is(tok::less) && - Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener) + Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener && + ParenLevel < State.Indent.size()) State.Indent[ParenLevel] = State.Column; if (Current.Tok.is(tok::colon)) { State.Indent[ParenLevel] = State.Column + 3; State.InCtorInitializer = true; } // Top-level spaces are exempt as that mostly leads to better results. - if (Spaces > 0 && ParenLevel != 0) + if (Spaces > 0 && ParenLevel != 0 && + ParenLevel < State.LastSpace.size()) State.LastSpace[ParenLevel] = State.Column + Spaces; State.Column += Current.Tok.getLength() + Spaces; } - if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || - Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) { + if (!DryRun && + (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || + Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener)) { State.Indent.pop_back(); State.LastSpace.pop_back(); } @@ -341,11 +345,9 @@ public: /// into template parameter lists. class AnnotatingParser { public: - AnnotatingParser(const SourceManager &SourceMgr, - const SmallVector &Tokens, + AnnotatingParser(const SmallVector &Tokens, std::vector &Annotations) - : SourceMgr(SourceMgr), - Tokens(Tokens), + : Tokens(Tokens), Annotations(Annotations), Index(0) { } @@ -457,7 +459,6 @@ public: } private: - const SourceManager &SourceMgr; const SmallVector &Tokens; std::vector &Annotations; unsigned Index; @@ -469,7 +470,7 @@ public: Annotations.push_back(TokenAnnotation()); } - AnnotatingParser Parser(SourceMgr, Line.Tokens, Annotations); + AnnotatingParser Parser(Line.Tokens, Annotations); Parser.parseLine(); determineTokenTypes();