[llvm-bugs] [Bug 30338] New: functionDecl->getLocation of specializations incorrectly points at template definition

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 9 11:47:31 PDT 2016


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

            Bug ID: 30338
           Summary: functionDecl->getLocation of specializations
                    incorrectly points at template definition
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: lukasza at chromium.org
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

This is almost, but not quite similar to
https://llvm.org/bugs/show_bug.cgi?id=29145

Expected behavior:

    1. The FunctionDecl corresponding to
           template void foo<int>(const int& i);
       should have getLocation point at line 5.

    2. Iterating/matching over all FunctionDecls and generating replacements
       for f>getLocation() should cover all occurrences of the function name.

Actual behavior:

    1. There are 2 FunctionDecls.
       Dump of one of them corresponds to line 3 of the input.
       Dump of second of them corresponds to line 5 of the input, but
getLocation incorrectly says "input.cc:3"

    2. Generating replacements based on FunctionDecl::getLocation() would
result in:

          template <typename T>
          void RenamedFoo(const T& t) {}  // <- rename happens correctly here.

          // No rename below, because of wrong source locations in the AST:
          template void foo<int>(const int& i);  

    3. Output of the repro program below:

        ---------------------------------
        parm->getName()         : t
        parm->getLocation()     : 'input.cc:3:25'
        func->getLocation()     : 'input.cc:3:12'
        func->dump()            :
        FunctionDecl 0x3a20ca0 <input.cc:3:7, col:29> col:12 foo 'void (const T
&)'
        |-ParmVarDecl 0x3a20b98 <col:16, col:25> col:25 t 'const T &'
        `-CompoundStmt 0x3a20de0 <col:28, col:29>
        ---------------------------------
        parm->getName()         : t
        parm->getLocation()     : 'input.cc:3:25'
        func->getLocation()     : 'input.cc:3:12'
        func->dump()            :
        FunctionDecl 0x3a21190 <input.cc:3:7, col:29> col:12 foo 'void (const
int &)'
        |-TemplateArgument type 'int'
        |-ParmVarDecl 0x3a21108 <col:16, col:25> col:25 t 'const int &'
        `-CompoundStmt 0x3a20de0 <col:28, col:29>

Repro:

#include <memory>

#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"

using namespace clang::ast_matchers;

class ReproMatchCallback : public MatchFinder::MatchCallback {
 public:
  void run(const MatchFinder::MatchResult& result) override {
    const clang::FunctionDecl* func =
        result.Nodes.getNodeAs<clang::FunctionDecl>("func");
    const clang::ParmVarDecl* parm =
        result.Nodes.getNodeAs<clang::ParmVarDecl>("parm");
    if (!func || !parm)
      return;

    llvm::outs() << "---------------------------------\n";
    llvm::outs() << "parm->getName()         : " << parm->getNameAsString() <<
"\n";
    llvm::outs() << "parm->getLocation()     : '"
                 << parm->getLocation().printToString(
                        result.Context->getSourceManager())
                 << "'\n";
    llvm::outs() << "func->getLocation()     : '"
                 << func->getLocation().printToString(
                        result.Context->getSourceManager())
                 << "'\n";
    llvm::outs() << "func->dump()            :\n";
    func->dump();
  }
};

int main(int argc, const char* argv[]) {
  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmParser();
  llvm::cl::OptionCategory category("Repro.");
  clang::tooling::CommonOptionsParser options(argc, argv, category);
  clang::tooling::ClangTool tool(options.getCompilations(),
                                 options.getSourcePathList());

  const char* code = R"code(                 // line 1
      template <typename T>                  // line 2
      void foo(const T& t) {}                // line 3
                                             // line 4
      template void foo<int>(const int& i);  // line 5
  )code";

  auto matcher =
      id("func", functionDecl(hasAnyParameter(id("parm", parmVarDecl()))));

  MatchFinder match_finder;
  ReproMatchCallback match_callback;
  match_finder.addMatcher(matcher, &match_callback);
  std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
      clang::tooling::newFrontendActionFactory(&match_finder);
  clang::tooling::runToolOnCode(factory->create(), code);
  return 0;
}

-- 
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/20160909/b12ea67b/attachment.html>


More information about the llvm-bugs mailing list