Re: [llvm-branch-commits] [clang] c3a21e5 - [ASTMatchers] Ensure that we can match inside lambdas

2021-02-21 Thread Stephen Kelly via llvm-branch-commits
                                |-ImplicitCastExpr 0x45593fda9f88 
 'auto (*)() const -> void' 
                                | `-DeclRefExpr 0x45593fda9f08 col:5> 'auto () const -> void' lvalue CXXMethod 0x45593fda98f0 
'operator()' 'auto () const -> void'
                                `-ImplicitCastExpr 0x45593fda9fe0 
 'const (lambda at /tmp/nested-lambdas.cc:4:3)' 
lvalue 
`-MaterializeTemporaryExpr 0x45593fda9fc8  
'(lambda at /tmp/nested-lambdas.cc:4:3)' lvalue
                                    `-LambdaExpr 0x45593fda9dd0 
 '(lambda at /tmp/nested-lambdas.cc:4:3)'
                                      |-CXXRecordDecl 0x45593fda97b8 
 col:3 implicit class definition
                                      | |-DefinitionData lambda 
pass_in_registers empty standard_layout trivially_copyable literal 
can_const_default_init
                                      | | |-DefaultConstructor 
defaulted_is_constexpr
                                      | | |-CopyConstructor simple 
trivial has_const_param needs_implicit implicit_has_const_param
                                      | | |-MoveConstructor exists 
simple trivial needs_implicit
                                      | | |-CopyAssignment trivial 
has_const_param needs_implicit implicit_has_const_param

                                      | | |-MoveAssignment
                                      | | `-Destructor simple 
irrelevant trivial
                                      | |-CXXMethodDecl 0x45593fda98f0 
 line:4:3 used constexpr operator() 'auto () const -> 
void' inline
                                      | | `-CompoundStmt 
0x45593fda99a0 
                                      | |-CXXConversionDecl 
0x45593fda9c68  line:4:3 implicit constexpr 
operator void (*)() 'auto (*() const noexcept)() -> void' inline
                                      | |-CXXMethodDecl 0x45593fda9d18 
 line:4:3 implicit __invoke 'auto () -> void' static 
inline
                                      | `-CXXDestructorDecl 
0x45593fda9df8  col:3 implicit referenced ~ 'void () noexcept' 
inline default trivial
                                      `-CompoundStmt 0x45593fda99a0 




On Tue, Jan 5, 2021 at 3:45 PM Stephen Kelly via llvm-branch-commits 
<mailto:llvm-branch-commits@lists.llvm.org>> wrote:



Author: Stephen Kelly
Date: 2021-01-05T14:39:46Z
New Revision: c3a21e5de3dc3f55e4d219afd55dec518159d356

URL:

https://github.com/llvm/llvm-project/commit/c3a21e5de3dc3f55e4d219afd55dec518159d356
DIFF:

https://github.com/llvm/llvm-project/commit/c3a21e5de3dc3f55e4d219afd55dec518159d356.diff

LOG: [ASTMatchers] Ensure that we can match inside lambdas

Because we don't know in ASTMatchFinder whether we're matching in AsIs
or IgnoreUnlessSpelledInSource mode, we need to traverse the lambda
twice, but store whether we're matching in nodes spelled in source or
not.

Differential Revision: https://reviews.llvm.org/D93688

Added:


Modified:
    clang/include/clang/ASTMatchers/ASTMatchersInternal.h
    clang/lib/ASTMatchers/ASTMatchFinder.cpp
    clang/lib/ASTMatchers/ASTMatchersInternal.cpp
    clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed:





diff  --git
a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index 46de4093272d..f49728d1f50e 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -723,6 +723,8 @@ class ASTMatchFinder {

   virtual bool IsMatchingInASTNodeNotSpelledInSource() const = 0;

+  virtual bool IsMatchingInASTNodeNotAsIs() const = 0;
+
   bool isTraversalIgnoringImplicitNodes() const;

 protected:

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 762885fa0052..af21e2283d8b 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -475,6 +475,55 @@ class MatchASTVisitor : public
RecursiveASTVisitor,
         }
       }
       return true;
+    } else if (auto *LE = dyn_cast(S)) {
+      for (auto I : llvm::zip(LE->captures(), LE->capture_inits())) {
+        auto C = std::get<0>(I);
+        ASTNodeNotSpelledInSourceScope RAII(
+            this, TraversingASTNodeNotSpelledInSource ||
!C.isExplicit());
+        TraverseLambdaCapture(LE, , std::get<1>(I));
+      }
+
+      {
+        ASTNodeNotSpelledInSourceScope RAII(this, true);
+        TraverseDecl(LE->getLambdaClass());


The line above triggers an additional traversal of all nested lambdas, 
leading to exponential time growth.


+      }
+      {
+        ASTNodeNotAsIsSource

[llvm-branch-commits] [clang] 2a917b7 - Extend release notes for AST Matchers changes

2021-02-03 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-02-03T23:05:27Z
New Revision: 2a917b70e770e2d25d96f91beebf2a3e52bb9e66

URL: 
https://github.com/llvm/llvm-project/commit/2a917b70e770e2d25d96f91beebf2a3e52bb9e66
DIFF: 
https://github.com/llvm/llvm-project/commit/2a917b70e770e2d25d96f91beebf2a3e52bb9e66.diff

LOG: Extend release notes for AST Matchers changes

Added: 


Modified: 
clang/docs/ReleaseNotes.rst

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a34cd512ca59..9efd4c01f053 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -250,15 +250,41 @@ release of Clang. Users of the build system should adjust 
accordingly.
 AST Matchers
 
 
-- The behavior of TK_IgnoreUnlessSpelledInSource with the traverse() matcher
-  has been changed to no longer match on template instantiations or on
+- The ``mapAnyOf()`` matcher was added. This allows convenient matching of
+  
diff erent AST nodes which have a compatible matcher API. For example,
+  ``mapAnyOf(ifStmt, forStmt).with(hasCondition(integerLiteral()))``
+  matches any ``IfStmt`` or ``ForStmt`` with a integer literal as the
+  condition.
+
+- The ``binaryOperation()`` matcher allows matching expressions which
+  appear like binary operators in the code, even if they are really
+  ``CXXOperatorCallExpr`` for example. It is based on the ``mapAnyOf()``
+  matcher functionality. The matcher API for the latter node has been
+  extended with ``hasLHS()`` etc to facilitate the abstraction.
+
+- Matcher API for ``CXXRewrittenBinaryOperator`` has been added. In addition
+  to explicit matching with the ``cxxRewrittenBinaryOperator()`` matcher, the
+  ``binaryOperation()`` matches on nodes of this type.
+
+- The behavior of ``TK_IgnoreUnlessSpelledInSource`` with the ``traverse()``
+  matcher has been changed to no longer match on template instantiations or on
   implicit nodes which are not spelled in the source.
 
-- The TK_IgnoreImplicitCastsAndParentheses traversal kind was removed. It
-  is recommended to use TK_IgnoreUnlessSpelledInSource instead.
+- The ``TK_IgnoreImplicitCastsAndParentheses`` traversal kind was removed. It
+  is recommended to use ``TK_IgnoreUnlessSpelledInSource`` instead.
 
-- The behavior of the forEach() matcher was changed to not internally ignore
-  implicit and parenthesis nodes.
+- The behavior of the ``forEach()`` matcher was changed to not internally
+  ignore implicit and parenthesis nodes.  This makes it consistent with
+  the ``has()`` matcher.  Uses of ``forEach()`` relying on the old behavior
+  can now use the  ``traverse()`` matcher or ``ignoringParenCasts()``.
+
+- Several AST Matchers have been changed to match based on the active
+  traversal mode.  For example, ``argumentCountIs()`` matches the number of
+  arguments written in the source, ignoring default arguments represented
+  by ``CXXDefaultArgExpr`` nodes.
+
+- Improvements in AST Matchers allow more matching of template declarations,
+  independent of their template instantations.
 
 clang-format
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] e377c8e - Implement dynamic mapAnyOf in terms of ASTNodeKinds

2021-01-20 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-20T15:53:05Z
New Revision: e377c8eeb4aa2eb239a651f1fe12c27fc77deda3

URL: 
https://github.com/llvm/llvm-project/commit/e377c8eeb4aa2eb239a651f1fe12c27fc77deda3
DIFF: 
https://github.com/llvm/llvm-project/commit/e377c8eeb4aa2eb239a651f1fe12c27fc77deda3.diff

LOG: Implement dynamic mapAnyOf in terms of ASTNodeKinds

This reduces template bloat, but more importantly, makes it possible to
construct one from clang-query without template types.

Differential Revision: https://reviews.llvm.org/D94879

Added: 


Modified: 
clang/lib/ASTMatchers/Dynamic/Marshallers.h

Removed: 




diff  --git a/clang/lib/ASTMatchers/Dynamic/Marshallers.h 
b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
index 23e26dcd9db6..690b52162e2b 100644
--- a/clang/lib/ASTMatchers/Dynamic/Marshallers.h
+++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
@@ -925,32 +925,50 @@ class VariadicOperatorMatcherDescriptor : public 
MatcherDescriptor {
   const StringRef MatcherName;
 };
 
-template 
 class MapAnyOfMatcherDescriptor : public MatcherDescriptor {
-  std::vector Funcs;
+  ASTNodeKind CladeNodeKind;
+  std::vector NodeKinds;
 
 public:
-  MapAnyOfMatcherDescriptor(StringRef MatcherName)
-  : Funcs{DynCastAllOfMatcherDescriptor(
-ast_matchers::internal::VariadicDynCastAllOfMatcher{},
-MatcherName)...} {}
+  MapAnyOfMatcherDescriptor(ASTNodeKind CladeNodeKind,
+std::vector NodeKinds)
+  : CladeNodeKind(CladeNodeKind), NodeKinds(NodeKinds) {}
 
   VariantMatcher create(SourceRange NameRange, ArrayRef Args,
 Diagnostics *Error) const override {
-std::vector InnerArgs;
 
-for (auto const  : Funcs) {
-  InnerArgs.push_back(F.create(NameRange, Args, Error));
-  if (!Error->errors().empty())
-return {};
+std::vector NodeArgs;
+
+for (auto NK : NodeKinds) {
+  std::vector InnerArgs;
+
+  for (const auto  : Args) {
+if (!Arg.Value.isMatcher())
+  return {};
+const VariantMatcher  = Arg.Value.getMatcher();
+if (VM.hasTypedMatcher(NK)) {
+  auto DM = VM.getTypedMatcher(NK);
+  InnerArgs.push_back(DM);
+}
+  }
+
+  if (InnerArgs.empty()) {
+NodeArgs.push_back(
+DynTypedMatcher::trueMatcher(NK).dynCastTo(CladeNodeKind));
+  } else {
+NodeArgs.push_back(
+DynTypedMatcher::constructVariadic(
+ast_matchers::internal::DynTypedMatcher::VO_AllOf, NK,
+InnerArgs)
+.dynCastTo(CladeNodeKind));
+  }
 }
-return VariantMatcher::SingleMatcher(
-ast_matchers::internal::BindableMatcher(
-VariantMatcher::VariadicOperatorMatcher(
-ast_matchers::internal::DynTypedMatcher::VO_AnyOf,
-std::move(InnerArgs))
-.getTypedMatcher()));
+
+auto Result = DynTypedMatcher::constructVariadic(
+ast_matchers::internal::DynTypedMatcher::VO_AnyOf, CladeNodeKind,
+NodeArgs);
+Result.setAllowBind(true);
+return VariantMatcher::SingleMatcher(Result);
   }
 
   bool isVariadic() const override { return true; }
@@ -963,9 +981,11 @@ class MapAnyOfMatcherDescriptor : public MatcherDescriptor 
{
 
   bool isConvertibleTo(ASTNodeKind Kind, unsigned *Specificity,
ASTNodeKind *LeastDerivedKind) const override {
-return llvm::all_of(Funcs, [=](const auto ) {
-  return F.isConvertibleTo(Kind, Specificity, LeastDerivedKind);
-});
+if (Specificity)
+  *Specificity = 1;
+if (LeastDerivedKind)
+  *LeastDerivedKind = CladeNodeKind;
+return true;
   }
 };
 
@@ -1077,8 +1097,9 @@ template 
 std::unique_ptr makeMatcherAutoMarshall(
 ast_matchers::internal::MapAnyOfMatcherImpl,
 StringRef MatcherName) {
-  return std::make_unique>(
-  MatcherName);
+  return std::make_unique(
+  ASTNodeKind::getFromNodeKind(),
+  std::vector{ASTNodeKind::getFromNodeKind()...});
 }
 
 } // namespace internal



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 8000c77 - Make it possible to store a ASTNodeKind in VariantValue

2021-01-20 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-20T15:44:45Z
New Revision: 8000c778532bfe1cc74191e41e19272e54477ed0

URL: 
https://github.com/llvm/llvm-project/commit/8000c778532bfe1cc74191e41e19272e54477ed0
DIFF: 
https://github.com/llvm/llvm-project/commit/8000c778532bfe1cc74191e41e19272e54477ed0.diff

LOG: Make it possible to store a ASTNodeKind in VariantValue

Differential Revision: https://reviews.llvm.org/D94878

Added: 


Modified: 
clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h 
b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
index 140b41dffc40..fa033f49bc90 100644
--- a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
+++ b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
@@ -251,6 +251,7 @@ class VariantValue {
   VariantValue(double Double);
   VariantValue(unsigned Unsigned);
   VariantValue(StringRef String);
+  VariantValue(ASTNodeKind NodeKind);
   VariantValue(const VariantMatcher );
 
   /// Constructs an \c unsigned value (disambiguation from bool).
@@ -280,6 +281,10 @@ class VariantValue {
   const std::string () const;
   void setString(StringRef String);
 
+  bool isNodeKind() const;
+  const ASTNodeKind () const;
+  void setNodeKind(ASTNodeKind NodeKind);
+
   /// Matcher value functions.
   bool isMatcher() const;
   const VariantMatcher () const;
@@ -316,7 +321,8 @@ class VariantValue {
 VT_Double,
 VT_Unsigned,
 VT_String,
-VT_Matcher
+VT_Matcher,
+VT_NodeKind
   };
 
   /// All supported value types.
@@ -326,6 +332,7 @@ class VariantValue {
 bool Boolean;
 std::string *String;
 VariantMatcher *Matcher;
+ASTNodeKind *NodeKind;
   };
 
   ValueType Type;

diff  --git a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp 
b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
index f31dda82a932..d1ecb1e00b91 100644
--- a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
@@ -268,6 +268,10 @@ VariantValue::VariantValue(StringRef String) : 
Type(VT_Nothing) {
   setString(String);
 }
 
+VariantValue::VariantValue(ASTNodeKind NodeKind) : Type(VT_Nothing) {
+  setNodeKind(NodeKind);
+}
+
 VariantValue::VariantValue(const VariantMatcher ) : Type(VT_Nothing) {
   setMatcher(Matcher);
 }
@@ -290,6 +294,9 @@ VariantValue ::operator=(const VariantValue 
) {
   case VT_String:
 setString(Other.getString());
 break;
+  case VT_NodeKind:
+setNodeKind(Other.getNodeKind());
+break;
   case VT_Matcher:
 setMatcher(Other.getMatcher());
 break;
@@ -308,6 +315,9 @@ void VariantValue::reset() {
   case VT_Matcher:
 delete Value.Matcher;
 break;
+  case VT_NodeKind:
+delete Value.NodeKind;
+break;
   // Cases that do nothing.
   case VT_Boolean:
   case VT_Double:
@@ -378,6 +388,19 @@ void VariantValue::setString(StringRef NewValue) {
   Value.String = new std::string(NewValue);
 }
 
+bool VariantValue::isNodeKind() const { return Type == VT_NodeKind; }
+
+const ASTNodeKind ::getNodeKind() const {
+  assert(isNodeKind());
+  return *Value.NodeKind;
+}
+
+void VariantValue::setNodeKind(ASTNodeKind NewValue) {
+  reset();
+  Type = VT_NodeKind;
+  Value.NodeKind = new ASTNodeKind(NewValue);
+}
+
 bool VariantValue::isMatcher() const {
   return Type == VT_Matcher;
 }
@@ -449,6 +472,8 @@ std::string VariantValue::getTypeAsString() const {
   case VT_Boolean: return "Boolean";
   case VT_Double: return "Double";
   case VT_Unsigned: return "Unsigned";
+  case VT_NodeKind:
+return getNodeKind().asStringRef().str();
   case VT_Nothing: return "Nothing";
   }
   llvm_unreachable("Invalid Type");

diff  --git a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp 
b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
index c08d7fc3ff74..c62a6b385e28 100644
--- a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
+++ b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
@@ -184,6 +184,25 @@ TEST(VariantValueTest, Matcher) {
   .getTypedMatcher()));
 }
 
+TEST(VariantValueTest, NodeKind) {
+  VariantValue Value = ASTNodeKind::getFromNodeKind();
+  EXPECT_TRUE(Value.isNodeKind());
+  
EXPECT_TRUE(Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind()));
+
+  Value = ASTNodeKind::getFromNodeKind();
+  EXPECT_TRUE(Value.isNodeKind());
+  EXPECT_TRUE(Value.getNodeKind().isSame(
+  ASTNodeKind::getFromNodeKind()));
+
+  Value.setNodeKind(ASTNodeKind::getFromNodeKind());
+  EXPECT_TRUE(Value.isNodeKind());
+  EXPECT_TRUE(
+  Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind()));
+
+  Value = 42;
+  EXPECT_TRUE(!Value.isNodeKind());
+}
+
 } // end anonymous namespace
 } // end namespace dynamic
 } // end namespace ast_matchers




