diff options
| author | Ariel J. Bernal <ariel.j.bernal@intel.com> | 2013-04-09 16:54:56 +0000 |
|---|---|---|
| committer | Ariel J. Bernal <ariel.j.bernal@intel.com> | 2013-04-09 16:54:56 +0000 |
| commit | 464957e2d6ee20ab3f836bfc7d08a75e992cdf08 (patch) | |
| tree | fcbef3b9d5c6f473173425a5892906b9eaafdcea /clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp | |
| parent | 1cc814a8e6f4595e6ccfb1c102db2054e98e4311 (diff) | |
| download | bcm5719-llvm-464957e2d6ee20ab3f836bfc7d08a75e992cdf08.tar.gz bcm5719-llvm-464957e2d6ee20ab3f836bfc7d08a75e992cdf08.zip | |
Fix UseNullptr fails to replace c-style explicit cast in a return statement
This happens whenever there is a c-style explicit cast to nullptr not
surrounded by parentheses following a return statement.
- Added a white space before nullptr if the character before is alphanumeric
when replacing the null pointer expression.
- Simplified visitor
- Addes tests
llvm-svn: 179103
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp')
| -rw-r--r-- | clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp index 53c6341b31c..f41b1463110 100644 --- a/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp +++ b/clang-tools-extra/cpp11-migrate/UseNullptr/NullptrActions.cpp @@ -20,6 +20,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Basic/CharInfo.h" #include "clang/Lex/Lexer.h" using namespace clang::ast_matchers; @@ -42,7 +43,14 @@ bool ReplaceWithNullptr(tooling::Replacements &Replace, SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLoc) { if (SM.isFromSameFile(StartLoc, EndLoc) && SM.isFromMainFile(StartLoc)) { CharSourceRange Range(SourceRange(StartLoc, EndLoc), true); - Replace.insert(tooling::Replacement(SM, Range, "nullptr")); + // Add a space if nullptr follows an alphanumeric character. This happens + // whenever there is an c-style explicit cast to nullptr not surrounded by + // parentheses and right beside a return statement. + SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1); + if (isAlphanumeric(*FullSourceLoc(PreviousLocation, SM).getCharacterData())) + Replace.insert(tooling::Replacement(SM, Range, " nullptr")); + else + Replace.insert(tooling::Replacement(SM, Range, "nullptr")); return true; } else return false; @@ -93,19 +101,11 @@ public: // within a cast expression. bool VisitStmt(Stmt *S) { CastExpr *C = dyn_cast<CastExpr>(S); - if (!C) { ResetFirstSubExpr(); return true; } else if (!FirstSubExpr) { - // Keep parentheses for implicit casts to avoid cases where an implicit - // cast within a parentheses expression is right next to a return - // statement otherwise get the subexpression of the outermost explicit - // cast. - if (C->getStmtClass() == Stmt::ImplicitCastExprClass) - FirstSubExpr = C->IgnoreParenImpCasts(); - else - FirstSubExpr = C->getSubExpr(); + FirstSubExpr = C->getSubExpr()->IgnoreParens(); } if (C->getCastKind() == CK_NullToPointer || |

