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

Side by Side Diff: base/logging.h

Issue 1193873004: Make CHECK_EQ and friends work properly in an if/else structure without braces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Proposed re-working of unit-tests. Created 5 years, 5 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 | base/logging_unittest.cc » ('j') | 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 <cassert> 8 #include <cassert>
9 #include <string> 9 #include <string>
10 #include <cstring> 10 #include <cstring>
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 #define CHECK(condition) \ 473 #define CHECK(condition) \
474 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ 474 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
475 << "Check failed: " #condition ". " 475 << "Check failed: " #condition ". "
476 476
477 #define PCHECK(condition) \ 477 #define PCHECK(condition) \
478 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 478 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
479 << "Check failed: " #condition ". " 479 << "Check failed: " #condition ". "
480 480
481 #endif // _PREFAST_ 481 #endif // _PREFAST_
482 482
483 // Captures the result of a CHECK_EQ (for example) and facilitates testing as a
484 // boolean.
485 class CheckOpResult {
486 public:
487 // |message| must be null if and only if the check failed.
488 CheckOpResult(std::string* message) : message_(message) {}
489 // Returns true if the check succeeded.
490 operator bool() const { return !message_; }
491 // Returns the message.
492 std::string* message() { return message_; }
493
494 private:
495 std::string* message_;
496 };
497
483 // Helper macro for binary operators. 498 // Helper macro for binary operators.
484 // Don't use this macro directly in your code, use CHECK_EQ et al below. 499 // Don't use this macro directly in your code, use CHECK_EQ et al below.
485 // 500 // The 'switch' is used to prevent the 'if' from being ambiguous when the macro
486 // TODO(akalin): Rewrite this so that constructs like if (...) 501 // is used in an 'if' clause such as:
487 // CHECK_EQ(...) else { ... } work properly. 502 // if (a == 1)
488 #define CHECK_OP(name, op, val1, val2) \ 503 // CHECK_EQ(2, a);
489 if (std::string* _result = \ 504 #define CHECK_OP(name, op, val1, val2) \
490 logging::Check##name##Impl((val1), (val2), \ 505 switch (0) case 0: default: \
491 #val1 " " #op " " #val2)) \ 506 if (logging::CheckOpResult _result = \
492 logging::LogMessage(__FILE__, __LINE__, _result).stream() 507 logging::Check##name##Impl((val1), (val2), \
508 #val1 " " #op " " #val2)) \
509 ; \
510 else \
511 logging::LogMessage(__FILE__, __LINE__, _result.message()).stream()
Nico 2015/07/14 20:22:41 So if you say DCHECK() << "foo"; won't the <<
erikwright (departed) 2015/07/14 20:33:08 It binds to the else, as desired. Note that CheckO
Nico 2015/07/14 20:47:09 Ah, sorry. This might be too tricky for people wit
493 512
494 #endif 513 #endif
495 514
496 // Build the error message string. This is separate from the "Impl" 515 // Build the error message string. This is separate from the "Impl"
497 // function template because it is not performance critical and so can 516 // function template because it is not performance critical and so can
498 // be out of line, while the "Impl" code should be inline. Caller 517 // be out of line, while the "Impl" code should be inline. Caller
499 // takes ownership of the returned string. 518 // takes ownership of the returned string.
500 template<class t1, class t2> 519 template<class t1, class t2>
501 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { 520 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
502 std::ostringstream ss; 521 std::ostringstream ss;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 << "Check failed: " #condition ". " 677 << "Check failed: " #condition ". "
659 678
660 #define DPCHECK(condition) \ 679 #define DPCHECK(condition) \
661 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ 680 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \
662 << "Check failed: " #condition ". " 681 << "Check failed: " #condition ". "
663 682
664 #endif // _PREFAST_ 683 #endif // _PREFAST_
665 684
666 // Helper macro for binary operators. 685 // Helper macro for binary operators.
667 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 686 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
668 #define DCHECK_OP(name, op, val1, val2) \ 687 // The 'switch' is used to prevent the 'if' from being ambiguous when the macro
669 if (DCHECK_IS_ON()) \ 688 // is used in an 'if' clause such as:
670 if (std::string* _result = logging::Check##name##Impl( \ 689 // if (a == 1)
671 (val1), (val2), #val1 " " #op " " #val2)) \ 690 // DCHECK_EQ(2, a);
672 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, _result) \ 691 #define DCHECK_OP(name, op, val1, val2) \
673 .stream() 692 switch (0) case 0: default: \
693 if (logging::CheckOpResult _result = \
694 DCHECK_IS_ON() ? \
695 logging::Check##name##Impl((val1), (val2), \
696 #val1 " " #op " " #val2) : nullptr) \
697 ; \
698 else \
699 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \
700 _result.message()).stream()
674 701
675 // Equality/Inequality checks - compare two values, and log a 702 // Equality/Inequality checks - compare two values, and log a
676 // LOG_DCHECK message including the two values when the result is not 703 // LOG_DCHECK message including the two values when the result is not
677 // as expected. The values must have operator<<(ostream, ...) 704 // as expected. The values must have operator<<(ostream, ...)
678 // defined. 705 // defined.
679 // 706 //
680 // You may append to the error message like so: 707 // You may append to the error message like so:
681 // DCHECK_NE(1, 2) << ": The world must be ending!"; 708 // DCHECK_NE(1, 2) << ": The world must be ending!";
682 // 709 //
683 // We are very careful to ensure that each argument is evaluated exactly 710 // We are very careful to ensure that each argument is evaluated exactly
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 #elif NOTIMPLEMENTED_POLICY == 5 957 #elif NOTIMPLEMENTED_POLICY == 5
931 #define NOTIMPLEMENTED() do {\ 958 #define NOTIMPLEMENTED() do {\
932 static bool logged_once = false;\ 959 static bool logged_once = false;\
933 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 960 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
934 logged_once = true;\ 961 logged_once = true;\
935 } while(0);\ 962 } while(0);\
936 EAT_STREAM_PARAMETERS 963 EAT_STREAM_PARAMETERS
937 #endif 964 #endif
938 965
939 #endif // BASE_LOGGING_H_ 966 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698