<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/107680>107680</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] RFC: Add generic check that takes match expression from clang-query file
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          olologin
      </td>
    </tr>
</table>

<pre>
    A wild idea, but maybe someone else will also find it useful, you can close this issue if it doesn't make sense or raises too many concerns.

Can we have clang-tidy check that takes its AST match expressions from clang-query input file?
I can explain why this might be useful:
Clang-tidy has a lot of things built around it, like plugins for IDE, wrapper-scripts like [https://github.com/Ericsson/codechecker](codechecker) which we use at work, and I believe also a lot of other things. Implementing all this for clang-query is not easy.
And unfortunately some projects need to have their own weird (but simple) checks, which are useless for upstream clang-tidy. So we have to find some way to build our custom checks into clang-tidy/clang-plugin and then build it ourselves. But rebuilding clang-tidy to add simple match expression seems like overkill, especially because you need to build it for different platforms, distribute it, find some way for your developers to keep it updated, etc.

What if we just develop a new check like `clang-query-based` which in its configuration takes path to the clang-query file? It seems clang-query parser is already implemented, maybe it is easy to reuse it for this purpose.

`.clang-tidy`:
```
Checks: >
 clang-query-based

CheckOptions:
    clang-query-based.QueryFile: clang-query/custom_query.txt
```

`clang-query/custom_query.txt` contents:
```
match callExpr(
    callee(functionDecl(
 hasAnyName("sort", "nth_element", "partial_sort", "partition"),
 anyOf(
 hasDeclContext(namespaceDecl(hasName("std"))),
 unless(hasParent(namespaceDecl()))
        )
    )),
 isExpansionInFileMatching(".*my_project/dev/.*")
).bind("Please use sort functions from platform-stable library instead of std (std sort functions lead to different results on different STLs)")
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVttu48AN_Rr5hYihjOPbgx-8SQwEaLvb7gJ9DCiJsmYzmlGHlB39fcGRnNibX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