From 7ae2faafd30524ef5f863bb3b8701977888839bb Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 13 Aug 2010 05:36:37 +0000 Subject: Implement clang_saveTranslationUnit(), which saves a translation unit into a PCH/AST file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111006 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/ASTUnit.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/Frontend/ASTUnit.cpp') diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index b56a0d83a8..e501260af7 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1340,3 +1340,26 @@ void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, Clang.takeCodeCompletionConsumer(); CCInvocation.getLangOpts().SpellChecking = SpellChecking; } + +bool ASTUnit::Save(llvm::StringRef File) { + if (getDiagnostics().hasErrorOccurred()) + return true; + + // FIXME: Can we somehow regenerate the stat cache here, or do we need to + // unconditionally create a stat cache when we parse the file? + std::string ErrorInfo; + llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo); + if (!ErrorInfo.empty() || Out.has_error()) + return true; + + std::vector Buffer; + llvm::BitstreamWriter Stream(Buffer); + PCHWriter Writer(Stream); + Writer.WritePCH(getSema(), 0, 0); + + // Write the generated bitstream to "Out". + Out.write((char *)&Buffer.front(), Buffer.size()); + Out.flush(); + Out.close(); + return Out.has_error(); +} -- cgit v1.2.3-18-g5258