[clang] cc6113d - [OpenACC] Fix ast-print for OpenACC Clauses
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 30 08:54:25 PDT 2024
Author: erichkeane
Date: 2024-04-30T08:54:19-07:00
New Revision: cc6113da826e82f19762eb813e6d932fcef09593
URL: https://github.com/llvm/llvm-project/commit/cc6113da826e82f19762eb813e6d932fcef09593
DIFF: https://github.com/llvm/llvm-project/commit/cc6113da826e82f19762eb813e6d932fcef09593.diff
LOG: [OpenACC] Fix ast-print for OpenACC Clauses
Previously we weren't printing expressions correctly, so this patch adds
a test to ensure we do, and fixes how expressions are printed.
Added:
clang/test/AST/ast-print-openacc-compute-construct.cpp
Modified:
clang/include/clang/AST/OpenACCClause.h
clang/lib/AST/OpenACCClause.cpp
clang/lib/AST/StmtPrinter.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/OpenACCClause.h b/clang/include/clang/AST/OpenACCClause.h
index 277a351c49fcb8..704f15ff60edb2 100644
--- a/clang/include/clang/AST/OpenACCClause.h
+++ b/clang/include/clang/AST/OpenACCClause.h
@@ -299,6 +299,9 @@ template <class Impl> class OpenACCClauseVisitor {
class OpenACCClausePrinter final
: public OpenACCClauseVisitor<OpenACCClausePrinter> {
raw_ostream &OS;
+ const PrintingPolicy &Policy;
+
+ void printExpr(const Expr *E);
public:
void VisitClauseList(ArrayRef<const OpenACCClause *> List) {
@@ -309,7 +312,8 @@ class OpenACCClausePrinter final
OS << ' ';
}
}
- OpenACCClausePrinter(raw_ostream &OS) : OS(OS) {}
+ OpenACCClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
+ : OS(OS), Policy(Policy) {}
#define VISIT_CLAUSE(CLAUSE_NAME) \
void Visit##CLAUSE_NAME##Clause(const OpenACC##CLAUSE_NAME##Clause &Clause);
diff --git a/clang/lib/AST/OpenACCClause.cpp b/clang/lib/AST/OpenACCClause.cpp
index 6cd5b28802187d..b59578d80ed820 100644
--- a/clang/lib/AST/OpenACCClause.cpp
+++ b/clang/lib/AST/OpenACCClause.cpp
@@ -137,32 +137,47 @@ OpenACCNumGangsClause *OpenACCNumGangsClause::Create(const ASTContext &C,
//===----------------------------------------------------------------------===//
// OpenACC clauses printing methods
//===----------------------------------------------------------------------===//
+
+void OpenACCClausePrinter::printExpr(const Expr *E) {
+ E->printPretty(OS, nullptr, Policy, 0);
+}
+
void OpenACCClausePrinter::VisitDefaultClause(const OpenACCDefaultClause &C) {
OS << "default(" << C.getDefaultClauseKind() << ")";
}
void OpenACCClausePrinter::VisitIfClause(const OpenACCIfClause &C) {
- OS << "if(" << C.getConditionExpr() << ")";
+ OS << "if(";
+ printExpr(C.getConditionExpr());
+ OS << ")";
}
void OpenACCClausePrinter::VisitSelfClause(const OpenACCSelfClause &C) {
OS << "self";
- if (const Expr *CondExpr = C.getConditionExpr())
- OS << "(" << CondExpr << ")";
+ if (const Expr *CondExpr = C.getConditionExpr()) {
+ OS << "(";
+ printExpr(CondExpr);
+ OS << ")";
+ }
}
void OpenACCClausePrinter::VisitNumGangsClause(const OpenACCNumGangsClause &C) {
OS << "num_gangs(";
- llvm::interleaveComma(C.getIntExprs(), OS);
+ llvm::interleaveComma(C.getIntExprs(), OS,
+ [&](const Expr *E) { printExpr(E); });
OS << ")";
}
void OpenACCClausePrinter::VisitNumWorkersClause(
const OpenACCNumWorkersClause &C) {
- OS << "num_workers(" << C.getIntExpr() << ")";
+ OS << "num_workers(";
+ printExpr(C.getIntExpr());
+ OS << ")";
}
void OpenACCClausePrinter::VisitVectorLengthClause(
const OpenACCVectorLengthClause &C) {
- OS << "vector_length(" << C.getIntExpr() << ")";
+ OS << "vector_length(";
+ printExpr(C.getIntExpr());
+ OS << ")";
}
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index f010d36513a49e..be2d5a2eb6b46d 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -1148,9 +1148,10 @@ void StmtPrinter::VisitOpenACCComputeConstruct(OpenACCComputeConstruct *S) {
if (!S->clauses().empty()) {
OS << ' ';
- OpenACCClausePrinter Printer(OS);
+ OpenACCClausePrinter Printer(OS, Policy);
Printer.VisitClauseList(S->clauses());
}
+ OS << '\n';
PrintStmt(S->getStructuredBlock());
}
diff --git a/clang/test/AST/ast-print-openacc-compute-construct.cpp b/clang/test/AST/ast-print-openacc-compute-construct.cpp
new file mode 100644
index 00000000000000..a0e4294426a7fa
--- /dev/null
+++ b/clang/test/AST/ast-print-openacc-compute-construct.cpp
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -fopenacc -ast-print %s -o - | FileCheck %s
+
+void foo() {
+ int i;
+ float array[5];
+// CHECK: #pragma acc parallel default(none)
+// CHECK-NEXT: while (true)
+#pragma acc parallel default(none)
+ while(true);
+// CHECK: #pragma acc serial default(present)
+// CHECK-NEXT: while (true)
+#pragma acc serial default(present)
+ while(true);
+// CHECK: #pragma acc kernels if(i == array[1])
+// CHECK-NEXT: while (true)
+#pragma acc kernels if(i == array[1])
+ while(true);
+// CHECK: #pragma acc parallel self(i == 3)
+// CHECK-NEXT: while (true)
+#pragma acc parallel self(i == 3)
+ while(true);
+
+// CHECK: #pragma acc parallel num_gangs(i, (int)array[2])
+// CHECK-NEXT: while (true)
+#pragma acc parallel num_gangs(i, (int)array[2])
+ while(true);
+
+// CHECK: #pragma acc parallel num_workers(i)
+// CHECK-NEXT: while (true)
+#pragma acc parallel num_workers(i)
+ while(true);
+
+// CHECK: #pragma acc parallel vector_length((int)array[1])
+// CHECK-NEXT: while (true)
+#pragma acc parallel vector_length((int)array[1])
+ while(true);
+
+}
+
More information about the cfe-commits
mailing list