clang-tools  14.0.0git
SignedCharMisuseCheck.cpp
Go to the documentation of this file.
1 //===--- SignedCharMisuseCheck.cpp - clang-tidy ---------------------------===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 using namespace clang::ast_matchers::internal;
16 
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20 
21 static constexpr int UnsignedASCIIUpperBound = 127;
22 
23 static Matcher<TypedefDecl> hasAnyListedName(const std::string &Names) {
24  const std::vector<std::string> NameList =
26  return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
27 }
28 
29 SignedCharMisuseCheck::SignedCharMisuseCheck(StringRef Name,
30  ClangTidyContext *Context)
31  : ClangTidyCheck(Name, Context),
32  CharTypdefsToIgnoreList(Options.get("CharTypdefsToIgnore", "")),
33  DiagnoseSignedUnsignedCharComparisons(
34  Options.get("DiagnoseSignedUnsignedCharComparisons", true)) {}
35 
37  Options.store(Opts, "CharTypdefsToIgnore", CharTypdefsToIgnoreList);
38  Options.store(Opts, "DiagnoseSignedUnsignedCharComparisons",
39  DiagnoseSignedUnsignedCharComparisons);
40 }
41 
42 // Create a matcher for char -> integer cast.
43 BindableMatcher<clang::Stmt> SignedCharMisuseCheck::charCastExpression(
44  bool IsSigned, const Matcher<clang::QualType> &IntegerType,
45  const std::string &CastBindName) const {
46  // We can ignore typedefs which are some kind of integer types
47  // (e.g. typedef char sal_Int8). In this case, we don't need to
48  // worry about the misinterpretation of char values.
49  const auto IntTypedef = qualType(
50  hasDeclaration(typedefDecl(hasAnyListedName(CharTypdefsToIgnoreList))));
51 
52  auto CharTypeExpr = expr();
53  if (IsSigned) {
54  CharTypeExpr = expr(hasType(
55  qualType(isAnyCharacter(), isSignedInteger(), unless(IntTypedef))));
56  } else {
57  CharTypeExpr = expr(hasType(qualType(
58  isAnyCharacter(), unless(isSignedInteger()), unless(IntTypedef))));
59  }
60 
61  const auto ImplicitCastExpr =
62  implicitCastExpr(hasSourceExpression(CharTypeExpr),
63  hasImplicitDestinationType(IntegerType))
64  .bind(CastBindName);
65 
66  const auto CStyleCastExpr = cStyleCastExpr(has(ImplicitCastExpr));
67  const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr));
68  const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr));
69 
70  // We catch any type of casts to an integer. We need to have these cast
71  // expressions explicitly to catch only those casts which are direct children
72  // of the checked expressions. (e.g. assignment, declaration).
73  return traverse(TK_AsIs, expr(anyOf(ImplicitCastExpr, CStyleCastExpr,
74  StaticCastExpr, FunctionalCastExpr)));
75 }
76 
77 void SignedCharMisuseCheck::registerMatchers(MatchFinder *Finder) {
78  const auto IntegerType =
79  qualType(isInteger(), unless(isAnyCharacter()), unless(booleanType()))
80  .bind("integerType");
81  const auto SignedCharCastExpr =
82  charCastExpression(true, IntegerType, "signedCastExpression");
83  const auto UnSignedCharCastExpr =
84  charCastExpression(false, IntegerType, "unsignedCastExpression");
85 
86  // Catch assignments with signed char -> integer conversion.
87  const auto AssignmentOperatorExpr =
88  expr(binaryOperator(hasOperatorName("="), hasLHS(hasType(IntegerType)),
89  hasRHS(SignedCharCastExpr)));
90 
91  Finder->addMatcher(AssignmentOperatorExpr, this);
92 
93  // Catch declarations with signed char -> integer conversion.
94  const auto Declaration = varDecl(isDefinition(), hasType(IntegerType),
95  hasInitializer(SignedCharCastExpr));
96 
97  Finder->addMatcher(Declaration, this);
98 
99  if (DiagnoseSignedUnsignedCharComparisons) {
100  // Catch signed char/unsigned char comparison.
101  const auto CompareOperator =
102  expr(binaryOperator(hasAnyOperatorName("==", "!="),
103  anyOf(allOf(hasLHS(SignedCharCastExpr),
104  hasRHS(UnSignedCharCastExpr)),
105  allOf(hasLHS(UnSignedCharCastExpr),
106  hasRHS(SignedCharCastExpr)))))
107  .bind("comparison");
108 
109  Finder->addMatcher(CompareOperator, this);
110  }
111 
112  // Catch array subscripts with signed char -> integer conversion.
113  // Matcher for C arrays.
114  const auto CArraySubscript =
115  arraySubscriptExpr(hasIndex(SignedCharCastExpr)).bind("arraySubscript");
116 
117  Finder->addMatcher(CArraySubscript, this);
118 
119  // Matcher for std arrays.
120  const auto STDArraySubscript =
121  cxxOperatorCallExpr(
122  hasOverloadedOperatorName("[]"),
123  hasArgument(0, hasType(cxxRecordDecl(hasName("::std::array")))),
124  hasArgument(1, SignedCharCastExpr))
125  .bind("arraySubscript");
126 
127  Finder->addMatcher(STDArraySubscript, this);
128 }
129 
130 void SignedCharMisuseCheck::check(const MatchFinder::MatchResult &Result) {
131  const auto *SignedCastExpression =
132  Result.Nodes.getNodeAs<ImplicitCastExpr>("signedCastExpression");
133  const auto *IntegerType = Result.Nodes.getNodeAs<QualType>("integerType");
134  assert(SignedCastExpression);
135  assert(IntegerType);
136 
137  // Ignore the match if we know that the signed char's value is not negative.
138  // The potential misinterpretation happens for negative values only.
139  Expr::EvalResult EVResult;
140  if (!SignedCastExpression->isValueDependent() &&
141  SignedCastExpression->getSubExpr()->EvaluateAsInt(EVResult,
142  *Result.Context)) {
143  llvm::APSInt Value = EVResult.Val.getInt();
144  if (Value.isNonNegative())
145  return;
146  }
147 
148  if (const auto *Comparison = Result.Nodes.getNodeAs<Expr>("comparison")) {
149  const auto *UnSignedCastExpression =
150  Result.Nodes.getNodeAs<ImplicitCastExpr>("unsignedCastExpression");
151 
152  // We can ignore the ASCII value range also for unsigned char.
153  Expr::EvalResult EVResult;
154  if (!UnSignedCastExpression->isValueDependent() &&
155  UnSignedCastExpression->getSubExpr()->EvaluateAsInt(EVResult,
156  *Result.Context)) {
157  llvm::APSInt Value = EVResult.Val.getInt();
158  if (Value <= UnsignedASCIIUpperBound)
159  return;
160  }
161 
162  diag(Comparison->getBeginLoc(),
163  "comparison between 'signed char' and 'unsigned char'");
164  } else if (Result.Nodes.getNodeAs<Expr>("arraySubscript")) {
165  diag(SignedCastExpression->getBeginLoc(),
166  "'signed char' to %0 conversion in array subscript; "
167  "consider casting to 'unsigned char' first.")
168  << *IntegerType;
169  } else {
170  diag(SignedCastExpression->getBeginLoc(),
171  "'signed char' to %0 conversion; "
172  "consider casting to 'unsigned char' first.")
173  << *IntegerType;
174  }
175 }
176 
177 } // namespace bugprone
178 } // namespace tidy
179 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::bugprone::SignedCharMisuseCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SignedCharMisuseCheck.cpp:36
clang::tidy::bugprone::hasAnyListedName
static Matcher< TypedefDecl > hasAnyListedName(const std::string &Names)
Definition: SignedCharMisuseCheck.cpp:23
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::bugprone::SignedCharMisuseCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SignedCharMisuseCheck.cpp:77
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
SignedCharMisuseCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::tidy::bugprone::SignedCharMisuseCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SignedCharMisuseCheck.cpp:130
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::UnsignedASCIIUpperBound
static constexpr int UnsignedASCIIUpperBound
Definition: SignedCharMisuseCheck.cpp:21
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120