summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/Format.cpp27
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp4
2 files changed, 19 insertions, 12 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index e2aff03e88e..bd0dc6eb735 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/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/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index bc9bbad6017..6ee150c6624 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/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) {
OpenPOWER on IntegriCloud