Skip to content

Commit c67dab5

Browse files
committedJan 25, 2019
[clang-tidy] Add check for underscores in googletest names.
Summary: Adds a clang-tidy warning for underscores in googletest names. Patch by Kar Epker! Reviewers: hokein, alexfh, aaron.ballman Reviewed By: hokein Subscribers: Eugene.Zelenko, JonasToth, MyDeveloperDay, lebedev.ri, xazax.hun, mgorny, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D56424 llvm-svn: 352183
1 parent 914e838 commit c67dab5

8 files changed

+278
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===--- AvoidUnderscoreInGoogletestNameCheck.cpp - clang-tidy --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <string>
10+
11+
#include "AvoidUnderscoreInGoogletestNameCheck.h"
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/ASTMatchers/ASTMatchers.h"
14+
#include "clang/Frontend/CompilerInstance.h"
15+
#include "clang/Lex/MacroArgs.h"
16+
17+
namespace clang {
18+
namespace tidy {
19+
namespace google {
20+
namespace readability {
21+
22+
constexpr llvm::StringLiteral kDisabledTestPrefix = "DISABLED_";
23+
24+
// Determines whether the macro is a Googletest test macro.
25+
static bool isGoogletestTestMacro(StringRef MacroName) {
26+
static const llvm::StringSet<> MacroNames = {"TEST", "TEST_F", "TEST_P",
27+
"TYPED_TEST", "TYPED_TEST_P"};
28+
return MacroNames.find(MacroName) != MacroNames.end();
29+
}
30+
31+
namespace {
32+
33+
class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks {
34+
public:
35+
AvoidUnderscoreInGoogletestNameCallback(
36+
Preprocessor *PP, AvoidUnderscoreInGoogletestNameCheck *Check)
37+
: PP(PP), Check(Check) {}
38+
39+
// Detects expansions of the TEST, TEST_F, TEST_P, TYPED_TEST, TYPED_TEST_P
40+
// macros and checks that their arguments do not have any underscores.
41+
void MacroExpands(const Token &MacroNameToken,
42+
const MacroDefinition &MacroDefinition, SourceRange Range,
43+
const MacroArgs *Args) override {
44+
IdentifierInfo *NameIdentifierInfo = MacroNameToken.getIdentifierInfo();
45+
if (!NameIdentifierInfo)
46+
return;
47+
StringRef MacroName = NameIdentifierInfo->getName();
48+
if (!isGoogletestTestMacro(MacroName) || !Args ||
49+
Args->getNumMacroArguments() < 2)
50+
return;
51+
const Token *TestCaseNameToken = Args->getUnexpArgument(0);
52+
const Token *TestNameToken = Args->getUnexpArgument(1);
53+
if (!TestCaseNameToken || !TestNameToken)
54+
return;
55+
std::string TestCaseName = PP->getSpelling(*TestCaseNameToken);
56+
if (TestCaseName.find('_') != std::string::npos)
57+
Check->diag(TestCaseNameToken->getLocation(),
58+
"avoid using \"_\" in test case name \"%0\" according to "
59+
"Googletest FAQ")
60+
<< TestCaseName;
61+
62+
std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);
63+
StringRef TestName = TestNameMaybeDisabled;
64+
TestName.consume_front(kDisabledTestPrefix);
65+
if (TestName.contains('_'))
66+
Check->diag(TestNameToken->getLocation(),
67+
"avoid using \"_\" in test name \"%0\" according to "
68+
"Googletest FAQ")
69+
<< TestName;
70+
}
71+
72+
private:
73+
Preprocessor *PP;
74+
AvoidUnderscoreInGoogletestNameCheck *Check;
75+
};
76+
77+
} // namespace
78+
79+
void AvoidUnderscoreInGoogletestNameCheck::registerPPCallbacks(
80+
CompilerInstance &Compiler) {
81+
Compiler.getPreprocessor().addPPCallbacks(
82+
llvm::make_unique<AvoidUnderscoreInGoogletestNameCallback>(
83+
&Compiler.getPreprocessor(), this));
84+
}
85+
86+
} // namespace readability
87+
} // namespace google
88+
} // namespace tidy
89+
} // namespace clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- AvoidUnderscoreInGoogletestNameCheck.h - clang-tidy ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDUNDERSCOREINGOOGLETESTNAMECHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDUNDERSCOREINGOOGLETESTNAMECHECK_H
11+
12+
#include "../ClangTidy.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace google {
17+
namespace readability {
18+
19+
// Check for underscores in the names of googletest tests, per
20+
// https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore
21+
class AvoidUnderscoreInGoogletestNameCheck : public ClangTidyCheck {
22+
public:
23+
using ClangTidyCheck::ClangTidyCheck;
24+
25+
void registerPPCallbacks(CompilerInstance &Compiler) override;
26+
};
27+
28+
} // namespace readability
29+
} // namespace google
30+
} // namespace tidy
31+
} // namespace clang
32+
33+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDUNDERSCOREINGOOGLETESTNAMECHECK_H

