[cfe-dev] How to correlate macro expansions with AST nodes?

Richard via cfe-dev cfe-dev at lists.llvm.org
Sun Jan 9 13:06:39 PST 2022


[Please reply *only* to the list and do not include my email directly
in the To: or Cc: of your reply; otherwise I will not see your reply.

Thanks.]

In article <00DEDF73-162C-4EA7-93D3-90D3ED411F42 at gmail.com>,
    David Rector via cfe-dev <cfe-dev at lists.llvm.org> writes:

> As you're aware there's no map from source locations to AST nodes, nor
> would that be a good use of memory in general; and while you could manually
> search the AST I suppose, that is a lot of work, given that you'd have to
> look at nodes in the right order etc.

I was thinking of writing an ASTVisitor.  At the moment I'm playing
around with a matcher for TranslationUnitDecl(); is there an AST node
at a higher level than that?

> So having exhausted the alternatives let's consider what I think is the
> ``right'' way to do it, given that this could be very helpful in other
> tools too: introduce a handle for ASTConsumers to call during
> ASTContext::Allocate<T>().  Something like this:
> [...]
> Then, for your case I *think* you could just keep track of the last macro
> expansion, and the last Decl/Type/StmtAllocateCallbacks, and just check if
> their source locations match --- probably a little more complexity
> involved, but not more than any other solution.

That's an interesting idea.  In my case, I don't think it's essential
that I be able to examine the nodes during parsing, but it feels like
less work than building the entire AST and visiting it after the fact.

> Worth a shot?

I'm going to prototype something with the existing matching framework
first and see how poorly that performs.

For context, this is what I'm working on:
Create modernize-enum-from-macros check
<https://github.com/llvm/llvm-project/issues/27408>

I'm less concerned about "torture" cases and more concerned with
common cases that occur in normal code.  Anything that helps migrate
away from macros (particularly now that modules are in C++20) is
useful.
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
            The Terminals Wiki <http://terminals-wiki.org>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>


More information about the cfe-dev mailing list