<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">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.<div class=""><br class=""></div><div class="">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:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-kerning: none" class="">```</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">class ASTContext {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  ...</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  bool AnyAllocateCallbacks;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  … DeclAllocateCallbacks; //a DeclVisitor or chain of them a la PPCallbacks</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  … TypeAllocateCallbacks; //a TypeVisitor “"</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  … StmtAllocateCallbacks; //a StmtVisitor “"</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; min-height: 15px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">public:</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  template<typename T, typename = std::enable_if_t<std::is_base_of_v<Decl, T>>></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  void DispatchAllocateCallback(T *D) {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">    if (DeclAllocateCallbacks)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">      DeclAllocateCallbacks->Visit(D);</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  }</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  //…Same for Type, Stmt</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; min-height: 15px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  void DispatchAllocateCallback(</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  template <typename T> T *Allocate(size_t Num = 1) const {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">    T *res = static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">    DispatchAllocateCallbacks(res);</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">    return res;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">  }</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-kerning: none" class="">};</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">```</span></div></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">An ASTConsumer would add its Decl/Type/StmtAllocateCallbacks via some ASTConsumer::handle* virtual method called before parsing, i.e. same way it would add PPCallbacks.</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal;" class="">I can’t imagine this would add noticeably to compilation times when there are no callbacks defined, but that would be the key measurement.</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div class=""><div>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.</div><div><br class=""></div><div>More importantly, this would be a big step toward e.g. enabling ASTMatchers to work *during* parsing, for consumers that wanted to see e.g. Sema or Parser state info at the time the node was created.  Maybe doing that would even be trivial — if it were, then you could use your old solution and IdentifierInfo::hasMacroDefinitinion/Preprocessor::isMacroDefined.</div><div><br class=""></div><div>Worth a shot?</div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 8, 2022, at 7:37 PM, Richard via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">#<br class="">Hi Team,<br class=""><br class="">If I'm writing a clang-tidy check that focuses on replacing macros<br class="">with a more suitable modern construct, it's easy to analyze the macro<br class="">definitions by using the PP callbacks.  From the callbacks I can also<br class="">monitor the macro expansions, but the callback only gives me a<br class="">SourceRange for the expansion.<br class=""><br class="">Is there a way I can correlate the SourceRange to the appropriate<br class="">nodes in the AST for further analysis?<br class=""><br class="">Thanks,<br class="">-- Richard<br class=""><br class="">-- <br class="">"The Direct3D Graphics Pipeline" free book <<a href="http://tinyurl.com/d3d-pipeline" class="">http://tinyurl.com/d3d-pipeline</a>><br class="">             The Terminals Wiki <<a href="http://terminals-wiki.org" class="">http://terminals-wiki.org</a>><br class="">     The Computer Graphics Museum <<a href="http://ComputerGraphicsMuseum.org" class="">http://ComputerGraphicsMuseum.org</a>><br class="">  Legalize Adulthood! (my blog) <<a href="http://LegalizeAdulthood.wordpress.com" class="">http://LegalizeAdulthood.wordpress.com</a>><br class="">_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></div></div></blockquote></div><br class=""></div></div></body></html>