‎clang-tools-extra/clang-tidy/google/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
33
add_clang_library(clangTidyGoogleModule
44
AvoidCStyleCastsCheck.cpp
55
AvoidThrowingObjCExceptionCheck.cpp
6+
AvoidUnderscoreInGoogletestNameCheck.cpp
67
DefaultArgumentsCheck.cpp
78
ExplicitConstructorCheck.cpp
89
ExplicitMakePairCheck.cpp

‎clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../readability/NamespaceCommentCheck.h"
1515
#include "AvoidCStyleCastsCheck.h"
1616
#include "AvoidThrowingObjCExceptionCheck.h"
17+
#include "AvoidUnderscoreInGoogletestNameCheck.h"
1718
#include "DefaultArgumentsCheck.h"
1819
#include "ExplicitConstructorCheck.h"
1920
#include "ExplicitMakePairCheck.h"
@@ -60,6 +61,9 @@ class GoogleModule : public ClangTidyModule {
6061
"google-runtime-operator");
6162
CheckFactories.registerCheck<runtime::NonConstReferences>(
6263
"google-runtime-references");
64+
CheckFactories
65+
.registerCheck<readability::AvoidUnderscoreInGoogletestNameCheck>(
66+
"google-readability-avoid-underscore-in-googletest-name");
6367
CheckFactories.registerCheck<readability::AvoidCStyleCastsCheck>(
6468
"google-readability-casting");
6569
CheckFactories.registerCheck<readability::TodoCommentCheck>(

‎clang-tools-extra/docs/ReleaseNotes.rst

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ Improvements to clang-tidy
7373
Checks for casts of ``absl::Duration`` conversion functions, and recommends
7474
the right conversion function instead.
7575

76+
- New :doc:`google-readability-avoid-underscore-in-googletest-name
77+
<clang-tidy/checks/google-readability-avoid-underscore-in-googletest-name>`
78+
check.
79+
80+
Checks whether there are underscores in googletest test and test case names in
81+
test macros, which is prohibited by the Googletest FAQ.
82+
83+
7684
Improvements to include-fixer
7785
-----------------------------
7886

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. title:: clang-tidy - google-readability-avoid-underscore-in-googletest-name
2+
3+
google-readability-avoid-underscore-in-googletest-name
4+
======================================================
5+
6+
Checks whether there are underscores in googletest test and test case names in
7+
test macros:
8+
9+
- ``TEST``
10+
- ``TEST_F``
11+
- ``TEST_P``
12+
- ``TYPED_TEST``
13+
- ``TYPED_TEST_P``
14+
15+
The ``FRIEND_TEST`` macro is not included.
16+
17+
For example:
18+
19+
.. code-block:: c++
20+
21+
TEST(TestCaseName, Illegal_TestName) {}
22+
TEST(Illegal_TestCaseName, TestName) {}
23+
24+
would trigger the check. `Underscores are not allowed`_ in test names nor test
25+
case names.
26+
27+
The ``DISABLED_`` prefix, which may be used to `disable individual tests`_, is
28+
ignored when checking test names, but the rest of the rest of the test name is
29+
still checked.
30+
31+
This check does not propose any fixes.
32+
33+
.. _Underscores are not allowed: https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore
34+
.. _disable individual tests: https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore

‎clang-tools-extra/docs/clang-tidy/checks/list.rst

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Clang-Tidy Checks
131131
google-objc-avoid-throwing-exception
132132
google-objc-function-naming
133133
google-objc-global-variable-declaration
134+
google-readability-avoid-underscore-in-googletest-name
134135
google-readability-braces-around-statements (redirects to readability-braces-around-statements) <google-readability-braces-around-statements>
135136
google-readability-casting
136137
google-readability-function-size (redirects to readability-function-size) <google-readability-function-size>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// RUN: %check_clang_tidy %s google-readability-avoid-underscore-in-googletest-name %t
2+
3+
#define TEST(test_case_name, test_name) void test_case_name##test_name()
4+
#define TEST_F(test_case_name, test_name) void test_case_name##test_name()
5+
#define TEST_P(test_case_name, test_name) void test_case_name##test_name()
6+
#define TYPED_TEST(test_case_name, test_name) void test_case_name##test_name()
7+
#define TYPED_TEST_P(test_case_name, test_name) void test_case_name##test_name()
8+
#define FRIEND_TEST(test_case_name, test_name) void test_case_name##test_name()
9+
10+
TEST(TestCaseName, Illegal_TestName) {}
11+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
12+
13+
TEST(TestCaseName, DISABLED_Illegal_TestName) {}
14+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
15+
TEST(TestCaseName, Illegal_Test_Name) {}
16+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
17+
TEST(Illegal_TestCaseName, TestName) {}
18+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: avoid using "_" in test case name "Illegal_TestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
19+
TEST(Illegal_Test_CaseName, TestName) {}
20+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: avoid using "_" in test case name "Illegal_Test_CaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
21+
TEST(Illegal_TestCaseName, Illegal_TestName) {}
22+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: avoid using "_" in test case name "Illegal_TestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
23+
// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
24+
25+
TEST_F(TestCaseFixtureName, Illegal_TestName) {}
26+
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
27+
TEST_F(TestCaseFixtureName, DISABLED_Illegal_Test_Name) {}
28+
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
29+
TEST_F(TestCaseFixtureName, Illegal_Test_Name) {}
30+
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
31+
32+
TEST_F(Illegal_TestCaseFixtureName, TestName) {}
33+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_TestCaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
34+
TEST_F(Illegal_TestCaseFixtureName, Illegal_TestName) {}
35+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_TestCaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
36+
// CHECK-MESSAGES: :[[@LINE-2]]:37: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
37+
38+
TEST_F(Illegal_Test_CaseFixtureName, TestName) {}
39+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_Test_CaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
40+
41+
TEST_P(ParameterizedTestCaseFixtureName, Illegal_TestName) {}
42+
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
43+
TEST_P(ParameterizedTestCaseFixtureName, DISABLED_Illegal_TestName) {}
44+
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
45+
TEST_P(ParameterizedTestCaseFixtureName, Illegal_Test_Name) {}
46+
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
47+
48+
TEST_P(Illegal_ParameterizedTestCaseFixtureName, TestName) {}
49+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_ParameterizedTestCaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
50+
TEST_P(Illegal_ParameterizedTestCaseFixtureName, Illegal_TestName) {}
51+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_ParameterizedTestCaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
52+
// CHECK-MESSAGES: :[[@LINE-2]]:50: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
53+
54+
TEST_P(Illegal_Parameterized_TestCaseFixtureName, TestName) {}
55+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: avoid using "_" in test case name "Illegal_Parameterized_TestCaseFixtureName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
56+
57+
TYPED_TEST(TypedTestCaseName, Illegal_TestName) {}
58+
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
59+
TYPED_TEST(TypedTestCaseName, DISABLED_Illegal_TestName) {}
60+
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
61+
TYPED_TEST(TypedTestCaseName, Illegal_Test_Name) {}
62+
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
63+
64+
TYPED_TEST(Illegal_TypedTestCaseName, TestName) {}
65+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: avoid using "_" in test case name "Illegal_TypedTestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
66+
TYPED_TEST(Illegal_TypedTestCaseName, Illegal_TestName) {}
67+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: avoid using "_" in test case name "Illegal_TypedTestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
68+
// CHECK-MESSAGES: :[[@LINE-2]]:39: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
69+
70+
TYPED_TEST(Illegal_Typed_TestCaseName, TestName) {}
71+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: avoid using "_" in test case name "Illegal_Typed_TestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
72+
73+
TYPED_TEST_P(TypeParameterizedTestCaseName, Illegal_TestName) {}
74+
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
75+
TYPED_TEST_P(TypeParameterizedTestCaseName, DISABLED_Illegal_TestName) {}
76+
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
77+
TYPED_TEST_P(TypeParameterizedTestCaseName, Illegal_Test_Name) {}
78+
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: avoid using "_" in test name "Illegal_Test_Name" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
79+
80+
TYPED_TEST_P(Illegal_TypeParameterizedTestCaseName, TestName) {}
81+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: avoid using "_" in test case name "Illegal_TypeParameterizedTestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
82+
TYPED_TEST_P(Illegal_TypeParameterizedTestCaseName, Illegal_TestName) {}
83+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: avoid using "_" in test case name "Illegal_TypeParameterizedTestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
84+
// CHECK-MESSAGES: :[[@LINE-2]]:53: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
85+
86+
TYPED_TEST_P(Illegal_Type_ParameterizedTestCaseName, TestName) {}
87+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: avoid using "_" in test case name "Illegal_Type_ParameterizedTestCaseName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name]
88+
89+
// Underscores are allowed to disable a test with the DISABLED_ prefix.
90+
// https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore
91+
TEST(TestCaseName, TestName) {}
92+
TEST(TestCaseName, DISABLED_TestName) {}
93+
94+
TEST_F(TestCaseFixtureName, TestName) {}
95+
TEST_F(TestCaseFixtureName, DISABLED_TestName) {}
96+
97+
TEST_P(ParameterizedTestCaseFixtureName, TestName) {}
98+
TEST_P(ParameterizedTestCaseFixtureName, DISABLED_TestName) {}
99+
100+
TYPED_TEST(TypedTestName, TestName) {}
101+
TYPED_TEST(TypedTestName, DISABLED_TestName) {}
102+
103+
TYPED_TEST_P(TypeParameterizedTestName, TestName) {}
104+
TYPED_TEST_P(TypeParameterizedTestName, DISABLED_TestName) {}
105+
106+
FRIEND_TEST(FriendTest, Is_NotChecked) {}
107+
FRIEND_TEST(Friend_Test, IsNotChecked) {}
108+
FRIEND_TEST(Friend_Test, Is_NotChecked) {}

0 commit comments

Comments
 (0)
Please sign in to comment.