[clang-tools-extra] r369925 - [clang-doc] Switch Generator::CreateResources to use llvm::Error

Julie Hockett via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 09:39:48 PDT 2019


Author: juliehockett
Date: Mon Aug 26 09:39:48 2019
New Revision: 369925

URL: http://llvm.org/viewvc/llvm-project?rev=369925&view=rev
Log:
[clang-doc] Switch Generator::CreateResources to use llvm::Error

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

Modified:
    clang-tools-extra/trunk/clang-doc/Generators.cpp
    clang-tools-extra/trunk/clang-doc/Generators.h
    clang-tools-extra/trunk/clang-doc/HTMLGenerator.cpp
    clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp

Modified: clang-tools-extra/trunk/clang-doc/Generators.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/Generators.cpp?rev=369925&r1=369924&r2=369925&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/Generators.cpp (original)
+++ clang-tools-extra/trunk/clang-doc/Generators.cpp Mon Aug 26 09:39:48 2019
@@ -57,7 +57,9 @@ std::string getTagType(TagTypeKind AS) {
   llvm_unreachable("Unknown TagTypeKind");
 }
 
-bool Generator::createResources(ClangDocContext &CDCtx) { return true; }
+llvm::Error Generator::createResources(ClangDocContext &CDCtx) {
+  return llvm::Error::success();
+}
 
 // A function to add a reference to Info in Idx.
 // Given an Info X with the following namespaces: [B,A]; a reference to X will

Modified: clang-tools-extra/trunk/clang-doc/Generators.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/Generators.h?rev=369925&r1=369924&r2=369925&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/Generators.h (original)
+++ clang-tools-extra/trunk/clang-doc/Generators.h Mon Aug 26 09:39:48 2019
@@ -32,7 +32,7 @@ public:
   // It can be overwritten by any of the inherited generators.
   // If the override method wants to run this it should call
   // Generator::createResources(CDCtx);
-  virtual bool createResources(ClangDocContext &CDCtx);
+  virtual llvm::Error createResources(ClangDocContext &CDCtx);
 
   static void addInfoToIndex(Index &Idx, const doc::Info *Info);
 };

Modified: clang-tools-extra/trunk/clang-doc/HTMLGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/HTMLGenerator.cpp?rev=369925&r1=369924&r2=369925&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/HTMLGenerator.cpp (original)
+++ clang-tools-extra/trunk/clang-doc/HTMLGenerator.cpp Mon Aug 26 09:39:48 2019
@@ -829,7 +829,7 @@ public:
 
   llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
                                  const ClangDocContext &CDCtx) override;
-  bool createResources(ClangDocContext &CDCtx) override;
+  llvm::Error createResources(ClangDocContext &CDCtx) override;
 };
 
 const char *HTMLGenerator::Format = "html";
@@ -883,7 +883,7 @@ static std::string getRefType(InfoType I
   llvm_unreachable("Unknown InfoType");
 }
 
