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

            Bug ID: 47675
           Summary: CXXCtorInitializer::getSourceRange() returns the
                    incorrect range
           Product: clang
           Version: 11.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Tooling
          Assignee: unassignedclangb...@nondot.org
          Reporter: tiagomacar...@gmail.com
                CC: llvm-bugs@lists.llvm.org

Created attachment 24003
  --> https://bugs.llvm.org/attachment.cgi?id=24003&action=edit
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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to