Index: clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.h @@ -0,0 +1,34 @@ +//===--- AvoidUnderscoreInGoogletestNameCheck.h - clang-tidy---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_READABILITY_UNDERSCORE_IN_GOOGLETEST_TEST_MACRO_H_ +#define LLVM_TOOLS_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_READABILITY_UNDERSCORE_IN_GOOGLETEST_TEST_MACRO_H_ + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace google { +namespace readability { + +// Check for underscores in the names of googletest tests, per +// https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore +class AvoidUnderscoreInGoogletestNameCheck : public ClangTidyCheck { +public: + using ClangTidyCheck::ClangTidyCheck; + + void registerPPCallbacks(CompilerInstance &Compiler) override; +}; + +} // namespace readability +} // namespace google +} // namespace tidy +} // namespace clang + +#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_READABILITY_UNDERSCORE_IN_GOOGLETEST_TEST_MACRO_H_ Index: clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.cpp @@ -0,0 +1,97 @@ +//===--- AvoidUnderscoreInGoogletestNameCheck.cpp - clang-tidy-------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include + +#include "AvoidUnderscoreInGoogletestNameCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/MacroArgs.h" + +namespace clang { +namespace tidy { +namespace google { +namespace readability { + +constexpr char kDisabledTestPrefix[] = "DISABLED_"; + +// Determines whether the macro is a Googletest test macro. +static bool isGoogletestTestMacro(StringRef MacroName) { + static const llvm::StringSet<> MacroNames = {"TEST", "TEST_F", "TEST_P", + "TYPED_TEST", "TYPED_TEST_P"}; + return MacroNames.find(MacroName) != MacroNames.end(); +} + +namespace { + +class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks { +public: + AvoidUnderscoreInGoogletestNameCallback( + Preprocessor *PP, AvoidUnderscoreInGoogletestNameCheck *Check) + : PP(PP), Check(Check) {} + + // Detects expansions of the TEST, TEST_F, TEST_P, TYPED_TEST, TYPED_TEST_P + // macros and checks that their arguments do not have any underscores. + void MacroExpands(const Token &MacroNameToken, + const MacroDefinition &MacroDefinition, SourceRange Range, + const MacroArgs *Args) override { + auto IdentifierInfo = MacroNameToken.getIdentifierInfo(); + if (!IdentifierInfo) { + return; + } + const StringRef MacroName = IdentifierInfo->getName(); + if (!isGoogletestTestMacro(MacroName)) { + return; + } + if (!Args || Args->getNumMacroArguments() < 2) { + return; + } + const Token *TestCaseNameToken = Args->getUnexpArgument(0); + const Token *TestNameToken = Args->getUnexpArgument(1); + if (!TestCaseNameToken || !TestNameToken) { + return; + } + std::string TestCaseName = PP->getSpelling(*TestCaseNameToken); + if (TestCaseName.find('_') != std::string::npos) { + Check->diag(TestCaseNameToken->getLocation(), + "avoid using \"_\" in test case name \"%0\" according to " + "Googletest FAQ") + << TestCaseName; + } + + std::string TestName_maybe_disabled = PP->getSpelling(*TestNameToken); + StringRef TestName = TestName_maybe_disabled; + TestName.consume_front(kDisabledTestPrefix); + if (TestName.find('_') != std::string::npos) { + Check->diag(TestNameToken->getLocation(), + "avoid using \"_\" in test name \"%0\" according to " + "Googletest FAQ") + << TestName; + } + } + +private: + Preprocessor *PP; + AvoidUnderscoreInGoogletestNameCheck *Check; +}; + +} // namespace + +void AvoidUnderscoreInGoogletestNameCheck::registerPPCallbacks( + CompilerInstance &Compiler) { + Compiler.getPreprocessor().addPPCallbacks( + llvm::make_unique( + &Compiler.getPreprocessor(), this)); +} + +} // namespace readability +} // namespace google +} // namespace tidy +} // namespace clang Index: clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tidy/google/CMakeLists.txt +++ clang-tidy/google/CMakeLists.txt @@ -3,6 +3,7 @@ add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp AvoidThrowingObjCExceptionCheck.cpp + AvoidUnderscoreInGoogletestNameCheck.cpp DefaultArgumentsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -15,6 +15,7 @@ #include "../readability/NamespaceCommentCheck.h" #include "AvoidCStyleCastsCheck.h" #include "AvoidThrowingObjCExceptionCheck.h" +#include "AvoidUnderscoreInGoogletestNameCheck.h" #include "DefaultArgumentsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" @@ -61,6 +62,9 @@ "google-runtime-operator"); CheckFactories.registerCheck( "google-runtime-references"); + CheckFactories + .registerCheck( + "google-readability-avoid-underscore-in-googletest-name"); CheckFactories.registerCheck( "google-readability-casting"); CheckFactories.registerCheck( Index: docs/clang-tidy/checks/google-readability-avoid-underscore-in-googletest-name.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/google-readability-avoid-underscore-in-googletest-name.rst @@ -0,0 +1,26 @@ +.. title:: clang-tidy - google-readability-avoid-underscore-in-googletest-name + +google-readability-avoid-underscore-in-googletest-name +====================================================== + +Checks whether there are underscores in googletest test and test case names in +test macros, not including the ``FRIEND_TEST`` macro. + +For example: + +.. code-block:: c++ + + TEST(TestCaseName, Illegal_TestName) {} + TEST(Illegal_TestCaseName, TestName) {} + +would trigger the check. `Underscores are not allowed`_ in test names nor test +case names. + +The ``DISABLED_`` prefix, which may be used to `disable individual tests`_, is +ignored when checking test names, but the rest of the rest of the test name is +still checked. + +This check does not propose any fixes. + +.. _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 +.. _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 Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -130,6 +130,7 @@ google-objc-avoid-throwing-exception google-objc-function-naming google-objc-global-variable-declaration + google-readability-avoid-underscore-in-googletest-name google-readability-braces-around-statements (redirects to readability-braces-around-statements) google-readability-casting google-readability-function-size (redirects to readability-function-size) Index: test/clang-tidy/readability-avoid-underscore-in-googletest-name.cpp =================================================================== --- /dev/null +++ test/clang-tidy/readability-avoid-underscore-in-googletest-name.cpp @@ -0,0 +1,103 @@ +// RUN: %check_clang_tidy %s google-readability-avoid-underscore-in-googletest-name %t + +#define TEST(test_case_name, test_name) void test_case_name##test_name() +#define TEST_F(test_case_name, test_name) void test_case_name##test_name() +#define TEST_P(test_case_name, test_name) void test_case_name##test_name() +#define TYPED_TEST(test_case_name, test_name) void test_case_name##test_name() +#define TYPED_TEST_P(test_case_name, test_name) void test_case_name##test_name() + +TEST(TestCaseName, Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TEST(TestCaseName, DISABLED_Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TEST(TestCaseName, Illegal_Test_Name) {} +// 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] +TEST(Illegal_TestCaseName, TestName) {} +// 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] +TEST(Illegal_Test_CaseName, TestName) {} +// 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] +TEST(Illegal_TestCaseName, Illegal_TestName) {} +// 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] +// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TEST_F(TestCaseFixtureName, Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TEST_F(TestCaseFixtureName, DISABLED_Illegal_Test_Name) {} +// 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] +TEST_F(TestCaseFixtureName, Illegal_Test_Name) {} +// 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] + +TEST_F(Illegal_TestCaseFixtureName, TestName) {} +// 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] +TEST_F(Illegal_TestCaseFixtureName, Illegal_TestName) {} +// 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] +// CHECK-MESSAGES: :[[@LINE-2]]:37: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TEST_F(Illegal_Test_CaseFixtureName, TestName) {} +// 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] + +TEST_P(ParameterizedTestCaseFixtureName, Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TEST_P(ParameterizedTestCaseFixtureName, DISABLED_Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TEST_P(ParameterizedTestCaseFixtureName, Illegal_Test_Name) {} +// 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] + +TEST_P(Illegal_ParameterizedTestCaseFixtureName, TestName) {} +// 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] +TEST_P(Illegal_ParameterizedTestCaseFixtureName, Illegal_TestName) {} +// 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] +// CHECK-MESSAGES: :[[@LINE-2]]:50: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TEST_P(Illegal_Parameterized_TestCaseFixtureName, TestName) {} +// 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] + +TYPED_TEST(TypedTestCaseName, Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TYPED_TEST(TypedTestCaseName, DISABLED_Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TYPED_TEST(TypedTestCaseName, Illegal_Test_Name) {} +// 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] + +TYPED_TEST(Illegal_TypedTestCaseName, TestName) {} +// 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] +TYPED_TEST(Illegal_TypedTestCaseName, Illegal_TestName) {} +// 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] +// CHECK-MESSAGES: :[[@LINE-2]]:39: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TYPED_TEST(Illegal_Typed_TestCaseName, TestName) {} +// 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] + +TYPED_TEST_P(TypeParameterizedTestCaseName, Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TYPED_TEST_P(TypeParameterizedTestCaseName, DISABLED_Illegal_TestName) {} +// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] +TYPED_TEST_P(TypeParameterizedTestCaseName, Illegal_Test_Name) {} +// 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] + +TYPED_TEST_P(Illegal_TypeParameterizedTestCaseName, TestName) {} +// 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] +TYPED_TEST_P(Illegal_TypeParameterizedTestCaseName, Illegal_TestName) {} +// 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] +// CHECK-MESSAGES: :[[@LINE-2]]:53: warning: avoid using "_" in test name "Illegal_TestName" according to Googletest FAQ [google-readability-avoid-underscore-in-googletest-name] + +TYPED_TEST_P(Illegal_Type_ParameterizedTestCaseName, TestName) {} +// 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] + +// Underscores are allowed to disable a test with the DISABLED_ prefix. +// https://github.com/google/googletest/blob/master/googletest/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore +TEST(TestCaseName, TestName) {} +TEST(TestCaseName, DISABLED_TestName) {} + +TEST_F(TestCaseFixtureName, TestName) {} +TEST_F(TestCaseFixtureName, DISABLED_TestName) {} + +TEST_P(ParameterizedTestCaseFixtureName, TestName) {} +TEST_P(ParameterizedTestCaseFixtureName, DISABLED_TestName) {} + +TYPED_TEST(TypedTestName, TestName) {} +TYPED_TEST(TypedTestName, DISABLED_TestName) {} + +TYPED_TEST_P(TypeParameterizedTestName, TestName) {} +TYPED_TEST_P(TypeParameterizedTestName, DISABLED_TestName) {}