diff options
author | Etienne Bergeron <etienneb@google.com> | 2016-05-11 17:32:12 +0000 |
---|---|---|
committer | Etienne Bergeron <etienneb@google.com> | 2016-05-11 17:32:12 +0000 |
commit | e646a833ab1990bf5de8587a0050644dad63ec3a (patch) | |
tree | c08bd69a9ef6ab19e0307ba9d8dd0f5576fede87 /clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp | |
parent | 23dcd6e0ab001efe6f48e821fde8363c1d3cd193 (diff) | |
download | bcm5719-llvm-e646a833ab1990bf5de8587a0050644dad63ec3a.tar.gz bcm5719-llvm-e646a833ab1990bf5de8587a0050644dad63ec3a.zip |
[clang-tidy] Add FixIt for swapping arguments in string-constructor-checker.
Summary:
Arguments can be swapped using fixit when they are not in macros.
This is the same implementation than SwappedArguments. Some code
got lifted to be reused.
Others checks are not safe to be fixed as they tend to be bugs or errors.
It is better to let the user manually review them.
Reviewers: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D19547
llvm-svn: 269208
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp index 0723ec1ed40..7ff488133f3 100644 --- a/clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/StringConstructorCheck.cpp @@ -10,6 +10,7 @@ #include "StringConstructorCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/FixIt.h" using namespace clang::ast_matchers; @@ -54,7 +55,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { isDefinition(), hasType(pointerType(pointee(isAnyCharacter(), isConstQualified()))), hasInitializer(ignoringParenImpCasts(BoundStringLiteral))); - auto ConstStrLiteral = expr(ignoringParenImpCasts(anyOf( + const auto ConstStrLiteral = expr(ignoringParenImpCasts(anyOf( BoundStringLiteral, declRefExpr(hasDeclaration(anyOf( ConstPtrStrLiteralDecl, ConstStrLiteralDecl)))))); @@ -88,7 +89,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("...", 0); hasArgument(1, ZeroExpr.bind("empty-string")), // Detect the expression: string("...", -4); - hasArgument(1, NegativeExpr.bind("negative-length")), + hasArgument(1, NegativeExpr.bind("negative-length")), // Detect the expression: string("lit", 0x1234567); hasArgument(1, LargeLengthExpr.bind("large-length")), // Detect the expression: string("lit", 5) @@ -100,11 +101,18 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { } void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { - const auto *E = Result.Nodes.getNodeAs<Expr>("constructor"); + const ASTContext &Ctx = *Result.Context; + const auto *E = Result.Nodes.getNodeAs<CXXConstructExpr>("constructor"); + assert(E && "missing constructor expression"); SourceLocation Loc = E->getLocStart(); if (Result.Nodes.getNodeAs<Expr>("swapped-parameter")) { - diag(Loc, "constructor parameters are probably swapped"); + const Expr *P0 = E->getArg(0); + const Expr *P1 = E->getArg(1); + diag(Loc, "string constructor parameters are probably swapped;" + " expecting string(count, character)") + << tooling::fixit::createReplacement(*P0, *P1, Ctx) + << tooling::fixit::createReplacement(*P1, *P0, Ctx); } else if (Result.Nodes.getNodeAs<Expr>("empty-string")) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs<Expr>("negative-length")) { |