Path: blob/main/contrib/llvm-project/llvm/lib/Frontend/OpenMP/DirectiveNameParser.cpp
213799 views
//===- DirectiveNameParser.cpp --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/Frontend/OpenMP/DirectiveNameParser.h"9#include "llvm/ADT/Sequence.h"10#include "llvm/ADT/StringExtras.h"11#include "llvm/ADT/StringRef.h"12#include "llvm/Frontend/OpenMP/OMP.h"1314#include <cassert>15#include <memory>1617namespace llvm::omp {18DirectiveNameParser::DirectiveNameParser(SourceLanguage L) {19// Take every directive, get its name in every version, break the name up20// into whitespace-separated tokens, and insert each token.21for (size_t I : llvm::seq<size_t>(Directive_enumSize)) {22auto D = static_cast<Directive>(I);23if (D == Directive::OMPD_unknown || !(getDirectiveLanguages(D) & L))24continue;25for (unsigned Ver : getOpenMPVersions())26insertName(getOpenMPDirectiveName(D, Ver), D);27}28}2930const DirectiveNameParser::State *31DirectiveNameParser::consume(const State *Current, StringRef Tok) const {32if (!Current)33return Current;34assert(Current->isValid() && "Invalid input state");35if (const State *Next = Current->next(Tok))36return Next->isValid() ? Next : nullptr;37return nullptr;38}3940SmallVector<StringRef> DirectiveNameParser::tokenize(StringRef Str) {41SmallVector<StringRef> Tokens;42SplitString(Str, Tokens);43return Tokens;44}4546void DirectiveNameParser::insertName(StringRef Name, Directive D) {47State *Where = &InitialState;4849for (StringRef Tok : tokenize(Name))50Where = insertTransition(Where, Tok);5152Where->Value = D;53}5455DirectiveNameParser::State *56DirectiveNameParser::insertTransition(State *From, StringRef Tok) {57assert(From && "Expecting state");58if (!From->Transition)59From->Transition = std::make_unique<State::TransitionMapTy>();60if (State *Next = From->next(Tok))61return Next;6263auto [Where, DidIt] = From->Transition->try_emplace(Tok, State());64assert(DidIt && "Map insertion failed");65return &Where->second;66}6768const DirectiveNameParser::State *69DirectiveNameParser::State::next(StringRef Tok) const {70if (!Transition)71return nullptr;72auto F = Transition->find(Tok);73return F != Transition->end() ? &F->second : nullptr;74}7576DirectiveNameParser::State *DirectiveNameParser::State::next(StringRef Tok) {77if (!Transition)78return nullptr;79auto F = Transition->find(Tok);80return F != Transition->end() ? &F->second : nullptr;81}82} // namespace llvm::omp838485