r321266 - Added helper to get size of PrecompiledPreamble

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 21 06:04:39 PST 2017


Author: ibiryukov
Date: Thu Dec 21 06:04:39 2017
New Revision: 321266

URL: http://llvm.org/viewvc/llvm-project?rev=321266&view=rev
Log:
Added helper to get size of PrecompiledPreamble

Modified:
    cfe/trunk/include/clang/Frontend/PrecompiledPreamble.h
    cfe/trunk/lib/Frontend/PrecompiledPreamble.cpp

Modified: cfe/trunk/include/clang/Frontend/PrecompiledPreamble.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PrecompiledPreamble.h?rev=321266&r1=321265&r2=321266&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PrecompiledPreamble.h (original)
+++ cfe/trunk/include/clang/Frontend/PrecompiledPreamble.h Thu Dec 21 06:04:39 2017
@@ -19,6 +19,7 @@
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Support/AlignOf.h"
 #include "llvm/Support/MD5.h"
+#include <cstddef>
 #include <memory>
 #include <system_error>
 #include <type_traits>
@@ -89,6 +90,11 @@ public:
   /// PreambleBounds used to build the preamble.
   PreambleBounds getBounds() const;
 
+  /// Returns the size, in bytes, that preamble takes on disk or in memory.
+  /// For on-disk preambles returns 0 if filesystem operations fail. Intended to
+  /// be used for logging and debugging purposes only.
+  std::size_t getSize() const;
+
   /// Check whether PrecompiledPreamble can be reused for the new contents(\p
   /// MainFileBuffer) of the main file.
   bool CanReuse(const CompilerInvocation &Invocation,
@@ -246,7 +252,8 @@ public:
 
   /// Called before FrontendAction::BeginSourceFile.
   /// Can be used to store references to various CompilerInstance fields
-  /// (e.g. SourceManager) that may be interesting to the consumers of other callbacks.
+  /// (e.g. SourceManager) that may be interesting to the consumers of other
+  /// callbacks.
   virtual void BeforeExecute(CompilerInstance &CI);
   /// Called after FrontendAction::Execute(), but before
   /// FrontendAction::EndSourceFile(). Can be used to transfer ownership of

Modified: cfe/trunk/lib/Frontend/PrecompiledPreamble.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrecompiledPreamble.cpp?rev=321266&r1=321265&r2=321266&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PrecompiledPreamble.cpp (original)
+++ cfe/trunk/lib/Frontend/PrecompiledPreamble.cpp Thu Dec 21 06:04:39 2017
@@ -30,7 +30,7 @@
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/MutexGuard.h"
 #include "llvm/Support/Process.h"
-
+#include <limits>
 #include <utility>
 
 using namespace clang;
@@ -381,6 +381,27 @@ PreambleBounds PrecompiledPreamble::getB
   return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
 }
 
+std::size_t PrecompiledPreamble::getSize() const {
+  switch (Storage.getKind()) {
+  case PCHStorage::Kind::Empty:
+    assert(false && "Calling getSize() on invalid PrecompiledPreamble. "
+                    "Was it std::moved?");
+    return 0;
+  case PCHStorage::Kind::InMemory:
+    return Storage.asMemory().Data.size();
+  case PCHStorage::Kind::TempFile: {
+    uint64_t Result;
+    if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(), Result))
+      return 0;
+
+    assert(Result <= std::numeric_limits<std::size_t>::max() &&
+           "file size did not fit into size_t");
+    return Result;
+  }
+  }
+  llvm_unreachable("Unhandled storage kind");
+}
+
 bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation,
                                    const llvm::MemoryBuffer *MainFileBuffer,
                                    PreambleBounds Bounds,
@@ -506,8 +527,8 @@ PrecompiledPreamble::TempPCHFile::create
                                                         StringRef Suffix) {
   llvm::SmallString<64> File;
   // Using a version of createTemporaryFile with a file descriptor guarantees
-  // that we would never get a race condition in a multi-threaded setting (i.e.,
-  // multiple threads getting the same temporary path).
+  // that we would never get a race condition in a multi-threaded setting
+  // (i.e., multiple threads getting the same temporary path).
   int FD;
   auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File);
   if (EC)




More information about the cfe-commits mailing list