[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-06-11 Thread Gabor Marton via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL363062: [ASTImporter] Fix unhandled cases in 
ASTImporterLookupTable (authored by martong, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D62064?vs=204048=204049#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064

Files:
  cfe/trunk/lib/AST/ASTImporter.cpp
  cfe/trunk/lib/AST/ASTImporterLookupTable.cpp
  cfe/trunk/unittests/AST/ASTImporterTest.cpp

Index: cfe/trunk/lib/AST/ASTImporterLookupTable.cpp
===
--- cfe/trunk/lib/AST/ASTImporterLookupTable.cpp
+++ cfe/trunk/lib/AST/ASTImporterLookupTable.cpp
@@ -26,17 +26,30 @@
 LT.add(D);
 return true;
   }
+  // In most cases the FriendDecl contains the declaration of the befriended
+  // class as a child node, so it is discovered during the recursive
+  // visitation. However, there are cases when the befriended class is not a
+  // child, thus it must be fetched explicitly from the FriendDecl, and only
+  // then can we add it to the lookup table.
   bool VisitFriendDecl(FriendDecl *D) {
 if (D->getFriendType()) {
   QualType Ty = D->getFriendType()->getType();
-  // FIXME Can this be other than elaborated?
-  QualType NamedTy = cast(Ty)->getNamedType();
-  if (!NamedTy->isDependentType()) {
-if (const auto *RTy = dyn_cast(NamedTy))
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
+  // always has that decl as child node.
+  // However, there are non-dependent cases which does not have the
+  // type as a child node. We have to dig up that type now.
+  if (!Ty->isDependentType()) {
+if (const auto *RTy = dyn_cast(Ty))
   LT.add(RTy->getAsCXXRecordDecl());
-else if (const auto *SpecTy =
- dyn_cast(NamedTy)) {
+else if (const auto *SpecTy = dyn_cast(Ty))
   LT.add(SpecTy->getAsCXXRecordDecl());
+else if (isa(Ty)) {
+  // We do not put friend typedefs to the lookup table because
+  // ASTImporter does not organize typedefs into redecl chains.
+} else {
+  llvm_unreachable("Unhandled type of friend class");
 }
   }
 }
Index: cfe/trunk/lib/AST/ASTImporter.cpp
===
--- cfe/trunk/lib/AST/ASTImporter.cpp
+++ cfe/trunk/lib/AST/ASTImporter.cpp
@@ -2256,7 +2256,8 @@
   if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
 return Importer.MapImported(D, FoundTypedef);
 }
-// FIXME Handle redecl chain.
+// FIXME Handle redecl chain. When you do that make consistent changes
+// in ASTImporterLookupTable too.
 break;
   }
 
Index: cfe/trunk/unittests/AST/ASTImporterTest.cpp
===
--- cfe/trunk/unittests/AST/ASTImporterTest.cpp
+++ cfe/trunk/unittests/AST/ASTImporterTest.cpp
@@ -4237,13 +4237,13 @@
   ASSERT_EQ(Res.size(), 0u);
 }
 
-static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
-  QualType Ty = FD->getFriendType()->getType();
-  QualType NamedTy = cast(Ty)->getNamedType();
-  return cast(NamedTy)->getDecl();
+static const RecordDecl *getRecordDeclOfFriend(FriendDecl *FD) {
+  QualType Ty = FD->getFriendType()->getType().getCanonicalType();
+  return cast(Ty)->getDecl();
 }
 
-TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) {
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithElaboratedType) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
   class Y { friend class F; };
@@ -4267,6 +4267,52 @@
   EXPECT_EQ(Res.size(), 0u);
 }
 
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  class Y { friend F; };
+  )",
+  Lang_CXX11);
+
+  // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
+  // So we must dig up the underlying CXXRecordDecl.
+  ASTImporterLookupTable LT(*ToTU);
+  auto *FriendD = FirstDeclMatcher().match(ToTU, friendDecl());
+  const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
+  auto *Y = FirstDeclMatcher().match(ToTU, cxxRecordDecl(hasName("Y")));
+
+  DeclarationName Name = RD->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.size(), 1u);
+  EXPECT_EQ(*Res.begin(), RD);
+
+  Res = LT.lookup(Y, Name);
+  EXPECT_EQ(Res.size(), 0u);
+}
+
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  using alias_of_f = F;
+  

[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-06-11 Thread Gabor Marton via Phabricator via cfe-commits
martong updated this revision to Diff 204048.
martong marked an inline comment as done.
martong added a comment.

- Simplify getRecordDeclOfFriend


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064

Files:
  clang/lib/AST/ASTImporter.cpp
  clang/lib/AST/ASTImporterLookupTable.cpp
  clang/unittests/AST/ASTImporterTest.cpp

Index: clang/unittests/AST/ASTImporterTest.cpp
===
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -4246,13 +4246,13 @@
   ASSERT_EQ(Res.size(), 0u);
 }
 
