<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hi Miklos,</p>
<p>thanks for your hint. What do you mean by "Rewriting in header
files happens out of the box..."?</p>
<p>When I rewrite with my tools some definition in a header file
I'll get an error message from the rewriter:</p>
<p><<<INVALID SOURCE LOCATION >>></p>
<p><br>
</p>
<p>My code snippet looks something like this:</p>
<p><br>
</p>
<p><font face="Courier New, Courier, monospace"> bool
TransformFunctionReturnTypeDef(FunctionDecl *funcdecl) <br>
{<br>
if (funcdecl->getPreviousDecl()) <br>
{<br>
TypeSourceInfo *TI =
funcdecl->getTypeSourceInfo();<br>
TypeLoc TL = TI->getTypeLoc();<br>
FunctionTypeLoc FTL =
TL.getAsAdjusted<FunctionTypeLoc>();<br>
<br>
clang::SourceLocation l1 = FTL.getLParenLoc();<br>
clang::SourceLocation l2 = FTL.getRParenLoc();<br>
<br>
int offset = Lexer::MeasureTokenLength(l1,<br>
TheRewriter.getSourceMgr(),<br>
TheRewriter.getLangOpts());<br>
<br>
int offset2 = Lexer::MeasureTokenLength(l2,<br>
TheRewriter.getSourceMgr(),<br>
TheRewriter.getLangOpts());<br>
<br>
SourceLocation END1 = l1.getLocWithOffset(offset);<br>
SourceLocation END2 = l2.getLocWithOffset(offset2 -
1);<br>
SourceRange range(END1, END2);<br>
std::stringstream SSBefore;<br>
<br>
if (auto typ =
dyn_cast_or_null<PointerType>(funcdecl->getReturnType().getTypePtr()))
{<br>
SSBefore << ", " <<
funcdecl->getReturnType().getAsString() << "
new_param";<br>
}<br>
else {<br>
SSBefore << ", " <<
funcdecl->getReturnType().getAsString() << "
*new_param";<br>
}<br>
<br>
TheRewriter.ReplaceText(range, SSBefore.str());<br>
}<br>
<br>
return true;<br>
}<br>
</font></p>
<p>Marcel<br>
</p>
<p><br>
</p>
<p> </p>
<br>
<br>
<div class="moz-cite-prefix">Am 07.09.2017 um 14:28 schrieb Miklos
Vajna:<br>
</div>
<blockquote type="cite"
cite="mid:20170907122811.GG14795@collabora.co.uk">
<pre wrap="">Hi,
On Wed, Sep 06, 2017 at 08:01:30PM +0200, Marcel Schaible via cfe-dev <a class="moz-txt-link-rfc2396E" href="mailto:cfe-dev@lists.llvm.org"><cfe-dev@lists.llvm.org></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">my tool changes the signatures of some annotated C functions in .c files.
Now I am facing the problem that I have to propaged this changes into
the corresponding
header file.
How can I do this? Can I use something like ASTConsumer/RecursiveAstVisitor?
Any hint and/or idea is welcome. Is there some example code available?
</pre>
</blockquote>
<pre wrap="">
Look at clang-tools-extra for examples. Rewriting in header files
happens out of the box, but if you want to check if your location is in
the "main" file or in a header file, you can use the source manager's
isInMainFile() for that purpose. See e.g.
tools/clang/tools/extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp:55
for a place where that happens.
Regards,
Miklos
</pre>
</blockquote>
<br>
</body>
</html>