<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55500>55500</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang ASTImporter infinite loop crash
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          balazske
      </td>
    </tr>
</table>

<pre>
    The following script can be used to reproduce the problem:
(Set CLANG to the correct executable.)
The following commit (and later) contains the bug: 285b39a31ec63a0253fa88c3c61f447712e2f131 `Mon May 9 22:46:34 2022 -0700 "Revert "[NFC][lldb][trace] Use uint64_t when decoding and enconding json"`
```
#/bin/bash

cat > EPoll.cpp << EOF
namespace ns {
struct Foo {
  void wait();
};

template <typename> struct type {};

auto f() {
  auto lambda = [] {};
  return type<decltype(lambda)>{};
}

void Foo::wait() {}
} // namespace ns
EOF

cat > Client.cpp << EOF
namespace ns {
struct Foo {
  void wait();
};

void e(Foo *it) { it->wait(); }
} // namespace ns
EOF

CLANG=clang
EXTDEFTOOL=$CLANG-extdef-mapping

$CLANG -c -x c++ -pedantic -std=c++17 EPoll.cpp -emit-ast -w -o EPoll.cpp.ast

$EXTDEFTOOL EPoll.cpp -- -c -x c++ -pedantic -std=c++17 > externalDefMap.txt
sed -i 's:.cpp:.cpp.ast:g' externalDefMap.txt

$CLANG \
--analyze -Xclang -analyzer-checker=core -Xclang -analyzer-config -Xclang experimental-enable-naive-ctu-analysis=true -Xclang -analyzer-config -Xclang \
ctu-dir=. -pedantic -std=c++17 Client.cpp -fno-crash-diagnostics
```

There is a reference to a declaration inside a function from the function signature at function `f`. The problem is that this reference is hidden inside a template argument in the (computed) return type. This case is not recognized by `ASTImporter` for special handling. Making this work in general case is difficult, an AST visitor or matcher could be used to find such references in every case (see functionn `hasAutoReturnTypeDeclaredInside` in ASTImporter.cpp).
</pre>
<img width="1px" height="1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