summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/readability
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability')
-rw-r--r--clang-tools-extra/clang-tidy/readability/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.cpp57
-rw-r--r--clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.h36
-rw-r--r--clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp3
4 files changed, 97 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 1e80f940510..c2ec45c1068 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -10,6 +10,7 @@ add_clang_library(clangTidyReadabilityModule
IdentifierNamingCheck.cpp
ImplicitBoolCastCheck.cpp
InconsistentDeclarationParameterNameCheck.cpp
+ MisplacedArrayIndexCheck.cpp
NamedParameterCheck.cpp
NamespaceCommentCheck.cpp
NonConstParameterCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.cpp
new file mode 100644
index 00000000000..f5e09fabb5e
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.cpp
@@ -0,0 +1,57 @@
+//===--- MisplacedArrayIndexCheck.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 "MisplacedArrayIndexCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Tooling/FixIt.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+void MisplacedArrayIndexCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(arraySubscriptExpr(hasLHS(hasType(isInteger())),
+ hasRHS(hasType(isAnyPointer())))
+ .bind("expr"),
+ this);
+}
+
+void MisplacedArrayIndexCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *ArraySubscriptE =
+ Result.Nodes.getNodeAs<ArraySubscriptExpr>("expr");
+
+ auto Diag = diag(ArraySubscriptE->getLocStart(), "confusing array subscript "
+ "expression, usually the "
+ "index is inside the []");
+
+ // Only try to fixit when LHS and RHS can be swapped directly without changing
+ // the logic.
+ const Expr *RHSE = ArraySubscriptE->getRHS()->IgnoreParenImpCasts();
+ if (!isa<StringLiteral>(RHSE) && !isa<DeclRefExpr>(RHSE) &&
+ !isa<MemberExpr>(RHSE))
+ return;
+
+ const StringRef LText = tooling::fixit::getText(
+ ArraySubscriptE->getLHS()->getSourceRange(), *Result.Context);
+ const StringRef RText = tooling::fixit::getText(
+ ArraySubscriptE->getRHS()->getSourceRange(), *Result.Context);
+
+ Diag << FixItHint::CreateReplacement(
+ ArraySubscriptE->getLHS()->getSourceRange(), RText);
+ Diag << FixItHint::CreateReplacement(
+ ArraySubscriptE->getRHS()->getSourceRange(), LText);
+}
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.h b/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.h
new file mode 100644
index 00000000000..e9a22314f6c
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.h
@@ -0,0 +1,36 @@
+//===--- MisplacedArrayIndexCheck.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_READABILITY_MISPLACED_ARRAY_INDEX_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MISPLACED_ARRAY_INDEX_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+/// Warn about unusual array index syntax (`index[array]` instead of
+/// `array[index]`).
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/readability-misplaced-array-index.html
+class MisplacedArrayIndexCheck : public ClangTidyCheck {
+public:
+ MisplacedArrayIndexCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MISPLACED_ARRAY_INDEX_H
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index b32bbacf22d..6aedba202a0 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -19,6 +19,7 @@
#include "IdentifierNamingCheck.h"
#include "ImplicitBoolCastCheck.h"
#include "InconsistentDeclarationParameterNameCheck.h"
+#include "MisplacedArrayIndexCheck.h"
#include "NamedParameterCheck.h"
#include "NonConstParameterCheck.h"
#include "RedundantControlFlowCheck.h"
@@ -54,6 +55,8 @@ public:
"readability-implicit-bool-cast");
CheckFactories.registerCheck<InconsistentDeclarationParameterNameCheck>(
"readability-inconsistent-declaration-parameter-name");
+ CheckFactories.registerCheck<MisplacedArrayIndexCheck>(
+ "readability-misplaced-array-index");
CheckFactories.registerCheck<StaticDefinitionInAnonymousNamespaceCheck>(
"readability-static-definition-in-anonymous-namespace");
CheckFactories.registerCheck<readability::NamedParameterCheck>(
OpenPOWER on IntegriCloud