| OLD | NEW |
| 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 #include "FindBadConstructsConsumer.h" | 5 #include "FindBadConstructsConsumer.h" |
| 6 | 6 |
| 7 #include "clang/Frontend/CompilerInstance.h" | 7 #include "clang/Frontend/CompilerInstance.h" |
| 8 #include "clang/AST/Attr.h" | 8 #include "clang/AST/Attr.h" |
| 9 #include "clang/Lex/Lexer.h" | 9 #include "clang/Lex/Lexer.h" |
| 10 #include "llvm/Support/raw_ostream.h" | 10 #include "llvm/Support/raw_ostream.h" |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 loc = dtor->getInnerLocStart(); | 634 loc = dtor->getInnerLocStart(); |
| 635 return PublicDestructor; | 635 return PublicDestructor; |
| 636 } | 636 } |
| 637 } | 637 } |
| 638 | 638 |
| 639 return None; | 639 return None; |
| 640 } | 640 } |
| 641 | 641 |
| 642 // Returns true if |base| specifies one of the Chromium reference counted | 642 // Returns true if |base| specifies one of the Chromium reference counted |
| 643 // classes (base::RefCounted / base::RefCountedThreadSafe). | 643 // classes (base::RefCounted / base::RefCountedThreadSafe). |
| 644 #if defined(LLVM_FORCE_HEAD_REVISION) | |
| 645 bool FindBadConstructsConsumer::IsRefCounted( | 644 bool FindBadConstructsConsumer::IsRefCounted( |
| 646 const CXXBaseSpecifier* base, | 645 const CXXBaseSpecifier* base, |
| 647 CXXBasePath& path) { | 646 CXXBasePath& path) { |
| 648 FindBadConstructsConsumer* self = this; | 647 FindBadConstructsConsumer* self = this; |
| 649 #else | |
| 650 // static | |
| 651 bool FindBadConstructsConsumer::IsRefCountedCallback( | |
| 652 const CXXBaseSpecifier* base, | |
| 653 CXXBasePath& path, | |
| 654 void* user_data) { | |
| 655 FindBadConstructsConsumer* self = | |
| 656 static_cast<FindBadConstructsConsumer*>(user_data); | |
| 657 #endif | |
| 658 const TemplateSpecializationType* base_type = | 648 const TemplateSpecializationType* base_type = |
| 659 dyn_cast<TemplateSpecializationType>( | 649 dyn_cast<TemplateSpecializationType>( |
| 660 UnwrapType(base->getType().getTypePtr())); | 650 UnwrapType(base->getType().getTypePtr())); |
| 661 if (!base_type) { | 651 if (!base_type) { |
| 662 // Base-most definition is not a template, so this cannot derive from | 652 // Base-most definition is not a template, so this cannot derive from |
| 663 // base::RefCounted. However, it may still be possible to use with a | 653 // base::RefCounted. However, it may still be possible to use with a |
| 664 // scoped_refptr<> and support ref-counting, so this is not a perfect | 654 // scoped_refptr<> and support ref-counting, so this is not a perfect |
| 665 // guarantee of safety. | 655 // guarantee of safety. |
| 666 return false; | 656 return false; |
| 667 } | 657 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 // sticking it in a non-ref-counted container (like scoped_ptr<>). | 723 // sticking it in a non-ref-counted container (like scoped_ptr<>). |
| 734 void FindBadConstructsConsumer::CheckRefCountedDtors( | 724 void FindBadConstructsConsumer::CheckRefCountedDtors( |
| 735 SourceLocation record_location, | 725 SourceLocation record_location, |
| 736 CXXRecordDecl* record) { | 726 CXXRecordDecl* record) { |
| 737 // Skip anonymous structs. | 727 // Skip anonymous structs. |
| 738 if (record->getIdentifier() == NULL) | 728 if (record->getIdentifier() == NULL) |
| 739 return; | 729 return; |
| 740 | 730 |
| 741 // Determine if the current type is even ref-counted. | 731 // Determine if the current type is even ref-counted. |
| 742 CXXBasePaths refcounted_path; | 732 CXXBasePaths refcounted_path; |
| 743 #if defined(LLVM_FORCE_HEAD_REVISION) | |
| 744 if (!record->lookupInBases( | 733 if (!record->lookupInBases( |
| 745 [this](const CXXBaseSpecifier* base, CXXBasePath& path) { | 734 [this](const CXXBaseSpecifier* base, CXXBasePath& path) { |
| 746 return IsRefCounted(base, path); | 735 return IsRefCounted(base, path); |
| 747 }, | 736 }, |
| 748 refcounted_path)) { | 737 refcounted_path)) { |
| 749 #else | |
| 750 if (!record->lookupInBases(&FindBadConstructsConsumer::IsRefCountedCallback, | |
| 751 this, | |
| 752 refcounted_path)) { | |
| 753 #endif | |
| 754 return; // Class does not derive from a ref-counted base class. | 738 return; // Class does not derive from a ref-counted base class. |
| 755 } | 739 } |
| 756 | 740 |
| 757 // Easy check: Check to see if the current type is problematic. | 741 // Easy check: Check to see if the current type is problematic. |
| 758 SourceLocation loc; | 742 SourceLocation loc; |
| 759 RefcountIssue issue = CheckRecordForRefcountIssue(record, loc); | 743 RefcountIssue issue = CheckRecordForRefcountIssue(record, loc); |
| 760 if (issue != None) { | 744 if (issue != None) { |
| 761 diagnostic().Report(loc, DiagnosticForIssue(issue)); | 745 diagnostic().Report(loc, DiagnosticForIssue(issue)); |
| 762 PrintInheritanceChain(refcounted_path.front()); | 746 PrintInheritanceChain(refcounted_path.front()); |
| 763 return; | 747 return; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 793 // scoped_refptr<RefCountedInterface> some_class( | 777 // scoped_refptr<RefCountedInterface> some_class( |
| 794 // new RefCountedInterface); | 778 // new RefCountedInterface); |
| 795 // // Calls SomeInterface::~SomeInterface(), which is unsafe. | 779 // // Calls SomeInterface::~SomeInterface(), which is unsafe. |
| 796 // delete static_cast<SomeInterface*>(some_class.get()); | 780 // delete static_cast<SomeInterface*>(some_class.get()); |
| 797 if (!options_.check_base_classes) | 781 if (!options_.check_base_classes) |
| 798 return; | 782 return; |
| 799 | 783 |
| 800 // Find all public destructors. This will record the class hierarchy | 784 // Find all public destructors. This will record the class hierarchy |
| 801 // that leads to the public destructor in |dtor_paths|. | 785 // that leads to the public destructor in |dtor_paths|. |
| 802 CXXBasePaths dtor_paths; | 786 CXXBasePaths dtor_paths; |
| 803 #if defined(LLVM_FORCE_HEAD_REVISION) | |
| 804 if (!record->lookupInBases( | 787 if (!record->lookupInBases( |
| 805 [](const CXXBaseSpecifier* base, CXXBasePath& path) { | 788 [](const CXXBaseSpecifier* base, CXXBasePath& path) { |
| 806 // TODO(thakis): Inline HasPublicDtorCallback() after clang roll. | 789 // TODO(thakis): Inline HasPublicDtorCallback() after clang roll. |
| 807 return HasPublicDtorCallback(base, path, nullptr); | 790 return HasPublicDtorCallback(base, path, nullptr); |
| 808 }, | 791 }, |
| 809 dtor_paths)) { | 792 dtor_paths)) { |
| 810 #else | |
| 811 if (!record->lookupInBases(&FindBadConstructsConsumer::HasPublicDtorCallback, | |
| 812 nullptr, | |
| 813 dtor_paths)) { | |
| 814 #endif | |
| 815 return; | 793 return; |
| 816 } | 794 } |
| 817 | 795 |
| 818 for (CXXBasePaths::const_paths_iterator it = dtor_paths.begin(); | 796 for (CXXBasePaths::const_paths_iterator it = dtor_paths.begin(); |
| 819 it != dtor_paths.end(); | 797 it != dtor_paths.end(); |
| 820 ++it) { | 798 ++it) { |
| 821 // The record with the problem will always be the last record | 799 // The record with the problem will always be the last record |
| 822 // in the path, since it is the record that stopped the search. | 800 // in the path, since it is the record that stopped the search. |
| 823 const CXXRecordDecl* problem_record = dyn_cast<CXXRecordDecl>( | 801 const CXXRecordDecl* problem_record = dyn_cast<CXXRecordDecl>( |
| 824 it->back().Base->getType()->getAs<RecordType>()->getDecl()); | 802 it->back().Base->getType()->getAs<RecordType>()->getDecl()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 // one of those, it means there is at least one member after a factory. | 866 // one of those, it means there is at least one member after a factory. |
| 889 if (weak_ptr_factory_location.isValid() && | 867 if (weak_ptr_factory_location.isValid() && |
| 890 !param_is_weak_ptr_factory_to_self) { | 868 !param_is_weak_ptr_factory_to_self) { |
| 891 diagnostic().Report(weak_ptr_factory_location, | 869 diagnostic().Report(weak_ptr_factory_location, |
| 892 diag_weak_ptr_factory_order_); | 870 diag_weak_ptr_factory_order_); |
| 893 } | 871 } |
| 894 } | 872 } |
| 895 } | 873 } |
| 896 | 874 |
| 897 } // namespace chrome_checker | 875 } // namespace chrome_checker |
| OLD | NEW |