[PATCH] Add isSpecific matcher for builtin types.
Peter Collingbourne
peter at pcc.me.uk
Tue Feb 18 22:14:48 PST 2014
Hi klimek,
http://llvm-reviews.chandlerc.com/D2823
Files:
include/clang/ASTMatchers/ASTMatchers.h
lib/ASTMatchers/Dynamic/Registry.cpp
unittests/ASTMatchers/ASTMatchersTest.cpp
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -47,8 +47,10 @@
#include "clang/AST/DeclFriend.h"
#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/PrettyPrinter.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Regex.h"
#include <iterator>
@@ -2896,6 +2898,29 @@
/// matches "int b", "float c" and "bool d"
AST_TYPE_MATCHER(BuiltinType, builtinType);
+/// \brief Matches builtin types with a specific canonical name.
+///
+/// For C and C++, the canonical builtin type names are "void", "bool", "char",
+/// "signed char", "short", "int", "long", "long long", "unsigned char",
+/// "unsigned short", "unsigned int", "unsigned long", "unsigned long long",
+/// "float", "double", "long double", "wchar_t", "char16_t", "char32_t",
+/// "nullptr_t".
+///
+/// Given
+/// \code
+/// int a;
+/// float b;
+/// bool c;
+/// \endcode
+/// builtinType(isSpecific("float"))
+/// matches "float b"
+AST_MATCHER_P(BuiltinType, isSpecific, std::string, Name) {
+ PrintingPolicy PP((LangOptions()));
+ PP.Bool = 1; // Use "bool" instead of "_Bool".
+ PP.MSWChar = 0; // Use "wchar_t" instead of "__wchar_t".
+ return Node.getName(PP) == Name;
+}
+
/// \brief Matches all kinds of arrays.
///
/// Given
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -236,6 +236,7 @@
REGISTER_MATCHER(isPrivate);
REGISTER_MATCHER(isProtected);
REGISTER_MATCHER(isPublic);
+ REGISTER_MATCHER(isSpecific);
REGISTER_MATCHER(isTemplateInstantiation);
REGISTER_MATCHER(isVirtual);
REGISTER_MATCHER(isWritten);
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -3574,6 +3574,40 @@
EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
}
+TEST(TypeMatching, MatchesBuiltinTypes) {
+ EXPECT_TRUE(matches("void *a;",
+ pointerType(pointee(builtinType(isSpecific("void"))))));
+ EXPECT_TRUE(matches("bool a;", builtinType(isSpecific("bool"))));
+ EXPECT_TRUE(matches("char a;", builtinType(isSpecific("char"))));
+ EXPECT_TRUE(
+ matches("signed char a;", builtinType(isSpecific("signed char"))));
+ EXPECT_TRUE(matches("short a;", builtinType(isSpecific("short"))));
+ EXPECT_TRUE(matches("int a;", builtinType(isSpecific("int"))));
+ EXPECT_TRUE(matches("long a;", builtinType(isSpecific("long"))));
+ EXPECT_TRUE(matches("long long a;", builtinType(isSpecific("long long"))));
+ EXPECT_TRUE(
+ matches("unsigned char a;", builtinType(isSpecific("unsigned char"))));
+ EXPECT_TRUE(
+ matches("unsigned short a;", builtinType(isSpecific("unsigned short"))));
+ EXPECT_TRUE(
+ matches("unsigned int a;", builtinType(isSpecific("unsigned int"))));
+ EXPECT_TRUE(
+ matches("unsigned long a;", builtinType(isSpecific("unsigned long"))));
+ EXPECT_TRUE(matches("unsigned long long a;",
+ builtinType(isSpecific("unsigned long long"))));
+ EXPECT_TRUE(matches("float a;", builtinType(isSpecific("float"))));
+ EXPECT_TRUE(matches("double a;", builtinType(isSpecific("double"))));
+ EXPECT_TRUE(
+ matches("long double a;", builtinType(isSpecific("long double"))));
+ EXPECT_TRUE(matches("wchar_t a;", builtinType(isSpecific("wchar_t"))));
+ EXPECT_TRUE(matches("char16_t a;", builtinType(isSpecific("char16_t"))));
+ EXPECT_TRUE(matches("char32_t a;", builtinType(isSpecific("char32_t"))));
+ EXPECT_TRUE(matches("decltype(nullptr) a;",
+ hasCanonicalType(builtinType(isSpecific("nullptr_t")))));
+
+ EXPECT_TRUE(notMatches("long a;", builtinType(isSpecific("long long"))));
+}
+
TEST(TypeMatching, MatchesArrayTypes) {
EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
EXPECT_TRUE(matches("int a[42];", arrayType()));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2823.1.patch
Type: text/x-patch
Size: 4286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140218/95c3258c/attachment.bin>
More information about the cfe-commits
mailing list