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"
25 static const llvm::StringSet<> MacroNames = {
"TEST",
"TEST_F",
"TEST_P",
26 "TYPED_TEST",
"TYPED_TEST_P"};
32class AvoidUnderscoreInGoogletestNameCallback :
public PPCallbacks {
34 AvoidUnderscoreInGoogletestNameCallback(
35 Preprocessor *PP, AvoidUnderscoreInGoogletestNameCheck *Check)
36 : PP(PP), Check(Check) {}
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)
46 StringRef
MacroName = NameIdentifierInfo->getName();
48 Args->getNumMacroArguments() < 2)
50 const Token *TestSuiteNameToken =
Args->getUnexpArgument(0);
51 const Token *TestNameToken =
Args->getUnexpArgument(1);
52 if (!TestSuiteNameToken || !TestNameToken)
54 std::string TestSuiteNameMaybeDisabled =
55 PP->getSpelling(*TestSuiteNameToken);
56 StringRef TestSuiteName = TestSuiteNameMaybeDisabled;
58 if (TestSuiteName.contains(
'_'))
59 Check->diag(TestSuiteNameToken->getLocation(),
60 "avoid using \"_\" in test suite name \"%0\" according to "
64 std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);
65 StringRef TestName = TestNameMaybeDisabled;
67 if (TestName.contains(
'_'))
68 Check->diag(TestNameToken->getLocation(),
69 "avoid using \"_\" in test name \"%0\" according to "
76 AvoidUnderscoreInGoogletestNameCheck *Check;
82 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
84 std::make_unique<AvoidUnderscoreInGoogletestNameCallback>(PP,
this));
CharSourceRange Range
SourceRange for the file name.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
static bool isGoogletestTestMacro(StringRef MacroName)
constexpr llvm::StringLiteral KDisabledTestPrefix