aboutsummaryrefslogtreecommitdiff
path: root/clang-tidy/hicpp
diff options
context:
space:
mode:
authorJonas Toth <jonas.toth@gmail.com>2017-08-30 13:32:05 +0000
committerJonas Toth <jonas.toth@gmail.com>2017-08-30 13:32:05 +0000
commit4255b775f2d5fbc661a633e9e78b296dfbd5ee7f (patch)
tree2f17f24fc54ae8ed9c069f5b487b23236a6bf6b5 /clang-tidy/hicpp
parentb5672f2f79e882e4f2d81db42a2aaad8ddb0f0be (diff)
[clang-tidy] hicpp bitwise operations on signed integers
Summary: This check implements the rule [[ http://www.codingstandard.com/section/5-6-shift-operators/ | 5.6. HIC++ ]] that forbidds bitwise operations on signed integer types. Reviewers: aaron.ballman, hokein, alexfh, Eugene.Zelenko Reviewed By: aaron.ballman Subscribers: cfe-commits, mgorny, JDevlieghere, xazax.hun Differential Revision: https://reviews.llvm.org/D36586 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@312122 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/hicpp')
-rw-r--r--clang-tidy/hicpp/CMakeLists.txt1
-rw-r--r--clang-tidy/hicpp/HICPPTidyModule.cpp3
-rw-r--r--clang-tidy/hicpp/SignedBitwiseCheck.cpp56
-rw-r--r--clang-tidy/hicpp/SignedBitwiseCheck.h36
4 files changed, 96 insertions, 0 deletions
diff --git a/clang-tidy/hicpp/CMakeLists.txt b/clang-tidy/hicpp/CMakeLists.txt
index e5e70955..fd15d485 100644
--- a/clang-tidy/hicpp/CMakeLists.txt
+++ b/clang-tidy/hicpp/CMakeLists.txt
@@ -4,6 +4,7 @@ add_clang_library(clangTidyHICPPModule
ExceptionBaseclassCheck.cpp
NoAssemblerCheck.cpp
HICPPTidyModule.cpp
+ SignedBitwiseCheck.cpp
LINK_LIBS
clangAST
diff --git a/clang-tidy/hicpp/HICPPTidyModule.cpp b/clang-tidy/hicpp/HICPPTidyModule.cpp
index 321d8791..f7b70d2b 100644
--- a/clang-tidy/hicpp/HICPPTidyModule.cpp
+++ b/clang-tidy/hicpp/HICPPTidyModule.cpp
@@ -26,6 +26,7 @@
#include "../readability/IdentifierNamingCheck.h"
#include "ExceptionBaseclassCheck.h"
#include "NoAssemblerCheck.h"
+#include "SignedBitwiseCheck.h"
namespace clang {
namespace tidy {
@@ -38,6 +39,8 @@ public:
"hicpp-braces-around-statements");
CheckFactories.registerCheck<ExceptionBaseclassCheck>(
"hicpp-exception-baseclass");
+ CheckFactories.registerCheck<SignedBitwiseCheck>(
+ "hicpp-signed-bitwise");
CheckFactories.registerCheck<google::ExplicitConstructorCheck>(
"hicpp-explicit-conversions");
CheckFactories.registerCheck<readability::FunctionSizeCheck>(
diff --git a/clang-tidy/hicpp/SignedBitwiseCheck.cpp b/clang-tidy/hicpp/SignedBitwiseCheck.cpp
new file mode 100644
index 00000000..acd7216a
--- /dev/null
+++ b/clang-tidy/hicpp/SignedBitwiseCheck.cpp
@@ -0,0 +1,56 @@
+//===--- 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)))
+ .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
diff --git a/clang-tidy/hicpp/SignedBitwiseCheck.h b/clang-tidy/hicpp/SignedBitwiseCheck.h
new file mode 100644
index 00000000..24338ad9
--- /dev/null
+++ b/clang-tidy/hicpp/SignedBitwiseCheck.h
@@ -0,0 +1,36 @@
+//===--- SignedBitwiseCheck.h - clang-tidy-----------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNED_BITWISE_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNED_BITWISE_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace hicpp {
+
+/// This check implements the rule 5.6.1 of the HICPP Standard, which disallows
+/// bitwise operations on signed integer types.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/hicpp-signed-bitwise.html
+class SignedBitwiseCheck : public ClangTidyCheck {
+public:
+ SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace hicpp
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNED_BITWISE_H