brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · b156d75 Raw
88 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include <string>10 11#include "AvoidUnderscoreInGoogletestNameCheck.h"12#include "clang/AST/ASTContext.h"13#include "clang/Frontend/CompilerInstance.h"14#include "clang/Lex/MacroArgs.h"15#include "clang/Lex/PPCallbacks.h"16#include "clang/Lex/Preprocessor.h"17 18namespace clang::tidy::google::readability {19 20constexpr llvm::StringLiteral KDisabledTestPrefix = "DISABLED_";21 22// Determines whether the macro is a Googletest test macro.23static bool isGoogletestTestMacro(StringRef MacroName) {24  static const llvm::StringSet<> MacroNames = {"TEST", "TEST_F", "TEST_P",25                                               "TYPED_TEST", "TYPED_TEST_P"};26  return MacroNames.contains(MacroName);27}28 29namespace {30 31class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks {32public:33  AvoidUnderscoreInGoogletestNameCallback(34      Preprocessor *PP, AvoidUnderscoreInGoogletestNameCheck *Check)35      : PP(PP), Check(Check) {}36 37  // Detects expansions of the TEST, TEST_F, TEST_P, TYPED_TEST, TYPED_TEST_P38  // macros and checks that their arguments do not have any underscores.39  void MacroExpands(const Token &MacroNameToken,40                    const MacroDefinition &MacroDefinition, SourceRange Range,41                    const MacroArgs *Args) override {42    const IdentifierInfo *NameIdentifierInfo =43        MacroNameToken.getIdentifierInfo();44    if (!NameIdentifierInfo)45      return;46    const StringRef MacroName = NameIdentifierInfo->getName();47    if (!isGoogletestTestMacro(MacroName) || !Args ||48        Args->getNumMacroArguments() < 2)49      return;50    const Token *TestSuiteNameToken = Args->getUnexpArgument(0);51    const Token *TestNameToken = Args->getUnexpArgument(1);52    if (!TestSuiteNameToken || !TestNameToken)53      return;54    const std::string TestSuiteNameMaybeDisabled =55        PP->getSpelling(*TestSuiteNameToken);56    StringRef TestSuiteName = TestSuiteNameMaybeDisabled;57    TestSuiteName.consume_front(KDisabledTestPrefix);58    if (TestSuiteName.contains('_'))59      Check->diag(TestSuiteNameToken->getLocation(),60                  "avoid using \"_\" in test suite name \"%0\" according to "61                  "Googletest FAQ")62          << TestSuiteName;63 64    const std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);65    StringRef TestName = TestNameMaybeDisabled;66    TestName.consume_front(KDisabledTestPrefix);67    if (TestName.contains('_'))68      Check->diag(TestNameToken->getLocation(),69                  "avoid using \"_\" in test name \"%0\" according to "70                  "Googletest FAQ")71          << TestName;72  }73 74private:75  Preprocessor *PP;76  AvoidUnderscoreInGoogletestNameCheck *Check;77};78 79} // namespace80 81void AvoidUnderscoreInGoogletestNameCheck::registerPPCallbacks(82    const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {83  PP->addPPCallbacks(84      std::make_unique<AvoidUnderscoreInGoogletestNameCallback>(PP, this));85}86 87} // namespace clang::tidy::google::readability88