aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
blob: 10329820bef76dfdfdef33e2da9f6d157d17b16e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//===- llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Frontend/OpenMP/DirectiveNameParser.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "gtest/gtest.h"

#include <cctype>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

using namespace llvm;

static const omp::DirectiveNameParser &getParser() {
  static omp::DirectiveNameParser Parser(omp::SourceLanguage::C |
                                         omp::SourceLanguage::Fortran);
  return Parser;
}

static std::vector<std::string> tokenize(StringRef S) {
  std::vector<std::string> Tokens;

  using TokenIterator = std::istream_iterator<std::string>;
  std::string Copy = S.str();
  std::istringstream Stream(Copy);

  for (auto I = TokenIterator(Stream), E = TokenIterator(); I != E; ++I)
    Tokens.push_back(*I);
  return Tokens;
}

static std::string &prepareParamName(std::string &Name) {
  for (size_t I = 0, E = Name.size(); I != E; ++I) {
    // The parameter name must only have alphanumeric characters.
    if (!isalnum(Name[I]))
      Name[I] = 'X';
  }
  return Name;
}

// Test tokenizing.

class Tokenize : public testing::TestWithParam<omp::Directive> {};

static bool isEqual(const SmallVector<StringRef> &A,
                    const std::vector<std::string> &B) {
  if (A.size() != B.size())
    return false;

  for (size_t I = 0, E = A.size(); I != E; ++I) {
    if (A[I] != StringRef(B[I]))
      return false;
  }
  return true;
}

TEST_P(Tokenize, T) {
  omp::Directive DirId = GetParam();
  StringRef Name = omp::getOpenMPDirectiveName(DirId, omp::FallbackVersion);

  SmallVector<StringRef> tokens1 = omp::DirectiveNameParser::tokenize(Name);
  std::vector<std::string> tokens2 = tokenize(Name);
  ASSERT_TRUE(isEqual(tokens1, tokens2));
}

static std::string
getParamName1(const testing::TestParamInfo<Tokenize::ParamType> &Info) {
  omp::Directive DirId = Info.param;
  std::string Name =
      omp::getOpenMPDirectiveName(DirId, omp::FallbackVersion).str();
  return prepareParamName(Name);
}

INSTANTIATE_TEST_SUITE_P(DirectiveNameParserTest, Tokenize,
                         testing::ValuesIn(llvm::enum_seq_inclusive(
                             omp::Directive::First_, omp::Directive::Last_)),
                         getParamName1);

// Test parsing of valid names.

using ValueType = std::tuple<omp::Directive, unsigned>;

class ParseValid : public testing::TestWithParam<ValueType> {};

TEST_P(ParseValid, T) {
  auto [DirId, Version] = GetParam();
  if (DirId == omp::Directive::OMPD_unknown)
    return;

  std::string Name = omp::getOpenMPDirectiveName(DirId, Version).str();

  // Tokenize and parse
  auto &Parser = getParser();
  auto *State = Parser.initial();
  ASSERT_TRUE(State != nullptr);

  std::vector<std::string> Tokens = tokenize(Name);
  for (auto &Tok : Tokens) {
    State = Parser.consume(State, Tok);
    ASSERT_TRUE(State != nullptr);
  }

  ASSERT_EQ(State->Value, DirId);
}

static std::string
getParamName2(const testing::TestParamInfo<ParseValid::ParamType> &Info) {
  auto [DirId, Version] = Info.param;
  std::string Name = omp::getOpenMPDirectiveName(DirId, Version).str() + "v" +
                     std::to_string(Version);
  return prepareParamName(Name);
}

INSTANTIATE_TEST_SUITE_P(
    DirectiveNameParserTest, ParseValid,
    testing::Combine(testing::ValuesIn(llvm::enum_seq_inclusive(
                         omp::Directive::First_, omp::Directive::Last_)),
                     testing::ValuesIn(omp::getOpenMPVersions())),
    getParamName2);

// Test parsing of invalid names

class ParseInvalid : public testing::TestWithParam<std::string> {};

TEST_P(ParseInvalid, T) {
  std::string Name = GetParam();

  auto &Parser = getParser();
  auto *State = Parser.initial();
  ASSERT_TRUE(State != nullptr);

  std::vector<std::string> Tokens = tokenize(Name);
  for (auto &Tok : Tokens)
    State = Parser.consume(State, Tok);

  ASSERT_TRUE(State == nullptr || State->Value == omp::Directive::OMPD_unknown);
}

namespace {
using namespace std;

INSTANTIATE_TEST_SUITE_P(DirectiveNameParserTest, ParseInvalid,
                         testing::Values(
                             // Names that contain invalid tokens
                             "bad"s, "target teams invalid"s,
                             "target sections parallel"s,
                             "target teams distribute parallel for wrong"s,
                             // Valid beginning, but not a complete name
                             "begin declare"s,
                             // Complete name with extra tokens
                             "distribute simd target"s));
} // namespace