There are several ways to specify a null pointer constant. C++11 has nullptr, GNU has the __null extension, and there's always NULL/0. This AST matcher will match on anything that generates a null pointer value, without requiring the user to figure out how to spell that with existing matchers.
Diff Detail
Event Timeline
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4821 | Use AST_MATCHER_FUNCTION instead, where the return value is the matcher (instead of the application of the matcher). | |
4821 | Maybe use Expr::isNullPointerConstant? |
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4821 | One or the other. |
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4821 | One interesting problem I ran into with using Expr::isNullPointerConstant is that it makes the matcher overly aggressive with nullptr. Given: AST_MATCHER(Expr, nullPointerConstant) { QualType QT = Node.getType(); if (QT->isNullPtrType()) return true; return QT->isPointerType() && Node.isNullPointerConstant(Finder->getASTContext(), Expr::NPC_NeverValueDependent); } If you attempt to match somePtr = nullptr; with expr(nullPointerConstant()), you will get two matches. One for the initializer expression of the declaration, and one for the implicit cast expression. However, with somePtr = __null or somePtr = 0, you only get a single match (for the initializer expression). This seems like surprising behavior, but I'm not quite certain of the best way to address it, if it should be addressed at all. |
Using the AST_MATCHER_FUNCTION macro instead.
Using Expr::isNullPointerConstant() turns out to have strange behavior with implicit casts and nullptr, and also requires further restrictions (for instance, it claims the initializer for int i = 0 is a null pointer constant, which is strictly true, but practically useless).
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4838 | is this expr() necessary? |
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4838 | Yes, without it I get errors about not having a matching overload for hasParent(). |
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4838 | Sorry, I meant the one around integerLiteral(). |
include/clang/ASTMatchers/ASTMatchers.h | ||
---|---|---|
4838 | Ah, good catch! No, that one was not required, I've removed it. |
Use AST_MATCHER_FUNCTION instead, where the return value is the matcher (instead of the application of the matcher).
It is simpler to write and since it has no arguments it will memoize the matcher and construct it only once.