[clang-tidy][NFC] replace some redundant std::string creations

This commit is contained in:
Nathan James 2022-01-19 18:36:35 +00:00
parent 81c6693329
commit a4bdeb9a56
No known key found for this signature in database
GPG Key ID: CC007AFCDA90AA5F
4 changed files with 133 additions and 129 deletions

View File

@ -23,42 +23,53 @@ namespace optutils = clang::tidy::utils::options;
static constexpr std::size_t DefaultMinimumLength = 2;
/// The default value for ignored parameter names.
static const std::string DefaultIgnoredParameterNames =
optutils::serializeStringList({"\"\"", "iterator", "Iterator", "begin",
"Begin", "end", "End", "first", "First",
"last", "Last", "lhs", "LHS", "rhs", "RHS"});
static constexpr llvm::StringLiteral DefaultIgnoredParameterNames = "\"\";"
"iterator;"
"Iterator;"
"begin;"
"Begin;"
"end;"
"End;"
"first;"
"First;"
"last;"
"Last;"
"lhs;"
"LHS;"
"rhs;"
"RHS";
/// The default value for ignored parameter type suffixes.
static const std::string DefaultIgnoredParameterTypeSuffixes =
optutils::serializeStringList({"bool",
"Bool",
"_Bool",
"it",
"It",
"iterator",
"Iterator",
"inputit",
"InputIt",
"forwardit",
"ForwardIt",
"bidirit",
"BidirIt",
"constiterator",
"const_iterator",
"Const_Iterator",
"Constiterator",
"ConstIterator",
"RandomIt",
"randomit",
"random_iterator",
"ReverseIt",
"reverse_iterator",
"reverse_const_iterator",
"ConstReverseIterator",
"Const_Reverse_Iterator",
"const_reverse_iterator",
"Constreverseiterator",
"constreverseiterator"});
static constexpr llvm::StringLiteral DefaultIgnoredParameterTypeSuffixes =
"bool;"
"Bool;"
"_Bool;"
"it;"
"It;"
"iterator;"
"Iterator;"
"inputit;"
"InputIt;"
"forwardit;"
"ForwardIt;"
"bidirit;"
"BidirIt;"
"constiterator;"
"const_iterator;"
"Const_Iterator;"
"Constiterator;"
"ConstIterator;"
"RandomIt;"
"randomit;"
"random_iterator;"
"ReverseIt;"
"reverse_iterator;"
"reverse_const_iterator;"
"ConstReverseIterator;"
"Const_Reverse_Iterator;"
"const_reverse_iterator;"
"Constreverseiterator;"
"constreverseiterator";
/// The default value for the QualifiersMix check option.
static constexpr bool DefaultQualifiersMix = false;

View File

