10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13#include "clang/Tooling/FixIt.h"
21 traverse(TK_AsIs, arraySubscriptExpr(hasLHS(hasType(isInteger())),
22 hasRHS(hasType(isAnyPointer())))
28 const auto *ArraySubscriptE =
29 Result.Nodes.getNodeAs<ArraySubscriptExpr>(
"expr");
31 auto Diag =
diag(ArraySubscriptE->getBeginLoc(),
"confusing array subscript "
32 "expression, usually the "
33 "index is inside the []");
37 const Expr *RHSE = ArraySubscriptE->getRHS()->IgnoreParenImpCasts();
38 if (!isa<StringLiteral>(RHSE) && !isa<DeclRefExpr>(RHSE) &&
39 !isa<MemberExpr>(RHSE))
42 const StringRef LText = tooling::fixit::getText(
43 ArraySubscriptE->getLHS()->getSourceRange(), *Result.Context);
44 const StringRef RText = tooling::fixit::getText(
45 ArraySubscriptE->getRHS()->getSourceRange(), *Result.Context);
47 Diag << FixItHint::CreateReplacement(
48 ArraySubscriptE->getLHS()->getSourceRange(), RText);
49 Diag << FixItHint::CreateReplacement(
50 ArraySubscriptE->getRHS()->getSourceRange(), LText);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.