r200230 - Comment parsing: don't crash while parsing \deprecated in a standalone comment

Dmitri Gribenko gribozavr at gmail.com
Mon Jan 27 09:55:44 PST 2014


Author: gribozavr
Date: Mon Jan 27 11:55:43 2014
New Revision: 200230

URL: http://llvm.org/viewvc/llvm-project?rev=200230&view=rev
Log:
Comment parsing: don't crash while parsing \deprecated in a standalone comment
(comment without a decl).

I think this can not happen during normal compilation with -Wdocumentation,
only while using Clang APIs to parse comments outside of a source file.

Based on a patch by Olivier Goffart.

Modified:
    cfe/trunk/lib/AST/CommentSema.cpp
    cfe/trunk/unittests/AST/CommentParser.cpp

Modified: cfe/trunk/lib/AST/CommentSema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/CommentSema.cpp?rev=200230&r1=200229&r2=200230&view=diff
==============================================================================
--- cfe/trunk/lib/AST/CommentSema.cpp (original)
+++ cfe/trunk/lib/AST/CommentSema.cpp Mon Jan 27 11:55:43 2014
@@ -68,8 +68,12 @@ void Sema::actOnBlockCommandFinish(Block
   Command->setParagraph(Paragraph);
   checkBlockCommandEmptyParagraph(Command);
   checkBlockCommandDuplicate(Command);
-  checkReturnsCommand(Command);
-  checkDeprecatedCommand(Command);
+  if (ThisDeclInfo) {
+    // These checks only make sense if the comment is attached to a
+    // declaration.
+    checkReturnsCommand(Command);
+    checkDeprecatedCommand(Command);
+  }
 }
 
 ParamCommandComment *Sema::actOnParamCommandStart(
@@ -558,6 +562,9 @@ void Sema::checkBlockCommandEmptyParagra
 void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
   if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
     return;
+
+  assert(ThisDeclInfo && "should not call this check on a bare comment");
+
   if (isFunctionDecl()) {
     if (ThisDeclInfo->ReturnType->isVoidType()) {
       unsigned DiagKind;
@@ -636,6 +643,8 @@ void Sema::checkDeprecatedCommand(const
   if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
     return;
 
+  assert(ThisDeclInfo && "should not call this check on a bare comment");
+
   const Decl *D = ThisDeclInfo->CommentDecl;
   if (!D)
     return;

Modified: cfe/trunk/unittests/AST/CommentParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/CommentParser.cpp?rev=200230&r1=200229&r2=200230&view=diff
==============================================================================
--- cfe/trunk/unittests/AST/CommentParser.cpp (original)
+++ cfe/trunk/unittests/AST/CommentParser.cpp Mon Jan 27 11:55:43 2014
@@ -1420,6 +1420,26 @@ TEST_F(CommentParserTest, VerbatimLine2)
   }
 }
 
+TEST_F(CommentParserTest, Deprecated) {
+  const char *Sources[] = {
+    "/** @deprecated*/",
+    "/// @deprecated\n"
+  };
+
+  for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
+    FullComment *FC = parseString(Sources[i]);
+    ASSERT_TRUE(HasChildCount(FC, 2));
+
+    ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
+    {
+      BlockCommandComment *BCC;
+      ParagraphComment *PC;
+      ASSERT_TRUE(HasBlockCommandAt(FC, Traits, 1, BCC, "deprecated", PC));
+      ASSERT_TRUE(HasChildCount(PC, 0));
+    }
+  }
+}
+
 } // unnamed namespace
 
 } // end namespace comments





More information about the cfe-commits mailing list