blob: 08aed6571b8c1f6cab8f4faafd34c9eba4af5771 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
//===--- SignedBitwiseCheck.cpp - clang-tidy-------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "SignedBitwiseCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
using namespace clang::ast_matchers::internal;
namespace clang {
namespace tidy {
namespace hicpp {
void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
const auto SignedIntegerOperand =
expr(ignoringImpCasts(hasType(isSignedInteger()))).bind("signed_operand");
// Match binary bitwise operations on signed integer arguments.
Finder->addMatcher(
binaryOperator(allOf(anyOf(hasOperatorName("|"), hasOperatorName("&"),
hasOperatorName("^"), hasOperatorName("<<"),
hasOperatorName(">>")),
hasEitherOperand(SignedIntegerOperand),
hasLHS(hasType(isInteger())),
hasRHS(hasType(isInteger()))))
.bind("binary_signed"),
this);
// Match unary operations on signed integer types.
Finder->addMatcher(unaryOperator(allOf(hasOperatorName("~"),
hasUnaryOperand(SignedIntegerOperand)))
.bind("unary_signed"),
this);
}
void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
const ast_matchers::BoundNodes &N = Result.Nodes;
const auto *SignedBinary = N.getNodeAs<BinaryOperator>("binary_signed");
const auto *SignedUnary = N.getNodeAs<UnaryOperator>("unary_signed");
const auto *SignedOperand = N.getNodeAs<Expr>("signed_operand");
const bool IsUnary = SignedUnary != nullptr;
diag(IsUnary ? SignedUnary->getLocStart() : SignedBinary->getLocStart(),
"use of a signed integer operand with a %select{binary|unary}0 bitwise "
"operator")
<< IsUnary << SignedOperand->getSourceRange();
}
} // namespace hicpp
} // namespace tidy
} // namespace clang
|