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 #include "clang/Lex/PPCallbacks.h"
17 #include "clang/Lex/Preprocessor.h"
18
19 namespace clang {
20 namespace tidy {
21 namespace google {
22 namespace readability {
23
24 constexpr llvm::StringLiteral kDisabledTestPrefix = "DISABLED_";
25
26 // Determines whether the macro is a Googletest test macro.
isGoogletestTestMacro(StringRef MacroName)27 static bool isGoogletestTestMacro(StringRef MacroName) {
28 static const llvm::StringSet<> MacroNames = {"TEST", "TEST_F", "TEST_P",
29 "TYPED_TEST", "TYPED_TEST_P"};
30 return MacroNames.find(MacroName) != MacroNames.end();
31 }
32
33 namespace {
34
35 class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks {
36 public:
AvoidUnderscoreInGoogletestNameCallback(Preprocessor * PP,AvoidUnderscoreInGoogletestNameCheck * Check)37 AvoidUnderscoreInGoogletestNameCallback(
38 Preprocessor *PP, AvoidUnderscoreInGoogletestNameCheck *Check)
39 : PP(PP), Check(Check) {}
40
41 // Detects expansions of the TEST, TEST_F, TEST_P, TYPED_TEST, TYPED_TEST_P
42 // macros and checks that their arguments do not have any underscores.
MacroExpands(const Token & MacroNameToken,const MacroDefinition & MacroDefinition,SourceRange Range,const MacroArgs * Args)43 void MacroExpands(const Token &MacroNameToken,
44 const MacroDefinition &MacroDefinition, SourceRange Range,
45 const MacroArgs *Args) override {
46 IdentifierInfo *NameIdentifierInfo = MacroNameToken.getIdentifierInfo();
47 if (!NameIdentifierInfo)
48 return;
49 StringRef MacroName = NameIdentifierInfo->getName();
50 if (!isGoogletestTestMacro(MacroName) || !Args ||
51 Args->getNumMacroArguments() < 2)
52 return;
53 const Token *TestCaseNameToken = Args->getUnexpArgument(0);
54 const Token *TestNameToken = Args->getUnexpArgument(1);
55 if (!TestCaseNameToken || !TestNameToken)
56 return;
57 std::string TestCaseName = PP->getSpelling(*TestCaseNameToken);
58 if (TestCaseName.find('_') != std::string::npos)
59 Check->diag(TestCaseNameToken->getLocation(),
60 "avoid using \"_\" in test case name \"%0\" according to "
61 "Googletest FAQ")
62 << TestCaseName;
63
64 std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);
65 StringRef TestName = TestNameMaybeDisabled;
66 TestName.consume_front(kDisabledTestPrefix);
67 if (TestName.contains('_'))
68 Check->diag(TestNameToken->getLocation(),
69 "avoid using \"_\" in test name \"%0\" according to "
70 "Googletest FAQ")
71 << TestName;
72 }
73
74 private:
75 Preprocessor *PP;
76 AvoidUnderscoreInGoogletestNameCheck *Check;
77 };
78
79 } // namespace
80
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)81 void AvoidUnderscoreInGoogletestNameCheck::registerPPCallbacks(
82 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
83 PP->addPPCallbacks(
84 std::make_unique<AvoidUnderscoreInGoogletestNameCallback>(PP, this));
85 }
86
87 } // namespace readability
88 } // namespace google
89 } // namespace tidy
90 } // namespace clang
91