This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Interactive AST matchers with #pragma clang query
Needs ReviewPublic

Authored by sammccall on May 22 2023, 3:29 PM.



The idea: you can type in AST matchers, and clangd shows you matches
within the document.

Syntax is like:

#pragma clang query varDecl().bind("var")


  • matches are shown within the document as diagnostics
  • find-references on the pragma will produce a list of matches
  • errors within matchers are shown as diagnostics
  • code completion is available within matchers


There are some rough edges here, e.g. the diagnostics are warnings and
have no LSP diagnostic code, the diagnostic text for matcher errors
isn't ideal etc.
I think this is a good start as-is and we can work out if we want to
improve these cases later, if this feature ends up being useful.

Diff Detail

Event Timeline

sammccall created this revision.May 22 2023, 3:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 3:29 PM
sammccall requested review of this revision.May 22 2023, 3:29 PM
sammccall updated this revision to Diff 524519.May 22 2023, 3:41 PM
sammccall edited the summary of this revision. (Show Details)

Add demo link

sammccall added a comment.EditedMay 22 2023, 3:46 PM

@kadircet this is a slightly silly feature that I put together on vacation

I'd like your call on whether this is something we should have (take a look at the demo). If so, feel free to hand off the review.

It's definitely a power-user feature and nigh undiscoverable. I'd plan to write some docs, but even so I'm not sure how much use it will get.

Endill added a subscriber: Endill.May 23 2023, 3:21 AM

This is an interesting feature!
There's a related #pragma clang __debug dump, that dumps name lookup result or a bit of AST for expression you pass to it ( I think clangd might be interested to support that pragma as well.

nridge added a subscriber: nridge.May 25 2023, 10:23 PM