]> granicus.if.org Git - clang/commitdiff
Implement clang_saveTranslationUnit(), which saves a translation unit
authorDouglas Gregor <dgregor@apple.com>
Fri, 13 Aug 2010 05:36:37 +0000 (05:36 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 13 Aug 2010 05:36:37 +0000 (05:36 +0000)
into a PCH/AST file.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111006 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
include/clang/Frontend/ASTUnit.h
lib/Frontend/ASTUnit.cpp
test/Index/TestClassDecl.m
test/Index/preamble.c
tools/c-index-test/c-index-test.c
tools/libclang/CIndex.cpp
tools/libclang/libclang.darwin.exports
tools/libclang/libclang.exports

index b3141f2dfe97f807fea738631fd8cf869e68bd84..4216e96c604a5178beb6abb78d68acb19ae16392 100644 (file)
@@ -764,6 +764,26 @@ CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
                                                      unsigned num_unsaved_files,
                                                             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.
  */
index d4a351c6b07e63825eca5dc75664bf5b2746a31e..07bba5c0aa695daa0e7ebef34160c645d9f1b269 100644 (file)
@@ -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
index b56a0d83a850e568dac607e43f942e7dc8f1cc91..e501260af722c9e7e59f87c872aee4b51e163797 100644 (file)
@@ -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();
+}
index b55c8623a514bccb5b96db3f5e930fb9eaf1e4ba..09a7d48cfdd37ee98a532e9c9bac7955e60cd77b 100644 (file)
@@ -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
 
index c285cd2b2daa20bdeb0aa8fde299ff2cdf9600da..1d3e7e82cf2244470b53c459625c15ccc5765751 100644 (file)
@@ -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]
index f95829b95ac06ee7ff0151748317958e50923a3a..323469c0f063f061db29c9bfb2fc7a5d5620880e 100644 (file)
@@ -1277,6 +1277,43 @@ int print_usrs_file(const char *file_name) {
   return 0;
 }
 
+/******************************************************************************/
+/* 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.                                                   */
 /******************************************************************************/
@@ -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;
 }
index 0f43cf635911ab0ab1e5d432dce49cabef43529b..4ba41b55cbbef56cf238702719d864ff64ddedf9 100644 (file)
@@ -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);
index 671d376a86e30f36f9f759a3fd7587628d7c0154..f8f44020388588d0ebb25fa7a719f9ea94c48d35 100644 (file)
@@ -93,6 +93,7 @@ _clang_isTranslationUnit
 _clang_isUnexposed
 _clang_parseTranslationUnit
 _clang_reparseTranslationUnit
+_clang_saveTranslationUnit
 _clang_setUseExternalASTGeneration
 _clang_tokenize
 _clang_visitChildren
index 9b2d0ad4694162e1066efe20302d7ee65eba2c6c..cdb04bc93463700770c5b11768202abd22e8c74b 100644 (file)
@@ -93,6 +93,7 @@ clang_isTranslationUnit
 clang_isUnexposed
 clang_parseTranslationUnit
 clang_reparseTranslationUnit
+clang_saveTranslationUnit
 clang_setUseExternalASTGeneration
 clang_tokenize
 clang_visitChildren