[NFC] Cleanup the overload of ASTImporter::import()
authorBalazs Benics <balazs.benics@sigmatechnology.se>
Thu, 30 Sep 2021 09:53:08 +0000 (11:53 +0200)
committerBalazs Benics <balazs.benics@sigmatechnology.se>
Thu, 30 Sep 2021 09:53:08 +0000 (11:53 +0200)
commite5e0e00831ba093639edbd443b6e05b3118f8930
tree8d36fcaf874aa43366dafa970ddcedcd8a3a44a8
parent230a6edb49c31e2c96613f5b7deefd4f4faf9a6d
[NFC] Cleanup the overload of ASTImporter::import()

This patch aims to address the comment of a previous review:
https://reviews.llvm.org/D109237#inline-1040678

The original problem was the following:
  `T` is substituted by `clang::Type`

  Expected<T *> import(T *From) {
    auto ToOrErr = Importer.Import(From);
    //             ^^^^^^^^^^^^^^^^^^^^^
    if (!ToOrErr)
      return ToOrErr.takeError();
    return cast_or_null<T>(*ToOrErr);
    //     ^^^^^^^^^^^^^^^^^^^^^^^^^
  }

`Importer.Import()` operates on `const Type *`, thus returns `const Type *`.
Later, at the return statement, we will try to construct an `Expected<Type*>`
from a `const Type *`, which failed with a miserable error message.

In all other cases `importer.Import()` results in a non-const version,
so everything works out just fine, but for `clang::type`s, we should
really return a const version.

So, in case of `T` is a subclass of `clang::Type`, it will return a
`Exprected<const T*>` instead.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D109269
clang/lib/AST/ASTImporter.cpp