Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.20)
project(accparser)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Set default build type if not specified
if(NOT CMAKE_BUILD_TYPE)
Expand Down
78 changes: 46 additions & 32 deletions src/OpenACCASTConstructor.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "OpenACCASTConstructor.h"
#include "acclexer.h"
#include "accparser.h"
#include <antlr4-runtime.h>
#include <algorithm>
#include <antlr4-runtime.h>
#include <memory>

OpenACCDirective *current_directive = NULL;
OpenACCClause *current_clause = NULL;
OpenACCDirective *current_parent_directive = NULL;
OpenACCClause *current_parent_clause = NULL;
OpenACCDirective *current_directive = nullptr;
OpenACCClause *current_clause = nullptr;
OpenACCDirective *current_parent_directive = nullptr;
OpenACCClause *current_parent_clause = nullptr;

static void setOpenACCLang(OpenACCDirective *directive, bool useFortran) {
if (useFortran == true) {
Expand All @@ -18,6 +18,13 @@ static void setOpenACCLang(OpenACCDirective *directive, bool useFortran) {
}
}

static std::string stripStringLiteral(const std::string &text) {
if (text.size() < 2) {
return text;
}
return text.substr(1, text.size() - 2);
}

void OpenACCIRConstructor::enterC_prefix(accparser::C_prefixContext *ctx) {
setFortran(false);
}
Expand Down Expand Up @@ -73,8 +80,8 @@ void OpenACCIRConstructor::exitFortran_paired_directive(
->setPairedDirective(current_directive);
current_directive = current_parent_directive;
current_clause = current_parent_clause;
current_parent_directive = NULL;
current_parent_clause = NULL;
current_parent_directive = nullptr;
current_parent_clause = nullptr;
setOpenACCLang(current_directive, isFortran());
}

Expand Down Expand Up @@ -224,6 +231,9 @@ void OpenACCIRConstructor::exitName_or_string(
if (current_clause && current_clause->getKind() == ACCC_bind) {
std::string expression = trimEnclosingWhiteSpace(ctx->getText());
bool is_string = ctx->STRING_LITERAL() != nullptr;
if (is_string) {
expression = stripStringLiteral(expression);
}
static_cast<OpenACCBindClause *>(current_clause)
->setBinding(expression, is_string);
}
Expand Down Expand Up @@ -525,6 +535,22 @@ void OpenACCIRConstructor::enterGang_no_list_clause(
current_clause = current_directive->addOpenACCClause(ACCC_gang);
}

void OpenACCIRConstructor::exitGang_no_list_clause(
accparser::Gang_no_list_clauseContext *ctx) {
if (!current_clause || current_clause->getKind() != ACCC_gang) {
return;
}
if (ctx->int_expr()) {
std::string value_text =
trimEnclosingWhiteSpace(ctx->int_expr()->getText());
static_cast<OpenACCGangClause *>(current_clause)
->addArg(ACCC_GANG_ARG_dim,
OpenACCExpressionItem{value_text, ACCC_CLAUSE_SEP_comma});
}
static_cast<OpenACCGangClause *>(current_clause)
->mergeClause(current_directive, current_clause);
}

void OpenACCIRConstructor::enterHost_clause(
accparser::Host_clauseContext *ctx) {
current_clause = current_directive->addOpenACCClause(ACCC_host);
Expand Down Expand Up @@ -701,8 +727,7 @@ void OpenACCIRConstructor::enterTile_clause(
current_clause = current_directive->addOpenACCClause(ACCC_tile);
}

void OpenACCIRConstructor::exitTile_clause(
accparser::Tile_clauseContext *ctx) {
void OpenACCIRConstructor::exitTile_clause(accparser::Tile_clauseContext *ctx) {
((OpenACCTileClause *)current_clause)
->mergeClause(current_directive, current_clause);
}
Expand Down Expand Up @@ -738,13 +763,14 @@ void OpenACCIRConstructor::exitVector_clause(
accparser::Vector_clauseContext *ctx) {
// If the vector clause had a length expression, capture it
if (ctx->vector_clause_args() && ctx->vector_clause_args()->int_expr()) {
std::string expr =
trimEnclosingWhiteSpace(ctx->vector_clause_args()->int_expr()->getText());
std::string expr = trimEnclosingWhiteSpace(
ctx->vector_clause_args()->int_expr()->getText());
((OpenACCVectorClause *)current_clause)
->setLengthExpr(OpenACCExpressionItem{expr, ACCC_CLAUSE_SEP_comma});
if (((OpenACCVectorClause *)current_clause)->getModifier() ==
ACCC_VECTOR_unspecified) {
((OpenACCVectorClause *)current_clause)->setModifier(ACCC_VECTOR_expr_only);
((OpenACCVectorClause *)current_clause)
->setModifier(ACCC_VECTOR_expr_only);
}
}
((OpenACCVectorClause *)current_clause)
Expand All @@ -759,8 +785,7 @@ void OpenACCIRConstructor::enterVector_length_clause(
void OpenACCIRConstructor::exitVector_length_clause(
accparser::Vector_length_clauseContext *ctx) {
if (ctx->int_expr()) {
std::string expr =
trimEnclosingWhiteSpace(ctx->int_expr()->getText());
std::string expr = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
((OpenACCVectorLengthClause *)current_clause)
->setLengthExpr(OpenACCExpressionItem{expr, ACCC_CLAUSE_SEP_comma});
}
Expand Down Expand Up @@ -841,13 +866,14 @@ void OpenACCIRConstructor::exitWorker_clause_modifier(
void OpenACCIRConstructor::exitWorker_clause(
accparser::Worker_clauseContext *ctx) {
if (ctx->worker_clause_args() && ctx->worker_clause_args()->int_expr()) {
std::string expr =
trimEnclosingWhiteSpace(ctx->worker_clause_args()->int_expr()->getText());
std::string expr = trimEnclosingWhiteSpace(
ctx->worker_clause_args()->int_expr()->getText());
((OpenACCWorkerClause *)current_clause)
->setNumExpr(OpenACCExpressionItem{expr, ACCC_CLAUSE_SEP_comma});
if (((OpenACCWorkerClause *)current_clause)->getModifier() ==
ACCC_WORKER_unspecified) {
((OpenACCWorkerClause *)current_clause)->setModifier(ACCC_WORKER_expr_only);
((OpenACCWorkerClause *)current_clause)
->setModifier(ACCC_WORKER_expr_only);
}
}
((OpenACCWorkerClause *)current_clause)
Expand Down Expand Up @@ -1000,7 +1026,7 @@ OpenACCDirective *parseOpenACC(std::string source) {
parser.setBuildParseTree(true);
parser.setErrorHandler(std::make_shared<antlr4::BailErrorStrategy>());

current_directive = NULL;
current_directive = nullptr;
antlr4::tree::ParseTree *tree = parser.acc();

antlr4::tree::ParseTreeWalker walker;
Expand All @@ -1021,20 +1047,8 @@ void OpenACCIRConstructor::enterIndirect_clause(
std::string text =
trimEnclosingWhiteSpace(ctx->name_or_string()->getText());
bool is_str = ctx->name_or_string()->STRING_LITERAL() != nullptr;
// Fallback: IF lexer returns EXPR for a quoted string (e.g. in expr_clause mode),
// detect it manually to preserve semantics.
if (!is_str && text.length() >= 2) {
if ((text.front() == '"' && text.back() == '"') ||
(text.front() == '\'' && text.back() == '\'')) {
is_str = true;
}
}

if (is_str && text.length() >= 2) {
if ((text.front() == '"' && text.back() == '"') ||
(text.front() == '\'' && text.back() == '\'')) {
text = text.substr(1, text.length() - 2);
}
if (is_str) {
text = stripStringLiteral(text);
}
indirect->setValue(text, is_str);
}
Expand Down
10 changes: 6 additions & 4 deletions src/OpenACCASTConstructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class OpenACCIRConstructor : public accparserBaseListener {
enterNohost_clause(accparser::Nohost_clauseContext * /*ctx*/) override;
virtual void enterGang_no_list_clause(
accparser::Gang_no_list_clauseContext * /*ctx*/) override;
virtual void exitGang_no_list_clause(
accparser::Gang_no_list_clauseContext * /*ctx*/) override;
virtual void
enterHost_clause(accparser::Host_clauseContext * /*ctx*/) override;
virtual void enterIf_clause(accparser::If_clauseContext * /*ctx*/) override;
Expand Down Expand Up @@ -185,8 +187,8 @@ class OpenACCIRConstructor : public accparserBaseListener {
exitSelf_clause(accparser::Self_clauseContext * /*ctx*/) override;
virtual void
enterSelf_list_clause(accparser::Self_list_clauseContext * /*ctx*/) override;
virtual void exitSelf_list_clause(
accparser::Self_list_clauseContext * /*ctx*/) override;
virtual void
exitSelf_list_clause(accparser::Self_list_clauseContext * /*ctx*/) override;
virtual void enterSeq_clause(accparser::Seq_clauseContext * /*ctx*/) override;
virtual void
enterTile_clause(accparser::Tile_clauseContext * /*ctx*/) override;
Expand All @@ -196,8 +198,8 @@ class OpenACCIRConstructor : public accparserBaseListener {
enterUpdate_clause(accparser::Update_clauseContext * /*ctx*/) override;
virtual void enterUse_device_clause(
accparser::Use_device_clauseContext * /*ctx*/) override;
virtual void exitUse_device_clause(
accparser::Use_device_clauseContext * /*ctx*/) override;
virtual void
exitUse_device_clause(accparser::Use_device_clauseContext * /*ctx*/) override;
virtual void
enterVector_clause(accparser::Vector_clauseContext * /*ctx*/) override;
virtual void exitVector_clause_modifier(
Expand Down
Loading