blob: 415babe9890d6088f0f8c3134e3aee3b441a80f1 [file] [log] [blame]
Jonas Totha8c34532017-08-30 13:32:05 +00001//===--- SignedBitwiseCheck.cpp - clang-tidy-------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jonas Totha8c34532017-08-30 13:32:05 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SignedBitwiseCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14using namespace clang::ast_matchers::internal;
15
16namespace clang {
17namespace tidy {
18namespace hicpp {
19
Vladimir Plyashkun4de6b152019-10-30 14:07:49 -040020SignedBitwiseCheck::SignedBitwiseCheck(StringRef Name,
21 ClangTidyContext *Context)
22 : ClangTidyCheck(Name, Context),
23 IgnorePositiveIntegerLiterals(
24 Options.get("IgnorePositiveIntegerLiterals", false)) {}
25
26void SignedBitwiseCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
27 Options.store(Opts, "IgnorePositiveIntegerLiterals",
28 IgnorePositiveIntegerLiterals);
29}
30
Jonas Totha8c34532017-08-30 13:32:05 +000031void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
32 const auto SignedIntegerOperand =
Vladimir Plyashkun4de6b152019-10-30 14:07:49 -040033 (IgnorePositiveIntegerLiterals
34 ? expr(ignoringImpCasts(hasType(isSignedInteger())),
35 unless(integerLiteral()))
36 : expr(ignoringImpCasts(hasType(isSignedInteger()))))
37 .bind("signed-operand");
Jonas Toth8ba28c72017-10-27 14:44:08 +000038
39 // The standard [bitmask.types] allows some integral types to be implemented
40 // as signed types. Exclude these types from diagnosing for bitwise or(|) and
41 // bitwise and(&). Shifting and complementing such values is still not
42 // allowed.
Alexander Kornienko976e0c02018-11-25 02:41:01 +000043 const auto BitmaskType = namedDecl(
44 hasAnyName("::std::locale::category", "::std::ctype_base::mask",
45 "::std::ios_base::fmtflags", "::std::ios_base::iostate",
46 "::std::ios_base::openmode"));
Jonas Toth8ba28c72017-10-27 14:44:08 +000047 const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
Jonas Totha8c34532017-08-30 13:32:05 +000048
49 // Match binary bitwise operations on signed integer arguments.
50 Finder->addMatcher(
Nathan James97572fa2020-03-10 00:42:21 +000051 binaryOperator(hasAnyOperatorName("^", "|", "&", "^=", "|=", "&="),
Jonas Toth8ba28c72017-10-27 14:44:08 +000052
Alexander Kornienko976e0c02018-11-25 02:41:01 +000053 unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
Jonas Toth8ba28c72017-10-27 14:44:08 +000054
Alexander Kornienko976e0c02018-11-25 02:41:01 +000055 hasEitherOperand(SignedIntegerOperand),
56 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
Jonas Toth8ba28c72017-10-27 14:44:08 +000057 .bind("binary-no-sign-interference"),
58 this);
59
60 // Shifting and complement is not allowed for any signed integer type because
61 // the sign bit may corrupt the result.
62 Finder->addMatcher(
Nathan James97572fa2020-03-10 00:42:21 +000063 binaryOperator(hasAnyOperatorName("<<", ">>", "<<=", ">>="),
Alexander Kornienko976e0c02018-11-25 02:41:01 +000064 hasEitherOperand(SignedIntegerOperand),
65 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
Jonas Toth8ba28c72017-10-27 14:44:08 +000066 .bind("binary-sign-interference"),
Jonas Totha8c34532017-08-30 13:32:05 +000067 this);
68
69 // Match unary operations on signed integer types.
Alexander Kornienko976e0c02018-11-25 02:41:01 +000070 Finder->addMatcher(
71 unaryOperator(hasOperatorName("~"), hasUnaryOperand(SignedIntegerOperand))
72 .bind("unary-signed"),
73 this);
Jonas Totha8c34532017-08-30 13:32:05 +000074}
75
76void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
77 const ast_matchers::BoundNodes &N = Result.Nodes;
Jonas Toth8ba28c72017-10-27 14:44:08 +000078 const auto *SignedOperand = N.getNodeAs<Expr>("signed-operand");
79 assert(SignedOperand &&
80 "No signed operand found in problematic bitwise operations");
Jonas Totha8c34532017-08-30 13:32:05 +000081
Jonas Toth8ba28c72017-10-27 14:44:08 +000082 bool IsUnary = false;
83 SourceLocation Location;
84
85 if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
86 IsUnary = true;
Stephen Kelly43465bf2018-08-09 22:42:26 +000087 Location = UnaryOp->getBeginLoc();
Jonas Toth8ba28c72017-10-27 14:44:08 +000088 } else {
89 if (const auto *BinaryOp =
90 N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
Stephen Kelly43465bf2018-08-09 22:42:26 +000091 Location = BinaryOp->getBeginLoc();
Jonas Toth8ba28c72017-10-27 14:44:08 +000092 else if (const auto *BinaryOp =
93 N.getNodeAs<BinaryOperator>("binary-sign-interference"))
Stephen Kelly43465bf2018-08-09 22:42:26 +000094 Location = BinaryOp->getBeginLoc();
Jonas Toth8ba28c72017-10-27 14:44:08 +000095 else
96 llvm_unreachable("unexpected matcher result");
97 }
Jonas Toth0f5f41d2018-04-11 09:53:08 +000098 diag(Location, "use of a signed integer operand with a "
99 "%select{binary|unary}0 bitwise operator")
Jonas Totha8c34532017-08-30 13:32:05 +0000100 << IsUnary << SignedOperand->getSourceRange();
101}
102
103} // namespace hicpp
104} // namespace tidy
105} // namespace clang