Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "nullability_verification/pointer_nullability_matchers.h" |
| 6 | |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 7 | #include "clang/AST/OperationKinds.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 8 | #include "clang/ASTMatchers/ASTMatchers.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 9 | |
| 10 | namespace clang { |
| 11 | namespace tidy { |
| 12 | namespace nullability { |
| 13 | |
Wei Yi Tee | f02698e | 2022-06-10 04:25:48 -0700 | [diff] [blame] | 14 | using ast_matchers::anyOf; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 15 | using ast_matchers::binaryOperator; |
Wei Yi Tee | c1e1d86 | 2022-08-19 14:11:28 -0700 | [diff] [blame] | 16 | using ast_matchers::callExpr; |
Wei Yi Tee | afbca01 | 2022-09-19 11:54:28 -0700 | [diff] [blame] | 17 | using ast_matchers::cxxConstructExpr; |
Wei Yi Tee | 566e2df | 2022-09-19 11:59:03 -0700 | [diff] [blame] | 18 | using ast_matchers::cxxCtorInitializer; |
Wei Yi Tee | cebc226 | 2022-06-10 04:27:57 -0700 | [diff] [blame] | 19 | using ast_matchers::cxxThisExpr; |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 20 | using ast_matchers::declRefExpr; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 21 | using ast_matchers::expr; |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 22 | using ast_matchers::hasAnyOperatorName; |
Wei Yi Tee | 264ee4d | 2022-06-10 04:20:10 -0700 | [diff] [blame] | 23 | using ast_matchers::hasCastKind; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 24 | using ast_matchers::hasOperands; |
| 25 | using ast_matchers::hasOperatorName; |
Wei Yi Tee | ed480d6 | 2022-08-26 03:19:54 -0700 | [diff] [blame] | 26 | using ast_matchers::hasReturnValue; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 27 | using ast_matchers::hasType; |
| 28 | using ast_matchers::hasUnaryOperand; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 29 | using ast_matchers::implicitCastExpr; |
| 30 | using ast_matchers::isAnyPointer; |
Wei Yi Tee | f02698e | 2022-06-10 04:25:48 -0700 | [diff] [blame] | 31 | using ast_matchers::isArrow; |
Wei Yi Tee | 566e2df | 2022-09-19 11:59:03 -0700 | [diff] [blame] | 32 | using ast_matchers::isMemberInitializer; |
Wei Yi Tee | e314ca6 | 2022-06-10 04:23:08 -0700 | [diff] [blame] | 33 | using ast_matchers::memberExpr; |
Wei Yi Tee | ed480d6 | 2022-08-26 03:19:54 -0700 | [diff] [blame] | 34 | using ast_matchers::returnStmt; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 35 | using ast_matchers::unaryOperator; |
| 36 | using ast_matchers::internal::Matcher; |
| 37 | |
| 38 | Matcher<Stmt> isPointerExpr() { return expr(hasType(isAnyPointer())); } |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 39 | Matcher<Stmt> isNullPointerLiteral() { |
Wei Yi Tee | 9f55817 | 2022-07-25 08:34:56 -0700 | [diff] [blame] | 40 | return implicitCastExpr(anyOf(hasCastKind(CK_NullToPointer), |
| 41 | hasCastKind(CK_NullToMemberPointer))); |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 42 | } |
| 43 | Matcher<Stmt> isAddrOf() { return unaryOperator(hasOperatorName("&")); } |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 44 | Matcher<Stmt> isPointerDereference() { |
| 45 | return unaryOperator(hasOperatorName("*"), hasUnaryOperand(isPointerExpr())); |
| 46 | } |
Wei Yi Tee | 1cd62af | 2022-06-09 00:56:46 -0700 | [diff] [blame] | 47 | Matcher<Stmt> isPointerCheckBinOp() { |
| 48 | return binaryOperator(hasAnyOperatorName("!=", "=="), |
| 49 | hasOperands(isPointerExpr(), isPointerExpr())); |
| 50 | } |
| 51 | Matcher<Stmt> isImplicitCastPointerToBool() { |
Wei Yi Tee | 264ee4d | 2022-06-10 04:20:10 -0700 | [diff] [blame] | 52 | return implicitCastExpr(hasCastKind(CK_PointerToBoolean)); |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 53 | } |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 54 | Matcher<Stmt> isMemberOfPointerType() { |
| 55 | return memberExpr(hasType(isAnyPointer())); |
Wei Yi Tee | e314ca6 | 2022-06-10 04:23:08 -0700 | [diff] [blame] | 56 | } |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 57 | Matcher<Stmt> isPointerArrow() { return memberExpr(isArrow()); } |
Wei Yi Tee | cebc226 | 2022-06-10 04:27:57 -0700 | [diff] [blame] | 58 | Matcher<Stmt> isCXXThisExpr() { return cxxThisExpr(); } |
Wei Yi Tee | c1e1d86 | 2022-08-19 14:11:28 -0700 | [diff] [blame] | 59 | Matcher<Stmt> isCallExpr() { return callExpr(); } |
Wei Yi Tee | ed480d6 | 2022-08-26 03:19:54 -0700 | [diff] [blame] | 60 | Matcher<Stmt> isPointerReturn() { |
| 61 | return returnStmt(hasReturnValue(hasType(isAnyPointer()))); |
| 62 | } |
Wei Yi Tee | afbca01 | 2022-09-19 11:54:28 -0700 | [diff] [blame] | 63 | Matcher<Stmt> isConstructExpr() { return cxxConstructExpr(); } |
Wei Yi Tee | 566e2df | 2022-09-19 11:59:03 -0700 | [diff] [blame] | 64 | Matcher<CXXCtorInitializer> isCtorMemberInitializer() { |
| 65 | return cxxCtorInitializer(isMemberInitializer()); |
| 66 | } |
Wei Yi Tee | c1e1d86 | 2022-08-19 14:11:28 -0700 | [diff] [blame] | 67 | |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 68 | } // namespace nullability |
| 69 | } // namespace tidy |
| 70 | } // namespace clang |