================
@@ -0,0 +1,713 @@
+#include "clang/Analysis/Scalable/Serialization/JSONFormat.h"
+#include "clang/Analysis/Scalable/TUSummary/TUSummary.h"
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/JSON.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Registry.h"
+
+using namespace clang::ssaf;
+
+namespace {
+// Helper to wrap an error with additional context
+template <typename... Args>
+llvm::Error wrapError(llvm::Error E, std::errc ErrorCode, const char *Fmt,
+                      Args &&...Vals) {
+  return llvm::joinErrors(
+      llvm::createStringError(ErrorCode, Fmt, std::forward<Args>(Vals)...),
+      std::move(E));
+}
+
+// Convenience version that defaults to invalid_argument
+template <typename... Args>
+llvm::Error wrapError(llvm::Error E, const char *Fmt, Args &&...Vals) {
+  return wrapError(std::move(E), std::errc::invalid_argument, Fmt,
+                   std::forward<Args>(Vals)...);
+}
+
+} // namespace
+
+//----------------------------------------------------------------------------
+// JSON Reader and Writer
+//----------------------------------------------------------------------------
+
+llvm::Error isJSONFile(llvm::StringRef Path) {
+  if (!llvm::sys::fs::exists(Path))
+    return llvm::createStringError(std::errc::no_such_file_or_directory,
+                                   "file does not exist: '%s'",
+                                   Path.str().c_str());
+
+  if (llvm::sys::fs::is_directory(Path))
+    return llvm::createStringError(std::errc::is_a_directory,
+                                   "path is a directory, not a file: '%s'",
+                                   Path.str().c_str());
+
+  if (!Path.ends_with_insensitive(".json"))
+    return llvm::createStringError(std::errc::invalid_argument,
+                                   "not a JSON file: '%s'", 
Path.str().c_str());
+
+  return llvm::Error::success();
+}
+
+llvm::Expected<llvm::json::Value> readJSON(llvm::StringRef Path) {
+  if (llvm::Error Err = isJSONFile(Path))
+    return wrapError(std::move(Err), "failed to validate JSON file '%s'",
+                     Path.str().c_str());
+
+  auto BufferOrError = llvm::MemoryBuffer::getFile(Path);
+  if (!BufferOrError) {
+    return llvm::createStringError(BufferOrError.getError(),
----------------
aviralg wrote:

Path tested by `error-permission-denied.test`.

https://github.com/llvm/llvm-project/pull/180021
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to