[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-30 Thread Erik Verbruggen via Phabricator via cfe-commits
erikjv closed this revision.
erikjv added a comment.

Committed as r304212.


https://reviews.llvm.org/D33042



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-29 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik added a comment.

I'm new to this review tool. I've addressed the comments and rebased. Does this 
needs a re-review? Note that this is my first change and I probably do not have 
any permissions to submit this change.


https://reviews.llvm.org/D33042



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-22 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik updated this revision to Diff 99718.
nik edited the summary of this revision.

https://reviews.llvm.org/D33042

Files:
  include/clang-c/Index.h
  include/clang/Frontend/ASTUnit.h
  lib/Frontend/ASTUnit.cpp
  tools/c-index-test/c-index-test.c
  tools/libclang/CIndex.cpp
  tools/libclang/libclang.exports

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
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/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: 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 = >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: 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: 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) * 1)   \
@@ -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);
___
cfe-commits mailing list

[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-22 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik marked an inline comment as done.
nik added a comment.

Adapted to the changes that happened in the meanwhile (CINDEX_VERSION_MINOR 
increased, CleanTemporaryFiles got removed).


https://reviews.llvm.org/D33042



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-19 Thread Benjamin Kramer via Phabricator via cfe-commits
bkramer accepted this revision.
bkramer added a comment.
This revision is now accepted and ready to land.

This makes sense to me.




Comment at: lib/Frontend/ASTUnit.cpp:2089
+void ASTUnit::ResetForParse()
+{
+  SavedMainFileBuffer.reset();

Put the `{` on the same line as the decl.


https://reviews.llvm.org/D33042



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-18 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik added a comment.

Ping :)


https://reviews.llvm.org/D33042



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-10 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik updated this revision to Diff 98461.

https://reviews.llvm.org/D33042

Files:
  include/clang-c/Index.h
  include/clang/Frontend/ASTUnit.h
  lib/Frontend/ASTUnit.cpp
  tools/c-index-test/c-index-test.c
  tools/libclang/CIndex.cpp
  tools/libclang/libclang.exports

Index: tools/libclang/libclang.exports
===
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -304,6 +304,7 @@
 clang_remap_getNumFiles
 clang_reparseTranslationUnit
 clang_saveTranslationUnit
+clang_suspendTranslationUnit
 clang_sortCodeCompletionResults
 clang_toggleCrashRecovery
 clang_tokenize
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/c-index-test/c-index-test.c
===
--- tools/c-index-test/c-index-test.c
+++ tools/c-index-test/c-index-test.c
@@ -1729,6 +1729,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: lib/Frontend/ASTUnit.cpp
===
--- lib/Frontend/ASTUnit.cpp
+++ lib/Frontend/ASTUnit.cpp
@@ -1042,8 +1042,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;
 
@@ -1090,18 +1088,11 @@
 Clang->createFileManager();
 FileMgr = >getFileManager();
   }
-  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
-UserFilesAreVolatile);
-  TheSema.reset();
-  Ctx = nullptr;
-  PP = nullptr;
-  Reader = nullptr;
 
-  // Clear out old caches and data.
-  TopLevelDecls.clear();
-  clearFileLevelDecls();
-  CleanTemporaryFiles();
+  ResetForParse();
 
+  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
+UserFilesAreVolatile);
   if (!OverrideMainBuffer) {
 checkAndRemoveNonDriverDiags(StoredDiagnostics);
 TopLevelDeclsInPreamble.clear();
@@ -2094,6 +2085,21 @@
   return Result;
 }
 
+void ASTUnit::ResetForParse()
+{
+  SavedMainFileBuffer.reset();
+
+  SourceMgr.reset();
+  TheSema.reset();
+  Ctx.reset();
+  PP.reset();
+  Reader.reset();
+
+  TopLevelDecls.clear();
+  clearFileLevelDecls();
+  CleanTemporaryFiles();
+}
+
 ////
 // Code completion
 ////