-static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
-  QualType Ty = FD->getFriendType()->getType();
-  QualType NamedTy = cast(Ty)->getNamedType();
-  return cast(NamedTy)->getDecl();
+static const RecordDecl *getRecordDeclOfFriend(FriendDecl *FD) {
+  QualType Ty = FD->getFriendType()->getType().getCanonicalType();
+  return cast(Ty)->getDecl();
 }
 
-TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) {
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithElaboratedType) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
   class Y { friend class F; };
@@ -4276,6 +4276,52 @@
   EXPECT_EQ(Res.size(), 0u);
 }
 
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  class Y { friend F; };
+  )",
+  Lang_CXX11);
+
+  // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
+  // So we must dig up the underlying CXXRecordDecl.
+  ASTImporterLookupTable LT(*ToTU);
+  auto *FriendD = FirstDeclMatcher().match(ToTU, friendDecl());
+  const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
+  auto *Y = FirstDeclMatcher().match(ToTU, cxxRecordDecl(hasName("Y")));
+
+  DeclarationName Name = RD->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.size(), 1u);
+  EXPECT_EQ(*Res.begin(), RD);
+
+  Res = LT.lookup(Y, Name);
+  EXPECT_EQ(Res.size(), 0u);
+}
+
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  using alias_of_f = F;
+  class Y { friend alias_of_f; };
+  )",
+  Lang_CXX11);
+
+  // ASTImporterLookupTable constructor handles using declarations correctly,
+  // no assert is expected.
+  ASTImporterLookupTable LT(*ToTU);
+
+  auto *Alias = FirstDeclMatcher().match(
+  ToTU, typeAliasDecl(hasName("alias_of_f")));
+  DeclarationName Name = Alias->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.count(Alias), 1u);
+}
+
 TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
Index: clang/lib/AST/ASTImporterLookupTable.cpp
===
--- clang/lib/AST/ASTImporterLookupTable.cpp
+++ clang/lib/AST/ASTImporterLookupTable.cpp
@@ -26,17 +26,30 @@
 LT.add(D);
 return true;
   }
