<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Apr 18, 2013 at 3:49 PM, Thompson, John <span dir="ltr"><<a href="mailto:John_Thompson@playstation.sony.com" target="_blank">John_Thompson@playstation.sony.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="">Is there a way to find the association of an AST sub-tree with its enclosing preprocessor conditionals?<u></u><u></u></p>
<p class=""><u></u> <u></u></p>
<p class="">It seems there is not, and the conditional stack is transitory anyway.<u></u><u></u></p>
<p class=""><u></u> <u></u></p>
<p class="">Any ideas on a not-too-painful way to do this from a tool (i.e. modularize)?<u></u><u></u></p>
<p class=""><u></u> <u></u></p>
<p class="">Here’s one idea.  Set up a hook so that modularize could have a callback called whenever a preprocessor conditional block is entered or exited.  </p></div></div></blockquote><div style>That seems reasonable.</div>
<div style><br></div><div style>See clang::Preprocessor::addPPCallbacks and the PPCallbacks class itself <<a href="http://clang.llvm.org/doxygen/classclang_1_1PPCallbacks.html">http://clang.llvm.org/doxygen/classclang_1_1PPCallbacks.html</a>>.<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple">
<div><p class="">This callback would record the state of the conditional stack such that the tool could give
 messages referring to conditional source locations.  Also set up a hook so that a callback could be called whenever a declaration node is added to the AST.  (Is there already any sort of callback mechanism for when AST nodes are added?)  This callback would
 associate the declaration node with the conditional stack state.  (This raises the question of how to make the association.  Save it as an attribute?  Maintain an external table?)  </p></div></div></blockquote><div>You could use PPCallbacks to turn the transitory preprocessor callback events into a fixed data structure ("AST") representing the structure of preprocessor directives. I can think of many other places where a "PPAST" like that could be useful. Once that structure is obtained, then the association can be made by looking at the SourceLocation's/SourceRange's that Clang so religiously stores for the AST nodes. Source range intersection and subrange inclusion tests can be done efficiently using e.g. an augmented red-black tree (we might have something in ADT up to the job since these kinds of range/interval operations are reminiscent of the needs of register allocators (maybe ADT/IntervalMap.h?)).</div>
<div style> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple">
<div><p class="">When modularize finds its “not always provided” items, it could look up the
 association and display the preprocessor conditionals involved.<u></u><u></u></p>
<p class=""><u></u> </p></div></div></blockquote><div style>That would be nice.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple"><div><p class=""><u></u></p>
<p class="">Does it even make sense to attempt to something like this in modularize?  This has arisen because while running modularize on a large group of headers, I found it a little difficult to manually inspect the code to find the involved conditionals.<u></u><u></u></p>

<p class=""><u></u></p></div></div></blockquote><div style>I think it's a good idea if it translates to significantly improved usability of the tool.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple"><p class=""> <u></u></p>
<p class="">Or, maybe we don’t need to involve the AST at all.  Maybe for each header seen by modularize, just create a tree of the conditionals per file, linked with that of nested include files, and save the path through the enabled blocks, this
 through a callback mechanism.  Then, add a check to modularize that checks whether the collected paths for individual headers are the same.  If not, it can display the conflicting paths.</p></div></blockquote><div style>
 I really like this idea.</div><div style><br></div><div style>-- Sean Silva</div></div></div></div>