[PATCH] D145868: [clang][ASTImporter] Fix import of anonymous structures

Vince Bridgers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 12 06:03:15 PDT 2023


vabridgers created this revision.
vabridgers added reviewers: a.sidorin, shafik, donat.nagy, gamesh411, balazske.
Herald added a subscriber: martong.
Herald added a project: All.
vabridgers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fix crash in ASTImporter for anonymous structures. There was a series of
problems exposed by https://reviews.llvm.org/D133468 (commit
69a6417406a1b0316a1fa6aeb63339d0e1d2abbd <https://reviews.llvm.org/rG69a6417406a1b0316a1fa6aeb63339d0e1d2abbd>) in the ASTImporter breaking
cross-translation unit analysis. This change fixes the problem exposed
by that change for importing anonymous structures. The problem was
discovered when running clang static analysis on open source projects
using cross-translation unit analysis.

Simple test command. Produces crash without change, passes all tests
with change.

ninja ASTTests && ./tools/clang/unittests/AST/ASTTests

  --gtest_filter="*/*ImportAnonymousStruct/0"

Formatted crash stack:

ASTTests: <root>/clang/lib/AST/ASTContext.cpp:4787:

  clang::QualType clang::ASTContext::getTypedefType(const clang::TypedefNameDecl*,
  clang::QualType) const: Assertion `hasSameType(Decl->getUnderlyingType(), Underlying)' failed.

...
 #9 <addr> clang::ASTContext::getTypedefType(clang::TypedefNameDecl const*, clang::QualType) const

  <root>/clang/lib/AST/ASTContext.cpp:4789:26
  <root>/clang/lib/AST/ASTImporter.cpp:1374:71
  <root>/tools/clang/include/clang/AST/TypeNodes.inc:75:1
  <root>/clang/lib/AST/ASTImporter.cpp:8663:8


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145868

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


Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -8474,6 +8474,45 @@
       ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType()));
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, ImportAnonymousStruct) {
+  // Tests importing of anonymous structures. This is a reduction of a real
+  // issue found in the wild into unittest case form. This crashes without
+  // the fix. Just don't crash when this unittest case is run.
+  Decl *ToTU = getToTuDecl(
+      R"(  
+      typedef struct _XDisplay Display;
+      typedef struct {
+        int var;
+      } *_XPrivDisplay;
+      extern Display  *xterm_dpy;
+      int function(int);
+      int internal(Display *dpy) {
+        return function(((_XPrivDisplay)dpy)->var );
+      }
+      )",
+      Lang_C99);
+  auto *ToX = FirstDeclMatcher<FunctionDecl>().match(
+      ToTU, functionDecl(hasName("internal"), isDefinition()));
+  ASSERT_TRUE(ToX);
+
+  Decl *FromTU = getTuDecl(
+      R"(  
+      typedef struct _XDisplay Display;
+      typedef struct {
+        int var;
+      } *_XPrivDisplay;
+      extern Display  *xterm_dpy;
+      int function(int var) {
+        return ((_XPrivDisplay)xterm_dpy)->var;
+      }
+      )",
+      Lang_C99, "input1.c");
+  auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
+      FromTU, functionDecl(hasName("function"), isDefinition()));
+  auto *ToF = Import(FromF, Lang_C99);
+  EXPECT_TRUE(ToF);
+}
+
 INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
                          DefaultTestValuesForRunOptions);
 
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -1363,12 +1363,7 @@
   Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
   if (!ToDeclOrErr)
     return ToDeclOrErr.takeError();
-  ExpectedType ToUnderlyingTypeOrErr = import(T->desugar());
-  if (!ToUnderlyingTypeOrErr)
-    return ToUnderlyingTypeOrErr.takeError();
-
-  return Importer.getToContext().getTypedefType(*ToDeclOrErr,
-                                                *ToUnderlyingTypeOrErr);
+  return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
 }
 
 ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145868.504423.patch
Type: text/x-patch
Size: 2464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230312/0a83b967/attachment.bin>


More information about the cfe-commits mailing list