[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-06-04 Thread via cfe-commits

https://github.com/DeNiCoN closed 
https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-06-04 Thread Baranov Victor via cfe-commits

vbvictor wrote:

Closed as further review happens in 
https://github.com/llvm/llvm-project/pull/131804

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-06-04 Thread Baranov Victor via cfe-commits

vbvictor wrote:

@DeNiCoN, do you mind if this PR would be closed (or converted to draft at 
least)?
I'm in the process of reviewing old PR to lower the number of open ones for 
ease of tracking.
This PR could be reopened if needed.

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-03-17 Thread Ibraim Ganiev via cfe-commits

olologin wrote:

Just FYI:
I have tested @DeNiCoN on our codebase in the company we work with him together 
(~8k Translation units), and it seems to work fine. `NOLINT(check-name)` works, 
.clang-tidy config inheritance works (In case some subfolders want to disable 
particular matchers).
Very useful feature, I hope it gets into clang-tidy at some point. At the 
moment we use backported patch for clang-tidy-16

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-03-09 Thread via cfe-commits

DeNiCoN wrote:

> Hello @DeNiCoN, Are you still working on this patch? If not, I hope I can 
> take over this patch to finally finish it.

Hello @HerrCai0907. I'm not actively working on it. You can take over

Some notes:
I've found a bug that if a config file is not explicitly specified(no 
`--config-file` option given) custom checks do not run. This happens because in 
the current code custom checks are added at the start but 
`ClangTidyContext::CurrentOptions` gets updated by finding nearest config file 
only later. I've used the following workaround 
https://github.com/llvm/llvm-project/commit/e30df2d7d686efc832a6f3107235a8e4decbac65
But I believe in the current state it introduces a bug that some checks can be 
used even if they are not defined in the parent configuration files because it 
updates `CheckFactories` globally

I've also found that --list-checks would only list custom checks for the first 
file specified 
https://github.com/llvm/llvm-project/blob/c4808741e89df29dcd06c7be2784824c82f34e46/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp#L623
And --verify-config does not respect custom checks
clangd seems to not see custom checks because it uses `ClangTidyModuleRegistry` 
to get all checks

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-03-08 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

Hello @DeNiCoN, Are you still working on this patch? If not, I hope I can take 
over this patch to finally finish it.

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-22 Thread via cfe-commits

https://github.com/DeNiCoN edited 
https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-22 Thread via cfe-commits


