<div dir="ltr"><div><div><div><div><div><div><div>Note that this sort of problem needs to be solved in a "context-aware" way. Assuming your "extrainclude.h" contains something that you want to stick into the global context of the file, it needs to cope with:<br><br></div>#include <stdio.h><br>...<br></div>void func()<br>{<br></div>#define SOME_MACRO ...<br></div>#include "myheader.h"<br></div>... // more code here ... <br>}<br><br></div>You most likely don't want to insert your header after the inclusion of "myheader.h" in this case. Likewise, if you want to for example wrap malloc & free with macros for debugging [or some other functions], you will need to avoid this:<br><br></div><div>#include <stdio.h><br>...<br>int main()<br>{<br>...</div><div>}<br><br></div><div>#include "mytable.h"<br><br></div><div>Another interesting scenario is if you have #if along with your includes;<br><br>#include <stdio.h><br>...</div><div>#if WINDOWS</div><div>#include "some_windows_file.h"<br></div><div>#else<br></div><div>#include "some_linux_file.h"<br></div><div>#endif<br><br></div><div>You don't want to insert your header file after only the linux include... :)<br></div><div><br></div><div>Maybe not a very common style of programming, but it does happen that some header files are included "last" in a file.<br><br></div><div>I know that I've not told you how to solve the problem, but if you want to solve this for generic cases, you will need to consider these things as well as "which is the last of the includes". <br><br></div><div>For the simple case, a small script in awk, python or something similar is probably all you actually need - but for the more complex cases, you need a whole lot more logic, and actually understand "where you are" in the source-file.<br></div><div><br>--</div><div>Mats<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 8 December 2017 at 09:24, 于 秋沣 via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div id="m_-6357607392208984875divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p style="margin-top:0;margin-bottom:0"><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">Hi All,</span></p>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
I'm working on a project which given a C/C++ source code, I need to find the last #include and insert one additional #include right after that. For example, suppose I have a file called Test.c which looks like this:</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
#include <stdio.h></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
#include <stdlib.h></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
int main(){</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
// some code here...</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
}</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
I want to get the location of the #include <stdlib.h> and insert one extra #include, then the code should be like this:</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
#include <stdio.h></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
#include <stdlib.h></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
#include "extraInclude.h"</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
int main(){</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
// some code here....</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
}</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
I feel like the right class I need to use is clang::Preprocessor and clang::PPCallbacks, but I haven't figured out a way to implement this.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
Any help would be appreciated.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
<br>
</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small">
Patrick</div>
<br>
<p></p>
</div>
</div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>