<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - QualifiedTypeLoc getSourceRange() excludes qualifiers"
   href="https://bugs.llvm.org/show_bug.cgi?id=37188">37188</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>QualifiedTypeLoc getSourceRange() excludes qualifiers
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>edward.brown.96@live.co.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>