clang-tools  7.0.0
SignedBitwiseCheck.cpp
Go to the documentation of this file.
1 //===--- SignedBitwiseCheck.cpp - clang-tidy-------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "SignedBitwiseCheck.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 hicpp {
20 
21 void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
22  const auto SignedIntegerOperand =
23  expr(ignoringImpCasts(hasType(isSignedInteger()))).bind("signed-operand");
24 
25  // The standard [bitmask.types] allows some integral types to be implemented
26  // as signed types. Exclude these types from diagnosing for bitwise or(|) and
27  // bitwise and(&). Shifting and complementing such values is still not
28  // allowed.
29  const auto BitmaskType = namedDecl(anyOf(
30  hasName("::std::locale::category"), hasName("::std::ctype_base::mask"),
31  hasName("::std::ios_base::fmtflags"), hasName("::std::ios_base::iostate"),
32  hasName("::std::ios_base::openmode")));
33  const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
34 
35  // Match binary bitwise operations on signed integer arguments.
36  Finder->addMatcher(
37  binaryOperator(
38  allOf(anyOf(hasOperatorName("^"), hasOperatorName("|"),
39  hasOperatorName("&"), hasOperatorName("^="),
40  hasOperatorName("|="), hasOperatorName("&=")),
41 
42  unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
43 
44  hasEitherOperand(SignedIntegerOperand),
45  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger()))))
46  .bind("binary-no-sign-interference"),
47  this);
48 
49  // Shifting and complement is not allowed for any signed integer type because
50  // the sign bit may corrupt the result.
51  Finder->addMatcher(
52  binaryOperator(
53  allOf(anyOf(hasOperatorName("<<"), hasOperatorName(">>"),
54  hasOperatorName("<<="), hasOperatorName(">>=")),
55  hasEitherOperand(SignedIntegerOperand),
56  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger()))))
57  .bind("binary-sign-interference"),
58  this);
59 
60  // Match unary operations on signed integer types.
61  Finder->addMatcher(unaryOperator(allOf(hasOperatorName("~"),
62  hasUnaryOperand(SignedIntegerOperand)))
63  .bind("unary-signed"),
64  this);
65 }
66 
67 void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
68  const ast_matchers::BoundNodes &N = Result.Nodes;
69  const auto *SignedOperand = N.getNodeAs<Expr>("signed-operand");
70  assert(SignedOperand &&
71  "No signed operand found in problematic bitwise operations");
72 
73  bool IsUnary = false;
74  SourceLocation Location;
75 
76  if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
77  IsUnary = true;
78  Location = UnaryOp->getLocStart();
79  } else {
80  if (const auto *BinaryOp =
81  N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
82  Location = BinaryOp->getLocStart();
83  else if (const auto *BinaryOp =
84  N.getNodeAs<BinaryOperator>("binary-sign-interference"))
85  Location = BinaryOp->getLocStart();
86  else
87  llvm_unreachable("unexpected matcher result");
88  }
89  diag(Location, "use of a signed integer operand with a "
90  "%select{binary|unary}0 bitwise operator")
91  << IsUnary << SignedOperand->getSourceRange();
92 }
93 
94 } // namespace hicpp
95 } // namespace tidy
96 } // namespace clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//