clang-tools  11.0.0
SignedBitwiseCheck.cpp
Go to the documentation of this file.
1 //===--- SignedBitwiseCheck.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 "SignedBitwiseCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 using namespace clang::ast_matchers::internal;
15 
16 namespace clang {
17 namespace tidy {
18 namespace hicpp {
19 
20 SignedBitwiseCheck::SignedBitwiseCheck(StringRef Name,
21  ClangTidyContext *Context)
22  : ClangTidyCheck(Name, Context),
23  IgnorePositiveIntegerLiterals(
24  Options.get("IgnorePositiveIntegerLiterals", false)) {}
25 
27  Options.store(Opts, "IgnorePositiveIntegerLiterals",
28  IgnorePositiveIntegerLiterals);
29 }
30 
31 void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
32  const auto SignedIntegerOperand =
33  (IgnorePositiveIntegerLiterals
34  ? expr(ignoringImpCasts(hasType(isSignedInteger())),
35  unless(integerLiteral()))
36  : expr(ignoringImpCasts(hasType(isSignedInteger()))))
37  .bind("signed-operand");
38 
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.
43  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"));
47  const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
48 
49  // Match binary bitwise operations on signed integer arguments.
50  Finder->addMatcher(
51  binaryOperator(hasAnyOperatorName("^", "|", "&", "^=", "|=", "&="),
52 
53  unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
54 
55  hasEitherOperand(SignedIntegerOperand),
56  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
57  .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(
63  binaryOperator(hasAnyOperatorName("<<", ">>", "<<=", ">>="),
64  hasEitherOperand(SignedIntegerOperand),
65  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
66  .bind("binary-sign-interference"),
67  this);
68 
69  // Match unary operations on signed integer types.
70  Finder->addMatcher(
71  unaryOperator(hasOperatorName("~"), hasUnaryOperand(SignedIntegerOperand))
72  .bind("unary-signed"),
73  this);
74 }
75 
76 void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
77  const ast_matchers::BoundNodes &N = Result.Nodes;
78  const auto *SignedOperand = N.getNodeAs<Expr>("signed-operand");
79  assert(SignedOperand &&
80  "No signed operand found in problematic bitwise operations");
81 
82  bool IsUnary = false;
83  SourceLocation Location;
84 
85  if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
86  IsUnary = true;
87  Location = UnaryOp->getBeginLoc();
88  } else {
89  if (const auto *BinaryOp =
90  N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
91  Location = BinaryOp->getBeginLoc();
92  else if (const auto *BinaryOp =
93  N.getNodeAs<BinaryOperator>("binary-sign-interference"))
94  Location = BinaryOp->getBeginLoc();
95  else
96  llvm_unreachable("unexpected matcher result");
97  }
98  diag(Location, "use of a signed integer operand with a "
99  "%select{binary|unary}0 bitwise operator")
100  << IsUnary << SignedOperand->getSourceRange();
101 }
102 
103 } // namespace hicpp
104 } // namespace tidy
105 } // namespace clang
clang::tidy::hicpp::SignedBitwiseCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SignedBitwiseCheck.cpp:31
Location
Definition: Modularize.cpp:383
clang::tidy::hicpp::SignedBitwiseCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SignedBitwiseCheck.cpp:76
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
SignedBitwiseCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::hicpp::SignedBitwiseCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SignedBitwiseCheck.cpp:26
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
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:55
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
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:152
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111