diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h --- a/llvm/include/llvm/Support/Error.h +++ b/llvm/include/llvm/Support/Error.h @@ -1286,6 +1286,13 @@ Error createFileError(const Twine &F, ErrorSuccess) = delete; +#ifndef NDEBUG +#define llvm_SourceLocationError(E) \ + ::llvm::createFileError(__FILE__, __LINE__, E) +#else +#define llvm_SourceLocationError(E) E +#endif + /// Helper for check-and-exit error handling. /// /// For tool use only. NOT FOR USE IN LIBRARY CODE. diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp --- a/llvm/unittests/Support/ErrorTest.cpp +++ b/llvm/unittests/Support/ErrorTest.cpp @@ -906,6 +906,24 @@ 0); } +TEST(Error, SourceLocationErrorTest) { + std::string ErrStr; + raw_string_ostream OS(ErrStr); + +#if defined(NDEBUG) + // __FILE__ and __LINE_ not added + OS << "^CustomError \\{1\\}$"; + auto E = llvm_SourceLocationError(make_error(1)); + EXPECT_THAT(toString(std::move(FE1)), ::testing::ContainsRegex(OS.str())); +#else + // __FILE__ and __LINE__ added + int Line = __LINE__; + OS << "'" << __FILE__ << "': line " << (Line + 2) << ": CustomError \\{1\\}$"; + auto E = llvm_SourceLocationError(make_error(1)); + EXPECT_THAT(toString(std::move(E)), ::testing::ContainsRegex(OS.str())); +#endif +} + enum class test_error_code { unspecified = 1, error_1,