-static bool SerializeIndex(ClangDocContext &CDCtx) {
+static llvm::Error SerializeIndex(ClangDocContext &CDCtx) {
   std::error_code OK;
   std::error_code FileErr;
   llvm::SmallString<128> FilePath;
@@ -891,8 +891,9 @@ static bool SerializeIndex(ClangDocConte
   llvm::sys::path::append(FilePath, "index_json.js");
   llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::F_None);
   if (FileErr != OK) {
-    llvm::errs() << "Error creating index file: " << FileErr.message() << "\n";
-    return false;
+    return llvm::make_error<llvm::StringError>(
+        "Error creating index file: " + FileErr.message() + "\n",
+        llvm::inconvertibleErrorCode());
   }
   CDCtx.Idx.sort();
   llvm::json::OStream J(OS, 2);
@@ -911,7 +912,7 @@ static bool SerializeIndex(ClangDocConte
   OS << "var JsonIndex = `\n";
   IndexToJSON(CDCtx.Idx);
   OS << "`;\n";
-  return true;
+  return llvm::Error::success();
 }
 
 // Generates a main HTML node that has the main content of the file that shows
@@ -932,15 +933,16 @@ static std::unique_ptr<TagNode> genIndex
   return MainNode;
 }
 
-static bool GenIndex(const ClangDocContext &CDCtx) {
+static llvm::Error GenIndex(const ClangDocContext &CDCtx) {
   std::error_code FileErr, OK;
   llvm::SmallString<128> IndexPath;
   llvm::sys::path::native(CDCtx.OutDirectory, IndexPath);
   llvm::sys::path::append(IndexPath, "index.html");
   llvm::raw_fd_ostream IndexOS(IndexPath, FileErr, llvm::sys::fs::F_None);
   if (FileErr != OK) {
-    llvm::errs() << "Error creating main index: " << FileErr.message() << "\n";
-    return false;
+    return llvm::make_error<llvm::StringError>(
+        "Error creating main index: " + FileErr.message() + "\n",
+        llvm::inconvertibleErrorCode());
   }
 
   HTMLFile F;
@@ -958,10 +960,10 @@ static bool GenIndex(const ClangDocConte
 
   F.Render(IndexOS);
 
-  return true;
+  return llvm::Error::success();
 }
 
-static bool CopyFile(StringRef FilePath, StringRef OutDirectory) {
+static llvm::Error CopyFile(StringRef FilePath, StringRef OutDirectory) {
   llvm::SmallString<128> PathWrite;
   llvm::sys::path::native(OutDirectory, PathWrite);
   llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
@@ -970,24 +972,33 @@ static bool CopyFile(StringRef FilePath,
   std::error_code OK;
   std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
   if (FileErr != OK) {
-    llvm::errs() << "Error creating file "
-                 << llvm::sys::path::filename(FilePath) << ": "
-                 << FileErr.message() << "\n";
-    return false;
-  }
-  return true;
-}
-
-bool HTMLGenerator::createResources(ClangDocContext &CDCtx) {
-  if (!SerializeIndex(CDCtx) || !GenIndex(CDCtx))
-    return false;
-  for (const auto &FilePath : CDCtx.UserStylesheets)
-    if (!CopyFile(FilePath, CDCtx.OutDirectory))
-      return false;
-  for (const auto &FilePath : CDCtx.FilesToCopy)
-    if (!CopyFile(FilePath, CDCtx.OutDirectory))
-      return false;
-  return true;
+    return llvm::make_error<llvm::StringError>(
+        "Error creating file " + llvm::sys::path::filename(FilePath) + ": " +
+            FileErr.message() + "\n",
+        llvm::inconvertibleErrorCode());
+  }
+  return llvm::Error::success();
+}
+
+llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) {
+  auto Err = SerializeIndex(CDCtx);
+  if (Err)
+    return Err;
+  Err = GenIndex(CDCtx);
+  if (Err)
+    return Err;
+
+  for (const auto &FilePath : CDCtx.UserStylesheets) {
+    Err = CopyFile(FilePath, CDCtx.OutDirectory);
+    if (Err)
+      return Err;
+  }
+  for (const auto &FilePath : CDCtx.FilesToCopy) {
+    Err = CopyFile(FilePath, CDCtx.OutDirectory);
+    if (Err)
+      return Err;
+  }
+  return llvm::Error::success();
 }
 
 static GeneratorRegistry::Add<HTMLGenerator> HTML(HTMLGenerator::Format,

Modified: clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp?rev=369925&r1=369924&r2=369925&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp (original)
+++ clang-tools-extra/trunk/clang-doc/tool/ClangDocMain.cpp Mon Aug 26 09:39:48 2019
@@ -326,8 +326,11 @@ int main(int argc, const char **argv) {
     return 1;
 
   llvm::outs() << "Generating assets for docs...\n";
-  if (!G->get()->createResources(CDCtx))
+  Err = G->get()->createResources(CDCtx);
+  if (Err) {
+    llvm::errs() << toString(std::move(Err)) << "\n";
     return 1;
+  }
 
   return 0;
 }




More information about the cfe-commits mailing list