[clang] 81aa66f - Extract infrastructure to ignore intermediate expressions into `clang/AST/IgnoreExpr.h`
Eduardo Caldas via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 7 02:32:50 PDT 2020
Author: Eduardo Caldas
Date: 2020-09-07T09:32:30Z
New Revision: 81aa66f65f504af18982baa078a5f3f7d2aa88fa
URL: https://github.com/llvm/llvm-project/commit/81aa66f65f504af18982baa078a5f3f7d2aa88fa
DIFF: https://github.com/llvm/llvm-project/commit/81aa66f65f504af18982baa078a5f3f7d2aa88fa.diff
LOG: Extract infrastructure to ignore intermediate expressions into `clang/AST/IgnoreExpr.h`
Rationale:
This allows users to use `IgnoreExprNodes` and `Ignore*SingleStep` outside of
`clang/AST/Expr.cpp`.
Minor:
Rename `IgnoreImp...SingleStep` into `IgnoreImplicit...SingleStep`.
Differential Revision: https://reviews.llvm.org/D86778
Added:
clang/include/clang/AST/IgnoreExpr.h
clang/lib/AST/IgnoreExpr.cpp
Modified:
clang/lib/AST/CMakeLists.txt
clang/lib/AST/Expr.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/IgnoreExpr.h b/clang/include/clang/AST/IgnoreExpr.h
new file mode 100644
index 000000000000..15d31f3af995
--- /dev/null
+++ b/clang/include/clang/AST/IgnoreExpr.h
@@ -0,0 +1,61 @@
+//===--- IgnoreExpr.h - Ignore intermediate Expressions -----------------===//
+//
+// 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 defines common functions to ignore intermediate expression nodes
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_IGNOREEXPR_H
+#define LLVM_CLANG_AST_IGNOREEXPR_H
+
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace detail {
+/// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
+/// Return Fn_n(...(Fn_1(E)))
+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)...);
+}
+} // namespace detail
+
+/// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
+/// Recursively apply each of the functions to E until reaching a fixed point.
+/// Note that a null E is valid; in this case nothing is done.
+template <typename... FnTys> Expr *IgnoreExprNodes(Expr *E, FnTys &&... Fns) {
+ Expr *LastE = nullptr;
+ while (E != LastE) {
+ LastE = E;
+ E = detail::IgnoreExprNodesImpl(E, std::forward<FnTys>(Fns)...);
+ }
+ return E;
+}
+
+Expr *IgnoreImplicitCastsSingleStep(Expr *E);
+
+Expr *IgnoreImplicitCastsExtraSingleStep(Expr *E);
+
+Expr *IgnoreCastsSingleStep(Expr *E);
+
+Expr *IgnoreLValueCastsSingleStep(Expr *E);
+
+Expr *IgnoreBaseCastsSingleStep(Expr *E);
+
+Expr *IgnoreImplicitSingleStep(Expr *E);
+
+Expr *IgnoreImplicitAsWrittenSingleStep(Expr *E);
+
+Expr *IgnoreParensOnlySingleStep(Expr *E);
+
+Expr *IgnoreParensSingleStep(Expr *E);
+
+} // namespace clang
+
+#endif // LLVM_CLANG_AST_IGNOREEXPR_H
diff --git a/clang/lib/AST/CMakeLists.txt b/clang/lib/AST/CMakeLists.txt
index 35099fd0dacf..dfd26fd97bc6 100644
--- a/clang/lib/AST/CMakeLists.txt
+++ b/clang/lib/AST/CMakeLists.txt
@@ -55,6 +55,7 @@ add_clang_library(clangAST
ExternalASTMerger.cpp
ExternalASTSource.cpp
FormatString.cpp
+ IgnoreExpr.cpp
InheritViz.cpp
Interp/ByteCodeEmitter.cpp
Interp/ByteCodeExprGen.cpp
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 8efd6837c541..1029acbf68cd 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -21,6 +21,7 @@
#include "clang/AST/DependenceFlags.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/ExprCXX.h"
+#include "clang/AST/IgnoreExpr.h"
#include "clang/AST/Mangle.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/StmtVisitor.h"
@@ -2779,118 +2780,6 @@ QualType Expr::findBoundMemberType(const Expr *expr) {
return QualType();
}
-static Expr *IgnoreImpCastsSingleStep(Expr *E) {
- if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
- return ICE->getSubExpr();
-
- if (auto *FE = dyn_cast<FullExpr>(E))
- return FE->getSubExpr();
-
- return E;
-}
-
-static Expr *IgnoreImpCastsExtraSingleStep(Expr *E) {
- // FIXME: Skip MaterializeTemporaryExpr and SubstNonTypeTemplateParmExpr in
- // addition to what IgnoreImpCasts() skips to account for the current
- // behaviour of IgnoreParenImpCasts().
- Expr *SubE = IgnoreImpCastsSingleStep(E);
- if (SubE != E)
- return SubE;
-
- if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
- return MTE->getSubExpr();
-
- if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
- return NTTP->getReplacement();
-
- return E;
-}
-
-static Expr *IgnoreCastsSingleStep(Expr *E) {
- if (auto *CE = dyn_cast<CastExpr>(E))
- return CE->getSubExpr();
-
- if (auto *FE = dyn_cast<FullExpr>(E))
- return FE->getSubExpr();
-
- if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
- return MTE->getSubExpr();
-
- if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
- return NTTP->getReplacement();
-
- return E;
-}
-
-static Expr *IgnoreLValueCastsSingleStep(Expr *E) {
- // Skip what IgnoreCastsSingleStep skips, except that only
- // lvalue-to-rvalue casts are skipped.
- if (auto *CE = dyn_cast<CastExpr>(E))
- if (CE->getCastKind() != CK_LValueToRValue)
- return E;
-
- return IgnoreCastsSingleStep(E);
-}
-
-static Expr *IgnoreBaseCastsSingleStep(Expr *E) {
- if (auto *CE = dyn_cast<CastExpr>(E))
- if (CE->getCastKind() == CK_DerivedToBase ||
- CE->getCastKind() == CK_UncheckedDerivedToBase ||
- CE->getCastKind() == CK_NoOp)
- return CE->getSubExpr();
-
- return E;
-}
-
-static Expr *IgnoreImplicitSingleStep(Expr *E) {
- Expr *SubE = IgnoreImpCastsSingleStep(E);
- if (SubE != E)
- return SubE;
-
- if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
- return MTE->getSubExpr();
-
- if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
- return BTE->getSubExpr();
-
- return E;
-}
-
-static Expr *IgnoreImplicitAsWrittenSingleStep(Expr *E) {
- if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
- return ICE->getSubExprAsWritten();
-
- return IgnoreImplicitSingleStep(E);
-}
-
-static Expr *IgnoreParensOnlySingleStep(Expr *E) {
- if (auto *PE = dyn_cast<ParenExpr>(E))
- return PE->getSubExpr();
- return E;
-}
-
-static Expr *IgnoreParensSingleStep(Expr *E) {
- if (auto *PE = dyn_cast<ParenExpr>(E))
- return PE->getSubExpr();
-
- if (auto *UO = dyn_cast<UnaryOperator>(E)) {
- if (UO->getOpcode() == UO_Extension)
- return UO->getSubExpr();
- }
-
- else if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) {
- if (!GSE->isResultDependent())
- return GSE->getResultExpr();
- }
-
- else if (auto *CE = dyn_cast<ChooseExpr>(E)) {
- if (!CE->isConditionDependent())
- return CE->getChosenSubExpr();
- }
-
- return E;
-}
-
static Expr *IgnoreNoopCastsSingleStep(const ASTContext &Ctx, Expr *E) {
if (auto *CE = dyn_cast<CastExpr>(E)) {
// We ignore integer <-> casts that are of the same width, ptr<->ptr and
@@ -2914,27 +2803,8 @@ static Expr *IgnoreNoopCastsSingleStep(const ASTContext &Ctx, Expr *E) {
return E;
}
-static Expr *IgnoreExprNodesImpl(Expr *E) { return E; }
-template <typename FnTy, typename... FnTys>
-static Expr *IgnoreExprNodesImpl(Expr *E, FnTy &&Fn, FnTys &&... Fns) {
- return IgnoreExprNodesImpl(Fn(E), std::forward<FnTys>(Fns)...);
-}
-
-/// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
-/// Recursively apply each of the functions to E until reaching a fixed point.
-/// Note that a null E is valid; in this case nothing is done.
-template <typename... FnTys>
-static Expr *IgnoreExprNodes(Expr *E, FnTys &&... Fns) {
- Expr *LastE = nullptr;
- while (E != LastE) {
- LastE = E;
- E = IgnoreExprNodesImpl(E, std::forward<FnTys>(Fns)...);
- }
- return E;
-}
-
Expr *Expr::IgnoreImpCasts() {
- return IgnoreExprNodes(this, IgnoreImpCastsSingleStep);
+ return IgnoreExprNodes(this, IgnoreImplicitCastsSingleStep);
}
Expr *Expr::IgnoreCasts() {
@@ -2955,7 +2825,7 @@ Expr *Expr::IgnoreParens() {
Expr *Expr::IgnoreParenImpCasts() {
return IgnoreExprNodes(this, IgnoreParensSingleStep,
- IgnoreImpCastsExtraSingleStep);
+ IgnoreImplicitCastsExtraSingleStep);
}
Expr *Expr::IgnoreParenCasts() {
@@ -2993,7 +2863,7 @@ Expr *Expr::IgnoreUnlessSpelledInSource() {
while (E != LastE) {
LastE = E;
E = IgnoreExprNodes(E, IgnoreImplicitSingleStep,
- IgnoreImpCastsExtraSingleStep,
+ IgnoreImplicitCastsExtraSingleStep,
IgnoreParensOnlySingleStep);
auto SR = E->getSourceRange();
diff --git a/clang/lib/AST/IgnoreExpr.cpp b/clang/lib/AST/IgnoreExpr.cpp
new file mode 100644
index 000000000000..65aaaeb6a1ed
--- /dev/null
+++ b/clang/lib/AST/IgnoreExpr.cpp
@@ -0,0 +1,129 @@
+//===--- IgnoreExpr.cpp - Ignore intermediate Expressions -----------------===//
+//
+// 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 implements common functions to ignore intermediate expression nodes
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/IgnoreExpr.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+
+using namespace clang;
+
+Expr *clang::IgnoreImplicitCastsSingleStep(Expr *E) {
+ if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
+ return ICE->getSubExpr();
+
+ if (auto *FE = dyn_cast<FullExpr>(E))
+ return FE->getSubExpr();
+
+ return E;
+}
+
+Expr *clang::IgnoreImplicitCastsExtraSingleStep(Expr *E) {
+ // FIXME: Skip MaterializeTemporaryExpr and SubstNonTypeTemplateParmExpr in
+ // addition to what IgnoreImpCasts() skips to account for the current
+ // behaviour of IgnoreParenImpCasts().
+ Expr *SubE = IgnoreImplicitCastsSingleStep(E);
+ if (SubE != E)
+ return SubE;
+
+ if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
+ return MTE->getSubExpr();
+
+ if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
+ return NTTP->getReplacement();
+
+ return E;
+}
+
+Expr *clang::IgnoreCastsSingleStep(Expr *E) {
+ if (auto *CE = dyn_cast<CastExpr>(E))
+ return CE->getSubExpr();
+
+ if (auto *FE = dyn_cast<FullExpr>(E))
+ return FE->getSubExpr();
+
+ if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
+ return MTE->getSubExpr();
+
+ if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
+ return NTTP->getReplacement();
+
+ return E;
+}
+
+Expr *clang::IgnoreLValueCastsSingleStep(Expr *E) {
+ // Skip what IgnoreCastsSingleStep skips, except that only
+ // lvalue-to-rvalue casts are skipped.
+ if (auto *CE = dyn_cast<CastExpr>(E))
+ if (CE->getCastKind() != CK_LValueToRValue)
+ return E;
+
+ return IgnoreCastsSingleStep(E);
+}
+
+Expr *clang::IgnoreBaseCastsSingleStep(Expr *E) {
+ if (auto *CE = dyn_cast<CastExpr>(E))
+ if (CE->getCastKind() == CK_DerivedToBase ||
+ CE->getCastKind() == CK_UncheckedDerivedToBase ||
+ CE->getCastKind() == CK_NoOp)
+ return CE->getSubExpr();
+
+ return E;
+}
+
+Expr *clang::IgnoreImplicitSingleStep(Expr *E) {
+ Expr *SubE = IgnoreImplicitCastsSingleStep(E);
+ if (SubE != E)
+ return SubE;
+
+ if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
+ return MTE->getSubExpr();
+
+ if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
+ return BTE->getSubExpr();
+
+ return E;
+}
+
+Expr *clang::IgnoreImplicitAsWrittenSingleStep(Expr *E) {
+ if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
+ return ICE->getSubExprAsWritten();
+
+ return IgnoreImplicitSingleStep(E);
+}
+
+Expr *clang::IgnoreParensOnlySingleStep(Expr *E) {
+ if (auto *PE = dyn_cast<ParenExpr>(E))
+ return PE->getSubExpr();
+ return E;
+}
+
+Expr *clang::IgnoreParensSingleStep(Expr *E) {
+ if (auto *PE = dyn_cast<ParenExpr>(E))
+ return PE->getSubExpr();
+
+ if (auto *UO = dyn_cast<UnaryOperator>(E)) {
+ if (UO->getOpcode() == UO_Extension)
+ return UO->getSubExpr();
+ }
+
+ else if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) {
+ if (!GSE->isResultDependent())
+ return GSE->getResultExpr();
+ }
+
+ else if (auto *CE = dyn_cast<ChooseExpr>(E)) {
+ if (!CE->isConditionDependent())
+ return CE->getChosenSubExpr();
+ }
+
+ return E;
+}
More information about the cfe-commits
mailing list