Skip to content
Snippets Groups Projects
Commit a18931c6 authored by Dmitry Polukhin's avatar Dmitry Polukhin
Browse files

[OPENMP] NFC rewrite ParseOpenMPDirectiveKind

New implementation is easier to read and extend.

Differential Revision: http://reviews.llvm.org/D17197

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260811 91177308-0d34-0410-b5e6-96231b3b80d8
parent 5528df3a
No related branches found
No related tags found
No related merge requests found
...@@ -26,74 +26,80 @@ using namespace clang; ...@@ -26,74 +26,80 @@ using namespace clang;
// OpenMP declarative directives. // OpenMP declarative directives.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
namespace {
enum OpenMPDirectiveKindEx {
OMPD_cancellation = OMPD_unknown + 1,
OMPD_data,
OMPD_enter,
OMPD_exit,
OMPD_point,
OMPD_target_enter,
OMPD_target_exit
};
} // namespace
// Map token string to extended OMP token kind that are
// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
static unsigned getOpenMPDirectiveKindEx(StringRef S) {
auto DKind = getOpenMPDirectiveKind(S);
if (DKind != OMPD_unknown)
return DKind;
return llvm::StringSwitch<unsigned>(S)
.Case("cancellation", OMPD_cancellation)
.Case("data", OMPD_data)
.Case("enter", OMPD_enter)
.Case("exit", OMPD_exit)
.Case("point", OMPD_point)
.Default(OMPD_unknown);
}
static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) { static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
// Array of foldings: F[i][0] F[i][1] ===> F[i][2]. // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
// E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
// TODO: add other combined directives in topological order. // TODO: add other combined directives in topological order.
const OpenMPDirectiveKind F[][3] = { static const unsigned F[][3] = {
{OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/, { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
OMPD_cancellation_point}, { OMPD_target, OMPD_data, OMPD_target_data },
{OMPD_target, OMPD_unknown /*data*/, OMPD_target_data}, { OMPD_target, OMPD_enter, OMPD_target_enter },
{OMPD_target, OMPD_unknown /*enter/exit*/, { OMPD_target, OMPD_exit, OMPD_target_exit },
OMPD_unknown /*target enter/exit*/}, { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
{OMPD_unknown /*target enter*/, OMPD_unknown /*data*/, { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
OMPD_target_enter_data}, { OMPD_for, OMPD_simd, OMPD_for_simd },
{OMPD_unknown /*target exit*/, OMPD_unknown /*data*/, { OMPD_parallel, OMPD_for, OMPD_parallel_for },
OMPD_target_exit_data}, { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
{OMPD_for, OMPD_simd, OMPD_for_simd}, { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
{OMPD_parallel, OMPD_for, OMPD_parallel_for}, { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
{OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd}, { OMPD_target, OMPD_parallel, OMPD_target_parallel },
{OMPD_parallel, OMPD_sections, OMPD_parallel_sections}, { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
{OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}, };
{OMPD_target, OMPD_parallel, OMPD_target_parallel},
{OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for}};
auto Tok = P.getCurToken(); auto Tok = P.getCurToken();
auto DKind = unsigned DKind =
Tok.isAnnotation() Tok.isAnnotation()
? OMPD_unknown ? static_cast<unsigned>(OMPD_unknown)
: getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok)); : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
if (DKind == OMPD_unknown)
return OMPD_unknown;
bool TokenMatched = false;
for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) { for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
if (!Tok.isAnnotation() && DKind == OMPD_unknown) { if (DKind != F[i][0])
TokenMatched = continue;
((i == 0) &&
!P.getPreprocessor().getSpelling(Tok).compare("cancellation")) || Tok = P.getPreprocessor().LookAhead(0);
((i == 3) && unsigned SDKind =
!P.getPreprocessor().getSpelling(Tok).compare("enter")) || Tok.isAnnotation()
((i == 4) && !P.getPreprocessor().getSpelling(Tok).compare("exit")); ? static_cast<unsigned>(OMPD_unknown)
} else { : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown; if (SDKind == OMPD_unknown)
} continue;
if (TokenMatched) { if (SDKind == F[i][1]) {
Tok = P.getPreprocessor().LookAhead(0); P.ConsumeToken();
auto TokenIsAnnotation = Tok.isAnnotation(); DKind = F[i][2];
auto SDKind =
TokenIsAnnotation
? OMPD_unknown
: getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
TokenMatched =
((i == 0) &&
!P.getPreprocessor().getSpelling(Tok).compare("point")) ||
((i == 1 || i == 3 || i == 4) &&
!P.getPreprocessor().getSpelling(Tok).compare("data")) ||
((i == 2) &&
(!P.getPreprocessor().getSpelling(Tok).compare("enter") ||
!P.getPreprocessor().getSpelling(Tok).compare("exit")));
} else {
TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
}
if (TokenMatched) {
P.ConsumeToken();
DKind = F[i][2];
}
} }
} }
return DKind; return DKind <= OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
: OMPD_unknown;
} }
/// \brief Parsing of declarative OpenMP directives. /// \brief Parsing of declarative OpenMP directives.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment