From: Dmitri Gribenko Date: Fri, 23 Aug 2013 18:03:40 +0000 (+0000) Subject: Comment parsing: fix a bug where a line with whitespace between two paragraphs X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2b429358a9849e2e48520a03065fa385a02dc115;p=clang Comment parsing: fix a bug where a line with whitespace between two paragraphs would cause us to concatenate these paragraphs into a single one. The no-op whitespace churn in test/Index test happened because these tests don't use the correct approach for testing and are more strict than required for they are testing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189126 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/CommentParser.cpp b/lib/AST/CommentParser.cpp index d89c79b3d9..03e01015b9 100644 --- a/lib/AST/CommentParser.cpp +++ b/lib/AST/CommentParser.cpp @@ -16,6 +16,15 @@ #include "llvm/Support/ErrorHandling.h" namespace clang { + +static inline bool isWhitespace(llvm::StringRef S) { + for (StringRef::const_iterator I = S.begin(), E = S.end(); I != E; ++I) { + if (!isWhitespace(*I)) + return false; + } + return true; +} + namespace comments { /// Re-lexes a sequence of tok::text tokens. @@ -594,6 +603,18 @@ BlockContentComment *Parser::parseParagraphOrBlockCommand() { consumeToken(); break; // Two newlines -- end of paragraph. } + // Also allow [tok::newline, tok::text, tok::newline] if the middle + // tok::text is just whitespace. + if (Tok.is(tok::text) && isWhitespace(Tok.getText())) { + Token WhitespaceTok = Tok; + consumeToken(); + if (Tok.is(tok::newline) || Tok.is(tok::eof)) { + consumeToken(); + break; + } + // We have [tok::newline, tok::text, non-newline]. Put back tok::text. + putBack(WhitespaceTok); + } if (Content.size() > 0) Content.back()->addTrailingNewline(); continue; diff --git a/test/Index/comment-misc-tags.m b/test/Index/comment-misc-tags.m index a187527223..bb16ea97b6 100644 --- a/test/Index/comment-misc-tags.m +++ b/test/Index/comment-misc-tags.m @@ -45,7 +45,7 @@ // CHECK-NEXT: (CXComment_Text Text=[ ] IsWhitespace))) // CHECK: (CXComment_BlockCommand CommandName=[seealso] // CHECK-NEXT: (CXComment_Paragraph -// CHECK-NEXT: (CXComment_Text Text=[ //k_ref/doc/uid/XX30000905-CH204 Programming] HasTrailingNewline) +// CHECK-NEXT: (CXComment_Text Text=[ //k_ref/doc/uid/XX30000905-CH204 Programming]) // rdar://12379053 /*! diff --git a/test/Index/overriding-method-comments.mm b/test/Index/overriding-method-comments.mm index 46ead4f642..9285693396 100644 --- a/test/Index/overriding-method-comments.mm +++ b/test/Index/overriding-method-comments.mm @@ -19,7 +19,7 @@ - (void)METH:(id)AAA; @end -// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)AAA;AAA0in ZZZ ] +// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)AAA;AAA0in ZZZ] @interface Sub : Root @end @@ -28,13 +28,13 @@ - (void)METH:(id)BBB; @end -// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)BBB;BBB0in ZZZ ] +// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)BBB;BBB0in ZZZ] @implementation Sub(CAT) - (void)METH:(id)III {} @end -// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)III;III0in ZZZ ] +// CHECK: FullCommentAsXML=[METH:c:objc(cs)Root(im)METH:- (void)METH:(id)III;III0in ZZZ] @interface Redec : Root @end @@ -48,13 +48,13 @@ - (void)EXT_METH:(id)AAA : (double)BBB : (int)CCC; @end -// CHECK: FullCommentAsXML=[EXT_METH:::c:objc(cs)Redec(im)EXT_METH:::- (void)EXT_METH:(id)AAA:(double)BBB:(int)CCC;AAA0in input value BBB1in 2nd input value is double CCC2out output value is int ] +// CHECK: FullCommentAsXML=[EXT_METH:::c:objc(cs)Redec(im)EXT_METH:::- (void)EXT_METH:(id)AAA:(double)BBB:(int)CCC;AAA0in input value BBB1in 2nd input value is doubleCCC2out output value is int ] @implementation Redec - (void)EXT_METH:(id)PPP : (double)QQQ : (int)RRR {} @end -// CHECK: FullCommentAsXML=[EXT_METH:::c:objc(cs)Redec(im)EXT_METH:::- (void)EXT_METH:(id)PPP:(double)QQQ:(int)RRR;PPP0in input value QQQ1in 2nd input value is double RRR2out output value is int ] +// CHECK: FullCommentAsXML=[EXT_METH:::c:objc(cs)Redec(im)EXT_METH:::- (void)EXT_METH:(id)PPP:(double)QQQ:(int)RRR;PPP0in input value QQQ1in 2nd input value is doubleRRR2out output value is int ] struct Base { /// \brief Does something. diff --git a/unittests/AST/CommentParser.cpp b/unittests/AST/CommentParser.cpp index d05fb58faf..f75c636e01 100644 --- a/unittests/AST/CommentParser.cpp +++ b/unittests/AST/CommentParser.cpp @@ -628,18 +628,43 @@ TEST_F(CommentParserTest, Basic3) { } } -TEST_F(CommentParserTest, Paragraph1) { +TEST_F(CommentParserTest, ParagraphSplitting1) { const char *Sources[] = { "// Aaa\n" "//\n" "// Bbb", + "// Aaa\n" + "// \n" + "// Bbb", + + "// Aaa\n" + "//\t\n" + "// Bbb", + "// Aaa\n" "//\n" "//\n" "// Bbb", - }; + "/**\n" + " Aaa\n" + "\n" + " Bbb\n" + "*/", + + "/**\n" + " Aaa\n" + " \n" + " Bbb\n" + "*/", + + "/**\n" + " Aaa\n" + "\t \n" + " Bbb\n" + "*/", + }; for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) { FullComment *FC = parseString(Sources[i]); @@ -650,7 +675,7 @@ TEST_F(CommentParserTest, Paragraph1) { } } -TEST_F(CommentParserTest, Paragraph2) { +TEST_F(CommentParserTest, Paragraph1) { const char *Source = "// \\brief Aaa\n" "//\n" @@ -670,7 +695,7 @@ TEST_F(CommentParserTest, Paragraph2) { ASSERT_TRUE(HasParagraphCommentAt(FC, 2, " Bbb")); } -TEST_F(CommentParserTest, Paragraph3) { +TEST_F(CommentParserTest, Paragraph2) { const char *Source = "// \\brief \\author"; FullComment *FC = parseString(Source); @@ -694,7 +719,7 @@ TEST_F(CommentParserTest, Paragraph3) { } } -TEST_F(CommentParserTest, Paragraph4) { +TEST_F(CommentParserTest, Paragraph3) { const char *Source = "// \\brief Aaa\n" "// Bbb \\author\n"