llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Remove implicit nodes from AST when using AST Matchers

Open steveire opened this issue 8 years ago • 5 comments

Bugzilla Link 39522
Version unspecified
OS Linux
CC @JonasToth,@LebedevRI,@ymand

Extended Description

The Clang AST has many implicit nodes which are not so useful when writing AST Matchers. They cause confusion for newcomers. ASTMatchFinder deliberately includes them when traversing the AST with RAV. It should instead use the default behavior of RAV, which is to exclude those nodes. This also requires some changes to the ASTMatchFinder itself.

$ cat struct.cpp

class A { int foo(); };

#ifdef DEFINE_B_STRUCT class B { virtual int foo(); }; #endif

$ cat cmds.txt

m functionDecl()

set output dump

m functionDecl()

clang-query-6.0 -f cmds.txt struct.cpp -- -DDEFINE_B_STRUCT

Match #​1:

struct.cpp:4:2: note: "root" binds here int foo(); ^~~~~~~~~

Match #​2:

struct.cpp:10:3: note: "root" binds here virtual int foo(); ^~~~~~~~~~~~~~~~~

Match #​3:

Match #​4:

Match #​5:

struct.cpp:8:7: note: "root" binds here class B ^ 5 matches.

Match #​1:

Binding for "root": CXXMethodDecl 0x55df7fd37458 <struct.cpp:4:2, col:10> col:6 foo 'int ()'

Match #​2:

Binding for "root": CXXMethodDecl 0x55df7fd37720 <struct.cpp:10:3, col:19> col:15 foo 'int ()' virtual

Match #​3:

Binding for "root": CXXMethodDecl 0x55df7fd37818 <struct.cpp:8:7, > col:7 implicit constexpr operator= 'B &(const B &)' inline default noexcept-unevaluated 0x55df7fd37818 `-ParmVarDecl 0x55df7fd37940 col:7 col:7 'const B &'

Match #​4:

Binding for "root": CXXMethodDecl 0x55df7fd379d8 <struct.cpp:8:7, > col:7 implicit constexpr operator= 'B &(B &&)' inline default noexcept-unevaluated 0x55df7fd379d8 `-ParmVarDecl 0x55df7fd37b00 col:7 col:7 'B &&'

Match #​5:

Binding for "root": CXXDestructorDecl 0x55df7fd37b88 struct.cpp:8:7 col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x55df7fd37b88

5 matches.

steveire avatar Nov 01 '18 19:11 steveire

Could we add a new matcher (e.g. ignoringImplicitDescendants(inner_matchers...)) that ignores all implicit nodes when executing the inner matchers? This would preserve the default behavior and explicitly mark in the code when this ignoring is happening.

ymand avatar Nov 13 '18 15:11 ymand

Hi Stephen,

my opinion on the proposal is, that the default should not change, even though it is annoying and I personally hated it already, too :)

We could document is louder in the writing a clang-tidy check page, but clang-tidy should work on the AST as-is and not ignore some aspects of it because it is easier for smaller checks.

Usually, once you have a check in review these things can get figured out.

JonasToth avatar Nov 04 '18 21:11 JonasToth

ASTMatchFinder deliberately includes them when traversing the AST with RAV.

Deliberately suggests that it was a concise decision? Were you able to track down where it was made? Are there any details, reasoning?

They cause confusion for newcomers. It should instead use the default behavior of RAV, which is to exclude those nodes. Are you confident that such functionality isn't required by non-novices (that is what everyone, including novices, eventually becomes with time & expirience.)

Also, currently, there are ignoringImpCasts(), ignoringImplicit(), ignoringParenImpCasts(). If they are suddenly the default, how does one get back the previous behaviour?

LebedevRI avatar Nov 01 '18 20:11 LebedevRI

See http://lists.llvm.org/pipermail/cfe-dev/2018-November/060034.html

steveire avatar Nov 01 '18 20:11 steveire

This is already fixed, now you can provide TK_IgnoreUnlessSpelledInSource on both matcher level in code and in clang-query with set traversal. Done by 246b428fb3b5d58685064966bcea6592f8839b7e

PiotrZSL avatar Aug 08 '23 05:08 PiotrZSL