Index: include/clang/Frontend/ASTUnit.h
===
--- include/clang/Frontend/ASTUnit.h
+++ include/clang/Frontend/ASTUnit.h
@@ -858,6 +858,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: 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 38
+#define CINDEX_VERSION_MINOR 39
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
   ((major) * 1)   \
@@ -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);
___
cfe-commits mailing 

[PATCH] D33042: [libclang] Allow to suspend a translation unit.

2017-05-10 Thread Nikolai Kosjar via Phabricator via cfe-commits
nik created this revision.
Herald added a subscriber: klimek.

A suspended translation unit uses significantly less memory but on the other
side does not support any other calls than clang_reparseTranslationUnit to
resume it or clang_disposeTranslationUnit to dispose it completely.

This helps IDEs to reduce the memory footprint. The data that is freed
by a call to clang_suspendTranslationUnit will be re-generated on the
next (re)parse anyway. Used with a preamble, this allows pretty fast
resumption of the translation for further use (compared to disposal of
the translation unit and a parse from scratch).


Repository:
  rL LLVM

https://reviews.llvm.org/D33042

Files:
  include/clang-c/Index.h
  include/clang/Frontend/ASTUnit.h
  lib/Format/UnwrappedLineParser.cpp
  lib/Frontend/ASTUnit.cpp
  tools/c-index-test/c-index-test.c
  tools/libclang/CIndex.cpp
  tools/libclang/libclang.exports
  unittests/Format/FormatTestJS.cpp

Index: unittests/Format/FormatTestJS.cpp
===
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -470,6 +470,16 @@
"  inner2(a, b);\n"
"}");
   verifyFormat("function f() {}");
+  verifyFormat("function aFunction() {}\n"
+   "(function f() {\n"
+   "  var x = 1;\n"
+   "}());\n");
+  // Known issue: this should wrap after {}, but calculateBraceTypes
+  // misclassifies the first braces as a BK_BracedInit.
+  verifyFormat("function aFunction(){} {\n"
+   "  let x = 1;\n"
+   "  console.log(x);\n"
+   "}\n");
 }
 
 TEST_F(FormatTestJS, GeneratorFunctions) {
Index: tools/libclang/libclang.exports
===
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -304,6 +304,7 @@
 clang_remap_getNumFiles
 clang_reparseTranslationUnit
 clang_saveTranslationUnit
+clang_suspendTranslationUnit
 clang_sortCodeCompletionResults
 clang_toggleCrashRecovery
 clang_tokenize
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/c-index-test/c-index-test.c
===
--- tools/c-index-test/c-index-test.c
+++ tools/c-index-test/c-index-test.c
@@ -1729,6 +1729,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: lib/Frontend/ASTUnit.cpp
===
--- lib/Frontend/ASTUnit.cpp
+++ lib/Frontend/ASTUnit.cpp
@@ -1042,8 +1042,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;
 
@@ -1090,18 +1088,11 @@
 Clang->createFileManager();
 FileMgr = >getFileManager();
   }
-  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
-UserFilesAreVolatile);
-  TheSema.reset();
-  Ctx = nullptr;
-  PP = nullptr;
-  Reader = nullptr;
 
-  // Clear out old caches and data.
-  TopLevelDecls.clear();
-  clearFileLevelDecls();
-  CleanTemporaryFiles();
+  ResetForParse();
 
+  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
+UserFilesAreVolatile);
   if (!OverrideMainBuffer) {
 checkAndRemoveNonDriverDiags(StoredDiagnostics);
 TopLevelDeclsInPreamble.clear();
@@ -2094,6 +2085,21 @@
   return Result;
 }
 
+void ASTUnit::ResetForParse()
+{
+  SavedMainFileBuffer.reset();
+
+  SourceMgr.reset();
+  TheSema.reset();
+  Ctx.reset();
+  PP.reset();
+  Reader.reset();
+
+  TopLevelDecls.clear();
+  clearFileLevelDecls();
+  CleanTemporaryFiles();
+}
+
 ////
 // Code completion
 ////
Index: lib/Format/UnwrappedLineParser.cpp
===
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -368,9 +368,10 @@
   (Style.Language == FormatStyle::LK_JavaScript &&