Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is invalid SourceLocation in clang and how to interact with it?
    text
    copied!<p> I need to replace some variable declarations with others.</p> <p>For example</p> <pre class="lang-c++ prettyprint-override"><code>int a = 5; </code></pre> <p>becomes </p> <pre class="lang-c++ prettyprint-override"><code>T&lt;int&gt; a = 5; </code></pre> <p>More generic </p> <pre class="lang-c++ prettyprint-override"><code>X a = _const; </code></pre> <p>becomes </p> <pre class="lang-c++ prettyprint-override"><code>T&lt;X&gt; a = _const; </code></pre> <p>I tried to implement an ASTVisitor descendant in clang:</p> <pre class="lang-c++ prettyprint-override"><code>bool VisitVarDecl(Decl *f) { if (VarDecl* VD = dyn_cast_or_null&lt;VarDecl&gt;(f)){ Expr* init = VD-&gt;getInit(); SourceRange definition = VD-&gt;getDefinition()-&gt;getSourceRange(); FullSourceLoc exprLoc = ctx.getFullLoc(init-&gt;getLocStart()); FullSourceLoc vLoc = ctx.getFullLoc(VD-&gt;getLocStart()); ... </code></pre> <p>I want to replace the definition of variable in the next way:</p> <pre class="lang-c++ prettyprint-override"><code>TheRewriter.ReplaceText(VD-&gt;getLocStart(), exprLoc.getSpellingColumnNumber()-vLoc.getSpellingColumnNumber(), someSting); </code></pre> <p>But any call exprLoc.getSpellingColumnNumber() leads to segmentation fault. For example I just try to print exprLoc.getSpellingColumnNumber():</p> <pre class="lang-c++ prettyprint-override"><code>llvm::outs()&lt;&lt; "init start at " &lt;&lt;exprLoc.getSpellingColumnNumber(&amp;isValid) &lt;&lt;" is "&lt;&lt;(isValid?"valid ":"invalid ") &lt;&lt;", decl start at " &lt;&lt;vLoc.getSpellingColumnNumber() &lt;&lt;".\n"; </code></pre> <p>And output is init start at 9 is invalid , decl start at 1. &lt;…> Segmentation fault</p> <p>What does it mean “invalid” SourceLocation and how to interact with it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload