<div dir="ltr">I'm trying to refactor in C++:<div><br></div><div> using namespace std;</div><div> string sp = " ";<br></div><div><br></div><div>into</div><div><br></div><div> std::string sp = " ";</div><div><br></div><div>However when I examine the VarDecl node it is not obvious how to get to the namespace the "typedef <snip> sting" is declared in.</div><div><br></div><div>My code gets the QualType, in QT from the VarDecl then the trouble starts:</div><div><br></div><div><div>** QT.getAsString() = string</div><div> ** QT.getCanonicalType().getAsString() = class std::__cxx11::basic_string<char></div><div> ** QT.getUnqualifiedType().getAsString() = string</div><div> ** ds.getAsString() = basic_string<char></div><div> ** QT.getBaseTypeIdentifier() = basic_string</div><div> ** VarDecl: string sp = " "</div><div><br></div><div>below is the code, but the problem seems to be I cannot bind to a declaration of the typedef. Any suggestions or code to look at that does this would be most appreciated. Thanks!</div><div><br></div><div>Code that dumped the above:</div><div><br></div><div><div>/// Handle types in variable declarations</div><div>/// string s;</div><div>/// becomes</div><div>/// std::string s;</div><div>void UsingCheck::handleVarDecl(</div><div> const clang::ast_matchers::MatchFinder::MatchResult &Result) {</div><div> const VarDecl *VD = Result.Nodes.getNodeAs<VarDecl>("varDecl");</div><div> if (!VD)</div><div> return;</div><div><br></div><div> clang::SourceLocation Loc{VD->getLocStart()};</div><div> if (VD->isImplicit() || !Loc.isValid() ||</div><div> utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,</div><div> HeaderFileExtensions))</div><div> return;</div><div> clang::SourceRange Range{VD->getSourceRange()};</div><div> clang::QualType QT = VD->getType();</div><div><br></div><div> const clang::Type *UT = QT.getTypePtr();</div><div> if (!UT)</div><div> return;</div><div><br></div><div> if (UT->isBuiltinType())</div><div> return;</div><div><br></div><div> std::cerr << " ** QT.getAsString() = " << QT.getAsString() << "\n";</div><div> std::cerr << " ** QT.getCanonicalType().getAsString() = " << QT.getCanonicalType().getAsString() << "\n";</div><div> std::cerr << " ** QT.getUnqualifiedType().getAsString() = " << QT.getUnqualifiedType().getAsString() << "\n";</div><div><br></div><div> clang::QualType ds = UT->getLocallyUnqualifiedSingleStepDesugaredType();</div><div><br></div><div> std::cerr << " ** ds.getAsString() = " << ds.getAsString() << "\n";</div><div><br></div><div> const clang::IdentifierInfo *idinfo = QT.getBaseTypeIdentifier();</div><div> if (idinfo)</div><div> std::cerr << " ** QT.getBaseTypeIdentifier() = " << idinfo->getName().str()</div><div> << "\n";</div><div><br></div><div> clang::SourceRange RangeForString{Range};</div><div> CharSourceRange CSR = Lexer::makeFileCharRange(</div><div> CharSourceRange::getTokenRange(RangeForString), *Result.SourceManager,</div><div> Result.Context->getLangOpts());</div><div> std::string possibleSemi = Lexer::getSourceText(CSR, *Result.SourceManager,</div><div> Result.Context->getLangOpts())</div><div> .str();</div><div> std::cerr << " ** VarDecl: " << possibleSemi << "\n";</div><div>}</div></div><div><br></div><div><br></div>-- <br><div class="gmail_signature">Tim Halloran<br>SureLogic, Inc.<br>5808 Forbes Avenue, Pittsburgh PA 15217-1602<br>(412) 722-3338</div>
</div></div>