Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-11 Thread Etienne Bergeron via cfe-commits
etienneb updated this revision to Diff 56942.
etienneb added a comment.

re-generating doc


http://reviews.llvm.org/D19876

Files:
  docs/LibASTMatchersReference.html
  include/clang/ASTMatchers/ASTMatchers.h
  include/clang/ASTMatchers/ASTMatchersInternal.h
  unittests/ASTMatchers/ASTMatchersTest.cpp

Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -51,7 +51,6 @@
 
   // Do not accept non-toplevel matchers.
   EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), nullptr));
-  EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), nullptr));
   EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr));
 }
 
@@ -2536,6 +2535,17 @@
   EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
 }
 
+TEST(StringLiteral, HasSize) {
+  StatementMatcher Literal = stringLiteral(hasSize(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: include/clang/ASTMatchers/ASTMatchersInternal.h
===
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1651,6 +1651,19 @@
 }
 
 template 
+struct HasSizeMatcher {
+  static bool hasSize(const Ty &Node, unsigned int N) {
+return Node.getSize() == N;
+  }
+};
+
+template <>
+inline bool HasSizeMatcher::hasSize(
+const StringLiteral &Node, unsigned int N) {
+  return Node.getLength() == N;
+}
+
+template 
 struct GetSourceExpressionMatcher {
   static const Expr *get(const Ty &Node) {
 return Node.getSubExpr();
Index: include/clang/ASTMatchers/ASTMatchers.h
===
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -1560,7 +1560,8 @@
 ///
 /// 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,
@@ -1573,7 +1574,8 @@
 ///
 /// Example matches 'a', L'a'
 /// \code
-///   char ch = 'a'; wchar_t chw = L'a';
+///   char ch = 'a';
+///   wchar_t chw = L'a';
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -1609,7 +1611,8 @@
 ///
 /// Example match: {1}, (1, 2)
 /// \code
-///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
+///   int array[4] = {1};
+///   vector int myvec = (vector int)(1, 2);
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -4228,18 +4231,26 @@
 ///   matches "int a[2]"
 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType);
 
-/// \brief Matches \c ConstantArrayType nodes that have the specified size.
+/// \brief Matches nodes that have the specified size.
 ///
 /// Given
 /// \code
 ///   int a[42];
 ///   int b[2 * 21];
 ///   int c[41], d[43];
+///   char *s = "abcd";
+///   wchar_t *ws = L"abcd";
+///   char *w = "a";
 /// \endcode
 /// constantArrayType(hasSize(42))
 ///   matches "int a[42]" and "int b[2 * 21]"
-AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
-  return Node.getSize() == N;
+/// stringLiteral(hasSize(4))
+///   matches "abcd", L"abcd"
+AST_POLYMORPHIC_MATCHER_P(hasSize,
+  AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType,
+  StringLiteral),
+  unsigned, N) {
+  return internal::HasSizeMatcher::hasSize(Node, N);
 }
 
 /// \brief Matches C++ arrays whose size is a value-dependent expression.
Index: docs/LibASTMatchersReference.html
===
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -644,15 +644,17 @@
 though.
 
 Example matches 'a', L'a'
-  char ch = 'a'; wchar_t chw = L'a';
+  char ch = 'a';
+  wchar_t chw = L'a';
 
 
 
 MatcherStmt>compoundLiteralExprMatcherCompoundLiteralExpr>...
 Matches compound (i.e. non-scalar) literals
 
 Example match: {1}, (1, 2)
-  int array[4] = {1}; vector int myvec = (vector int)(1, 2);
+  int array[4] = {1};
+  vector int myvec = (vector int)(1, 2);
 
 
 
@@ -1217,7 +1219,8 @@
 Matches string literals (also matches wide string literals).
 
 Example matches "abcd", L"abcd"
-  char *s = "abcd"; wchar_t *ws = L"abcd"
+  char *s = "

Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-11 Thread Aaron Ballman via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

LGTM, thank you for this!


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-03 Thread Etienne Bergeron via cfe-commits
etienneb updated this revision to Diff 56025.
etienneb added a comment.

fix doc


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,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: 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";
 
 
 
@@ -2941,6 +2941,17 @@
 
 
 
+MatcherStringLiteral>lengthIsunsigned N
+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";
+
+
+
 MatcherTagDecl>isDefinition
 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


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-03 Thread Aaron Ballman via cfe-commits
aaron.ballman added inline comments.


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1575
@@ +1574,3 @@
+/// \code
+///   char *s = "abcd"; wchar_t *ws = L"abcd";
+///   char *t = "a";

Split these onto two lines?


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

Perhaps we can adjust the `hasSize()` matcher instead? It currently works with 
ConstantArrayType, but it seems reasonable for it to also work with 
StringLiteral.


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-03 Thread Etienne Bergeron via cfe-commits
etienneb added inline comments.


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

aaron.ballman wrote:
> Perhaps we can adjust the `hasSize()` matcher instead? It currently works 
> with ConstantArrayType, but it seems reasonable for it to also work with 
> StringLiteral.
I didn't like the term "size" as it typically refer to the size in bytes.
Which is not the same for a wide-string.

Now, there is two different convention for naming matchers:
  hasLength   and  lengthIs  ?

Any toughs on that?




http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-04 Thread Etienne Bergeron via cfe-commits
etienneb added a comment.

Aaron? could you comment on it?



Comment at: include/clang/ASTMatchers/ASTMatchers.h:1575
@@ +1574,3 @@
+/// \code
+///   char *s = "abcd"; wchar_t *ws = L"abcd";
+///   char *t = "a";

aaron.ballman wrote:
> Split these onto two lines?
If I look around, it seems to be more consistent to keep it on the same line 
(line 1563)

```
///   char *s = "abcd"; wchar_t *ws = L"abcd";
```

```
///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
```

```
///   char ch = 'a'; wchar_t chw = L'a';
``


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

etienneb wrote:
> aaron.ballman wrote:
> > Perhaps we can adjust the `hasSize()` matcher instead? It currently works 
> > with ConstantArrayType, but it seems reasonable for it to also work with 
> > StringLiteral.
> I didn't like the term "size" as it typically refer to the size in bytes.
> Which is not the same for a wide-string.
> 
> Now, there is two different convention for naming matchers:
>   hasLength   and  lengthIs  ?
> 
> Any toughs on that?
> 
> 
Here is the matcher for hasSize
```
AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
  return Node.getSize() == N;
}
```

It's getting the getSize attribute. I believe we should stick with the name of 
the attribute.
But, I'm not sure if we should use hasLength, or lengthIs.


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-04 Thread Aaron Ballman via cfe-commits
aaron.ballman added inline comments.


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1575
@@ +1574,3 @@
+/// \code
+///   char *s = "abcd"; wchar_t *ws = L"abcd";
+///   char *t = "a";

etienneb wrote:
> aaron.ballman wrote:
> > Split these onto two lines?
> If I look around, it seems to be more consistent to keep it on the same line 
> (line 1563)
> 
> ```
> ///   char *s = "abcd"; wchar_t *ws = L"abcd";
> ```
> 
> ```
> ///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
> ```
> 
> ```
> ///   char ch = 'a'; wchar_t chw = L'a';
> ``
I don't have a strong opinion on it; however, since these get turned into 
examples that are on the website, I would weakly prefer the examples not be 
hideous. :-P


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

etienneb wrote:
> etienneb wrote:
> > aaron.ballman wrote:
> > > Perhaps we can adjust the `hasSize()` matcher instead? It currently works 
> > > with ConstantArrayType, but it seems reasonable for it to also work with 
> > > StringLiteral.
> > I didn't like the term "size" as it typically refer to the size in bytes.
> > Which is not the same for a wide-string.
> > 
> > Now, there is two different convention for naming matchers:
> >   hasLength   and  lengthIs  ?
> > 
> > Any toughs on that?
> > 
> > 
> Here is the matcher for hasSize
> ```
> AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
>   return Node.getSize() == N;
> }
> ```
> 
> It's getting the getSize attribute. I believe we should stick with the name 
> of the attribute.
> But, I'm not sure if we should use hasLength, or lengthIs.
I'm not too worried about size vs length (for instance, std::string has both). 
I would imagine this being implemented the same way we do other things with 
variance in API but not concept. See GetBodyMatcher in ASTMatchersInternal.h 
(and others near there) as an example.

I prefer hasSize because the two concepts are quite similar. For instance, a 
string literal's type is of a constant array type already.


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-04 Thread Etienne Bergeron via cfe-commits
etienneb added a comment.

Other opinions?
I'll proceed to the cleanup if no one else has comments.



Comment at: include/clang/ASTMatchers/ASTMatchers.h:1575
@@ +1574,3 @@
+/// \code
+///   char *s = "abcd"; wchar_t *ws = L"abcd";
+///   char *t = "a";

aaron.ballman wrote:
> etienneb wrote:
> > aaron.ballman wrote:
> > > Split these onto two lines?
> > If I look around, it seems to be more consistent to keep it on the same 
> > line (line 1563)
> > 
> > ```
> > ///   char *s = "abcd"; wchar_t *ws = L"abcd";
> > ```
> > 
> > ```
> > ///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
> > ```
> > 
> > ```
> > ///   char ch = 'a'; wchar_t chw = L'a';
> > ``
> I don't have a strong opinion on it; however, since these get turned into 
> examples that are on the website, I would weakly prefer the examples not be 
> hideous. :-P
Ditto. No strong opinion.
But, I like consistency. I'm willing to fix all other instances too.


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

aaron.ballman wrote:
> etienneb wrote:
> > etienneb wrote:
> > > aaron.ballman wrote:
> > > > Perhaps we can adjust the `hasSize()` matcher instead? It currently 
> > > > works with ConstantArrayType, but it seems reasonable for it to also 
> > > > work with StringLiteral.
> > > I didn't like the term "size" as it typically refer to the size in bytes.
> > > Which is not the same for a wide-string.
> > > 
> > > Now, there is two different convention for naming matchers:
> > >   hasLength   and  lengthIs  ?
> > > 
> > > Any toughs on that?
> > > 
> > > 
> > Here is the matcher for hasSize
> > ```
> > AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
> >   return Node.getSize() == N;
> > }
> > ```
> > 
> > It's getting the getSize attribute. I believe we should stick with the name 
> > of the attribute.
> > But, I'm not sure if we should use hasLength, or lengthIs.
> I'm not too worried about size vs length (for instance, std::string has 
> both). I would imagine this being implemented the same way we do other things 
> with variance in API but not concept. See GetBodyMatcher in 
> ASTMatchersInternal.h (and others near there) as an example.
> 
> I prefer hasSize because the two concepts are quite similar. For instance, a 
> string literal's type is of a constant array type already.
I do not have strong opinion too on the naming. I'm curious if others also has 
opinion on it?
Then, I'll proceed.


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-06 Thread Etienne Bergeron via cfe-commits
etienneb added inline comments.


Comment at: include/clang/ASTMatchers/ASTMatchers.h:1578
@@ +1577,3 @@
+/// \endcode
+AST_MATCHER_P(StringLiteral, lengthIs, unsigned, N) {
+  return Node.getLength() == N;

etienneb wrote:
> aaron.ballman wrote:
> > etienneb wrote:
> > > etienneb wrote:
> > > > aaron.ballman wrote:
> > > > > Perhaps we can adjust the `hasSize()` matcher instead? It currently 
> > > > > works with ConstantArrayType, but it seems reasonable for it to also 
> > > > > work with StringLiteral.
> > > > I didn't like the term "size" as it typically refer to the size in 
> > > > bytes.
> > > > Which is not the same for a wide-string.
> > > > 
> > > > Now, there is two different convention for naming matchers:
> > > >   hasLength   and  lengthIs  ?
> > > > 
> > > > Any toughs on that?
> > > > 
> > > > 
> > > Here is the matcher for hasSize
> > > ```
> > > AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
> > >   return Node.getSize() == N;
> > > }
> > > ```
> > > 
> > > It's getting the getSize attribute. I believe we should stick with the 
> > > name of the attribute.
> > > But, I'm not sure if we should use hasLength, or lengthIs.
> > I'm not too worried about size vs length (for instance, std::string has 
> > both). I would imagine this being implemented the same way we do other 
> > things with variance in API but not concept. See GetBodyMatcher in 
> > ASTMatchersInternal.h (and others near there) as an example.
> > 
> > I prefer hasSize because the two concepts are quite similar. For instance, 
> > a string literal's type is of a constant array type already.
> I do not have strong opinion too on the naming. I'm curious if others also 
> has opinion on it?
> Then, I'll proceed.
>  For instance, a string literal's type

stringLiteral is matching an expression and not a type.
And, hasSize is currently applied on types.
Do we want this?


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-06 Thread Etienne Bergeron via cfe-commits
etienneb updated this revision to Diff 56423.
etienneb added a comment.

merging hasSize


http://reviews.llvm.org/D19876

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  include/clang/ASTMatchers/ASTMatchersInternal.h
  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: include/clang/ASTMatchers/ASTMatchersInternal.h
===
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1651,6 +1651,19 @@
 }
 
 template 
+struct HasSizeMatcher {
+  static bool hasSize(const Ty &Node, unsigned int N) {
+return Node.getSize() == N;
+  }
+};
+
+template <>
+inline bool HasSizeMatcher::hasSize(
+const StringLiteral &Node, unsigned int N) {
+  return Node.getLength() == N;
+}
+
+template 
 struct GetSourceExpressionMatcher {
   static const Expr *get(const Ty &Node) {
 return Node.getSubExpr();
Index: include/clang/ASTMatchers/ASTMatchers.h
===
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -1560,7 +1560,8 @@
 ///
 /// 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,
@@ -1573,7 +1574,8 @@
 ///
 /// Example matches 'a', L'a'
 /// \code
-///   char ch = 'a'; wchar_t chw = L'a';
+///   char ch = 'a';
+///   wchar_t chw = L'a';
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -1609,7 +1611,8 @@
 ///
 /// Example match: {1}, (1, 2)
 /// \code
-///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
+///   int array[4] = {1};
+///   vector int myvec = (vector int)(1, 2);
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -4228,18 +4231,26 @@
 ///   matches "int a[2]"
 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType);
 
-/// \brief Matches \c ConstantArrayType nodes that have the specified size.
+/// \brief Matches nodes that have the specified size.
 ///
 /// Given
 /// \code
 ///   int a[42];
 ///   int b[2 * 21];
 ///   int c[41], d[43];
+///   char *s = "abcd";
+///   wchar_t *ws = L"abcd";
+///   char *w = "a";
 /// \endcode
 /// constantArrayType(hasSize(42))
 ///   matches "int a[42]" and "int b[2 * 21]"
-AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
-  return Node.getSize() == N;
+/// stringLiteral(lengthIs(4))
+///   matches "abcd", L"abcd"
+AST_POLYMORPHIC_MATCHER_P(hasSize,
+  AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType,
+  StringLiteral),
+  unsigned, N) {
+  return internal::HasSizeMatcher::hasSize(Node, N);
 }
 
 /// \brief Matches C++ arrays whose size is a value-dependent expression.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-06 Thread Etienne Bergeron via cfe-commits
etienneb updated this revision to Diff 56425.
etienneb added a comment.

fix unittests


http://reviews.llvm.org/D19876

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  include/clang/ASTMatchers/ASTMatchersInternal.h
  unittests/ASTMatchers/ASTMatchersTest.cpp

Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -51,7 +51,6 @@
 
   // Do not accept non-toplevel matchers.
   EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), nullptr));
-  EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), nullptr));
   EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr));
 }
 
@@ -2536,6 +2535,17 @@
   EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
 }
 
+TEST(StringLiteral, HasSize) {
+  StatementMatcher Literal = stringLiteral(hasSize(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: include/clang/ASTMatchers/ASTMatchersInternal.h
===
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1651,6 +1651,19 @@
 }
 
 template 
+struct HasSizeMatcher {
+  static bool hasSize(const Ty &Node, unsigned int N) {
+return Node.getSize() == N;
+  }
+};
+
+template <>
+inline bool HasSizeMatcher::hasSize(
+const StringLiteral &Node, unsigned int N) {
+  return Node.getLength() == N;
+}
+
+template 
 struct GetSourceExpressionMatcher {
   static const Expr *get(const Ty &Node) {
 return Node.getSubExpr();
Index: include/clang/ASTMatchers/ASTMatchers.h
===
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -1560,7 +1560,8 @@
 ///
 /// 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,
@@ -1573,7 +1574,8 @@
 ///
 /// Example matches 'a', L'a'
 /// \code
-///   char ch = 'a'; wchar_t chw = L'a';
+///   char ch = 'a';
+///   wchar_t chw = L'a';
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -1609,7 +1611,8 @@
 ///
 /// Example match: {1}, (1, 2)
 /// \code
-///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
+///   int array[4] = {1};
+///   vector int myvec = (vector int)(1, 2);
 /// \endcode
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
@@ -4228,18 +4231,26 @@
 ///   matches "int a[2]"
 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType);
 
-/// \brief Matches \c ConstantArrayType nodes that have the specified size.
+/// \brief Matches nodes that have the specified size.
 ///
 /// Given
 /// \code
 ///   int a[42];
 ///   int b[2 * 21];
 ///   int c[41], d[43];
+///   char *s = "abcd";
+///   wchar_t *ws = L"abcd";
+///   char *w = "a";
 /// \endcode
 /// constantArrayType(hasSize(42))
 ///   matches "int a[42]" and "int b[2 * 21]"
-AST_MATCHER_P(ConstantArrayType, hasSize, unsigned, N) {
-  return Node.getSize() == N;
+/// stringLiteral(hasSize(4))
+///   matches "abcd", L"abcd"
+AST_POLYMORPHIC_MATCHER_P(hasSize,
+  AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType,
+  StringLiteral),
+  unsigned, N) {
+  return internal::HasSizeMatcher::hasSize(Node, N);
 }
 
 /// \brief Matches C++ arrays whose size is a value-dependent expression.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-06 Thread Etienne Bergeron via cfe-commits
etienneb added a comment.

Aaron? minus re-generation of the doc.
Is that what you want to see?

note: returned types for both getLength are not the same (APInt vs unsigned 
int).
I took the 'hasSize'-Matcher approach to let overloaded operators do the job 
for dispatching types.

note: there is a unittest for top-level node that is no longer valid because 
stringLiteral can be a top-level node now.


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D19876: Add an AST matcher for string-literal length

2016-05-10 Thread Aaron Ballman via cfe-commits
aaron.ballman added a comment.

In http://reviews.llvm.org/D19876#423582, @etienneb wrote:

> Aaron? minus re-generation of the doc.
>  Is that what you want to see?


Yes, this is the direction I was hoping for, thank you!

> note: returned types for both getLength are not the same (APInt vs unsigned 
> int).

>  I took the 'hasSize'-Matcher approach to let overloaded operators do the job 
> for dispatching types.


Perfect!

> note: there is a unittest for top-level node that is no longer valid because 
> stringLiteral can be a top-level node now.


Thank you for that explanation, I was curious about that. :-)


http://reviews.llvm.org/D19876



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits