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 "clang/Sema/Sema.h" |
10 #include "llvm/Support/raw_ostream.h" | 11 #include "llvm/Support/raw_ostream.h" |
11 | 12 |
12 using namespace clang; | 13 using namespace clang; |
13 | 14 |
14 namespace chrome_checker { | 15 namespace chrome_checker { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char kMethodRequiresOverride[] = | 19 const char kMethodRequiresOverride[] = |
19 "[chromium-style] Overriding method must be marked with 'override' or " | 20 "[chromium-style] Overriding method must be marked with 'override' or " |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return FixItHint::CreateRemoval(range); | 92 return FixItHint::CreateRemoval(range); |
92 } | 93 } |
93 | 94 |
94 bool IsPodOrTemplateType(const CXXRecordDecl& record) { | 95 bool IsPodOrTemplateType(const CXXRecordDecl& record) { |
95 return record.isPOD() || | 96 return record.isPOD() || |
96 record.getDescribedClassTemplate() || | 97 record.getDescribedClassTemplate() || |
97 record.getTemplateSpecializationKind() || | 98 record.getTemplateSpecializationKind() || |
98 record.isDependentType(); | 99 record.isDependentType(); |
99 } | 100 } |
100 | 101 |
| 102 // Use a local RAV implementation to simply collect all FunctionDecls marked for |
| 103 // late template parsing. This happens with the flag -fdelayed-template-parsing, |
| 104 // which is on by default in MSVC-compatible mode. |
| 105 std::set<FunctionDecl*> GetLateParsedFunctionDecls(TranslationUnitDecl* decl) { |
| 106 struct Visitor : public RecursiveASTVisitor<Visitor> { |
| 107 bool VisitFunctionDecl(FunctionDecl* function_decl) { |
| 108 if (function_decl->isLateTemplateParsed()) |
| 109 late_parsed_decls.insert(function_decl); |
| 110 return true; |
| 111 } |
| 112 |
| 113 std::set<FunctionDecl*> late_parsed_decls; |
| 114 } v; |
| 115 v.TraverseDecl(decl); |
| 116 return v.late_parsed_decls; |
| 117 } |
| 118 |
101 } // namespace | 119 } // namespace |
102 | 120 |
103 FindBadConstructsConsumer::FindBadConstructsConsumer(CompilerInstance& instance, | 121 FindBadConstructsConsumer::FindBadConstructsConsumer(CompilerInstance& instance, |
104 const Options& options) | 122 const Options& options) |
105 : ChromeClassTester(instance, options) { | 123 : ChromeClassTester(instance, options) { |
| 124 if (options.check_ipc) { |
| 125 ipc_visitor_.reset(new CheckIPCVisitor(instance)); |
| 126 } |
| 127 |
106 // Messages for virtual method specifiers. | 128 // Messages for virtual method specifiers. |
107 diag_method_requires_override_ = | 129 diag_method_requires_override_ = |
108 diagnostic().getCustomDiagID(getErrorLevel(), kMethodRequiresOverride); | 130 diagnostic().getCustomDiagID(getErrorLevel(), kMethodRequiresOverride); |
109 diag_redundant_virtual_specifier_ = | 131 diag_redundant_virtual_specifier_ = |
110 diagnostic().getCustomDiagID(getErrorLevel(), kRedundantVirtualSpecifier); | 132 diagnostic().getCustomDiagID(getErrorLevel(), kRedundantVirtualSpecifier); |
111 diag_base_method_virtual_and_final_ = | 133 diag_base_method_virtual_and_final_ = |
112 diagnostic().getCustomDiagID(getErrorLevel(), kBaseMethodVirtualAndFinal); | 134 diagnostic().getCustomDiagID(getErrorLevel(), kBaseMethodVirtualAndFinal); |
113 | 135 |
114 // Messages for destructors. | 136 // Messages for destructors. |
115 diag_no_explicit_dtor_ = | 137 diag_no_explicit_dtor_ = |
(...skipping 13 matching lines...) Expand all Loading... |
129 diag_note_inheritance_ = | 151 diag_note_inheritance_ = |
130 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNoteInheritance); | 152 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNoteInheritance); |
131 diag_note_implicit_dtor_ = | 153 diag_note_implicit_dtor_ = |
132 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNoteImplicitDtor); | 154 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNoteImplicitDtor); |
133 diag_note_public_dtor_ = | 155 diag_note_public_dtor_ = |
134 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNotePublicDtor); | 156 diagnostic().getCustomDiagID(DiagnosticsEngine::Note, kNotePublicDtor); |
135 diag_note_protected_non_virtual_dtor_ = diagnostic().getCustomDiagID( | 157 diag_note_protected_non_virtual_dtor_ = diagnostic().getCustomDiagID( |
136 DiagnosticsEngine::Note, kNoteProtectedNonVirtualDtor); | 158 DiagnosticsEngine::Note, kNoteProtectedNonVirtualDtor); |
137 } | 159 } |
138 | 160 |
| 161 void FindBadConstructsConsumer::Traverse(ASTContext& context) { |
| 162 if (ipc_visitor_) { |
| 163 ipc_visitor_->set_context(&context); |
| 164 ParseFunctionTemplates(context.getTranslationUnitDecl()); |
| 165 } |
| 166 RecursiveASTVisitor::TraverseDecl(context.getTranslationUnitDecl()); |
| 167 if (ipc_visitor_) ipc_visitor_->set_context(nullptr); |
| 168 } |
| 169 |
| 170 bool FindBadConstructsConsumer::TraverseDecl(Decl* decl) { |
| 171 if (ipc_visitor_) ipc_visitor_->BeginDecl(decl); |
| 172 bool result = RecursiveASTVisitor::TraverseDecl(decl); |
| 173 if (ipc_visitor_) ipc_visitor_->EndDecl(); |
| 174 return result; |
| 175 } |
| 176 |
139 bool FindBadConstructsConsumer::VisitDecl(clang::Decl* decl) { | 177 bool FindBadConstructsConsumer::VisitDecl(clang::Decl* decl) { |
140 clang::TagDecl* tag_decl = dyn_cast<clang::TagDecl>(decl); | 178 clang::TagDecl* tag_decl = dyn_cast<clang::TagDecl>(decl); |
141 if (tag_decl && tag_decl->isCompleteDefinition()) | 179 if (tag_decl && tag_decl->isCompleteDefinition()) |
142 CheckTag(tag_decl); | 180 CheckTag(tag_decl); |
143 return true; | 181 return true; |
144 } | 182 } |
145 | 183 |
| 184 bool FindBadConstructsConsumer::VisitTemplateSpecializationType( |
| 185 TemplateSpecializationType* spec) { |
| 186 if (ipc_visitor_) ipc_visitor_->VisitTemplateSpecializationType(spec); |
| 187 return true; |
| 188 } |
| 189 |
| 190 bool FindBadConstructsConsumer::VisitCallExpr(CallExpr* call_expr) { |
| 191 if (ipc_visitor_) ipc_visitor_->VisitCallExpr(call_expr); |
| 192 return true; |
| 193 } |
| 194 |
146 void FindBadConstructsConsumer::CheckChromeClass(SourceLocation record_location, | 195 void FindBadConstructsConsumer::CheckChromeClass(SourceLocation record_location, |
147 CXXRecordDecl* record) { | 196 CXXRecordDecl* record) { |
148 // By default, the clang checker doesn't check some types (templates, etc). | 197 // By default, the clang checker doesn't check some types (templates, etc). |
149 // That was only a mistake; once Chromium code passes these checks, we should | 198 // That was only a mistake; once Chromium code passes these checks, we should |
150 // remove the "check-templates" option and remove this code. | 199 // remove the "check-templates" option and remove this code. |
151 // See crbug.com/441916 | 200 // See crbug.com/441916 |
152 if (!options_.check_templates && IsPodOrTemplateType(*record)) | 201 if (!options_.check_templates && IsPodOrTemplateType(*record)) |
153 return; | 202 return; |
154 | 203 |
155 bool implementation_file = InImplementationFile(record_location); | 204 bool implementation_file = InImplementationFile(record_location); |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 // If we've already seen a WeakPtrFactory<OwningType> and this param is not | 918 // If we've already seen a WeakPtrFactory<OwningType> and this param is not |
870 // one of those, it means there is at least one member after a factory. | 919 // one of those, it means there is at least one member after a factory. |
871 if (weak_ptr_factory_location.isValid() && | 920 if (weak_ptr_factory_location.isValid() && |
872 !param_is_weak_ptr_factory_to_self) { | 921 !param_is_weak_ptr_factory_to_self) { |
873 diagnostic().Report(weak_ptr_factory_location, | 922 diagnostic().Report(weak_ptr_factory_location, |
874 diag_weak_ptr_factory_order_); | 923 diag_weak_ptr_factory_order_); |
875 } | 924 } |
876 } | 925 } |
877 } | 926 } |
878 | 927 |
| 928 void FindBadConstructsConsumer::ParseFunctionTemplates( |
| 929 TranslationUnitDecl* decl) { |
| 930 if (!instance().getLangOpts().DelayedTemplateParsing) |
| 931 return; // Nothing to do. |
| 932 |
| 933 std::set<FunctionDecl*> late_parsed_decls = GetLateParsedFunctionDecls(decl); |
| 934 clang::Sema& sema = instance().getSema(); |
| 935 |
| 936 for (const FunctionDecl* fd : late_parsed_decls) { |
| 937 assert(fd->isLateTemplateParsed()); |
| 938 |
| 939 // Force parsing and AST building of the yet-uninstantiated function |
| 940 // template trace method bodies. |
| 941 clang::LateParsedTemplate* lpt = sema.LateParsedTemplateMap[fd]; |
| 942 sema.LateTemplateParser(sema.OpaqueParser, *lpt); |
| 943 } |
| 944 } |
| 945 |
879 } // namespace chrome_checker | 946 } // namespace chrome_checker |
OLD | NEW |