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() {