[clang-tools-extra] r179103 - Fix UseNullptr fails to replace c-style explicit cast in a return statement
Ariel J. Bernal
ariel.j.bernal at intel.com
Tue Apr 9 09:54:56 PDT 2013
Author: ajbernal
Date: Tue Apr 9 11:54:56 2013
New Revision: 179103
URL: http://llvm.org/viewvc/llvm-project?rev=179103&view=rev
Log:
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
Modified:
clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp
clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp
Modified: clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp?rev=179103&r1=179102&r2=179103&view=diff
==============================================================================
--- clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp (original)
+++ clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrActions.cpp Tue Apr 9 11:54:56 2013
@@ -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::Replace
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 ||
Modified: clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp?rev=179103&r1=179102&r2=179103&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp (original)
+++ clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp Tue Apr 9 11:54:56 2013
@@ -180,6 +180,18 @@ int test_function_return6() {
// CHECK: return g_null;
}
+int *test_function_return_cast1() {
+ return(int)0;
+ // CHECK: return nullptr;
+}
+
+int *test_function_return_cast2() {
+ #define RET return
+ RET(int)0;
+ // CHECK: RET nullptr;
+ #undef RET
+}
+
// Test parentheses expressions resulting in a nullptr.
int *test_parentheses_expression1() {
return(0);
More information about the cfe-commits
mailing list