Index: include/clang/Frontend/ASTUnit.h =================================================================== --- include/clang/Frontend/ASTUnit.h +++ include/clang/Frontend/ASTUnit.h @@ -438,6 +438,8 @@ void setASTContext(ASTContext *ctx) { Ctx = ctx; } void setPreprocessor(std::shared_ptr pp); + void beginSourceFile(); + bool hasSema() const { return (bool)TheSema; } Sema &getSema() const { Index: lib/Frontend/ASTUnit.cpp =================================================================== --- lib/Frontend/ASTUnit.cpp +++ lib/Frontend/ASTUnit.cpp @@ -274,6 +274,12 @@ this->PP = std::move(PP); } +void ASTUnit::beginSourceFile() { + assert(getDiagnostics().getClient() && PP && Ctx && + "Bad context for source file"); + getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get()); +} + /// Determine the set of code-completion contexts in which this /// declaration should be shown. static unsigned getDeclShowContexts(const NamedDecl *ND, Index: unittests/AST/ASTImporterTest.cpp =================================================================== --- unittests/AST/ASTImporterTest.cpp +++ unittests/AST/ASTImporterTest.cpp @@ -98,6 +98,9 @@ ASTContext &FromCtx = FromAST->getASTContext(), &ToCtx = ToAST->getASTContext(); + FromAST->beginSourceFile(); + ToAST->beginSourceFile(); + ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx, FromAST->getFileManager(), false); @@ -172,7 +175,9 @@ : Code(Code), FileName(FileName), Unit(tooling::buildASTFromCodeWithArgs(this->Code, Args, this->FileName)), - TUDecl(Unit->getASTContext().getTranslationUnitDecl()) {} + TUDecl(Unit->getASTContext().getTranslationUnitDecl()) { + Unit->beginSourceFile(); + } }; // We may have several From contexts and related translation units. In each @@ -214,6 +219,7 @@ ToCode = ToSrcCode; assert(!ToAST); ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName); + ToAST->beginSourceFile(); ASTContext &FromCtx = FromTU.Unit->getASTContext(), &ToCtx = ToAST->getASTContext(); @@ -261,6 +267,7 @@ ToCode = ToSrcCode; assert(!ToAST); ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName); + ToAST->beginSourceFile(); return ToAST->getASTContext().getTranslationUnitDecl(); } @@ -274,6 +281,7 @@ // Build the AST from an empty file. ToAST = tooling::buildASTFromCodeWithArgs(/*Code=*/"", ToArgs, "empty.cc"); + ToAST->beginSourceFile(); } // Create a virtual file in the To Ctx which corresponds to the file from