From 29f8cd7906aa177cfe17e56e0dd6e12da6f02d51 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 29 Oct 2014 16:51:38 +0000 Subject: [PATCH] clang-format: [JS] Support more regex literals. Previously a regex-literal containing "/*" would through clang-format off, e.g.: var regex = /\/*$/; Would lead to none of the following code to be formatted. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220860 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 27 +++++++++++++++------------ unittests/Format/FormatTestJS.cpp | 4 ++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index e2aff03e88..bd0dc6eb73 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1420,14 +1420,14 @@ private: if (Tokens.size() < 2) return false; FormatToken *Previous = Tokens[Tokens.size() - 2]; - if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\" || - Tokens.back()->NewlinesBefore != 0) + if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\") return false; - Previous->ColumnWidth += Tokens.back()->ColumnWidth; + ++Previous->ColumnWidth; StringRef Text = Previous->TokenText; - Previous->TokenText = - StringRef(Text.data(), Text.size() + Tokens.back()->TokenText.size()); + Previous->TokenText = StringRef(Text.data(), Text.size() + 1); + resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1); Tokens.resize(Tokens.size() - 1); + Column = Previous->OriginalColumn + Previous->ColumnWidth; return true; } @@ -1460,15 +1460,10 @@ private: tok::question, tok::kw_return) || I[1]->isBinaryOperator())) { if (MightEndWithEscapedSlash) { - StringRef Buffer = SourceMgr.getBufferData(ID); // This regex literal ends in '\//'. Skip past the '//' of the last // token and re-start lexing from there. - int offset = - SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 2; - Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), - getFormattingLangOpts(Style), Buffer.begin(), - Buffer.begin() + offset, Buffer.end())); - Lex->SetKeepWhitespaceMode(true); + SourceLocation Loc = Tokens.back()->Tok.getLocation(); + resetLexer(SourceMgr.getFileOffset(Loc) + 2); } Tokens.resize(Tokens.size() - TokenCount); Tokens.back()->Tok.setKind(tok::unknown); @@ -1764,6 +1759,14 @@ private: FormattingDisabled = true; } } + + void resetLexer(unsigned Offset) { + StringRef Buffer = SourceMgr.getBufferData(ID); + Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), + getFormattingLangOpts(Style), Buffer.begin(), + Buffer.begin() + Offset, Buffer.end())); + Lex->SetKeepWhitespaceMode(true); + } }; static StringRef getLanguageName(FormatStyle::LanguageKind Language) { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index bc9bbad601..6ee150c662 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -408,6 +408,10 @@ TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { verifyFormat("var regex = /\a\\//g;"); verifyFormat("var regex = /a\\//;\n" "var x = 0;"); + EXPECT_EQ("var regex = /\\/*/;\n" + "var x = 0;", + format("var regex = /\\/*/;\n" + "var x=0;")); } TEST_F(FormatTestJS, RegexLiteralModifiers) { -- 2.40.0