| 1 |
//===- llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp ----------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/ADT/Sequence.h" |
| 10 |
#include "llvm/ADT/SmallVector.h" |
| 11 |
#include "llvm/ADT/StringRef.h" |
| 12 |
#include "llvm/Frontend/OpenMP/DirectiveNameParser.h" |
| 13 |
#include "llvm/Frontend/OpenMP/OMP.h" |
| 14 |
#include "gtest/gtest.h" |
| 15 |
|
| 16 |
#include <cctype> |
| 17 |
#include <sstream> |
| 18 |
#include <string> |
| 19 |
#include <tuple> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using namespace llvm; |
| 23 |
|
| 24 |
static const omp::DirectiveNameParser &getParser() { |
| 25 |
static omp::DirectiveNameParser Parser(omp::SourceLanguage::C | |
| 26 |
omp::SourceLanguage::Fortran); |
| 27 |
return Parser; |
| 28 |
} |
| 29 |
|
| 30 |
static std::vector<std::string> tokenize(StringRef S) { |
| 31 |
std::vector<std::string> Tokens; |
| 32 |
|
| 33 |
using TokenIterator = std::istream_iterator<std::string>; |
| 34 |
std::string Copy = S.str(); |
| 35 |
std::istringstream Stream(Copy); |
| 36 |
|
| 37 |
for (auto I = TokenIterator(Stream), E = TokenIterator(); I != E; ++I) |
| 38 |
Tokens.push_back(*I); |
| 39 |
return Tokens; |
| 40 |
} |
| 41 |
|
| 42 |
static std::string &prepareParamName(std::string &Name) { |
| 43 |
for (size_t I = 0, E = Name.size(); I != E; ++I) { |
| 44 |
// The parameter name must only have alphanumeric characters. |
| 45 |
if (!isalnum(Name[I])) |
| 46 |
Name[I] = 'X'; |
| 47 |
} |
| 48 |
return Name; |
| 49 |
} |
| 50 |
|
| 51 |
namespace llvm { |
| 52 |
template <> struct enum_iteration_traits<omp::Directive> { |
| 53 |
static constexpr bool is_iterable = true; |
| 54 |
}; |
| 55 |
} // namespace llvm |
| 56 |
|
| 57 |
// Test tokenizing. |
| 58 |
|
| 59 |
class Tokenize : public testing::TestWithParam<omp::Directive> {}; |
| 60 |
|
| 61 |
static bool isEqual(const SmallVector<StringRef> &A, |
| 62 |
const std::vector<std::string> &B) { |
| 63 |
if (A.size() != B.size()) |
| 64 |
return false; |
| 65 |
|
| 66 |
for (size_t I = 0, E = A.size(); I != E; ++I) { |
| 67 |
if (A[I] != StringRef(B[I])) |
| 68 |
return false; |
| 69 |
} |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
TEST_P(Tokenize, T) { |
| 74 |
omp::Directive DirId = GetParam(); |
| 75 |
StringRef Name = omp::getOpenMPDirectiveName(DirId, omp::FallbackVersion); |
| 76 |
|
| 77 |
SmallVector<StringRef> tokens1 = omp::DirectiveNameParser::tokenize(Name); |
| 78 |
std::vector<std::string> tokens2 = tokenize(Name); |
| 79 |
ASSERT_TRUE(isEqual(tokens1, tokens2)); |
| 80 |
} |
| 81 |
|
| 82 |
static std::string |
| 83 |
getParamName1(const testing::TestParamInfo<Tokenize::ParamType> &Info) { |
| 84 |
omp::Directive DirId = Info.param; |
| 85 |
std::string Name = |
| 86 |
omp::getOpenMPDirectiveName(DirId, omp::FallbackVersion).str(); |
| 87 |
return prepareParamName(Name); |
| 88 |
} |
| 89 |
|
| 90 |
INSTANTIATE_TEST_SUITE_P( |
| 91 |
DirectiveNameParserTest, Tokenize, |
| 92 |
testing::ValuesIn( |
| 93 |
llvm::enum_seq(static_cast<omp::Directive>(0), |
| 94 |
static_cast<omp::Directive>(omp::Directive_enumSize))), |
| 95 |
getParamName1); |
| 96 |
|
| 97 |
// Test parsing of valid names. |
| 98 |
|
| 99 |
using ValueType = std::tuple<omp::Directive, unsigned>; |
| 100 |
|
| 101 |
class ParseValid : public testing::TestWithParam<ValueType> {}; |
| 102 |
|
| 103 |
TEST_P(ParseValid, T) { |
| 104 |
auto [DirId, Version] = GetParam(); |
| 105 |
if (DirId == omp::Directive::OMPD_unknown) |
| 106 |
return; |
| 107 |
|
| 108 |
std::string Name = omp::getOpenMPDirectiveName(DirId, Version).str(); |
| 109 |
|
| 110 |
// Tokenize and parse |
| 111 |
auto &Parser = getParser(); |
| 112 |
auto *State = Parser.initial(); |
| 113 |
ASSERT_TRUE(State != nullptr); |
| 114 |
|
| 115 |
std::vector<std::string> Tokens = tokenize(Name); |
| 116 |
for (auto &Tok : Tokens) { |
| 117 |
State = Parser.consume(State, Tok); |
| 118 |
ASSERT_TRUE(State != nullptr); |
| 119 |
} |
| 120 |
|
| 121 |
ASSERT_EQ(State->Value, DirId); |
| 122 |
} |
| 123 |
|
| 124 |
static std::string |
| 125 |
getParamName2(const testing::TestParamInfo<ParseValid::ParamType> &Info) { |
| 126 |
auto [DirId, Version] = Info.param; |
| 127 |
std::string Name = omp::getOpenMPDirectiveName(DirId, Version).str() + "v" + |
| 128 |
std::to_string(Version); |
| 129 |
return prepareParamName(Name); |
| 130 |
} |
| 131 |
|
| 132 |
INSTANTIATE_TEST_SUITE_P( |
| 133 |
DirectiveNameParserTest, ParseValid, |
| 134 |
testing::Combine(testing::ValuesIn(llvm::enum_seq( |
| 135 |
static_cast<omp::Directive>(0), |
| 136 |
static_cast<omp::Directive>(omp::Directive_enumSize))), |
| 137 |
testing::ValuesIn(omp::getOpenMPVersions())), |
| 138 |
getParamName2); |
| 139 |
|
| 140 |
// Test parsing of invalid names |
| 141 |
|
| 142 |
class ParseInvalid : public testing::TestWithParam<std::string> {}; |
| 143 |
|
| 144 |
TEST_P(ParseInvalid, T) { |
| 145 |
std::string Name = GetParam(); |
| 146 |
|
| 147 |
auto &Parser = getParser(); |
| 148 |
auto *State = Parser.initial(); |
| 149 |
ASSERT_TRUE(State != nullptr); |
| 150 |
|
| 151 |
std::vector<std::string> Tokens = tokenize(Name); |
| 152 |
for (auto &Tok : Tokens) |
| 153 |
State = Parser.consume(State, Tok); |
| 154 |
|
| 155 |
ASSERT_TRUE(State == nullptr || State->Value == omp::Directive::OMPD_unknown); |
| 156 |
} |
| 157 |
|
| 158 |
namespace { |
| 159 |
using namespace std; |
| 160 |
|
| 161 |
INSTANTIATE_TEST_SUITE_P(DirectiveNameParserTest, ParseInvalid, |
| 162 |
testing::Values( |
| 163 |
// Names that contain invalid tokens |
| 164 |
"bad"s, "target teams invalid"s, |
| 165 |
"target sections parallel"s, |
| 166 |
"target teams distribute parallel for wrong"s, |
| 167 |
// Valid beginning, but not a complete name |
| 168 |
"begin declare"s, |
| 169 |
// Complete name with extra tokens |
| 170 |
"distribute simd target"s)); |
| 171 |
} // namespace |