From 258d9a528eb292970f25bf7036c11b7bd6380d87 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Tue, 7 Feb 2017 11:39:56 +0000 Subject: [clang-tidy] misc-argument-comment - extended gmock support It looks like direct calls to mocked methods happen in the wild. This patch add support for these as well. llvm-svn: 294293 --- .../clang-tidy/misc/ArgumentCommentCheck.cpp | 54 +++++++++++++--------- 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'clang-tools-extra/clang-tidy/misc/ArgumentCommentCheck.cpp') diff --git a/clang-tools-extra/clang-tidy/misc/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/misc/ArgumentCommentCheck.cpp index cad112bfb67..d7ac9f3a9b3 100644 --- a/clang-tools-extra/clang-tidy/misc/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ArgumentCommentCheck.cpp @@ -147,33 +147,43 @@ static bool sameName(StringRef InComment, StringRef InDecl, bool StrictMode) { return InComment.compare_lower(InDecl) == 0; } +static bool looksLikeExpectMethod(const CXXMethodDecl *Expect) { + return Expect != nullptr && Expect->getLocation().isMacroID() && + Expect->getNameInfo().getName().isIdentifier() && + Expect->getName().startswith("gmock_"); +} +static bool areMockAndExpectMethods(const CXXMethodDecl *Mock, + const CXXMethodDecl *Expect) { + assert(looksLikeExpectMethod(Expect)); + return Mock != nullptr && Mock->getNextDeclInContext() == Expect && + Mock->getNumParams() == Expect->getNumParams() && + Mock->getLocation().isMacroID() && + Mock->getNameInfo().getName().isIdentifier() && + Mock->getName() == Expect->getName().substr(strlen("gmock_")); +} + // This uses implementation details of MOCK_METHODx_ macros: for each mocked // method M it defines M() with appropriate signature and a method used to set // up expectations - gmock_M() - with each argument's type changed the -// corresponding matcher. This function finds M by gmock_M. -static const CXXMethodDecl * -findMockedMethod(const CXXMethodDecl *ExpectMethod) { - if (!ExpectMethod->getNameInfo().getName().isIdentifier()) - return nullptr; - StringRef Name = ExpectMethod->getName(); - if (!Name.startswith("gmock_")) - return nullptr; - Name = Name.substr(strlen("gmock_")); - - const DeclContext *Ctx = ExpectMethod->getDeclContext(); - if (Ctx == nullptr || !Ctx->isRecord()) - return nullptr; - for (const auto *D : Ctx->decls()) { - if (const auto *Method = dyn_cast(D)) { - if (Method->getName() != Name) - continue; - // Sanity check the mocked method. - if (Method->getNextDeclInContext() == ExpectMethod && - Method->getLocation().isMacroID() && - Method->getNumParams() == ExpectMethod->getNumParams()) { - return Method; +// corresponding matcher. This function returns M when given either M or +// gmock_M. +static const CXXMethodDecl *findMockedMethod(const CXXMethodDecl *Method) { + if (looksLikeExpectMethod(Method)) { + const DeclContext *Ctx = Method->getDeclContext(); + if (Ctx == nullptr || !Ctx->isRecord()) + return nullptr; + for (const auto *D : Ctx->decls()) { + if (D->getNextDeclInContext() == Method) { + const auto *Previous = dyn_cast(D); + return areMockAndExpectMethods(Previous, Method) ? Previous : nullptr; } } + return nullptr; + } + if (const auto *Next = dyn_cast_or_null( + Method->getNextDeclInContext())) { + if (looksLikeExpectMethod(Next) && areMockAndExpectMethods(Method, Next)) + return Method; } return nullptr; } -- cgit v1.2.3