]> granicus.if.org Git - clang/commitdiff
[PCH] Add an option to not write comments into PCH
authorIlya Biryukov <ibiryukov@google.com>
Mon, 9 Jul 2018 11:33:23 +0000 (11:33 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Mon, 9 Jul 2018 11:33:23 +0000 (11:33 +0000)
Summary:
Will be used in clangd, see the follow-up change.
Clangd does not use comments read from PCH to avoid crashes due to
changed contents of the file. However, reading them considerably slows
down code completion on files with large preambles.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ioeric, cfe-commits

Differential Revision: https://reviews.llvm.org/D48942

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

include/clang/Lex/PreprocessorOptions.h
lib/Serialization/ASTWriter.cpp

index f4ec2f27c405239a9366db732a25aaaa2255bae2..3d7e5ab4a8dc72c3b27122c0d1b814bfb33b5e0a 100644 (file)
@@ -95,6 +95,11 @@ public:
   /// processing the rest of the file.
   bool GeneratePreamble = false;
 
+  /// Whether to write comment locations into the PCH when building it.
+  /// Reading the comments from the PCH can be a performance hit even if the
+  /// clients don't use them.
+  bool WriteCommentListToPCH = true;
+
   /// The implicit PTH input included at the start of the translation unit, or
   /// empty.
   std::string ImplicitPTHInclude;
index 279960a129bda305277490de95adcd775c7dcdc6..bc569d17f2f1abb456b6220c11743a520919a0b4 100644 (file)
@@ -78,6 +78,7 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
@@ -3252,6 +3253,9 @@ void ASTWriter::WriteFileDeclIDsMap() {
 
 void ASTWriter::WriteComments() {
   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
+  auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
+  if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
+    return;
   ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
   RecordData Record;
   for (const auto *I : RawComments) {
@@ -3262,7 +3266,6 @@ void ASTWriter::WriteComments() {
     Record.push_back(I->isAlmostTrailingComment());
     Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
   }
-  Stream.ExitBlock();
 }
 
 //===----------------------------------------------------------------------===//