diff options
-rw-r--r-- | include/clang-c/Index.h | 20 | ||||
-rw-r--r-- | include/clang/Frontend/ASTUnit.h | 5 | ||||
-rw-r--r-- | lib/Frontend/ASTUnit.cpp | 23 | ||||
-rw-r--r-- | test/Index/TestClassDecl.m | 2 | ||||
-rw-r--r-- | test/Index/preamble.c | 2 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 44 | ||||
-rw-r--r-- | tools/libclang/CIndex.cpp | 7 | ||||
-rw-r--r-- | tools/libclang/libclang.darwin.exports | 1 | ||||
-rw-r--r-- | tools/libclang/libclang.exports | 1 |
9 files changed, 101 insertions, 4 deletions
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index b3141f2dfe..4216e96c60 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -765,6 +765,26 @@ CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, 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 d4a351c6b0..07bba5c0aa 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -440,6 +440,11 @@ public: 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 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<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 b55c8623a5..09a7d48cfd 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 c285cd2b2d..1d3e7e82cf 100644 --- a/test/Index/preamble.c +++ b/test/Index/preamble.c @@ -5,7 +5,7 @@ int wibble(int); 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 f95829b95a..323469c0f0 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -1280,6 +1280,43 @@ int print_usrs_file(const char *file_name) { /******************************************************************************/ /* 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 @@ static void print_usage(void) { " 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 @@ int main(int argc, const char **argv) { } 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 0f43cf6359..4ba41b55cb 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -1454,6 +1454,13 @@ CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, 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 671d376a86..f8f4402038 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -93,6 +93,7 @@ _clang_isTranslationUnit _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 9b2d0ad469..cdb04bc934 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -93,6 +93,7 @@ clang_isTranslationUnit clang_isUnexposed clang_parseTranslationUnit clang_reparseTranslationUnit +clang_saveTranslationUnit clang_setUseExternalASTGeneration clang_tokenize clang_visitChildren |