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
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index b3141f2..4216e96 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -765,6 +765,26 @@
unsigned options);
/**
+ * \brief Saves a translation unit into a serialized representation of
+ * that translation unit on disk.
+ *
+ * Any translation unit that was parsed without error can be saved
+ * into a file. The translation unit can then be deserialized into a
+ * new \c CXTranslationUnit with \c clang_createTranslationUnit() or,
+ * if it is an incomplete translation unit that corresponds to a
+ * header, used as a precompiled header when parsing other translation
+ * units.
+ *
+ * \param TU The translation unit to save.
+ * \param FileName The file to which the translation unit will be saved.
+ *
+ * \returns Zero if the translation unit was saved successfully, a
+ * non-zero value otherwise.
+ */
+CINDEX_LINKAGE int clang_saveTranslationUnit(CXTranslationUnit TU,
+ const char *FileName);
+
+/**
* \brief Destroy the specified CXTranslationUnit object.
*/
CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h
index d4a351c..07bba5c 100644
--- a/include/clang/Frontend/ASTUnit.h
+++ b/include/clang/Frontend/ASTUnit.h
@@ -440,6 +440,11 @@
Diagnostic &Diag, LangOptions &LangOpts,
SourceManager &SourceMgr, FileManager &FileMgr,
llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics);
+
+ /// \brief Save this translation unit to a file with the given name.
+ ///
+ /// \returns True if an error occurred, false otherwise.
+ bool Save(llvm::StringRef File);
};
} // namespace clang
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index b56a0d8..e501260 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -1340,3 +1340,26 @@
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<unsigned char> 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();
+}
diff --git a/test/Index/TestClassDecl.m b/test/Index/TestClassDecl.m
index b55c862..09a7d48 100644
--- a/test/Index/TestClassDecl.m
+++ b/test/Index/TestClassDecl.m
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fblocks -emit-pch -x objective-c %s -o %t.ast
+// RUN: c-index-test -write-pch %t.ast -fobjc-nonfragile-abi -fblocks -x objective-c %s
// RUN: c-index-test -test-file-scan %t.ast %s | FileCheck -check-prefix=scan %s
// RUN: c-index-test -test-load-tu %t.ast local | FileCheck -check-prefix=load %s
diff --git a/test/Index/preamble.c b/test/Index/preamble.c
index c285cd2..1d3e7e8 100644
--- a/test/Index/preamble.c
+++ b/test/Index/preamble.c
@@ -5,7 +5,7 @@
void f(int x) {
}
-// RUN: %clang -x c-header -o %t.pch %S/Inputs/prefix.h
+// RUN: c-index-test -write-pch %t.pch -x c-header %S/Inputs/prefix.h
// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-load-source-reparse 5 local -I %S/Inputs -include %t %s 2> %t.stderr.txt | FileCheck %s
// RUN: FileCheck -check-prefix CHECK-DIAG %s < %t.stderr.txt
// CHECK: preamble.h:1:12: FunctionDecl=bar:1:12 (Definition) Extent=[1:12 - 6:2]
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index f95829b..323469c 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -1280,6 +1280,43 @@
/******************************************************************************/
/* Command line processing. */
/******************************************************************************/
+int write_pch_file(const char *filename, int argc, const char *argv[]) {
+ CXIndex Idx;
+ CXTranslationUnit TU;
+ struct CXUnsavedFile *unsaved_files = 0;
+ int num_unsaved_files = 0;
+
+ Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
+
+ if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
+ clang_disposeIndex(Idx);
+ return -1;
+ }
+
+ TU = clang_parseTranslationUnit(Idx, 0,
+ argv + num_unsaved_files,
+ argc - num_unsaved_files,
+ unsaved_files,
+ num_unsaved_files,
+ CXTranslationUnit_Incomplete);
+ if (!TU) {
+ fprintf(stderr, "Unable to load translation unit!\n");
+ free_remapped_files(unsaved_files, num_unsaved_files);
+ clang_disposeIndex(Idx);
+ return 1;
+ }
+
+ if (clang_saveTranslationUnit(TU, filename))
+ fprintf(stderr, "Unable to write PCH file %s\n", filename);
+ clang_disposeTranslationUnit(TU);
+ free_remapped_files(unsaved_files, num_unsaved_files);
+ clang_disposeIndex(Idx);
+ return 0;
+}
+
+/******************************************************************************/
+/* Command line processing. */
+/******************************************************************************/
static CXCursorVisitor GetVisitor(const char *s) {
if (s[0] == '\0')
@@ -1312,7 +1349,8 @@
" c-index-test -test-print-typekind {<args>}*\n"
" c-index-test -print-usr [<CursorKind> {<args>}]*\n");
fprintf(stderr,
- " c-index-test -print-usr-file <file>\n\n");
+ " c-index-test -print-usr-file <file>\n"
+ " c-index-test -write-pch <file> <compiler arguments>\n\n");
fprintf(stderr,
" <symbol filter> values:\n%s",
" all - load all symbols, including those from PCH\n"
@@ -1379,7 +1417,9 @@
}
else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
return print_usrs_file(argv[2]);
-
+ else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
+ return write_pch_file(argv[2], argc - 3, argv + 3);
+
print_usage();
return 1;
}
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 0f43cf6..4ba41b5 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -1454,6 +1454,13 @@
return ATU;
}
+int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName) {
+ if (!TU)
+ return 1;
+
+ return static_cast<ASTUnit *>(TU)->Save(FileName);
+}
+
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
if (CTUnit)
delete static_cast<ASTUnit *>(CTUnit);
diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports
index 671d376..f8f4402 100644
--- a/tools/libclang/libclang.darwin.exports
+++ b/tools/libclang/libclang.darwin.exports
@@ -93,6 +93,7 @@
_clang_isUnexposed
_clang_parseTranslationUnit
_clang_reparseTranslationUnit
+_clang_saveTranslationUnit
_clang_setUseExternalASTGeneration
_clang_tokenize
_clang_visitChildren
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index 9b2d0ad..cdb04bc 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -93,6 +93,7 @@
clang_isUnexposed
clang_parseTranslationUnit
clang_reparseTranslationUnit
+clang_saveTranslationUnit
clang_setUseExternalASTGeneration
clang_tokenize
clang_visitChildren