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
9 #include "SignedCharMisuseCheck.h"
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
hasAnyListedName(const std::string & Names)23 static Matcher<TypedefDecl> hasAnyListedName(const std::string &Names) {
24 const std::vector<std::string> NameList =
25 utils::options::parseStringList(Names);
26 return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
27 }
28
SignedCharMisuseCheck(StringRef Name,ClangTidyContext * Context)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
storeOptions(ClangTidyOptions::OptionMap & Opts)36 void SignedCharMisuseCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
37 Options.store(Opts, "CharTypdefsToIgnore", CharTypdefsToIgnoreList);
38 Options.store(Opts, "DiagnoseSignedUnsignedCharComparisons",
39 DiagnoseSignedUnsignedCharComparisons);
40 }
41
42 // Create a matcher for char -> integer cast.
charCastExpression(bool IsSigned,const Matcher<clang::QualType> & IntegerType,const std::string & CastBindName) const43 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
registerMatchers(MatchFinder * Finder)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
check(const MatchFinder::MatchResult & Result)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
180