Skip to content

Commit 3d64079

Browse files
authored
[clang][OpenMP] Use DirectiveNameParser to parse directive names (#146779)
This simplifies the parsing code in clang quite a bit.
1 parent 20864c4 commit 3d64079

File tree

1 file changed

+19
-162
lines changed

1 file changed

+19
-162
lines changed

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 19 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Sema/SemaOpenMP.h"
2626
#include "llvm/ADT/SmallBitVector.h"
2727
#include "llvm/ADT/StringSwitch.h"
28+
#include "llvm/Frontend/OpenMP/DirectiveNameParser.h"
2829
#include "llvm/Frontend/OpenMP/OMPAssume.h"
2930
#include "llvm/Frontend/OpenMP/OMPContext.h"
3031
#include <optional>
@@ -37,48 +38,6 @@ using namespace llvm::omp;
3738
//===----------------------------------------------------------------------===//
3839

3940
namespace {
40-
enum OpenMPDirectiveKindEx {
41-
OMPD_cancellation = llvm::omp::Directive_enumSize + 1,
42-
OMPD_data,
43-
OMPD_declare,
44-
OMPD_end,
45-
OMPD_end_declare,
46-
OMPD_enter,
47-
OMPD_exit,
48-
OMPD_point,
49-
OMPD_reduction,
50-
OMPD_target_enter,
51-
OMPD_target_exit,
52-
OMPD_update,
53-
OMPD_distribute_parallel,
54-
OMPD_teams_distribute_parallel,
55-
OMPD_target_teams_distribute_parallel,
56-
OMPD_mapper,
57-
OMPD_variant,
58-
OMPD_begin,
59-
OMPD_begin_declare,
60-
};
61-
62-
// Helper to unify the enum class OpenMPDirectiveKind with its extension
63-
// the OpenMPDirectiveKindEx enum which allows to use them together as if they
64-
// are unsigned values.
65-
struct OpenMPDirectiveKindExWrapper {
66-
OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {}
67-
OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {}
68-
bool operator==(OpenMPDirectiveKindExWrapper V) const {
69-
return Value == V.Value;
70-
}
71-
bool operator!=(OpenMPDirectiveKindExWrapper V) const {
72-
return Value != V.Value;
73-
}
74-
bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); }
75-
bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); }
76-
bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); }
77-
operator unsigned() const { return Value; }
78-
operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); }
79-
unsigned Value;
80-
};
81-
8241
class DeclDirectiveListParserHelper final {
8342
SmallVector<Expr *, 4> Identifiers;
8443
Parser *P;
@@ -97,130 +56,32 @@ class DeclDirectiveListParserHelper final {
9756
};
9857
} // namespace
9958

100-
// Map token string to extended OMP token kind that are
101-
// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
102-
static unsigned getOpenMPDirectiveKindEx(StringRef S) {
103-
OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S);
104-
if (DKind != OMPD_unknown)
105-
return DKind;
106-
107-
return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S)
108-
.Case("cancellation", OMPD_cancellation)
109-
.Case("data", OMPD_data)
110-
.Case("declare", OMPD_declare)
111-
.Case("end", OMPD_end)
112-
.Case("enter", OMPD_enter)
113-
.Case("exit", OMPD_exit)
114-
.Case("point", OMPD_point)
115-
.Case("reduction", OMPD_reduction)
116-
.Case("update", OMPD_update)
117-
.Case("mapper", OMPD_mapper)
118-
.Case("variant", OMPD_variant)
119-
.Case("begin", OMPD_begin)
120-
.Default(OMPD_unknown);
121-
}
59+
static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
60+
static const DirectiveNameParser DirParser;
61+
62+
const DirectiveNameParser::State *S = DirParser.initial();
12263

123-
static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) {
124-
// Array of foldings: F[i][0] F[i][1] ===> F[i][2].
125-
// E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
126-
// TODO: add other combined directives in topological order.
127-
static const OpenMPDirectiveKindExWrapper F[][3] = {
128-
{OMPD_begin, OMPD_declare, OMPD_begin_declare},
129-
{OMPD_begin, OMPD_assumes, OMPD_begin_assumes},
130-
{OMPD_end, OMPD_declare, OMPD_end_declare},
131-
{OMPD_end, OMPD_assumes, OMPD_end_assumes},
132-
{OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
133-
{OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
134-
{OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
135-
{OMPD_declare, OMPD_simd, OMPD_declare_simd},
136-
{OMPD_declare, OMPD_target, OMPD_declare_target},
137-
{OMPD_declare, OMPD_variant, OMPD_declare_variant},
138-
{OMPD_begin_declare, OMPD_target, OMPD_begin_declare_target},
139-
{OMPD_begin_declare, OMPD_variant, OMPD_begin_declare_variant},
140-
{OMPD_end_declare, OMPD_variant, OMPD_end_declare_variant},
141-
{OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
142-
{OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
143-
{OMPD_distribute_parallel_for, OMPD_simd,
144-
OMPD_distribute_parallel_for_simd},
145-
{OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
146-
{OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
147-
{OMPD_target, OMPD_data, OMPD_target_data},
148-
{OMPD_target, OMPD_enter, OMPD_target_enter},
149-
{OMPD_target, OMPD_exit, OMPD_target_exit},
150-
{OMPD_target, OMPD_update, OMPD_target_update},
151-
{OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
152-
{OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
153-
{OMPD_for, OMPD_simd, OMPD_for_simd},
154-
{OMPD_parallel, OMPD_for, OMPD_parallel_for},
155-
{OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
156-
{OMPD_parallel, OMPD_loop, OMPD_parallel_loop},
157-
{OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
158-
{OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
159-
{OMPD_target, OMPD_parallel, OMPD_target_parallel},
160-
{OMPD_target, OMPD_simd, OMPD_target_simd},
161-
{OMPD_target_parallel, OMPD_loop, OMPD_target_parallel_loop},
162-
{OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
163-
{OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
164-
{OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
165-
{OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
166-
{OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
167-
{OMPD_teams_distribute_parallel, OMPD_for,
168-
OMPD_teams_distribute_parallel_for},
169-
{OMPD_teams_distribute_parallel_for, OMPD_simd,
170-
OMPD_teams_distribute_parallel_for_simd},
171-
{OMPD_teams, OMPD_loop, OMPD_teams_loop},
172-
{OMPD_target, OMPD_teams, OMPD_target_teams},
173-
{OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
174-
{OMPD_target_teams, OMPD_loop, OMPD_target_teams_loop},
175-
{OMPD_target_teams_distribute, OMPD_parallel,
176-
OMPD_target_teams_distribute_parallel},
177-
{OMPD_target_teams_distribute, OMPD_simd,
178-
OMPD_target_teams_distribute_simd},
179-
{OMPD_target_teams_distribute_parallel, OMPD_for,
180-
OMPD_target_teams_distribute_parallel_for},
181-
{OMPD_target_teams_distribute_parallel_for, OMPD_simd,
182-
OMPD_target_teams_distribute_parallel_for_simd},
183-
{OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
184-
{OMPD_masked, OMPD_taskloop, OMPD_masked_taskloop},
185-
{OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
186-
{OMPD_masked_taskloop, OMPD_simd, OMPD_masked_taskloop_simd},
187-
{OMPD_parallel, OMPD_master, OMPD_parallel_master},
188-
{OMPD_parallel, OMPD_masked, OMPD_parallel_masked},
189-
{OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
190-
{OMPD_parallel_masked, OMPD_taskloop, OMPD_parallel_masked_taskloop},
191-
{OMPD_parallel_master_taskloop, OMPD_simd,
192-
OMPD_parallel_master_taskloop_simd},
193-
{OMPD_parallel_masked_taskloop, OMPD_simd,
194-
OMPD_parallel_masked_taskloop_simd}};
195-
enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
19664
Token Tok = P.getCurToken();
197-
OpenMPDirectiveKindExWrapper DKind =
198-
Tok.isAnnotation()
199-
? static_cast<unsigned>(OMPD_unknown)
200-
: getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
201-
if (DKind == OMPD_unknown)
65+
if (Tok.isAnnotation())
20266
return OMPD_unknown;
20367

204-
for (const auto &I : F) {
205-
if (DKind != I[0])
206-
continue;
68+
S = DirParser.consume(S, P.getPreprocessor().getSpelling(Tok));
69+
if (S == nullptr)
70+
return OMPD_unknown;
20771

72+
while (!Tok.isAnnotation()) {
73+
OpenMPDirectiveKind DKind = S->Value;
20874
Tok = P.getPreprocessor().LookAhead(0);
209-
OpenMPDirectiveKindExWrapper SDKind =
210-
Tok.isAnnotation()
211-
? static_cast<unsigned>(OMPD_unknown)
212-
: getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
213-
if (SDKind == OMPD_unknown)
214-
continue;
215-
216-
if (SDKind == I[1]) {
75+
if (!Tok.isAnnotation()) {
76+
S = DirParser.consume(S, P.getPreprocessor().getSpelling(Tok));
77+
if (S == nullptr)
78+
return DKind;
21779
P.ConsumeToken();
218-
DKind = I[2];
21980
}
22081
}
221-
return unsigned(DKind) < llvm::omp::Directive_enumSize
222-
? static_cast<OpenMPDirectiveKind>(DKind)
223-
: OMPD_unknown;
82+
83+
assert(S && "Should have exited early");
84+
return S->Value;
22485
}
22586

22687
static DeclarationName parseOpenMPReductionId(Parser &P) {
@@ -2629,10 +2490,6 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
26292490
Diag(Tok, diag::err_omp_unknown_directive);
26302491
return StmtError();
26312492
}
2632-
if (!(getDirectiveLanguages(DKind) & SourceLanguage::C)) {
2633-
// Treat directives that are not allowed in C/C++ as unknown.
2634-
DKind = OMPD_unknown;
2635-
}
26362493

26372494
StmtResult Directive = StmtError();
26382495

@@ -4014,7 +3871,7 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
40143871
KLoc.push_back(Tok.getLocation());
40153872
TentativeParsingAction TPA(*this);
40163873
auto DK = parseOpenMPDirectiveKind(*this);
4017-
Arg.push_back(DK);
3874+
Arg.push_back(static_cast<unsigned>(DK));
40183875
if (DK != OMPD_unknown) {
40193876
ConsumeToken();
40203877
if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {

0 commit comments

Comments
 (0)