]> granicus.if.org Git - clang/commitdiff
Comment parsing: fix a bug where a line with whitespace between two paragraphs
authorDmitri Gribenko <gribozavr@gmail.com>
Fri, 23 Aug 2013 18:03:40 +0000 (18:03 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Fri, 23 Aug 2013 18:03:40 +0000 (18:03 +0000)
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

lib/AST/CommentParser.cpp
test/Index/comment-misc-tags.m
test/Index/overriding-method-comments.mm
unittests/AST/CommentParser.cpp

index d89c79b3d93d16c488b4e63fc18cc08b861e2795..03e01015b95440ffb096f2c42bb5900b4c84ceba 100644 (file)
 #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;
index a187527223669aa91e55dff7f7b436bad38e72d9..bb16ea97b6d6221fbfce48096c66791ca54a33af 100644 (file)
@@ -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
 /*!
index 46ead4f6425686459bd2bde9d0221653a501c387..9285693396e08d3c0ece610d3ef3f9ae8ad031bd 100644 (file)
@@ -19,7 +19,7 @@
 - (void)METH:(id)AAA;
 @end
 
-// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)AAA;</Declaration><Parameters><Parameter><Name>AAA</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ </Para></Discussion></Parameter></Parameters></Function>]
+// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)AAA;</Declaration><Parameters><Parameter><Name>AAA</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ</Para></Discussion></Parameter></Parameters></Function>]
 
 @interface Sub : Root
 @end
 - (void)METH:(id)BBB;
 @end
 
-// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)BBB;</Declaration><Parameters><Parameter><Name>BBB</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ </Para></Discussion></Parameter></Parameters></Function>]
+// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)BBB;</Declaration><Parameters><Parameter><Name>BBB</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ</Para></Discussion></Parameter></Parameters></Function>]
 
 @implementation Sub(CAT)
 - (void)METH:(id)III {}
 @end
 
-// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)III;</Declaration><Parameters><Parameter><Name>III</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ </Para></Discussion></Parameter></Parameters></Function>]
+// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>METH:</Name><USR>c:objc(cs)Root(im)METH:</USR><Declaration>- (void)METH:(id)III;</Declaration><Parameters><Parameter><Name>III</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> ZZZ</Para></Discussion></Parameter></Parameters></Function>]
 
 @interface Redec : Root
 @end
 - (void)EXT_METH:(id)AAA : (double)BBB : (int)CCC;
 @end
 
-// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>EXT_METH:::</Name><USR>c:objc(cs)Redec(im)EXT_METH:::</USR><Declaration>- (void)EXT_METH:(id)AAA:(double)BBB:(int)CCC;</Declaration><Parameters><Parameter><Name>AAA</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> input value </Para></Discussion></Parameter><Parameter><Name>BBB</Name><Index>1</Index><Direction isExplicit="1">in</Direction><Discussion><Para> 2nd input value is double </Para></Discussion></Parameter><Parameter><Name>CCC</Name><Index>2</Index><Direction isExplicit="1">out</Direction><Discussion><Para> output value is int </Para></Discussion></Parameter></Parameters></Function>]
+// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>EXT_METH:::</Name><USR>c:objc(cs)Redec(im)EXT_METH:::</USR><Declaration>- (void)EXT_METH:(id)AAA:(double)BBB:(int)CCC;</Declaration><Parameters><Parameter><Name>AAA</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> input value </Para></Discussion></Parameter><Parameter><Name>BBB</Name><Index>1</Index><Direction isExplicit="1">in</Direction><Discussion><Para> 2nd input value is double</Para></Discussion></Parameter><Parameter><Name>CCC</Name><Index>2</Index><Direction isExplicit="1">out</Direction><Discussion><Para> output value is int </Para></Discussion></Parameter></Parameters></Function>]
 
 @implementation Redec
 - (void)EXT_METH:(id)PPP : (double)QQQ : (int)RRR {}
 @end
 
-// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>EXT_METH:::</Name><USR>c:objc(cs)Redec(im)EXT_METH:::</USR><Declaration>- (void)EXT_METH:(id)PPP:(double)QQQ:(int)RRR;</Declaration><Parameters><Parameter><Name>PPP</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> input value </Para></Discussion></Parameter><Parameter><Name>QQQ</Name><Index>1</Index><Direction isExplicit="1">in</Direction><Discussion><Para> 2nd input value is double </Para></Discussion></Parameter><Parameter><Name>RRR</Name><Index>2</Index><Direction isExplicit="1">out</Direction><Discussion><Para> output value is int </Para></Discussion></Parameter></Parameters></Function>]
+// CHECK: FullCommentAsXML=[<Function isInstanceMethod="1" file="{{[^"]+}}overriding-method-comments.mm" line="[[@LINE-3]]" column="1"><Name>EXT_METH:::</Name><USR>c:objc(cs)Redec(im)EXT_METH:::</USR><Declaration>- (void)EXT_METH:(id)PPP:(double)QQQ:(int)RRR;</Declaration><Parameters><Parameter><Name>PPP</Name><Index>0</Index><Direction isExplicit="1">in</Direction><Discussion><Para> input value </Para></Discussion></Parameter><Parameter><Name>QQQ</Name><Index>1</Index><Direction isExplicit="1">in</Direction><Discussion><Para> 2nd input value is double</Para></Discussion></Parameter><Parameter><Name>RRR</Name><Index>2</Index><Direction isExplicit="1">out</Direction><Discussion><Para> output value is int </Para></Discussion></Parameter></Parameters></Function>]
 
 struct Base {
   /// \brief Does something.
index d05fb58fafaa7d450e2f82495e5178145a9a46f7..f75c636e01dd6ee2320369f9fedac10d7dbce3b5 100644 (file)
@@ -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"