Skip to content

Commit baec6c5

Browse files
authored
[clang][ASTImporter] Fixed test 'ctu-import-type-decl-definition' and ASTImporter (#158016)
The test was faulty and did not reproduce the error in ASTImporter. The previous fix #156056 for the crash was not correct, this is fixed here.
1 parent 4884d6c commit baec6c5

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,15 +1745,13 @@ ExpectedType ASTNodeImporter::VisitTagType(const TagType *T) {
17451745
if (!ToDeclOrErr)
17461746
return ToDeclOrErr.takeError();
17471747

1748-
if (DeclForType->isUsed()) {
1749-
// If there is a definition of the 'OriginalDecl', it should be imported to
1750-
// have all information for the type in the "To" AST. (In some cases no
1751-
// other reference may exist to the definition decl and it would not be
1752-
// imported otherwise.)
1753-
Expected<TagDecl *> ToDefDeclOrErr = import(DeclForType->getDefinition());
1754-
if (!ToDefDeclOrErr)
1755-
return ToDefDeclOrErr.takeError();
1756-
}
1748+
// If there is a definition of the 'OriginalDecl', it should be imported to
1749+
// have all information for the type in the "To" AST. (In some cases no
1750+
// other reference may exist to the definition decl and it would not be
1751+
// imported otherwise.)
1752+
Expected<TagDecl *> ToDefDeclOrErr = import(DeclForType->getDefinition());
1753+
if (!ToDefDeclOrErr)
1754+
return ToDefDeclOrErr.takeError();
17571755

17581756
if (T->isCanonicalUnqualified())
17591757
return Importer.getToContext().getCanonicalTagType(*ToDeclOrErr);

clang/test/Analysis/ctu-import-type-decl-definition.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22
// RUN: mkdir -p %t
33
// RUN: split-file %s %t
44

5-
// RUN: %clang_cc1 -emit-pch -o %t/import.c.ast %t/import.c
5+
// RUN: %clang_cc1 -x c -emit-pch -o %t/import.c.ast %t/import.c
66

7-
// RUN: %clang_extdef_map -- -x c %t/import.c >> %t/externalDefMap.tmp.txt
8-
// RUN: sed 's/$/.ast/' %t/externalDefMap.tmp.txt >> %t/externalDefMap.txt
7+
// RUN: %clang_extdef_map %t/import.c -- -c -x c > %t/externalDefMap.tmp.txt
8+
// RUN: sed 's/$/.ast/' %t/externalDefMap.tmp.txt > %t/externalDefMap.txt
99

1010
// RUN: %clang_cc1 -analyze \
1111
// RUN: -analyzer-checker=core \
1212
// RUN: -analyzer-config experimental-enable-naive-ctu-analysis=true \
1313
// RUN: -analyzer-config display-ctu-progress=true \
1414
// RUN: -analyzer-config ctu-dir=%t \
15-
// RUN: -verify %t/main.c
15+
// RUN: -verify %t/main.c 2>&1 | FileCheck %s
1616

1717
//--- main.c
1818

1919
// expected-no-diagnostics
20+
// CHECK: CTU loaded AST file:
2021

2122
typedef struct X_s X_t;
22-
unsigned long f_import(struct X_s *xPtr);
2323

24-
static void freeWriteFileResources(struct X_s *xPtr) {
24+
long f_import(struct X_s *xPtr);
25+
26+
static void f_main(struct X_s *xPtr) {
2527
f_import(xPtr);
2628
}
2729

@@ -36,7 +38,7 @@ struct X_s {
3638
Y_t y;
3739
};
3840

39-
unsigned long f_import(struct X_s *xPtr) {
41+
long f_import(struct X_s *xPtr) {
4042
if (xPtr != 0) {
4143
}
4244
return 0;

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10025,7 +10025,8 @@ struct ImportTemplateParmDeclDefaultValue
1002510025
EXPECT_EQ(ToD->getPreviousDecl(), ToDInherited);
1002610026
} else {
1002710027
EXPECT_EQ(FromD, FromDInherited->getPreviousDecl());
10028-
EXPECT_EQ(ToD, ToDInherited->getPreviousDecl());
10028+
// The order is reversed by the import process.
10029+
EXPECT_EQ(ToD->getPreviousDecl(), ToDInherited);
1002910030
}
1003010031
}
1003110032

0 commit comments

Comments
 (0)