From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- build/clang-plugin/NonTrivialTypeInFfiChecker.cpp | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 build/clang-plugin/NonTrivialTypeInFfiChecker.cpp (limited to 'build/clang-plugin/NonTrivialTypeInFfiChecker.cpp') diff --git a/build/clang-plugin/NonTrivialTypeInFfiChecker.cpp b/build/clang-plugin/NonTrivialTypeInFfiChecker.cpp new file mode 100644 index 0000000000..f482fb131f --- /dev/null +++ b/build/clang-plugin/NonTrivialTypeInFfiChecker.cpp @@ -0,0 +1,56 @@ +/* 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 "NonTrivialTypeInFfiChecker.h" +#include "CustomMatchers.h" + +void NonTrivialTypeInFfiChecker::registerMatchers(MatchFinder *AstMatcher) { + AstMatcher->addMatcher(functionDecl(isExternC()).bind("func"), this); +} + +void NonTrivialTypeInFfiChecker::check(const MatchFinder::MatchResult &Result) { + static DenseSet CheckedFunctionDecls; + + const FunctionDecl *func = Result.Nodes.getNodeAs("func"); + // Don't report errors on the same declarations more than once. + if (!CheckedFunctionDecls.insert(func).second) { + return; + } + + if (inThirdPartyPath(func)) { + return; + } + + auto NoteFor = [](const QualType &T) -> std::string { + std::string s = "Please consider using a pointer or reference"; + if (T->getAs()) { + s += ", or explicitly instantiating the template"; + } + return s + " instead"; + }; + + for (ParmVarDecl *p : func->parameters()) { + QualType T = p->getType().getUnqualifiedType(); + if (!T->isVoidType() && !T->isReferenceType() && + !T.isTriviallyCopyableType(*Result.Context)) { + diag(p->getLocation(), + "Type %0 must not be used as parameter to extern " + "\"C\" function", + DiagnosticIDs::Error) + << T; + diag(p->getLocation(), NoteFor(T), DiagnosticIDs::Note); + } + } + + QualType T = func->getReturnType().getUnqualifiedType(); + if (!T->isVoidType() && !T->isReferenceType() && + !T.isTriviallyCopyableType(*Result.Context)) { + diag(func->getLocation(), + "Type %0 must not be used as return type of " + "extern \"C\" function", + DiagnosticIDs::Error) + << T; + diag(func->getLocation(), NoteFor(T), DiagnosticIDs::Note); + } +} -- cgit v1.2.3