@@ -4738,6 +4738,93 @@ TEST_P(ErrorHandlingTest, ErrorHappensAfterNodeIsCreatedAndLinked) {
4738
4738
EXPECT_EQ (OptErr->Error , ImportError::UnsupportedConstruct);
4739
4739
}
4740
4740
4741
+ // An error should be set for a class if we cannot import one member.
4742
+ TEST_P (ErrorHandlingTest, ErrorIsPropagatedFromMemberToClass) {
4743
+ TranslationUnitDecl *FromTU = getTuDecl (
4744
+ std::string (R"(
4745
+ class X {
4746
+ void f() { )" ) + ErroneousStmt + R"( } // This member has the error
4747
+ // during import.
4748
+ void ok(); // The error should not prevent importing this.
4749
+ }; // An error will be set for X too.
4750
+ )" ,
4751
+ Lang_CXX);
4752
+ auto *FromX = FirstDeclMatcher<CXXRecordDecl>().match (
4753
+ FromTU, cxxRecordDecl (hasName (" X" )));
4754
+ CXXRecordDecl *ImportedX = Import (FromX, Lang_CXX);
4755
+
4756
+ // An error is set for X.
4757
+ EXPECT_FALSE (ImportedX);
4758
+ ASTImporter *Importer = findFromTU (FromX)->Importer .get ();
4759
+ Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny (FromX);
4760
+ ASSERT_TRUE (OptErr);
4761
+ EXPECT_EQ (OptErr->Error , ImportError::UnsupportedConstruct);
4762
+
4763
+ // An error is set for f().
4764
+ auto *FromF = FirstDeclMatcher<CXXMethodDecl>().match (
4765
+ FromTU, cxxMethodDecl (hasName (" f" )));
4766
+ OptErr = Importer->getImportDeclErrorIfAny (FromF);
4767
+ ASSERT_TRUE (OptErr);
4768
+ EXPECT_EQ (OptErr->Error , ImportError::UnsupportedConstruct);
4769
+ // And any subsequent import should fail.
4770
+ CXXMethodDecl *ImportedF = Import (FromF, Lang_CXX);
4771
+ EXPECT_FALSE (ImportedF);
4772
+
4773
+ // There is no error set for ok().
4774
+ auto *FromOK = FirstDeclMatcher<CXXMethodDecl>().match (
4775
+ FromTU, cxxMethodDecl (hasName (" ok" )));
4776
+ OptErr = Importer->getImportDeclErrorIfAny (FromOK);
4777
+ EXPECT_FALSE (OptErr);
4778
+ // And we should be able to import.
4779
+ CXXMethodDecl *ImportedOK = Import (FromOK, Lang_CXX);
4780
+ EXPECT_TRUE (ImportedOK);
4781
+
4782
+ // Unwary clients may access X even if the error is set, so, at least make
4783
+ // sure the class is set to be complete.
4784
+ CXXRecordDecl *ToX = cast<CXXRecordDecl>(ImportedOK->getDeclContext ());
4785
+ EXPECT_TRUE (ToX->isCompleteDefinition ());
4786
+ }
4787
+
4788
+ TEST_P (ErrorHandlingTest, ErrorIsNotPropagatedFromMemberToNamespace) {
4789
+ TranslationUnitDecl *FromTU = getTuDecl (
4790
+ std::string (R"(
4791
+ namespace X {
4792
+ void f() { )" ) + ErroneousStmt + R"( } // This member has the error
4793
+ // during import.
4794
+ void ok(); // The error should not prevent importing this.
4795
+ }; // An error will be set for X too.
4796
+ )" ,
4797
+ Lang_CXX);
4798
+ auto *FromX = FirstDeclMatcher<NamespaceDecl>().match (
4799
+ FromTU, namespaceDecl (hasName (" X" )));
4800
+ NamespaceDecl *ImportedX = Import (FromX, Lang_CXX);
4801
+
4802
+ // There is no error set for X.
4803
+ EXPECT_TRUE (ImportedX);
4804
+ ASTImporter *Importer = findFromTU (FromX)->Importer .get ();
4805
+ Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny (FromX);
4806
+ ASSERT_FALSE (OptErr);
4807
+
4808
+ // An error is set for f().
4809
+ auto *FromF = FirstDeclMatcher<FunctionDecl>().match (
4810
+ FromTU, functionDecl (hasName (" f" )));
4811
+ OptErr = Importer->getImportDeclErrorIfAny (FromF);
4812
+ ASSERT_TRUE (OptErr);
4813
+ EXPECT_EQ (OptErr->Error , ImportError::UnsupportedConstruct);
4814
+ // And any subsequent import should fail.
4815
+ FunctionDecl *ImportedF = Import (FromF, Lang_CXX);
4816
+ EXPECT_FALSE (ImportedF);
4817
+
4818
+ // There is no error set for ok().
4819
+ auto *FromOK = FirstDeclMatcher<FunctionDecl>().match (
4820
+ FromTU, functionDecl (hasName (" ok" )));
4821
+ OptErr = Importer->getImportDeclErrorIfAny (FromOK);
4822
+ EXPECT_FALSE (OptErr);
4823
+ // And we should be able to import.
4824
+ FunctionDecl *ImportedOK = Import (FromOK, Lang_CXX);
4825
+ EXPECT_TRUE (ImportedOK);
4826
+ }
4827
+
4741
4828
INSTANTIATE_TEST_CASE_P (ParameterizedTests, ErrorHandlingTest,
4742
4829
DefaultTestValuesForRunOptions, );
4743
4830
0 commit comments