clang-tools  8.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(
30  hasAnyName("::std::locale::category", "::std::ctype_base::mask",
31  "::std::ios_base::fmtflags", "::std::ios_base::iostate",
32  "::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(anyOf(hasOperatorName("^"), hasOperatorName("|"),
38  hasOperatorName("&"), hasOperatorName("^="),
39  hasOperatorName("|="), hasOperatorName("&=")),
40 
41  unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
42 
43  hasEitherOperand(SignedIntegerOperand),
44  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
45  .bind("binary-no-sign-interference"),
46  this);
47 
48  // Shifting and complement is not allowed for any signed integer type because
49  // the sign bit may corrupt the result.
50  Finder->addMatcher(
51  binaryOperator(anyOf(hasOperatorName("<<"), hasOperatorName(">>"),
52  hasOperatorName("<<="), hasOperatorName(">>=")),
53  hasEitherOperand(SignedIntegerOperand),
54  hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
55  .bind("binary-sign-interference"),
56  this);
57 
58  // Match unary operations on signed integer types.
59  Finder->addMatcher(
60  unaryOperator(hasOperatorName("~"), hasUnaryOperand(SignedIntegerOperand))
61  .bind("unary-signed"),
62  this);
63 }
64 
65 void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
66  const ast_matchers::BoundNodes &N = Result.Nodes;
67  const auto *SignedOperand = N.getNodeAs<Expr>("signed-operand");
68  assert(SignedOperand &&
69  "No signed operand found in problematic bitwise operations");
70 
71  bool IsUnary = false;
72  SourceLocation Location;
73 
74  if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
75  IsUnary = true;
76  Location = UnaryOp->getBeginLoc();
77  } else {
78  if (const auto *BinaryOp =
79  N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
80  Location = BinaryOp->getBeginLoc();
81  else if (const auto *BinaryOp =
82  N.getNodeAs<BinaryOperator>("binary-sign-interference"))
83  Location = BinaryOp->getBeginLoc();
84  else
85  llvm_unreachable("unexpected matcher result");
86  }
87  diag(Location, "use of a signed integer operand with a "
88  "%select{binary|unary}0 bitwise operator")
89  << IsUnary << SignedOperand->getSourceRange();
90 }
91 
92 } // namespace hicpp
93 } // namespace tidy
94 } // namespace clang
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//