+  // In most cases the FriendDecl contains the declaration of the befriended
+  // class as a child node, so it is discovered during the recursive
+  // visitation. However, there are cases when the befriended class is not a
+  // child, thus it must be fetched explicitly from the FriendDecl, and only
+  // then can we add it to the lookup table.
   bool VisitFriendDecl(FriendDecl *D) {
 if (D->getFriendType()) {
   QualType Ty = D->getFriendType()->getType();
-  // FIXME Can this be other than elaborated?
-  QualType NamedTy = cast(Ty)->getNamedType();
-  if (!NamedTy->isDependentType()) {
-if (const auto *RTy = dyn_cast(NamedTy))
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
+  // always has that decl as child node.
+  // However, there are non-dependent cases which does not have the
+  // type as a child node. We have to dig up that type now.
+  if (!Ty->isDependentType()) {
+if (const auto *RTy = dyn_cast(Ty))
   LT.add(RTy->getAsCXXRecordDecl());
-else if (const auto *SpecTy =
- dyn_cast(NamedTy)) {
+else if (const auto *SpecTy = dyn_cast(Ty))
   LT.add(SpecTy->getAsCXXRecordDecl());
+else if (isa(Ty)) {
+  // We do not put friend typedefs to the lookup table because
+  // ASTImporter does not organize typedefs into redecl chains.
+} else {
+  llvm_unreachable("Unhandled type of friend class");
 }
   }
 }
Index: clang/lib/AST/ASTImporter.cpp
===
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -2256,7 +2256,8 @@
   

[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-06-11 Thread Gabor Marton via Phabricator via cfe-commits
martong marked 3 inline comments as done.
martong added inline comments.



Comment at: clang/unittests/AST/ASTImporterTest.cpp:4259
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {

a_sidorin wrote:
> martong wrote:
> > a_sidorin wrote:
> > > Will getCanonicalType() do the job?
> > That's a good catch! Thank you for pointing out.
> Can we replace the whole function with:
> ```
> QualType Ty = FD->getFriendType()->getType().getCanonicalType();
> return cast(Ty)->getDecl();
> ```
> ?
Thank you, this is indeed simpler, changed that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-05-26 Thread Aleksei Sidorin via Phabricator via cfe-commits
a_sidorin accepted this revision.
a_sidorin added inline comments.
This revision is now accepted and ready to land.



Comment at: clang/unittests/AST/ASTImporterTest.cpp:4259
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {

martong wrote:
> a_sidorin wrote:
> > Will getCanonicalType() do the job?
> That's a good catch! Thank you for pointing out.
Can we replace the whole function with:
```
QualType Ty = FD->getFriendType()->getType().getCanonicalType();
return cast(Ty)->getDecl();
```
?



Comment at: clang/unittests/AST/ASTImporterTest.cpp:4249
 
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();

Redundant space after '*'.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-05-21 Thread Gabor Marton via Phabricator via cfe-commits
martong marked 2 inline comments as done.
martong added inline comments.



Comment at: clang/unittests/AST/ASTImporterTest.cpp:4259
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {

a_sidorin wrote:
> Will getCanonicalType() do the job?
That's a good catch! Thank you for pointing out.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-05-21 Thread Gabor Marton via Phabricator via cfe-commits
martong updated this revision to Diff 200435.
martong marked an inline comment as done.
martong added a comment.

- Remove getUnderlyingType


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064

Files:
  clang/lib/AST/ASTImporter.cpp
  clang/lib/AST/ASTImporterLookupTable.cpp
  clang/unittests/AST/ASTImporterTest.cpp

Index: clang/unittests/AST/ASTImporterTest.cpp
===
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -4248,11 +4248,16 @@
 
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
-  QualType NamedTy = cast(Ty)->getNamedType();
-  return cast(NamedTy)->getDecl();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {
+Ty = Ty.getCanonicalType();
+  }
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  return cast(Ty)->getDecl();
 }
 
-TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) {
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithElaboratedType) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
   class Y { friend class F; };
@@ -4276,6 +4281,52 @@
   EXPECT_EQ(Res.size(), 0u);
 }
 
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  class Y { friend F; };
+  )",
+  Lang_CXX11);
+
+  // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
+  // So we must dig up the underlying CXXRecordDecl.
+  ASTImporterLookupTable LT(*ToTU);
+  auto *FriendD = FirstDeclMatcher().match(ToTU, friendDecl());
+  const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
+  auto *Y = FirstDeclMatcher().match(ToTU, cxxRecordDecl(hasName("Y")));
+
+  DeclarationName Name = RD->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.size(), 1u);
+  EXPECT_EQ(*Res.begin(), RD);
+
+  Res = LT.lookup(Y, Name);
+  EXPECT_EQ(Res.size(), 0u);
+}
+
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  using alias_of_f = F;
+  class Y { friend alias_of_f; };
+  )",
+  Lang_CXX11);
+
+  // ASTImporterLookupTable constructor handles using declarations correctly,
+  // no assert is expected.
+  ASTImporterLookupTable LT(*ToTU);
+
+  auto *Alias = FirstDeclMatcher().match(
+  ToTU, typeAliasDecl(hasName("alias_of_f")));
+  DeclarationName Name = Alias->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.count(Alias), 1u);
+}
+
 TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
Index: clang/lib/AST/ASTImporterLookupTable.cpp
===
--- clang/lib/AST/ASTImporterLookupTable.cpp
+++ clang/lib/AST/ASTImporterLookupTable.cpp
@@ -26,17 +26,30 @@
 LT.add(D);
 return true;
   }
