[PATCH] D77791: [ASTMatchers] Add support for dynamic matching of ofKind narrowing matcher
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 9 04:50:46 PDT 2020
njames93 created this revision.
njames93 added reviewers: klimek, aaron.ballman.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Adds support for using the ofKind in clang-query and other dynamic matcher use cases
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D77791
Files:
clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
clang/lib/ASTMatchers/Dynamic/Marshallers.h
clang/lib/ASTMatchers/Dynamic/Registry.cpp
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -95,9 +95,6 @@
RegistryMaps::RegistryMaps() {
// TODO: Here is the list of the missing matchers, grouped by reason.
//
- // Need Variant/Parser fixes:
- // ofKind
- //
// Polymorphic + argument overload:
// findAll
//
@@ -458,6 +455,7 @@
REGISTER_MATCHER(objcThrowStmt);
REGISTER_MATCHER(objcTryStmt);
REGISTER_MATCHER(ofClass);
+ REGISTER_MATCHER(ofKind);
REGISTER_MATCHER(ompDefaultClause);
REGISTER_MATCHER(ompExecutableDirective);
REGISTER_MATCHER(on);
Index: clang/lib/ASTMatchers/Dynamic/Marshallers.h
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Marshallers.h
+++ clang/lib/ASTMatchers/Dynamic/Marshallers.h
@@ -27,6 +27,7 @@
#include "clang/Basic/AttrKinds.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/OpenMPKinds.h"
+#include "clang/Basic/TypeTraits.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
@@ -214,6 +215,34 @@
static llvm::Optional<std::string> getBestGuess(const VariantValue &Value);
};
+template <> struct ArgTypeTraits<UnaryExprOrTypeTrait> {
+private:
+ static Optional<UnaryExprOrTypeTrait>
+ getUnaryOrTypeTraitKind(llvm::StringRef ClauseKind) {
+ // FIXME: Typetraits should probably be in a `.def` to make less error prone
+ return llvm::StringSwitch<Optional<UnaryExprOrTypeTrait>>(ClauseKind)
+ .Case("UETT_SizeOf", UETT_SizeOf)
+ .Case("UETT_AlignOf", UETT_AlignOf)
+ .Case("UETT_VecStep", UETT_VecStep)
+ .Case("UETT_OpenMPRequiredSimdAlign", UETT_OpenMPRequiredSimdAlign)
+ .Case("UETT_PreferredAlignOf", UETT_PreferredAlignOf)
+ .Default(llvm::None);
+ }
+
+public:
+ static bool is(const VariantValue &Value) {
+ return Value.isString() && getUnaryOrTypeTraitKind(Value.getString());
+ }
+
+ static UnaryExprOrTypeTrait get(const VariantValue &Value) {
+ return *getUnaryOrTypeTraitKind(Value.getString());
+ }
+
+ static ArgKind getKind() { return ArgKind(ArgKind::AK_String); }
+
+ static llvm::Optional<std::string> getBestGuess(const VariantValue &Value);
+};
+
/// Matcher descriptor interface.
///
/// Provides a \c create() method that constructs the matcher from the provided
Index: clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
+++ clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
@@ -95,3 +95,18 @@
"OMPC_");
return llvm::None;
}
+
+llvm::Optional<std::string>
+clang::ast_matchers::dynamic::internal::ArgTypeTraits<
+ clang::UnaryExprOrTypeTrait>::getBestGuess(const VariantValue &Value) {
+ static constexpr llvm::StringRef Allowed[] = {
+ "UETT_SizeOf", "UETT_AlignOf",
+ "UETT_VecStep", "UETT_OpenMPRequiredSimdAlign",
+ "UETT_PreferredAlignOf",
+ };
+ if (Value.isString()) {
+ return ::getBestGuess(Value.getString(), llvm::makeArrayRef(Allowed),
+ "UETT_");
+ }
+ return llvm::None;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77791.256244.patch
Type: text/x-patch
Size: 3295 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200409/d05c1977/attachment-0001.bin>
More information about the cfe-commits
mailing list