<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 - CXXCtorInitializer::getSourceRange() returns the incorrect range"
   href="https://bugs.llvm.org/show_bug.cgi?id=47675">47675</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CXXCtorInitializer::getSourceRange() returns the incorrect range
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>11.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Tooling
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tiagomacarios@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24003" name="attach_24003" title="preprocessed SmallVector.cpp">attachment 24003</a> <a href="attachment.cgi?id=24003&action=edit" title="preprocessed SmallVector.cpp">[details]</a></span>
preprocessed SmallVector.cpp

Sometimes CXXCtorInitializer::getSourceRange() returns the incorrect range. I
could not get a small self-contained repro. To repro this you will need to
create a clang-tidy check and add the code below.

void TestCheck::registerMatchers(MatchFinder *Finder) {
  Finder->addMatcher(
      cxxConstructorDecl(
          allOf(hasBody(anything()),
hasAnyConstructorInitializer(isWritten())))
          .bind("constructorDecl"),
      this);
}

static bool IsProperlyTerminated(const llvm::StringRef Ref) {
  if (Ref.endswith(")") || Ref.endswith("}")) {
    return true;
  }
  return false;
}

void TestCheck::check(const MatchFinder::MatchResult &Result) {

  const CXXConstructorDecl *constructorDecl =
      Result.Nodes.getNodeAs<CXXConstructorDecl>("constructorDecl");

  if (!constructorDecl) {
    return;
  }

  for (CXXCtorInitializer *init : constructorDecl->inits()) {
    if (init->isWritten()) {

      /* CXXCtorInitializer::getSourceRange() seems to incorrectly return where
       * the Initializer ends. It seems that in certain cases it will report
the
       * initializer ending shorter of the actual end
       *
       *  expected:
       *  member ( parameter )
       *                      ^
       *
       *  current:
       *  member ( parameter )
       *                  ^
       */
      SourceRange Range = init->getSourceRange();
      const StringRef Text = clang::Lexer::getSourceText(
          CharSourceRange::getTokenRange(Range), *Result.SourceManager,
          Result.Context->getLangOpts());

      if (Text.empty() || IsProperlyTerminated(Text)) {
        continue;
      }

      assert(false && "not properly terminated");
    }
  }
}


In my case the assert below fired while compiling SmallVector.cpp. Here is the
initializer where it failed:

  SmallVectorBase(void *FirstEl, size_t TotalCapacity)
      : BeginX(FirstEl), Capacity(TotalCapacity) {}


The range reported was:

  BeginX(FirstEl

(The paren is missing)


I attached the preprocessed file from my machine. I am not sure but it looks
like the bug is not deterministic. While trying to create a self contained
repro the issue would not repro after I deleted unrelated parts of the file.</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>