-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathcpp_attribute.cpp
88 lines (74 loc) · 2.59 KB
/
cpp_attribute.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include <cppast/cpp_attribute.hpp>
#include <algorithm>
#include <cppast/cpp_entity.hpp>
using namespace cppast;
namespace
{
const char* get_attribute_name(cpp_attribute_kind kind) noexcept
{
switch (kind)
{
case cpp_attribute_kind::alignas_:
return "alignas";
case cpp_attribute_kind::carries_dependency:
return "carries_dependency";
case cpp_attribute_kind::deprecated:
return "deprecated";
case cpp_attribute_kind::fallthrough:
return "fallthrough";
case cpp_attribute_kind::maybe_unused:
return "maybe_unused";
case cpp_attribute_kind::nodiscard:
return "nodiscard";
case cpp_attribute_kind::noreturn:
return "noreturn";
case cpp_attribute_kind::unknown:
return "unknown";
}
return "<error>";
}
} // namespace
cpp_attribute::cpp_attribute(cpp_attribute_kind kind,
type_safe::optional<cpp_token_string> arguments)
: cpp_attribute(type_safe::nullopt, get_attribute_name(kind), std::move(arguments), false)
{
kind_ = kind;
}
type_safe::optional_ref<const cpp_attribute> cppast::has_attribute(
const cpp_attribute_list& attributes, const std::string& name)
{
auto iter
= std::find_if(attributes.begin(), attributes.end(), [&](const cpp_attribute& attribute) {
if (attribute.scope())
return attribute.scope().value() + "::" + attribute.name() == name;
else
return attribute.name() == name;
});
if (iter == attributes.end())
return nullptr;
else
return type_safe::ref(*iter);
}
type_safe::optional_ref<const cpp_attribute> cppast::has_attribute(
const cpp_attribute_list& attributes, cpp_attribute_kind kind)
{
auto iter
= std::find_if(attributes.begin(), attributes.end(),
[&](const cpp_attribute& attribute) { return attribute.kind() == kind; });
if (iter == attributes.end())
return nullptr;
else
return type_safe::ref(*iter);
}
type_safe::optional_ref<const cpp_attribute> cppast::has_attribute(const cpp_entity& e,
const std::string& name)
{
return has_attribute(e.attributes(), name);
}
type_safe::optional_ref<const cpp_attribute> cppast::has_attribute(const cpp_entity& e,
cpp_attribute_kind kind)
{
return has_attribute(e.attributes(), kind);
}