forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_attribute.hpp
More file actions
121 lines (99 loc) · 4 KB
/
Copy pathcpp_attribute.hpp
File metadata and controls
121 lines (99 loc) · 4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_CPP_ATTRIBUTE_HPP_INCLUDED
#define CPPAST_CPP_ATTRIBUTE_HPP_INCLUDED
#include <string>
#include <vector>
#include <type_safe/optional.hpp>
#include <type_safe/optional_ref.hpp>
#include <cppast/cpp_token.hpp>
namespace cppast
{
/// The known C++ attributes.
enum class cpp_attribute_kind
{
// update get_attribute_kind() in tokenizer, when updating this
alignas_,
carries_dependency,
deprecated,
fallthrough,
maybe_unused,
nodiscard,
noreturn,
unknown, //< An unknown attribute.
};
/// A C++ attribute, including `alignas` specifiers.
///
/// It consists of a name, an optional namespace scope and optional arguments.
/// The scope is just a single identifier and doesn't include the `::` and can be given explicitly
/// or via using. The arguments are as specified in the source code but do not include the
/// outer-most `(` and `)`. It can also be variadic or not.
///
/// An attribute can be known or unknown.
/// A known attribute will have the [cppast::cpp_attribute_kind]() set properly.
class cpp_attribute
{
public:
/// \effects Creates a known attribute, potentially with arguments.
cpp_attribute(cpp_attribute_kind kind, type_safe::optional<cpp_token_string> arguments);
/// \effects Creates an unknown attribute giving it the optional scope, names, arguments and
/// whether it is variadic.
cpp_attribute(type_safe::optional<std::string> scope, std::string name,
type_safe::optional<cpp_token_string> arguments, bool is_variadic)
: scope_(std::move(scope)), arguments_(std::move(arguments)), name_(std::move(name)),
variadic_(is_variadic)
{}
/// \returns The kind of attribute, if it is known.
const cpp_attribute_kind& kind() const noexcept
{
return kind_;
}
/// \returns The name of the attribute.
const std::string& name() const noexcept
{
return name_;
}
/// \returns The scope of the attribute, if there is any.
const type_safe::optional<std::string>& scope() const noexcept
{
return scope_;
}
/// \returns Whether or not the attribute is variadic.
bool is_variadic() const noexcept
{
return variadic_;
}
/// \returns The arguments of the attribute, if they are any.
const type_safe::optional<cpp_token_string>& arguments() const noexcept
{
return arguments_;
}
private:
type_safe::optional<std::string> scope_;
type_safe::optional<cpp_token_string> arguments_;
std::string name_;
cpp_attribute_kind kind_ = cpp_attribute_kind::unknown;
bool variadic_;
};
/// A list of C++ attributes.
using cpp_attribute_list = std::vector<cpp_attribute>;
/// Checks whether an attribute is given.
/// \returns `true` if the given attribute list (1-2) / entity (3-4) contain
/// an attribute of the given name (1+3) / kind (2+4).
/// `false` otherwise.
/// \group has_attribute
type_safe::optional_ref<const cpp_attribute> has_attribute(const cpp_attribute_list& attributes,
const std::string& name);
/// \group has_attribute
type_safe::optional_ref<const cpp_attribute> has_attribute(const cpp_attribute_list& attributes,
cpp_attribute_kind kind);
class cpp_entity;
/// \group has_attribute
type_safe::optional_ref<const cpp_attribute> has_attribute(const cpp_entity& e,
const std::string& name);
/// \group has_attribute
type_safe::optional_ref<const cpp_attribute> has_attribute(const cpp_entity& e,
cpp_attribute_kind kind);
} // namespace cppast
#endif // CPPAST_CPP_ATTRIBUTE_HPP_INCLUDED