comparison clang-tools-extra/clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
147:c2174574ed3a 150:1d019706d866
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 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
81 PP->addPPCallbacks(
82 std::make_unique<AvoidUnderscoreInGoogletestNameCallback>(PP, this));
83 }
84
85 } // namespace readability
86 } // namespace google
87 } // namespace tidy
88 } // namespace clang