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

Side by Side Diff: base/logging.h

Issue 2720893003: Roll back ANALYSIS_ASSUME_TRUE() until bitfield support is developed (Closed)
Patch Set: Created 3 years, 9 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 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 282
283 // Sets the Log Message Handler that gets passed every log message before 283 // Sets the Log Message Handler that gets passed every log message before
284 // it's sent to other log destinations (if any). 284 // it's sent to other log destinations (if any).
285 // Returns true to signal that it handled the message and the message 285 // Returns true to signal that it handled the message and the message
286 // should not be sent to other log destinations. 286 // should not be sent to other log destinations.
287 typedef bool (*LogMessageHandlerFunction)(int severity, 287 typedef bool (*LogMessageHandlerFunction)(int severity,
288 const char* file, int line, size_t message_start, const std::string& str); 288 const char* file, int line, size_t message_start, const std::string& str);
289 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); 289 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
290 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); 290 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
291 291
292 // ANALYZER_ASSUME_TRUE(...) generates compiler-specific annotations which
293 // prevent the static analyzer from analyzing the code using hypothetical
294 // values that are asserted to be impossible.
295 // The value of the condition passed to ANALYZER_ASSUME_TRUE() is returned
296 // directly.
297 #if defined(__clang_analyzer__)
298
299 inline void AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {}
300
301 // |arg| is a universal reference for compatibility with lvalue and rvalue
302 // arguments.
303 template <typename TVal>
304 inline constexpr TVal&& AnalysisAssumeTrue(TVal&& arg) {
305 if (!arg) {
306 AnalyzerNoReturn();
307 }
308 return std::forward<TVal>(arg);
309 }
310
311 #define ANALYZER_ASSUME_TRUE(val) ::logging::AnalysisAssumeTrue(val)
312
313 #elif defined(_PREFAST_) && defined(OS_WIN)
314
315 // |arg| is a universal reference for compatibility with lvalue and rvalue
316 // arguments.
317 template <typename TVal>
318 inline constexpr TVal&& AnalysisAssumeTrue(TVal&& arg) {
319 __analysis_assume(!!arg);
320 return std::forward<TVal>(arg);
321 }
322
323 #define ANALYZER_ASSUME_TRUE(val) ::logging::AnalysisAssumeTrue(val)
324
325 #else // !_PREFAST_ & !__clang_analyzer__
326
327 #define ANALYZER_ASSUME_TRUE(val) (val)
328
329 #endif // !_PREFAST_ & !__clang_analyzer__
330
331 typedef int LogSeverity; 292 typedef int LogSeverity;
332 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity 293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
333 // Note: the log severities are used to index into the array of names, 294 // Note: the log severities are used to index into the array of names,
334 // see log_severity_names. 295 // see log_severity_names.
335 const LogSeverity LOG_INFO = 0; 296 const LogSeverity LOG_INFO = 0;
336 const LogSeverity LOG_WARNING = 1; 297 const LogSeverity LOG_WARNING = 1;
337 const LogSeverity LOG_ERROR = 2; 298 const LogSeverity LOG_ERROR = 2;
338 const LogSeverity LOG_FATAL = 3; 299 const LogSeverity LOG_FATAL = 3;
339 const LogSeverity LOG_NUM_SEVERITIES = 4; 300 const LogSeverity LOG_NUM_SEVERITIES = 4;
340 301
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 401
441 #define VPLOG(verbose_level) \ 402 #define VPLOG(verbose_level) \
442 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) 403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
443 404
444 #define VPLOG_IF(verbose_level, condition) \ 405 #define VPLOG_IF(verbose_level, condition) \
445 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ 406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
446 VLOG_IS_ON(verbose_level) && (condition)) 407 VLOG_IS_ON(verbose_level) && (condition))
447 408
448 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. 409 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
449 410
450 #define LOG_ASSERT(condition) \ 411 #define LOG_ASSERT(condition) \
451 LOG_IF(FATAL, !ANALYZER_ASSUME_TRUE(condition)) \ 412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
452 << "Assert failed: " #condition ". "
453 413
454 #if defined(OS_WIN) 414 #if defined(OS_WIN)
455 #define PLOG_STREAM(severity) \ 415 #define PLOG_STREAM(severity) \
456 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ 416 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
457 ::logging::GetLastSystemErrorCode()).stream() 417 ::logging::GetLastSystemErrorCode()).stream()
458 #elif defined(OS_POSIX) 418 #elif defined(OS_POSIX)
459 #define PLOG_STREAM(severity) \ 419 #define PLOG_STREAM(severity) \
460 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ 420 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
461 ::logging::GetLastSystemErrorCode()).stream() 421 ::logging::GetLastSystemErrorCode()).stream()
462 #endif 422 #endif
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 // compiler optimizations. 555 // compiler optimizations.
596 #define CHECK(condition) \ 556 #define CHECK(condition) \
597 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS 557 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS
598 558
599 #define PCHECK(condition) CHECK(condition) 559 #define PCHECK(condition) CHECK(condition)
600 560
601 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) 561 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
602 562
603 #else // !(OFFICIAL_BUILD && NDEBUG) 563 #else // !(OFFICIAL_BUILD && NDEBUG)
604 564
565 #if defined(_PREFAST_) && defined(OS_WIN)
566 // Use __analysis_assume to tell the VC++ static analysis engine that
567 // assert conditions are true, to suppress warnings. The LAZY_STREAM
568 // parameter doesn't reference 'condition' in /analyze builds because
569 // this evaluation confuses /analyze. The !! before condition is because
570 // __analysis_assume gets confused on some conditions:
571 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/
572
573 #define CHECK(condition) \
574 __analysis_assume(!!(condition)), \
575 LAZY_STREAM(LOG_STREAM(FATAL), false) \
576 << "Check failed: " #condition ". "
577
578 #define PCHECK(condition) \
579 __analysis_assume(!!(condition)), \
580 LAZY_STREAM(PLOG_STREAM(FATAL), false) \
581 << "Check failed: " #condition ". "
582
583 #else // _PREFAST_
584
605 // Do as much work as possible out of line to reduce inline code size. 585 // Do as much work as possible out of line to reduce inline code size.
606 #define CHECK(condition) \ 586 #define CHECK(condition) \
607 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ 587 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \
608 !ANALYZER_ASSUME_TRUE(condition)) 588 !(condition))
609 589
610 #define PCHECK(condition) \ 590 #define PCHECK(condition) \
611 LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ 591 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
612 << "Check failed: " #condition ". " 592 << "Check failed: " #condition ". "
613 593
594 #endif // _PREFAST_
595
614 // Helper macro for binary operators. 596 // Helper macro for binary operators.
615 // Don't use this macro directly in your code, use CHECK_EQ et al below. 597 // Don't use this macro directly in your code, use CHECK_EQ et al below.
616 // The 'switch' is used to prevent the 'else' from being ambiguous when the 598 // The 'switch' is used to prevent the 'else' from being ambiguous when the
617 // macro is used in an 'if' clause such as: 599 // macro is used in an 'if' clause such as:
618 // if (a == 1) 600 // if (a == 1)
619 // CHECK_EQ(2, a); 601 // CHECK_EQ(2, a);
620 #define CHECK_OP(name, op, val1, val2) \ 602 #define CHECK_OP(name, op, val1, val2) \
621 switch (0) case 0: default: \ 603 switch (0) case 0: default: \
622 if (::logging::CheckOpResult true_if_passed = \ 604 if (::logging::CheckOpResult true_if_passed = \
623 ::logging::Check##name##Impl((val1), (val2), \ 605 ::logging::Check##name##Impl((val1), (val2), \
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 const std::string&, const std::string&, const char* name); 682 const std::string&, const std::string&, const char* name);
701 683
702 // Helper functions for CHECK_OP macro. 684 // Helper functions for CHECK_OP macro.
703 // The (int, int) specialization works around the issue that the compiler 685 // The (int, int) specialization works around the issue that the compiler
704 // will not instantiate the template version of the function on values of 686 // will not instantiate the template version of the function on values of
705 // unnamed enum type - see comment below. 687 // unnamed enum type - see comment below.
706 #define DEFINE_CHECK_OP_IMPL(name, op) \ 688 #define DEFINE_CHECK_OP_IMPL(name, op) \
707 template <class t1, class t2> \ 689 template <class t1, class t2> \
708 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ 690 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
709 const char* names) { \ 691 const char* names) { \
710 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ 692 if (v1 op v2) \
711 return NULL; \ 693 return NULL; \
712 else \ 694 else \
713 return ::logging::MakeCheckOpString(v1, v2, names); \ 695 return ::logging::MakeCheckOpString(v1, v2, names); \
714 } \ 696 } \
715 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ 697 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
716 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ 698 if (v1 op v2) \
717 return NULL; \ 699 return NULL; \
718 else \ 700 else \
719 return ::logging::MakeCheckOpString(v1, v2, names); \ 701 return ::logging::MakeCheckOpString(v1, v2, names); \
720 } 702 }
721 DEFINE_CHECK_OP_IMPL(EQ, ==) 703 DEFINE_CHECK_OP_IMPL(EQ, ==)
722 DEFINE_CHECK_OP_IMPL(NE, !=) 704 DEFINE_CHECK_OP_IMPL(NE, !=)
723 DEFINE_CHECK_OP_IMPL(LE, <=) 705 DEFINE_CHECK_OP_IMPL(LE, <=)
724 DEFINE_CHECK_OP_IMPL(LT, < ) 706 DEFINE_CHECK_OP_IMPL(LT, < )
725 DEFINE_CHECK_OP_IMPL(GE, >=) 707 DEFINE_CHECK_OP_IMPL(GE, >=)
726 DEFINE_CHECK_OP_IMPL(GT, > ) 708 DEFINE_CHECK_OP_IMPL(GT, > )
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 785
804 // DCHECK et al. make sure to reference |condition| regardless of 786 // DCHECK et al. make sure to reference |condition| regardless of
805 // whether DCHECKs are enabled; this is so that we don't get unused 787 // whether DCHECKs are enabled; this is so that we don't get unused
806 // variable warnings if the only use of a variable is in a DCHECK. 788 // variable warnings if the only use of a variable is in a DCHECK.
807 // This behavior is different from DLOG_IF et al. 789 // This behavior is different from DLOG_IF et al.
808 // 790 //
809 // Note that the definition of the DCHECK macros depends on whether or not 791 // Note that the definition of the DCHECK macros depends on whether or not
810 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use 792 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use
811 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. 793 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries.
812 794
795 #if defined(_PREFAST_) && defined(OS_WIN)
796 // See comments on the previous use of __analysis_assume.
797
798 #define DCHECK(condition) \
799 __analysis_assume(!!(condition)), \
800 LAZY_STREAM(LOG_STREAM(DCHECK), false) \
801 << "Check failed: " #condition ". "
802
803 #define DPCHECK(condition) \
804 __analysis_assume(!!(condition)), \
805 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \
806 << "Check failed: " #condition ". "
807
808 #elif defined(__clang_analyzer__)
809
810 // Keeps the static analyzer from proceeding along the current codepath,
811 // otherwise false positive errors may be generated by null pointer checks.
812 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
813 return false;
814 }
815
816 #define DCHECK(condition) \
817 LAZY_STREAM( \
818 LOG_STREAM(DCHECK), \
819 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \
820 << "Check failed: " #condition ". "
821
822 #define DPCHECK(condition) \
823 LAZY_STREAM( \
824 PLOG_STREAM(DCHECK), \
825 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \
826 << "Check failed: " #condition ". "
827
828 #else
829
813 #if DCHECK_IS_ON() 830 #if DCHECK_IS_ON()
814 831
815 #define DCHECK(condition) \ 832 #define DCHECK(condition) \
816 LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ 833 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \
817 << "Check failed: " #condition ". " 834 << "Check failed: " #condition ". "
818 #define DPCHECK(condition) \ 835 #define DPCHECK(condition) \
819 LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ 836 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \
820 << "Check failed: " #condition ". " 837 << "Check failed: " #condition ". "
821 838
822 #else // DCHECK_IS_ON() 839 #else // DCHECK_IS_ON()
823 840
824 #define DCHECK(condition) \ 841 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition)
825 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition) 842 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition)
826 #define DPCHECK(condition) \
827 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition)
828 843
829 #endif // DCHECK_IS_ON() 844 #endif // DCHECK_IS_ON()
830 845
846 #endif
831 847
832 // Helper macro for binary operators. 848 // Helper macro for binary operators.
833 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 849 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
834 // The 'switch' is used to prevent the 'else' from being ambiguous when the 850 // The 'switch' is used to prevent the 'else' from being ambiguous when the
835 // macro is used in an 'if' clause such as: 851 // macro is used in an 'if' clause such as:
836 // if (a == 1) 852 // if (a == 1)
837 // DCHECK_EQ(2, a); 853 // DCHECK_EQ(2, a);
838 #if DCHECK_IS_ON() 854 #if DCHECK_IS_ON()
839 855
840 #define DCHECK_OP(name, op, val1, val2) \ 856 #define DCHECK_OP(name, op, val1, val2) \
(...skipping 15 matching lines...) Expand all
856 // using |op|. 872 // using |op|.
857 // 873 //
858 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated 874 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated
859 // once. Even though |val1| and |val2| appear twice in this version of the macro 875 // once. Even though |val1| and |val2| appear twice in this version of the macro
860 // expansion, this is OK, since the expression is never actually evaluated. 876 // expansion, this is OK, since the expression is never actually evaluated.
861 #define DCHECK_OP(name, op, val1, val2) \ 877 #define DCHECK_OP(name, op, val1, val2) \
862 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ 878 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \
863 ::logging::g_swallow_stream, val1), \ 879 ::logging::g_swallow_stream, val1), \
864 ::logging::MakeCheckOpValueString( \ 880 ::logging::MakeCheckOpValueString( \
865 ::logging::g_swallow_stream, val2), \ 881 ::logging::g_swallow_stream, val2), \
866 ANALYZER_ASSUME_TRUE((val1)op(val2))) 882 (val1)op(val2))
867 883
868 #endif // DCHECK_IS_ON() 884 #endif // DCHECK_IS_ON()
869 885
870 // Equality/Inequality checks - compare two values, and log a 886 // Equality/Inequality checks - compare two values, and log a
871 // LOG_DCHECK message including the two values when the result is not 887 // LOG_DCHECK message including the two values when the result is not
872 // as expected. The values must have operator<<(ostream, ...) 888 // as expected. The values must have operator<<(ostream, ...)
873 // defined. 889 // defined.
874 // 890 //
875 // You may append to the error message like so: 891 // You may append to the error message like so:
876 // DCHECK_NE(1, 2) << "The world must be ending!"; 892 // DCHECK_NE(1, 2) << "The world must be ending!";
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 BASE_EXPORT void CloseLogFile(); 1061 BASE_EXPORT void CloseLogFile();
1046 1062
1047 // Async signal safe logging mechanism. 1063 // Async signal safe logging mechanism.
1048 BASE_EXPORT void RawLog(int level, const char* message); 1064 BASE_EXPORT void RawLog(int level, const char* message);
1049 1065
1050 #define RAW_LOG(level, message) \ 1066 #define RAW_LOG(level, message) \
1051 ::logging::RawLog(::logging::LOG_##level, message) 1067 ::logging::RawLog(::logging::LOG_##level, message)
1052 1068
1053 #define RAW_CHECK(condition) \ 1069 #define RAW_CHECK(condition) \
1054 do { \ 1070 do { \
1055 if (!ANALYZER_ASSUME_TRUE(condition)) \ 1071 if (!(condition)) \
1056 ::logging::RawLog(::logging::LOG_FATAL, \ 1072 ::logging::RawLog(::logging::LOG_FATAL, \
1057 "Check failed: " #condition "\n"); \ 1073 "Check failed: " #condition "\n"); \
1058 } while (0) 1074 } while (0)
1059 1075
1060 #if defined(OS_WIN) 1076 #if defined(OS_WIN)
1061 // Returns true if logging to file is enabled. 1077 // Returns true if logging to file is enabled.
1062 BASE_EXPORT bool IsLoggingToFileEnabled(); 1078 BASE_EXPORT bool IsLoggingToFileEnabled();
1063 1079
1064 // Returns the default log file path. 1080 // Returns the default log file path.
1065 BASE_EXPORT std::wstring GetLogFileFullPath(); 1081 BASE_EXPORT std::wstring GetLogFileFullPath();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 #elif NOTIMPLEMENTED_POLICY == 5 1146 #elif NOTIMPLEMENTED_POLICY == 5
1131 #define NOTIMPLEMENTED() do {\ 1147 #define NOTIMPLEMENTED() do {\
1132 static bool logged_once = false;\ 1148 static bool logged_once = false;\
1133 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1149 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
1134 logged_once = true;\ 1150 logged_once = true;\
1135 } while(0);\ 1151 } while(0);\
1136 EAT_STREAM_PARAMETERS 1152 EAT_STREAM_PARAMETERS
1137 #endif 1153 #endif
1138 1154
1139 #endif // BASE_LOGGING_H_ 1155 #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