[clang] 58ec6e0 - [NFC] [clang] Forward forwarding reference

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


Author: Chris Cotter
Date: 2023-02-28T01:27:21Z
New Revision: 58ec6e09abe8083709802833572bca931b2d15d9

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

LOG: [NFC] [clang] Forward forwarding reference

Update function bodies to forward forwarding references.
I spotted this while authoring a clang-tidy tool for CppCoreGuideline F.19

Reviewed By: dblaikie

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

Added: 
    clang/unittests/AST/ASTExprTest.cpp

Modified: 
    clang/include/clang/AST/IgnoreExpr.h
    clang/unittests/AST/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/IgnoreExpr.h b/clang/include/clang/AST/IgnoreExpr.h
index a7e9b07bef6c4..f8d2d6c7d00c0 100644
--- a/clang/include/clang/AST/IgnoreExpr.h
+++ b/clang/include/clang/AST/IgnoreExpr.h
@@ -23,7 +23,8 @@ namespace detail {
 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
 

diff  --git a/clang/unittests/AST/ASTExprTest.cpp b/clang/unittests/AST/ASTExprTest.cpp
new file mode 100644
index 0000000000000..ec75492ccff8e
--- /dev/null
+++ b/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));
+  }
+}

diff  --git a/clang/unittests/AST/CMakeLists.txt b/clang/unittests/AST/CMakeLists.txt
index 13b945df3b589..b664b64070328 100644
--- a/clang/unittests/AST/CMakeLists.txt
+++ b/clang/unittests/AST/CMakeLists.txt
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_clang_unittest(ASTTests
   ASTContextParentMapTest.cpp
+  ASTExprTest.cpp
   ASTImporterFixtures.cpp
   ASTImporterTest.cpp
   ASTImporterObjCTest.cpp


        


More information about the cfe-commits mailing list