blob: ee32366b9edf14dc6bb765f9f564d2e40eb4f660 [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091// Copyright 2012 The Chromium Authors
[email protected]28cf60b2014-06-19 21:14:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file defines a bunch of recurring problems in the Chromium C++ code.
6//
7// Checks that are implemented:
8// - Constructors/Destructors should not be inlined if they are of a complex
9// class type.
10// - Missing "virtual" keywords on methods that should be virtual.
11// - Non-annotated overriding virtual methods.
12// - Virtual methods with nonempty implementations in their headers.
13// - Classes that derive from base::RefCounted / base::RefCountedThreadSafe
14// should have protected or private destructors.
15// - WeakPtrFactory members that refer to their outer class should be the last
16// member.
17// - Enum types with a xxxx_LAST or xxxxLast const actually have that constant
18// have the maximal value for that type.
19
dcheng80484b1d2015-05-09 18:39:4320#ifndef TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
21#define TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
22
dskiba2bc89462016-04-06 15:51:0623#include <memory>
24
[email protected]28cf60b2014-06-19 21:14:3725#include "clang/AST/AST.h"
26#include "clang/AST/ASTConsumer.h"
27#include "clang/AST/Attr.h"
28#include "clang/AST/CXXInheritance.h"
dcheng80f54dc2015-01-07 19:13:4929#include "clang/AST/RecursiveASTVisitor.h"
[email protected]28cf60b2014-06-19 21:14:3730#include "clang/AST/TypeLoc.h"
31#include "clang/Basic/SourceManager.h"
dcheng80484b1d2015-05-09 18:39:4332#include "clang/Basic/SourceLocation.h"
[email protected]28cf60b2014-06-19 21:14:3733
Xianzhu Wang86b11462022-09-24 22:00:1234#include "BlinkDataMemberTypeChecker.h"
dskiba2bc89462016-04-06 15:51:0635#include "CheckIPCVisitor.h"
Yuki Yamadaead8ffe2021-10-21 06:09:3136#include "CheckLayoutObjectMethodsVisitor.h"
[email protected]28cf60b2014-06-19 21:14:3737#include "ChromeClassTester.h"
38#include "Options.h"
Stefan Zagere9734a5c2023-01-20 22:44:4639#include "StackAllocatedChecker.h"
dcheng80484b1d2015-05-09 18:39:4340#include "SuppressibleDiagnosticBuilder.h"
[email protected]28cf60b2014-06-19 21:14:3741
42namespace chrome_checker {
43
44// Searches for constructs that we know we don't want in the Chromium code base.
dcheng80f54dc2015-01-07 19:13:4945class FindBadConstructsConsumer
46 : public clang::RecursiveASTVisitor<FindBadConstructsConsumer>,
47 public ChromeClassTester {
[email protected]28cf60b2014-06-19 21:14:3748 public:
49 FindBadConstructsConsumer(clang::CompilerInstance& instance,
50 const Options& options);
51
dskiba2bc89462016-04-06 15:51:0652 void Traverse(clang::ASTContext& context);
53
dcheng80f54dc2015-01-07 19:13:4954 // RecursiveASTVisitor:
dskiba2bc89462016-04-06 15:51:0655 bool TraverseDecl(clang::Decl* decl);
danakjfffdbb932024-06-07 17:11:4156 bool VisitCXXConstructExpr(clang::CXXConstructExpr* expr);
Stefan Zagere9734a5c2023-01-20 22:44:4657 bool VisitCXXRecordDecl(clang::CXXRecordDecl* cxx_record_decl);
Daniel Chengd8358f642018-03-30 18:36:5858 bool VisitEnumDecl(clang::EnumDecl* enum_decl);
vmpstra48d78b2016-06-09 20:03:3459 bool VisitTagDecl(clang::TagDecl* tag_decl);
60 bool VisitVarDecl(clang::VarDecl* var_decl);
dskiba2bc89462016-04-06 15:51:0661 bool VisitTemplateSpecializationType(clang::TemplateSpecializationType* spec);
62 bool VisitCallExpr(clang::CallExpr* call_expr);
dcheng80f54dc2015-01-07 19:13:4963
[email protected]28cf60b2014-06-19 21:14:3764 // ChromeClassTester overrides:
dcheng6aff6f12017-04-11 20:26:1265 void CheckChromeClass(LocationType location_type,
66 clang::SourceLocation record_location,
dcheng993e04f2014-09-28 19:27:5467 clang::CXXRecordDecl* record) override;
[email protected]28cf60b2014-06-19 21:14:3768
69 private:
70 // The type of problematic ref-counting pattern that was encountered.
71 enum RefcountIssue { None, ImplicitDestructor, PublicDestructor };
72
73 void CheckCtorDtorWeight(clang::SourceLocation record_location,
74 clang::CXXRecordDecl* record);
75
dcheng80484b1d2015-05-09 18:39:4376 // Returns a diagnostic builder that only emits the diagnostic if the spelling
77 // location (the actual characters that make up the token) is not in an
78 // ignored file. This is useful for situations where the token might originate
79 // from a macro in a system header: warning isn't useful, since system headers
80 // generally can't be easily updated.
81 SuppressibleDiagnosticBuilder ReportIfSpellingLocNotIgnored(
82 clang::SourceLocation loc,
83 unsigned diagnostic_id);
84
[email protected]28cf60b2014-06-19 21:14:3785 void CheckVirtualMethods(clang::SourceLocation record_location,
86 clang::CXXRecordDecl* record,
87 bool warn_on_inline_bodies);
dcheng993e04f2014-09-28 19:27:5488 void CheckVirtualSpecifiers(const clang::CXXMethodDecl* method);
89 void CheckVirtualBodies(const clang::CXXMethodDecl* method);
[email protected]28cf60b2014-06-19 21:14:3790
Will Cassella64da6c52022-01-06 18:13:5791 enum class TypeClassification {
92 kTrivial,
93 kNonTrivial,
94 kTrivialTemplate,
95 kNonTrivialTemplate,
96 kNonTrivialExternTemplate
97 };
98 TypeClassification ClassifyType(const clang::Type* type);
99
[email protected]28cf60b2014-06-19 21:14:37100 static RefcountIssue CheckRecordForRefcountIssue(
101 const clang::CXXRecordDecl* record,
102 clang::SourceLocation& loc);
Nico Weber8805f842015-07-25 18:41:04103 bool IsRefCounted(const clang::CXXBaseSpecifier* base,
104 clang::CXXBasePath& path);
[email protected]28cf60b2014-06-19 21:14:37105 static bool HasPublicDtorCallback(const clang::CXXBaseSpecifier* base,
106 clang::CXXBasePath& path,
107 void* user_data);
108 void PrintInheritanceChain(const clang::CXXBasePath& path);
109 unsigned DiagnosticForIssue(RefcountIssue issue);
110 void CheckRefCountedDtors(clang::SourceLocation record_location,
111 clang::CXXRecordDecl* record);
112
113 void CheckWeakPtrFactoryMembers(clang::SourceLocation record_location,
114 clang::CXXRecordDecl* record);
Daniel Chengd8358f642018-03-30 18:36:58115 void CheckEnumMaxValue(clang::EnumDecl* decl);
danakj3296e9e12024-02-05 21:33:59116 void CheckDeducedAutoPointer(clang::VarDecl* decl);
danakjfffdbb932024-06-07 17:11:41117 void CheckConstructingSpanFromStringLiteral(
118 clang::CXXConstructorDecl* ctor_decl,
119 llvm::ArrayRef<const clang::Expr*> args,
120 clang::SourceLocation loc);
[email protected]28cf60b2014-06-19 21:14:37121
dskiba2bc89462016-04-06 15:51:06122 void ParseFunctionTemplates(clang::TranslationUnitDecl* decl);
123
[email protected]28cf60b2014-06-19 21:14:37124 unsigned diag_method_requires_override_;
dcheng993e04f2014-09-28 19:27:54125 unsigned diag_redundant_virtual_specifier_;
Daniel Cheng1d514492017-10-24 17:49:53126 unsigned diag_will_be_redundant_virtual_specifier_;
dcheng993e04f2014-09-28 19:27:54127 unsigned diag_base_method_virtual_and_final_;
Daniel Cheng2fb72ec2017-09-15 22:15:56128 unsigned diag_virtual_with_inline_body_;
129 unsigned diag_no_explicit_ctor_;
130 unsigned diag_no_explicit_copy_ctor_;
131 unsigned diag_inline_complex_ctor_;
[email protected]28cf60b2014-06-19 21:14:37132 unsigned diag_no_explicit_dtor_;
Daniel Cheng2fb72ec2017-09-15 22:15:56133 unsigned diag_inline_complex_dtor_;
134 unsigned diag_refcounted_needs_explicit_dtor_;
135 unsigned diag_refcounted_with_public_dtor_;
136 unsigned diag_refcounted_with_protected_non_virtual_dtor_;
[email protected]28cf60b2014-06-19 21:14:37137 unsigned diag_weak_ptr_factory_order_;
Daniel Chengd8358f642018-03-30 18:36:58138 unsigned diag_bad_enum_max_value_;
139 unsigned diag_enum_max_value_unique_;
vmpstra48d78b2016-06-09 20:03:34140 unsigned diag_auto_deduced_to_a_pointer_type_;
[email protected]28cf60b2014-06-19 21:14:37141 unsigned diag_note_inheritance_;
142 unsigned diag_note_implicit_dtor_;
143 unsigned diag_note_public_dtor_;
144 unsigned diag_note_protected_non_virtual_dtor_;
danakjfffdbb932024-06-07 17:11:41145 unsigned diag_span_from_string_literal_;
146 unsigned diag_note_span_from_string_literal1_;
dskiba2bc89462016-04-06 15:51:06147
Xianzhu Wang86b11462022-09-24 22:00:12148 std::unique_ptr<BlinkDataMemberTypeChecker> blink_data_member_type_checker_;
dskiba2bc89462016-04-06 15:51:06149 std::unique_ptr<CheckIPCVisitor> ipc_visitor_;
Yuki Yamadaead8ffe2021-10-21 06:09:31150 std::unique_ptr<CheckLayoutObjectMethodsVisitor> layout_visitor_;
Stefan Zagere9734a5c2023-01-20 22:44:46151 std::unique_ptr<StackAllocatedChecker> stack_allocated_checker_;
[email protected]28cf60b2014-06-19 21:14:37152};
153
154} // namespace chrome_checker
dcheng80484b1d2015-05-09 18:39:43155
156#endif // TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_