From 4d6382430066465774f6f696ea3f4c402da1d705 Mon Sep 17 00:00:00 2001 From: Aaron Puchert Date: Tue, 9 Nov 2021 21:51:38 +0100 Subject: [PATCH] Comment AST: Declare function pointer variables as functions We were doing this already for type aliases, and it deduplicates the code looking through aliases and pointers to find a function type. As a side effect, this finds two warnings that we apparently missed before. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D111264 --- clang/include/clang/AST/Comment.h | 1 + clang/include/clang/AST/CommentSema.h | 4 ---- clang/lib/AST/Comment.cpp | 3 +-- clang/lib/AST/CommentSema.cpp | 34 ++-------------------------------- clang/test/Sema/warn-documentation.cpp | 1 + clang/test/Sema/warn-documentation.m | 1 + 6 files changed, 6 insertions(+), 38 deletions(-) diff --git a/clang/include/clang/AST/Comment.h b/clang/include/clang/AST/Comment.h index 54a4b0a..e394e96 100644 --- a/clang/include/clang/AST/Comment.h +++ b/clang/include/clang/AST/Comment.h @@ -1019,6 +1019,7 @@ struct DeclInfo { /// \li member function template, /// \li member function template specialization, /// \li ObjC method, + /// \li variable of function pointer, member function pointer or block type, /// \li a typedef for a function pointer, member function pointer, /// ObjC block. FunctionKind, diff --git a/clang/include/clang/AST/CommentSema.h b/clang/include/clang/AST/CommentSema.h index 6dfe0f4..4a5174e 100644 --- a/clang/include/clang/AST/CommentSema.h +++ b/clang/include/clang/AST/CommentSema.h @@ -207,10 +207,6 @@ public: /// \returns \c true if declaration that this comment is attached to declares /// a function pointer. bool isFunctionPointerVarDecl(); - /// \returns \c true if the declaration that this comment is attached to - /// declares a variable or a field whose type is a function or a block - /// pointer. - bool isFunctionOrBlockPointerVarLikeDecl(); bool isFunctionOrMethodVariadic(); bool isObjCMethodDecl(); bool isObjCPropertyDecl(); diff --git a/clang/lib/AST/Comment.cpp b/clang/lib/AST/Comment.cpp index 94f6546..ce90abf5 100644 --- a/clang/lib/AST/Comment.cpp +++ b/clang/lib/AST/Comment.cpp @@ -333,8 +333,7 @@ void DeclInfo::fill() { TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc(); FunctionTypeLoc FTL; if (getFunctionTypeLoc(TL, FTL)) { - if (Kind == TypedefKind) - Kind = FunctionKind; + Kind = FunctionKind; ParamVars = FTL.getParams(); ReturnType = FTL.getReturnLoc().getType(); } diff --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp index e385c58..3977e6c 100644 --- a/clang/lib/AST/CommentSema.cpp +++ b/clang/lib/AST/CommentSema.cpp @@ -86,7 +86,7 @@ ParamCommandComment *Sema::actOnParamCommandStart( new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, CommandMarker); - if (!isFunctionDecl() && !isFunctionOrBlockPointerVarLikeDecl()) + if (!isFunctionDecl()) Diag(Command->getLocation(), diag::warn_doc_param_not_attached_to_a_function_decl) << CommandMarker @@ -588,7 +588,7 @@ void Sema::checkReturnsCommand(const BlockCommandComment *Command) { // to document the value that the property getter returns. if (isObjCPropertyDecl()) return; - if (isFunctionDecl() || isFunctionOrBlockPointerVarLikeDecl()) { + if (isFunctionDecl()) { assert(!ThisDeclInfo->ReturnType.isNull() && "should have a valid return type"); if (ThisDeclInfo->ReturnType->isVoidType()) { @@ -871,36 +871,6 @@ bool Sema::isFunctionPointerVarDecl() { return false; } -bool Sema::isFunctionOrBlockPointerVarLikeDecl() { - if (!ThisDeclInfo) - return false; - if (!ThisDeclInfo->IsFilled) - inspectThisDecl(); - if (ThisDeclInfo->getKind() != DeclInfo::VariableKind || - !ThisDeclInfo->CurrentDecl) - return false; - QualType QT; - if (const auto *VD = dyn_cast(ThisDeclInfo->CurrentDecl)) - QT = VD->getType(); - else if (const auto *PD = - dyn_cast(ThisDeclInfo->CurrentDecl)) - QT = PD->getType(); - else - return false; - // We would like to warn about the 'returns'/'param' commands for - // variables that don't directly specify the function type, so type aliases - // can be ignored. - if (QT->getAs()) - return false; - if (const auto *P = QT->getAs()) - if (P->getPointeeType()->getAs()) - return false; - if (const auto *P = QT->getAs()) - if (P->getPointeeType()->getAs()) - return false; - return QT->isFunctionPointerType() || QT->isBlockPointerType(); -} - bool Sema::isObjCPropertyDecl() { if (!ThisDeclInfo) return false; diff --git a/clang/test/Sema/warn-documentation.cpp b/clang/test/Sema/warn-documentation.cpp index 2b2a3d1..f30f05f 100644 --- a/clang/test/Sema/warn-documentation.cpp +++ b/clang/test/Sema/warn-documentation.cpp @@ -1333,6 +1333,7 @@ struct HasFields { int (*functionPointerField)(int i); }; +// expected-warning@+5 {{parameter 'p' not found in the function declaration}} // expected-warning@+5 {{'\returns' command used in a comment that is attached to a function returning void}} /** * functionPointerVariable diff --git a/clang/test/Sema/warn-documentation.m b/clang/test/Sema/warn-documentation.m index 5d60a52..6f6411e 100644 --- a/clang/test/Sema/warn-documentation.m +++ b/clang/test/Sema/warn-documentation.m @@ -248,6 +248,7 @@ struct HasFields { int (^blockPointerFields)(int i); }; +// expected-warning@+5 {{parameter 'p' not found in the function declaration}} // expected-warning@+5 {{'\returns' command used in a comment that is attached to a function returning void}} /** * functionPointerVariable -- 2.7.4