etienneb created this revision. etienneb added reviewers: alexfh, sbenza, aaron.ballman, klimek. etienneb added a subscriber: cfe-commits. Herald added a subscriber: klimek.
This patch is adding support for a matcher to check string literal length. This matcher is used in clang-tidy checkers and is part of this refactoring: see: http://reviews.llvm.org/D19841 http://reviews.llvm.org/D19876 Files: docs/LibASTMatchersReference.html include/clang/ASTMatchers/ASTMatchers.h lib/ASTMatchers/Dynamic/Registry.cpp unittests/ASTMatchers/ASTMatchersTest.cpp
Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2536,6 +2536,17 @@ EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); } +TEST(StringLiteral, LengthIs) { + StatementMatcher Literal = stringLiteral(lengthIs(4)); + EXPECT_TRUE(matches("const char *s = \"abcd\";", Literal)); + // wide string + EXPECT_TRUE(matches("const wchar_t *s = L\"abcd\";", Literal)); + // with escaped characters + EXPECT_TRUE(matches("const char *s = \"\x05\x06\x07\x08\";", Literal)); + // no matching, too small + EXPECT_TRUE(notMatches("const char *s = \"ab\";", Literal)); +} + TEST(Matcher, CharacterLiterals) { StatementMatcher CharLiteral = characterLiteral(); EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -323,6 +323,7 @@ REGISTER_MATCHER(labelDecl); REGISTER_MATCHER(labelStmt); REGISTER_MATCHER(lambdaExpr); + REGISTER_MATCHER(lengthIs); REGISTER_MATCHER(lValueReferenceType); REGISTER_MATCHER(matchesName); REGISTER_MATCHER(matchesSelector); Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -1560,12 +1560,23 @@ /// /// Example matches "abcd", L"abcd" /// \code -/// char *s = "abcd"; wchar_t *ws = L"abcd" +/// char *s = "abcd"; wchar_t *ws = L"abcd"; /// \endcode const internal::VariadicDynCastAllOfMatcher< Stmt, StringLiteral> stringLiteral; +/// \brief Matches string length for a given string literal node. +/// +/// Example matches "abcd", L"abcd" +/// \code +/// char *s = "abcd"; wchar_t *ws = L"abcd"; +/// char *t = "a"; +/// \endcode +AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) { + return Node.getLength() == N; +} + /// \brief Matches character literals (also matches wchar_t). /// /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral), Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -410,7 +410,7 @@ Given typedef int X; - using Y = int; + using Y = int; typeAliasDecl() matches "using Y = int", but not "typedef int X" </pre></td></tr> @@ -421,7 +421,7 @@ Given typedef int X; - using Y = int; + using Y = int; typedefDecl() matches "typedef int X", but not "using Y = int" </pre></td></tr> @@ -432,7 +432,7 @@ Given typedef int X; - using Y = int; + using Y = int; typedefNameDecl() matches "typedef int X" and "using Y = int" </pre></td></tr> @@ -1217,7 +1217,7 @@ <tr><td colspan="4" class="doc" id="stringLiteral0"><pre>Matches string literals (also matches wide string literals). Example matches "abcd", L"abcd" - char *s = "abcd"; wchar_t *ws = L"abcd" + char *s = "abcd"; wchar_t *ws = L"abcd"; </pre></td></tr> @@ -2941,6 +2941,15 @@ </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1StringLiteral.html">StringLiteral</a>></td><td class="name" onclick="toggle('lengthIs0')"><a name="lengthIs0Anchor">lengthIs</a></td><td>unsigned N</td></tr> +<tr><td colspan="4" class="doc" id="lengthIs0"><pre>Matches string length for a given string literal node. + +Example matches "abcd", L"abcd" + char *s = "abcd"; wchar_t *ws = L"abcd"; + char *t = "a"; +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TagDecl.html">TagDecl</a>></td><td class="name" onclick="toggle('isDefinition0')"><a name="isDefinition0Anchor">isDefinition</a></td><td></td></tr> <tr><td colspan="4" class="doc" id="isDefinition0"><pre>Matches if a declaration has a body attached.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits