diff options
author | Gil <mcg@google.com> | 2018-05-22 13:21:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-22 13:21:08 -0700 |
commit | d439bbccd4b90583a89d209d2cc81308aabca8ac (patch) | |
tree | 13fb14cc905f667e1470bcc14a3c84dfb6a7a109 /Firestore/core/src/firebase/firestore/util | |
parent | 476be0ba2ba8340296a5b5b05f27f3ded4bd6c72 (diff) |
Add a HARD_ASSERT C++ assertion macro (#1304)
* Add HARD_ASSERT
* Use HARD_ASSERT
* Remove FIREBASE_ASSERT
* Remove StringPrintf
Diffstat (limited to 'Firestore/core/src/firebase/firestore/util')
17 files changed, 207 insertions, 379 deletions
diff --git a/Firestore/core/src/firebase/firestore/util/CMakeLists.txt b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt index 2c12fa4..043713f 100644 --- a/Firestore/core/src/firebase/firestore/util/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt @@ -24,8 +24,6 @@ cc_library( SOURCES string_format.cc string_format.h - string_printf.cc - string_printf.h DEPENDS absl_base absl_strings @@ -36,8 +34,8 @@ cc_library( cc_library( firebase_firestore_util_log_stdio SOURCES - firebase_assert.h - assert_stdio.cc + hard_assert_stdio.cc + hard_assert.h log.h log_stdio.cc DEPENDS @@ -49,14 +47,15 @@ cc_library( cc_library( firebase_firestore_util_log_apple SOURCES - firebase_assert.h - assert_apple.mm + hard_assert.h + hard_assert_apple.mm log.h log_apple.mm string_apple.h DEPENDS FirebaseCore absl_strings + firebase_firestore_util_base EXCLUDE_FROM_ALL ) diff --git a/Firestore/core/src/firebase/firestore/util/async_queue.cc b/Firestore/core/src/firebase/firestore/util/async_queue.cc index 81aac7c..b42dec3 100644 --- a/Firestore/core/src/firebase/firestore/util/async_queue.cc +++ b/Firestore/core/src/firebase/firestore/util/async_queue.cc @@ -18,7 +18,7 @@ #include <utility> -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "absl/memory/memory.h" namespace firebase { @@ -35,27 +35,26 @@ AsyncQueue::AsyncQueue(std::unique_ptr<Executor> executor) // TODO(varconst): assert in destructor that the queue is empty. void AsyncQueue::VerifyIsCurrentExecutor() const { - FIREBASE_ASSERT_MESSAGE( + HARD_ASSERT( executor_->IsCurrentExecutor(), "Expected to be called by the executor associated with this queue " "(expected executor: '%s', actual executor: '%s')", - executor_->Name().c_str(), executor_->CurrentExecutorName().c_str()); + executor_->Name(), executor_->CurrentExecutorName()); } void AsyncQueue::VerifyIsCurrentQueue() const { VerifyIsCurrentExecutor(); - FIREBASE_ASSERT_MESSAGE( - is_operation_in_progress_, - "VerifyIsCurrentQueue called when no operation is executing " - "(expected executor: '%s', actual executor: '%s')", - executor_->Name().c_str(), executor_->CurrentExecutorName().c_str()); + HARD_ASSERT(is_operation_in_progress_, + "VerifyIsCurrentQueue called when no operation is executing " + "(expected executor: '%s', actual executor: '%s')", + executor_->Name(), executor_->CurrentExecutorName()); } void AsyncQueue::ExecuteBlocking(const Operation& operation) { VerifyIsCurrentExecutor(); - FIREBASE_ASSERT_MESSAGE(!is_operation_in_progress_, - "ExecuteBlocking may not be called " - "before the previous operation finishes executing"); + HARD_ASSERT(!is_operation_in_progress_, + "ExecuteBlocking may not be called " + "before the previous operation finishes executing"); is_operation_in_progress_ = true; operation(); @@ -79,9 +78,8 @@ DelayedOperation AsyncQueue::EnqueueAfterDelay(const Milliseconds delay, // While not necessarily harmful, we currently don't expect to have multiple // callbacks with the same timer_id in the queue, so defensively reject // them. - FIREBASE_ASSERT_MESSAGE( - !IsScheduled(timer_id), - "Attempted to schedule multiple operations with id %d", timer_id); + HARD_ASSERT(!IsScheduled(timer_id), + "Attempted to schedule multiple operations with id %s", timer_id); Executor::TaggedOperation tagged{static_cast<int>(timer_id), Wrap(operation)}; return executor_->Schedule(delay, std::move(tagged)); @@ -97,12 +95,11 @@ AsyncQueue::Operation AsyncQueue::Wrap(const Operation& operation) { void AsyncQueue::VerifySequentialOrder() const { // This is the inverse of `VerifyIsCurrentQueue`. - FIREBASE_ASSERT_MESSAGE( - !is_operation_in_progress_ || !executor_->IsCurrentExecutor(), - "Enqueue methods cannot be called when we are already running on " - "target executor" - "(this queue's executor: '%s', current executor: '%s')", - executor_->Name().c_str(), executor_->CurrentExecutorName().c_str()); + HARD_ASSERT(!is_operation_in_progress_ || !executor_->IsCurrentExecutor(), + "Enqueue methods cannot be called when we are already running on " + "target executor " + "(this queue's executor: '%s', current executor: '%s')", + executor_->Name(), executor_->CurrentExecutorName()); } // Test-only functions @@ -117,14 +114,13 @@ bool AsyncQueue::IsScheduled(const TimerId timer_id) const { } void AsyncQueue::RunScheduledOperationsUntil(const TimerId last_timer_id) { - FIREBASE_ASSERT_MESSAGE( - !executor_->IsCurrentExecutor(), - "RunScheduledOperationsUntil must not be called on the queue"); + HARD_ASSERT(!executor_->IsCurrentExecutor(), + "RunScheduledOperationsUntil must not be called on the queue"); executor_->ExecuteBlocking([this, last_timer_id] { - FIREBASE_ASSERT_MESSAGE( + HARD_ASSERT( last_timer_id == TimerId::All || IsScheduled(last_timer_id), - "Attempted to run scheduled operations until missing timer id: %d", + "Attempted to run scheduled operations until missing timer id: %s", last_timer_id); for (auto next = executor_->PopFromSchedule(); next.has_value(); diff --git a/Firestore/core/src/firebase/firestore/util/bits.cc b/Firestore/core/src/firebase/firestore/util/bits.cc index 0bfe4c3..03d9b77 100644 --- a/Firestore/core/src/firebase/firestore/util/bits.cc +++ b/Firestore/core/src/firebase/firestore/util/bits.cc @@ -16,7 +16,7 @@ #include "Firestore/core/src/firebase/firestore/util/bits.h" -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" namespace firebase { namespace firestore { @@ -34,7 +34,7 @@ int Bits::Log2Floor_Portable(uint32_t n) { log += shift; } } - FIREBASE_ASSERT(value == 1); + HARD_ASSERT(value == 1); return log; } diff --git a/Firestore/core/src/firebase/firestore/util/executor_libdispatch.h b/Firestore/core/src/firebase/firestore/util/executor_libdispatch.h index f913fe5..d356a74 100644 --- a/Firestore/core/src/firebase/firestore/util/executor_libdispatch.h +++ b/Firestore/core/src/firebase/firestore/util/executor_libdispatch.h @@ -26,7 +26,6 @@ #include "dispatch/dispatch.h" #include "Firestore/core/src/firebase/firestore/util/executor.h" -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" #include "absl/strings/string_view.h" #if !defined(__OBJC__) diff --git a/Firestore/core/src/firebase/firestore/util/executor_libdispatch.mm b/Firestore/core/src/firebase/firestore/util/executor_libdispatch.mm index 597d450..70d0b3a 100644 --- a/Firestore/core/src/firebase/firestore/util/executor_libdispatch.mm +++ b/Firestore/core/src/firebase/firestore/util/executor_libdispatch.mm @@ -16,6 +16,8 @@ #include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" + namespace firebase { namespace firestore { namespace util { @@ -55,7 +57,7 @@ void DispatchAsync(const dispatch_queue_t queue, std::function<void()>&& work) { } void DispatchSync(const dispatch_queue_t queue, std::function<void()> work) { - FIREBASE_ASSERT_MESSAGE( + HARD_ASSERT( GetCurrentQueueLabel() != GetQueueLabel(queue), "Calling DispatchSync on the current queue will lead to a deadlock."); @@ -176,8 +178,8 @@ void TimeSlot::Execute() { RemoveFromSchedule(); - FIREBASE_ASSERT_MESSAGE(tagged_.operation, - "TimeSlot contains an invalid function object"); + HARD_ASSERT(tagged_.operation, + "TimeSlot contains an invalid function object"); tagged_.operation(); } diff --git a/Firestore/core/src/firebase/firestore/util/executor_std.cc b/Firestore/core/src/firebase/firestore/util/executor_std.cc index f03a712..a87ec20 100644 --- a/Firestore/core/src/firebase/firestore/util/executor_std.cc +++ b/Firestore/core/src/firebase/firestore/util/executor_std.cc @@ -64,8 +64,7 @@ DelayedOperation ExecutorStd::Schedule(const Milliseconds delay, // While negative delay can be interpreted as a request for immediate // execution, supporting it would provide a hacky way to modify FIFO ordering // of immediate operations. - FIREBASE_ASSERT_MESSAGE(delay.count() >= 0, - "Schedule: delay cannot be negative"); + HARD_ASSERT(delay.count() >= 0, "Schedule: delay cannot be negative"); namespace chr = std::chrono; const auto now = chr::time_point_cast<Milliseconds>(chr::steady_clock::now()); diff --git a/Firestore/core/src/firebase/firestore/util/executor_std.h b/Firestore/core/src/firebase/firestore/util/executor_std.h index b6f0b6b..efe60db 100644 --- a/Firestore/core/src/firebase/firestore/util/executor_std.h +++ b/Firestore/core/src/firebase/firestore/util/executor_std.h @@ -27,7 +27,7 @@ #include <utility> #include "Firestore/core/src/firebase/firestore/util/executor.h" -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "absl/types/optional.h" namespace firebase { @@ -181,8 +181,8 @@ class Schedule { // This function expects the mutex to be already locked. T ExtractLocked(const Iterator where) { - FIREBASE_ASSERT_MESSAGE(!scheduled_.empty(), - "Trying to pop an entry from an empty queue."); + HARD_ASSERT(!scheduled_.empty(), + "Trying to pop an entry from an empty queue."); T result = std::move(where->value); scheduled_.erase(where); diff --git a/Firestore/core/src/firebase/firestore/util/firebase_assert.h b/Firestore/core/src/firebase/firestore/util/firebase_assert.h deleted file mode 100644 index 6a9c2eb..0000000 --- a/Firestore/core/src/firebase/firestore/util/firebase_assert.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2018 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// To avoid naming-collision, this header is called firebase_assert.h instead -// of assert.h. - -#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_FIREBASE_ASSERT_H_ -#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_FIREBASE_ASSERT_H_ - -#include <cstdlib> - -#include "Firestore/core/src/firebase/firestore/util/log.h" -#include "absl/base/attributes.h" - -#define FIREBASE_EXPAND_STRINGIFY_(X) #X -#define FIREBASE_EXPAND_STRINGIFY(X) FIREBASE_EXPAND_STRINGIFY_(X) - -// FIREBASE_ASSERT_* macros are not compiled out of release builds. They should -// be used for assertions that need to be propagated to end-users of SDKs. -// FIREBASE_DEV_ASSERT_* macros are compiled out of release builds, similar to -// the C assert() macro. They should be used for internal assertions that are -// only shown to SDK developers. - -// Assert condition is true, if it's false log an assert with the specified -// expression as a string. -#define FIREBASE_ASSERT_WITH_EXPRESSION(condition, expression) \ - do { \ - if (!(condition)) { \ - firebase::firestore::util::FailAssert( \ - __FILE__, __PRETTY_FUNCTION__, __LINE__, \ - FIREBASE_EXPAND_STRINGIFY(expression)); \ - } \ - } while (0) - -// Assert condition is true, if it's false log an assert with the specified -// expression as a string. Compiled out of release builds. -#if defined(NDEBUG) -#define FIREBASE_DEV_ASSERT_WITH_EXPRESSION(condition, expression) \ - { (void)(condition); } -#else -#define FIREBASE_DEV_ASSERT_WITH_EXPRESSION(condition, expression) \ - FIREBASE_ASSERT_WITH_EXPRESSION(condition, expression) -#endif // defined(NDEBUG) - -// Custom assert() implementation that is not compiled out in release builds. -#define FIREBASE_ASSERT(expression) \ - FIREBASE_ASSERT_WITH_EXPRESSION(expression, expression) - -// Custom assert() implementation that is compiled out in release builds. -// Compiled out of release builds. -#define FIREBASE_DEV_ASSERT(expression) \ - FIREBASE_DEV_ASSERT_WITH_EXPRESSION(expression, expression) - -// Assert condition is true otherwise display the specified expression, -// message and abort. -#define FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION(condition, expression, ...) \ - do { \ - if (!(condition)) { \ - firebase::firestore::util::LogError( \ - FIREBASE_EXPAND_STRINGIFY(expression)); \ - firebase::firestore::util::FailAssert(__FILE__, __PRETTY_FUNCTION__, \ - __LINE__, __VA_ARGS__); \ - } \ - } while (0) - -// Assert condition is true otherwise display the specified expression, -// message and abort. Compiled out of release builds. -#if defined(NDEBUG) -#define FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION(condition, expression, \ - ...) \ - { (void)(condition); } -#else -#define FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION(condition, expression, \ - ...) \ - FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION(condition, expression, __VA_ARGS__) -#endif // defined(NDEBUG) - -// Assert expression is true otherwise display the specified message and -// abort. -#define FIREBASE_ASSERT_MESSAGE(expression, ...) \ - FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION(expression, expression, __VA_ARGS__) - -// Assert expression is true otherwise display the specified message and -// abort. Compiled out of release builds. -#define FIREBASE_DEV_ASSERT_MESSAGE(expression, ...) \ - FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION(expression, expression, \ - __VA_ARGS__) - -// Indicates an area of the code that cannot be reached (except possibly due to -// undefined behaviour or other similar badness). The only reasonable thing to -// do in these cases is to immediately abort. -#define FIREBASE_UNREACHABLE() abort() - -namespace firebase { -namespace firestore { -namespace util { - -// A no-return helper function. To raise an assertion, use Macro instead. -ABSL_ATTRIBUTE_NORETURN void FailAssert( - const char* file, const char* func, int line, const char* format, ...); - -} // namespace util -} // namespace firestore -} // namespace firebase - -#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_FIREBASE_ASSERT_H_ diff --git a/Firestore/core/src/firebase/firestore/util/hard_assert.h b/Firestore/core/src/firebase/firestore/util/hard_assert.h new file mode 100644 index 0000000..e60d71a --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/hard_assert.h @@ -0,0 +1,88 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_HARD_ASSERT_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_HARD_ASSERT_H_ + +#include <string> + +#include "Firestore/core/src/firebase/firestore/util/string_format.h" + +/** + * Fails the current function if the given condition is false. + * + * Unlike assert(3) or NSAssert, this macro is never compiled out. + * + * @param condition The condition to test. + * @param format (optional) A format string suitable for util::StringFormat. + * @param ... format arguments to pass to util::StringFormat. + */ +#define HARD_ASSERT(condition, ...) \ + do { \ + if (!(condition)) { \ + std::string _message = \ + firebase::firestore::util::StringFormat(__VA_ARGS__); \ + firebase::firestore::util::internal::Fail( \ + __FILE__, __PRETTY_FUNCTION__, __LINE__, _message, #condition); \ + } \ + } while (0) + +/** + * Unconditionally fails the current function. + * + * Unlike assert(3) or NSAssert, this macro is never compiled out. + * + * @param format A format string suitable for util::StringFormat. + * @param ... format arguments to pass to util::StringFormat. + */ +#define HARD_FAIL(...) \ + do { \ + std::string _failure = \ + firebase::firestore::util::StringFormat(__VA_ARGS__); \ + firebase::firestore::util::internal::Fail(__FILE__, __PRETTY_FUNCTION__, \ + __LINE__, _failure); \ + } while (0) + +/** + * Indicates an area of the code that cannot be reached (except possibly due to + * undefined behaviour or other similar badness). The only reasonable thing to + * do in these cases is to immediately abort. + */ +#define UNREACHABLE() abort() + +namespace firebase { +namespace firestore { +namespace util { +namespace internal { + +// A no-return helper function. To raise an assertion, use Macro instead. +ABSL_ATTRIBUTE_NORETURN void Fail(const char* file, + const char* func, + int line, + const std::string& message); + +ABSL_ATTRIBUTE_NORETURN void Fail(const char* file, + const char* func, + int line, + const std::string& message, + const char* condition); + +} // namespace internal +} // namespace util +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_HARD_ASSERT_H_ diff --git a/Firestore/core/src/firebase/firestore/util/assert_apple.mm b/Firestore/core/src/firebase/firestore/util/hard_assert_apple.mm index 9b6a651..3324fe8 100644 --- a/Firestore/core/src/firebase/firestore/util/assert_apple.mm +++ b/Firestore/core/src/firebase/firestore/util/hard_assert_apple.mm @@ -14,36 +14,47 @@ * limitations under the License. */ +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" + #import <Foundation/Foundation.h> -// TODO(wilhuff): match basenames so this can move up top -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include <string> + #include "Firestore/core/src/firebase/firestore/util/string_apple.h" namespace firebase { namespace firestore { namespace util { +namespace internal { -void FailAssert(const char* file, - const char* func, - const int line, - const char* format, - ...) { - va_list args; - va_start(args, format); - NSString* description = - [[NSString alloc] initWithFormat:WrapNSStringNoCopy(format) - arguments:args]; - va_end(args); +void Fail(const char* file, + const char* func, + const int line, + const std::string& message) { [[NSAssertionHandler currentHandler] handleFailureInFunction:WrapNSStringNoCopy(func) file:WrapNSStringNoCopy(file) lineNumber:line - description:@"FIRESTORE INTERNAL ASSERTION FAILED: %@", - description]; + description:@"FIRESTORE INTERNAL ASSERTION FAILED: %s", + message.c_str()]; abort(); } +void Fail(const char* file, + const char* func, + const int line, + const std::string& message, + const char* condition) { + std::string failure; + if (message.empty()) { + failure = condition; + } else { + failure = StringFormat("%s (expected %s)", message, condition); + } + Fail(file, func, line, failure); +} + +} // namespace internal } // namespace util } // namespace firestore } // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/util/assert_stdio.cc b/Firestore/core/src/firebase/firestore/util/hard_assert_stdio.cc index e01e564..6c50eb7 100644 --- a/Firestore/core/src/firebase/firestore/util/assert_stdio.cc +++ b/Firestore/core/src/firebase/firestore/util/hard_assert_stdio.cc @@ -14,40 +14,51 @@ * limitations under the License. */ +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" + #include <cstdarg> #include <stdexcept> #include <string> -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" -#include "Firestore/core/src/firebase/firestore/util/string_printf.h" +#include "Firestore/core/src/firebase/firestore/util/string_format.h" #include "absl/base/config.h" namespace firebase { namespace firestore { namespace util { +namespace internal { -void FailAssert(const char* file, - const char* func, - const int line, - const char* format, - ...) { - std::string message; - StringAppendF(&message, "ASSERT: %s(%d) %s: ", file, line, func); - - va_list args; - va_start(args, format); - StringAppendV(&message, format, args); - va_end(args); +void Fail(const char* file, + const char* func, + const int line, + const std::string& message) { + std::string failure = + StringFormat("ASSERT: %s(%s) %s: %s", file, line, func, message); #if ABSL_HAVE_EXCEPTIONS - throw std::logic_error(message); + throw std::logic_error(failure); #else - fprintf(stderr, "%s\n", message.c_str()); + fprintf(stderr, "%s\n", failure.c_str()); std::terminate(); #endif } +void Fail(const char* file, + const char* func, + const int line, + const std::string& message, + const char* condition) { + std::string failure; + if (message.empty()) { + failure = condition; + } else { + failure = StringFormat("%s (expected %s)", message, condition); + } + Fail(file, func, line, failure); +} + +} // namespace internal } // namespace util } // namespace firestore } // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/util/ordered_code.cc b/Firestore/core/src/firebase/firestore/util/ordered_code.cc index cb53b09..03da08d 100644 --- a/Firestore/core/src/firebase/firestore/util/ordered_code.cc +++ b/Firestore/core/src/firebase/firestore/util/ordered_code.cc @@ -17,7 +17,7 @@ #include "Firestore/core/src/firebase/firestore/util/ordered_code.h" #include "Firestore/core/src/firebase/firestore/util/bits.h" -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "absl/base/internal/endian.h" #include "absl/base/internal/unaligned_access.h" #include "absl/base/port.h" @@ -99,20 +99,20 @@ inline static bool IsSpecialByte(char c) { * assertion checking). */ inline static int AdvanceIfNoSpecialBytes(uint32_t v_32, const char* p) { - FIREBASE_DEV_ASSERT(UNALIGNED_LOAD32(p) == v_32); + HARD_ASSERT(UNALIGNED_LOAD32(p) == v_32); // See comments in SkipToNextSpecialByte if you wish to // understand this expression (which checks for the occurrence // of the special byte values 0 or 255 in any of the bytes of v_32). if ((v_32 - 0x01010101u) & ~(v_32 + 0x01010101u) & 0x80808080u) { // Special byte is in p[0..3] - FIREBASE_DEV_ASSERT(IsSpecialByte(p[0]) || IsSpecialByte(p[1]) || - IsSpecialByte(p[2]) || IsSpecialByte(p[3])); + HARD_ASSERT(IsSpecialByte(p[0]) || IsSpecialByte(p[1]) || + IsSpecialByte(p[2]) || IsSpecialByte(p[3])); return 0; } else { - FIREBASE_DEV_ASSERT(!IsSpecialByte(p[0])); - FIREBASE_DEV_ASSERT(!IsSpecialByte(p[1])); - FIREBASE_DEV_ASSERT(!IsSpecialByte(p[2])); - FIREBASE_DEV_ASSERT(!IsSpecialByte(p[3])); + HARD_ASSERT(!IsSpecialByte(p[0])); + HARD_ASSERT(!IsSpecialByte(p[1])); + HARD_ASSERT(!IsSpecialByte(p[2])); + HARD_ASSERT(!IsSpecialByte(p[3])); return 4; } } @@ -125,8 +125,8 @@ inline static int AdvanceIfNoSpecialBytes(uint32_t v_32, const char* p) { inline static const char* SkipToNextSpecialByte(const char* start, const char* limit) { // If these constants were ever changed, this routine needs to change - FIREBASE_DEV_ASSERT(kEscape1 == 0); - FIREBASE_DEV_ASSERT((kEscape2 & 0xff) == 255); + HARD_ASSERT(kEscape1 == 0); + HARD_ASSERT((kEscape2 & 0xff) == 255); const char* p = start; while (p + 8 <= limit) { // Find out if any of the next 8 bytes are either 0 or 255 (our @@ -164,8 +164,7 @@ inline static const char* SkipToNextSpecialByte(const char* start, if (IsSpecialByte(p[0])) return p; if (IsSpecialByte(p[1])) return p + 1; if (IsSpecialByte(p[2])) return p + 2; - FIREBASE_DEV_ASSERT( - IsSpecialByte(p[3])); // Last byte must be the special one + HARD_ASSERT(IsSpecialByte(p[3])); // Last byte must be the special one return p + 3; } } @@ -198,14 +197,14 @@ inline static void EncodeStringFragment(std::string* dest, p = SkipToNextSpecialByte(p, limit); if (p >= limit) break; // No more special characters that need escaping char c = *(p++); - FIREBASE_DEV_ASSERT(IsSpecialByte(c)); + HARD_ASSERT(IsSpecialByte(c)); if (c == kEscape1) { AppendBytes(dest, copy_start, static_cast<size_t>(p - copy_start) - 1); dest->push_back(kEscape1); dest->push_back(kNullCharacter); copy_start = p; } else { - FIREBASE_DEV_ASSERT(c == kEscape2); + HARD_ASSERT(c == kEscape2); AppendBytes(dest, copy_start, static_cast<size_t>(p - copy_start) - 1); dest->push_back(kEscape2); dest->push_back(kFFCharacter); @@ -302,7 +301,7 @@ inline static bool ReadStringInternal(absl::string_view* src, // If inversion is required, instead of inverting 'c', we invert the // character constants to which 'c' is compared. We get the same // behavior but save the runtime cost of inverting 'c'. - FIREBASE_DEV_ASSERT(IsSpecialByte(c)); + HARD_ASSERT(IsSpecialByte(c)); if (c == kEscape1) { if (result) { AppendBytes(result, copy_start, @@ -323,7 +322,7 @@ inline static bool ReadStringInternal(absl::string_view* src, } copy_start = start; } else { - FIREBASE_DEV_ASSERT(c == kEscape2); + HARD_ASSERT(c == kEscape2); if (result) { AppendBytes(result, copy_start, static_cast<size_t>(start - copy_start) - 1); @@ -359,7 +358,7 @@ bool OrderedCode::ReadNumIncreasing(absl::string_view* src, uint64_t* result) { // If len > 0 and src is longer than 1, the first byte of "payload" // must be non-zero (otherwise the encoding is not minimal). // In opt mode, we don't enforce that encodings must be minimal. - FIREBASE_DEV_ASSERT(0 == len || src->size() == 1 || (*src)[1] != '\0'); + HARD_ASSERT(0 == len || src->size() == 1 || (*src)[1] != '\0'); if (len + 1 > src->size() || len > 8) { return false; // Not enough bytes or too many bytes @@ -557,11 +556,9 @@ void OrderedCode::WriteSignedNumIncreasing(std::string* dest, int64_t val) { }; UNALIGNED_STORE64(buf + 2, absl::ghtonll(static_cast<uint64_t>(val))); - FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION(sizeof(buf) == kMaxSigned64Length, - sizeof(buf) == kMaxSigned64Length, - "max length size mismatch"); + HARD_ASSERT(sizeof(buf) == kMaxSigned64Length, "max length size mismatch"); const size_t len = static_cast<size_t>(SignedEncodingLengthPositive(x)); - FIREBASE_DEV_ASSERT(len >= 2); + HARD_ASSERT(len >= 2); char* const begin = buf + sizeof(buf) - len; begin[0] ^= kLengthToHeaderBits[len][0]; begin[1] ^= kLengthToHeaderBits[len][1]; // ok because len >= 2 @@ -608,8 +605,8 @@ bool OrderedCode::ReadSignedNumIncreasing(absl::string_view* src, x ^= kLengthToMask[len]; // remove spurious header bits - FIREBASE_DEV_ASSERT(len == static_cast<size_t>(SignedEncodingLength( - static_cast<int64_t>(x)))); + HARD_ASSERT(len == static_cast<size_t>( + SignedEncodingLength(static_cast<int64_t>(x)))); if (result) *result = static_cast<int64_t>(x); src->remove_prefix(static_cast<size_t>(len)); diff --git a/Firestore/core/src/firebase/firestore/util/status.cc b/Firestore/core/src/firebase/firestore/util/status.cc index e918846..46f3ce6 100644 --- a/Firestore/core/src/firebase/firestore/util/status.cc +++ b/Firestore/core/src/firebase/firestore/util/status.cc @@ -23,7 +23,7 @@ namespace firestore { namespace util { Status::Status(FirestoreErrorCode code, absl::string_view msg) { - FIREBASE_ASSERT(code != FirestoreErrorCode::Ok); + HARD_ASSERT(code != FirestoreErrorCode::Ok); state_ = std::unique_ptr<State>(new State); state_->code = code; state_->msg = static_cast<std::string>(msg); @@ -117,7 +117,7 @@ void Status::IgnoreError() const { } std::string StatusCheckOpHelperOutOfLine(const Status& v, const char* msg) { - FIREBASE_ASSERT(!v.ok()); + HARD_ASSERT(!v.ok()); std::string r("Non-OK-status: "); r += msg; r += " status: "; diff --git a/Firestore/core/src/firebase/firestore/util/status.h b/Firestore/core/src/firebase/firestore/util/status.h index 13bf320..9121b36 100644 --- a/Firestore/core/src/firebase/firestore/util/status.h +++ b/Firestore/core/src/firebase/firestore/util/status.h @@ -23,7 +23,7 @@ #include <string> #include "Firestore/core/include/firebase/firestore/firestore_errors.h" -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "absl/base/attributes.h" #include "absl/strings/string_view.h" @@ -124,15 +124,8 @@ typedef std::function<void(const Status&)> StatusCallback; extern std::string StatusCheckOpHelperOutOfLine(const Status& v, const char* msg); -#define STATUS_CHECK_OK(val) \ - FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION( \ - val.ok(), val.ok(), StatusCheckOpHelperOutOfLine(val, #val).c_str()) - -// DEBUG only version of STATUS_CHECK_OK. Compiler still parses 'val' even in -// opt mode. -#define STATUS_DCHECK_OK(val) \ - FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION( \ - val.ok(), val.ok(), StatusCheckOpHelperOutOfLine(val, #val).c_str()) +#define STATUS_CHECK_OK(val) \ + HARD_ASSERT(val.ok(), "%s", StatusCheckOpHelperOutOfLine(val, #val)) } // namespace util } // namespace firestore diff --git a/Firestore/core/src/firebase/firestore/util/statusor.cc b/Firestore/core/src/firebase/firestore/util/statusor.cc index be1e03a..bbd5781 100644 --- a/Firestore/core/src/firebase/firestore/util/statusor.cc +++ b/Firestore/core/src/firebase/firestore/util/statusor.cc @@ -15,6 +15,7 @@ */ #include "Firestore/core/src/firebase/firestore/util/statusor.h" +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" namespace firebase { namespace firestore { @@ -24,15 +25,14 @@ namespace internal_statusor { void Helper::HandleInvalidStatusCtorArg(Status* status) { const char* kMessage = "An OK status is not a valid constructor argument to StatusOr<T>"; - FIREBASE_DEV_ASSERT_MESSAGE(false, kMessage); + HARD_FAIL("%s", kMessage); // Fall back to Internal for non-debug builds *status = Status(FirestoreErrorCode::Internal, kMessage); } void Helper::Crash(const Status& status) { - FIREBASE_ASSERT_MESSAGE( - false, "Attempting to fetch value instead of handling error ", - status.ToString().c_str()); + HARD_FAIL("Attempting to fetch value instead of handling error %s", + status.ToString()); } } // namespace internal_statusor diff --git a/Firestore/core/src/firebase/firestore/util/string_printf.cc b/Firestore/core/src/firebase/firestore/util/string_printf.cc deleted file mode 100644 index c5483f4..0000000 --- a/Firestore/core/src/firebase/firestore/util/string_printf.cc +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2018 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "Firestore/core/src/firebase/firestore/util/string_printf.h" - -#include <cstdio> - -namespace firebase { -namespace firestore { -namespace util { - -void StringAppendV(std::string* dst, const char* format, va_list ap) { - // First try with a small fixed size buffer - static const int kSpaceLength = 1024; - char space[kSpaceLength]; - - // It's possible for methods that use a va_list to invalidate - // the data in it upon use. The fix is to make a copy - // of the structure before using it and use that copy instead. - va_list backup_ap; - va_copy(backup_ap, ap); - int result = vsnprintf(space, kSpaceLength, format, backup_ap); - va_end(backup_ap); - - if (result < kSpaceLength) { - if (result >= 0) { - // Normal case -- everything fit. - dst->append(space, static_cast<size_t>(result)); - return; - } - -#ifdef _MSC_VER - // Error or MSVC running out of space. MSVC 8.0 and higher - // can be asked about space needed with the special idiom below: - va_copy(backup_ap, ap); - result = vsnprintf(nullptr, 0, format, backup_ap); - va_end(backup_ap); -#endif - } - - if (result < 0) { - // Just an error. - return; - } - size_t result_size = static_cast<size_t>(result); - - // Increase the buffer size to the size requested by vsnprintf, - // plus one for the closing \0. - size_t initial_size = dst->size(); - size_t target_size = initial_size + result_size; - - dst->resize(target_size + 1); - char* buf = &(*dst)[initial_size]; - size_t buf_remain = result_size + 1; - - // Restore the va_list before we use it again - va_copy(backup_ap, ap); - result = vsnprintf(buf, buf_remain, format, backup_ap); - va_end(backup_ap); - - if (result >= 0 && static_cast<size_t>(result) < buf_remain) { - // It fit and vsnprintf copied in directly. Resize down one to - // remove the trailing \0. - dst->resize(target_size); - } else { - // Didn't fit. Leave the original string unchanged. - dst->resize(initial_size); - } -} - -std::string StringPrintf(const char* format, ...) { - va_list ap; - va_start(ap, format); - std::string result; - StringAppendV(&result, format, ap); - va_end(ap); - return result; -} - -void StringAppendF(std::string* dst, const char* format, ...) { - va_list ap; - va_start(ap, format); - StringAppendV(dst, format, ap); - va_end(ap); -} - -} // namespace util -} // namespace firestore -} // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/util/string_printf.h b/Firestore/core/src/firebase/firestore/util/string_printf.h deleted file mode 100644 index 553af66..0000000 --- a/Firestore/core/src/firebase/firestore/util/string_printf.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2018 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_ -#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_ - -#include <cstdarg> -#include <string> - -#include "absl/base/attributes.h" - -namespace firebase { -namespace firestore { -namespace util { - -/** Return a C++ string. */ -std::string StringPrintf(const char* format, ...) ABSL_PRINTF_ATTRIBUTE(1, 2); - -/** Append result to a supplied string. */ -void StringAppendF(std::string* dst, const char* format, ...) - ABSL_PRINTF_ATTRIBUTE(2, 3); - -/** - * Lower-level routine that takes a va_list and appends to a specified - * string. All other routines are just convenience wrappers around it. - */ -void StringAppendV(std::string* dst, const char* format, va_list ap); - -} // namespace util -} // namespace firestore -} // namespace firebase - -#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_ |