[llvm] 0c50250 - FileError: Support zero-length file names

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 8 16:17:42 PDT 2021


Author: David Blaikie
Date: 2021-09-08T16:16:54-07:00
New Revision: 0c502507f4627f72eeecbb5e78c2f9c1cb8f9219

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

LOG: FileError: Support zero-length file names

It's a common error in an API - to try to open an empty file, so it
seems like a reasonable FileError to produce "hey, you tried to open an
empty file" and to handle it the same way as any other file error.

Added: 
    

Modified: 
    llvm/include/llvm/Support/Error.h
    llvm/unittests/Support/ErrorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 707e0b98b9005..77ff2f47d463c 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1259,7 +1259,7 @@ class FileError final : public ErrorInfo<FileError> {
 
 public:
   void log(raw_ostream &OS) const override {
-    assert(Err && !FileName.empty() && "Trying to log after takeError().");
+    assert(Err && "Trying to log after takeError().");
     OS << "'" << FileName << "': ";
     if (Line.hasValue())
       OS << "line " << Line.getValue() << ": ";
@@ -1279,8 +1279,6 @@ class FileError final : public ErrorInfo<FileError> {
   FileError(const Twine &F, Optional<size_t> LineNum,
             std::unique_ptr<ErrorInfoBase> E) {
     assert(E && "Cannot create FileError from Error success value.");
-    assert(!F.isTriviallyEmpty() &&
-           "The file name provided to FileError must not be empty.");
     FileName = F.str();
     Err = std::move(E);
     Line = std::move(LineNum);

diff  --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index d9e86fdd617ef..93564026743a0 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -957,6 +957,9 @@ TEST(Error, FileErrorTest) {
                 .compare("'file.bin': CustomError {41}\n"
                          "'file2.bin': CustomError {42}"),
             0);
+
+  Error FE5 = createFileError("", make_error<CustomError>(1));
+  EXPECT_EQ(toString(std::move(FE5)).compare("'': CustomError {1}"), 0);
 }
 
 enum class test_error_code {


        


More information about the llvm-commits mailing list