Line data Source code
1 : // Copyright (C) 2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_BUILTIN_ATTRIBUTE_CHECKER_H
20 : #define RUST_BUILTIN_ATTRIBUTE_CHECKER_H
21 :
22 : #include "rust-ast-visitor.h"
23 :
24 : namespace Rust {
25 : namespace Analysis {
26 :
27 : void check_valid_attribute_for_item (const AST::Attribute &attr,
28 : const AST::Item &item);
29 :
30 : /**
31 : * Checks the validity of builtin attributes.
32 : *
33 : * The goal of this visitor is to make sure that builtin attributes are
34 : * correctly used by enforcing those rules:
35 : *
36 : * - Attributes are applied in allowed contexts, for example to make sure that
37 : * #[inline] is only applied to functions and closures, as well as checking the
38 : * "arguments"
39 : *
40 : * - input given to these attributes is appropriate and valid.
41 : */
42 : class BuiltinAttributeChecker : public AST::DefaultASTVisitor
43 : {
44 : using AST::DefaultASTVisitor::visit;
45 :
46 18373 : template <class I> void default_outer_attribute_check (I item)
47 : {
48 23327 : for (auto &attr : item.get_outer_attrs ())
49 4954 : check_valid_attribute_for_item (attr, item);
50 18373 : AST::DefaultASTVisitor::visit (item);
51 18373 : }
52 :
53 : public:
54 : BuiltinAttributeChecker ();
55 : void go (AST::Crate &crate);
56 : void visit (AST::Crate &crate) override;
57 : void visit (AST::Attribute &attribute) override;
58 :
59 : // Items
60 : void visit (AST::Module &module) override;
61 : void visit (AST::ExternCrate &crate) override;
62 : void visit (AST::UseDeclaration &use_decl) override;
63 : void visit (AST::Function &function) override;
64 : void visit (AST::TypeAlias &type_alias) override;
65 : void visit (AST::StructStruct &struct_item) override;
66 : void visit (AST::TupleStruct &tuple_struct) override;
67 : void visit (AST::Enum &enum_item) override;
68 : void visit (AST::Union &union_item) override;
69 : void visit (AST::ConstantItem &const_item) override;
70 : void visit (AST::StaticItem &static_item) override;
71 : void visit (AST::Trait &trait) override;
72 : void visit (AST::InherentImpl &impl) override;
73 : void visit (AST::TraitImpl &impl) override;
74 : void visit (AST::ExternBlock &block) override;
75 : };
76 :
77 : } // namespace Analysis
78 : } // namespace Rust
79 :
80 : #endif /* ! RUST_BUILTIN_ATTRIBUTE_CHECKER_H */
|