You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-reorder-fields
clang-tidy
android
boost
bugprone
cert
cppcoreguidelines
fuchsia
google
hicpp
CMakeLists.txt
ExceptionBaseclassCheck.cpp
ExceptionBaseclassCheck.h
HICPPTidyModule.cpp
LICENSE.TXT
NoAssemblerCheck.cpp
NoAssemblerCheck.h
SignedBitwiseCheck.cpp
SignedBitwiseCheck.h
llvm
misc
modernize
mpi
objc
performance
plugin
readability
tool
utils
CMakeLists.txt
ClangTidy.cpp
ClangTidy.h
ClangTidyDiagnosticConsumer.cpp
ClangTidyDiagnosticConsumer.h
ClangTidyModule.cpp
ClangTidyModule.h
ClangTidyModuleRegistry.h
ClangTidyOptions.cpp
ClangTidyOptions.h
add_new_check.py
rename_check.py
clang-tidy-vs
clangd
docs
include-fixer
modularize
pp-trace
test
tool-template
unittests
.arcconfig
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
LICENSE.TXT
README.txt
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
97 lines
3.6 KiB
C++
97 lines
3.6 KiB
C++
![]() |
//===--- 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");
|
||
|
|
||
|
// The standard [bitmask.types] allows some integral types to be implemented
|
||
|
// as signed types. Exclude these types from diagnosing for bitwise or(|) and
|
||
|
// bitwise and(&). Shifting and complementing such values is still not
|
||
|
// allowed.
|
||
|
const auto BitmaskType = namedDecl(anyOf(
|
||
|
hasName("::std::locale::category"), hasName("::std::ctype_base::mask"),
|
||
|
hasName("::std::ios_base::fmtflags"), hasName("::std::ios_base::iostate"),
|
||
|
hasName("::std::ios_base::openmode")));
|
||
|
const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
|
||
|
|
||
|
// Match binary bitwise operations on signed integer arguments.
|
||
|
Finder->addMatcher(
|
||
|
binaryOperator(
|
||
|
allOf(anyOf(hasOperatorName("^"), hasOperatorName("|"),
|
||
|
hasOperatorName("&")),
|
||
|
|
||
|
unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
|
||
|
|
||
|
hasEitherOperand(SignedIntegerOperand),
|
||
|
hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger()))))
|
||
|
.bind("binary-no-sign-interference"),
|
||
|
this);
|
||
|
|
||
|
// Shifting and complement is not allowed for any signed integer type because
|
||
|
// the sign bit may corrupt the result.
|
||
|
Finder->addMatcher(
|
||
|
binaryOperator(allOf(anyOf(hasOperatorName("<<"), hasOperatorName(">>")),
|
||
|
hasEitherOperand(SignedIntegerOperand),
|
||
|
hasLHS(hasType(isInteger())),
|
||
|
hasRHS(hasType(isInteger()))))
|
||
|
.bind("binary-sign-interference"),
|
||
|
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 *SignedOperand = N.getNodeAs<Expr>("signed-operand");
|
||
|
assert(SignedOperand &&
|
||
|
"No signed operand found in problematic bitwise operations");
|
||
|
|
||
|
bool IsUnary = false;
|
||
|
SourceLocation Location;
|
||
|
|
||
|
if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
|
||
|
IsUnary = true;
|
||
|
Location = UnaryOp->getLocStart();
|
||
|
} else {
|
||
|
if (const auto *BinaryOp =
|
||
|
N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
|
||
|
Location = BinaryOp->getLocStart();
|
||
|
else if (const auto *BinaryOp =
|
||
|
N.getNodeAs<BinaryOperator>("binary-sign-interference"))
|
||
|
Location = BinaryOp->getLocStart();
|
||
|
else
|
||
|
llvm_unreachable("unexpected matcher result");
|
||
|
}
|
||
|
|
||
|
diag(Location,
|
||
|
"use of a signed integer operand with a %select{binary|unary}0 bitwise "
|
||
|
"operator")
|
||
|
<< IsUnary << SignedOperand->getSourceRange();
|
||
|
}
|
||
|
|
||
|
} // namespace hicpp
|
||
|
} // namespace tidy
|
||
|
} // namespace clang
|