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

Side by Side Diff: base/logging.h

Issue 2669213005: Add new macro "ANALYZER_ASSUME_TRUE" and integrate with all assertion types. (Closed)
Patch Set: rebase Created 3 years, 10 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 template <typename TVal>
302 inline constexpr TVal&& AnalysisAssumeTrue(TVal&& arg) {
303 if (!arg) {
304 AnalyzerNoReturn();
305 }
306 return std::forward<TVal>(arg);
Nico 2017/02/10 18:08:47 this looks a lot more complicated than the last ti
Kevin M 2017/02/10 18:20:08 Using rvalue/forward() means this function can be
307 }
308
309 #define ANALYZER_ASSUME_TRUE(val) ::logging::AnalysisAssumeTrue(val)
310
311 #elif defined(_PREFAST_) && defined(OS_WIN)
312
313 template <typename TVal>
314 inline constexpr TVal&& AnalysisAssumeTrue(TVal&& arg) {
315 __analysis_assume(!!arg);
316 return std::forward<TVal>(arg);
317 }
318
319 #define ANALYZER_ASSUME_TRUE(val) ::logging::AnalysisAssumeTrue(val)
320
321 #else // !_PREFAST_ & !__clang_analyzer__
322
323 #define ANALYZER_ASSUME_TRUE(val) (val)
324
325 #endif // !_PREFAST_ & !__clang_analyzer__
326
292 typedef int LogSeverity; 327 typedef int LogSeverity;
293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity 328 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
294 // Note: the log severities are used to index into the array of names, 329 // Note: the log severities are used to index into the array of names,
295 // see log_severity_names. 330 // see log_severity_names.
296 const LogSeverity LOG_INFO = 0; 331 const LogSeverity LOG_INFO = 0;
297 const LogSeverity LOG_WARNING = 1; 332 const LogSeverity LOG_WARNING = 1;
298 const LogSeverity LOG_ERROR = 2; 333 const LogSeverity LOG_ERROR = 2;
299 const LogSeverity LOG_FATAL = 3; 334 const LogSeverity LOG_FATAL = 3;
300 const LogSeverity LOG_NUM_SEVERITIES = 4; 335 const LogSeverity LOG_NUM_SEVERITIES = 4;
301 336
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 436
402 #define VPLOG(verbose_level) \ 437 #define VPLOG(verbose_level) \
403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) 438 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
404 439
405 #define VPLOG_IF(verbose_level, condition) \ 440 #define VPLOG_IF(verbose_level, condition) \
406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ 441 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
407 VLOG_IS_ON(verbose_level) && (condition)) 442 VLOG_IS_ON(verbose_level) && (condition))
408 443
409 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. 444 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
410 445
411 #define LOG_ASSERT(condition) \ 446 #define LOG_ASSERT(condition) \
412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 447 LOG_IF(FATAL, !ANALYZER_ASSUME_TRUE(condition)) \
448 << "Assert failed: " #condition ". "
413 449
414 #if defined(OS_WIN) 450 #if defined(OS_WIN)
415 #define PLOG_STREAM(severity) \ 451 #define PLOG_STREAM(severity) \
416 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ 452 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
417 ::logging::GetLastSystemErrorCode()).stream() 453 ::logging::GetLastSystemErrorCode()).stream()
418 #elif defined(OS_POSIX) 454 #elif defined(OS_POSIX)
419 #define PLOG_STREAM(severity) \ 455 #define PLOG_STREAM(severity) \
420 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ 456 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
421 ::logging::GetLastSystemErrorCode()).stream() 457 ::logging::GetLastSystemErrorCode()).stream()
422 #endif 458 #endif
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 // compiler optimizations. 520 // compiler optimizations.
485 #define CHECK(condition) \ 521 #define CHECK(condition) \
486 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS 522 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS
487 523
488 #define PCHECK(condition) CHECK(condition) 524 #define PCHECK(condition) CHECK(condition)
489 525
490 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) 526 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
491 527
492 #else // !(OFFICIAL_BUILD && NDEBUG) 528 #else // !(OFFICIAL_BUILD && NDEBUG)
493 529
494 #if defined(_PREFAST_) && defined(OS_WIN)
495 // Use __analysis_assume to tell the VC++ static analysis engine that
496 // assert conditions are true, to suppress warnings. The LAZY_STREAM
497 // parameter doesn't reference 'condition' in /analyze builds because
498 // this evaluation confuses /analyze. The !! before condition is because
499 // __analysis_assume gets confused on some conditions:
500 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/
501
502 #define CHECK(condition) \
503 __analysis_assume(!!(condition)), \
504 LAZY_STREAM(LOG_STREAM(FATAL), false) \
505 << "Check failed: " #condition ". "
506
507 #define PCHECK(condition) \
508 __analysis_assume(!!(condition)), \
509 LAZY_STREAM(PLOG_STREAM(FATAL), false) \
510 << "Check failed: " #condition ". "
511
512 #else // _PREFAST_
513
514 // Do as much work as possible out of line to reduce inline code size. 530 // Do as much work as possible out of line to reduce inline code size.
515 #define CHECK(condition) \ 531 #define CHECK(condition) \
516 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ 532 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \
517 !(condition)) 533 !ANALYZER_ASSUME_TRUE(condition))
518 534
519 #define PCHECK(condition) \ 535 #define PCHECK(condition) \
520 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 536 LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \
521 << "Check failed: " #condition ". " 537 << "Check failed: " #condition ". "
522
523 #endif // _PREFAST_
524 538
525 // Helper macro for binary operators. 539 // Helper macro for binary operators.
526 // Don't use this macro directly in your code, use CHECK_EQ et al below. 540 // Don't use this macro directly in your code, use CHECK_EQ et al below.
527 // The 'switch' is used to prevent the 'else' from being ambiguous when the 541 // The 'switch' is used to prevent the 'else' from being ambiguous when the
528 // macro is used in an 'if' clause such as: 542 // macro is used in an 'if' clause such as:
529 // if (a == 1) 543 // if (a == 1)
530 // CHECK_EQ(2, a); 544 // CHECK_EQ(2, a);
531 #define CHECK_OP(name, op, val1, val2) \ 545 #define CHECK_OP(name, op, val1, val2) \
532 switch (0) case 0: default: \ 546 switch (0) case 0: default: \
533 if (::logging::CheckOpResult true_if_passed = \ 547 if (::logging::CheckOpResult true_if_passed = \
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 724
711 // DCHECK et al. make sure to reference |condition| regardless of 725 // DCHECK et al. make sure to reference |condition| regardless of
712 // whether DCHECKs are enabled; this is so that we don't get unused 726 // 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. 727 // variable warnings if the only use of a variable is in a DCHECK.
714 // This behavior is different from DLOG_IF et al. 728 // This behavior is different from DLOG_IF et al.
715 // 729 //
716 // Note that the definition of the DCHECK macros depends on whether or not 730 // 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 731 // 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. 732 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries.
719 733
720 #if defined(_PREFAST_) && defined(OS_WIN)
721 // See comments on the previous use of __analysis_assume.
722
723 #define DCHECK(condition) \
724 __analysis_assume(!!(condition)), \
725 LAZY_STREAM(LOG_STREAM(DCHECK), false) \
726 << "Check failed: " #condition ". "
727
728 #define DPCHECK(condition) \
729 __analysis_assume(!!(condition)), \
730 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \
731 << "Check failed: " #condition ". "
732
733 #elif defined(__clang_analyzer__)
734
735 // Keeps the static analyzer from proceeding along the current codepath,
736 // otherwise false positive errors may be generated by null pointer checks.
737 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
738 return false;
739 }
740
741 #define DCHECK(condition) \
742 LAZY_STREAM( \
743 LOG_STREAM(DCHECK), \
744 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \
745 << "Check failed: " #condition ". "
746
747 #define DPCHECK(condition) \
748 LAZY_STREAM( \
749 PLOG_STREAM(DCHECK), \
750 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \
751 << "Check failed: " #condition ". "
752
753 #else
754
755 #if DCHECK_IS_ON() 734 #if DCHECK_IS_ON()
756 735
757 #define DCHECK(condition) \ 736 #define DCHECK(condition) \
758 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \ 737 LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \
759 << "Check failed: " #condition ". " 738 << "Check failed: " #condition ". "
760 #define DPCHECK(condition) \ 739 #define DPCHECK(condition) \
761 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \ 740 LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \
762 << "Check failed: " #condition ". " 741 << "Check failed: " #condition ". "
763 742
764 #else // DCHECK_IS_ON() 743 #else // DCHECK_IS_ON()
765 744
766 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) 745 #define DCHECK(condition) \
767 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) 746 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition)
747 #define DPCHECK(condition) \
748 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition)
768 749
769 #endif // DCHECK_IS_ON() 750 #endif // DCHECK_IS_ON()
770 751
771 #endif
772 752
773 // Helper macro for binary operators. 753 // Helper macro for binary operators.
774 // 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.
775 // 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
776 // macro is used in an 'if' clause such as: 756 // macro is used in an 'if' clause such as:
777 // if (a == 1) 757 // if (a == 1)
778 // DCHECK_EQ(2, a); 758 // DCHECK_EQ(2, a);
779 #if DCHECK_IS_ON() 759 #if DCHECK_IS_ON()
780 760
781 #define DCHECK_OP(name, op, val1, val2) \ 761 #define DCHECK_OP(name, op, val1, val2) \
(...skipping 15 matching lines...) Expand all
797 // using |op|. 777 // using |op|.
798 // 778 //
799 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated 779 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated
800 // once. Even though |val1| and |val2| appear twice in this version of the macro 780 // once. Even though |val1| and |val2| appear twice in this version of the macro
801 // expansion, this is OK, since the expression is never actually evaluated. 781 // expansion, this is OK, since the expression is never actually evaluated.
802 #define DCHECK_OP(name, op, val1, val2) \ 782 #define DCHECK_OP(name, op, val1, val2) \
803 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ 783 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \
804 ::logging::g_swallow_stream, val1), \ 784 ::logging::g_swallow_stream, val1), \
805 ::logging::MakeCheckOpValueString( \ 785 ::logging::MakeCheckOpValueString( \
806 ::logging::g_swallow_stream, val2), \ 786 ::logging::g_swallow_stream, val2), \
807 (val1)op(val2)) 787 ANALYZER_ASSUME_TRUE((val1)op(val2)))
808 788
809 #endif // DCHECK_IS_ON() 789 #endif // DCHECK_IS_ON()
810 790
811 // Equality/Inequality checks - compare two values, and log a 791 // Equality/Inequality checks - compare two values, and log a
812 // 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
813 // as expected. The values must have operator<<(ostream, ...) 793 // as expected. The values must have operator<<(ostream, ...)
814 // defined. 794 // defined.
815 // 795 //
816 // You may append to the error message like so: 796 // You may append to the error message like so:
817 // DCHECK_NE(1, 2) << "The world must be ending!"; 797 // DCHECK_NE(1, 2) << "The world must be ending!";
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 BASE_EXPORT void CloseLogFile(); 966 BASE_EXPORT void CloseLogFile();
987 967
988 // Async signal safe logging mechanism. 968 // Async signal safe logging mechanism.
989 BASE_EXPORT void RawLog(int level, const char* message); 969 BASE_EXPORT void RawLog(int level, const char* message);
990 970
991 #define RAW_LOG(level, message) \ 971 #define RAW_LOG(level, message) \
992 ::logging::RawLog(::logging::LOG_##level, message) 972 ::logging::RawLog(::logging::LOG_##level, message)
993 973
994 #define RAW_CHECK(condition) \ 974 #define RAW_CHECK(condition) \
995 do { \ 975 do { \
996 if (!(condition)) \ 976 if (!ANALYZER_ASSUME_TRUE(condition)) \
997 ::logging::RawLog(::logging::LOG_FATAL, \ 977 ::logging::RawLog(::logging::LOG_FATAL, \
998 "Check failed: " #condition "\n"); \ 978 "Check failed: " #condition "\n"); \
999 } while (0) 979 } while (0)
1000 980
1001 #if defined(OS_WIN) 981 #if defined(OS_WIN)
1002 // Returns true if logging to file is enabled. 982 // Returns true if logging to file is enabled.
1003 BASE_EXPORT bool IsLoggingToFileEnabled(); 983 BASE_EXPORT bool IsLoggingToFileEnabled();
1004 984
1005 // Returns the default log file path. 985 // Returns the default log file path.
1006 BASE_EXPORT std::wstring GetLogFileFullPath(); 986 BASE_EXPORT std::wstring GetLogFileFullPath();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 #elif NOTIMPLEMENTED_POLICY == 5 1051 #elif NOTIMPLEMENTED_POLICY == 5
1072 #define NOTIMPLEMENTED() do {\ 1052 #define NOTIMPLEMENTED() do {\
1073 static bool logged_once = false;\ 1053 static bool logged_once = false;\
1074 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1054 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
1075 logged_once = true;\ 1055 logged_once = true;\
1076 } while(0);\ 1056 } while(0);\
1077 EAT_STREAM_PARAMETERS 1057 EAT_STREAM_PARAMETERS
1078 #endif 1058 #endif
1079 1059
1080 #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