10 #include "../utils/OptionsUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 22 const std::vector<std::string> NameList =
24 return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
27 SignedCharMisuseCheck::SignedCharMisuseCheck(StringRef
Name,
30 CharTypdefsToIgnoreList(Options.get(
"CharTypdefsToIgnore",
"")) {}
33 Options.
store(Opts,
"CharTypdefsToIgnore", CharTypdefsToIgnoreList);
40 const auto IntTypedef = qualType(
43 const auto SignedCharType = expr(hasType(qualType(
44 allOf(isAnyCharacter(), isSignedInteger(), unless(IntTypedef)))));
46 const auto IntegerType = qualType(allOf(isInteger(), unless(isAnyCharacter()),
47 unless(booleanType())))
51 const auto ImplicitCastExpr =
52 implicitCastExpr(hasSourceExpression(SignedCharType),
53 hasImplicitDestinationType(IntegerType))
54 .bind(
"castExpression");
56 const auto CStyleCastExpr = cStyleCastExpr(has(ImplicitCastExpr));
57 const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr));
58 const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr));
63 const auto CastExpr = expr(anyOf(ImplicitCastExpr, CStyleCastExpr,
64 StaticCastExpr, FunctionalCastExpr));
67 const auto AssignmentOperatorExpr = expr(binaryOperator(
68 hasOperatorName(
"="), hasLHS(hasType(IntegerType)), hasRHS(CastExpr)));
70 Finder->addMatcher(AssignmentOperatorExpr,
this);
73 const auto Declaration =
74 varDecl(isDefinition(), hasType(IntegerType), hasInitializer(CastExpr));
76 Finder->addMatcher(Declaration,
this);
80 const auto *CastExpression =
81 Result.Nodes.getNodeAs<ImplicitCastExpr>(
"castExpression");
82 const auto *IntegerType = Result.Nodes.getNodeAs<QualType>(
"integerType");
83 assert(CastExpression);
88 Expr::EvalResult EVResult;
89 if (!CastExpression->isValueDependent() &&
90 CastExpression->getSubExpr()->EvaluateAsInt(EVResult, *Result.Context)) {
91 llvm::APSInt Value1 = EVResult.Val.getInt();
92 if (Value1.isNonNegative())
96 diag(CastExpression->getBeginLoc(),
97 "'signed char' to %0 conversion; " 98 "consider casting to 'unsigned char' first.")
static Matcher< TypedefDecl > hasAnyListedName(const std::string &Names)
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.