[PATCH] D143877: [NFC] [clang] Forward forwarding reference

David Blaikie via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 27 17:27:45 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG58ec6e09abe8: [NFC] [clang] Forward forwarding reference (authored by ccotter, committed by dblaikie).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143877/new/

https://reviews.llvm.org/D143877

Files:
  clang/include/clang/AST/IgnoreExpr.h
  clang/unittests/AST/ASTExprTest.cpp
  clang/unittests/AST/CMakeLists.txt


Index: clang/unittests/AST/CMakeLists.txt
===================================================================
--- clang/unittests/AST/CMakeLists.txt
+++ clang/unittests/AST/CMakeLists.txt
@@ -7,6 +7,7 @@
 
 add_clang_unittest(ASTTests
   ASTContextParentMapTest.cpp
+  ASTExprTest.cpp
   ASTImporterFixtures.cpp
   ASTImporterTest.cpp
   ASTImporterObjCTest.cpp
Index: clang/unittests/AST/ASTExprTest.cpp
===================================================================
--- /dev/null
+++ clang/unittests/AST/ASTExprTest.cpp
@@ -0,0 +1,58 @@
+//===- unittests/AST/ASTExprTest.cpp --- AST Expr tests -------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains tests for AST Expr related methods.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ASTPrint.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/IgnoreExpr.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Tooling/Tooling.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+
+TEST(ASTExpr, IgnoreExprCallbackForwarded) {
+  constexpr char Code[] = "";
+  auto AST = tooling::buildASTFromCodeWithArgs(Code, /*Args=*/{"-std=c++20"});
+  ASTContext &Ctx = AST->getASTContext();
+
+  auto createIntLiteral = [&](uint32_t Value) -> IntegerLiteral * {
+    const int numBits = 32;
+    return IntegerLiteral::Create(Ctx, llvm::APInt(numBits, Value),
+                                  Ctx.UnsignedIntTy, {});
+  };
+
+  struct IgnoreParens {
+    Expr *operator()(Expr *E) & { return nullptr; }
+    Expr *operator()(Expr *E) && {
+      if (auto *PE = dyn_cast<ParenExpr>(E)) {
+        return PE->getSubExpr();
+      }
+      return E;
+    }
+  };
+
+  {
+    auto *IntExpr = createIntLiteral(10);
+    ParenExpr *PE =
+        new (Ctx) ParenExpr(SourceLocation{}, SourceLocation{}, IntExpr);
+    EXPECT_EQ(IntExpr, IgnoreExprNodes(PE, IgnoreParens{}));
+  }
+
+  {
+    IgnoreParens CB{};
+    auto *IntExpr = createIntLiteral(10);
+    ParenExpr *PE =
+        new (Ctx) ParenExpr(SourceLocation{}, SourceLocation{}, IntExpr);
+    EXPECT_EQ(nullptr, IgnoreExprNodes(PE, CB));
+  }
+}
Index: clang/include/clang/AST/IgnoreExpr.h
===================================================================
--- clang/include/clang/AST/IgnoreExpr.h
+++ clang/include/clang/AST/IgnoreExpr.h
@@ -23,7 +23,8 @@
 inline Expr *IgnoreExprNodesImpl(Expr *E) { return E; }
 template <typename FnTy, typename... FnTys>
 Expr *IgnoreExprNodesImpl(Expr *E, FnTy &&Fn, FnTys &&... Fns) {
-  return IgnoreExprNodesImpl(Fn(E), std::forward<FnTys>(Fns)...);
+  return IgnoreExprNodesImpl(std::forward<FnTy>(Fn)(E),
+                             std::forward<FnTys>(Fns)...);
 }
 } // namespace detail
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143877.500982.patch
Type: text/x-patch
Size: 3034 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230228/e368aa4b/attachment-0001.bin>


More information about the cfe-commits mailing list