+  // In most cases the FriendDecl contains the declaration of the befriended
+  // class as a child node, so it is discovered during the recursive
+  // visitation. However, there are cases when the befriended class is not a
+  // child, thus it must be fetched explicitly from the FriendDecl, and only
+  // then can we add it to the lookup table.
   bool VisitFriendDecl(FriendDecl *D) {
 if (D->getFriendType()) {
   QualType Ty = D->getFriendType()->getType();
-  // FIXME Can this be other than elaborated?
-  QualType NamedTy = cast(Ty)->getNamedType();
-  if (!NamedTy->isDependentType()) {
-if (const auto *RTy = dyn_cast(NamedTy))
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
+  // always has that decl as child node.
+  // However, there are non-dependent cases which does not have the
+  // type as a child node. We have to dig up that type now.
+  if (!Ty->isDependentType()) {
+if (const auto *RTy = dyn_cast(Ty))
   LT.add(RTy->getAsCXXRecordDecl());
-else if (const auto *SpecTy =
- dyn_cast(NamedTy)) {
+else if (const auto *SpecTy = dyn_cast(Ty))
   LT.add(SpecTy->getAsCXXRecordDecl());
+else if (isa(Ty)) {
+  // We do not put friend typedefs to the lookup table because
+  // ASTImporter does not organize typedefs into redecl chains.
+} else {
+  llvm_unreachable("Unhandled type of friend class");
 }
   }
 }
Index: clang/lib/AST/ASTImporter.cpp
===
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -2256,7 +2256,8 @@
   if (!FromUT->isIncompleteType() 

[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-05-19 Thread Aleksei Sidorin via Phabricator via cfe-commits
a_sidorin added a comment.

Hi Gabor,
This looks fine, but I have a question inline.




Comment at: clang/unittests/AST/ASTImporterTest.cpp:4259
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {

Will getCanonicalType() do the job?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62064/new/

https://reviews.llvm.org/D62064



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D62064: [ASTImporter] Fix unhandled cases in ASTImporterLookupTable

2019-05-17 Thread Gabor Marton via Phabricator via cfe-commits
martong created this revision.
martong added a reviewer: a_sidorin.
Herald added subscribers: cfe-commits, gamesh411, Szelethus, dkrupp, rnkovacs.
Herald added a reviewer: a.sidorin.
Herald added a reviewer: shafik.
Herald added a project: clang.

In most cases the FriendDecl contains the declaration of the befriended
class as a child node, so it is discovered during the recursive
visitation. However, there are cases when the befriended class is not a
child, thus it must be fetched explicitly from the FriendDecl, and only
then can we add it to the lookup table.
(Note, this does affect only CTU and does not affect LLDB, because we
cannot and do not use the ASTImporterLookupTable in LLDB.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D62064

Files:
  clang/lib/AST/ASTImporter.cpp
  clang/lib/AST/ASTImporterLookupTable.cpp
  clang/unittests/AST/ASTImporterTest.cpp

Index: clang/unittests/AST/ASTImporterTest.cpp
===
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -4246,13 +4246,27 @@
   ASSERT_EQ(Res.size(), 0u);
 }
 
+static QualType getUnderlyingType(const TypedefType *TDT) {
+  QualType T = TDT->getDecl()->getUnderlyingType();
+
+  if (const auto *Inner = dyn_cast(T.getTypePtr()))
+return getUnderlyingType(Inner);
+
+  return T;
+}
+
 static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) {
   QualType Ty = FD->getFriendType()->getType();
-  QualType NamedTy = cast(Ty)->getNamedType();
-  return cast(NamedTy)->getDecl();
+  if (auto *Inner = dyn_cast(Ty.getTypePtr())) {
+Ty = getUnderlyingType(Inner);
+  }
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  return cast(Ty)->getDecl();
 }
 
-TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) {
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithElaboratedType) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
   class Y { friend class F; };
@@ -4276,6 +4290,52 @@
   EXPECT_EQ(Res.size(), 0u);
 }
 
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  class Y { friend F; };
+  )",
+  Lang_CXX11);
+
+  // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
+  // So we must dig up the underlying CXXRecordDecl.
+  ASTImporterLookupTable LT(*ToTU);
+  auto *FriendD = FirstDeclMatcher().match(ToTU, friendDecl());
+  const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
+  auto *Y = FirstDeclMatcher().match(ToTU, cxxRecordDecl(hasName("Y")));
+
+  DeclarationName Name = RD->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.size(), 1u);
+  EXPECT_EQ(*Res.begin(), RD);
+
+  Res = LT.lookup(Y, Name);
+  EXPECT_EQ(Res.size(), 0u);
+}
+
+TEST_P(ASTImporterLookupTableTest,
+   LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+  R"(
+  class F;
+  using alias_of_f = F;
+  class Y { friend alias_of_f; };
+  )",
+  Lang_CXX11);
+
+  // ASTImporterLookupTable constructor handles using declarations correctly,
+  // no assert is expected.
+  ASTImporterLookupTable LT(*ToTU);
+
+  auto *Alias = FirstDeclMatcher().match(
+  ToTU, typeAliasDecl(hasName("alias_of_f")));
+  DeclarationName Name = Alias->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.count(Alias), 1u);
+}
+
 TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
   TranslationUnitDecl *ToTU = getToTuDecl(
   R"(
Index: clang/lib/AST/ASTImporterLookupTable.cpp
===
--- clang/lib/AST/ASTImporterLookupTable.cpp
+++ clang/lib/AST/ASTImporterLookupTable.cpp
@@ -26,17 +26,30 @@
 LT.add(D);
 return true;
   }
+  // In most cases the FriendDecl contains the declaration of the befriended
+  // class as a child node, so it is discovered during the recursive
+  // visitation. However, there are cases when the befriended class is not a
+  // child, thus it must be fetched explicitly from the FriendDecl, and only
+  // then can we add it to the lookup table.
   bool VisitFriendDecl(FriendDecl *D) {
 if (D->getFriendType()) {
   QualType Ty = D->getFriendType()->getType();
-  // FIXME Can this be other than elaborated?
-  QualType NamedTy = cast(Ty)->getNamedType();
-  if (!NamedTy->isDependentType()) {
-if (const auto *RTy = dyn_cast(NamedTy))
+  if (isa(Ty))
+Ty = cast(Ty)->getNamedType();
+  // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
+  // always has that decl as child node.
+  // However, there are non-dependent cases which does not have the
+  // type as a child node. We have to dig up that type now.
+  if (!Ty->isDependentType()) {
+if (const auto *RTy =