@ -9,8 +9,8 @@
#include "TypeMismatchCheck.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/FixIt.h"
#include "llvm/ADT/StringSet.h"
#include <map>
#include <unordered_set>
using namespace clang::ast_matchers;
@ -26,9 +26,8 @@ namespace mpi {
///
/// \returns true if the pair matches
static bool
isMPITypeMatching(const std::multimap<BuiltinType::Kind, std::string> &MultiMap,
const BuiltinType::Kind Kind,
const std::string &MPIDatatype) {
isMPITypeMatching(const std::multimap<BuiltinType::Kind, StringRef> &MultiMap,
const BuiltinType::Kind Kind, StringRef MPIDatatype) {
auto ItPair = MultiMap.equal_range(Kind);
while (ItPair.first != ItPair.second) {
if (ItPair.first->second == MPIDatatype)
@ -43,41 +42,40 @@ isMPITypeMatching(const std::multimap<BuiltinType::Kind, std::string> &MultiMap,
/// \param MPIDatatype name of the MPI datatype
///
/// \returns true if the type is a standard type
static bool isStandardMPIDatatype(const std::string &MPIDatatype) {
static std::unordered_set<std::string> AllTypes = {
"MPI_C_BOOL",
"MPI_CHAR",
"MPI_SIGNED_CHAR",
"MPI_UNSIGNED_CHAR",
"MPI_WCHAR",
"MPI_INT",
"MPI_LONG",
"MPI_SHORT",
"MPI_LONG_LONG",
"MPI_LONG_LONG_INT",
"MPI_UNSIGNED",
"MPI_UNSIGNED_SHORT",
"MPI_UNSIGNED_LONG",
"MPI_UNSIGNED_LONG_LONG",
"MPI_FLOAT",
"MPI_DOUBLE",
"MPI_LONG_DOUBLE",
"MPI_C_COMPLEX",
"MPI_C_FLOAT_COMPLEX",
"MPI_C_DOUBLE_COMPLEX",
"MPI_C_LONG_DOUBLE_COMPLEX",
"MPI_INT8_T",
"MPI_INT16_T",
"MPI_INT32_T",
"MPI_INT64_T",
"MPI_UINT8_T",
"MPI_UINT16_T",
"MPI_UINT32_T",
"MPI_UINT64_T",
"MPI_CXX_BOOL",
"MPI_CXX_FLOAT_COMPLEX",
"MPI_CXX_DOUBLE_COMPLEX",
"MPI_CXX_LONG_DOUBLE_COMPLEX"};
static bool isStandardMPIDatatype(StringRef MPIDatatype) {
static llvm::StringSet<> AllTypes = {"MPI_C_BOOL",
"MPI_CHAR",
"MPI_SIGNED_CHAR",
"MPI_UNSIGNED_CHAR",
"MPI_WCHAR",
"MPI_INT",
"MPI_LONG",
"MPI_SHORT",
"MPI_LONG_LONG",
"MPI_LONG_LONG_INT",
"MPI_UNSIGNED",
"MPI_UNSIGNED_SHORT",
"MPI_UNSIGNED_LONG",
"MPI_UNSIGNED_LONG_LONG",
"MPI_FLOAT",
"MPI_DOUBLE",
"MPI_LONG_DOUBLE",
"MPI_C_COMPLEX",
"MPI_C_FLOAT_COMPLEX",
"MPI_C_DOUBLE_COMPLEX",
"MPI_C_LONG_DOUBLE_COMPLEX",
"MPI_INT8_T",
"MPI_INT16_T",
"MPI_INT32_T",
"MPI_INT64_T",
"MPI_UINT8_T",
"MPI_UINT16_T",
"MPI_UINT32_T",
"MPI_UINT64_T",
"MPI_CXX_BOOL",
"MPI_CXX_FLOAT_COMPLEX",
"MPI_CXX_DOUBLE_COMPLEX",
"MPI_CXX_LONG_DOUBLE_COMPLEX"};
return AllTypes.find(MPIDatatype) != AllTypes.end();
}
@ -92,9 +90,9 @@ static bool isStandardMPIDatatype(const std::string &MPIDatatype) {
/// \returns true if the type matches
static bool isBuiltinTypeMatching(const BuiltinType *Builtin,
std::string &BufferTypeName,
const std::string &MPIDatatype,
StringRef MPIDatatype,
const LangOptions &LO) {
static std::multimap<BuiltinType::Kind, std::string> BuiltinMatches = {
static std::multimap<BuiltinType::Kind, StringRef> BuiltinMatches = {
// On some systems like PPC or ARM, 'char' is unsigned by default which is
// why distinct signedness for the buffer and MPI type is tolerated.
{BuiltinType::SChar, "MPI_CHAR"},
@ -145,9 +143,9 @@ static bool isBuiltinTypeMatching(const BuiltinType *Builtin,
/// \returns true if the type matches or the buffer type is unknown
static bool isCComplexTypeMatching(const ComplexType *const Complex,
std::string &BufferTypeName,
const std::string &MPIDatatype,
StringRef MPIDatatype,
const LangOptions &LO) {
static std::multimap<BuiltinType::Kind, std::string> ComplexCMatches = {
static std::multimap<BuiltinType::Kind, StringRef> ComplexCMatches = {
{BuiltinType::Float, "MPI_C_COMPLEX"},
{BuiltinType::Float, "MPI_C_FLOAT_COMPLEX"},
{BuiltinType::Double, "MPI_C_DOUBLE_COMPLEX"},
@ -175,10 +173,9 @@ static bool isCComplexTypeMatching(const ComplexType *const Complex,
/// \returns true if the type matches or the buffer type is unknown
static bool
isCXXComplexTypeMatching(const TemplateSpecializationType *const Template,
std::string &BufferTypeName,
const std::string &MPIDatatype,
std::string &BufferTypeName, StringRef MPIDatatype,
const LangOptions &LO) {
static std::multimap<BuiltinType::Kind, std::string> ComplexCXXMatches = {
static std::multimap<BuiltinType::Kind, StringRef> ComplexCXXMatches = {
{BuiltinType::Float, "MPI_CXX_FLOAT_COMPLEX"},
{BuiltinType::Double, "MPI_CXX_DOUBLE_COMPLEX"},
{BuiltinType::LongDouble, "MPI_CXX_LONG_DOUBLE_COMPLEX"}};
@ -208,8 +205,8 @@ isCXXComplexTypeMatching(const TemplateSpecializationType *const Template,
/// \returns true if the type matches or the buffer type is unknown
static bool isTypedefTypeMatching(const TypedefType *const Typedef,
std::string &BufferTypeName,
const std::string &MPIDatatype) {
static llvm::StringMap<std::string> FixedWidthMatches = {
StringRef MPIDatatype) {
static llvm::StringMap<StringRef> FixedWidthMatches = {
{"int8_t", "MPI_INT8_T"}, {"int16_t", "MPI_INT16_T"},
{"int32_t", "MPI_INT32_T"}, {"int64_t", "MPI_INT64_T"},
{"uint8_t", "MPI_UINT8_T"}, {"uint16_t", "MPI_UINT16_T"},
@ -272,8 +269,7 @@ void TypeMismatchCheck::check(const MatchFinder::MatchResult &Result) {
const Type *ArgType = argumentType(CE, BufferIdx);
// Skip unknown MPI datatypes and void pointers.
if (!isStandardMPIDatatype(std::string(MPIDatatype)) ||
ArgType->isVoidType())
if (!isStandardMPIDatatype(MPIDatatype) || ArgType->isVoidType())
return;
BufferTypes.push_back(ArgType);
@ -311,17 +307,16 @@ void TypeMismatchCheck::checkArguments(ArrayRef<const Type *> BufferTypes,
bool Error = false;
if (const auto *Typedef = BT->getAs<TypedefType>()) {
Error = !isTypedefTypeMatching(Typedef, BufferTypeName,
std::string(MPIDatatypes[I]));
Error = !isTypedefTypeMatching(Typedef, BufferTypeName, MPIDatatypes[I]);
} else if (const auto *Complex = BT->getAs<ComplexType>()) {
Error = !isCComplexTypeMatching(Complex, BufferTypeName,
std::string(MPIDatatypes[I]), LO);
Error =
!isCComplexTypeMatching(Complex, BufferTypeName, MPIDatatypes[I], LO);
} else if (const auto *Template = BT->getAs<TemplateSpecializationType>()) {
Error = !isCXXComplexTypeMatching(Template, BufferTypeName,
std::string(MPIDatatypes[I]), LO);
MPIDatatypes[I], LO);
} else if (const auto *Builtin = BT->getAs<BuiltinType>()) {
Error = !isBuiltinTypeMatching(Builtin, BufferTypeName,
std::string(MPIDatatypes[I]), LO);
Error =
!isBuiltinTypeMatching(Builtin, BufferTypeName, MPIDatatypes[I], LO);
}
if (Error) {

View File

@ -24,16 +24,17 @@ struct PreprocessorEntry {
std::string Condition;
};
const char WarningDescription[] =
"nested redundant %select{#if|#ifdef|#ifndef}0; consider removing it";
const char NoteDescription[] = "previous %select{#if|#ifdef|#ifndef}0 was here";
class RedundantPreprocessorCallbacks : public PPCallbacks {
enum DirectiveKind { DK_If = 0, DK_Ifdef = 1, DK_Ifndef = 2 };
public:
explicit RedundantPreprocessorCallbacks(ClangTidyCheck &Check,
Preprocessor &PP)
: Check(Check), PP(PP),
WarningDescription("nested redundant %select{#if|#ifdef|#ifndef}0; "
"consider removing it"),
NoteDescription("previous %select{#if|#ifdef|#ifndef}0 was here") {}
: Check(Check), PP(PP) {}
void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override {
@ -94,8 +95,6 @@ private:
SmallVector<PreprocessorEntry, 4> IfStack;
SmallVector<PreprocessorEntry, 4> IfdefStack;
SmallVector<PreprocessorEntry, 4> IfndefStack;
const std::string WarningDescription;
const std::string NoteDescription;
};
} // namespace

View File

@ -93,38 +93,37 @@ struct AllHeuristicsBoundsWellConfigured {
static_assert(AllHeuristicsBoundsWellConfigured::Value, "");
} // namespace
static const std::string DefaultAbbreviations =
optutils::serializeStringList({"addr=address",
"arr=array",
"attr=attribute",
"buf=buffer",
"cl=client",
"cnt=count",
"col=column",
"cpy=copy",
"dest=destination",
"dist=distance"
"dst=distance",
"elem=element",
"hght=height",
"i=index",
"idx=index",
"len=length",
"ln=line",
"lst=list",
"nr=number",
"num=number",
"pos=position",
"ptr=pointer",
"ref=reference",
"src=source",
"srv=server",
"stmt=statement",
"str=string",
"val=value",
"var=variable",
"vec=vector",
"wdth=width"});
static constexpr llvm::StringLiteral DefaultAbbreviations = "addr=address;"
"arr=array;"
"attr=attribute;"
"buf=buffer;"
"cl=client;"
"cnt=count;"
"col=column;"
"cpy=copy;"
"dest=destination;"
"dist=distance"
"dst=distance;"
"elem=element;"
"hght=height;"
"i=index;"
"idx=index;"
"len=length;"
"ln=line;"
"lst=list;"
"nr=number;"
"num=number;"
"pos=position;"
"ptr=pointer;"
"ref=reference;"
"src=source;"
"srv=server;"
"stmt=statement;"
"str=string;"
"val=value;"
"var=variable;"
"vec=vector;"
"wdth=width";
static constexpr std::size_t SmallVectorSize =
SuspiciousCallArgumentCheck::SmallVectorSize;