[llvm-branch-commits] [clang] 0cd0eb6 - Add API to retrieve a clade kind from ASTNodeKind

2021-01-19 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-19T22:51:30Z
New Revision: 0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b

URL: 
https://github.com/llvm/llvm-project/commit/0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b
DIFF: 
https://github.com/llvm/llvm-project/commit/0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b.diff

LOG: Add API to retrieve a clade kind from ASTNodeKind

Differential Revision: https://reviews.llvm.org/D94877

Added: 


Modified: 
clang/include/clang/AST/ASTTypeTraits.h
clang/lib/AST/ASTTypeTraits.cpp
clang/unittests/AST/ASTTypeTraitsTest.cpp

Removed: 




diff  --git a/clang/include/clang/AST/ASTTypeTraits.h 
b/clang/include/clang/AST/ASTTypeTraits.h
index a91f6b0c1a69..57195a9d6066 100644
--- a/clang/include/clang/AST/ASTTypeTraits.h
+++ b/clang/include/clang/AST/ASTTypeTraits.h
@@ -100,6 +100,8 @@ class ASTNodeKind {
   static ASTNodeKind getMostDerivedCommonAncestor(ASTNodeKind Kind1,
   ASTNodeKind Kind2);
 
+  ASTNodeKind getCladeKind() const;
+
   /// Hooks for using ASTNodeKind as a key in a DenseMap.
   struct DenseMapInfo {
 // ASTNodeKind() is a good empty key because it is represented as a 0.

diff  --git a/clang/lib/AST/ASTTypeTraits.cpp b/clang/lib/AST/ASTTypeTraits.cpp
index 49335ff37c41..b040f9e4cc40 100644
--- a/clang/lib/AST/ASTTypeTraits.cpp
+++ b/clang/lib/AST/ASTTypeTraits.cpp
@@ -63,6 +63,17 @@ bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId 
Derived,
   return Derived == Base;
 }
 
+ASTNodeKind ASTNodeKind::getCladeKind() const {
+  NodeKindId LastId = KindId;
+  while (LastId) {
+NodeKindId ParentId = AllKindInfo[LastId].ParentId;
+if (ParentId == NKI_None)
+  return LastId;
+LastId = ParentId;
+  }
+  return NKI_None;
+}
+
 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
 
 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,

diff  --git a/clang/unittests/AST/ASTTypeTraitsTest.cpp 
b/clang/unittests/AST/ASTTypeTraitsTest.cpp
index 998488f1f1f9..18e8b8431b4c 100644
--- a/clang/unittests/AST/ASTTypeTraitsTest.cpp
+++ b/clang/unittests/AST/ASTTypeTraitsTest.cpp
@@ -39,6 +39,18 @@ TEST(ASTNodeKind, Bases) {
   EXPECT_TRUE(DNT().isSame(DNT()));
 }
 
+TEST(DynTypedNode, Clades) {
+  EXPECT_TRUE(DNT().getCladeKind().isSame(DNT()));
+  EXPECT_TRUE(DNT().getCladeKind().isSame(DNT()));
+
+  EXPECT_TRUE(DNT().getCladeKind().isSame(DNT()));
+  EXPECT_TRUE(DNT().getCladeKind().isSame(DNT()));
+
+  EXPECT_FALSE(DNT().getCladeKind().isSame(DNT()));
+
+  EXPECT_TRUE(ASTNodeKind().getCladeKind().isNone());
+}
+
 TEST(ASTNodeKind, BaseDistances) {
   unsigned Distance = 1;
   EXPECT_TRUE(DNT().isBaseOf(DNT(), ));



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 8d112a8 - Remove TypedMatcherOps from VariantValue

2021-01-19 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-19T22:39:58Z
New Revision: 8d112a8eda9d78bc4c97cf7bc9e133afae7b6eed

URL: 
https://github.com/llvm/llvm-project/commit/8d112a8eda9d78bc4c97cf7bc9e133afae7b6eed
DIFF: 
https://github.com/llvm/llvm-project/commit/8d112a8eda9d78bc4c97cf7bc9e133afae7b6eed.diff

LOG: Remove TypedMatcherOps from VariantValue

It provides no features or advantage over ASTNodeKind-based handling.

Differential Revision: https://reviews.llvm.org/D94876

Added: 


Modified: 
clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
clang/lib/ASTMatchers/Dynamic/VariantValue.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h 
b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
index e47b42a4f38c..140b41dffc40 100644
--- a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
+++ b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
@@ -100,8 +100,7 @@ class VariantMatcher {
 
 /// Convert \p Matcher the destination type and return it as a new
 /// DynTypedMatcher.
-virtual DynTypedMatcher
-convertMatcher(const DynTypedMatcher ) const = 0;
+DynTypedMatcher convertMatcher(const DynTypedMatcher ) const;
 
 /// Constructs a variadic typed matcher from \p InnerMatchers.
 /// Will try to convert each inner matcher to the destination type and
@@ -110,9 +109,6 @@ class VariantMatcher {
 constructVariadicOperator(DynTypedMatcher::VariadicOperator Op,
   ArrayRef InnerMatchers) const;
 
-  protected:
-~MatcherOps() = default;
-
   private:
 ASTNodeKind NodeKind;
   };
@@ -174,8 +170,12 @@ class VariantMatcher {
   /// that can, the result would be ambiguous and false is returned.
   template 
   bool hasTypedMatcher() const {
+return hasTypedMatcher(ASTNodeKind::getFromNodeKind());
+  }
+
+  bool hasTypedMatcher(ASTNodeKind NK) const {
 if (!Value) return false;
-return Value->getTypedMatcher(TypedMatcherOps()).hasValue();
+return Value->getTypedMatcher(MatcherOps(NK)).hasValue();
   }
 
   /// Determines if the contained matcher can be converted to \p Kind.
@@ -197,10 +197,15 @@ class VariantMatcher {
   template 
   ast_matchers::internal::Matcher getTypedMatcher() const {
 assert(hasTypedMatcher() && "hasTypedMatcher() == false");
-return Value->getTypedMatcher(TypedMatcherOps())
+return 
Value->getTypedMatcher(MatcherOps(ASTNodeKind::getFromNodeKind()))
 ->template convertTo();
   }
 
+  DynTypedMatcher getTypedMatcher(ASTNodeKind NK) const {
+assert(hasTypedMatcher(NK) && "hasTypedMatcher(NK) == false");
+return *Value->getTypedMatcher(MatcherOps(NK));
+  }
+
   /// String representation of the type of the value.
   ///
   /// If the underlying matcher is a polymorphic one, the string will show all
@@ -211,7 +216,6 @@ class VariantMatcher {
   explicit VariantMatcher(std::shared_ptr Value)
   : Value(std::move(Value)) {}
 
-  template  struct TypedMatcherOps;
 
   class SinglePayload;
   class PolymorphicPayload;
@@ -220,17 +224,6 @@ class VariantMatcher {
   std::shared_ptr Value;
 };
 
-template 
-struct VariantMatcher::TypedMatcherOps final : VariantMatcher::MatcherOps {
-  TypedMatcherOps() : MatcherOps(ASTNodeKind::getFromNodeKind()) {}
-  typedef ast_matchers::internal::Matcher MatcherT;
-
-  DynTypedMatcher
-  convertMatcher(const DynTypedMatcher ) const override {
-return DynTypedMatcher(Matcher.convertTo());
-  }
-};
-
 /// Variant value class.
 ///
 /// Basically, a tagged union with value type semantics.

diff  --git a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp 
b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
index 866e2d0e3491..f31dda82a932 100644
--- a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
@@ -59,6 +59,11 @@ VariantMatcher::MatcherOps::canConstructFrom(const 
DynTypedMatcher ,
   return Matcher.canConvertTo(NodeKind);
 }
 
+DynTypedMatcher VariantMatcher::MatcherOps::convertMatcher(
+const DynTypedMatcher ) const {
+  return Matcher.dynCastTo(NodeKind);
+}
+
 llvm::Optional
 VariantMatcher::MatcherOps::constructVariadicOperator(
 DynTypedMatcher::VariadicOperator Op,



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] ecf6966 - [ASTMatchers] Allow use of mapAnyOf in more contexts

2021-01-19 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-19T22:10:09Z
New Revision: ecf696641e6ce4b22e8c8ea3c7476b9c1f0f200b

URL: 
https://github.com/llvm/llvm-project/commit/ecf696641e6ce4b22e8c8ea3c7476b9c1f0f200b
DIFF: 
https://github.com/llvm/llvm-project/commit/ecf696641e6ce4b22e8c8ea3c7476b9c1f0f200b.diff

LOG: [ASTMatchers] Allow use of mapAnyOf in more contexts

Add an operator overload to ArgumentAdaptingMatcherFunc to allow use of
mapAnyOf within hasAncestor, hasParent etc.

Differential Revision: https://reviews.llvm.org/D94864

Added: 


Modified: 
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index aa78a893dcf6b..2af4e6e88109b 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1443,6 +1443,13 @@ struct ArgumentAdaptingMatcherFunc {
   operator()(const Matcher ) const {
 return create(InnerMatcher);
   }
+
+  template 
+  ArgumentAdaptingMatcherFuncAdaptor::Type, ToTypes>
+  operator()(const MapAnyOfHelper ) const {
+return create(InnerMatcher.with());
+  }
 };
 
 template  class TraversalMatcher : public MatcherInterface {

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index b3582a02243a2..d681620cf5483 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -491,6 +491,17 @@ void F() {
   Code, traverse(TK_IgnoreUnlessSpelledInSource,
  mapAnyOf(ifStmt, 
forStmt).with(hasCondition(falseExpr);
 
+  EXPECT_TRUE(
+  matches(Code, cxxBoolLiteral(equals(true),
+   hasAncestor(mapAnyOf(ifStmt, forStmt);
+
+  EXPECT_TRUE(
+  matches(Code, cxxBoolLiteral(equals(false),
+   hasAncestor(mapAnyOf(ifStmt, forStmt);
+
+  EXPECT_TRUE(
+  notMatches(Code, floatLiteral(hasAncestor(mapAnyOf(ifStmt, forStmt);
+
   Code = R"cpp(
 void func(bool b) {}
 struct S {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] ce24bb0 - [ASTMatchers] NFC Rearrange declarations to allow more arg adapting

2021-01-19 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-19T21:32:42Z
New Revision: ce24bb0eddab12460a01e4d91faa435f2fc84bb6

URL: 
https://github.com/llvm/llvm-project/commit/ce24bb0eddab12460a01e4d91faa435f2fc84bb6
DIFF: 
https://github.com/llvm/llvm-project/commit/ce24bb0eddab12460a01e4d91faa435f2fc84bb6.diff

LOG: [ASTMatchers] NFC Rearrange declarations to allow more arg adapting

Added: 


Modified: 
clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index f56cda318f4e..aa78a893dcf6 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1171,6 +1171,232 @@ using HasDeclarationSupportedTypes =
  TemplateSpecializationType, TemplateTypeParmType, TypedefType,
  UnresolvedUsingType, ObjCIvarRefExpr>;
 
+/// A Matcher that allows binding the node it matches to an id.
+///
+/// BindableMatcher provides a \a bind() method that allows binding the
+/// matched node to an id if the match was successful.
+template  class BindableMatcher : public Matcher {
+public:
+  explicit BindableMatcher(const Matcher ) : Matcher(M) {}
+  explicit BindableMatcher(MatcherInterface *Implementation)
+  : Matcher(Implementation) {}
+
+  /// Returns a matcher that will bind the matched node on a match.
+  ///
+  /// The returned matcher is equivalent to this matcher, but will
+  /// bind the matched node on a match.
+  Matcher bind(StringRef ID) const {
+return DynTypedMatcher(*this)
+.tryBind(ID)
+->template unconditionalConvertTo();
+  }
+
+  /// Same as Matcher's conversion operator, but enables binding on
+  /// the returned matcher.
+  operator DynTypedMatcher() const {
+DynTypedMatcher Result = static_cast &>(*this);
+Result.setAllowBind(true);
+return Result;
+  }
+};
+
+/// Matches any instance of the given NodeType.
+///
+/// This is useful when a matcher syntactically requires a child matcher,
+/// but the context doesn't care. See for example: anything().
+class TrueMatcher {
+public:
+  using ReturnTypes = AllNodeBaseTypes;
+
+  template  operator Matcher() const {
+return DynTypedMatcher::trueMatcher(ASTNodeKind::getFromNodeKind())
+.template unconditionalConvertTo();
+  }
+};
+
+/// Creates a Matcher that matches if all inner matchers match.
+template 
+BindableMatcher
+makeAllOfComposite(ArrayRef *> InnerMatchers) {
+  // For the size() == 0 case, we return a "true" matcher.
+  if (InnerMatchers.empty()) {
+return BindableMatcher(TrueMatcher());
+  }
+  // For the size() == 1 case, we simply return that one matcher.
+  // No need to wrap it in a variadic operation.
+  if (InnerMatchers.size() == 1) {
+return BindableMatcher(*InnerMatchers[0]);
+  }
+
+  using PI = llvm::pointee_iterator *const *>;
+
+  std::vector DynMatchers(PI(InnerMatchers.begin()),
+   PI(InnerMatchers.end()));
+  return BindableMatcher(
+  DynTypedMatcher::constructVariadic(DynTypedMatcher::VO_AllOf,
+ ASTNodeKind::getFromNodeKind(),
+ std::move(DynMatchers))
+  .template unconditionalConvertTo());
+}
+
+/// Creates a Matcher that matches if
+/// T is dyn_cast'able into InnerT and all inner matchers match.
+///
+/// Returns BindableMatcher, as matchers that use dyn_cast have
+/// the same object both to match on and to run submatchers on,
+/// so there is no ambiguity with what gets bound.
+template 
+BindableMatcher
+makeDynCastAllOfComposite(ArrayRef *> InnerMatchers) {
+  return BindableMatcher(
+  makeAllOfComposite(InnerMatchers).template dynCastTo());
+}
+
+/// A VariadicDynCastAllOfMatcher object is a
+/// variadic functor that takes a number of Matcher and returns a
+/// Matcher that matches TargetT nodes that are matched by all of the
+/// given matchers, if SourceT can be dynamically casted into TargetT.
+///
+/// For example:
+///   const VariadicDynCastAllOfMatcher record;
+/// Creates a functor record(...) that creates a Matcher given
+/// a variable number of arguments of type Matcher.
+/// The returned matcher matches if the given Decl can by dynamically
+/// casted to CXXRecordDecl and all given matchers match.
+template 
+class VariadicDynCastAllOfMatcher
+: public VariadicFunction, Matcher,
+  makeDynCastAllOfComposite> {
+public:
+  VariadicDynCastAllOfMatcher() {}
+};
+
+/// A \c VariadicAllOfMatcher object is a variadic functor that takes
+/// a number of \c Matcher and returns a \c Matcher that matches \c T
+/// nodes that are matched by all of the given matchers.
+///
+/// For example:
+///   const VariadicAllOfMatcher nestedNameSpecifier;
+/// Creates a functor nestedNameSpecifier(...) 

[llvm-branch-commits] [clang] 9a7fb08 - NFC: Minor cleanup of function calls

2021-01-17 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-17T18:47:17Z
New Revision: 9a7fb0848771e3d38baf10e4d1078b50dd884265

URL: 
https://github.com/llvm/llvm-project/commit/9a7fb0848771e3d38baf10e4d1078b50dd884265
DIFF: 
https://github.com/llvm/llvm-project/commit/9a7fb0848771e3d38baf10e4d1078b50dd884265.diff

LOG: NFC: Minor cleanup of function calls

Added: 


Modified: 
clang/lib/ASTMatchers/ASTMatchFinder.cpp

Removed: 




diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index e35600a083d3..8ddd3c87e09d 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -257,10 +257,7 @@ class MatchChildASTVisitor
   return true;
 ScopedIncrement ScopedDepth();
 
-if (!match(*Node->getDecomposedForm().LHS) ||
-!match(*Node->getDecomposedForm().RHS))
-  return false;
-return true;
+return match(*Node->getLHS()) && match(*Node->getRHS());
   }
   bool TraverseLambdaExpr(LambdaExpr *Node) {
 if (!Finder->isTraversalIgnoringImplicitNodes())



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] b765eaf - [ASTMatchers] Add support for CXXRewrittenBinaryOperator

2021-01-16 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-16T13:44:22Z
New Revision: b765eaf9a617bd3da30f47ece731b33593929885

URL: 
https://github.com/llvm/llvm-project/commit/b765eaf9a617bd3da30f47ece731b33593929885
DIFF: 
https://github.com/llvm/llvm-project/commit/b765eaf9a617bd3da30f47ece731b33593929885.diff

LOG: [ASTMatchers] Add support for CXXRewrittenBinaryOperator

Differential Revision: https://reviews.llvm.org/D94130

Added: 


Modified: 
clang/docs/LibASTMatchersReference.html
clang/include/clang/AST/ASTNodeTraverser.h
clang/include/clang/AST/ExprCXX.h
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/AST/ASTTraverserTest.cpp
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index 453d8f16c25a..912da6e62c2f 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -498,6 +498,42 @@ Traverse Mode
 
 
 
+
+
+
+
+
+
+  Rewritten binary operators
+
+binaryOperator(
+  hasOperatorName(""),
+  hasRHS(integerLiteral(equals(0)))
+  )
+
+given:
+
+#include compare
+
+class HasSpaceship {
+public:
+   int x;
+   bool operator==(const HasSpaceship&) const = default;
+   std::strong_ordering operator<=>(const HasSpaceship&) const = default;
+};
+
+bool isLess(const HasSpaceship& a, const HasSpaceship& b) {
+   return a < b;
+}
+
+
+
+1 match found.
+
+
+No match found.
+
+
 
 
 
@@ -1523,6 +1559,25 @@ Node Matchers
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtcxxRewrittenBinaryOperatorMatcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html;>CXXRewrittenBinaryOperator...
+Matches 
rewritten binary operators
+
+Example matches use of "":
+  #include compare
+  struct HasSpaceshipMem {
+int a;
+constexpr auto operator=(const HasSpaceshipMem) const = 
default;
+  };
+  void compare() {
+HasSpaceshipMem hs1, hs2;
+if (hs1  hs2)
+return;
+  }
+See also the binaryOperation() matcher for more-general matching
+of this AST node.
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtcxxStaticCastExprMatcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXStaticCastExpr.html;>CXXStaticCastExpr...
 Matches a C++ 
static_cast expression.
 
@@ -3405,6 +3460,53 @@ Narrowing Matchers
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html;>CXXRewrittenBinaryOperatorhasAnyOperatorNameStringRef, ..., 
StringRef
+Matches operator 
expressions (binary or unary) that have any of the
+specified names.
+
+   hasAnyOperatorName("+", "-")
+ Is equivalent to
+   anyOf(hasOperatorName("+"), hasOperatorName("-"))
+
+
+
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html;>CXXRewrittenBinaryOperatorhasOperatorNamestd::string 
Name
+Matches the 
operator Name of operator expressions (binary or
+unary).
+
+Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
+  !(a || b)
+
+
+
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html;>CXXRewrittenBinaryOperatorisAssignmentOperator
+Matches all 
kinds of assignment operators.
+
+Example 1: matches a += b (matcher = binaryOperator(isAssignmentOperator()))
+  if (a == b)
+a += b;
+
+Example 2: matches s1 = s2
+   (matcher = cxxOperatorCallExpr(isAssignmentOperator()))
+  struct S { S operator=(const S); };
+  void x() { S s1, s2; s1 = s2; }
+
+
+
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html;>CXXRewrittenBinaryOperatorisComparisonOperator
+Matches 
comparison operators.
+
+Example 1: matches a == b (matcher = binaryOperator(isComparisonOperator()))
+  if (a == b)
+a += b;
+
+Example 2: matches s1  s2
+   (matcher = cxxOperatorCallExpr(isComparisonOperator()))
+  struct S { bool operator(const S other); };
+  void x(S s1, S s2) { bool b1 = s1  s2; }
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXUnresolvedConstructExpr.html;>CXXUnresolvedConstructExprargumentCountIsunsigned N
 Checks that a call 
expression or a constructor call expression has
 a specific number of arguments (including absent default arguments).
@@ -5174,8 +5276,8 @@ Narrowing Matchers
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasAnyOperatorNameStringRef, ..., 
StringRef
-Matches operator 
expressions (binary or unary) that have any of the
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasAnyOperatorNameStringRef, ..., 
StringRef

[llvm-branch-commits] [clang] e810e95 - [ASTMatchers] Add binaryOperation matcher

2021-01-16 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-16T13:44:09Z
New Revision: e810e95e4bb908d1c8844e2c6f7da999732cabc9

URL: 
https://github.com/llvm/llvm-project/commit/e810e95e4bb908d1c8844e2c6f7da999732cabc9
DIFF: 
https://github.com/llvm/llvm-project/commit/e810e95e4bb908d1c8844e2c6f7da999732cabc9.diff

LOG: [ASTMatchers] Add binaryOperation matcher

This is a simple utility which allows matching on binaryOperator and
cxxOperatorCallExpr. It can also be extended to support
cxxRewrittenBinaryOperator.

Add generic support for MapAnyOfMatchers to auto-marshalling functions.

Differential Revision: https://reviews.llvm.org/D94129

Added: 


Modified: 
clang/docs/LibASTMatchersReference.html
clang/docs/tools/dump_ast_matchers.py
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/lib/ASTMatchers/Dynamic/Marshallers.h
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Removed: 




diff  --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index 497e2d4584a2..453d8f16c25a 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -1206,6 +1206,7 @@ Node Matchers
 
 Example matches a || b
   !(a || b)
+See also the binaryOperation() matcher for more-general matching.
 
 
 
@@ -1505,6 +1506,8 @@ Node Matchers
   ostream operator (ostream out, int i) { };
   ostream o; int b = 1, c = 1;
   o  b  c;
+See also the binaryOperation() matcher for more-general matching of binary
+uses of this AST node.
 
 
 
@@ -5438,6 +5441,48 @@ AST Traversal Matchers
 Return 
typeNameParameters
 
 
+Matcher*binaryOperationMatcher*...Matcher*
+Matches nodes which 
can be used with binary operators.
+
+The code
+  var1 != var2;
+might be represented in the clang AST as a binaryOperator or a
+cxxOperatorCallExpr, depending on
+
+* whether the types of var1 and var2 are fundamental (binaryOperator) or at
+  least one is a class type (cxxOperatorCallExpr)
+* whether the code appears in a template declaration, if at least one of the
+  vars is a dependent-type (binaryOperator)
+
+This matcher elides details in places where the matchers for the nodes are
+compatible.
+
+Given
+  binaryOperation(
+hasOperatorName("!="),
+hasLHS(expr().bind("lhs")),
+hasRHS(expr().bind("rhs"))
+  )
+matches each use of "!=" in:
+  struct S{
+  bool operator!=(const S) const;
+  };
+
+  void foo()
+  {
+ 1 != 2;
+ S() != S();
+  }
+
+  templatetypename T
+  void templ()
+  {
+ 1 != 2;
+ T() != S();
+  }
+
+
+
 Matcher*eachOfMatcher*, ..., 
Matcher*
 Matches if any of the given 
matchers matches.
 

diff  --git a/clang/docs/tools/dump_ast_matchers.py 
b/clang/docs/tools/dump_ast_matchers.py
index d3aff1284f60..18afbdd36c6e 100755
--- a/clang/docs/tools/dump_ast_matchers.py
+++ b/clang/docs/tools/dump_ast_matchers.py
@@ -379,6 +379,14 @@ def act_on_decl(declaration, comment, allowed_types):
 add_matcher('*', name, 'Matcher<*>, ..., Matcher<*>', comment)
 return
 
+m = re.match(
+r"""^.*MapAnyOfMatcher<.*>\s*
+  ([a-zA-Z]*);$""",
+declaration, flags=re.X)
+if m:
+  name = m.groups()[0]
+  add_matcher('*', name, 'Matcher<*>...Matcher<*>', comment)
+  return
 
 # Parse free standing matcher functions, like:
 #   Matcher Name(Matcher InnerMatcher) {

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index e81ac4cb7bf8..8140e20c9cad 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1971,6 +1971,8 @@ extern const internal::VariadicDynCastAllOfMatcher
 ///   ostream  int b = 1, c = 1;
 ///   o << b << c;
 /// \endcode
+/// See also the binaryOperation() matcher for more-general matching of binary
+/// uses of this AST node.
 extern const internal::VariadicDynCastAllOfMatcher
 cxxOperatorCallExpr;
 
@@ -2393,6 +2395,7 @@ extern const internal::VariadicDynCastAllOfMatcher stmtExpr;
 /// \code
 ///   !(a || b)
 /// \endcode
+/// See also the binaryOperation() matcher for more-general matching.
 extern const internal::VariadicDynCastAllOfMatcher
 binaryOperator;
 
@@ -2729,6 +2732,53 @@ auto mapAnyOf(internal::VariadicDynCastAllOfMatcher const &...) {
   return internal::MapAnyOfHelper();
 }
 
+/// Matches nodes which can be used with binary operators.
+///
+/// The code
+/// \code
+///   var1 != var2;
+/// \endcode
+/// might be represented in the clang AST as a binaryOperator or a
+/// cxxOperatorCallExpr, depending on
+///
+/// * whether the types of var1 and var2 are fundamental (binaryOperator) or at
+///   least one is a class type (cxxOperatorCallExpr)
+/// * whether the code appears in a template declaration, if at least one of 
the
+///   vars is a 

[llvm-branch-commits] [clang] dbe056c - [ASTMatchers] Make cxxOperatorCallExpr matchers API-compatible with n-ary operators

2021-01-16 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-16T12:53:11Z
New Revision: dbe056c2e37f00b9f33ab63bba73dbb004e13562

URL: 
https://github.com/llvm/llvm-project/commit/dbe056c2e37f00b9f33ab63bba73dbb004e13562
DIFF: 
https://github.com/llvm/llvm-project/commit/dbe056c2e37f00b9f33ab63bba73dbb004e13562.diff

LOG: [ASTMatchers] Make cxxOperatorCallExpr matchers API-compatible with n-ary 
operators

This makes them composable with mapAnyOf().

Differential Revision: https://reviews.llvm.org/D94128

Added: 


Modified: 
clang/docs/LibASTMatchersReference.html
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index e5de645266b7..497e2d4584a2 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -3232,6 +3232,16 @@ Narrowing Matchers
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXOperatorCallExpr.html;>CXXOperatorCallExprhasAnyOperatorNameStringRef, ..., 
StringRef
+Matches operator 
expressions (binary or unary) that have any of the
+specified names.
+
+   hasAnyOperatorName("+", "-")
+ Is equivalent to
+   anyOf(hasOperatorName("+"), hasOperatorName("-"))
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXOperatorCallExpr.html;>CXXOperatorCallExprhasAnyOverloadedOperatorNameStringRef,
 ..., StringRef
 Matches overloaded operator names.
 
@@ -3244,6 +3254,15 @@ Narrowing Matchers
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXOperatorCallExpr.html;>CXXOperatorCallExprhasOperatorNamestd::string 
Name
+Matches the 
operator Name of operator expressions (binary or
+unary).
+
+Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
+  !(a || b)
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXOperatorCallExpr.html;>CXXOperatorCallExprhasOverloadedOperatorNameStringRef
 Name
 Matches 
overloaded operator names.
 
@@ -5152,8 +5171,8 @@ Narrowing Matchers
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasAnyOperatorNameStringRef, ..., 
StringRef
-Matches operator 
expressions (binary or unary) that have any of the
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasAnyOperatorNameStringRef, ..., 
StringRef
+Matches operator 
expressions (binary or unary) that have any of the
 specified names.
 
hasAnyOperatorName("+", "-")
@@ -5162,8 +5181,8 @@ Narrowing Matchers
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasOperatorNamestd::string 
Name
-Matches the 
operator Name of operator expressions (binary or
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html;>UnaryOperatorhasOperatorNamestd::string 
Name
+Matches the 
operator Name of operator expressions (binary or
 unary).
 
 Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
@@ -5689,16 +5708,16 @@ AST Traversal Matchers
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html;>ArraySubscriptExprhasLHSMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr 
InnerMatcher
-Matches the left hand side 
of binary operator expressions.
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html;>ArraySubscriptExprhasLHSMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr 
InnerMatcher
+Matches the left hand side 
of binary operator expressions.
 
 Example matches a (matcher = binaryOperator(hasLHS()))
   a || b
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html;>ArraySubscriptExprhasRHSMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr 
InnerMatcher
-Matches the right hand side 
of binary operator expressions.
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1ArraySubscriptExpr.html;>ArraySubscriptExprhasRHSMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr 
InnerMatcher
+Matches the right hand side 
of binary operator expressions.
 
 Example matches b (matcher = binaryOperator(hasRHS()))
   a || b
@@ -5749,7 +5768,7 @@ AST Traversal Matchers
 
 
 
-Matcherhttps://clang.llvm.org/doxygen/classclang_1_1BinaryOperator.html;>BinaryOperatorhasEitherOperandMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr  
InnerMatcher
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1BinaryOperator.html;>BinaryOperatorhasEitherOperandMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr 
InnerMatcher
 Matches if either 
the left hand side or the right hand side of a
 binary operator matches.
 
@@ -5763,7 +5782,7 @@ AST Traversal Matchers
 
 
 

[llvm-branch-commits] [clang] a710145 - [ASTMatchers] Add mapAnyOf matcher

2021-01-16 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-16T12:53:11Z
New Revision: a7101450a42e4f1ed5af1a38a6def08f1b5b58fe

URL: 
https://github.com/llvm/llvm-project/commit/a7101450a42e4f1ed5af1a38a6def08f1b5b58fe
DIFF: 
https://github.com/llvm/llvm-project/commit/a7101450a42e4f1ed5af1a38a6def08f1b5b58fe.diff

LOG: [ASTMatchers] Add mapAnyOf matcher

Make it possible to compose a matcher for different base nodes.

This accepts one or more node matcher functors and zero or more
matchers, composing the latter into the former.

This allows composing of matchers where the same inner matcher name is
used for the same concept, but with a different node functor. Currently,
there is a limitation that the nodes must be in the same "clade", so
while

  mapAnyOf(ifStmt, forStmt).with(hasBody(stmt()))

can be used, functionDecl can not be added to the tuple.

It is possible to use this in clang-query, but it will require changes
to the QueryParser, so is deferred to a future review.

Differential Revision: https://reviews.llvm.org/D94127

Added: 


Modified: 
clang/docs/LibASTMatchersReference.html
clang/docs/tools/dump_ast_matchers.py
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Removed: 




diff  --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index 18acfc48..e5de645266b7 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2593,6 +2593,29 @@ Narrowing Matchers
 
 
 
+unspecifiedmapAnyOfnodeMatcherFunction...
+Matches any of the 
NodeMatchers with InnerMatchers nested within
+
+Given
+  if (true);
+  for (; true; );
+with the matcher
+  mapAnyOf(ifStmt, forStmt).with(
+hasCondition(cxxBoolLiteralExpr(equals(true)))
+).bind("trueCond")
+matches the if and the for. It is equivalent to:
+  auto trueCond = hasCondition(cxxBoolLiteralExpr(equals(true)));
+  anyOf(
+ifStmt(trueCond).bind("trueCond"),
+forStmt(trueCond).bind("trueCond")
+);
+
+The with() chain-call accepts zero or more matchers which are combined
+as-if with allOf() in each of the node matchers.
+Usable as: Any Matcher
+
+
+
 Matcher*unlessMatcher*
 Matches if the provided 
matcher does not match.
 
@@ -2980,7 +3003,7 @@ Narrowing Matchers
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXDependentScopeMemberExpr.html;>CXXDependentScopeMemberExprhasMemberNamestd::string N
-Matches 
template-dependent, but known, member names
+Matches 
template-dependent, but known, member names.
 
 In template declarations, dependent members are not resolved and so can
 not be matched to particular named declarations.

diff  --git a/clang/docs/tools/dump_ast_matchers.py 
b/clang/docs/tools/dump_ast_matchers.py
index 045833be7673..d3aff1284f60 100755
--- a/clang/docs/tools/dump_ast_matchers.py
+++ b/clang/docs/tools/dump_ast_matchers.py
@@ -119,8 +119,15 @@ def add_matcher(result_type, name, args, comment, 
is_dyncast=False):
   ids[name] += 1
   args = unify_arguments(args)
   result_type = unify_type(result_type)
+
+  docs_result_type = esc('Matcher<%s>' % result_type);
+
+  if name == 'mapAnyOf':
+args = "nodeMatcherFunction..."
+docs_result_type = "unspecified"
+
   matcher_html = TD_TEMPLATE % {
-'result': esc('Matcher<%s>' % result_type),
+'result': docs_result_type,
 'name': name,
 'args': esc(args),
 'comment': esc(strip_doxygen(comment)),
@@ -135,7 +142,7 @@ def add_matcher(result_type, name, args, comment, 
is_dyncast=False):
   # exclude known narrowing matchers that also take other matchers as
   # arguments.
   elif ('Matcher<' not in args or
-name in ['allOf', 'anyOf', 'anything', 'unless']):
+name in ['allOf', 'anyOf', 'anything', 'unless', 'mapAnyOf']):
 dict = narrowing_matchers
 lookup = result_type + name + esc(args)
   else:

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index a45e08345fb3..dd99e6a420af 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -847,6 +847,12 @@ traverse(TraversalKind TK, const 
internal::PolymorphicMatcherWithParam2<
   TK, InnerMatcher);
 }
 
+template 
+internal::Matcher::Type>
+traverse(TraversalKind TK, const internal::MapAnyOfHelper ) 
{
+  return traverse(TK, InnerMatcher.with());
+}
+
 /// Matches expressions that match InnerMatcher after any implicit AST
 /// nodes are stripped off.
 ///
@@ -2693,6 +2699,36 @@ extern const internal::VariadicDynCastAllOfMatcher
 unaryExprOrTypeTraitExpr;
 
+/// Matches any of the \p NodeMatchers with InnerMatchers nested within
+///
+/// Given
+/// \code
+///   if (true);
+///   for (; true; );
+/// \endcode
+/// with the matcher
+/// \code
+///   mapAnyOf(ifStmt, forStmt).with(

[llvm-branch-commits] [clang] 16c6e9c - [ASTMatchers] Fix child traversal over range-for loops

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T21:29:37Z
New Revision: 16c6e9c58e9ae50a775945e6b407f1891f353d2f

URL: 
https://github.com/llvm/llvm-project/commit/16c6e9c58e9ae50a775945e6b407f1891f353d2f
DIFF: 
https://github.com/llvm/llvm-project/commit/16c6e9c58e9ae50a775945e6b407f1891f353d2f.diff

LOG: [ASTMatchers] Fix child traversal over range-for loops

Differential Revision: https://reviews.llvm.org/D94031

Added: 


Modified: 
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 99d95838af61..39bdb94e62c6 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -236,6 +236,20 @@ class MatchChildASTVisitor
 ScopedIncrement ScopedDepth();
 return traverse(TAL);
   }
+  bool TraverseCXXForRangeStmt(CXXForRangeStmt *Node) {
+if (!Finder->isTraversalIgnoringImplicitNodes())
+  return VisitorBase::TraverseCXXForRangeStmt(Node);
+if (!Node)
+  return true;
+ScopedIncrement ScopedDepth();
+if (auto *Init = Node->getInit())
+  if (!match(*Init))
+return false;
+if (!match(*Node->getLoopVariable()) || !match(*Node->getRangeInit()) ||
+!match(*Node->getBody()))
+  return false;
+return VisitorBase::TraverseStmt(Node->getBody());
+  }
   bool TraverseLambdaExpr(LambdaExpr *Node) {
 if (!Finder->isTraversalIgnoringImplicitNodes())
   return VisitorBase::TraverseLambdaExpr(Node);
@@ -575,8 +589,6 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
 
 if (isTraversalIgnoringImplicitNodes()) {
   IgnoreImplicitChildren = true;
-  if (Node.get())
-ScopedTraversal = true;
 }
 
 ASTNodeNotSpelledInSourceScope RAII(this, ScopedTraversal);

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index e706ea4b2a54..19ab6187d960 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2553,7 +2553,9 @@ struct CtorInitsNonTrivial : NonTrivial
 int arr[2];
 for (auto i : arr)
 {
-
+  if (true)
+  {
+  }
 }
   }
   )cpp";
@@ -2596,6 +2598,33 @@ struct CtorInitsNonTrivial : NonTrivial
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
 EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
   }
+  {
+auto M = cxxForRangeStmt(hasDescendant(ifStmt()));
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
+  {
+EXPECT_TRUE(matches(
+Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt(
+hasSingleDecl(varDecl(hasName("i");
+EXPECT_TRUE(
+matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
+   cxxForRangeStmt(has(varDecl(hasName("i")));
+  }
+  {
+EXPECT_TRUE(matches(
+Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt(hasSingleDecl(
+varDecl(hasInitializer(declRefExpr(
+to(varDecl(hasName("arr");
+EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
+   cxxForRangeStmt(has(declRefExpr(
+   to(varDecl(hasName("arr");
+  }
+  {
+auto M = cxxForRangeStmt(has(compoundStmt()));
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
   {
 auto M = binaryOperator(hasOperatorName("!="));
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
@@ -2659,7 +2688,8 @@ struct CtorInitsNonTrivial : NonTrivial
  true, {"-std=c++20"}));
   }
   {
-auto M = cxxForRangeStmt(has(declStmt()));
+auto M =
+cxxForRangeStmt(has(declStmt(hasSingleDecl(varDecl(hasName("i"));
 EXPECT_TRUE(
 matchesConditionally(Code, traverse(TK_AsIs, M), true, 
{"-std=c++20"}));
 EXPECT_FALSE(
@@ -2679,6 +2709,19 @@ struct CtorInitsNonTrivial : NonTrivial
 matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M),
  true, {"-std=c++20"}));
   }
+  {
+auto M = cxxForRangeStmt(
+has(declStmt(hasSingleDecl(varDecl(
+hasName("a"),
+hasInitializer(declRefExpr(to(varDecl(hasName("arr"),
+hasLoopVariable(varDecl(hasName("i"))),
+hasRangeInit(declRefExpr(to(varDecl(hasName("a"));
+EXPECT_TRUE(
+matchesConditionally(Code, traverse(TK_AsIs, M), true, 
{"-std=c++20"}));
+EXPECT_TRUE(
+

[llvm-branch-commits] [clang] f22c0f4 - [ASTMatchers] Omit methods from explicit template instantations

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T17:42:33Z
New Revision: f22c0f40b5d657c0293fc9332274c18d3c4f836c

URL: 
https://github.com/llvm/llvm-project/commit/f22c0f40b5d657c0293fc9332274c18d3c4f836c
DIFF: 
https://github.com/llvm/llvm-project/commit/f22c0f40b5d657c0293fc9332274c18d3c4f836c.diff

LOG: [ASTMatchers] Omit methods from explicit template instantations

Differential Revision: https://reviews.llvm.org/D94032

Added: 


Modified: 
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 54dc874470dd..99d95838af61 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -1158,6 +1158,8 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
   } else if (const auto *FD = dyn_cast(DeclNode)) {
 if (FD->isDefaulted())
   ScopedChildren = true;
+if (FD->isTemplateInstantiation())
+  ScopedTraversal = true;
   }
 
   ASTNodeNotSpelledInSourceScope RAII1(this, ScopedTraversal);

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index cde3e460eeb0..e706ea4b2a54 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2201,10 +2201,18 @@ struct TemplStruct {
   TemplStruct() {}
   ~TemplStruct() {}
 
+  void outOfLine(T);
+
 private:
   T m_t;
 };
 
+template
+void TemplStruct::outOfLine(T)
+{
+
+}
+
 template
 T timesTwo(T input)
 {
@@ -2277,7 +2285,7 @@ template<> bool timesTwo(bool){
  hasTemplateArgument(0, refersToType(asString("float";
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MTempl)));
 // TODO: If we could match on explicit instantiations of function 
templates,
-// this would be EXPECT_TRUE.
+// this would be EXPECT_TRUE. See Sema::ActOnExplicitInstantiation.
 EXPECT_FALSE(
 matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MTempl)));
   }
@@ -2324,6 +2332,14 @@ template<> bool timesTwo(bool){
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
 EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
   }
+  {
+// Instantiated, out-of-line methods are not matchable.
+auto M =
+cxxMethodDecl(hasName("outOfLine"), isDefinition(),
+  hasParameter(0, 
parmVarDecl(hasType(asString("float");
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
   {
 // Explicit specialization is written in source and it matches:
 auto M = classTemplateSpecializationDecl(



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 53c3acb - [clang-tidy] Add extra tests

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T15:43:56Z
New Revision: 53c3acb89fcc25ba7ef1f1d76a79c241eeacb7f0

URL: 
https://github.com/llvm/llvm-project/commit/53c3acb89fcc25ba7ef1f1d76a79c241eeacb7f0
DIFF: 
https://github.com/llvm/llvm-project/commit/53c3acb89fcc25ba7ef1f1d76a79c241eeacb7f0.diff

LOG: [clang-tidy] Add extra tests

By default, check_clang_tidy runs tests in c++11-or-later mode.

Differential Revision: https://reviews.llvm.org/D94029

Added: 


Modified: 

clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-semicolon-constexpr.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-raw-string-literal.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp

clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-value-param-header.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-avoid-const-params-in-decls.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-delete-null-pointer.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Removed: 




diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-semicolon-constexpr.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-semicolon-constexpr.cpp
index c18dd7bd1e93..bc82ce1c3ad7 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-semicolon-constexpr.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-semicolon-constexpr.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s bugprone-suspicious-semicolon %t -- -- -std=c++17
+// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-suspicious-semicolon 
%t
 
 void fail()
 {

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii.cpp
index 91ade5242ef1..d637806ba20a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-raii.cpp
@@ -36,6 +36,16 @@ struct Ctor {
   }
 };
 
+template 
+void templ() {
+  T();
+}
+
+template 
+void neverInstantiated() {
+  T();
+}
+
 void test() {
   Foo(42);
 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after 
creation; did you mean to name the object?
@@ -54,6 +64,9 @@ void test() {
 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after 
creation; did you mean to name the object?
 // CHECK-FIXES: FooBar give_me_a_name;
 
+  templ();
+  templ();
+
   Bar();
   f();
   qux();

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
index 8a69ca42a072..527c79840696 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++17 
-fno-delayed-template-parsing
+// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-use-after-move %t -- 
-- -fno-delayed-template-parsing
 
 typedef decltype(nullptr) nullptr_t;
 

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize-raw-string-literal.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize-raw-string-literal.cpp
index 626dcbd8972b..859d3bf819ff 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/modernize-raw-string-literal.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize-raw-string-literal.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s 
modernize-raw-string-literal %t -- -config="{CheckOptions: [{key: 
modernize-raw-string-literal.ReplaceShorterLiterals, value: 1}]}"
-// FIXME: Fix the checker to work in C++20 mode.
+// RUN: %check_clang_tidy %s modernize-raw-string-literal %t -- 
-config="{CheckOptions: [{key: 
modernize-raw-string-literal.ReplaceShorterLiterals, value: 1}]}"
 
 char const *const BackSlash("goink\\frob");
 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: escaped string literal can be 
written as a raw string literal [modernize-raw-string-literal]
@@ -47,8 +46,8 @@ char const *const MultibyteSnowman("\xE2\x98\x83");
 char const *const TrailingSpace("A line \\with space. \n");
 char const *const TrailingNewLine("A single \\line.\n");
 char const *const AlreadyRaw(R"(foobie\\bletch)");
-char const *const UTF8Literal(u8"foobie\\bletch");
-char const *const UTF8RawLiteral(u8R"(foobie\\bletch)");
+auto const *const UTF8Literal(u8"foobie\\bletch");
+auto const *const UTF8RawLiteral(u8R"(foobie\\bletch)");
 // TODO: enable these tests once all supported compilers
 // support char16_t and char32_t 

[llvm-branch-commits] [clang] 7e4f53f - [ASTMatchers] Fix traversal matchers with explicit and defaulted methods

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T15:22:21Z
New Revision: 7e4f53f748d3c97f0b9b852bfbcab0740aba521b

URL: 
https://github.com/llvm/llvm-project/commit/7e4f53f748d3c97f0b9b852bfbcab0740aba521b
DIFF: 
https://github.com/llvm/llvm-project/commit/7e4f53f748d3c97f0b9b852bfbcab0740aba521b.diff

LOG: [ASTMatchers] Fix traversal matchers with explicit and defaulted methods

Differential Revision: https://reviews.llvm.org/D94030

Added: 


Modified: 
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index ba2dd862f171..a45e08345fb3 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5490,6 +5490,9 @@ AST_MATCHER(FunctionDecl, isVariadic) {
 /// \endcode
 AST_MATCHER_P(CXXMethodDecl, ofClass,
   internal::Matcher, InnerMatcher) {
+
+  ASTChildrenNotSpelledInSourceScope RAII(Finder, false);
+
   const CXXRecordDecl *Parent = Node.getParent();
   return (Parent != nullptr &&
   InnerMatcher.matches(*Parent, Finder, Builder));
@@ -7037,6 +7040,9 @@ AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, 
internal::Matcher,
   ExplicitSpecifier ES = ExplicitSpecifier::getFromDecl();
   if (!ES.getExpr())
 return false;
+
+  ASTChildrenNotSpelledInSourceScope RAII(Finder, false);
+
   return InnerMatcher.matches(*ES.getExpr(), Finder, Builder);
 }
 

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index f49728d1f50e..39b3a8406a61 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -742,6 +742,24 @@ class ASTMatchFinder {
  const DynTypedMatcher ,
  BoundNodesTreeBuilder *Builder,
  AncestorMatchMode MatchMode) = 0;
+private:
+  friend struct ASTChildrenNotSpelledInSourceScope;
+  virtual bool isMatchingChildrenNotSpelledInSource() const = 0;
+  virtual void setMatchingChildrenNotSpelledInSource(bool Set) = 0;
+};
+
+struct ASTChildrenNotSpelledInSourceScope {
+  ASTChildrenNotSpelledInSourceScope(ASTMatchFinder *V, bool B)
+  : MV(V), MB(V->isMatchingChildrenNotSpelledInSource()) {
+V->setMatchingChildrenNotSpelledInSource(B);
+  }
+  ~ASTChildrenNotSpelledInSourceScope() {
+MV->setMatchingChildrenNotSpelledInSource(MB);
+  }
+
+private:
+  ASTMatchFinder *MV;
+  bool MB;
 };
 
 /// Specialization of the conversion functions for QualType.

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index af21e2283d8b..54dc874470dd 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -666,6 +666,13 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
   bool IsMatchingInASTNodeNotSpelledInSource() const override {
 return TraversingASTNodeNotSpelledInSource;
   }
+  bool isMatchingChildrenNotSpelledInSource() const override {
+return TraversingASTChildrenNotSpelledInSource;
+  }
+  void setMatchingChildrenNotSpelledInSource(bool Set) override {
+TraversingASTChildrenNotSpelledInSource = Set;
+  }
+
   bool IsMatchingInASTNodeNotAsIs() const override {
 return TraversingASTNodeNotAsIs;
   }
@@ -719,20 +726,6 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
 bool MB;
   };
 
-  struct ASTChildrenNotSpelledInSource {
-ASTChildrenNotSpelledInSource(MatchASTVisitor *V, bool B)
-: MV(V), MB(V->TraversingASTChildrenNotSpelledInSource) {
-  V->TraversingASTChildrenNotSpelledInSource = B;
-}
-~ASTChildrenNotSpelledInSource() {
-  MV->TraversingASTChildrenNotSpelledInSource = MB;
-}
-
-  private:
-MatchASTVisitor *MV;
-bool MB;
-  };
-
   class TimeBucketRegion {
   public:
 TimeBucketRegion() : Bucket(nullptr) {}
@@ -1168,7 +1161,7 @@ bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
   }
 
   ASTNodeNotSpelledInSourceScope RAII1(this, ScopedTraversal);
-  ASTChildrenNotSpelledInSource RAII2(this, ScopedChildren);
+  ASTChildrenNotSpelledInSourceScope RAII2(this, ScopedChildren);
 
   match(*DeclNode);
   return RecursiveASTVisitor::TraverseDecl(DeclNode);

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 1dc5179ce857..cde3e460eeb0 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2706,6 +2706,52 @@ void callDefaultArg()
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
 

[llvm-branch-commits] [llvm] c3403dc - [ASTMatchers] Fix build when no targets are enabled

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T14:40:35Z
New Revision: c3403dc63d73710d14844e8a8cfad7a0f52d4a54

URL: 
https://github.com/llvm/llvm-project/commit/c3403dc63d73710d14844e8a8cfad7a0f52d4a54
DIFF: 
https://github.com/llvm/llvm-project/commit/c3403dc63d73710d14844e8a8cfad7a0f52d4a54.diff

LOG: [ASTMatchers] Fix build when no targets are enabled

This makes sense to do when building only tools like clang-tidy for
example.

Differential Revision: https://reviews.llvm.org/D93987

Added: 


Modified: 
llvm/cmake/modules/LLVM-Config.cmake

Removed: 




diff  --git a/llvm/cmake/modules/LLVM-Config.cmake 
b/llvm/cmake/modules/LLVM-Config.cmake
index ebe13e7fc31c..5d9ec79c7c56 100644
--- a/llvm/cmake/modules/LLVM-Config.cmake
+++ b/llvm/cmake/modules/LLVM-Config.cmake
@@ -28,7 +28,9 @@ function(is_llvm_target_library library return_var)
 string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
   elseif(ARG_OMITTED_TARGETS)
 set(omitted_targets ${LLVM_ALL_TARGETS})
-list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
+if (LLVM_TARGETS_TO_BUILD)
+  list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
+endif()
 string(TOUPPER "${omitted_targets}" targets)
   else()
 string(TOUPPER "${LLVM_ALL_TARGETS}" targets)



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] c3a21e5 - [ASTMatchers] Ensure that we can match inside lambdas

2021-01-05 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2021-01-05T14:39:46Z
New Revision: c3a21e5de3dc3f55e4d219afd55dec518159d356

URL: 
https://github.com/llvm/llvm-project/commit/c3a21e5de3dc3f55e4d219afd55dec518159d356
DIFF: 
https://github.com/llvm/llvm-project/commit/c3a21e5de3dc3f55e4d219afd55dec518159d356.diff

LOG: [ASTMatchers] Ensure that we can match inside lambdas

Because we don't know in ASTMatchFinder whether we're matching in AsIs
or IgnoreUnlessSpelledInSource mode, we need to traverse the lambda
twice, but store whether we're matching in nodes spelled in source or
not.

Differential Revision: https://reviews.llvm.org/D93688

Added: 


Modified: 
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index 46de4093272d..f49728d1f50e 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -723,6 +723,8 @@ class ASTMatchFinder {
 
   virtual bool IsMatchingInASTNodeNotSpelledInSource() const = 0;
 
+  virtual bool IsMatchingInASTNodeNotAsIs() const = 0;
+
   bool isTraversalIgnoringImplicitNodes() const;
 
 protected:

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 762885fa0052..af21e2283d8b 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -475,6 +475,55 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
 }
   }
   return true;
+} else if (auto *LE = dyn_cast(S)) {
+  for (auto I : llvm::zip(LE->captures(), LE->capture_inits())) {
+auto C = std::get<0>(I);
+ASTNodeNotSpelledInSourceScope RAII(
+this, TraversingASTNodeNotSpelledInSource || !C.isExplicit());
+TraverseLambdaCapture(LE, , std::get<1>(I));
+  }
+
+  {
+ASTNodeNotSpelledInSourceScope RAII(this, true);
+TraverseDecl(LE->getLambdaClass());
+  }
+  {
+ASTNodeNotAsIsSourceScope RAII(this, true);
+
+// We need to poke around to find the bits that might be explicitly
+// written.
+TypeLoc TL = LE->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
+FunctionProtoTypeLoc Proto = TL.getAsAdjusted();
+
+if (auto *TPL = LE->getTemplateParameterList()) {
+  for (NamedDecl *D : *TPL) {
+TraverseDecl(D);
+  }
+  if (Expr *RequiresClause = TPL->getRequiresClause()) {
+TraverseStmt(RequiresClause);
+  }
+}
+
+if (LE->hasExplicitParameters()) {
+  // Visit parameters.
+  for (ParmVarDecl *Param : Proto.getParams())
+TraverseDecl(Param);
+}
+
+const auto *T = Proto.getTypePtr();
+for (const auto  : T->exceptions())
+  TraverseType(E);
+
+if (Expr *NE = T->getNoexceptExpr())
+  TraverseStmt(NE, Queue);
+
+if (LE->hasExplicitResultType())
+  TraverseTypeLoc(Proto.getReturnLoc());
+TraverseStmt(LE->getTrailingRequiresClause());
+
+TraverseStmt(LE->getBody());
+  }
+  return true;
 }
 return RecursiveASTVisitor::dataTraverseNode(S, Queue);
   }
@@ -617,6 +666,9 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
   bool IsMatchingInASTNodeNotSpelledInSource() const override {
 return TraversingASTNodeNotSpelledInSource;
   }
+  bool IsMatchingInASTNodeNotAsIs() const override {
+return TraversingASTNodeNotAsIs;
+  }
 
   bool TraverseTemplateInstantiations(ClassTemplateDecl *D) {
 ASTNodeNotSpelledInSourceScope RAII(this, true);
@@ -638,6 +690,7 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
 
 private:
   bool TraversingASTNodeNotSpelledInSource = false;
+  bool TraversingASTNodeNotAsIs = false;
   bool TraversingASTChildrenNotSpelledInSource = false;
 
   struct ASTNodeNotSpelledInSourceScope {
@@ -654,6 +707,18 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
 bool MB;
   };
 
+  struct ASTNodeNotAsIsSourceScope {
+ASTNodeNotAsIsSourceScope(MatchASTVisitor *V, bool B)
+: MV(V), MB(V->TraversingASTNodeNotAsIs) {
+  V->TraversingASTNodeNotAsIs = B;
+}
+~ASTNodeNotAsIsSourceScope() { MV->TraversingASTNodeNotAsIs = MB; }
+
+  private:
+MatchASTVisitor *MV;
+bool MB;
+  };
+
   struct ASTChildrenNotSpelledInSource {
 ASTChildrenNotSpelledInSource(MatchASTVisitor *V, bool B)
 : MV(V), MB(V->TraversingASTChildrenNotSpelledInSource) {

diff  --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp 
b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 3c19bceb079e..eb0fffcc3c37 100644
--- 

[llvm-branch-commits] [clang-tools-extra] 7ec7788 - Try to fix build on Windows

2020-12-22 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-12-22T20:25:58Z
New Revision: 7ec7788ac175f3ccb7083de0e786438ad8610771

URL: 
https://github.com/llvm/llvm-project/commit/7ec7788ac175f3ccb7083de0e786438ad8610771
DIFF: 
https://github.com/llvm/llvm-project/commit/7ec7788ac175f3ccb7083de0e786438ad8610771.diff

LOG: Try to fix build on Windows

Added: 


Modified: 

clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp

Removed: 




diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
index e730b1b22130..4fe75f46932d 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s readability-container-size-empty %t
+// RUN: %check_clang_tidy %s readability-container-size-empty %t -- -- 
-fno-delayed-template-parsing
 
 namespace std {
 template  struct vector {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] a5311d7 - [clang-tidy] Handle template instantiations in container size check

2020-12-22 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-12-22T18:44:45Z
New Revision: a5311d731e1b95e93b35b1e9183a4a531df386e7

URL: 
https://github.com/llvm/llvm-project/commit/a5311d731e1b95e93b35b1e9183a4a531df386e7
DIFF: 
https://github.com/llvm/llvm-project/commit/a5311d731e1b95e93b35b1e9183a4a531df386e7.diff

LOG: [clang-tidy] Handle template instantiations in container size check

readability-container-size-empty currently modifies source code based on
AST nodes in template instantiations, which means that it makes
transformations based on substituted types.  This can lead to
transforming code to be broken.

Change the matcher implementation to ignore template instantiations
explicitly, and add a matcher to explicitly handle template declarations
instead of instantiations.

Differential Revision: https://reviews.llvm.org/D91302

Added: 


Modified: 
clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index 14a38c09ad9b..e7c5f0ab05be 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -16,6 +16,77 @@
 using namespace clang::ast_matchers;
 
 namespace clang {
+namespace ast_matchers {
+AST_POLYMORPHIC_MATCHER_P2(hasAnyArgumentWithParam,
+   AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
+   CXXConstructExpr),
+   internal::Matcher, ArgMatcher,
+   internal::Matcher, ParamMatcher) {
+  BoundNodesTreeBuilder Result;
+  // The first argument of an overloaded member operator is the implicit object
+  // argument of the method which should not be matched against a parameter, so
+  // we skip over it here.
+  BoundNodesTreeBuilder Matches;
+  unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
+  .matches(Node, Finder, )
+  ? 1
+  : 0;
+  int ParamIndex = 0;
+  for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
+BoundNodesTreeBuilder ArgMatches(*Builder);
+if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()), 
Finder,
+   )) {
+  BoundNodesTreeBuilder ParamMatches(ArgMatches);
+  if (expr(anyOf(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
+ hasParameter(ParamIndex, ParamMatcher,
+ callExpr(callee(functionDecl(
+ hasParameter(ParamIndex, ParamMatcher))
+  .matches(Node, Finder, )) {
+Result.addMatch(ParamMatches);
+*Builder = std::move(Result);
+return true;
+  }
+}
+++ParamIndex;
+  }
+  return false;
+}
+
+AST_MATCHER(Expr, usedInBooleanContext) {
+  const char *ExprName = "__booleanContextExpr";
+  auto Result =
+  expr(expr().bind(ExprName),
+   anyOf(hasParent(varDecl(hasType(booleanType(,
+ hasParent(cxxConstructorDecl(
+ hasAnyConstructorInitializer(cxxCtorInitializer(
+ withInitializer(expr(equalsBoundNode(ExprName))),
+ forField(hasType(booleanType())),
+ hasParent(fieldDecl(hasType(booleanType(,
+ hasParent(stmt(anyOf(
+ explicitCastExpr(hasDestinationType(booleanType())),
+ ifStmt(hasCondition(expr(equalsBoundNode(ExprName,
+ doStmt(hasCondition(expr(equalsBoundNode(ExprName,
+ whileStmt(hasCondition(expr(equalsBoundNode(ExprName,
+ forStmt(hasCondition(expr(equalsBoundNode(ExprName,
+ conditionalOperator(
+ hasCondition(expr(equalsBoundNode(ExprName,
+ parenListExpr(hasParent(varDecl(hasType(booleanType(),
+ parenExpr(hasParent(
+ explicitCastExpr(hasDestinationType(booleanType(),
+ returnStmt(forFunction(returns(booleanType(,
+ cxxUnresolvedConstructExpr(hasType(booleanType())),
+ callExpr(hasAnyArgumentWithParam(
+ expr(equalsBoundNode(ExprName)),
+ parmVarDecl(hasType(booleanType(),
+ binaryOperator(hasAnyOperatorName("&&", "||")),
+ unaryOperator(hasOperatorName("!")).bind("NegOnSize"))
+  .matches(Node, Finder, Builder);
+  Builder->removeBindings([ExprName](const BoundNodesMap ) {
+return 

[llvm-branch-commits] [clang] 3b879fc - [ASTMatchers] Traverse-ignore range-for implementation details

2020-12-22 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-12-22T12:09:32Z
New Revision: 3b879fc97305849026db0e856920d318fadbc04b

URL: 
https://github.com/llvm/llvm-project/commit/3b879fc97305849026db0e856920d318fadbc04b
DIFF: 
https://github.com/llvm/llvm-project/commit/3b879fc97305849026db0e856920d318fadbc04b.diff

LOG: [ASTMatchers] Traverse-ignore range-for implementation details

Differential Revision: https://reviews.llvm.org/D93596

Added: 


Modified: 
clang/include/clang/AST/RecursiveASTVisitor.h
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/include/clang/AST/RecursiveASTVisitor.h 
b/clang/include/clang/AST/RecursiveASTVisitor.h
index 61e524793ec7..1426e569eabe 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -468,6 +468,8 @@ template  class RecursiveASTVisitor {
   DEF_TRAVERSE_TMPL_INST(Function)
 #undef DEF_TRAVERSE_TMPL_INST
 
+  bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
+
 private:
   // These are helper methods used by more than one Traverse* method.
   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
@@ -497,7 +499,6 @@ template  class RecursiveASTVisitor {
   bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node);
   bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node);
 
-  bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
   bool PostVisitStmt(Stmt *S);
 };
 

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index cc9537144524..762885fa0052 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -463,6 +463,22 @@ class MatchASTVisitor : public 
RecursiveASTVisitor,
   bool TraverseConstructorInitializer(CXXCtorInitializer *CtorInit);
   bool TraverseTemplateArgumentLoc(TemplateArgumentLoc TAL);
 
+  bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue) {
+if (auto *RF = dyn_cast(S)) {
+  for (auto *SubStmt : RF->children()) {
+if (SubStmt == RF->getInit() || SubStmt == RF->getLoopVarStmt() ||
+SubStmt == RF->getRangeInit() || SubStmt == RF->getBody()) {
+  TraverseStmt(SubStmt, Queue);
+} else {
+  ASTNodeNotSpelledInSourceScope RAII(this, true);
+  TraverseStmt(SubStmt, Queue);
+}
+  }
+  return true;
+}
+return RecursiveASTVisitor::dataTraverseNode(S, Queue);
+  }
+
   // Matches children or descendants of 'Node' with 'BaseMatcher'.
   bool memoizedMatchesRecursively(const DynTypedNode , ASTContext ,
   const DynTypedMatcher ,

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 10d2d6ec3916..a3a3a911b85c 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2580,6 +2580,31 @@ struct CtorInitsNonTrivial : NonTrivial
 EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
 EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
   }
+  {
+auto M = binaryOperator(hasOperatorName("!="));
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
+  {
+auto M = unaryOperator(hasOperatorName("++"));
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
+  {
+auto M = declStmt(hasSingleDecl(varDecl(matchesName("__range";
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
+  {
+auto M = declStmt(hasSingleDecl(varDecl(matchesName("__begin";
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
+  {
+auto M = declStmt(hasSingleDecl(varDecl(matchesName("__end";
+EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+  }
 
   Code = R"cpp(
   void rangeFor()



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 0dd8f6f - [ClangTidy] NFC: Add more tests for container-size-empty

2020-12-15 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-12-15T23:27:38Z
New Revision: 0dd8f6f9035408fc18b4351ae4096a2852aa8fb5

URL: 
https://github.com/llvm/llvm-project/commit/0dd8f6f9035408fc18b4351ae4096a2852aa8fb5
DIFF: 
https://github.com/llvm/llvm-project/commit/0dd8f6f9035408fc18b4351ae4096a2852aa8fb5.diff

LOG: [ClangTidy] NFC: Add more tests for container-size-empty

Added: 


Modified: 

clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp

Removed: 




diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
index c67d9275bbfb..9100559233e3 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability-container-size-empty.cpp
@@ -100,7 +100,12 @@ std::string s_func() {
   return std::string();
 }
 
-int main() {
+void takesBool(bool)
+{
+
+}
+
+bool returnsBool() {
   std::set intSet;
   std::string str;
   std::string str2;
@@ -397,6 +402,42 @@ int main() {
 ;
   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
   // CHECK-FIXES: {{^  }}if (derived.empty()){{$}}
+
+  takesBool(derived.size());
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}takesBool(!derived.empty());
+
+  takesBool(derived.size() == 0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}takesBool(derived.empty());
+
+  takesBool(derived.size() != 0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}takesBool(!derived.empty());
+
+  bool b1 = derived.size();
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}bool b1 = !derived.empty();
+
+  bool b2(derived.size());
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}bool b2(!derived.empty());
+
+  auto b3 = static_cast(derived.size());
+  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}auto b3 = static_cast(!derived.empty());
+
+  auto b4 = (bool)derived.size();
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}auto b4 = (bool)!derived.empty();
+
+  auto b5 = bool(derived.size());
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}auto b5 = bool(!derived.empty());
+
+  return derived.size();
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be 
used
+  // CHECK-FIXES: {{^  }}return !derived.empty();
 }
 
 #define CHECKSIZE(x) if (x.size()) {}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 702f822 - [ASTMatcher] Avoid isImplicit call on object which could be nullptr

2020-12-15 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-12-15T23:27:38Z
New Revision: 702f822ca5bbff96401e9ede39f4ae5c7cbc6b05

URL: 
https://github.com/llvm/llvm-project/commit/702f822ca5bbff96401e9ede39f4ae5c7cbc6b05
DIFF: 
https://github.com/llvm/llvm-project/commit/702f822ca5bbff96401e9ede39f4ae5c7cbc6b05.diff

LOG: [ASTMatcher] Avoid isImplicit call on object which could be nullptr

A callExpr whose argument is dependent has a null getCalleeDecl().

Differential Revision: https://reviews.llvm.org/D93324

Added: 


Modified: 
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index fdc4f66f5d9c7..46de4093272d1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1064,10 +1064,11 @@ class HasDeclarationMatcher : public 
MatcherInterface {
   /// is \c NULL.
   bool matchesDecl(const Decl *Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const {
-if (Finder->isTraversalIgnoringImplicitNodes() && Node->isImplicit())
-  return false;
-return Node != nullptr && this->InnerMatcher.matches(
-  DynTypedNode::create(*Node), Finder, 
Builder);
+return Node != nullptr &&
+   !(Finder->isTraversalIgnoringImplicitNodes() &&
+ Node->isImplicit()) &&
+   this->InnerMatcher.matches(DynTypedNode::create(*Node), Finder,
+  Builder);
   }
 };
 

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 6f1ed0491ba1f..10d2d6ec3916a 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1935,6 +1935,18 @@ void bar()
   matches(Code, callExpr(traverse(TK_IgnoreUnlessSpelledInSource,
   hasAnyArgument(floatLiteral());
 
+  EXPECT_TRUE(matches(
+  R"cpp(
+void takesBool(bool){}
+
+template 
+void neverInstantiatedTemplate() {
+  takesBool(T{});
+}
+)cpp",
+  traverse(TK_IgnoreUnlessSpelledInSource,
+   callExpr(unless(callExpr(hasDeclaration(functionDecl(;
+
   EXPECT_TRUE(
   matches(VarDeclCode, varDecl(traverse(TK_IgnoreUnlessSpelledInSource,
 hasType(builtinType());



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 76bd444 - Fix tests for clang-query completion

2020-11-23 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-11-23T15:23:13Z
New Revision: 76bde36197465f1c72f4b6f1d59721012a59

URL: 
https://github.com/llvm/llvm-project/commit/76bde36197465f1c72f4b6f1d59721012a59
DIFF: 
https://github.com/llvm/llvm-project/commit/76bde36197465f1c72f4b6f1d59721012a59.diff

LOG: Fix tests for clang-query completion

Added: 


Modified: 
clang-tools-extra/unittests/clang-query/QueryParserTest.cpp

Removed: 




diff  --git a/clang-tools-extra/unittests/clang-query/QueryParserTest.cpp 
b/clang-tools-extra/unittests/clang-query/QueryParserTest.cpp
index 4a0a80146af4..78d6f593777d 100644
--- a/clang-tools-extra/unittests/clang-query/QueryParserTest.cpp
+++ b/clang-tools-extra/unittests/clang-query/QueryParserTest.cpp
@@ -232,14 +232,12 @@ TEST_F(QueryParserTest, Complete) {
   EXPECT_EQ("dump", Comps[3].DisplayText);
 
   Comps = QueryParser::complete("set traversal ", 14, QS);
-  ASSERT_EQ(3u, Comps.size());
+  ASSERT_EQ(2u, Comps.size());
 
   EXPECT_EQ("AsIs ", Comps[0].TypedText);
   EXPECT_EQ("AsIs", Comps[0].DisplayText);
-  EXPECT_EQ("IgnoreImplicitCastsAndParentheses ", Comps[1].TypedText);
-  EXPECT_EQ("IgnoreImplicitCastsAndParentheses", Comps[1].DisplayText);
-  EXPECT_EQ("IgnoreUnlessSpelledInSource ", Comps[2].TypedText);
-  EXPECT_EQ("IgnoreUnlessSpelledInSource", Comps[2].DisplayText);
+  EXPECT_EQ("IgnoreUnlessSpelledInSource ", Comps[1].TypedText);
+  EXPECT_EQ("IgnoreUnlessSpelledInSource", Comps[1].DisplayText);
 
   Comps = QueryParser::complete("match while", 11, QS);
   ASSERT_EQ(1u, Comps.size());



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 5e18018 - Remove the IgnoreImplicitCastsAndParentheses traversal kind

2020-11-23 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-11-23T14:27:48Z
New Revision: 5e1801813d93210acae84ff3c68a01512c2df9bc

URL: 
https://github.com/llvm/llvm-project/commit/5e1801813d93210acae84ff3c68a01512c2df9bc
DIFF: 
https://github.com/llvm/llvm-project/commit/5e1801813d93210acae84ff3c68a01512c2df9bc.diff

LOG: Remove the IgnoreImplicitCastsAndParentheses traversal kind

Differential Revision: https://reviews.llvm.org/D91918

Added: 


Modified: 
clang-tools-extra/clang-query/QueryParser.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/AST/ASTNodeTraverser.h
clang/include/clang/AST/ASTTypeTraits.h
clang/lib/AST/ParentMapContext.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-query/QueryParser.cpp 
b/clang-tools-extra/clang-query/QueryParser.cpp
index 2f1965e77ab4..45a0d425b7c2 100644
--- a/clang-tools-extra/clang-query/QueryParser.cpp
+++ b/clang-tools-extra/clang-query/QueryParser.cpp
@@ -134,8 +134,6 @@ QueryRef QueryParser::parseSetTraversalKind(
   unsigned Value =
   LexOrCompleteWord(this, ValStr)
   .Case("AsIs", ast_type_traits::TK_AsIs)
-  .Case("IgnoreImplicitCastsAndParentheses",
-ast_type_traits::TK_IgnoreImplicitCastsAndParentheses)
   .Case("IgnoreUnlessSpelledInSource",
 ast_type_traits::TK_IgnoreUnlessSpelledInSource)
   .Default(~0u);

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 5e78de2b0edc..c99a589b1212 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -57,7 +57,7 @@ The improvements are...
 Improvements to clang-query
 ---
 
-The improvements are...
+- The IgnoreImplicitCastsAndParentheses traversal mode has been removed.
 
 Improvements to clang-rename
 

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 737f165c80cb..d62c62dad3d2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -234,6 +234,9 @@ AST Matchers
   has been changed to no longer match on template instantiations or on
   implicit nodes which are not spelled in the source.
 
+- The TK_IgnoreImplicitCastsAndParentheses traversal kind was removed. It
+  is recommended to use TK_IgnoreUnlessSpelledInSource instead.
+
 - The behavior of the forEach() matcher was changed to not internally ignore
   implicit and parenthesis nodes.
 

diff  --git a/clang/include/clang/AST/ASTNodeTraverser.h 
b/clang/include/clang/AST/ASTNodeTraverser.h
index 4f33b0c67e94..6f7affe66273 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -126,9 +126,6 @@ class ASTNodeTraverser
 switch (Traversal) {
 case TK_AsIs:
   break;
-case TK_IgnoreImplicitCastsAndParentheses:
-  S = E->IgnoreParenImpCasts();
-  break;
 case TK_IgnoreUnlessSpelledInSource:
   S = E->IgnoreUnlessSpelledInSource();
   break;

diff  --git a/clang/include/clang/AST/ASTTypeTraits.h 
b/clang/include/clang/AST/ASTTypeTraits.h
index bd817b75bb84..034260cc8ed3 100644
--- a/clang/include/clang/AST/ASTTypeTraits.h
+++ b/clang/include/clang/AST/ASTTypeTraits.h
@@ -40,10 +40,6 @@ enum TraversalKind {
   /// Will traverse all child nodes.
   TK_AsIs,
 
-  /// Will not traverse implicit casts and parentheses.
-  /// Corresponds to Expr::IgnoreParenImpCasts()
-  TK_IgnoreImplicitCastsAndParentheses,
-
   /// Ignore AST nodes not written in the source
   TK_IgnoreUnlessSpelledInSource
 };
@@ -542,8 +538,6 @@ using ASTNodeKind = ::clang::ASTNodeKind;
 using TraversalKind = ::clang::TraversalKind;
 
 constexpr TraversalKind TK_AsIs = ::clang::TK_AsIs;
-constexpr TraversalKind TK_IgnoreImplicitCastsAndParentheses =
-::clang::TK_IgnoreImplicitCastsAndParentheses;
 constexpr TraversalKind TK_IgnoreUnlessSpelledInSource =
 ::clang::TK_IgnoreUnlessSpelledInSource;
 } // namespace ast_type_traits

diff  --git a/clang/lib/AST/ParentMapContext.cpp 
b/clang/lib/AST/ParentMapContext.cpp
index c80c8bc23e00..cb4995312efa 100644
--- a/clang/lib/AST/ParentMapContext.cpp
+++ b/clang/lib/AST/ParentMapContext.cpp
@@ -36,8 +36,6 @@ Expr *ParentMapContext::traverseIgnored(Expr *E) const {
   switch (Traversal) {
   case TK_AsIs:
 return E;
-  case TK_IgnoreImplicitCastsAndParentheses:
-return E->IgnoreParenImpCasts();
   case TK_IgnoreUnlessSpelledInSource:
 return E->IgnoreUnlessSpelledInSource();
   }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] f052cf4 - Update mode used in traverse() examples

2020-11-23 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-11-23T14:27:48Z
New Revision: f052cf494f07a33af5aa7c680cfe0bfcca24beae

URL: 
https://github.com/llvm/llvm-project/commit/f052cf494f07a33af5aa7c680cfe0bfcca24beae
DIFF: 
https://github.com/llvm/llvm-project/commit/f052cf494f07a33af5aa7c680cfe0bfcca24beae.diff

LOG: Update mode used in traverse() examples

traverse() predates the IgnoreUnlessSpelledInSource mode. Update example
and test code to use the newer mode.

Differential Revision: https://reviews.llvm.org/D91917

Added: 


Modified: 
clang/docs/LibASTMatchersReference.html
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index fbc53ed743a2..9f14ab9f4c5b 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -5567,7 +5567,7 @@ AST Traversal Matchers
   int i = 3.0;
   }
 The matcher
-  traverse(TK_IgnoreImplicitCastsAndParentheses,
+  traverse(TK_IgnoreUnlessSpelledInSource,
 varDecl(hasInitializer(floatLiteral().bind("init")))
   )
 matches the variable declaration with "init" bound to the "3.0".

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index d8b049f45341..0da469ea0f78 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -782,7 +782,7 @@ AST_POLYMORPHIC_MATCHER_P(
 /// \endcode
 /// The matcher
 /// \code
-///   traverse(TK_IgnoreImplicitCastsAndParentheses,
+///   traverse(TK_IgnoreUnlessSpelledInSource,
 /// varDecl(hasInitializer(floatLiteral().bind("init")))
 ///   )
 /// \endcode

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp 
b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 004c667f053d..b0ec1719daaf 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1873,8 +1873,8 @@ void foo()
   auto Matcher = varDecl(hasInitializer(floatLiteral()));
 
   EXPECT_TRUE(notMatches(VarDeclCode, traverse(TK_AsIs, Matcher)));
-  EXPECT_TRUE(matches(VarDeclCode,
-  traverse(TK_IgnoreImplicitCastsAndParentheses, 
Matcher)));
+  EXPECT_TRUE(
+  matches(VarDeclCode, traverse(TK_IgnoreUnlessSpelledInSource, Matcher)));
 
   auto ParentMatcher = floatLiteral(hasParent(varDecl(hasName("i";
 
@@ -2715,14 +2715,14 @@ void foo()
 )cpp";
 
   EXPECT_TRUE(
-  matches(Code, traverse(TK_IgnoreImplicitCastsAndParentheses,
+  matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
  callExpr(has(callExpr(traverse(
  TK_AsIs, callExpr(has(implicitCastExpr(
   has(floatLiteral(;
 
   EXPECT_TRUE(matches(
   Code,
-  traverse(TK_IgnoreImplicitCastsAndParentheses,
+  traverse(TK_IgnoreUnlessSpelledInSource,
traverse(TK_AsIs, implicitCastExpr(has(floatLiteral()));
 }
 
@@ -2738,8 +2738,7 @@ void constructImplicit() {
 }
   )cpp";
 
-  auto Matcher =
-  traverse(TK_IgnoreImplicitCastsAndParentheses, implicitCastExpr());
+  auto Matcher = traverse(TK_IgnoreUnlessSpelledInSource, implicitCastExpr());
 
   // Verfiy that it does not segfault
   EXPECT_FALSE(matches(Code, Matcher));
@@ -2766,7 +2765,7 @@ void foo()
   EXPECT_TRUE(matches(
   Code,
   functionDecl(anyOf(hasDescendant(Matcher),
- traverse(TK_IgnoreImplicitCastsAndParentheses,
+ traverse(TK_IgnoreUnlessSpelledInSource,
   functionDecl(hasDescendant(Matcher)));
 }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 72a9f36 - Remove automatic traversal from forEach matcher

2020-11-23 Thread Stephen Kelly via llvm-branch-commits

Author: Stephen Kelly
Date: 2020-11-23T14:27:47Z
New Revision: 72a9f365e9933d68645f796592932a27d11bbfd0

URL: 
https://github.com/llvm/llvm-project/commit/72a9f365e9933d68645f796592932a27d11bbfd0
DIFF: 
https://github.com/llvm/llvm-project/commit/72a9f365e9933d68645f796592932a27d11bbfd0.diff

LOG: Remove automatic traversal from forEach matcher

Differential Revision: https://reviews.llvm.org/D91916

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersInternalTest.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8da9490f7b6f..737f165c80cb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -234,6 +234,9 @@ AST Matchers
   has been changed to no longer match on template instantiations or on
   implicit nodes which are not spelled in the source.
 
+- The behavior of the forEach() matcher was changed to not internally ignore
+  implicit and parenthesis nodes.
+
 clang-format
 
 

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h 
b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index 480a12b0dafb..fdc4f66f5d9c 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -679,8 +679,7 @@ class ASTMatchFinder {
 
   template 
   bool matchesChildOf(const T , const DynTypedMatcher ,
-  BoundNodesTreeBuilder *Builder, TraversalKind Traverse,
-  BindKind Bind) {
+  BoundNodesTreeBuilder *Builder, BindKind Bind) {
 static_assert(std::is_base_of::value ||
   std::is_base_of::value ||
   std::is_base_of::value ||
@@ -689,7 +688,7 @@ class ASTMatchFinder {
   std::is_base_of::value,
   "unsupported type for recursive matching");
 return matchesChildOf(DynTypedNode::create(Node), getASTContext(), Matcher,
-  Builder, Traverse, Bind);
+  Builder, Bind);
   }
 
   template 
@@ -730,7 +729,7 @@ class ASTMatchFinder {
   virtual bool matchesChildOf(const DynTypedNode , ASTContext ,
   const DynTypedMatcher ,
   BoundNodesTreeBuilder *Builder,
-  TraversalKind Traverse, BindKind Bind) = 0;
+  BindKind Bind) = 0;
 
   virtual bool matchesDescendantOf(const DynTypedNode , ASTContext ,
const DynTypedMatcher ,
@@ -1367,7 +1366,6 @@ class HasMatcher : public MatcherInterface {
   bool matches(const T , ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override {
 return Finder->matchesChildOf(Node, this->InnerMatcher, Builder,
-  TraversalKind::TK_AsIs,
   ASTMatchFinder::BK_First);
   }
 };
@@ -1392,7 +1390,6 @@ class ForEachMatcher : public MatcherInterface {
BoundNodesTreeBuilder *Builder) const override {
 return Finder->matchesChildOf(
 Node, this->InnerMatcher, Builder,
-TraversalKind::TK_IgnoreImplicitCastsAndParentheses,
 ASTMatchFinder::BK_All);
   }
 };

diff  --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp 
b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index e43778b4fe8f..cc9537144524 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -95,12 +95,11 @@ class MatchChildASTVisitor
   // matching the descendants.
   MatchChildASTVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder, int MaxDepth,
-   TraversalKind Traversal, bool IgnoreImplicitChildren,
+   bool IgnoreImplicitChildren,
ASTMatchFinder::BindKind Bind)
   : Matcher(Matcher), Finder(Finder), Builder(Builder), CurrentDepth(0),
-MaxDepth(MaxDepth), Traversal(Traversal),
-IgnoreImplicitChildren(IgnoreImplicitChildren), Bind(Bind),
-Matches(false) {}
+MaxDepth(MaxDepth), IgnoreImplicitChildren(IgnoreImplicitChildren),
+Bind(Bind), Matches(false) {}
 
   // Returns true if a match is found in the subtree rooted at the
   // given AST node. This is done via a set of mutually recursive
@@ -168,10 +167,6 @@ class MatchChildASTVisitor
 Finder->getASTContext().getParentMapContext().traverseIgnored(
 ExprNode);
 }
-if (Traversal == TraversalKind::TK_IgnoreImplicitCastsAndParentheses) {
-  if (Expr *ExprNode = dyn_cast_or_null(StmtNode))
-StmtToTraverse =