Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.cpp

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