[clang-tools-extra] r294293 - [clang-tidy] misc-argument-comment - extended gmock support
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 7 03:39:57 PST 2017
Author: alexfh
Date: Tue Feb 7 05:39:56 2017
New Revision: 294293
URL: http://llvm.org/viewvc/llvm-project?rev=294293&view=rev
Log:
[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.
Modified:
clang-tools-extra/trunk/clang-tidy/misc/ArgumentCommentCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-argument-comment-gmock.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/ArgumentCommentCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/ArgumentCommentCheck.cpp?rev=294293&r1=294292&r2=294293&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/ArgumentCommentCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/ArgumentCommentCheck.cpp Tue Feb 7 05:39:56 2017
@@ -147,33 +147,43 @@ static bool sameName(StringRef InComment
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<CXXMethodDecl>(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<CXXMethodDecl>(D);
+ return areMockAndExpectMethods(Previous, Method) ? Previous : nullptr;
}
}
+ return nullptr;
+ }
+ if (const auto *Next = dyn_cast_or_null<CXXMethodDecl>(
+ Method->getNextDeclInContext())) {
+ if (looksLikeExpectMethod(Next) && areMockAndExpectMethods(Method, Next))
+ return Method;
}
return nullptr;
}
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-argument-comment-gmock.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-argument-comment-gmock.cpp?rev=294293&r1=294292&r2=294293&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-argument-comment-gmock.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-argument-comment-gmock.cpp Tue Feb 7 05:39:56 2017
@@ -80,7 +80,7 @@ class MockStandalone {
MOCK_METHOD2(Method, void(int aaa, int bbb));
};
-void test_gmock() {
+void test_gmock_expectations() {
MockDerived m;
EXPECT_CALL(m, Method(/*param_one=*/1, /*param_tw=*/2));
// CHECK-MESSAGES: [[@LINE-1]]:42: warning: argument name 'param_tw' in comment does not match parameter name 'param_two'
@@ -99,3 +99,10 @@ void test_gmock() {
MockStandalone m2;
EXPECT_CALL(m2, Method(/*aaa=*/5, /*bbc=*/6));
}
+
+void test_gmock_direct_calls() {
+ MockDerived m;
+ m.Method(/*param_one=*/1, /*param_tw=*/2);
+// CHECK-MESSAGES: [[@LINE-1]]:29: warning: argument name 'param_tw' in comment does not match parameter name 'param_two'
+// CHECK-FIXES: m.Method(/*param_one=*/1, /*param_two=*/2);
+}
More information about the cfe-commits
mailing list