12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Frontend/CompilerInstance.h" 15 #include "clang/Lex/MacroArgs.h" 20 namespace readability {
26 static const llvm::StringSet<> MacroNames = {
"TEST",
"TEST_F",
"TEST_P",
27 "TYPED_TEST",
"TYPED_TEST_P"};
28 return MacroNames.find(MacroName) != MacroNames.end();
33 class AvoidUnderscoreInGoogletestNameCallback :
public PPCallbacks {
35 AvoidUnderscoreInGoogletestNameCallback(
37 : PP(PP), Check(Check) {}
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)
47 StringRef MacroName = NameIdentifierInfo->getName();
49 Args->getNumMacroArguments() < 2)
51 const Token *TestCaseNameToken = Args->getUnexpArgument(0);
52 const Token *TestNameToken = Args->getUnexpArgument(1);
53 if (!TestCaseNameToken || !TestNameToken)
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 " 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 " 80 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
82 std::make_unique<AvoidUnderscoreInGoogletestNameCallback>(PP,
this));
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
static bool isGoogletestTestMacro(StringRef MacroName)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
constexpr llvm::StringLiteral kDisabledTestPrefix
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.