[clang] db75db8 - [Introspection] Dont emit json if unchanged.

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 18 12:22:18 PDT 2021


Author: Nathan James
Date: 2021-04-18T20:22:09+01:00
New Revision: db75db85f231bf194912c3b0dd918f2bc497d603

URL: https://github.com/llvm/llvm-project/commit/db75db85f231bf194912c3b0dd918f2bc497d603
DIFF: https://github.com/llvm/llvm-project/commit/db75db85f231bf194912c3b0dd918f2bc497d603.diff

LOG: [Introspection] Dont emit json if unchanged.

Saves running the generate inc script in the, somewhat common, case where the json file doesn't need changing.

Reviewed By: steveire

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

Added: 
    

Modified: 
    clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp b/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
index 8ad187eedfb0..a06f4ff9c4a3 100644
--- a/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
+++ b/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
@@ -10,6 +10,7 @@
 
 #include "clang/Frontend/CompilerInstance.h"
 #include "llvm/Support/JSON.h"
+#include "llvm/Support/MemoryBuffer.h"
 
 using namespace clang::tooling;
 using namespace llvm;
@@ -105,13 +106,27 @@ void WriteJSON(StringRef JsonPath, llvm::json::Object &&ClassInheritance,
   JsonObj["classesInClade"] = std::move(ClassesInClade);
   JsonObj["classEntries"] = std::move(ClassEntries);
 
+  llvm::json::Value JsonVal(std::move(JsonObj));
+
+  bool WriteChange = false;
+  std::string OutString;
+  if (auto ExistingOrErr = MemoryBuffer::getFile(JsonPath, /*IsText=*/true)) {
+    raw_string_ostream Out(OutString);
+    Out << formatv("{0:2}", JsonVal);
+    if (ExistingOrErr.get()->getBuffer() == Out.str())
+      return;
+    WriteChange = true;
+  }
+
   std::error_code EC;
   llvm::raw_fd_ostream JsonOut(JsonPath, EC, llvm::sys::fs::F_Text);
   if (EC)
     return;
 
-  llvm::json::Value JsonVal(std::move(JsonObj));
-  JsonOut << formatv("{0:2}", JsonVal);
+  if (WriteChange)
+    JsonOut << OutString;
+  else
+    JsonOut << formatv("{0:2}", JsonVal);
 }
 
 void ASTSrcLocProcessor::generate() {


        


More information about the cfe-commits mailing list