[Introspection] Dont emit json if unchanged.
authorNathan James <n.james93@hotmail.co.uk>
Sun, 18 Apr 2021 19:22:08 +0000 (20:22 +0100)
committerNathan James <n.james93@hotmail.co.uk>
Sun, 18 Apr 2021 19:22:09 +0000 (20:22 +0100)
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

clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp

index 8ad187e..a06f4ff 100644 (file)
@@ -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() {