summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2013-01-07 08:54:53 +0000
committerManuel Klimek <klimek@google.com>2013-01-07 08:54:53 +0000
commitc74d292229d6b9b35c582d8b3bbe81a9dc86c80a (patch)
tree2fcfa589d43b9dec93a1df46d93c1bea2359fcdc
parent096f544edcda393fabc345553a358c0970c58a0b (diff)
downloadbcm5719-llvm-c74d292229d6b9b35c582d8b3bbe81a9dc86c80a.tar.gz
bcm5719-llvm-c74d292229d6b9b35c582d8b3bbe81a9dc86c80a.zip
Fix layouting of single-line-comments preceded by an escaped newline.
Previously, we'd format int i;\ // comment as int i; // comment The problem is that the escaped newline is part of the next token, and thus the raw token text of the comment doesn't start with "//". llvm-svn: 171713
-rw-r--r--clang/lib/Format/Format.cpp14
-rw-r--r--clang/unittests/Format/FormatTest.cpp3
2 files changed, 10 insertions, 7 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index a6beff0e4f7..35c1e403c6d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -520,8 +520,8 @@ private:
class TokenAnnotator {
public:
TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
- SourceManager &SourceMgr)
- : Line(Line), Style(Style), SourceMgr(SourceMgr) {
+ SourceManager &SourceMgr, Lexer &Lex)
+ : Line(Line), Style(Style), SourceMgr(SourceMgr), Lex(Lex) {
}
/// \brief A parser that gathers additional information about tokens.
@@ -865,10 +865,9 @@ private:
} else if (isBinaryOperator(Line.Tokens[i])) {
Annotation.Type = TokenAnnotation::TT_BinaryOperator;
} else if (Tok.Tok.is(tok::comment)) {
- // FIXME: Use Lexer::getSpelling(Tok, SourceMgr, LangOpts, bool*);
- StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
- Tok.Tok.getLength());
- if (Data.startswith("//"))
+ std::string Data(
+ Lexer::getSpelling(Tok.Tok, SourceMgr, Lex.getLangOpts()));
+ if (StringRef(Data).startswith("//"))
Annotation.Type = TokenAnnotation::TT_LineComment;
else
Annotation.Type = TokenAnnotation::TT_BlockComment;
@@ -1012,6 +1011,7 @@ private:
const UnwrappedLine &Line;
FormatStyle Style;
SourceManager &SourceMgr;
+ Lexer &Lex;
std::vector<TokenAnnotation> Annotations;
};
@@ -1142,7 +1142,7 @@ private:
LineRange.getBegin()))
continue;
- TokenAnnotator Annotator(TheLine, Style, SourceMgr);
+ TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
if (!Annotator.annotate())
break;
UnwrappedLineFormatter Formatter(
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3e672ae5a3a..27ca53c1eab 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -290,6 +290,9 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
+
+ EXPECT_EQ("int i; // single line trailing comment",
+ format("int i;\\\n// single line trailing comment"));
}
TEST_F(FormatTest, UnderstandsMultiLineComments) {
OpenPOWER on IntegriCloud