Path: blob/main/contrib/llvm-project/llvm/lib/TableGen/StringMatcher.cpp
35232 views
//===- StringMatcher.cpp - Generate a matcher for input strings -----------===//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//===----------------------------------------------------------------------===//7//8// This file implements the StringMatcher class.9//10//===----------------------------------------------------------------------===//1112#include "llvm/TableGen/StringMatcher.h"13#include "llvm/ADT/StringRef.h"14#include "llvm/Support/ErrorHandling.h"15#include "llvm/Support/raw_ostream.h"16#include <cassert>17#include <map>18#include <string>19#include <utility>20#include <vector>2122using namespace llvm;2324/// FindFirstNonCommonLetter - Find the first character in the keys of the25/// string pairs that is not shared across the whole set of strings. All26/// strings are assumed to have the same length.27static unsigned28FindFirstNonCommonLetter(const std::vector<const29StringMatcher::StringPair*> &Matches) {30assert(!Matches.empty());31for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {32// Check to see if letter i is the same across the set.33char Letter = Matches[0]->first[i];3435for (const StringMatcher::StringPair *Match : Matches)36if (Match->first[i] != Letter)37return i;38}3940return Matches[0]->first.size();41}4243/// EmitStringMatcherForChar - Given a set of strings that are known to be the44/// same length and whose characters leading up to CharNo are the same, emit45/// code to verify that CharNo and later are the same.46///47/// \return - True if control can leave the emitted code fragment.48bool StringMatcher::EmitStringMatcherForChar(49const std::vector<const StringPair *> &Matches, unsigned CharNo,50unsigned IndentCount, bool IgnoreDuplicates) const {51assert(!Matches.empty() && "Must have at least one string to match!");52std::string Indent(IndentCount * 2 + 4, ' ');5354// If we have verified that the entire string matches, we're done: output the55// matching code.56if (CharNo == Matches[0]->first.size()) {57if (Matches.size() > 1 && !IgnoreDuplicates)58report_fatal_error("Had duplicate keys to match on");5960// If the to-execute code has \n's in it, indent each subsequent line.61StringRef Code = Matches[0]->second;6263std::pair<StringRef, StringRef> Split = Code.split('\n');64OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n";6566Code = Split.second;67while (!Code.empty()) {68Split = Code.split('\n');69OS << Indent << Split.first << "\n";70Code = Split.second;71}72return false;73}7475// Bucket the matches by the character we are comparing.76std::map<char, std::vector<const StringPair*>> MatchesByLetter;7778for (const StringPair *Match : Matches)79MatchesByLetter[Match->first[CharNo]].push_back(Match);8081// If we have exactly one bucket to match, see how many characters are common82// across the whole set and match all of them at once.83if (MatchesByLetter.size() == 1) {84unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);85unsigned NumChars = FirstNonCommonLetter-CharNo;8687// Emit code to break out if the prefix doesn't match.88if (NumChars == 1) {89// Do the comparison with if (Str[1] != 'f')90// FIXME: Need to escape general characters.91OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"92<< Matches[0]->first[CharNo] << "')\n";93OS << Indent << " break;\n";94} else {95// Do the comparison with if memcmp(Str.data()+1, "foo", 3).96// FIXME: Need to escape general strings.97OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo98<< ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", "99<< NumChars << ") != 0)\n";100OS << Indent << " break;\n";101}102103return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,104IgnoreDuplicates);105}106107// Otherwise, we have multiple possible things, emit a switch on the108// character.109OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";110OS << Indent << "default: break;\n";111112for (const auto &LI : MatchesByLetter) {113// TODO: escape hard stuff (like \n) if we ever care about it.114OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size()115<< " string";116if (LI.second.size() != 1)117OS << 's';118OS << " to match.\n";119if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1,120IgnoreDuplicates))121OS << Indent << " break;\n";122}123124OS << Indent << "}\n";125return true;126}127128/// Emit - Top level entry point.129///130void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {131// If nothing to match, just fall through.132if (Matches.empty()) return;133134// First level categorization: group strings by length.135std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;136137for (const StringPair &Match : Matches)138MatchesByLength[Match.first.size()].push_back(&Match);139140// Output a switch statement on length and categorize the elements within each141// bin.142OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";143OS.indent(Indent*2+2) << "default: break;\n";144145for (const auto &LI : MatchesByLength) {146OS.indent(Indent * 2 + 2)147<< "case " << LI.first << ":\t // " << LI.second.size() << " string"148<< (LI.second.size() == 1 ? "" : "s") << " to match.\n";149if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates))150OS.indent(Indent*2+4) << "break;\n";151}152153OS.indent(Indent*2+2) << "}\n";154}155156157