clang  14.0.0git
Registry.h
Go to the documentation of this file.
1 //===- Registry.h - Matcher registry ----------------------------*- 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 /// \file
10 /// Registry of all known matchers.
11 ///
12 /// The registry provides a generic interface to construct any matcher by name.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
17 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
18 
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace clang {
29 namespace ast_matchers {
30 namespace dynamic {
31 
32 namespace internal {
33 
34 class MatcherDescriptor;
35 
36 /// A smart (owning) pointer for MatcherDescriptor. We can't use unique_ptr
37 /// because MatcherDescriptor is forward declared
39 public:
46 
47  MatcherDescriptor *get() { return Ptr; }
48 
49 private:
50  MatcherDescriptor *Ptr;
51 };
52 
53 } // namespace internal
54 
56 
58  MatcherCompletion() = default;
59  MatcherCompletion(StringRef TypedText, StringRef MatcherDecl,
60  unsigned Specificity)
63 
64  bool operator==(const MatcherCompletion &Other) const {
65  return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
66  }
67 
68  /// The text to type to select this matcher.
70 
71  /// The "declaration" of the matcher, with type information.
73 
74  /// Value corresponding to the "specificity" of the converted matcher.
75  ///
76  /// Zero specificity indicates that this conversion would produce a trivial
77  /// matcher that will either always or never match.
78  /// Such matchers are excluded from code completion results.
79  unsigned Specificity;
80 };
81 
82 class Registry {
83 public:
84  Registry() = delete;
85 
87 
88  static bool isBuilderMatcher(MatcherCtor Ctor);
89 
93 
94  /// Look up a matcher in the registry by name,
95  ///
96  /// \return An opaque value which may be used to refer to the matcher
97  /// constructor, or Optional<MatcherCtor>() if not found.
98  static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
99 
100  /// Compute the list of completion types for \p Context.
101  ///
102  /// Each element of \p Context represents a matcher invocation, going from
103  /// outermost to innermost. Elements are pairs consisting of a reference to
104  /// the matcher constructor and the index of the next element in the
105  /// argument list of that matcher (or for the last element, the index of
106  /// the completion point in the argument list). An empty list requests
107  /// completion for the root matcher.
108  static std::vector<ArgKind> getAcceptedCompletionTypes(
109  llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
110 
111  /// Compute the list of completions that match any of
112  /// \p AcceptedTypes.
113  ///
114  /// \param AcceptedTypes All types accepted for this completion.
115  ///
116  /// \return All completions for the specified types.
117  /// Completions should be valid when used in \c lookupMatcherCtor().
118  /// The matcher constructed from the return of \c lookupMatcherCtor()
119  /// should be convertible to some type in \p AcceptedTypes.
120  static std::vector<MatcherCompletion>
122 
123  /// Construct a matcher from the registry.
124  ///
125  /// \param Ctor The matcher constructor to instantiate.
126  ///
127  /// \param NameRange The location of the name in the matcher source.
128  /// Useful for error reporting.
129  ///
130  /// \param Args The argument list for the matcher. The number and types of the
131  /// values must be valid for the matcher requested. Otherwise, the function
132  /// will return an error.
133  ///
134  /// \return The matcher object constructed if no error was found.
135  /// A null matcher if the number of arguments or argument types do not match
136  /// the signature. In that case \c Error will contain the description of
137  /// the error.
139  SourceRange NameRange,
141  Diagnostics *Error);
142 
143  /// Construct a matcher from the registry and bind it.
144  ///
145  /// Similar the \c constructMatcher() above, but it then tries to bind the
146  /// matcher to the specified \c BindID.
147  /// If the matcher is not bindable, it sets an error in \c Error and returns
148  /// a null matcher.
150  SourceRange NameRange,
151  StringRef BindID,
153  Diagnostics *Error);
154 };
155 
156 } // namespace dynamic
157 } // namespace ast_matchers
158 } // namespace clang
159 
160 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H
clang::ast_matchers::dynamic::Registry::Registry
Registry()=delete
clang::ast_matchers::dynamic::Registry::nodeMatcherType
static ASTNodeKind nodeMatcherType(MatcherCtor)
Definition: Registry.cpp:591
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ast_matchers::dynamic::MatcherCompletion::operator==
bool operator==(const MatcherCompletion &Other) const
Definition: Registry.h:64
clang::ast_matchers::dynamic::Registry
Definition: Registry.h:82
llvm::Optional
Definition: LLVM.h:40
clang::ast_matchers::dynamic::Registry::getMatcherCompletions
static std::vector< MatcherCompletion > getMatcherCompletions(ArrayRef< ArgKind > AcceptedTypes)
Compute the list of completions that match any of AcceptedTypes.
Definition: Registry.cpp:669
clang::ast_matchers::dynamic::internal::MatcherDescriptorPtr::MatcherDescriptorPtr
MatcherDescriptorPtr(MatcherDescriptor *)
Definition: Registry.cpp:595
clang::ast_matchers::dynamic::MatcherCompletion::MatcherDecl
std::string MatcherDecl
The "declaration" of the matcher, with type information.
Definition: Registry.h:72
VariantValue.h
clang::ast_matchers::dynamic::MatcherCompletion::MatcherCompletion
MatcherCompletion()=default
clang::ast_matchers::dynamic::Registry::buildMatcherCtor
static internal::MatcherDescriptorPtr buildMatcherCtor(MatcherCtor, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Definition: Registry.cpp:605
clang::ast_matchers::dynamic::MatcherCompletion::MatcherCompletion
MatcherCompletion(StringRef TypedText, StringRef MatcherDecl, unsigned Specificity)
Definition: Registry.h:59
clang::ast_matchers::dynamic::internal::MatcherDescriptorPtr::get
MatcherDescriptor * get()
Definition: Registry.h:47
clang::ast_matchers::dynamic::MatcherCompletion
Definition: Registry.h:57
Diagnostics.h
clang::ast_matchers::dynamic::VariantMatcher
A variant matcher object.
Definition: VariantValue.h:105
llvm::ArrayRef
Definition: LLVM.h:34
clang::ast_matchers::dynamic::internal::MatcherDescriptor
Matcher descriptor interface.
Definition: Marshallers.h:304
clang::ast_matchers::dynamic::SourceRange
Definition: Diagnostics.h:36
clang::ast_matchers::dynamic::internal::MatcherDescriptorPtr::~MatcherDescriptorPtr
~MatcherDescriptorPtr()
Definition: Registry.cpp:598
clang::ast_matchers::dynamic::Registry::lookupMatcherCtor
static llvm::Optional< MatcherCtor > lookupMatcherCtor(StringRef MatcherName)
Look up a matcher in the registry by name,.
Definition: Registry.cpp:612
clang::ast_matchers::dynamic::MatcherCompletion::Specificity
unsigned Specificity
Value corresponding to the "specificity" of the converted matcher.
Definition: Registry.h:79
clang::ast_matchers::dynamic::MatcherCompletion::TypedText
std::string TypedText
The text to type to select this matcher.
Definition: Registry.h:69
clang::ast_matchers::dynamic::Registry::constructMatcher
static VariantMatcher constructMatcher(MatcherCtor Ctor, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Construct a matcher from the registry.
Definition: Registry.cpp:774
clang::ast_matchers::dynamic::Registry::constructBoundMatcher
static VariantMatcher constructBoundMatcher(MatcherCtor Ctor, SourceRange NameRange, StringRef BindID, ArrayRef< ParserValue > Args, Diagnostics *Error)
Construct a matcher from the registry and bind it.
Definition: Registry.cpp:781
clang::ASTNodeKind
Kind identifier.
Definition: ASTTypeTraits.h:50
clang::ast_matchers::dynamic::Registry::getAcceptedCompletionTypes
static std::vector< ArgKind > getAcceptedCompletionTypes(llvm::ArrayRef< std::pair< MatcherCtor, unsigned >> Context)
Compute the list of completion types for Context.
Definition: Registry.cpp:635
clang::ast_matchers::dynamic::Diagnostics
Helper class to manage error messages.
Definition: Diagnostics.h:50
clang
Definition: CalledOnceCheck.h:17
clang::ast_matchers::dynamic::internal::MatcherDescriptorPtr::operator=
MatcherDescriptorPtr & operator=(MatcherDescriptorPtr &&)=default
clang::ast_matchers::dynamic::internal::MatcherDescriptorPtr
A smart (owning) pointer for MatcherDescriptor.
Definition: Registry.h:38
clang::ast_matchers::dynamic::Registry::isBuilderMatcher
static bool isBuilderMatcher(MatcherCtor Ctor)
Definition: Registry.cpp:600