Skip to content
Permalink
4bc58bfaca
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
81 lines (62 sloc) 2.45 KB
#ifndef __CSE3150_TESTING_BUILDLIST_H
#define __CSE3150_TESTING_BUILDLIST_H
#include <algorithm>
#include <cppast/cpp_function.hpp>
#include <cppast/cpp_template.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/libclang_parser.hpp>
#include <cppast/visitor.hpp>
#include <optional.hpp>
tl::optional<const cppast::cpp_entity&> find_build_list_fn(const cppast::cpp_file& file) {
tl::optional<const cppast::cpp_entity&> result = tl::nullopt;
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info info) {
const auto is_function = e.kind() == cppast::cpp_entity_kind::function_t;
const auto correct_name = e.name() == "buildList";
if (is_function && correct_name) {
result = e;
}
// Stop visiting further nodes if we found the one we're looking for.
const auto should_keep_looking = result == tl::nullopt;
return should_keep_looking;
});
return result;
}
bool type_is_list_template(const cppast::cpp_type& type) {
const auto is_template_instantiation = type.kind() == cppast::cpp_type_kind::template_instantiation_t;
if (!is_template_instantiation) {
return false;
}
const auto& template_instantiation = static_cast<const cppast::cpp_template_instantiation_type&>(type);
const auto primary_template = template_instantiation.primary_template();
return
primary_template.name() == "list" ||
primary_template.name() == "std::list";
}
bool function_takes_list_reference(const cppast::cpp_function& fn) {
const auto list_param = std::find_if(
fn.parameters().begin(),
fn.parameters().end(),
[](const cppast::cpp_function_parameter& parameter)
{
const auto& type = parameter.type();
const auto is_reference = type.kind() == cppast::cpp_type_kind::reference_t;
if (!is_reference) {
return false;
}
const auto& reference = static_cast<const cppast::cpp_reference_type&>(type);
return type_is_list_template(reference.referee());
});
return list_param != fn.parameters().end();
}
bool function_returns_list_pointer(const cppast::cpp_function& fn) {
const cppast::cpp_type& return_type = fn.return_type();
const cppast::cpp_type_kind& kind = return_type.kind();
const auto is_pointer = kind == cppast::cpp_type_kind::pointer_t;
if (!is_pointer) {
return false;
}
const auto& pointer = static_cast<const cppast::cpp_pointer_type&>(return_type);
const auto& pointee = pointer.pointee();
return type_is_list_template(pointer.pointee());
}
#endif