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.
5 // This file defines a bunch of recurring problems in the Chromium C++ code.
7 // Checks that are implemented:
8 // - Constructors/Destructors should not be inlined if they are of a complex
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
17 // - Enum types with a xxxx_LAST or xxxxLast const actually have that constant
18 // have the maximal value for that type.
20 #ifndef TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
21 #define TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_
23 #include "clang/AST/AST.h"
24 #include "clang/AST/ASTConsumer.h"
25 #include "clang/AST/Attr.h"
26 #include "clang/AST/CXXInheritance.h"
27 #include "clang/AST/RecursiveASTVisitor.h"
28 #include "clang/AST/TypeLoc.h"
29 #include "clang/Basic/SourceManager.h"
30 #include "clang/Basic/SourceLocation.h"
32 #include "ChromeClassTester.h"
34 #include "SuppressibleDiagnosticBuilder.h"
36 namespace chrome_checker
{
38 // Searches for constructs that we know we don't want in the Chromium code base.
39 class FindBadConstructsConsumer
40 : public clang::RecursiveASTVisitor
<FindBadConstructsConsumer
>,
41 public ChromeClassTester
{
43 FindBadConstructsConsumer(clang::CompilerInstance
& instance
,
44 const Options
& options
);
46 // RecursiveASTVisitor:
47 bool VisitDecl(clang::Decl
* decl
);
49 // ChromeClassTester overrides:
50 void CheckChromeClass(clang::SourceLocation record_location
,
51 clang::CXXRecordDecl
* record
) override
;
52 void CheckChromeEnum(clang::SourceLocation enum_location
,
53 clang::EnumDecl
* enum_decl
) override
;
56 // The type of problematic ref-counting pattern that was encountered.
57 enum RefcountIssue
{ None
, ImplicitDestructor
, PublicDestructor
};
59 void CheckCtorDtorWeight(clang::SourceLocation record_location
,
60 clang::CXXRecordDecl
* record
);
62 bool InTestingNamespace(const clang::Decl
* record
);
63 bool IsMethodInBannedOrTestingNamespace(const clang::CXXMethodDecl
* method
);
65 // Returns a diagnostic builder that only emits the diagnostic if the spelling
66 // location (the actual characters that make up the token) is not in an
67 // ignored file. This is useful for situations where the token might originate
68 // from a macro in a system header: warning isn't useful, since system headers
69 // generally can't be easily updated.
70 SuppressibleDiagnosticBuilder
ReportIfSpellingLocNotIgnored(
71 clang::SourceLocation loc
,
72 unsigned diagnostic_id
);
74 void CheckVirtualMethods(clang::SourceLocation record_location
,
75 clang::CXXRecordDecl
* record
,
76 bool warn_on_inline_bodies
);
77 void CheckVirtualSpecifiers(const clang::CXXMethodDecl
* method
);
78 void CheckVirtualBodies(const clang::CXXMethodDecl
* method
);
80 void CountType(const clang::Type
* type
,
82 int* non_trivial_member
,
83 int* templated_non_trivial_member
);
85 static RefcountIssue
CheckRecordForRefcountIssue(
86 const clang::CXXRecordDecl
* record
,
87 clang::SourceLocation
& loc
);
88 bool IsRefCounted(const clang::CXXBaseSpecifier
* base
,
89 clang::CXXBasePath
& path
);
90 static bool HasPublicDtorCallback(const clang::CXXBaseSpecifier
* base
,
91 clang::CXXBasePath
& path
,
93 void PrintInheritanceChain(const clang::CXXBasePath
& path
);
94 unsigned DiagnosticForIssue(RefcountIssue issue
);
95 void CheckRefCountedDtors(clang::SourceLocation record_location
,
96 clang::CXXRecordDecl
* record
);
98 void CheckWeakPtrFactoryMembers(clang::SourceLocation record_location
,
99 clang::CXXRecordDecl
* record
);
101 unsigned diag_method_requires_override_
;
102 unsigned diag_redundant_virtual_specifier_
;
103 unsigned diag_base_method_virtual_and_final_
;
104 unsigned diag_no_explicit_dtor_
;
105 unsigned diag_public_dtor_
;
106 unsigned diag_protected_non_virtual_dtor_
;
107 unsigned diag_weak_ptr_factory_order_
;
108 unsigned diag_bad_enum_last_value_
;
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_
;
115 } // namespace chrome_checker
117 #endif // TOOLS_CLANG_PLUGINS_FINDBADCONSTRUCTSCONSUMER_H_