7 constexpr const char* request_id =
"request";
8 constexpr const char* request_pattern =
"mf_signature_request_";
10 std::filesystem::path real_path(llvm::StringRef file)
12 llvm::SmallString<256> resolved;
13 if (llvm::sys::fs::real_path(file, resolved)) {
14 return { file.str() };
16 return { resolved.str().str() };
20 class FunctionFinder :
public clang::RecursiveASTVisitor<FunctionFinder> {
22 bool VisitDeclRefExpr(clang::DeclRefExpr* reference)
24 if (
const auto* function = llvm::dyn_cast<clang::FunctionDecl>(reference->getDecl())) {
25 m_function = function;
31 [[nodiscard]]
const clang::FunctionDecl* function() const noexcept {
return m_function; }
34 const clang::FunctionDecl* m_function {};
37 bool overloaded(
const clang::FunctionDecl* function)
39 const clang::DeclContext* context = function->getDeclContext()->getRedeclContext();
40 std::set<const clang::Decl*> entities;
41 for (
const clang::NamedDecl* found : context->lookup(function->getDeclName())) {
42 if (llvm::isa<clang::FunctionDecl>(found) || llvm::isa<clang::FunctionTemplateDecl>(found)) {
43 entities.insert(found->getCanonicalDecl());
46 return entities.size() > 1;
49 clang::PrintingPolicy qualified_policy(
const clang::ASTContext& context)
51 clang::PrintingPolicy policy = context.getPrintingPolicy();
52 policy.SuppressScope =
false;
53 policy.FullyQualifiedName =
true;
58 std::string callee_of(
const clang::FunctionDecl* function,
const clang::ASTContext& context)
60 const std::string name =
"::" + function->getQualifiedNameAsString();
61 if (
const clang::TemplateArgumentList* arguments = function->getTemplateSpecializationArgs()) {
63 llvm::raw_string_ostream stream(text);
64 clang::printTemplateArgumentList(stream, arguments->asArray(), qualified_policy(context));
74 std::string expression_of(
const clang::FunctionDecl* function,
const clang::ASTContext& context)
76 const std::string name =
"::" + function->getQualifiedNameAsString();
78 if (function->getTemplateSpecializationArgs() !=
nullptr) {
79 return "&" + callee_of(function, context);
81 if (!overloaded(function)) {
84 const clang::QualType pointer = clang::TypeName::getFullyQualifiedType(
85 context.getPointerType(function->getType()), context,
true);
86 return "static_cast<" + pointer.getAsString(context.getPrintingPolicy()) +
">(&" + name +
")";
93 using namespace clang::ast_matchers;
94 finder.addMatcher(varDecl(matchesName(request_pattern)).bind(request_id),
this);
99 const auto* variable = result.Nodes.getNodeAs<clang::VarDecl>(request_id);
100 if (variable ==
nullptr) {
106 const clang::SourceManager& manager = *result.SourceManager;
107 const clang::SourceLocation location = manager.getExpansionLoc(variable->getBeginLoc());
108 if (!location.isValid() || manager.isInSystemHeader(location)) {
112 const std::filesystem::path header = real_path(manager.getFilename(location));
113 const unsigned line = manager.getSpellingLineNumber(location);
114 const std::string where = header.string() +
":" + std::to_string(line) +
": ";
116 if (!variable->getDeclContext()->getRedeclContext()->isFileContext()) {
117 m_errors.push_back(where +
"MF_DESCRIBE_FUNCTION must appear at namespace scope");
121 FunctionFinder finder;
122 if (variable->getInit() !=
nullptr) {
123 finder.TraverseStmt(
const_cast<clang::Expr*
>(variable->getInit()));
125 const clang::FunctionDecl* function = finder.function();
126 if (function ==
nullptr) {
127 m_errors.push_back(where +
"request must name a function");
132 request.
name = function->getQualifiedNameAsString();
133 const std::string label = where + request.
name +
": ";
135 if (
const auto* method = llvm::dyn_cast<clang::CXXMethodDecl>(function); method !=
nullptr && !method->isStatic()) {
136 m_errors.push_back(label +
"member functions are unsupported");
139 if (function->getDescribedFunctionTemplate() !=
nullptr) {
140 m_errors.push_back(label +
"a function template needs explicit template arguments, as &name<T>");
144 bool defaulted =
false;
146 for (
const clang::ParmVarDecl* parameter : function->parameters()) {
147 const std::string name = parameter->getNameAsString();
149 m_errors.push_back(label +
"parameter " + std::to_string(number) +
" has no name");
152 defaulted = defaulted || parameter->hasDefaultArg();
160 request.
expression = expression_of(function, *result.Context);
161 request.
callee = callee_of(function, *result.Context);
163 unsigned anchor = line;
164 const clang::NamespaceDecl* outermost =
nullptr;
165 for (
const clang::DeclContext* context = variable->getLexicalDeclContext();
166 context !=
nullptr && !context->isTranslationUnit(); context = context->getLexicalParent()) {
167 if (
const auto* space = llvm::dyn_cast<clang::NamespaceDecl>(context)) {
171 if (outermost !=
nullptr && outermost->getRBraceLoc().isValid()) {
172 anchor = manager.getSpellingLineNumber(manager.getExpansionLoc(outermost->getRBraceLoc()));
174 request.
line = anchor;
183 for (
const auto& [other_header, other] :
m_requests) {
184 if (other_header != header
186 m_errors.push_back(label +
"already described in " + other_header.string());
190 entry.
functions.push_back(std::move(request));
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...