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" @@ -421,7 +421,7 @@ Given typedef int X; - using Y = int; + using Y = int; typedefDecl() matches "typedef int X", but not "using Y = int" @@ -432,7 +432,7 @@ Given typedef int X; - using Y = int; + using Y = int; typedefNameDecl() matches "typedef int X" and "using Y = int" @@ -1217,7 +1217,7 @@
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";
Matches string length for a given string literal node. + +Example: + stringLiteral(lengthIs(4)) +matches "abcd", L"abcd" + char *s = "abcd"; wchar_t *ws = L"abcd"; + char *t = "a"; +
Matches if a declaration has a body attached. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -1560,12 +1560,25 @@ /// /// 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: +/// stringLiteral(lengthIs(4)) +/// 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: 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: 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));