// Copyright (c) 2016 Google Inc. // // 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 SOURCE_OPT_LOG_H_ #define SOURCE_OPT_LOG_H_ #include <cstdio> #include <cstdlib> #include <utility> #include <vector> #include "spirv-tools/libspirv.hpp" // Asserts the given condition is true. Otherwise, sends a message to the // consumer and exits the program with failure code. Accepts the following // formats: // // SPIRV_ASSERT(<message-consumer>, <condition-expression>); // SPIRV_ASSERT(<message-consumer>, <condition-expression>, <message>); // SPIRV_ASSERT(<message-consumer>, <condition-expression>, // <message-format>, <variable-arguments>); // // In the third format, the number of <variable-arguments> cannot exceed (5 - // 2). If more arguments are wanted, grow PP_ARG_N and PP_NARGS in the below. #if !defined(NDEBUG) #define SPIRV_ASSERT(consumer, ...) … #else // Adding a use to avoid errors in the release build related to unused // consumers. #define SPIRV_ASSERT … #endif // Logs a debug message to the consumer. Accepts the following formats: // // SPIRV_DEBUG(<message-consumer>, <message>); // SPIRV_DEBUG(<message-consumer>, <message-format>, <variable-arguments>); // // In the second format, the number of <variable-arguments> cannot exceed (5 - // 1). If more arguments are wanted, grow PP_ARG_N and PP_NARGS in the below. #if !defined(NDEBUG) && defined(SPIRV_LOG_DEBUG) #define SPIRV_DEBUG … #else // Adding a use to avoid errors in the release build related to unused // consumers. #define SPIRV_DEBUG(consumer, ...) … #endif // Helper macros for concatenating arguments. #define SPIRV_CONCATENATE(a, b) … #define SPIRV_CONCATENATE_(a, b) … // Helper macro to force expanding __VA_ARGS__ to satisfy MSVC compiler. #define PP_EXPAND(x) … namespace spvtools { // Calls the given |consumer| by supplying the |message|. The |message| is from // the given |source| and |location| and of the given severity |level|. inline void Log(const MessageConsumer& consumer, spv_message_level_t level, const char* source, const spv_position_t& position, const char* message) { … } // Calls the given |consumer| by supplying the message composed according to the // given |format|. The |message| is from the given |source| and |location| and // of the given severity |level|. template <typename... Args> void Logf(const MessageConsumer& consumer, spv_message_level_t level, const char* source, const spv_position_t& position, const char* format, Args&&... args) { … } // Calls the given |consumer| by supplying the given error |message|. The // |message| is from the given |source| and |location|. inline void Error(const MessageConsumer& consumer, const char* source, const spv_position_t& position, const char* message) { … } // Calls the given |consumer| by supplying the error message composed according // to the given |format|. The |message| is from the given |source| and // |location|. template <typename... Args> inline void Errorf(const MessageConsumer& consumer, const char* source, const spv_position_t& position, const char* format, Args&&... args) { … } } // namespace spvtools #define SPIRV_ASSERT_IMPL(consumer, ...) … #define SPIRV_DEBUG_IMPL(consumer, ...) … #define SPIRV_ASSERT_1(consumer, condition) … #define SPIRV_ASSERT_2(consumer, condition, message) … #define SPIRV_ASSERT_more(consumer, condition, format, ...) … #define SPIRV_ASSERT_3(consumer, condition, format, ...) … #define SPIRV_ASSERT_4(consumer, condition, format, ...) … #define SPIRV_ASSERT_5(consumer, condition, format, ...) … #define SPIRV_DEBUG_1(consumer, message) … #define SPIRV_DEBUG_more(consumer, format, ...) … #define SPIRV_DEBUG_2(consumer, format, ...) … #define SPIRV_DEBUG_3(consumer, format, ...) … #define SPIRV_DEBUG_4(consumer, format, ...) … #define SPIRV_DEBUG_5(consumer, format, ...) … // Macros for counting the number of arguments passed in. #define PP_NARGS(...) … #define PP_ARG_N(_1, _2, _3, _4, _5, N, ...) … // Tests for making sure that PP_NARGS() behaves as expected. static_assert …; static_assert …; static_assert …; static_assert …; static_assert …; static_assert …; static_assert …; #endif // SOURCE_OPT_LOG_H_