[llvm-bugs] [Bug 37188] New: QualifiedTypeLoc getSourceRange() excludes qualifiers

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Apr 20 12:54:35 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37188

            Bug ID: 37188
           Summary: QualifiedTypeLoc getSourceRange() excludes qualifiers
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: edward.brown.96 at live.co.uk
                CC: llvm-bugs at lists.llvm.org

The SourceRange returned by QualifierTypeLoc::getSourceRange excludes the
qualifiers.

This can be demonstrated using the following code as part of a clang-tidy
check.

void ConstPositionCheck::registerMatchers(MatchFinder *Finder) {
  Finder->addMatcher(varDecl(anything()).bind("var"), this);
}

CharSourceRange AsFileCharRange(const ASTContext& Context, const SourceRange
&Range) {
  return Lexer::makeFileCharRange(CharSourceRange::getTokenRange(Range),
                                  Context.getSourceManager(),
                                  Context.getLangOpts());
}

void ConstPositionCheck::check(const MatchFinder::MatchResult &Result) {
  if (auto *VarNode = Result.Nodes.getNodeAs<VarDecl>("var")) {
    const TypeSourceInfo *TSI = VarNode->getTypeSourceInfo();

    auto QualTypeLoc = TSI->getTypeLoc().getAs<QualifiedTypeLoc>();
    auto QualTypeLocRange = AsFileCharRange(*Result.Context,
QualTypeLoc.getSourceRange());

    llvm::errs() << "Qualified Type is\n\n";
    TSI->getType().dump();
    llvm::errs() << "\n it goes from \n";
    QualTypeLocRange.getBegin().dump(Result.Context->getSourceManager());
    llvm::errs() << "\n to \n";
    QualTypeLocRange.getEnd().dump(Result.Context->getSourceManager());
    llvm::errs() << "\n";

    auto UnqualTypeLoc = QualTypeLoc.getUnqualifiedLoc();
    auto UnqualTypeLocRange = AsFileCharRange(*Result.Context,
UnqualTypeLoc.getSourceRange());

    llvm::errs() << "Unqualified type goes from \n";
    UnqualTypeLocRange.getBegin().dump(Result.Context->getSourceManager());
    llvm::errs() << "\n to \n";
    UnqualTypeLocRange.getEnd().dump(Result.Context->getSourceManager());
    llvm::errs() << "\n";
  }
}

Running on this test .cpp file:

const int x = 10;
int const y = 11;

Produces the following output:

Qualified Type is

QualType 0x3e65031 'const int' const
`-BuiltinType 0x3e65030 'int'

 it goes from
/usr/local/src/build-clang/test.cpp:1:7
 to
/usr/local/src/build-clang/test.cpp:1:10
Unqualified type goes from
/usr/local/src/build-clang/test.cpp:1:7
 to
/usr/local/src/build-clang/test.cpp:1:10
Qualified Type is

QualType 0x3e65031 'const int' const
`-BuiltinType 0x3e65030 'int'

 it goes from
/usr/local/src/build-clang/test.cpp:2:1
 to
/usr/local/src/build-clang/test.cpp:2:4
Unqualified type goes from
/usr/local/src/build-clang/test.cpp:2:1
 to
/usr/local/src/build-clang/test.cpp:2:4

The ranges for qualified and unqualified types are the same.

This makes it difficult to determine the positions of the qualifiers or to
replace and entire type (including qualifiers) in the source code.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180420/5d390ba8/attachment.html>


More information about the llvm-bugs mailing list