[C++20] Fix a crash with modules.
authorUtkarsh Saxena <usx@google.com>
Mon, 23 Jan 2023 18:15:46 +0000 (19:15 +0100)
committerUtkarsh Saxena <usx@google.com>
Thu, 2 Feb 2023 16:06:43 +0000 (17:06 +0100)
Differential Revision: https://reviews.llvm.org/D142384

clang/lib/AST/Decl.cpp

index e60cc28..51cff6c 100644 (file)
@@ -4770,7 +4770,10 @@ bool RecordDecl::isOrContainsUnion() const {
 RecordDecl::field_iterator RecordDecl::field_begin() const {
   if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage())
     LoadFieldsFromExternalStorage();
-
+  // This is necessary for correctness for C++ with modules.
+  // FIXME: Come up with a test case that breaks without definition.
+  if (RecordDecl *D = getDefinition(); D && D != this)
+    return D->field_begin();
   return field_iterator(decl_iterator(FirstDecl));
 }