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