[PATCH] D50488: [Analyzer] Checker for non-determinism caused by sorting of pointer-like elements

Mandeep Singh Grang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 4 11:20:22 PDT 2018


mgrang added a comment.

In https://reviews.llvm.org/D50488#1207398, @Szelethus wrote:

> In https://reviews.llvm.org/D50488#1204655, @mgrang wrote:
>
> > This is my first time with ASTMatchers and I am not sure how to get the value_type from hasType (I don't see a matcher for value_types in ASTMatchers.h). Would I end up using a visitor for that? If yes, then maybe the entire check for pointer types needs to be done via visitors? Sorry for the newbie questions :)
>
>
> I played around for a bit, my `clang-query` is a little out of date, but here's what I found: You can match `typedef`s with `typedefDecl()`, and `using` by `typeAliasDecl()`, and then add `hasName("value_type")` as a parameter. As to how to check whether a type has any of these, I'm a little unsure myself, but you could use `hasDescendant`, and narrow down the matches.
>
> I'm not sure whether this checks inherited type declarations, and it sure doesn't check template specializations of `std::iterator_traits`, but it is a good start :) I'll revisit this when I have a little more time on my hand.


I played around a lot with clang-query in the past week but I haven't been able to match typedefDecl with hasName("value_type"). Here are some matchers I tried:

  match callExpr(allOf (callee(functionDecl(hasName("std::sort"))), hasArgument(0,          stmt(hasDescendant(expr(hasType(typedefType(hasDeclaration(typedefNameDecl(hasName("value_type")))))))))))
  
  match callExpr(allOf (callee(functionDecl(hasName("std::sort"))), hasArgument(0,            hasDescendant(expr(hasType(typedefType(hasDeclaration(typedefDecl(matchesName(".*value.*"))))))))))
  
  match callExpr(allOf (callee(functionDecl(hasName("std::sort"))), hasArgument(0,            hasDescendant(declRefExpr(to(fieldDecl(hasName("value_type"))))))))))

Here's the AST dump for the IntPointerArray example:

  FunctionDecl 0x639e958 <../../tst/s.cpp:5:1, line:10:1> line:5:5 main 'int ()'
  `-CompoundStmt 0x639f1d0 <col:12, line:10:1>
    |-DeclStmt 0x639ead0 <line:6:3, col:27>
    | `-VarDecl 0x639ea70 <col:3, col:26> col:8 used IntPointerArray 'int *[20]'
    |-CallExpr 0x639ebd0 <line:7:3, col:23> 'void'
    | |-ImplicitCastExpr 0x639ebb8 <col:3> 'void (*)(int **)' <FunctionToPointerDecay>
    | | `-DeclRefExpr 0x639eb68 <col:3> 'void (int **)' lvalue Function 0x639e890 'fill' 'void (int **)'
    | `-ImplicitCastExpr 0x639ec00 <col:8> 'int **' <ArrayToPointerDecay>
    |   `-DeclRefExpr 0x639eb40 <col:8> 'int *[20]' lvalue Var 0x639ea70 'IntPointerArray' 'int *[20]'
    `-CallExpr 0x639f180 <line:9:3, col:50> 'void'
  |-ImplicitCastExpr 0x639f168 <col:3, col:8> 'void (*)(int **, int **)' <FunctionToPointerDecay>
      | `-DeclRefExpr 0x639f0d0 <col:3, col:8> 'void (int **, int **)' lvalue Function 0x639efd0 'sort' 'void (int **, int **)' (FunctionTemplate 0x638dd18 'sort')
      |-ImplicitCastExpr 0x639f1b8 <col:13> 'int **' <ArrayToPointerDecay>
      | `-DeclRefExpr 0x639ec98 <col:13> 'int *[20]' lvalue Var 0x639ea70 'IntPointerArray' 'int *[20]'
      `-BinaryOperator 0x639ed20 <col:30, col:48> 'int **' '+'
        |-ImplicitCastExpr 0x639ed08 <col:30> 'int **' <ArrayToPointerDecay>
        | `-DeclRefExpr 0x639ecc0 <col:30> 'int *[20]' lvalue Var 0x639ea70 'IntPointerArray' 'int *[20]'
        `-IntegerLiteral 0x639ece8 <col:48> 'int' 20


Repository:
  rC Clang

https://reviews.llvm.org/D50488





More information about the cfe-commits mailing list