1f4a2713aSLionel Sambuc //===--- Registry.cpp - Matcher registry -------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===------------------------------------------------------------===//
9f4a2713aSLionel Sambuc ///
10f4a2713aSLionel Sambuc /// \file
11f4a2713aSLionel Sambuc /// \brief Registry map populated at static initialization time.
12f4a2713aSLionel Sambuc ///
13f4a2713aSLionel Sambuc //===------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/ASTMatchers/Dynamic/Registry.h"
16f4a2713aSLionel Sambuc #include "Marshallers.h"
17f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchers.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/ManagedStatic.h"
21*0a6a1f1dSLionel Sambuc #include <set>
22*0a6a1f1dSLionel Sambuc #include <utility>
23*0a6a1f1dSLionel Sambuc
24*0a6a1f1dSLionel Sambuc using namespace clang::ast_type_traits;
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc namespace clang {
27f4a2713aSLionel Sambuc namespace ast_matchers {
28f4a2713aSLionel Sambuc namespace dynamic {
29f4a2713aSLionel Sambuc namespace {
30f4a2713aSLionel Sambuc
31*0a6a1f1dSLionel Sambuc using internal::MatcherDescriptor;
32f4a2713aSLionel Sambuc
33*0a6a1f1dSLionel Sambuc typedef llvm::StringMap<const MatcherDescriptor *> ConstructorMap;
34f4a2713aSLionel Sambuc class RegistryMaps {
35f4a2713aSLionel Sambuc public:
36f4a2713aSLionel Sambuc RegistryMaps();
37f4a2713aSLionel Sambuc ~RegistryMaps();
38f4a2713aSLionel Sambuc
constructors() const39f4a2713aSLionel Sambuc const ConstructorMap &constructors() const { return Constructors; }
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc private:
42*0a6a1f1dSLionel Sambuc void registerMatcher(StringRef MatcherName, MatcherDescriptor *Callback);
43f4a2713aSLionel Sambuc ConstructorMap Constructors;
44f4a2713aSLionel Sambuc };
45f4a2713aSLionel Sambuc
registerMatcher(StringRef MatcherName,MatcherDescriptor * Callback)46f4a2713aSLionel Sambuc void RegistryMaps::registerMatcher(StringRef MatcherName,
47*0a6a1f1dSLionel Sambuc MatcherDescriptor *Callback) {
48f4a2713aSLionel Sambuc assert(Constructors.find(MatcherName) == Constructors.end());
49f4a2713aSLionel Sambuc Constructors[MatcherName] = Callback;
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc #define REGISTER_MATCHER(name) \
53f4a2713aSLionel Sambuc registerMatcher(#name, internal::makeMatcherAutoMarshall( \
54f4a2713aSLionel Sambuc ::clang::ast_matchers::name, #name));
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc #define SPECIFIC_MATCHER_OVERLOAD(name, Id) \
57f4a2713aSLionel Sambuc static_cast< ::clang::ast_matchers::name##_Type##Id>( \
58f4a2713aSLionel Sambuc ::clang::ast_matchers::name)
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc #define REGISTER_OVERLOADED_2(name) \
61f4a2713aSLionel Sambuc do { \
62*0a6a1f1dSLionel Sambuc MatcherDescriptor *Callbacks[] = { \
63f4a2713aSLionel Sambuc internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 0), \
64f4a2713aSLionel Sambuc #name), \
65f4a2713aSLionel Sambuc internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 1), \
66f4a2713aSLionel Sambuc #name) \
67f4a2713aSLionel Sambuc }; \
68f4a2713aSLionel Sambuc registerMatcher(#name, \
69*0a6a1f1dSLionel Sambuc new internal::OverloadedMatcherDescriptor(Callbacks)); \
70f4a2713aSLionel Sambuc } while (0)
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc /// \brief Generate a registry map with all the known matchers.
RegistryMaps()73f4a2713aSLionel Sambuc RegistryMaps::RegistryMaps() {
74f4a2713aSLionel Sambuc // TODO: Here is the list of the missing matchers, grouped by reason.
75f4a2713aSLionel Sambuc //
76f4a2713aSLionel Sambuc // Need Variant/Parser fixes:
77f4a2713aSLionel Sambuc // ofKind
78f4a2713aSLionel Sambuc //
79f4a2713aSLionel Sambuc // Polymorphic + argument overload:
80f4a2713aSLionel Sambuc // findAll
81f4a2713aSLionel Sambuc //
82f4a2713aSLionel Sambuc // Other:
83f4a2713aSLionel Sambuc // equals
84f4a2713aSLionel Sambuc // equalsNode
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(callee);
87f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(hasPrefix);
88f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(hasType);
89f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(isDerivedFrom);
90f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
91*0a6a1f1dSLionel Sambuc REGISTER_OVERLOADED_2(loc);
92f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(pointsTo);
93f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(references);
94f4a2713aSLionel Sambuc REGISTER_OVERLOADED_2(thisPointerType);
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc REGISTER_MATCHER(accessSpecDecl);
97f4a2713aSLionel Sambuc REGISTER_MATCHER(alignOfExpr);
98f4a2713aSLionel Sambuc REGISTER_MATCHER(allOf);
99f4a2713aSLionel Sambuc REGISTER_MATCHER(anyOf);
100f4a2713aSLionel Sambuc REGISTER_MATCHER(anything);
101f4a2713aSLionel Sambuc REGISTER_MATCHER(argumentCountIs);
102f4a2713aSLionel Sambuc REGISTER_MATCHER(arraySubscriptExpr);
103f4a2713aSLionel Sambuc REGISTER_MATCHER(arrayType);
104f4a2713aSLionel Sambuc REGISTER_MATCHER(asmStmt);
105*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(asString);
106f4a2713aSLionel Sambuc REGISTER_MATCHER(atomicType);
107f4a2713aSLionel Sambuc REGISTER_MATCHER(autoType);
108f4a2713aSLionel Sambuc REGISTER_MATCHER(binaryOperator);
109f4a2713aSLionel Sambuc REGISTER_MATCHER(bindTemporaryExpr);
110f4a2713aSLionel Sambuc REGISTER_MATCHER(blockPointerType);
111f4a2713aSLionel Sambuc REGISTER_MATCHER(boolLiteral);
112f4a2713aSLionel Sambuc REGISTER_MATCHER(breakStmt);
113f4a2713aSLionel Sambuc REGISTER_MATCHER(builtinType);
114f4a2713aSLionel Sambuc REGISTER_MATCHER(callExpr);
115*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(caseStmt);
116f4a2713aSLionel Sambuc REGISTER_MATCHER(castExpr);
117f4a2713aSLionel Sambuc REGISTER_MATCHER(catchStmt);
118f4a2713aSLionel Sambuc REGISTER_MATCHER(characterLiteral);
119f4a2713aSLionel Sambuc REGISTER_MATCHER(classTemplateDecl);
120f4a2713aSLionel Sambuc REGISTER_MATCHER(classTemplateSpecializationDecl);
121f4a2713aSLionel Sambuc REGISTER_MATCHER(complexType);
122f4a2713aSLionel Sambuc REGISTER_MATCHER(compoundLiteralExpr);
123f4a2713aSLionel Sambuc REGISTER_MATCHER(compoundStmt);
124f4a2713aSLionel Sambuc REGISTER_MATCHER(conditionalOperator);
125f4a2713aSLionel Sambuc REGISTER_MATCHER(constantArrayType);
126*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(constCastExpr);
127f4a2713aSLionel Sambuc REGISTER_MATCHER(constructExpr);
128f4a2713aSLionel Sambuc REGISTER_MATCHER(constructorDecl);
129f4a2713aSLionel Sambuc REGISTER_MATCHER(containsDeclaration);
130f4a2713aSLionel Sambuc REGISTER_MATCHER(continueStmt);
131*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(cStyleCastExpr);
132f4a2713aSLionel Sambuc REGISTER_MATCHER(ctorInitializer);
133*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(CUDAKernelCallExpr);
134f4a2713aSLionel Sambuc REGISTER_MATCHER(decl);
135*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(declaratorDecl);
136f4a2713aSLionel Sambuc REGISTER_MATCHER(declCountIs);
137f4a2713aSLionel Sambuc REGISTER_MATCHER(declRefExpr);
138f4a2713aSLionel Sambuc REGISTER_MATCHER(declStmt);
139f4a2713aSLionel Sambuc REGISTER_MATCHER(defaultArgExpr);
140*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(defaultStmt);
141f4a2713aSLionel Sambuc REGISTER_MATCHER(deleteExpr);
142f4a2713aSLionel Sambuc REGISTER_MATCHER(dependentSizedArrayType);
143f4a2713aSLionel Sambuc REGISTER_MATCHER(destructorDecl);
144f4a2713aSLionel Sambuc REGISTER_MATCHER(doStmt);
145f4a2713aSLionel Sambuc REGISTER_MATCHER(dynamicCastExpr);
146f4a2713aSLionel Sambuc REGISTER_MATCHER(eachOf);
147f4a2713aSLionel Sambuc REGISTER_MATCHER(elaboratedType);
148f4a2713aSLionel Sambuc REGISTER_MATCHER(enumConstantDecl);
149f4a2713aSLionel Sambuc REGISTER_MATCHER(enumDecl);
150*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(equalsBoundNode);
151*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(equalsIntegralValue);
152f4a2713aSLionel Sambuc REGISTER_MATCHER(explicitCastExpr);
153f4a2713aSLionel Sambuc REGISTER_MATCHER(expr);
154*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(exprWithCleanups);
155f4a2713aSLionel Sambuc REGISTER_MATCHER(fieldDecl);
156f4a2713aSLionel Sambuc REGISTER_MATCHER(floatLiteral);
157f4a2713aSLionel Sambuc REGISTER_MATCHER(forEach);
158*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(forEachConstructorInitializer);
159f4a2713aSLionel Sambuc REGISTER_MATCHER(forEachDescendant);
160*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(forEachSwitchCase);
161f4a2713aSLionel Sambuc REGISTER_MATCHER(forField);
162f4a2713aSLionel Sambuc REGISTER_MATCHER(forRangeStmt);
163f4a2713aSLionel Sambuc REGISTER_MATCHER(forStmt);
164*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(friendDecl);
165*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(functionalCastExpr);
166f4a2713aSLionel Sambuc REGISTER_MATCHER(functionDecl);
167f4a2713aSLionel Sambuc REGISTER_MATCHER(functionTemplateDecl);
168f4a2713aSLionel Sambuc REGISTER_MATCHER(functionType);
169f4a2713aSLionel Sambuc REGISTER_MATCHER(gotoStmt);
170f4a2713aSLionel Sambuc REGISTER_MATCHER(has);
171f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAncestor);
172f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnyArgument);
173f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnyConstructorInitializer);
174f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnyParameter);
175f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnySubstatement);
176f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnyTemplateArgument);
177f4a2713aSLionel Sambuc REGISTER_MATCHER(hasAnyUsingShadowDecl);
178f4a2713aSLionel Sambuc REGISTER_MATCHER(hasArgument);
179f4a2713aSLionel Sambuc REGISTER_MATCHER(hasArgumentOfType);
180*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasAttr);
181f4a2713aSLionel Sambuc REGISTER_MATCHER(hasBase);
182f4a2713aSLionel Sambuc REGISTER_MATCHER(hasBody);
183f4a2713aSLionel Sambuc REGISTER_MATCHER(hasCanonicalType);
184*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasCaseConstant);
185f4a2713aSLionel Sambuc REGISTER_MATCHER(hasCondition);
186f4a2713aSLionel Sambuc REGISTER_MATCHER(hasConditionVariableStatement);
187f4a2713aSLionel Sambuc REGISTER_MATCHER(hasDeclaration);
188*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasDeclContext);
189f4a2713aSLionel Sambuc REGISTER_MATCHER(hasDeducedType);
190f4a2713aSLionel Sambuc REGISTER_MATCHER(hasDescendant);
191f4a2713aSLionel Sambuc REGISTER_MATCHER(hasDestinationType);
192f4a2713aSLionel Sambuc REGISTER_MATCHER(hasEitherOperand);
193f4a2713aSLionel Sambuc REGISTER_MATCHER(hasElementType);
194*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasElse);
195f4a2713aSLionel Sambuc REGISTER_MATCHER(hasFalseExpression);
196*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasGlobalStorage);
197f4a2713aSLionel Sambuc REGISTER_MATCHER(hasImplicitDestinationType);
198f4a2713aSLionel Sambuc REGISTER_MATCHER(hasIncrement);
199f4a2713aSLionel Sambuc REGISTER_MATCHER(hasIndex);
200f4a2713aSLionel Sambuc REGISTER_MATCHER(hasInitializer);
201f4a2713aSLionel Sambuc REGISTER_MATCHER(hasLHS);
202f4a2713aSLionel Sambuc REGISTER_MATCHER(hasLocalQualifiers);
203*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasLocalStorage);
204f4a2713aSLionel Sambuc REGISTER_MATCHER(hasLoopInit);
205*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasLoopVariable);
206f4a2713aSLionel Sambuc REGISTER_MATCHER(hasMethod);
207f4a2713aSLionel Sambuc REGISTER_MATCHER(hasName);
208f4a2713aSLionel Sambuc REGISTER_MATCHER(hasObjectExpression);
209f4a2713aSLionel Sambuc REGISTER_MATCHER(hasOperatorName);
210f4a2713aSLionel Sambuc REGISTER_MATCHER(hasOverloadedOperatorName);
211f4a2713aSLionel Sambuc REGISTER_MATCHER(hasParameter);
212f4a2713aSLionel Sambuc REGISTER_MATCHER(hasParent);
213f4a2713aSLionel Sambuc REGISTER_MATCHER(hasQualifier);
214*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasRangeInit);
215f4a2713aSLionel Sambuc REGISTER_MATCHER(hasRHS);
216f4a2713aSLionel Sambuc REGISTER_MATCHER(hasSingleDecl);
217f4a2713aSLionel Sambuc REGISTER_MATCHER(hasSize);
218f4a2713aSLionel Sambuc REGISTER_MATCHER(hasSizeExpr);
219f4a2713aSLionel Sambuc REGISTER_MATCHER(hasSourceExpression);
220f4a2713aSLionel Sambuc REGISTER_MATCHER(hasTargetDecl);
221f4a2713aSLionel Sambuc REGISTER_MATCHER(hasTemplateArgument);
222*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasThen);
223f4a2713aSLionel Sambuc REGISTER_MATCHER(hasTrueExpression);
224*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(hasTypeLoc);
225f4a2713aSLionel Sambuc REGISTER_MATCHER(hasUnaryOperand);
226f4a2713aSLionel Sambuc REGISTER_MATCHER(hasValueType);
227f4a2713aSLionel Sambuc REGISTER_MATCHER(ifStmt);
228f4a2713aSLionel Sambuc REGISTER_MATCHER(ignoringImpCasts);
229f4a2713aSLionel Sambuc REGISTER_MATCHER(ignoringParenCasts);
230f4a2713aSLionel Sambuc REGISTER_MATCHER(ignoringParenImpCasts);
231f4a2713aSLionel Sambuc REGISTER_MATCHER(implicitCastExpr);
232f4a2713aSLionel Sambuc REGISTER_MATCHER(incompleteArrayType);
233f4a2713aSLionel Sambuc REGISTER_MATCHER(initListExpr);
234f4a2713aSLionel Sambuc REGISTER_MATCHER(innerType);
235f4a2713aSLionel Sambuc REGISTER_MATCHER(integerLiteral);
236f4a2713aSLionel Sambuc REGISTER_MATCHER(isArrow);
237*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isConst);
238f4a2713aSLionel Sambuc REGISTER_MATCHER(isConstQualified);
239f4a2713aSLionel Sambuc REGISTER_MATCHER(isDefinition);
240*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isDeleted);
241f4a2713aSLionel Sambuc REGISTER_MATCHER(isExplicitTemplateSpecialization);
242*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isExpr);
243f4a2713aSLionel Sambuc REGISTER_MATCHER(isExternC);
244f4a2713aSLionel Sambuc REGISTER_MATCHER(isImplicit);
245*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isExpansionInFileMatching);
246*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isExpansionInMainFile);
247*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isInstantiated);
248*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isExpansionInSystemHeader);
249f4a2713aSLionel Sambuc REGISTER_MATCHER(isInteger);
250*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isIntegral);
251*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isInTemplateInstantiation);
252*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isListInitialization);
253f4a2713aSLionel Sambuc REGISTER_MATCHER(isOverride);
254f4a2713aSLionel Sambuc REGISTER_MATCHER(isPrivate);
255f4a2713aSLionel Sambuc REGISTER_MATCHER(isProtected);
256f4a2713aSLionel Sambuc REGISTER_MATCHER(isPublic);
257*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(isPure);
258f4a2713aSLionel Sambuc REGISTER_MATCHER(isTemplateInstantiation);
259f4a2713aSLionel Sambuc REGISTER_MATCHER(isVirtual);
260f4a2713aSLionel Sambuc REGISTER_MATCHER(isWritten);
261f4a2713aSLionel Sambuc REGISTER_MATCHER(labelStmt);
262f4a2713aSLionel Sambuc REGISTER_MATCHER(lambdaExpr);
263*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(lValueReferenceType);
264f4a2713aSLionel Sambuc REGISTER_MATCHER(matchesName);
265f4a2713aSLionel Sambuc REGISTER_MATCHER(materializeTemporaryExpr);
266f4a2713aSLionel Sambuc REGISTER_MATCHER(member);
267f4a2713aSLionel Sambuc REGISTER_MATCHER(memberCallExpr);
268f4a2713aSLionel Sambuc REGISTER_MATCHER(memberExpr);
269f4a2713aSLionel Sambuc REGISTER_MATCHER(memberPointerType);
270f4a2713aSLionel Sambuc REGISTER_MATCHER(methodDecl);
271f4a2713aSLionel Sambuc REGISTER_MATCHER(namedDecl);
272f4a2713aSLionel Sambuc REGISTER_MATCHER(namespaceDecl);
273*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(namesType);
274f4a2713aSLionel Sambuc REGISTER_MATCHER(nestedNameSpecifier);
275f4a2713aSLionel Sambuc REGISTER_MATCHER(nestedNameSpecifierLoc);
276f4a2713aSLionel Sambuc REGISTER_MATCHER(newExpr);
277f4a2713aSLionel Sambuc REGISTER_MATCHER(nullPtrLiteralExpr);
278f4a2713aSLionel Sambuc REGISTER_MATCHER(nullStmt);
279f4a2713aSLionel Sambuc REGISTER_MATCHER(ofClass);
280f4a2713aSLionel Sambuc REGISTER_MATCHER(on);
281f4a2713aSLionel Sambuc REGISTER_MATCHER(onImplicitObjectArgument);
282f4a2713aSLionel Sambuc REGISTER_MATCHER(operatorCallExpr);
283f4a2713aSLionel Sambuc REGISTER_MATCHER(parameterCountIs);
284f4a2713aSLionel Sambuc REGISTER_MATCHER(parenType);
285*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(parmVarDecl);
286f4a2713aSLionel Sambuc REGISTER_MATCHER(pointee);
287f4a2713aSLionel Sambuc REGISTER_MATCHER(pointerType);
288f4a2713aSLionel Sambuc REGISTER_MATCHER(qualType);
289f4a2713aSLionel Sambuc REGISTER_MATCHER(recordDecl);
290f4a2713aSLionel Sambuc REGISTER_MATCHER(recordType);
291f4a2713aSLionel Sambuc REGISTER_MATCHER(referenceType);
292f4a2713aSLionel Sambuc REGISTER_MATCHER(refersToDeclaration);
293*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(refersToIntegralType);
294f4a2713aSLionel Sambuc REGISTER_MATCHER(refersToType);
295f4a2713aSLionel Sambuc REGISTER_MATCHER(reinterpretCastExpr);
296f4a2713aSLionel Sambuc REGISTER_MATCHER(returns);
297*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(returnStmt);
298*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(rValueReferenceType);
299f4a2713aSLionel Sambuc REGISTER_MATCHER(sizeOfExpr);
300f4a2713aSLionel Sambuc REGISTER_MATCHER(specifiesNamespace);
301f4a2713aSLionel Sambuc REGISTER_MATCHER(specifiesType);
302f4a2713aSLionel Sambuc REGISTER_MATCHER(specifiesTypeLoc);
303f4a2713aSLionel Sambuc REGISTER_MATCHER(statementCountIs);
304f4a2713aSLionel Sambuc REGISTER_MATCHER(staticCastExpr);
305f4a2713aSLionel Sambuc REGISTER_MATCHER(stmt);
306f4a2713aSLionel Sambuc REGISTER_MATCHER(stringLiteral);
307*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(substNonTypeTemplateParmExpr);
308f4a2713aSLionel Sambuc REGISTER_MATCHER(switchCase);
309f4a2713aSLionel Sambuc REGISTER_MATCHER(switchStmt);
310*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(templateArgument);
311*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(templateArgumentCountIs);
312f4a2713aSLionel Sambuc REGISTER_MATCHER(templateSpecializationType);
313*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(temporaryObjectExpr);
314f4a2713aSLionel Sambuc REGISTER_MATCHER(thisExpr);
315f4a2713aSLionel Sambuc REGISTER_MATCHER(throughUsingDecl);
316f4a2713aSLionel Sambuc REGISTER_MATCHER(throwExpr);
317f4a2713aSLionel Sambuc REGISTER_MATCHER(to);
318f4a2713aSLionel Sambuc REGISTER_MATCHER(tryStmt);
319f4a2713aSLionel Sambuc REGISTER_MATCHER(type);
320*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(typedefDecl);
321f4a2713aSLionel Sambuc REGISTER_MATCHER(typedefType);
322*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(typeLoc);
323f4a2713aSLionel Sambuc REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
324f4a2713aSLionel Sambuc REGISTER_MATCHER(unaryOperator);
325*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(unaryTransformType);
326*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(unless);
327*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(unresolvedConstructExpr);
328*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(unresolvedUsingValueDecl);
329f4a2713aSLionel Sambuc REGISTER_MATCHER(userDefinedLiteral);
330f4a2713aSLionel Sambuc REGISTER_MATCHER(usingDecl);
331*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(usingDirectiveDecl);
332*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(valueDecl);
333f4a2713aSLionel Sambuc REGISTER_MATCHER(varDecl);
334f4a2713aSLionel Sambuc REGISTER_MATCHER(variableArrayType);
335*0a6a1f1dSLionel Sambuc REGISTER_MATCHER(voidType);
336f4a2713aSLionel Sambuc REGISTER_MATCHER(whileStmt);
337f4a2713aSLionel Sambuc REGISTER_MATCHER(withInitializer);
338f4a2713aSLionel Sambuc }
339f4a2713aSLionel Sambuc
~RegistryMaps()340f4a2713aSLionel Sambuc RegistryMaps::~RegistryMaps() {
341f4a2713aSLionel Sambuc for (ConstructorMap::iterator it = Constructors.begin(),
342f4a2713aSLionel Sambuc end = Constructors.end();
343f4a2713aSLionel Sambuc it != end; ++it) {
344f4a2713aSLionel Sambuc delete it->second;
345f4a2713aSLionel Sambuc }
346f4a2713aSLionel Sambuc }
347f4a2713aSLionel Sambuc
348f4a2713aSLionel Sambuc static llvm::ManagedStatic<RegistryMaps> RegistryData;
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc } // anonymous namespace
351f4a2713aSLionel Sambuc
352f4a2713aSLionel Sambuc // static
lookupMatcherCtor(StringRef MatcherName)353*0a6a1f1dSLionel Sambuc llvm::Optional<MatcherCtor> Registry::lookupMatcherCtor(StringRef MatcherName) {
354f4a2713aSLionel Sambuc ConstructorMap::const_iterator it =
355f4a2713aSLionel Sambuc RegistryData->constructors().find(MatcherName);
356*0a6a1f1dSLionel Sambuc return it == RegistryData->constructors().end()
357*0a6a1f1dSLionel Sambuc ? llvm::Optional<MatcherCtor>()
358*0a6a1f1dSLionel Sambuc : it->second;
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
361*0a6a1f1dSLionel Sambuc namespace {
362*0a6a1f1dSLionel Sambuc
operator <<(llvm::raw_ostream & OS,const std::set<ASTNodeKind> & KS)363*0a6a1f1dSLionel Sambuc llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
364*0a6a1f1dSLionel Sambuc const std::set<ASTNodeKind> &KS) {
365*0a6a1f1dSLionel Sambuc unsigned Count = 0;
366*0a6a1f1dSLionel Sambuc for (std::set<ASTNodeKind>::const_iterator I = KS.begin(), E = KS.end();
367*0a6a1f1dSLionel Sambuc I != E; ++I) {
368*0a6a1f1dSLionel Sambuc if (I != KS.begin())
369*0a6a1f1dSLionel Sambuc OS << "|";
370*0a6a1f1dSLionel Sambuc if (Count++ == 3) {
371*0a6a1f1dSLionel Sambuc OS << "...";
372*0a6a1f1dSLionel Sambuc break;
373*0a6a1f1dSLionel Sambuc }
374*0a6a1f1dSLionel Sambuc OS << *I;
375*0a6a1f1dSLionel Sambuc }
376*0a6a1f1dSLionel Sambuc return OS;
377*0a6a1f1dSLionel Sambuc }
378*0a6a1f1dSLionel Sambuc
379*0a6a1f1dSLionel Sambuc } // namespace
380*0a6a1f1dSLionel Sambuc
getAcceptedCompletionTypes(ArrayRef<std::pair<MatcherCtor,unsigned>> Context)381*0a6a1f1dSLionel Sambuc std::vector<ArgKind> Registry::getAcceptedCompletionTypes(
382*0a6a1f1dSLionel Sambuc ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
383*0a6a1f1dSLionel Sambuc ASTNodeKind InitialTypes[] = {
384*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<Decl>(),
385*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<QualType>(),
386*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<Type>(),
387*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<Stmt>(),
388*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<NestedNameSpecifier>(),
389*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<NestedNameSpecifierLoc>(),
390*0a6a1f1dSLionel Sambuc ASTNodeKind::getFromNodeKind<TypeLoc>()};
391*0a6a1f1dSLionel Sambuc
392*0a6a1f1dSLionel Sambuc // Starting with the above seed of acceptable top-level matcher types, compute
393*0a6a1f1dSLionel Sambuc // the acceptable type set for the argument indicated by each context element.
394*0a6a1f1dSLionel Sambuc std::set<ArgKind> TypeSet(std::begin(InitialTypes), std::end(InitialTypes));
395*0a6a1f1dSLionel Sambuc for (const auto &CtxEntry : Context) {
396*0a6a1f1dSLionel Sambuc MatcherCtor Ctor = CtxEntry.first;
397*0a6a1f1dSLionel Sambuc unsigned ArgNumber = CtxEntry.second;
398*0a6a1f1dSLionel Sambuc std::vector<ArgKind> NextTypeSet;
399*0a6a1f1dSLionel Sambuc for (const ArgKind &Kind : TypeSet) {
400*0a6a1f1dSLionel Sambuc if (Kind.getArgKind() == Kind.AK_Matcher &&
401*0a6a1f1dSLionel Sambuc Ctor->isConvertibleTo(Kind.getMatcherKind()) &&
402*0a6a1f1dSLionel Sambuc (Ctor->isVariadic() || ArgNumber < Ctor->getNumArgs()))
403*0a6a1f1dSLionel Sambuc Ctor->getArgKinds(Kind.getMatcherKind(), ArgNumber, NextTypeSet);
404*0a6a1f1dSLionel Sambuc }
405*0a6a1f1dSLionel Sambuc TypeSet.clear();
406*0a6a1f1dSLionel Sambuc TypeSet.insert(NextTypeSet.begin(), NextTypeSet.end());
407*0a6a1f1dSLionel Sambuc }
408*0a6a1f1dSLionel Sambuc return std::vector<ArgKind>(TypeSet.begin(), TypeSet.end());
409*0a6a1f1dSLionel Sambuc }
410*0a6a1f1dSLionel Sambuc
411*0a6a1f1dSLionel Sambuc std::vector<MatcherCompletion>
getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes)412*0a6a1f1dSLionel Sambuc Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
413*0a6a1f1dSLionel Sambuc std::vector<MatcherCompletion> Completions;
414*0a6a1f1dSLionel Sambuc
415*0a6a1f1dSLionel Sambuc // Search the registry for acceptable matchers.
416*0a6a1f1dSLionel Sambuc for (ConstructorMap::const_iterator I = RegistryData->constructors().begin(),
417*0a6a1f1dSLionel Sambuc E = RegistryData->constructors().end();
418*0a6a1f1dSLionel Sambuc I != E; ++I) {
419*0a6a1f1dSLionel Sambuc std::set<ASTNodeKind> RetKinds;
420*0a6a1f1dSLionel Sambuc unsigned NumArgs = I->second->isVariadic() ? 1 : I->second->getNumArgs();
421*0a6a1f1dSLionel Sambuc bool IsPolymorphic = I->second->isPolymorphic();
422*0a6a1f1dSLionel Sambuc std::vector<std::vector<ArgKind>> ArgsKinds(NumArgs);
423*0a6a1f1dSLionel Sambuc unsigned MaxSpecificity = 0;
424*0a6a1f1dSLionel Sambuc for (const ArgKind& Kind : AcceptedTypes) {
425*0a6a1f1dSLionel Sambuc if (Kind.getArgKind() != Kind.AK_Matcher)
426*0a6a1f1dSLionel Sambuc continue;
427*0a6a1f1dSLionel Sambuc unsigned Specificity;
428*0a6a1f1dSLionel Sambuc ASTNodeKind LeastDerivedKind;
429*0a6a1f1dSLionel Sambuc if (I->second->isConvertibleTo(Kind.getMatcherKind(), &Specificity,
430*0a6a1f1dSLionel Sambuc &LeastDerivedKind)) {
431*0a6a1f1dSLionel Sambuc if (MaxSpecificity < Specificity)
432*0a6a1f1dSLionel Sambuc MaxSpecificity = Specificity;
433*0a6a1f1dSLionel Sambuc RetKinds.insert(LeastDerivedKind);
434*0a6a1f1dSLionel Sambuc for (unsigned Arg = 0; Arg != NumArgs; ++Arg)
435*0a6a1f1dSLionel Sambuc I->second->getArgKinds(Kind.getMatcherKind(), Arg, ArgsKinds[Arg]);
436*0a6a1f1dSLionel Sambuc if (IsPolymorphic)
437*0a6a1f1dSLionel Sambuc break;
438*0a6a1f1dSLionel Sambuc }
439*0a6a1f1dSLionel Sambuc }
440*0a6a1f1dSLionel Sambuc
441*0a6a1f1dSLionel Sambuc if (!RetKinds.empty() && MaxSpecificity > 0) {
442*0a6a1f1dSLionel Sambuc std::string Decl;
443*0a6a1f1dSLionel Sambuc llvm::raw_string_ostream OS(Decl);
444*0a6a1f1dSLionel Sambuc
445*0a6a1f1dSLionel Sambuc if (IsPolymorphic) {
446*0a6a1f1dSLionel Sambuc OS << "Matcher<T> " << I->first() << "(Matcher<T>";
447*0a6a1f1dSLionel Sambuc } else {
448*0a6a1f1dSLionel Sambuc OS << "Matcher<" << RetKinds << "> " << I->first() << "(";
449*0a6a1f1dSLionel Sambuc for (const std::vector<ArgKind> &Arg : ArgsKinds) {
450*0a6a1f1dSLionel Sambuc if (&Arg != &ArgsKinds[0])
451*0a6a1f1dSLionel Sambuc OS << ", ";
452*0a6a1f1dSLionel Sambuc
453*0a6a1f1dSLionel Sambuc bool FirstArgKind = true;
454*0a6a1f1dSLionel Sambuc std::set<ASTNodeKind> MatcherKinds;
455*0a6a1f1dSLionel Sambuc // Two steps. First all non-matchers, then matchers only.
456*0a6a1f1dSLionel Sambuc for (const ArgKind &AK : Arg) {
457*0a6a1f1dSLionel Sambuc if (AK.getArgKind() == ArgKind::AK_Matcher) {
458*0a6a1f1dSLionel Sambuc MatcherKinds.insert(AK.getMatcherKind());
459*0a6a1f1dSLionel Sambuc } else {
460*0a6a1f1dSLionel Sambuc if (!FirstArgKind) OS << "|";
461*0a6a1f1dSLionel Sambuc FirstArgKind = false;
462*0a6a1f1dSLionel Sambuc OS << AK.asString();
463*0a6a1f1dSLionel Sambuc }
464*0a6a1f1dSLionel Sambuc }
465*0a6a1f1dSLionel Sambuc if (!MatcherKinds.empty()) {
466*0a6a1f1dSLionel Sambuc if (!FirstArgKind) OS << "|";
467*0a6a1f1dSLionel Sambuc OS << "Matcher<" << MatcherKinds << ">";
468*0a6a1f1dSLionel Sambuc }
469*0a6a1f1dSLionel Sambuc }
470*0a6a1f1dSLionel Sambuc }
471*0a6a1f1dSLionel Sambuc if (I->second->isVariadic())
472*0a6a1f1dSLionel Sambuc OS << "...";
473*0a6a1f1dSLionel Sambuc OS << ")";
474*0a6a1f1dSLionel Sambuc
475*0a6a1f1dSLionel Sambuc std::string TypedText = I->first();
476*0a6a1f1dSLionel Sambuc TypedText += "(";
477*0a6a1f1dSLionel Sambuc if (ArgsKinds.empty())
478*0a6a1f1dSLionel Sambuc TypedText += ")";
479*0a6a1f1dSLionel Sambuc else if (ArgsKinds[0][0].getArgKind() == ArgKind::AK_String)
480*0a6a1f1dSLionel Sambuc TypedText += "\"";
481*0a6a1f1dSLionel Sambuc
482*0a6a1f1dSLionel Sambuc Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
483*0a6a1f1dSLionel Sambuc }
484*0a6a1f1dSLionel Sambuc }
485*0a6a1f1dSLionel Sambuc
486*0a6a1f1dSLionel Sambuc return Completions;
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc
489f4a2713aSLionel Sambuc // static
constructMatcher(MatcherCtor Ctor,const SourceRange & NameRange,ArrayRef<ParserValue> Args,Diagnostics * Error)490*0a6a1f1dSLionel Sambuc VariantMatcher Registry::constructMatcher(MatcherCtor Ctor,
491*0a6a1f1dSLionel Sambuc const SourceRange &NameRange,
492*0a6a1f1dSLionel Sambuc ArrayRef<ParserValue> Args,
493*0a6a1f1dSLionel Sambuc Diagnostics *Error) {
494*0a6a1f1dSLionel Sambuc return Ctor->create(NameRange, Args, Error);
495*0a6a1f1dSLionel Sambuc }
496*0a6a1f1dSLionel Sambuc
497*0a6a1f1dSLionel Sambuc // static
constructBoundMatcher(MatcherCtor Ctor,const SourceRange & NameRange,StringRef BindID,ArrayRef<ParserValue> Args,Diagnostics * Error)498*0a6a1f1dSLionel Sambuc VariantMatcher Registry::constructBoundMatcher(MatcherCtor Ctor,
499f4a2713aSLionel Sambuc const SourceRange &NameRange,
500f4a2713aSLionel Sambuc StringRef BindID,
501f4a2713aSLionel Sambuc ArrayRef<ParserValue> Args,
502f4a2713aSLionel Sambuc Diagnostics *Error) {
503*0a6a1f1dSLionel Sambuc VariantMatcher Out = constructMatcher(Ctor, NameRange, Args, Error);
504f4a2713aSLionel Sambuc if (Out.isNull()) return Out;
505f4a2713aSLionel Sambuc
506f4a2713aSLionel Sambuc llvm::Optional<DynTypedMatcher> Result = Out.getSingleMatcher();
507f4a2713aSLionel Sambuc if (Result.hasValue()) {
508f4a2713aSLionel Sambuc llvm::Optional<DynTypedMatcher> Bound = Result->tryBind(BindID);
509f4a2713aSLionel Sambuc if (Bound.hasValue()) {
510f4a2713aSLionel Sambuc return VariantMatcher::SingleMatcher(*Bound);
511f4a2713aSLionel Sambuc }
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc Error->addError(NameRange, Error->ET_RegistryNotBindable);
514f4a2713aSLionel Sambuc return VariantMatcher();
515f4a2713aSLionel Sambuc }
516f4a2713aSLionel Sambuc
517f4a2713aSLionel Sambuc } // namespace dynamic
518f4a2713aSLionel Sambuc } // namespace ast_matchers
519f4a2713aSLionel Sambuc } // namespace clang
520