clang 20.0.0git
AttrIterator.h
Go to the documentation of this file.
1//===- AttrIterator.h - Classes for attribute iteration ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Attr vector and specific_attr_iterator interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_ATTRITERATOR_H
14#define LLVM_CLANG_AST_ATTRITERATOR_H
15
16#include "clang/Basic/LLVM.h"
17#include "llvm/ADT/ADL.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Support/Casting.h"
20#include <cassert>
21#include <cstddef>
22#include <iterator>
23#include <type_traits>
24
25namespace clang {
26
27class Attr;
28
29/// AttrVec - A vector of Attr, which is how they are stored on the AST.
31
32/// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
33/// providing attributes that are of a specific type.
34template <typename SpecificAttr, typename Container = AttrVec>
36 using Iterator = typename Container::const_iterator;
37
38 /// Current - The current, underlying iterator.
39 /// In order to ensure we don't dereference an invalid iterator unless
40 /// specifically requested, we don't necessarily advance this all the
41 /// way. Instead, we advance it when an operation is requested; if the
42 /// operation is acting on what should be a past-the-end iterator,
43 /// then we offer no guarantees, but this way we do not dereference a
44 /// past-the-end iterator when we move to a past-the-end position.
45 mutable Iterator Current;
46
47 void AdvanceToNext() const {
48 while (!isa<SpecificAttr>(*Current))
49 ++Current;
50 }
51
52 void AdvanceToNext(Iterator I) const {
53 while (Current != I && !isa<SpecificAttr>(*Current))
54 ++Current;
55 }
56
57public:
58 using value_type = SpecificAttr *;
59 using reference = SpecificAttr *;
60 using pointer = SpecificAttr *;
61 using iterator_category = std::forward_iterator_tag;
62 using difference_type = std::ptrdiff_t;
63
65 explicit specific_attr_iterator(Iterator i) : Current(i) {}
66
68 AdvanceToNext();