doc parsing. We want to issue a strong warning when
an @function comment is not followed by a function decl.
// rdar://13094352


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@176468 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/CommentCommandTraits.h b/include/clang/AST/CommentCommandTraits.h
index ad4f299..360a54b 100644
--- a/include/clang/AST/CommentCommandTraits.h
+++ b/include/clang/AST/CommentCommandTraits.h
@@ -100,7 +100,10 @@
   ///   \fn void f(int a);
   /// \endcode
   unsigned IsDeclarationCommand : 1;
-
+  
+  /// \brief True if verbatim-like line command is a function declaraton.
+  unsigned IsFunctionDeclarationCommand : 1;
+  
   /// \brief True if this command is unknown.  This \c CommandInfo object was
   /// created during parsing.
   unsigned IsUnknownCommand : 1;
diff --git a/include/clang/AST/CommentCommands.td b/include/clang/AST/CommentCommands.td
index f04509c..50abc59 100644
--- a/include/clang/AST/CommentCommands.td
+++ b/include/clang/AST/CommentCommands.td
@@ -24,6 +24,7 @@
   bit IsVerbatimBlockEndCommand = 0;
   bit IsVerbatimLineCommand = 0;
   bit IsDeclarationCommand = 0;
+  bit IsFunctionDeclarationCommand = 0;
 }
 
 class InlineCommand<string name> : Command<name> {
@@ -59,6 +60,12 @@
   let IsDeclarationCommand = 1;
 }
 
+class FunctionDeclarationVerbatimLineCommand<string name> :
+      VerbatimLineCommand<name> {
+  let IsDeclarationCommand = 1;
+  let IsFunctionDeclarationCommand = 1;
+}
+
 //===----------------------------------------------------------------------===//
 // InlineCommand
 //===----------------------------------------------------------------------===//
@@ -179,7 +186,7 @@
 def Protocol  : DeclarationVerbatimLineCommand<"protocol">;
 def Category  : DeclarationVerbatimLineCommand<"category">;
 def Template  : DeclarationVerbatimLineCommand<"template">;
-def Function  : DeclarationVerbatimLineCommand<"function">;
+def Function  : FunctionDeclarationVerbatimLineCommand<"function">;
 def Method    : DeclarationVerbatimLineCommand<"method">;
 def Callback  : DeclarationVerbatimLineCommand<"callback">;
 def Const     : DeclarationVerbatimLineCommand<"const">;
diff --git a/include/clang/AST/CommentSema.h b/include/clang/AST/CommentSema.h
index 6613feb..6df48dc 100644
--- a/include/clang/AST/CommentSema.h
+++ b/include/clang/AST/CommentSema.h
@@ -198,6 +198,8 @@
   void checkBlockCommandDuplicate(const BlockCommandComment *Command);
 
   void checkDeprecatedCommand(const BlockCommandComment *Comment);
+  
+  void checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment);
 
   /// Resolve parameter names to parameter indexes in function declaration.
   /// Emit diagnostics about unknown parametrs.
diff --git a/include/clang/Basic/DiagnosticCommentKinds.td b/include/clang/Basic/DiagnosticCommentKinds.td
index 829f98d..5f28625 100644
--- a/include/clang/Basic/DiagnosticCommentKinds.td
+++ b/include/clang/Basic/DiagnosticCommentKinds.td
@@ -73,6 +73,11 @@
   "a function declaration">,
   InGroup<Documentation>, DefaultIgnore;
 
+def warn_doc_function_not_attached_to_a_function_decl : Warning<
+  "'@function' command used in a comment that is attached to "
+  "a non-function declaration immediately following it">,
+  InGroup<Documentation>, DefaultIgnore;
+  
 def warn_doc_param_duplicate : Warning<
   "parameter '%0' is already documented">,
   InGroup<Documentation>, DefaultIgnore;
diff --git a/lib/AST/CommentParser.cpp b/lib/AST/CommentParser.cpp
index 09912c6..c361679 100644
--- a/lib/AST/CommentParser.cpp
+++ b/lib/AST/CommentParser.cpp
@@ -706,6 +706,8 @@
                                                 TextBegin,
                                                 Text);
   consumeToken();
+  S.checkFunctionDeclVerbatimLine(VL);
+  
   return VL;
 }
 
diff --git a/lib/AST/CommentSema.cpp b/lib/AST/CommentSema.cpp
index d959d19..0cf7b5f 100644
--- a/lib/AST/CommentSema.cpp
+++ b/lib/AST/CommentSema.cpp
@@ -88,6 +88,15 @@
   return Command;
 }
 
+void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
+  const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
+  if (Info->IsFunctionDeclarationCommand &&
+      !isFunctionDecl())
+    Diag(Comment->getLocation(),
+         diag::warn_doc_function_not_attached_to_a_function_decl)
+    << Comment->getSourceRange();
+}
+
 void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
                                          SourceLocation ArgLocBegin,
                                          SourceLocation ArgLocEnd,
diff --git a/test/Sema/warn-documentation.cpp b/test/Sema/warn-documentation.cpp
index 431bec1..8b38ddd 100644
--- a/test/Sema/warn-documentation.cpp
+++ b/test/Sema/warn-documentation.cpp
@@ -911,3 +911,12 @@
 ///@param x@param y
 int test_nocrash13(int x, int y);
 
+// expected-warning@+3 {{'@function' command used in a comment that is attached to a non-function declaration immediately following it}}
+// expected-warning@+3 {{'@param' command used in a comment that is not attached to a function declaration}}
+// expected-warning@+3 {{'@result' command used in a comment that is not attached to a function or method declaration}}
+/*!	@function Base64EncodeEx
+	@param	inFlags  This is error flag
+	@result	Error
+*/
+typedef unsigned int Base64Flags;
+unsigned Base64EncodeEx(Base64Flags	inFlags);
diff --git a/utils/TableGen/ClangCommentCommandInfoEmitter.cpp b/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
index 4dafc2e..b0bf752 100644
--- a/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
+++ b/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
@@ -47,6 +47,7 @@
        << Tag.getValueAsBit("IsVerbatimBlockEndCommand") << ", "
        << Tag.getValueAsBit("IsVerbatimLineCommand") << ", "
        << Tag.getValueAsBit("IsDeclarationCommand") << ", "
+       << Tag.getValueAsBit("IsFunctionDeclarationCommand") << ", "
        << /* IsUnknownCommand = */ "0"
        << " }";
     if (i + 1 != e)