NonMemMovableTemplateArgChecker.cpp (2323B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "NonMemMovableTemplateArgChecker.h" 6 #include "CustomMatchers.h" 7 8 void NonMemMovableTemplateArgChecker::registerMatchers( 9 MatchFinder *AstMatcher) { 10 // Handle non-mem-movable template specializations 11 AstMatcher->addMatcher( 12 classTemplateSpecializationDecl( 13 allOf(needsMemMovableTemplateArg(), 14 hasAnyTemplateArgument(refersToType(isNonMemMovable())))) 15 .bind("specialization"), 16 this); 17 } 18 19 void NonMemMovableTemplateArgChecker::check( 20 const MatchFinder::MatchResult &Result) { 21 const char *Error = 22 "Cannot instantiate %0 with non-memmovable template argument %1"; 23 const char *Note = "instantiation of %0 requested here"; 24 25 // Get the specialization 26 const ClassTemplateSpecializationDecl *Specialization = 27 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("specialization"); 28 SourceLocation RequestLoc = Specialization->getPointOfInstantiation(); 29 30 // Report an error for every template argument which is non-memmovable 31 const TemplateArgumentList &Args = 32 Specialization->getTemplateInstantiationArgs(); 33 for (unsigned i = 0; i < Args.size(); ++i) { 34 QualType ArgType = Args[i].getAsType(); 35 36 // It's impossible to correctly diagnose incomplete type, so let's be 37 // conservative. 38 if (auto *TD = ArgType->getAsTagDecl(); !TD->isCompleteDefinition()) { 39 continue; 40 } 41 42 if (NonMemMovable.hasEffectiveAnnotation(ArgType)) { 43 diag(Specialization->getLocation(), Error, DiagnosticIDs::Error) 44 << Specialization << ArgType; 45 // XXX It would be really nice if we could get the instantiation stack 46 // information 47 // from Sema such that we could print a full template instantiation stack, 48 // however, 49 // it seems as though that information is thrown out by the time we get 50 // here so we 51 // can only report one level of template specialization (which in many 52 // cases won't 53 // be useful) 54 diag(RequestLoc, Note, DiagnosticIDs::Note) << Specialization; 55 NonMemMovable.dumpAnnotationReason(*this, ArgType, RequestLoc); 56 } 57 } 58 }