clang-tools 10.0.0

AvoidUnderscoreInGoogletestNameCheck.cpp
Go to the documentation of this file.
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
AvoidUnderscoreInGoogletestNameCheck.h
clang::tidy::google::readability::AvoidUnderscoreInGoogletestNameCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: AvoidUnderscoreInGoogletestNameCheck.cpp:79
clang::tidy::google::readability::isGoogletestTestMacro
static bool isGoogletestTestMacro(StringRef MacroName)
Definition: AvoidUnderscoreInGoogletestNameCheck.cpp:25
PPCallbacks
clang::tidy::google::readability::AvoidUnderscoreInGoogletestNameCheck
Definition: AvoidUnderscoreInGoogletestNameCheck.h:21
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::google::readability::kDisabledTestPrefix
constexpr llvm::StringLiteral kDisabledTestPrefix
Definition: AvoidUnderscoreInGoogletestNameCheck.cpp:22
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21