2016-12-18 00:35:53 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "NonParamInsideFunctionDeclChecker.h"
|
|
|
|
#include "CustomMatchers.h"
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
class NonParamAnnotation : public CustomTypeAnnotation {
|
2017-04-27 19:44:49 +03:00
|
|
|
public:
|
2018-09-18 16:03:33 +03:00
|
|
|
NonParamAnnotation() : CustomTypeAnnotation(moz_non_param, "non-param"){};
|
2017-04-27 19:44:49 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Adding alignas(_) on a struct implicitly marks it as MOZ_NON_PARAM, due to
|
|
|
|
// MSVC limitations which prevent passing explcitly aligned types by value as
|
|
|
|
// parameters. This overload of hasFakeAnnotation injects fake MOZ_NON_PARAM
|
|
|
|
// annotations onto these types.
|
2017-04-27 19:44:55 +03:00
|
|
|
std::string getImplicitReason(const TagDecl *D) const override {
|
2017-04-27 19:44:49 +03:00
|
|
|
// Check if the decl itself has an AlignedAttr on it.
|
|
|
|
for (const Attr *A : D->attrs()) {
|
|
|
|
if (isa<AlignedAttr>(A)) {
|
2017-04-27 19:44:55 +03:00
|
|
|
return "it has an alignas(_) annotation";
|
2017-04-27 19:44:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the decl's fields have an AlignedAttr on them.
|
|
|
|
if (auto RD = dyn_cast<RecordDecl>(D)) {
|
|
|
|
for (auto F : RD->fields()) {
|
|
|
|
for (auto A : F->attrs()) {
|
|
|
|
if (isa<AlignedAttr>(A)) {
|
2017-10-20 20:11:50 +03:00
|
|
|
return ("member '" + F->getName() +
|
|
|
|
"' has an alignas(_) annotation")
|
|
|
|
.str();
|
2017-04-27 19:44:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to check the types of fields, as the CustomTypeAnnotation
|
|
|
|
// infrastructure will handle that for us.
|
2017-04-27 19:44:55 +03:00
|
|
|
return "";
|
2017-04-27 19:44:49 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
NonParamAnnotation NonParam;
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void NonParamInsideFunctionDeclChecker::registerMatchers(
|
|
|
|
MatchFinder *AstMatcher) {
|
2016-12-18 05:14:37 +03:00
|
|
|
AstMatcher->addMatcher(
|
2017-10-20 20:11:50 +03:00
|
|
|
functionDecl(
|
|
|
|
anyOf(allOf(isDefinition(),
|
|
|
|
hasAncestor(
|
|
|
|
classTemplateSpecializationDecl().bind("spec"))),
|
|
|
|
isDefinition()))
|
2016-12-18 00:35:53 +03:00
|
|
|
.bind("func"),
|
|
|
|
this);
|
2017-10-20 20:11:50 +03:00
|
|
|
AstMatcher->addMatcher(lambdaExpr().bind("lambda"), this);
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
|
|
|
|
2016-12-18 05:14:37 +03:00
|
|
|
void NonParamInsideFunctionDeclChecker::check(
|
2016-12-18 00:35:53 +03:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
2017-10-20 20:11:50 +03:00
|
|
|
static DenseSet<const FunctionDecl *> CheckedFunctionDecls;
|
2016-12-18 00:35:53 +03:00
|
|
|
|
|
|
|
const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>("func");
|
|
|
|
if (!func) {
|
|
|
|
const LambdaExpr *lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
|
|
|
|
if (lambda) {
|
|
|
|
func = lambda->getCallOperator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func->isDeleted()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:44:49 +03:00
|
|
|
// We need to skip decls which have these types as parameters in system
|
|
|
|
// headers, because presumably those headers act like an assertion that the
|
|
|
|
// alignment will be preserved in that situation.
|
|
|
|
if (getDeclarationNamespace(func) == "std") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inThirdPartyPath(func)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-18 00:35:53 +03:00
|
|
|
// Don't report errors on the same declarations more than once.
|
|
|
|
if (CheckedFunctionDecls.count(func)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckedFunctionDecls.insert(func);
|
|
|
|
|
|
|
|
const ClassTemplateSpecializationDecl *Spec =
|
|
|
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("spec");
|
|
|
|
|
|
|
|
for (ParmVarDecl *p : func->parameters()) {
|
|
|
|
QualType T = p->getType().withoutLocalFastQualifiers();
|
|
|
|
if (NonParam.hasEffectiveAnnotation(T)) {
|
2016-12-18 05:14:37 +03:00
|
|
|
diag(p->getLocation(), "Type %0 must not be used as parameter",
|
2017-10-20 20:11:50 +03:00
|
|
|
DiagnosticIDs::Error)
|
|
|
|
<< T;
|
|
|
|
diag(p->getLocation(),
|
|
|
|
"Please consider passing a const reference instead",
|
2016-12-18 05:14:37 +03:00
|
|
|
DiagnosticIDs::Note);
|
2016-12-18 00:35:53 +03:00
|
|
|
|
|
|
|
if (Spec) {
|
2016-12-18 05:14:37 +03:00
|
|
|
diag(Spec->getPointOfInstantiation(),
|
2017-10-20 20:11:50 +03:00
|
|
|
"The bad argument was passed to %0 here", DiagnosticIDs::Note)
|
|
|
|
<< Spec->getSpecializedTemplate();
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
2017-04-27 19:44:55 +03:00
|
|
|
|
|
|
|
NonParam.dumpAnnotationReason(*this, T, p->getLocation());
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|