From 9cf81c95fe55afb5893c6684e34657a45268ac41 Mon Sep 17 00:00:00 2001 From: Andreas Fertig Date: Thu, 10 Jan 2019 18:58:43 +0100 Subject: [PATCH] Fixed #108: Two different matchers did match the same statement. --- AutoStmtHandler.cpp | 11 +++-------- ImplicitCastHandler.cpp | 1 + InsightsMatchers.h | 12 ++++++++++++ tests/Issue106.cpp | 7 +++---- tests/Issue106.expect | 5 +++-- tests/Issue108.cpp | 11 +++++++++++ 6 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 tests/Issue108.cpp diff --git a/AutoStmtHandler.cpp b/AutoStmtHandler.cpp index e137e5f8..2a70c277 100644 --- a/AutoStmtHandler.cpp +++ b/AutoStmtHandler.cpp @@ -18,23 +18,18 @@ using namespace clang; using namespace clang::ast_matchers; //----------------------------------------------------------------------------- +namespace clang::ast_matchers { + // XXX: recent clang source has a declType matcher. Try to figure out a migration path. const internal::VariadicDynCastAllOfMatcher myDecltypeType; //----------------------------------------------------------------------------- +} // namespace clang::ast_matchers namespace clang::insights { AutoStmtHandler::AutoStmtHandler(Rewriter& rewrite, MatchFinder& matcher) : InsightsBase(rewrite) { - - static const auto isAutoAncestor = - hasAncestor(varDecl(anyOf(hasType(autoType().bind("autoType")), - hasType(qualType(hasDescendant(autoType().bind("autoType")))), - /* decltype and decltype(auto) */ - hasType(myDecltypeType().bind("dt")), - hasType(qualType(hasDescendant(myDecltypeType().bind("dt"))))))); - matcher.addMatcher(varDecl(unless(anyOf(isExpansionInSystemHeader(), isMacroOrInvalidLocation(), isAutoAncestor, diff --git a/ImplicitCastHandler.cpp b/ImplicitCastHandler.cpp index 8a93bd5a..74d07980 100644 --- a/ImplicitCastHandler.cpp +++ b/ImplicitCastHandler.cpp @@ -25,6 +25,7 @@ ImplicitCastHandler::ImplicitCastHandler(Rewriter& rewrite, MatchFinder& matcher anyOf(isExpansionInSystemHeader(), isMacroOrInvalidLocation(), isTemplate, + isAutoAncestor, hasAncestor(functionDecl()), hasAncestor(userDefinedLiteral()), hasAncestor(implicitCastExpr(hasMatchingCast())), /* will be catch by the walk down */ diff --git a/InsightsMatchers.h b/InsightsMatchers.h index 955e67fc..4f44360d 100644 --- a/InsightsMatchers.h +++ b/InsightsMatchers.h @@ -18,12 +18,24 @@ namespace clang { namespace ast_matchers { +// XXX: recent clang source has a declType matcher. Try to figure out a migration path. +extern const internal::VariadicDynCastAllOfMatcher myDecltypeType; +//----------------------------------------------------------------------------- + /* don't replace stuff in template definitions */ static const auto isTemplate = anyOf(hasAncestor(classTemplateDecl()), hasAncestor(functionTemplateDecl()), hasAncestor(classTemplateSpecializationDecl())); //----------------------------------------------------------------------------- +static const auto isAutoAncestor = + hasAncestor(varDecl(anyOf(hasType(autoType().bind("autoType")), + hasType(qualType(hasDescendant(autoType().bind("autoType")))), + /* decltype and decltype(auto) */ + hasType(myDecltypeType().bind("dt")), + hasType(qualType(hasDescendant(myDecltypeType().bind("dt"))))))); +//----------------------------------------------------------------------------- + /// \brief Shut up a unused variable warnings #define SILENCE \ (void)Finder; \ diff --git a/tests/Issue106.cpp b/tests/Issue106.cpp index c629339a..3cb02500 100644 --- a/tests/Issue106.cpp +++ b/tests/Issue106.cpp @@ -1,9 +1,8 @@ int main() { - auto p = new int[2][3][4]; + auto p = new int[3][3][4]; -// auto z = new int[1]; + auto z = new int[1]; -// auto x = new int; + auto x = new int; } - diff --git a/tests/Issue106.expect b/tests/Issue106.expect index 593855a3..155c748c 100644 --- a/tests/Issue106.expect +++ b/tests/Issue106.expect @@ -1,6 +1,7 @@ int main() { - int (*p)[3][4] = new int [2][3][4]; + int (*p)[3][4] = new int [3][3][4]; + int * z = new int[1]; + int * x = new int; } - diff --git a/tests/Issue108.cpp b/tests/Issue108.cpp new file mode 100644 index 00000000..714db0c3 --- /dev/null +++ b/tests/Issue108.cpp @@ -0,0 +1,11 @@ +auto gp = new int[2][3][4]; + +auto gz = new int[1]; + +auto gx = new int; + +auto gu = new int[0x2][0x3][0x4]; + +int main() +{ +}