Edwin Vane | aae3367 | 2013-07-08 12:17:37 +0000 | [diff] [blame] | 1 | //===-- LoopConvert/LoopMatchers.cpp - Matchers for for loops -------------===// |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains definitions of the matchers for use in migrating |
| 12 | /// C++ for loops. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
Edwin Vane | aae3367 | 2013-07-08 12:17:37 +0000 | [diff] [blame] | 15 | |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 16 | #include "LoopMatchers.h" |
| 17 | |
| 18 | using namespace clang::ast_matchers; |
| 19 | using namespace clang; |
| 20 | |
| 21 | const char LoopName[] = "forLoop"; |
| 22 | const char ConditionBoundName[] = "conditionBound"; |
| 23 | const char ConditionVarName[] = "conditionVar"; |
| 24 | const char IncrementVarName[] = "incrementVar"; |
| 25 | const char InitVarName[] = "initVar"; |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 26 | const char BeginCallName[] = "beginCall"; |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 27 | const char EndCallName[] = "endCall"; |
| 28 | const char ConditionEndVarName[] = "conditionEndVar"; |
| 29 | const char EndVarName[] = "endVar"; |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 30 | const char DerefByValueResultName[] = "derefByValueResult"; |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 31 | const char DerefByRefResultName[] = "derefByRefResult"; |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 32 | |
| 33 | // shared matchers |
| 34 | static const TypeMatcher AnyType = anything(); |
| 35 | |
| 36 | static const StatementMatcher IntegerComparisonMatcher = |
| 37 | expr(ignoringParenImpCasts(declRefExpr(to( |
| 38 | varDecl(hasType(isInteger())).bind(ConditionVarName))))); |
| 39 | |
| 40 | static const DeclarationMatcher InitToZeroMatcher = |
| 41 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 42 | integerLiteral(equals(0))))).bind(InitVarName); |
| 43 | |
| 44 | static const StatementMatcher IncrementVarMatcher = |
| 45 | declRefExpr(to( |
| 46 | varDecl(hasType(isInteger())).bind(IncrementVarName))); |
| 47 | |
| 48 | // FIXME: How best to document complicated matcher expressions? They're fairly |
| 49 | // self-documenting...but there may be some unintuitive parts. |
| 50 | |
| 51 | /// \brief The matcher for loops over arrays. |
| 52 | /// |
| 53 | /// In this general example, assuming 'j' and 'k' are of integral type: |
| 54 | /// \code |
| 55 | /// for (int i = 0; j < 3 + 2; ++k) { ... } |
| 56 | /// \endcode |
Alp Toker | 9a5134e | 2013-12-01 05:08:12 +0000 | [diff] [blame^] | 57 | /// The following string identifiers are bound to these parts of the AST: |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 58 | /// ConditionVarName: 'j' (as a VarDecl) |
| 59 | /// ConditionBoundName: '3 + 2' (as an Expr) |
| 60 | /// InitVarName: 'i' (as a VarDecl) |
| 61 | /// IncrementVarName: 'k' (as a VarDecl) |
| 62 | /// LoopName: The entire for loop (as a ForStmt) |
| 63 | /// |
| 64 | /// Client code will need to make sure that: |
| 65 | /// - The three index variables identified by the matcher are the same |
| 66 | /// VarDecl. |
| 67 | /// - The index variable is only used as an array index. |
| 68 | /// - All arrays indexed by the loop are the same. |
| 69 | StatementMatcher makeArrayLoopMatcher() { |
| 70 | StatementMatcher ArrayBoundMatcher = |
| 71 | expr(hasType(isInteger())).bind(ConditionBoundName); |
| 72 | |
| 73 | return forStmt( |
| 74 | hasLoopInit(declStmt(hasSingleDecl(InitToZeroMatcher))), |
| 75 | hasCondition(anyOf(binaryOperator(hasOperatorName("<"), |
| 76 | hasLHS(IntegerComparisonMatcher), |
| 77 | hasRHS(ArrayBoundMatcher)), |
| 78 | binaryOperator(hasOperatorName(">"), |
| 79 | hasLHS(ArrayBoundMatcher), |
| 80 | hasRHS(IntegerComparisonMatcher)))), |
| 81 | hasIncrement(unaryOperator(hasOperatorName("++"), |
| 82 | hasUnaryOperand(IncrementVarMatcher)))) |
| 83 | .bind(LoopName); |
| 84 | } |
| 85 | |
| 86 | /// \brief The matcher used for iterator-based for loops. |
| 87 | /// |
| 88 | /// This matcher is more flexible than array-based loops. It will match |
| 89 | /// catch loops of the following textual forms (regardless of whether the |
| 90 | /// iterator type is actually a pointer type or a class type): |
| 91 | /// |
| 92 | /// Assuming f, g, and h are of type containerType::iterator, |
| 93 | /// \code |
| 94 | /// for (containerType::iterator it = container.begin(), |
| 95 | /// e = createIterator(); f != g; ++h) { ... } |
| 96 | /// for (containerType::iterator it = container.begin(); |
| 97 | /// f != anotherContainer.end(); ++h) { ... } |
| 98 | /// \endcode |
| 99 | /// The following string identifiers are bound to the parts of the AST: |
| 100 | /// InitVarName: 'it' (as a VarDecl) |
| 101 | /// ConditionVarName: 'f' (as a VarDecl) |
| 102 | /// LoopName: The entire for loop (as a ForStmt) |
| 103 | /// In the first example only: |
| 104 | /// EndVarName: 'e' (as a VarDecl) |
| 105 | /// ConditionEndVarName: 'g' (as a VarDecl) |
| 106 | /// In the second example only: |
| 107 | /// EndCallName: 'container.end()' (as a CXXMemberCallExpr) |
| 108 | /// |
| 109 | /// Client code will need to make sure that: |
| 110 | /// - The iterator variables 'it', 'f', and 'h' are the same |
| 111 | /// - The two containers on which 'begin' and 'end' are called are the same |
| 112 | /// - If the end iterator variable 'g' is defined, it is the same as 'f' |
| 113 | StatementMatcher makeIteratorLoopMatcher() { |
| 114 | StatementMatcher BeginCallMatcher = |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 115 | memberCallExpr( |
| 116 | argumentCountIs(0), |
| 117 | callee( |
| 118 | methodDecl(hasName("begin")) |
| 119 | ) |
| 120 | ).bind(BeginCallName); |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 121 | |
| 122 | DeclarationMatcher InitDeclMatcher = |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 123 | varDecl( |
| 124 | hasInitializer( |
| 125 | anyOf( |
| 126 | ignoringParenImpCasts(BeginCallMatcher), |
| 127 | materializeTemporaryExpr(ignoringParenImpCasts(BeginCallMatcher)), |
| 128 | hasDescendant(BeginCallMatcher) |
| 129 | ) |
| 130 | ) |
| 131 | ).bind(InitVarName); |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 132 | |
| 133 | DeclarationMatcher EndDeclMatcher = |
| 134 | varDecl(hasInitializer(anything())).bind(EndVarName); |
| 135 | |
| 136 | StatementMatcher EndCallMatcher = |
| 137 | memberCallExpr(argumentCountIs(0), callee(methodDecl(hasName("end")))); |
| 138 | |
| 139 | StatementMatcher IteratorBoundMatcher = |
| 140 | expr(anyOf(ignoringParenImpCasts(declRefExpr(to( |
| 141 | varDecl().bind(ConditionEndVarName)))), |
| 142 | ignoringParenImpCasts( |
| 143 | expr(EndCallMatcher).bind(EndCallName)), |
| 144 | materializeTemporaryExpr(ignoringParenImpCasts( |
| 145 | expr(EndCallMatcher).bind(EndCallName))))); |
| 146 | |
| 147 | StatementMatcher IteratorComparisonMatcher = |
| 148 | expr(ignoringParenImpCasts(declRefExpr(to( |
| 149 | varDecl().bind(ConditionVarName))))); |
| 150 | |
| 151 | StatementMatcher OverloadedNEQMatcher = operatorCallExpr( |
| 152 | hasOverloadedOperatorName("!="), |
| 153 | argumentCountIs(2), |
| 154 | hasArgument(0, IteratorComparisonMatcher), |
| 155 | hasArgument(1, IteratorBoundMatcher)); |
| 156 | |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 157 | // This matcher tests that a declaration is a CXXRecordDecl that has an |
| 158 | // overloaded operator*(). If the operator*() returns by value instead of by |
| 159 | // reference then the return type is tagged with DerefByValueResultName. |
| 160 | internal::Matcher<VarDecl> TestDerefReturnsByValue = |
| 161 | hasType( |
| 162 | recordDecl( |
| 163 | hasMethod( |
| 164 | allOf( |
| 165 | hasOverloadedOperatorName("*"), |
| 166 | anyOf( |
| 167 | // Tag the return type if it's by value. |
| 168 | returns( |
| 169 | qualType( |
| 170 | unless(hasCanonicalType(referenceType())) |
| 171 | ).bind(DerefByValueResultName) |
| 172 | ), |
| 173 | returns( |
| 174 | // Skip loops where the iterator's operator* returns an |
| 175 | // rvalue reference. This is just weird. |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 176 | qualType( |
| 177 | unless( |
| 178 | hasCanonicalType(rValueReferenceType()) |
| 179 | ) |
| 180 | ).bind(DerefByRefResultName) |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 181 | ) |
| 182 | ) |
| 183 | ) |
| 184 | ) |
| 185 | ) |
| 186 | ); |
| 187 | |
Ariel J. Bernal | 3429028 | 2013-05-09 17:46:20 +0000 | [diff] [blame] | 188 | |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 189 | return |
| 190 | forStmt( |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 191 | hasLoopInit(anyOf( |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 192 | declStmt( |
| 193 | declCountIs(2), |
| 194 | containsDeclaration(0, InitDeclMatcher), |
| 195 | containsDeclaration(1, EndDeclMatcher) |
| 196 | ), |
| 197 | declStmt(hasSingleDecl(InitDeclMatcher)) |
| 198 | )), |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 199 | hasCondition(anyOf( |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 200 | binaryOperator( |
| 201 | hasOperatorName("!="), |
| 202 | hasLHS(IteratorComparisonMatcher), |
| 203 | hasRHS(IteratorBoundMatcher) |
| 204 | ), |
| 205 | binaryOperator( |
| 206 | hasOperatorName("!="), |
| 207 | hasLHS(IteratorBoundMatcher), |
| 208 | hasRHS(IteratorComparisonMatcher) |
| 209 | ), |
| 210 | OverloadedNEQMatcher |
| 211 | )), |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 212 | hasIncrement(anyOf( |
Edwin Vane | 4f05d71 | 2013-03-07 16:22:05 +0000 | [diff] [blame] | 213 | unaryOperator( |
| 214 | hasOperatorName("++"), |
| 215 | hasUnaryOperand( |
| 216 | declRefExpr(to( |
| 217 | varDecl(hasType(pointsTo(AnyType))).bind(IncrementVarName) |
| 218 | )) |
| 219 | ) |
| 220 | ), |
| 221 | operatorCallExpr( |
| 222 | hasOverloadedOperatorName("++"), |
| 223 | hasArgument(0, |
| 224 | declRefExpr(to( |
| 225 | varDecl(TestDerefReturnsByValue).bind(IncrementVarName) |
| 226 | )) |
| 227 | ) |
| 228 | ) |
| 229 | )) |
| 230 | ).bind(LoopName); |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// \brief The matcher used for array-like containers (pseudoarrays). |
| 234 | /// |
| 235 | /// This matcher is more flexible than array-based loops. It will match |
| 236 | /// loops of the following textual forms (regardless of whether the |
| 237 | /// iterator type is actually a pointer type or a class type): |
| 238 | /// |
| 239 | /// Assuming f, g, and h are of type containerType::iterator, |
| 240 | /// \code |
| 241 | /// for (int i = 0, j = container.size(); f < g; ++h) { ... } |
| 242 | /// for (int i = 0; f < container.size(); ++h) { ... } |
| 243 | /// \endcode |
| 244 | /// The following string identifiers are bound to the parts of the AST: |
| 245 | /// InitVarName: 'i' (as a VarDecl) |
| 246 | /// ConditionVarName: 'f' (as a VarDecl) |
| 247 | /// LoopName: The entire for loop (as a ForStmt) |
| 248 | /// In the first example only: |
| 249 | /// EndVarName: 'j' (as a VarDecl) |
| 250 | /// ConditionEndVarName: 'g' (as a VarDecl) |
| 251 | /// In the second example only: |
| 252 | /// EndCallName: 'container.size()' (as a CXXMemberCallExpr) |
| 253 | /// |
| 254 | /// Client code will need to make sure that: |
| 255 | /// - The index variables 'i', 'f', and 'h' are the same |
| 256 | /// - The containers on which 'size()' is called is the container indexed |
| 257 | /// - The index variable is only used in overloaded operator[] or |
| 258 | /// container.at() |
| 259 | /// - If the end iterator variable 'g' is defined, it is the same as 'j' |
| 260 | /// - The container's iterators would not be invalidated during the loop |
| 261 | StatementMatcher makePseudoArrayLoopMatcher() { |
Edwin Vane | b40bf83 | 2013-05-09 20:03:52 +0000 | [diff] [blame] | 262 | // Test that the incoming type has a record declaration that has methods |
| 263 | // called 'begin' and 'end'. If the incoming type is const, then make sure |
| 264 | // these methods are also marked const. |
Alp Toker | 9a5134e | 2013-12-01 05:08:12 +0000 | [diff] [blame^] | 265 | // |
Edwin Vane | b40bf83 | 2013-05-09 20:03:52 +0000 | [diff] [blame] | 266 | // FIXME: To be completely thorough this matcher should also ensure the |
| 267 | // return type of begin/end is an iterator that dereferences to the same as |
| 268 | // what operator[] or at() returns. Such a test isn't likely to fail except |
| 269 | // for pathological cases. |
| 270 | // |
| 271 | // FIXME: Also, a record doesn't necessarily need begin() and end(). Free |
| 272 | // functions called begin() and end() taking the container as an argument |
| 273 | // are also allowed. |
| 274 | TypeMatcher RecordWithBeginEnd = |
| 275 | qualType(anyOf( |
| 276 | qualType( |
| 277 | isConstQualified(), |
| 278 | hasDeclaration( |
| 279 | recordDecl( |
| 280 | hasMethod( |
| 281 | methodDecl( |
| 282 | hasName("begin"), |
| 283 | isConst() |
| 284 | ) |
| 285 | ), |
| 286 | hasMethod( |
| 287 | methodDecl( |
| 288 | hasName("end"), |
| 289 | isConst() |
| 290 | ) |
| 291 | ) |
| 292 | ) |
| 293 | ) // hasDeclaration |
| 294 | ), // qualType |
| 295 | qualType( |
| 296 | unless(isConstQualified()), |
| 297 | hasDeclaration( |
| 298 | recordDecl( |
| 299 | hasMethod(hasName("begin")), |
| 300 | hasMethod(hasName("end")) |
| 301 | ) |
| 302 | ) |
| 303 | ) // qualType |
| 304 | ) |
| 305 | ); |
| 306 | |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 307 | StatementMatcher SizeCallMatcher = |
| 308 | memberCallExpr(argumentCountIs(0), |
| 309 | callee(methodDecl(anyOf(hasName("size"), |
Edwin Vane | b40bf83 | 2013-05-09 20:03:52 +0000 | [diff] [blame] | 310 | hasName("length")))), |
| 311 | on(anyOf(hasType(pointsTo(RecordWithBeginEnd)), |
| 312 | hasType(RecordWithBeginEnd)))); |
Edwin Vane | dde168b | 2013-01-04 18:25:18 +0000 | [diff] [blame] | 313 | |
| 314 | StatementMatcher EndInitMatcher = |
| 315 | expr(anyOf( |
| 316 | ignoringParenImpCasts(expr(SizeCallMatcher).bind(EndCallName)), |
| 317 | explicitCastExpr(hasSourceExpression(ignoringParenImpCasts( |
| 318 | expr(SizeCallMatcher).bind(EndCallName)))))); |
| 319 | |
| 320 | DeclarationMatcher EndDeclMatcher = |
| 321 | varDecl(hasInitializer(EndInitMatcher)).bind(EndVarName); |
| 322 | |
| 323 | StatementMatcher IndexBoundMatcher = |
| 324 | expr(anyOf( |
| 325 | ignoringParenImpCasts(declRefExpr(to( |
| 326 | varDecl(hasType(isInteger())).bind(ConditionEndVarName)))), |
| 327 | EndInitMatcher)); |
| 328 | |
| 329 | return forStmt( |
| 330 | hasLoopInit(anyOf( |
| 331 | declStmt(declCountIs(2), |
| 332 | containsDeclaration(0, InitToZeroMatcher), |
| 333 | containsDeclaration(1, EndDeclMatcher)), |
| 334 | declStmt(hasSingleDecl(InitToZeroMatcher)))), |
| 335 | hasCondition(anyOf( |
| 336 | binaryOperator(hasOperatorName("<"), |
| 337 | hasLHS(IntegerComparisonMatcher), |
| 338 | hasRHS(IndexBoundMatcher)), |
| 339 | binaryOperator(hasOperatorName(">"), |
| 340 | hasLHS(IndexBoundMatcher), |
| 341 | hasRHS(IntegerComparisonMatcher)))), |
| 342 | hasIncrement(unaryOperator( |
| 343 | hasOperatorName("++"), |
| 344 | hasUnaryOperand(IncrementVarMatcher)))) |
| 345 | .bind(LoopName); |
| 346 | } |