@@ -126,8 +127,15 @@ struct ClangTidyOptions {
   using StringPair = std::pair;
   using OptionMap = llvm::StringMap;
 
+  struct QueryCheckValue {
+std::string Source;

DeNiCoN wrote:

It is used to dump the config back. As for example in the "Check config dump" 
test 
https://github.com/llvm/llvm-project/pull/123734/files#diff-3bffe1dc09dda3286149e7d1d46efbf11d0113d76262384b2b7c486677d11f22R47

I think I should add a comment line to that field, It wasn't obvious for me too 
at the beginning should the source be saved or not

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits

PiotrZSL wrote:

Additionally, consider implementing this as an "module" (like bugprone), simply 
implement factory that would look into config options that start with "custom-" 
and then parse options like: Source, Message, Language, and based on that 
construct checks, gain is that dependency on clang-query could be turn on/off 
in cmake file, and it could only result in registering or not that module. Also 
extending that functionality would be easier with things like TraverseMode and 
so on...

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL edited 
https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -292,6 +292,18 @@ An overview of all the command-line options:
 Checks   - Same as '--checks'. Additionally, the list 
of
globs can be specified as a list instead of 
a
string.
+ClangQueryChecks - List of key-value pairs. Key specifies a 
name
+   of the new check and value specifies a list
+   of matchers in the form of clang-query
+   syntax. Example:
+ ClangQueryChecks:
+   custom-check: |
+ let matcher varDecl(
+   hasTypeLoc(
+ typeLoc().bind("Custom message")

PiotrZSL wrote:

what if i want to get "name of fuction" in output message ?
or write check with multiple "bind" by simply using equalsBoundNode ?
Or generate "note"

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL requested changes to this pull request.

Additionally:
- more detailed documentation needed (entire section in documentation)
- i would name it CustomChecks instead
- need protection against check name conflicts, and maybe some protection for 
adding checks for current modules ? Maybe enforce "custom-" as prefix ?

Idea is fine, require some work.

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -38,6 +39,7 @@ clang_target_link_libraries(clangTidy
   clangSerialization
   clangTooling
   clangToolingCore
+  clangQuery

PiotrZSL wrote:

this introduces new dependency,
how it will impact for example clangd ?
will those checks work there ?

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -0,0 +1,53 @@
+// DEFINE: %{custom-call-yaml} = custom-call: 'm callExpr().bind(\"Custom 
message\")'
+//
+// DEFINE: %{custom-let-call-yaml} = custom-let-call: \" \
+// DEFINE: let expr varDecl( \
+// DEFINE:   hasType(asString(\\\"long long\\\")),   \
+// DEFINE:   hasTypeLoc(typeLoc().bind(\\\"Let message\\\")) \
+// DEFINE: ) \n  \
+// DEFINE: match expr\"
+//
+// DEFINE: %{full-config} = "{ClangQueryChecks: 
{%{custom-call-yaml},%{custom-let-call-yaml}}}"
+
+//Check single match expression
+// RUN: clang-tidy %s -checks='-*, custom-*'  \
+// RUN:   -config="{ClangQueryChecks: {%{custom-call-yaml}}}" \
+// RUN:   -- | FileCheck %s -check-prefix=CHECK-CUSTOM-CALL
+
+void a() {
+}
+
+// CHECK-CUSTOM-CALL: warning: Custom message [custom-call]
+// CHECK-CUSTOM-CALL-NEXT: a();{{$}}
+void b() {
+a();
+}
+
+//Check let with match expression
+// RUN: clang-tidy %s -checks='-*, custom-*'  \
+// RUN:   -config="{ClangQueryChecks: {%{custom-let-call-yaml}}}" \
+// RUN:   -- | FileCheck %s -check-prefix=CHECK-CUSTOM-LET
+void c() {
+// CHECK-CUSTOM-LET: warning: Let message [custom-let-call]
+// CHECK-CUSTOM-LET-NEXT: long long test_long_long = 0;{{$}}
+long long test_long_long_nolint = 0; //NOLINT(custom-let-call)
+long long test_long_long = 0;
+}
+
+//Check multiple checks in one config
+// RUN: clang-tidy %s -checks='-*, custom-*' \
+// RUN:   -config=%{full-config} \
+// RUN:   -- | FileCheck %s -check-prefixes=CHECK-CUSTOM-CALL,CHECK-CUSTOM-LET
+
+//Check multiple checks in one config but only one enabled
+// RUN: clang-tidy %s -checks='-*, custom-call' \
+// RUN:   -config=%{full-config}\
+// RUN:   -- | FileCheck %s -check-prefixes=CHECK-CUSTOM-CALL 
--implicit-check-not warning:
+
+//Check config dump
+// RUN: clang-tidy -dump-config -checks='-*, custom-*' \
+// RUN:   -config=%{full-config}   \
+// RUN:   -- | FileCheck %s -check-prefix=CHECK-CONFIG
+// CHECK-CONFIG: ClangQueryChecks:
+// CHECK-CONFIG-DAG: custom-let-call:
+// CHECK-CONFIG-DAG: custom-call:  |{{$[[:space:]]}} m callExpr().bind("Custom 
message")

PiotrZSL wrote:

missing test for --verify-config and --list-checks

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -292,6 +292,18 @@ An overview of all the command-line options:
 Checks   - Same as '--checks'. Additionally, the list 
of
globs can be specified as a list instead of 
a
string.
+ClangQueryChecks - List of key-value pairs. Key specifies a 
name

PiotrZSL wrote:

this should be documented in separate section, thats not a good place for 
example

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -0,0 +1,43 @@
+//===--- ClangQueryCheck.h - clang-tidy *- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGQUERYCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGQUERYCHECK_H
+
+#include "ClangTidyCheck.h"
+#include "clang/ASTMatchers/Dynamic/VariantValue.h"
+#include 
+
+namespace clang::query {
+class QuerySession;
+} // namespace clang::query
+
+namespace clang::tidy::misc {
+
+/// A check that matches a given matchers printing their binds as warnings
+class ClangQueryCheck : public ClangTidyCheck {
+  using MatcherVec = std::vector;
+
+public:
+  ClangQueryCheck(StringRef Name, ClangTidyContext *Context,
+  MatcherVec Matchers)
+  : ClangTidyCheck(Name, Context), Matchers(std::move(Matchers)) {}
+
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;

PiotrZSL wrote:

unnecessary limitation, maybe this should be an "config option"

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Piotr Zegar via cfe-commits


@@ -140,6 +140,9 @@ Improvements to clang-tidy
   :doc:`readability-redundant-access-specifiers 
`, 
CheckFirstDeclaration
   :doc:`readability-redundant-casting 
`, IgnoreTypeAliases
 
+- New :program:`clang-tidy` config property `ClangQueryChecks` that allows 
adding

PiotrZSL wrote:

merge with release notes in line 120

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread via cfe-commits

DeNiCoN wrote:

> Could it be a normal check under misc and pass the query in config instead of 
> create a new pattern?

It could be. But this will only be a single check. Having the ability to define 
multiple checks allows to have more fine control over them using NOLINT.

If multiple checks is defined in a single ClangTidyCheck then it should have 
the ability to report diagnostics under different names, but ClangTidyCheck 
base class allows to report diagnostics only with a single name passed in the 
constructor 
https://github.com/llvm/llvm-project/blob/5deb4ef9ab1144542d748f71235b029bed06dd26/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp#L31
 so some changes would have to be made in this case

And I think the approach with a single ClangTidyCheck and multiple checks 
defined will require some changes in config parsing too

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 commented:

Could it be a normal check under misc and pass the query in config instead of 
create a new pattern?

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy

Author: None (DeNiCoN)


Changes

This addresses the #107680. 

This pull request adds new config option that allows to define new checks using 
the [matchers](https://clang.llvm.org/docs/LibASTMatchersReference.html) syntax 
by incorporating clang-query parser

Example:
``` yaml
Checks: -*,custom-*
ClangQueryChecks:
  custom-math: |
let expr callExpr(
callee(functionDecl(
hasAnyName("acos", "asin", "atan", "atan2", "cos", "sin", "tan", 
"cosh", "sinh", "tanh", "ctan"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different math functions")

match expr

  custom-sort: |
match callExpr(
callee(functionDecl(
hasAnyName("sort", "nth_element", "partial_sort", "partition"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different sort functions")

  custom-long: |
match varDecl(
hasType(asString("long")),
hasTypeLoc(typeLoc().bind("Please use int instead of long"))
)
```

## Unimplemented ideas
- Let queries could be made global so that checks defined at the bottom could 
reuse let expressions from the checks at the top. Currently let query is local 
to the defined check
- File queries are disabled. But they can be used to implement the initial idea 
of using the clang-query files for custom checks. But as I understand clang 
tools use VFS interface to access files which might not correspond to a real 
filesystem and I haven't explored deep how to access that interface during 
clang-tidy configuration parsing so I left it as a FIXME comment

## Some thoughts on further possible work

If [transformers](https://clang.llvm.org/docs/ClangTransformerTutorial.html) 
parser will be implemented in the future(it 
[seems](https://github.com/llvm/llvm-project/blob/7acad6893b9b3b43e5e4a8e56404b1b19c07c79f/clang/include/clang/Tooling/Transformer/Parsing.h#L11-L12)
 like in the past there were some work on it being done by @ymand) it 
can be added to clang-query and as a consequence it can be made that custom 
checks defined by this new option will support automatic fixes being provided
```
makeRule(declRefExpr(to(functionDecl(hasName("MkX",
 changeTo(cat("MakeX")),
 cat("MkX has been renamed MakeX"));
```

---
Full diff: https://github.com/llvm/llvm-project/pull/123734.diff


10 Files Affected:

- (modified) clang-tools-extra/clang-tidy/CMakeLists.txt (+2) 
- (added) clang-tools-extra/clang-tidy/ClangQueryCheck.cpp (+30) 
- (added) clang-tools-extra/clang-tidy/ClangQueryCheck.h (+43) 
- (modified) clang-tools-extra/clang-tidy/ClangTidy.cpp (+8) 
- (modified) clang-tools-extra/clang-tidy/ClangTidyOptions.cpp (+84) 
- (modified) clang-tools-extra/clang-tidy/ClangTidyOptions.h (+8) 
- (modified) clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp (+14-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) clang-tools-extra/docs/clang-tidy/index.rst (+12) 
- (added) clang-tools-extra/test/clang-tidy/infrastructure/query-checks.cpp 
(+53) 


``diff
diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/CMakeLists.txt
index 93117cf1d6373a..76585b012d1742 100644
--- a/clang-tools-extra/clang-tidy/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
@@ -11,6 +11,7 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
 add_clang_library(clangTidy STATIC
   ClangTidy.cpp
   ClangTidyCheck.cpp
+  ClangQueryCheck.cpp
   ClangTidyModule.cpp
   ClangTidyDiagnosticConsumer.cpp
   ClangTidyOptions.cpp
@@ -38,6 +39,7 @@ clang_target_link_libraries(clangTidy
   clangSerialization
   clangTooling
   clangToolingCore
+  clangQuery
   )
 
 if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
diff --git a/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp 
b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
new file mode 100644
index 00..a9f46116f70899
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
@@ -0,0 +1,30 @@
+//===--- ClangQueryCheck.cpp - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ClangQueryCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+void ClangQueryCheck::registerMatchers(MatchFinder *Finder) {
+  for (const auto &Matcher : Matchers) {
+bool Ok = Finder->addDynamicMatcher(Matcher, this);
+assert(Ok && "Expected to get top leve

[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tools-extra

Author: None (DeNiCoN)


Changes

This addresses the #107680. 

This pull request adds new config option that allows to define new checks using 
the [matchers](https://clang.llvm.org/docs/LibASTMatchersReference.html) syntax 
by incorporating clang-query parser

Example:
``` yaml
Checks: -*,custom-*
ClangQueryChecks:
  custom-math: |
let expr callExpr(
callee(functionDecl(
hasAnyName("acos", "asin", "atan", "atan2", "cos", "sin", "tan", 
"cosh", "sinh", "tanh", "ctan"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different math functions")

match expr

  custom-sort: |
match callExpr(
callee(functionDecl(
hasAnyName("sort", "nth_element", "partial_sort", "partition"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different sort functions")

  custom-long: |
match varDecl(
hasType(asString("long")),
hasTypeLoc(typeLoc().bind("Please use int instead of long"))
)
```

## Unimplemented ideas
- Let queries could be made global so that checks defined at the bottom could 
reuse let expressions from the checks at the top. Currently let query is local 
to the defined check
- File queries are disabled. But they can be used to implement the initial idea 
of using the clang-query files for custom checks. But as I understand clang 
tools use VFS interface to access files which might not correspond to a real 
filesystem and I haven't explored deep how to access that interface during 
clang-tidy configuration parsing so I left it as a FIXME comment

## Some thoughts on further possible work

If [transformers](https://clang.llvm.org/docs/ClangTransformerTutorial.html) 
parser will be implemented in the future(it 
[seems](https://github.com/llvm/llvm-project/blob/7acad6893b9b3b43e5e4a8e56404b1b19c07c79f/clang/include/clang/Tooling/Transformer/Parsing.h#L11-L12)
 like in the past there were some work on it being done by @ymand) it 
can be added to clang-query and as a consequence it can be made that custom 
checks defined by this new option will support automatic fixes being provided
```
makeRule(declRefExpr(to(functionDecl(hasName("MkX",
 changeTo(cat("MakeX")),
 cat("MkX has been renamed MakeX"));
```

---
Full diff: https://github.com/llvm/llvm-project/pull/123734.diff


10 Files Affected:

- (modified) clang-tools-extra/clang-tidy/CMakeLists.txt (+2) 
- (added) clang-tools-extra/clang-tidy/ClangQueryCheck.cpp (+30) 
- (added) clang-tools-extra/clang-tidy/ClangQueryCheck.h (+43) 
- (modified) clang-tools-extra/clang-tidy/ClangTidy.cpp (+8) 
- (modified) clang-tools-extra/clang-tidy/ClangTidyOptions.cpp (+84) 
- (modified) clang-tools-extra/clang-tidy/ClangTidyOptions.h (+8) 
- (modified) clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp (+14-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) clang-tools-extra/docs/clang-tidy/index.rst (+12) 
- (added) clang-tools-extra/test/clang-tidy/infrastructure/query-checks.cpp 
(+53) 


``diff
diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/CMakeLists.txt
index 93117cf1d6373a..76585b012d1742 100644
--- a/clang-tools-extra/clang-tidy/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
@@ -11,6 +11,7 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
 add_clang_library(clangTidy STATIC
   ClangTidy.cpp
   ClangTidyCheck.cpp
+  ClangQueryCheck.cpp
   ClangTidyModule.cpp
   ClangTidyDiagnosticConsumer.cpp
   ClangTidyOptions.cpp
@@ -38,6 +39,7 @@ clang_target_link_libraries(clangTidy
   clangSerialization
   clangTooling
   clangToolingCore
+  clangQuery
   )
 
 if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
diff --git a/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp 
b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
new file mode 100644
index 00..a9f46116f70899
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
@@ -0,0 +1,30 @@
+//===--- ClangQueryCheck.cpp - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ClangQueryCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+void ClangQueryCheck::registerMatchers(MatchFinder *Finder) {
+  for (const auto &Matcher : Matchers) {
+bool Ok = Finder->addDynamicMatcher(Matcher, this);
+assert(Ok && "Expected to get t

[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread via cfe-commits

DeNiCoN wrote:

@AaronBallman could you be a reviewer please?

https://github.com/llvm/llvm-project/pull/123734
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add ClangQueryChecks config option (PR #123734)

2025-01-21 Thread via cfe-commits

https://github.com/DeNiCoN created 
https://github.com/llvm/llvm-project/pull/123734

This addresses the #107680. 

This pull request adds new config option that allows to define new checks using 
the [matchers](https://clang.llvm.org/docs/LibASTMatchersReference.html) syntax 
by incorporating clang-query parser

Example:
``` yaml
Checks: -*,custom-*
ClangQueryChecks:
  custom-math: |
let expr callExpr(
callee(functionDecl(
hasAnyName("acos", "asin", "atan", "atan2", "cos", "sin", "tan", 
"cosh", "sinh", "tanh", "ctan"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different math functions")

match expr

  custom-sort: |
match callExpr(
callee(functionDecl(
hasAnyName("sort", "nth_element", "partial_sort", "partition"),
anyOf(
hasDeclContext(namespaceDecl(hasName("std"))),
unless(hasParent(namespaceDecl()))
)
))
).bind("Please use different sort functions")

  custom-long: |
match varDecl(
hasType(asString("long")),
hasTypeLoc(typeLoc().bind("Please use int instead of long"))
)
```

## Unimplemented ideas
- Let queries could be made global so that checks defined at the bottom could 
reuse let expressions from the checks at the top. Currently let query is local 
to the defined check
- File queries are disabled. But they can be used to implement the initial idea 
of using the clang-query files for custom checks. But as I understand clang 
tools use VFS interface to access files which might not correspond to a real 
filesystem and I haven't explored deep how to access that interface during 
clang-tidy configuration parsing so I left it as a FIXME comment

## Some thoughts on further possible work

If [transformers](https://clang.llvm.org/docs/ClangTransformerTutorial.html) 
parser will be implemented in the future(it 
[seems](https://github.com/llvm/llvm-project/blob/7acad6893b9b3b43e5e4a8e56404b1b19c07c79f/clang/include/clang/Tooling/Transformer/Parsing.h#L11-L12)
 like in the past there were some work on it being done by @ymand) it can be 
added to clang-query and as a consequence it can be made that custom checks 
defined by this new option will support automatic fixes being provided
```
makeRule(declRefExpr(to(functionDecl(hasName("MkX",
 changeTo(cat("MakeX")),
 cat("MkX has been renamed MakeX"));
```

>From 148f94551d0660d434b6717c87cd68ec65ce6b82 Mon Sep 17 00:00:00 2001
From: DeNiCoN 
Date: Tue, 14 Jan 2025 07:35:38 +0100
Subject: [PATCH] [clang-tidy] Add ClangQueryChecks config option

---
 clang-tools-extra/clang-tidy/CMakeLists.txt   |  2 +
 .../clang-tidy/ClangQueryCheck.cpp| 30 +++
 .../clang-tidy/ClangQueryCheck.h  | 43 ++
 clang-tools-extra/clang-tidy/ClangTidy.cpp|  8 ++
 .../clang-tidy/ClangTidyOptions.cpp   | 84 +++
 .../clang-tidy/ClangTidyOptions.h |  8 ++
 .../clang-tidy/tool/ClangTidyMain.cpp | 15 +++-
 clang-tools-extra/docs/ReleaseNotes.rst   |  3 +
 clang-tools-extra/docs/clang-tidy/index.rst   | 12 +++
 .../infrastructure/query-checks.cpp   | 53 
 10 files changed, 257 insertions(+), 1 deletion(-)
 create mode 100644 clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/ClangQueryCheck.h
 create mode 100644 
clang-tools-extra/test/clang-tidy/infrastructure/query-checks.cpp

diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/CMakeLists.txt
index 93117cf1d6373a..76585b012d1742 100644
--- a/clang-tools-extra/clang-tidy/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
@@ -11,6 +11,7 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
 add_clang_library(clangTidy STATIC
   ClangTidy.cpp
   ClangTidyCheck.cpp
+  ClangQueryCheck.cpp
   ClangTidyModule.cpp
   ClangTidyDiagnosticConsumer.cpp
   ClangTidyOptions.cpp
@@ -38,6 +39,7 @@ clang_target_link_libraries(clangTidy
   clangSerialization
   clangTooling
   clangToolingCore
+  clangQuery
   )
 
 if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
diff --git a/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp 
b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
new file mode 100644
index 00..a9f46116f70899
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/ClangQueryCheck.cpp
@@ -0,0 +1,30 @@
+//===--- ClangQueryCheck.cpp - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ClangQueryCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"