Index: include/clang-c/Index.h =================================================================== --- include/clang-c/Index.h +++ include/clang-c/Index.h @@ -32,7 +32,7 @@ * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable. */ #define CINDEX_VERSION_MAJOR 0 -#define CINDEX_VERSION_MINOR 39 +#define CINDEX_VERSION_MINOR 40 #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -1419,6 +1419,15 @@ unsigned options); /** + * \brief Suspend a translation unit in order to free memory associated with it. + * + * A suspended translation unit uses significantly less memory but on the other + * side does not support any other calls than \c clang_reparseTranslationUnit + * to resume it or \c clang_disposeTranslationUnit to dispose it completely. + */ +CINDEX_LINKAGE unsigned clang_suspendTranslationUnit(CXTranslationUnit); + +/** * \brief Destroy the specified CXTranslationUnit object. */ CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit); Index: include/clang/Frontend/ASTUnit.h =================================================================== --- include/clang/Frontend/ASTUnit.h +++ include/clang/Frontend/ASTUnit.h @@ -852,6 +852,11 @@ bool Reparse(std::shared_ptr PCHContainerOps, ArrayRef RemappedFiles = None); + /// \brief Free data that will be re-generated on the next parse. + /// + /// Preamble-related data is not affected. + void ResetForParse(); + /// \brief Perform code completion at the given file, line, and /// column within this translation unit. /// Index: lib/Frontend/ASTUnit.cpp =================================================================== --- lib/Frontend/ASTUnit.cpp +++ lib/Frontend/ASTUnit.cpp @@ -1020,8 +1020,6 @@ /// contain any translation-unit information, false otherwise. bool ASTUnit::Parse(std::shared_ptr PCHContainerOps, std::unique_ptr OverrideMainBuffer) { - SavedMainFileBuffer.reset(); - if (!Invocation) return true; @@ -1068,17 +1066,11 @@ Clang->createFileManager(); FileMgr = &Clang->getFileManager(); } - SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, - UserFilesAreVolatile); - TheSema.reset(); - Ctx = nullptr; - PP = nullptr; - Reader = nullptr; - // Clear out old caches and data. - TopLevelDecls.clear(); - clearFileLevelDecls(); + ResetForParse(); + SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, + UserFilesAreVolatile); if (!OverrideMainBuffer) { checkAndRemoveNonDriverDiags(StoredDiagnostics); TopLevelDeclsInPreamble.clear(); @@ -2071,6 +2063,19 @@ return Result; } +void ASTUnit::ResetForParse() { + SavedMainFileBuffer.reset(); + + SourceMgr.reset(); + TheSema.reset(); + Ctx.reset(); + PP.reset(); + Reader.reset(); + + TopLevelDecls.clear(); + clearFileLevelDecls(); +} + //----------------------------------------------------------------------------// // Code completion //----------------------------------------------------------------------------// Index: tools/c-index-test/c-index-test.c =================================================================== --- tools/c-index-test/c-index-test.c +++ tools/c-index-test/c-index-test.c @@ -1742,6 +1742,8 @@ return -1; if (Repeats > 1) { + clang_suspendTranslationUnit(TU); + Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, clang_defaultReparseOptions(TU)); if (Err != CXError_Success) { Index: tools/libclang/CIndex.cpp =================================================================== --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -3918,6 +3918,20 @@ } } +unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { + if (CTUnit) { + ASTUnit *Unit = cxtu::getASTUnit(CTUnit); + + if (Unit && Unit->isUnsafeToFree()) + return false; + + Unit->ResetForParse(); + return true; + } + + return false; +} + unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { return CXReparse_None; } Index: tools/libclang/libclang.exports =================================================================== --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -305,6 +305,7 @@ clang_remap_getNumFiles clang_reparseTranslationUnit clang_saveTranslationUnit +clang_suspendTranslationUnit clang_sortCodeCompletionResults clang_toggleCrashRecovery clang_tokenize