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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ if(OMPPARSER_ENABLE_WASM)
add_subdirectory(wasm)
endif()

if(BUILD_TESTING)
add_custom_target(check
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
DEPENDS tester omp_roundtrip test_locations
COMMENT "Running all tests...")
endif()

set(ompparser_targets ompparser)

# Install headers and libraries
Expand Down
50 changes: 29 additions & 21 deletions src/OpenMPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ std::string trimWhitespace(const std::string &text) {
return text.substr(begin, end - begin + 1);
}

std::string trimWhitespace(const std::string &text, size_t pos, size_t count) {
if (pos >= text.size()) {
return std::string();
}
const char *whitespace = " \t\n\r\f\v";
size_t end_pos = pos + count;
if (end_pos > text.size() || end_pos < pos) {
end_pos = text.size();
}
size_t begin = text.find_first_not_of(whitespace, pos);
if (begin == std::string::npos || begin >= end_pos) {
return std::string();
}
size_t end = text.find_last_not_of(whitespace, end_pos - 1);
return text.substr(begin, end - begin + 1);
}

std::string normalizeRawExpression(const char *expr,
bool strip_trailing_colon = false) {
if (!expr) {
Expand Down Expand Up @@ -181,15 +198,6 @@ std::string normalizeClauseExpression(OpenMPClauseKind kind,

namespace {

std::string trimWhitespaceCopy(const std::string &value) {
const std::string::size_type begin = value.find_first_not_of(" \t\r\n");
if (begin == std::string::npos) {
return std::string();
}
const std::string::size_type end = value.find_last_not_of(" \t\r\n");
return value.substr(begin, end - begin + 1);
}

bool isIdentifierChar(char ch) {
const unsigned char uch = static_cast<unsigned char>(ch);
return std::isalnum(uch) != 0 || ch == '_';
Expand Down Expand Up @@ -284,7 +292,7 @@ bool hasIncompleteTrailingOperator(const std::string &expression) {
}

bool isValidDistDataBaseExpression(const std::string &expression) {
const std::string trimmed_expression = trimWhitespaceCopy(expression);
const std::string trimmed_expression = trimWhitespace(expression);
if (trimmed_expression.empty()) {
return false;
}
Expand Down Expand Up @@ -349,7 +357,7 @@ bool splitMapExpressionDistDataSuffix(const std::string &expression,
return false;
}

const std::string trimmed_expression = trimWhitespaceCopy(expression);
const std::string trimmed_expression = trimWhitespace(expression);
*array_section_expression = trimmed_expression;
dist_data_arguments->clear();
if (trimmed_expression.empty() || trimmed_expression.back() != ')') {
Expand Down Expand Up @@ -408,7 +416,7 @@ bool splitMapExpressionDistDataSuffix(const std::string &expression,
}

const std::string prefix =
trimWhitespaceCopy(trimmed_expression.substr(0, open_paren_pos));
trimWhitespace(trimmed_expression, 0, open_paren_pos);
if (prefix.empty()) {
return false;
}
Expand Down Expand Up @@ -442,14 +450,14 @@ bool splitMapExpressionDistDataSuffix(const std::string &expression,
}

const std::string base_expression =
trimWhitespaceCopy(prefix.substr(0, token_begin));
trimWhitespace(prefix, 0, token_begin);
if (!isValidDistDataBaseExpression(base_expression)) {
return false;
}

*array_section_expression = base_expression;
*dist_data_arguments = trimWhitespaceCopy(trimmed_expression.substr(
open_paren_pos + 1, trimmed_expression.size() - open_paren_pos - 2));
*dist_data_arguments = trimWhitespace(trimmed_expression, open_paren_pos + 1,
trimmed_expression.size() - open_paren_pos - 2);
return true;
}

Expand Down Expand Up @@ -863,7 +871,7 @@ void OpenMPMapClause::addItem(const std::string &expr,
bool has_dist_data = splitMapExpressionDistDataSuffix(
expr, &array_section_expression, &dist_data_arguments);

const std::string trimmed_expression = trimWhitespaceCopy(expr);
const std::string trimmed_expression = trimWhitespace(expr);
std::string item_expression = trimmed_expression;
if (has_dist_data) {
item_expression = array_section_expression + " dist_data(" +
Expand All @@ -881,7 +889,7 @@ void OpenMPMapClause::addItem(const std::string &expr,
const std::vector<std::string> policy_texts =
splitTopLevelCommaSeparated(dist_data_arguments);
for (const std::string &raw_policy : policy_texts) {
const std::string policy_text = trimWhitespaceCopy(raw_policy);
const std::string policy_text = trimWhitespace(raw_policy);
if (policy_text.empty()) {
continue;
}
Expand All @@ -907,12 +915,12 @@ void OpenMPMapClause::addItem(const std::string &expr,
}
}
if (close_pos == std::string::npos ||
trimWhitespaceCopy(policy_text.substr(close_pos + 1)).size() != 0) {
trimWhitespace(policy_text, close_pos + 1, policy_text.size() - close_pos - 1).size() != 0) {
continue;
}
policy_name = trimWhitespaceCopy(policy_text.substr(0, open_pos));
policy_argument = trimWhitespaceCopy(
policy_text.substr(open_pos + 1, close_pos - open_pos - 1));
policy_name = trimWhitespace(policy_text, 0, open_pos);
policy_argument = trimWhitespace(
policy_text, open_pos + 1, close_pos - open_pos - 1);
}

std::string normalized_name = policy_name;
Expand Down
14 changes: 6 additions & 8 deletions src/OpenMPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <string>
#include <vector>

using namespace std;

enum OpenMPBaseLang { Lang_C, Lang_Cplusplus, Lang_Fortran, Lang_unknown };

enum OpenMPExprParseMode {
Expand Down Expand Up @@ -211,7 +209,7 @@ class OpenMPDirective : public SourceLocation {
* should only have one OpenMPClause object for each instance of kind and full
* parameters
*/
map<OpenMPClauseKind, vector<OpenMPClause *> *> clauses;
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> clauses;

// Owned storage for clause objects to ensure automatic cleanup
std::vector<std::unique_ptr<OpenMPClause>> clause_storage;
Expand Down Expand Up @@ -270,7 +268,7 @@ class OpenMPDirective : public SourceLocation {

OpenMPDirectiveKind getKind() { return kind; };

map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *getAllClauses() {
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *getAllClauses() {
return &clauses;
};

Expand Down Expand Up @@ -320,8 +318,8 @@ class OpenMPDirective : public SourceLocation {
// atomic directive
class OpenMPAtomicDirective : public OpenMPDirective {
protected:
map<OpenMPClauseKind, vector<OpenMPClause *> *> clauses_atomic_after;
map<OpenMPClauseKind, vector<OpenMPClause *> *> clauses_atomic_clauses;
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> clauses_atomic_after;
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> clauses_atomic_clauses;
std::vector<std::unique_ptr<std::vector<OpenMPClause *>>>
atomic_clause_vector_storage;

Expand All @@ -343,11 +341,11 @@ class OpenMPAtomicDirective : public OpenMPDirective {
}
return clauses_atomic_clauses[kind];
};
map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *
getAllClausesAtomicAfter() {
return &clauses_atomic_after;
};
map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *getAllAtomicClauses() {
std::map<OpenMPClauseKind, std::vector<OpenMPClause *> *> *getAllAtomicClauses() {
return &clauses_atomic_clauses;
};
};
Expand Down