[cfe-dev] QualType question

Tim Halloran via cfe-dev cfe-dev at lists.llvm.org
Fri Feb 5 12:38:15 PST 2016


I'm trying to refactor in C++:

   using namespace std;
   string sp = " ";

into

   std::string sp = " ";

However when I examine the VarDecl node it is not obvious how to get to the
namespace the "typedef <snip> sting" is declared in.

My code gets the QualType, in QT from the VarDecl then the trouble starts:

** QT.getAsString() =  string
 ** QT.getCanonicalType().getAsString() =  class
std::__cxx11::basic_string<char>
 ** QT.getUnqualifiedType().getAsString() =  string
 ** ds.getAsString() =  basic_string<char>
 ** QT.getBaseTypeIdentifier() = basic_string
 ** VarDecl: string sp = " "

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!

Code that dumped the above:

/// Handle types in variable declarations
///   string s;
///  becomes
///    std::string s;
void UsingCheck::handleVarDecl(
    const clang::ast_matchers::MatchFinder::MatchResult &Result) {
  const VarDecl *VD = Result.Nodes.getNodeAs<VarDecl>("varDecl");
  if (!VD)
    return;

  clang::SourceLocation Loc{VD->getLocStart()};
  if (VD->isImplicit() || !Loc.isValid() ||
      utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
                                       HeaderFileExtensions))
    return;
  clang::SourceRange Range{VD->getSourceRange()};
  clang::QualType QT = VD->getType();

  const clang::Type *UT = QT.getTypePtr();
  if (!UT)
    return;

  if (UT->isBuiltinType())
    return;

  std::cerr << " ** QT.getAsString() =  " << QT.getAsString() << "\n";
  std::cerr << " ** QT.getCanonicalType().getAsString() =  " <<
QT.getCanonicalType().getAsString() << "\n";
  std::cerr << " ** QT.getUnqualifiedType().getAsString() =  " <<
QT.getUnqualifiedType().getAsString() << "\n";

  clang::QualType ds = UT->getLocallyUnqualifiedSingleStepDesugaredType();

  std::cerr << " ** ds.getAsString() =  " << ds.getAsString() << "\n";

  const clang::IdentifierInfo *idinfo = QT.getBaseTypeIdentifier();
  if (idinfo)
    std::cerr << " ** QT.getBaseTypeIdentifier() = " <<
idinfo->getName().str()
              << "\n";

  clang::SourceRange RangeForString{Range};
  CharSourceRange CSR = Lexer::makeFileCharRange(
      CharSourceRange::getTokenRange(RangeForString), *Result.SourceManager,
      Result.Context->getLangOpts());
  std::string possibleSemi = Lexer::getSourceText(CSR,
*Result.SourceManager,

Result.Context->getLangOpts())
                                 .str();
  std::cerr << " ** VarDecl: " << possibleSemi << "\n";
}


-- 
Tim Halloran
SureLogic, Inc.
5808 Forbes Avenue, Pittsburgh PA 15217-1602
(412) 722-3338
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160205/1e114177/attachment.html>


More information about the cfe-dev mailing list