r261008 - Add a nullPointerConstant() AST matcher to handle variations of null pointer constants in one matcher.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 22 10:13:27 PDT 2019
On Thu, Aug 22, 2019 at 12:58 PM Richard Smith <richard at metafoo.co.uk> wrote:
>
> (Sorry for the very late review!)
>
> On Tue, 16 Feb 2016 at 13:06, Aaron Ballman via cfe-commits <cfe-commits at lists.llvm.org> wrote:
>>
>> Author: aaronballman
>> Date: Tue Feb 16 15:02:23 2016
>> New Revision: 261008
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=261008&view=rev
>> Log:
>> Add a nullPointerConstant() AST matcher to handle variations of null pointer constants in one matcher.
>>
>> Modified:
>> cfe/trunk/docs/LibASTMatchersReference.html
>> cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
>> cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
>> cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
>>
>> Modified: cfe/trunk/docs/LibASTMatchersReference.html
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LibASTMatchersReference.html?rev=261008&r1=261007&r2=261008&view=diff
>> ==============================================================================
>> --- cfe/trunk/docs/LibASTMatchersReference.html (original)
>> +++ cfe/trunk/docs/LibASTMatchersReference.html Tue Feb 16 15:02:23 2016
>> @@ -2176,6 +2176,23 @@ fieldDecl(isPublic())
>> </pre></td></tr>
>>
>>
>> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>></td><td class="name" onclick="toggle('nullPointerConstant0')"><a name="nullPointerConstant0Anchor">nullPointerConstant</a></td><td></td></tr>
>> +<tr><td colspan="4" class="doc" id="nullPointerConstant0"><pre>Matches expressions that resolve to a null pointer constant, such as
>> +GNU's __null, C++11's nullptr, or C's NULL macro.
>> +
>> +Given:
>> + void *v1 = NULL;
>> + void *v2 = nullptr;
>> + void *v3 = __null; GNU extension
>> + char *cp = (char *)0;
>> + int *ip = 0;
>> + int i = 0;
>> +expr(nullPointerConstant())
>> + matches the initializer for v1, v2, v3, cp, and ip. Does not match the
>> + initializer for i.
>> +</pre></td></tr>
>> +
>> +
>> <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>></td><td class="name" onclick="toggle('equals1')"><a name="equals1Anchor">equals</a></td><td>ValueT Value</td></tr>
>> <tr><td colspan="4" class="doc" id="equals1"><pre>Matches literals that are equal to the given value.
>>
>>
>> Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=261008&r1=261007&r2=261008&view=diff
>> ==============================================================================
>> --- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original)
>> +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Tue Feb 16 15:02:23 2016
>> @@ -4816,6 +4816,27 @@ const internal::VariadicDynCastAllOfMatc
>> Stmt,
>> CUDAKernelCallExpr> cudaKernelCallExpr;
>>
>> +
>> +/// \brief Matches expressions that resolve to a null pointer constant, such as
>> +/// GNU's __null, C++11's nullptr, or C's NULL macro.
>> +///
>> +/// Given:
>> +/// \code
>> +/// void *v1 = NULL;
>> +/// void *v2 = nullptr;
>> +/// void *v3 = __null; // GNU extension
>> +/// char *cp = (char *)0;
>> +/// int *ip = 0;
>> +/// int i = 0;
>> +/// \endcode
>> +/// expr(nullPointerConstant())
>> +/// matches the initializer for v1, v2, v3, cp, and ip. Does not match the
>> +/// initializer for i.
>> +AST_MATCHER_FUNCTION(internal::Matcher<Expr>, nullPointerConstant) {
>> + return anyOf(
>> + gnuNullExpr(), cxxNullPtrLiteralExpr(),
>> + integerLiteral(equals(0), hasParent(expr(hasType(pointerType())))));
>
>
> Is there a reason this logic was reinvented rather than using Expr::isNullPointerConstant()? This is not correct in C or in Clang's interpretation of DRs against C++98.
No reason that I can recall, I'll make the switch. Thank you for the feedback!
~Aaron
>
>>
>> +}
>> } // end namespace ast_matchers
>> } // end namespace clang
>>
>>
>> Modified: cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp?rev=261008&r1=261007&r2=261008&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp (original)
>> +++ cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp Tue Feb 16 15:02:23 2016
>> @@ -326,6 +326,7 @@ RegistryMaps::RegistryMaps() {
>> REGISTER_MATCHER(namesType);
>> REGISTER_MATCHER(nestedNameSpecifier);
>> REGISTER_MATCHER(nestedNameSpecifierLoc);
>> + REGISTER_MATCHER(nullPointerConstant);
>> REGISTER_MATCHER(nullStmt);
>> REGISTER_MATCHER(numSelectorArgs);
>> REGISTER_MATCHER(ofClass);
>>
>> Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=261008&r1=261007&r2=261008&view=diff
>> ==============================================================================
>> --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original)
>> +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Tue Feb 16 15:02:23 2016
>> @@ -5348,5 +5348,15 @@ TEST(ObjCMessageExprMatcher, SimpleExprs
>> )));
>> }
>>
>> +TEST(NullPointerConstants, Basic) {
>> + EXPECT_TRUE(matches("#define NULL ((void *)0)\n"
>> + "void *v1 = NULL;", expr(nullPointerConstant())));
>> + EXPECT_TRUE(matches("void *v2 = nullptr;", expr(nullPointerConstant())));
>> + EXPECT_TRUE(matches("void *v3 = __null;", expr(nullPointerConstant())));
>> + EXPECT_TRUE(matches("char *cp = (char *)0;", expr(nullPointerConstant())));
>> + EXPECT_TRUE(matches("int *ip = 0;", expr(nullPointerConstant())));
>> + EXPECT_TRUE(notMatches("int i = 0", expr(nullPointerConstant())));
>> +}
>> +
>> } // end namespace ast_matchers
>> } // end namespace clang
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list