| 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 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 break; | 612 break; |
| 613 } | 613 } |
| 614 } | 614 } |
| 615 } | 615 } |
| 616 | 616 |
| 617 // Check |record| for issues that are problematic for ref-counted types. | 617 // Check |record| for issues that are problematic for ref-counted types. |
| 618 // Note that |record| may not be a ref-counted type, but a base class for | 618 // Note that |record| may not be a ref-counted type, but a base class for |
| 619 // a type that is. | 619 // a type that is. |
| 620 // If there are issues, update |loc| with the SourceLocation of the issue | 620 // If there are issues, update |loc| with the SourceLocation of the issue |
| 621 // and returns appropriately, or returns None if there are no issues. | 621 // and returns appropriately, or returns None if there are no issues. |
| 622 // static |
| 622 FindBadConstructsConsumer::RefcountIssue | 623 FindBadConstructsConsumer::RefcountIssue |
| 623 FindBadConstructsConsumer::CheckRecordForRefcountIssue( | 624 FindBadConstructsConsumer::CheckRecordForRefcountIssue( |
| 624 const CXXRecordDecl* record, | 625 const CXXRecordDecl* record, |
| 625 SourceLocation& loc) { | 626 SourceLocation& loc) { |
| 626 if (!record->hasUserDeclaredDestructor()) { | 627 if (!record->hasUserDeclaredDestructor()) { |
| 627 loc = record->getLocation(); | 628 loc = record->getLocation(); |
| 628 return ImplicitDestructor; | 629 return ImplicitDestructor; |
| 629 } | 630 } |
| 630 | 631 |
| 631 if (CXXDestructorDecl* dtor = record->getDestructor()) { | 632 if (CXXDestructorDecl* dtor = record->getDestructor()) { |
| 632 if (dtor->getAccess() == AS_public) { | 633 if (dtor->getAccess() == AS_public) { |
| 633 loc = dtor->getInnerLocStart(); | 634 loc = dtor->getInnerLocStart(); |
| 634 return PublicDestructor; | 635 return PublicDestructor; |
| 635 } | 636 } |
| 636 } | 637 } |
| 637 | 638 |
| 638 return None; | 639 return None; |
| 639 } | 640 } |
| 640 | 641 |
| 641 // Returns true if |base| specifies one of the Chromium reference counted | 642 // Returns true if |base| specifies one of the Chromium reference counted |
| 642 // classes (base::RefCounted / base::RefCountedThreadSafe). | 643 // classes (base::RefCounted / base::RefCountedThreadSafe). |
| 643 bool FindBadConstructsConsumer::IsRefCountedCallback( | 644 bool FindBadConstructsConsumer::IsRefCounted( |
| 644 const CXXBaseSpecifier* base, | 645 const CXXBaseSpecifier* base, |
| 645 CXXBasePath& path, | 646 CXXBasePath& path) { |
| 646 void* user_data) { | 647 FindBadConstructsConsumer* self = this; |
| 647 FindBadConstructsConsumer* self = | |
| 648 static_cast<FindBadConstructsConsumer*>(user_data); | |
| 649 | |
| 650 const TemplateSpecializationType* base_type = | 648 const TemplateSpecializationType* base_type = |
| 651 dyn_cast<TemplateSpecializationType>( | 649 dyn_cast<TemplateSpecializationType>( |
| 652 UnwrapType(base->getType().getTypePtr())); | 650 UnwrapType(base->getType().getTypePtr())); |
| 653 if (!base_type) { | 651 if (!base_type) { |
| 654 // 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 |
| 655 // 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 |
| 656 // 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 |
| 657 // guarantee of safety. | 655 // guarantee of safety. |
| 658 return false; | 656 return false; |
| 659 } | 657 } |
| 660 | 658 |
| 661 TemplateName name = base_type->getTemplateName(); | 659 TemplateName name = base_type->getTemplateName(); |
| 662 if (TemplateDecl* decl = name.getAsTemplateDecl()) { | 660 if (TemplateDecl* decl = name.getAsTemplateDecl()) { |
| 663 std::string base_name = decl->getNameAsString(); | 661 std::string base_name = decl->getNameAsString(); |
| 664 | 662 |
| 665 // Check for both base::RefCounted and base::RefCountedThreadSafe. | 663 // Check for both base::RefCounted and base::RefCountedThreadSafe. |
| 666 if (base_name.compare(0, 10, "RefCounted") == 0 && | 664 if (base_name.compare(0, 10, "RefCounted") == 0 && |
| 667 self->GetNamespace(decl) == "base") { | 665 self->GetNamespace(decl) == "base") { |
| 668 return true; | 666 return true; |
| 669 } | 667 } |
| 670 } | 668 } |
| 671 | 669 |
| 672 return false; | 670 return false; |
| 673 } | 671 } |
| 674 | 672 |
| 675 // Returns true if |base| specifies a class that has a public destructor, | 673 // Returns true if |base| specifies a class that has a public destructor, |
| 676 // either explicitly or implicitly. | 674 // either explicitly or implicitly. |
| 675 // static |
| 677 bool FindBadConstructsConsumer::HasPublicDtorCallback( | 676 bool FindBadConstructsConsumer::HasPublicDtorCallback( |
| 678 const CXXBaseSpecifier* base, | 677 const CXXBaseSpecifier* base, |
| 679 CXXBasePath& path, | 678 CXXBasePath& path, |
| 680 void* user_data) { | 679 void* user_data) { |
| 681 // Only examine paths that have public inheritance, as they are the | 680 // Only examine paths that have public inheritance, as they are the |
| 682 // only ones which will result in the destructor potentially being | 681 // only ones which will result in the destructor potentially being |
| 683 // exposed. This check is largely redundant, as Chromium code should be | 682 // exposed. This check is largely redundant, as Chromium code should be |
| 684 // exclusively using public inheritance. | 683 // exclusively using public inheritance. |
| 685 if (path.Access != AS_public) | 684 if (path.Access != AS_public) |
| 686 return false; | 685 return false; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 // sticking it in a non-ref-counted container (like scoped_ptr<>). | 723 // sticking it in a non-ref-counted container (like scoped_ptr<>). |
| 725 void FindBadConstructsConsumer::CheckRefCountedDtors( | 724 void FindBadConstructsConsumer::CheckRefCountedDtors( |
| 726 SourceLocation record_location, | 725 SourceLocation record_location, |
| 727 CXXRecordDecl* record) { | 726 CXXRecordDecl* record) { |
| 728 // Skip anonymous structs. | 727 // Skip anonymous structs. |
| 729 if (record->getIdentifier() == NULL) | 728 if (record->getIdentifier() == NULL) |
| 730 return; | 729 return; |
| 731 | 730 |
| 732 // Determine if the current type is even ref-counted. | 731 // Determine if the current type is even ref-counted. |
| 733 CXXBasePaths refcounted_path; | 732 CXXBasePaths refcounted_path; |
| 734 if (!record->lookupInBases(&FindBadConstructsConsumer::IsRefCountedCallback, | 733 if (!record->lookupInBases( |
| 735 this, | 734 [this](const CXXBaseSpecifier* base, CXXBasePath& path) { |
| 736 refcounted_path)) { | 735 return IsRefCounted(base, path); |
| 736 }, |
| 737 refcounted_path)) { |
| 737 return; // Class does not derive from a ref-counted base class. | 738 return; // Class does not derive from a ref-counted base class. |
| 738 } | 739 } |
| 739 | 740 |
| 740 // Easy check: Check to see if the current type is problematic. | 741 // Easy check: Check to see if the current type is problematic. |
| 741 SourceLocation loc; | 742 SourceLocation loc; |
| 742 RefcountIssue issue = CheckRecordForRefcountIssue(record, loc); | 743 RefcountIssue issue = CheckRecordForRefcountIssue(record, loc); |
| 743 if (issue != None) { | 744 if (issue != None) { |
| 744 diagnostic().Report(loc, DiagnosticForIssue(issue)); | 745 diagnostic().Report(loc, DiagnosticForIssue(issue)); |
| 745 PrintInheritanceChain(refcounted_path.front()); | 746 PrintInheritanceChain(refcounted_path.front()); |
| 746 return; | 747 return; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 776 // scoped_refptr<RefCountedInterface> some_class( | 777 // scoped_refptr<RefCountedInterface> some_class( |
| 777 // new RefCountedInterface); | 778 // new RefCountedInterface); |
| 778 // // Calls SomeInterface::~SomeInterface(), which is unsafe. | 779 // // Calls SomeInterface::~SomeInterface(), which is unsafe. |
| 779 // delete static_cast<SomeInterface*>(some_class.get()); | 780 // delete static_cast<SomeInterface*>(some_class.get()); |
| 780 if (!options_.check_base_classes) | 781 if (!options_.check_base_classes) |
| 781 return; | 782 return; |
| 782 | 783 |
| 783 // Find all public destructors. This will record the class hierarchy | 784 // Find all public destructors. This will record the class hierarchy |
| 784 // that leads to the public destructor in |dtor_paths|. | 785 // that leads to the public destructor in |dtor_paths|. |
| 785 CXXBasePaths dtor_paths; | 786 CXXBasePaths dtor_paths; |
| 786 if (!record->lookupInBases(&FindBadConstructsConsumer::HasPublicDtorCallback, | 787 if (!record->lookupInBases( |
| 787 this, | 788 [](const CXXBaseSpecifier* base, CXXBasePath& path) { |
| 788 dtor_paths)) { | 789 // TODO(thakis): Inline HasPublicDtorCallback() after clang roll. |
| 790 return HasPublicDtorCallback(base, path, nullptr); |
| 791 }, |
| 792 dtor_paths)) { |
| 789 return; | 793 return; |
| 790 } | 794 } |
| 791 | 795 |
| 792 for (CXXBasePaths::const_paths_iterator it = dtor_paths.begin(); | 796 for (CXXBasePaths::const_paths_iterator it = dtor_paths.begin(); |
| 793 it != dtor_paths.end(); | 797 it != dtor_paths.end(); |
| 794 ++it) { | 798 ++it) { |
| 795 // The record with the problem will always be the last record | 799 // The record with the problem will always be the last record |
| 796 // 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. |
| 797 const CXXRecordDecl* problem_record = dyn_cast<CXXRecordDecl>( | 801 const CXXRecordDecl* problem_record = dyn_cast<CXXRecordDecl>( |
| 798 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... |
| 862 // 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. |
| 863 if (weak_ptr_factory_location.isValid() && | 867 if (weak_ptr_factory_location.isValid() && |
| 864 !param_is_weak_ptr_factory_to_self) { | 868 !param_is_weak_ptr_factory_to_self) { |
| 865 diagnostic().Report(weak_ptr_factory_location, | 869 diagnostic().Report(weak_ptr_factory_location, |
| 866 diag_weak_ptr_factory_order_); | 870 diag_weak_ptr_factory_order_); |
| 867 } | 871 } |
| 868 } | 872 } |
| 869 } | 873 } |
| 870 | 874 |
| 871 } // namespace chrome_checker | 875 } // namespace chrome_checker |
| OLD | NEW |