MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ run()

void MayaFlux::Reflect::Tool::FunctionCollector::run ( const clang::ast_matchers::MatchFinder::MatchResult & result)
override

Definition at line 97 of file FunctionCollector.cpp.

98{
99 const auto* variable = result.Nodes.getNodeAs<clang::VarDecl>(request_id);
100 if (variable == nullptr) {
101 return;
102 }
103
104 ++m_matched;
105
106 const clang::SourceManager& manager = *result.SourceManager;
107 const clang::SourceLocation location = manager.getExpansionLoc(variable->getBeginLoc());
108 if (!location.isValid() || manager.isInSystemHeader(location)) {
109 return;
110 }
111
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) + ": ";
115
116 if (!variable->getDeclContext()->getRedeclContext()->isFileContext()) {
117 m_errors.push_back(where + "MF_DESCRIBE_FUNCTION must appear at namespace scope");
118 return;
119 }
120
121 FunctionFinder finder;
122 if (variable->getInit() != nullptr) {
123 finder.TraverseStmt(const_cast<clang::Expr*>(variable->getInit()));
124 }
125 const clang::FunctionDecl* function = finder.function();
126 if (function == nullptr) {
127 m_errors.push_back(where + "request must name a function");
128 return;
129 }
130
131 FunctionRequest request;
132 request.name = function->getQualifiedNameAsString();
133 const std::string label = where + request.name + ": ";
134
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");
137 return;
138 }
139 if (function->getDescribedFunctionTemplate() != nullptr) {
140 m_errors.push_back(label + "a function template needs explicit template arguments, as &name<T>");
141 return;
142 }
143
144 bool defaulted = false;
145 unsigned number = 0;
146 for (const clang::ParmVarDecl* parameter : function->parameters()) {
147 const std::string name = parameter->getNameAsString();
148 if (name.empty()) {
149 m_errors.push_back(label + "parameter " + std::to_string(number) + " has no name");
150 return;
151 }
152 defaulted = defaulted || parameter->hasDefaultArg();
153 if (!defaulted) {
154 ++request.required;
155 }
156 request.parameters.push_back(name);
157 ++number;
158 }
159
160 request.expression = expression_of(function, *result.Context);
161 request.callee = callee_of(function, *result.Context);
162
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)) {
168 outermost = space;
169 }
170 }
171 if (outermost != nullptr && outermost->getRBraceLoc().isValid()) {
172 anchor = manager.getSpellingLineNumber(manager.getExpansionLoc(outermost->getRBraceLoc()));
173 }
174 request.line = anchor;
175
176 HeaderFunctions& entry = m_requests[header];
177 entry.header = header;
178 const auto existing = std::ranges::find(entry.functions, request.expression, &FunctionRequest::expression);
179 if (existing != entry.functions.end()) {
180 existing->line = std::max(existing->line, request.line);
181 return;
182 }
183 for (const auto& [other_header, other] : m_requests) {
184 if (other_header != header
185 && std::ranges::find(other.functions, request.expression, &FunctionRequest::expression) != other.functions.end()) {
186 m_errors.push_back(label + "already described in " + other_header.string());
187 return;
188 }
189 }
190 entry.functions.push_back(std::move(request));
191}
Vruta::SoundRoutine line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool restartable)
Creates a continuous interpolation generator between two values over time.
Definition Tasks.cpp:60

References MayaFlux::Reflect::Tool::FunctionRequest::callee, MayaFlux::Reflect::Tool::FunctionRequest::expression, MayaFlux::Reflect::Tool::HeaderFunctions::functions, MayaFlux::Reflect::Tool::HeaderFunctions::header, MayaFlux::Reflect::Tool::FunctionRequest::line, m_errors, m_matched, m_requests, MayaFlux::Reflect::Tool::FunctionRequest::name, MayaFlux::Reflect::Tool::FunctionRequest::parameters, and MayaFlux::Reflect::Tool::FunctionRequest::required.