Open GKxxQAQ opened 5 months ago
@llvm/issue-subscribers-clang-frontend
Author: None (GKxxQAQ)
In this example, the integer literal
0
inp[0]
is matched byexpr(nullPointerConstant())
, because its parent expressionp[0]
does have a pointer type.
Yeah, this seems pretty wrong to me at least; I’m more familiar w/ C++ than C, but it would seem very strange to me candidly if the C standard really considered that 0
in p[0]
to be a null pointer constant. I’m also not too familiar w/ AST matchers, but we should probably be able to check for an enclosing (possibly implicit) cast expression instead?
Godbolt link: https://godbolt.org/z/jaz4jYohe
Current definition of
clang::ast_matchers::nullPointerConstant
(inclang/include/clang/ASTMatchers.h
) isThe third argument to
anyOf
here is weird: As the C standard (C99, C11, C17) says (6.3.2.3 p3)So, if
nullPointerConstant()
is supposed to match any null pointer constant based on this definition, why is it restricted withhasParent(expr(hasType(pointerType())))
? If it is only supposed to match null pointer constants that are really used as null pointers, it is still problematic:In this example, the integer literal
0
inp[0]
is matched byexpr(nullPointerConstant())
, because its parent expressionp[0]
does have a pointer type.