Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: base/logging.h

Issue 2653073002: Make sure DCHECK compiles out completely if DCHECKS aren't enabled. (Closed)
Patch Set: commented Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_LOGGING_H_ 5 #ifndef BASE_LOGGING_H_
6 #define BASE_LOGGING_H_ 6 #define BASE_LOGGING_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <cassert> 10 #include <cassert>
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) 705 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
706 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO 706 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
707 const LogSeverity LOG_DCHECK = LOG_INFO; 707 const LogSeverity LOG_DCHECK = LOG_INFO;
708 708
709 #endif // DCHECK_IS_ON() 709 #endif // DCHECK_IS_ON()
710 710
711 // DCHECK et al. make sure to reference |condition| regardless of 711 // DCHECK et al. make sure to reference |condition| regardless of
712 // whether DCHECKs are enabled; this is so that we don't get unused 712 // whether DCHECKs are enabled; this is so that we don't get unused
713 // variable warnings if the only use of a variable is in a DCHECK. 713 // variable warnings if the only use of a variable is in a DCHECK.
714 // This behavior is different from DLOG_IF et al. 714 // This behavior is different from DLOG_IF et al.
715 //
716 // Note that the definition of the DCHECK macros depends on whether or not
717 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use
718 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries.
715 719
716 #if defined(_PREFAST_) && defined(OS_WIN) 720 #if defined(_PREFAST_) && defined(OS_WIN)
717 // See comments on the previous use of __analysis_assume. 721 // See comments on the previous use of __analysis_assume.
718 722
719 #define DCHECK(condition) \ 723 #define DCHECK(condition) \
720 __analysis_assume(!!(condition)), \ 724 __analysis_assume(!!(condition)), \
721 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ 725 LAZY_STREAM(LOG_STREAM(DCHECK), false) \
722 << "Check failed: " #condition ". " 726 << "Check failed: " #condition ". "
723 727
724 #define DPCHECK(condition) \ 728 #define DPCHECK(condition) \
725 __analysis_assume(!!(condition)), \ 729 __analysis_assume(!!(condition)), \
726 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ 730 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \
727 << "Check failed: " #condition ". " 731 << "Check failed: " #condition ". "
728 732
729 #else // _PREFAST_ 733 #else // _PREFAST_
730 734
731 #define DCHECK(condition) \ 735 #if DCHECK_IS_ON()
danakj 2017/01/25 16:03:42 can you elif instead of else\nif?
dcheng 2017/01/25 17:42:53 We discussed offline and I'll leave it as nested #
732 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ 736
737 #define DCHECK(condition) \
738 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \
739 << "Check failed: " #condition ". "
740 #define DPCHECK(condition) \
741 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \
733 << "Check failed: " #condition ". " 742 << "Check failed: " #condition ". "
734 743
735 #define DPCHECK(condition) \ 744 #else // DCHECK_IS_ON()
736 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ 745
737 << "Check failed: " #condition ". " 746 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition)
747 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition)
748
749 #endif // DCHECK_IS_ON()
738 750
739 #endif // _PREFAST_ 751 #endif // _PREFAST_
740 752
741 // Helper macro for binary operators. 753 // Helper macro for binary operators.
742 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 754 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
743 // The 'switch' is used to prevent the 'else' from being ambiguous when the 755 // The 'switch' is used to prevent the 'else' from being ambiguous when the
744 // macro is used in an 'if' clause such as: 756 // macro is used in an 'if' clause such as:
745 // if (a == 1) 757 // if (a == 1)
746 // DCHECK_EQ(2, a); 758 // DCHECK_EQ(2, a);
759 #if DCHECK_IS_ON()
760
747 #define DCHECK_OP(name, op, val1, val2) \ 761 #define DCHECK_OP(name, op, val1, val2) \
748 switch (0) case 0: default: \ 762 switch (0) case 0: default: \
749 if (::logging::CheckOpResult true_if_passed = \ 763 if (::logging::CheckOpResult true_if_passed = \
750 DCHECK_IS_ON() ? \ 764 DCHECK_IS_ON() ? \
751 ::logging::Check##name##Impl((val1), (val2), \ 765 ::logging::Check##name##Impl((val1), (val2), \
752 #val1 " " #op " " #val2) : nullptr) \ 766 #val1 " " #op " " #val2) : nullptr) \
753 ; \ 767 ; \
754 else \ 768 else \
755 ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ 769 ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \
756 true_if_passed.message()).stream() 770 true_if_passed.message()).stream()
757 771
772 #else // DCHECK_IS_ON()
773
774 // When DCHECKs aren't enabled, DCHECK_OP still needs to reference operator<<
775 // overloads for |val1| and |val2| to avoid potential compiler warnings about
776 // unused functions. For the same reason, it also compares |val1| and |val2|
777 // using |op|.
778 //
779 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated
780 // once. Even though |val1| and |val2| appear twice in this version of the macro
781 // expansion, this is OK, since the expression is never actually evaluated.
782 #define DCHECK_OP(name, op, val1, val2) \
783 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \
784 ::logging::g_swallow_stream, val1), \
785 ::logging::MakeCheckOpValueString( \
786 ::logging::g_swallow_stream, val2), \
787 (val1)op(val2))
788
789 #endif // DCHECK_IS_ON()
790
758 // Equality/Inequality checks - compare two values, and log a 791 // Equality/Inequality checks - compare two values, and log a
759 // LOG_DCHECK message including the two values when the result is not 792 // LOG_DCHECK message including the two values when the result is not
760 // as expected. The values must have operator<<(ostream, ...) 793 // as expected. The values must have operator<<(ostream, ...)
761 // defined. 794 // defined.
762 // 795 //
763 // You may append to the error message like so: 796 // You may append to the error message like so:
764 // DCHECK_NE(1, 2) << "The world must be ending!"; 797 // DCHECK_NE(1, 2) << "The world must be ending!";
765 // 798 //
766 // We are very careful to ensure that each argument is evaluated exactly 799 // We are very careful to ensure that each argument is evaluated exactly
767 // once, and that anything which is legal to pass as a function argument is 800 // once, and that anything which is legal to pass as a function argument is
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 #elif NOTIMPLEMENTED_POLICY == 5 1051 #elif NOTIMPLEMENTED_POLICY == 5
1019 #define NOTIMPLEMENTED() do {\ 1052 #define NOTIMPLEMENTED() do {\
1020 static bool logged_once = false;\ 1053 static bool logged_once = false;\
1021 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1054 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
1022 logged_once = true;\ 1055 logged_once = true;\
1023 } while(0);\ 1056 } while(0);\
1024 EAT_STREAM_PARAMETERS 1057 EAT_STREAM_PARAMETERS
1025 #endif 1058 #endif
1026 1059
1027 #endif // BASE_LOGGING_H_ 1060 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698