FindBadConstructsConsumer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  3. // found in the LICENSE file.
  4. // This file defines a bunch of recurring problems in the Chromium C++ code.
  5. //
  6. // Checks that are implemented:
  7. // - Constructors/Destructors should not be inlined if they are of a complex
  8. // class type.
  9. // - Missing "virtual" keywords on methods that should be virtual.
  10. // - Non-annotated overriding virtual methods.
  11. // - Virtual methods with nonempty implementations in their headers.
  12. // - Classes that derive from base::RefCounted / base::RefCountedThreadSafe
  13. // should have protected or private destructors.
  14. // - WeakPtrFactory members that refer to their outer class should be the last
  15. // member.
  16. // - Enum types with a xxxx_LAST or xxxxLast const actually have that constant
  17. // have the maximal value for that type.
  18. #ifndef TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
  19. #define TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
  20. #include <memory>
  21. #include "clang/AST/AST.h"
  22. #include "clang/AST/ASTConsumer.h"
  23. #include "clang/AST/Attr.h"
  24. #include "clang/AST/CXXInheritance.h"
  25. #include "clang/AST/RecursiveASTVisitor.h"
  26. #include "clang/AST/TypeLoc.h"
  27. #include "clang/Basic/SourceManager.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "CheckIPCVisitor.h"
  30. #include "ChromeClassTester.h"
  31. #include "Options.h"
  32. #include "SuppressibleDiagnosticBuilder.h"
  33. namespace chrome_checker {
  34. // Searches for constructs that we know we don't want in the Chromium code base.
  35. class FindBadConstructsConsumer
  36. : public clang::RecursiveASTVisitor<FindBadConstructsConsumer>,
  37. public ChromeClassTester {
  38. public:
  39. FindBadConstructsConsumer(clang::CompilerInstance& instance,
  40. const Options& options);
  41. void Traverse(clang::ASTContext& context);
  42. // RecursiveASTVisitor:
  43. bool TraverseDecl(clang::Decl* decl);
  44. bool VisitEnumDecl(clang::EnumDecl* enum_decl);
  45. bool VisitTagDecl(clang::TagDecl* tag_decl);
  46. bool VisitVarDecl(clang::VarDecl* var_decl);
  47. bool VisitTemplateSpecializationType(clang::TemplateSpecializationType* spec);
  48. bool VisitCallExpr(clang::CallExpr* call_expr);
  49. // ChromeClassTester overrides:
  50. void CheckChromeClass(LocationType location_type,
  51. clang::SourceLocation record_location,
  52. clang::CXXRecordDecl* record) override;
  53. private:
  54. // The type of problematic ref-counting pattern that was encountered.
  55. enum RefcountIssue { None, ImplicitDestructor, PublicDestructor };
  56. void CheckCtorDtorWeight(clang::SourceLocation record_location,
  57. clang::CXXRecordDecl* record);
  58. // Returns a diagnostic builder that only emits the diagnostic if the spelling
  59. // location (the actual characters that make up the token) is not in an
  60. // ignored file. This is useful for situations where the token might originate
  61. // from a macro in a system header: warning isn't useful, since system headers
  62. // generally can't be easily updated.
  63. SuppressibleDiagnosticBuilder ReportIfSpellingLocNotIgnored(
  64. clang::SourceLocation loc,
  65. unsigned diagnostic_id);
  66. void CheckVirtualMethods(clang::SourceLocation record_location,
  67. clang::CXXRecordDecl* record,
  68. bool warn_on_inline_bodies);
  69. void CheckVirtualSpecifiers(const clang::CXXMethodDecl* method);
  70. void CheckVirtualBodies(const clang::CXXMethodDecl* method);
  71. void CountType(const clang::Type* type,
  72. int* trivial_member,
  73. int* non_trivial_member,
  74. int* templated_non_trivial_member);
  75. static RefcountIssue CheckRecordForRefcountIssue(
  76. const clang::CXXRecordDecl* record,
  77. clang::SourceLocation& loc);
  78. bool IsRefCounted(const clang::CXXBaseSpecifier* base,
  79. clang::CXXBasePath& path);
  80. static bool HasPublicDtorCallback(const clang::CXXBaseSpecifier* base,
  81. clang::CXXBasePath& path,
  82. void* user_data);
  83. void PrintInheritanceChain(const clang::CXXBasePath& path);
  84. unsigned DiagnosticForIssue(RefcountIssue issue);
  85. void CheckRefCountedDtors(clang::SourceLocation record_location,
  86. clang::CXXRecordDecl* record);
  87. void CheckWeakPtrFactoryMembers(clang::SourceLocation record_location,
  88. clang::CXXRecordDecl* record);
  89. void CheckEnumMaxValue(clang::EnumDecl* decl);
  90. void CheckVarDecl(clang::VarDecl* decl);
  91. void ParseFunctionTemplates(clang::TranslationUnitDecl* decl);
  92. unsigned diag_method_requires_override_;
  93. unsigned diag_redundant_virtual_specifier_;
  94. unsigned diag_will_be_redundant_virtual_specifier_;
  95. unsigned diag_base_method_virtual_and_final_;
  96. unsigned diag_virtual_with_inline_body_;
  97. unsigned diag_no_explicit_ctor_;
  98. unsigned diag_no_explicit_copy_ctor_;
  99. unsigned diag_inline_complex_ctor_;
  100. unsigned diag_no_explicit_dtor_;
  101. unsigned diag_inline_complex_dtor_;
  102. unsigned diag_refcounted_needs_explicit_dtor_;
  103. unsigned diag_refcounted_with_public_dtor_;
  104. unsigned diag_refcounted_with_protected_non_virtual_dtor_;
  105. unsigned diag_weak_ptr_factory_order_;
  106. unsigned diag_bad_enum_max_value_;
  107. unsigned diag_enum_max_value_unique_;
  108. unsigned diag_auto_deduced_to_a_pointer_type_;
  109. unsigned diag_note_inheritance_;
  110. unsigned diag_note_implicit_dtor_;
  111. unsigned diag_note_public_dtor_;
  112. unsigned diag_note_protected_non_virtual_dtor_;
  113. std::unique_ptr<CheckIPCVisitor> ipc_visitor_;
  114. };
  115. } // namespace chrome_checker
  116. #endif // TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_