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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed May 4 11:20:10 PDT 2016


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





More information about the cfe-commits mailing list