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

Side by Side Diff: base/logging.h

Issue 2669213005: Add new macro "ANALYZER_ASSUME_TRUE" and integrate with all assertion types. (Closed)
Patch Set: Fix wonky formatting 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 // ANALYSIS_MARK_ASSERTION(...) adds compiler-specific annotations to an
293 // asserted condition which stop static analysis if the asserted condition is
294 // untrue.
295 #if defined(__clang_analyzer__)
296
297 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
298 return false;
299 }
300
301 #define ANALYSIS_MARK_ASSERTION(condition) \
302 (!!(condition) || logging::AnalyzerNoReturn())
303
304 #elif defined(_PREFAST_) && defined(OS_WIN)
305
306 #define ANALYSIS_MARK_ASSERTION(condition) __analysis_assume(!!(condition))
307
308 #else // !_PREFAST_ & !__clang_analyzer__
309
310 // No analyzer attached - just pass it through.
311 #define ANALYSIS_MARK_ASSERTION(condition) !!(condition)
brucedawson 2017/02/02 21:37:57 Why the double ! here? They are needed in the /ana
Kevin M 2017/02/02 21:46:43 Wasn't sure if casting to a bool in this way yield
312
313 #endif // !_PREFAST_ & !__clang_analyzer__
314
292 typedef int LogSeverity; 315 typedef int LogSeverity;
293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity 316 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
294 // Note: the log severities are used to index into the array of names, 317 // Note: the log severities are used to index into the array of names,
295 // see log_severity_names. 318 // see log_severity_names.
296 const LogSeverity LOG_INFO = 0; 319 const LogSeverity LOG_INFO = 0;
297 const LogSeverity LOG_WARNING = 1; 320 const LogSeverity LOG_WARNING = 1;
298 const LogSeverity LOG_ERROR = 2; 321 const LogSeverity LOG_ERROR = 2;
299 const LogSeverity LOG_FATAL = 3; 322 const LogSeverity LOG_FATAL = 3;
300 const LogSeverity LOG_NUM_SEVERITIES = 4; 323 const LogSeverity LOG_NUM_SEVERITIES = 4;
301 324
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 424
402 #define VPLOG(verbose_level) \ 425 #define VPLOG(verbose_level) \
403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) 426 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
404 427
405 #define VPLOG_IF(verbose_level, condition) \ 428 #define VPLOG_IF(verbose_level, condition) \
406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ 429 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
407 VLOG_IS_ON(verbose_level) && (condition)) 430 VLOG_IS_ON(verbose_level) && (condition))
408 431
409 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. 432 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
410 433
411 #define LOG_ASSERT(condition) \ 434 #define LOG_ASSERT(condition) \
412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 435 LOG_IF(FATAL, !ANALYSIS_MARK_ASSERTION(condition)) \
436 << "Assert failed: " #condition ". "
413 437
414 #if defined(OS_WIN) 438 #if defined(OS_WIN)
415 #define PLOG_STREAM(severity) \ 439 #define PLOG_STREAM(severity) \
416 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ 440 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
417 ::logging::GetLastSystemErrorCode()).stream() 441 ::logging::GetLastSystemErrorCode()).stream()
418 #elif defined(OS_POSIX) 442 #elif defined(OS_POSIX)
419 #define PLOG_STREAM(severity) \ 443 #define PLOG_STREAM(severity) \
420 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ 444 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
421 ::logging::GetLastSystemErrorCode()).stream() 445 ::logging::GetLastSystemErrorCode()).stream()
422 #endif 446 #endif
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 // doing CHECK(FunctionWithSideEffect()) is a common idiom. 499 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
476 500
477 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) 501 #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
478 502
479 // Make all CHECK functions discard their log strings to reduce code bloat, and 503 // Make all CHECK functions discard their log strings to reduce code bloat, and
480 // improve performance, for official release builds. 504 // improve performance, for official release builds.
481 // 505 //
482 // This is not calling BreakDebugger since this is called frequently, and 506 // This is not calling BreakDebugger since this is called frequently, and
483 // calling an out-of-line function instead of a noreturn inline macro prevents 507 // calling an out-of-line function instead of a noreturn inline macro prevents
484 // compiler optimizations. 508 // compiler optimizations.
485 #define CHECK(condition) \ 509 #define CHECK(condition) \
486 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS 510 UNLIKELY(!ANALYSIS_MARK_ASSERTION(condition)) ? IMMEDIATE_CRASH() \
511 : EAT_STREAM_PARAMETERS
487 512
488 #define PCHECK(condition) CHECK(condition) 513 #define PCHECK(condition) CHECK(condition)
489 514
490 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) 515 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
491 516
492 #else // !(OFFICIAL_BUILD && NDEBUG) 517 #else // !(OFFICIAL_BUILD && NDEBUG)
493 518
494 #if defined(_PREFAST_) && defined(OS_WIN) 519 #if defined(_PREFAST_) && defined(OS_WIN)
495 // Use __analysis_assume to tell the VC++ static analysis engine that 520 // The LAZY_STREAM parameter doesn't reference 'condition' in /analyze builds
496 // assert conditions are true, to suppress warnings. The LAZY_STREAM 521 // because this evaluation confuses /analyze. The !! before condition is because
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: 522 // __analysis_assume gets confused on some conditions:
500 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/ 523 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/
501 524
502 #define CHECK(condition) \ 525 #define CHECK(condition) \
503 __analysis_assume(!!(condition)), \ 526 ANALYSIS_MARK_ASSERTION(condition) \
504 LAZY_STREAM(LOG_STREAM(FATAL), false) \ 527 , LAZY_STREAM(LOG_STREAM(FATAL), false) << "Check failed: " #condition ". "
505 << "Check failed: " #condition ". "
506 528
507 #define PCHECK(condition) \ 529 #define PCHECK(condition) \
508 __analysis_assume(!!(condition)), \ 530 ANALYSIS_MARK_ASSERTION(condition) \
509 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ 531 , LAZY_STREAM(PLOG_STREAM(FATAL), false) << "Check failed: " #condition ". "
510 << "Check failed: " #condition ". "
511 532
512 #else // _PREFAST_ 533 #else // _PREFAST_
513 534
514 // Do as much work as possible out of line to reduce inline code size. 535 // Do as much work as possible out of line to reduce inline code size.
515 #define CHECK(condition) \ 536 #define CHECK(condition) \
516 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ 537 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \
517 !(condition)) 538 ANALYSIS_MARK_ASSERTION(condition))
brucedawson 2017/02/02 21:37:57 I think you've gone from !(condition) to !!(condit
Kevin M 2017/02/02 21:46:44 Done.
518 539
519 #define PCHECK(condition) \ 540 #define PCHECK(condition) \
520 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 541 LAZY_STREAM(PLOG_STREAM(FATAL), ANALYSIS_MARK_ASSERTION(condition)) \
brucedawson 2017/02/02 21:37:57 I think you've gone from !(condition) to !!(condit
Kevin M 2017/02/02 21:46:44 Done.
521 << "Check failed: " #condition ". " 542 << "Check failed: " #condition ". "
522 543
523 #endif // _PREFAST_ 544 #endif // _PREFAST_
524 545
525 // Helper macro for binary operators. 546 // Helper macro for binary operators.
526 // Don't use this macro directly in your code, use CHECK_EQ et al below. 547 // 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 548 // The 'switch' is used to prevent the 'else' from being ambiguous when the
528 // macro is used in an 'if' clause such as: 549 // macro is used in an 'if' clause such as:
529 // if (a == 1) 550 // if (a == 1)
530 // CHECK_EQ(2, a); 551 // CHECK_EQ(2, a);
531 #define CHECK_OP(name, op, val1, val2) \ 552 #define CHECK_OP(name, op, val1, val2) \
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 #endif // DCHECK_IS_ON() 730 #endif // DCHECK_IS_ON()
710 731
711 // DCHECK et al. make sure to reference |condition| regardless of 732 // DCHECK et al. make sure to reference |condition| regardless of
712 // whether DCHECKs are enabled; this is so that we don't get unused 733 // 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. 734 // variable warnings if the only use of a variable is in a DCHECK.
714 // This behavior is different from DLOG_IF et al. 735 // This behavior is different from DLOG_IF et al.
715 // 736 //
716 // Note that the definition of the DCHECK macros depends on whether or not 737 // 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 738 // 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. 739 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries.
740 #if DCHECK_IS_ON()
719 741
720 #if defined(_PREFAST_) && defined(OS_WIN) 742 #define DCHECK(condition) \
721 // See comments on the previous use of __analysis_assume. 743 LAZY_STREAM(LOG_STREAM(DCHECK), \
722 744 DCHECK_IS_ON() ? !ANALYSIS_MARK_ASSERTION(condition) : false) \
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 ". " 745 << "Check failed: " #condition ". "
746 746
747 #define DPCHECK(condition) \ 747 #define DPCHECK(condition) \
748 LAZY_STREAM( \ 748 LAZY_STREAM(PLOG_STREAM(DCHECK), \
749 PLOG_STREAM(DCHECK), \ 749 DCHECK_IS_ON() ? !ANALYSIS_MARK_ASSERTION(condition) : false) \
750 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \
751 << "Check failed: " #condition ". "
752
753 #else
754
755 #if DCHECK_IS_ON()
756
757 #define DCHECK(condition) \
758 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \
759 << "Check failed: " #condition ". "
760 #define DPCHECK(condition) \
761 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \
762 << "Check failed: " #condition ". " 750 << "Check failed: " #condition ". "
763 751
764 #else // DCHECK_IS_ON() 752 #else // DCHECK_IS_ON()
765 753
766 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) 754 #define DCHECK(condition) \
767 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) 755 EAT_STREAM_PARAMETERS << !ANALYSIS_MARK_ASSERTION(condition)
756 #define DPCHECK(condition) \
757 EAT_STREAM_PARAMETERS << !ANALYSIS_MARK_ASSERTION(condition)
768 758
769 #endif // DCHECK_IS_ON() 759 #endif // DCHECK_IS_ON()
770 760
771 #endif
772
773 // Helper macro for binary operators. 761 // Helper macro for binary operators.
774 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 762 // 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 763 // The 'switch' is used to prevent the 'else' from being ambiguous when the
776 // macro is used in an 'if' clause such as: 764 // macro is used in an 'if' clause such as:
777 // if (a == 1) 765 // if (a == 1)
778 // DCHECK_EQ(2, a); 766 // DCHECK_EQ(2, a);
779 #if DCHECK_IS_ON() 767 #if DCHECK_IS_ON()
780 768
781 #define DCHECK_OP(name, op, val1, val2) \ 769 #define DCHECK_OP(name, op, val1, val2) \
782 switch (0) case 0: default: \ 770 switch (0) case 0: default: \
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 BASE_EXPORT void CloseLogFile(); 974 BASE_EXPORT void CloseLogFile();
987 975
988 // Async signal safe logging mechanism. 976 // Async signal safe logging mechanism.
989 BASE_EXPORT void RawLog(int level, const char* message); 977 BASE_EXPORT void RawLog(int level, const char* message);
990 978
991 #define RAW_LOG(level, message) \ 979 #define RAW_LOG(level, message) \
992 ::logging::RawLog(::logging::LOG_##level, message) 980 ::logging::RawLog(::logging::LOG_##level, message)
993 981
994 #define RAW_CHECK(condition) \ 982 #define RAW_CHECK(condition) \
995 do { \ 983 do { \
996 if (!(condition)) \ 984 if (!ANALYSIS_MARK_ASSERTION(condition)) \
997 ::logging::RawLog(::logging::LOG_FATAL, \ 985 ::logging::RawLog(::logging::LOG_FATAL, \
998 "Check failed: " #condition "\n"); \ 986 "Check failed: " #condition "\n"); \
999 } while (0) 987 } while (0)
1000 988
1001 #if defined(OS_WIN) 989 #if defined(OS_WIN)
1002 // Returns true if logging to file is enabled. 990 // Returns true if logging to file is enabled.
1003 BASE_EXPORT bool IsLoggingToFileEnabled(); 991 BASE_EXPORT bool IsLoggingToFileEnabled();
1004 992
1005 // Returns the default log file path. 993 // Returns the default log file path.
1006 BASE_EXPORT std::wstring GetLogFileFullPath(); 994 BASE_EXPORT std::wstring GetLogFileFullPath();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 #elif NOTIMPLEMENTED_POLICY == 5 1059 #elif NOTIMPLEMENTED_POLICY == 5
1072 #define NOTIMPLEMENTED() do {\ 1060 #define NOTIMPLEMENTED() do {\
1073 static bool logged_once = false;\ 1061 static bool logged_once = false;\
1074 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1062 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
1075 logged_once = true;\ 1063 logged_once = true;\
1076 } while(0);\ 1064 } while(0);\
1077 EAT_STREAM_PARAMETERS 1065 EAT_STREAM_PARAMETERS
1078 #endif 1066 #endif
1079 1067
1080 #endif // BASE_LOGGING_H_ 1068 #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