10b57cec5SDimitry Andric //===- Strings.h ------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLD_STRINGS_H 100b57cec5SDimitry Andric #define LLD_STRINGS_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 13*bdd1243dSDimitry Andric #include "llvm/ADT/SmallVector.h" 140b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 150b57cec5SDimitry Andric #include "llvm/Support/GlobPattern.h" 160b57cec5SDimitry Andric #include <string> 170b57cec5SDimitry Andric #include <vector> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace lld { 200b57cec5SDimitry Andric 21*bdd1243dSDimitry Andric llvm::SmallVector<uint8_t, 0> parseHex(llvm::StringRef s); 220b57cec5SDimitry Andric bool isValidCIdentifier(llvm::StringRef s); 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric // Write the contents of the a buffer to a file 250b57cec5SDimitry Andric void saveBuffer(llvm::StringRef buffer, const llvm::Twine &path); 260b57cec5SDimitry Andric 275ffd83dbSDimitry Andric // A single pattern to match against. A pattern can either be double-quoted 285ffd83dbSDimitry Andric // text that should be matched exactly after removing the quoting marks or a 295ffd83dbSDimitry Andric // glob pattern in the sense of GlobPattern. 305ffd83dbSDimitry Andric class SingleStringMatcher { 310b57cec5SDimitry Andric public: 32fe6060f1SDimitry Andric // Create a StringPattern from Pattern to be matched exactly regardless 335ffd83dbSDimitry Andric // of globbing characters if ExactMatch is true. 345ffd83dbSDimitry Andric SingleStringMatcher(llvm::StringRef Pattern); 350b57cec5SDimitry Andric 365ffd83dbSDimitry Andric // Match s against this pattern, exactly if ExactMatch is true. 370b57cec5SDimitry Andric bool match(llvm::StringRef s) const; 380b57cec5SDimitry Andric 39e8d8bef9SDimitry Andric // Returns true for pattern "*" which will match all inputs. isTrivialMatchAll()40e8d8bef9SDimitry Andric bool isTrivialMatchAll() const { 41e8d8bef9SDimitry Andric return !ExactMatch && GlobPatternMatcher.isTrivialMatchAll(); 42e8d8bef9SDimitry Andric } 43e8d8bef9SDimitry Andric 440b57cec5SDimitry Andric private: 45fe6060f1SDimitry Andric // Whether to do an exact match regardless of wildcard characters. 465ffd83dbSDimitry Andric bool ExactMatch; 475ffd83dbSDimitry Andric 485ffd83dbSDimitry Andric // GlobPattern object if not doing an exact match. 495ffd83dbSDimitry Andric llvm::GlobPattern GlobPatternMatcher; 505ffd83dbSDimitry Andric 515ffd83dbSDimitry Andric // StringRef to match exactly if doing an exact match. 525ffd83dbSDimitry Andric llvm::StringRef ExactPattern; 535ffd83dbSDimitry Andric }; 545ffd83dbSDimitry Andric 555ffd83dbSDimitry Andric // This class represents multiple patterns to match against. A pattern can 565ffd83dbSDimitry Andric // either be a double-quoted text that should be matched exactly after removing 575ffd83dbSDimitry Andric // the quoted marks or a glob pattern. 585ffd83dbSDimitry Andric class StringMatcher { 595ffd83dbSDimitry Andric private: 605ffd83dbSDimitry Andric // Patterns to match against. 615ffd83dbSDimitry Andric std::vector<SingleStringMatcher> patterns; 625ffd83dbSDimitry Andric 635ffd83dbSDimitry Andric public: 645ffd83dbSDimitry Andric StringMatcher() = default; 655ffd83dbSDimitry Andric 665ffd83dbSDimitry Andric // Matcher for a single pattern. StringMatcher(llvm::StringRef Pattern)675ffd83dbSDimitry Andric StringMatcher(llvm::StringRef Pattern) 685ffd83dbSDimitry Andric : patterns({SingleStringMatcher(Pattern)}) {} 695ffd83dbSDimitry Andric 705ffd83dbSDimitry Andric // Add a new pattern to the existing ones to match against. addPattern(SingleStringMatcher Matcher)715ffd83dbSDimitry Andric void addPattern(SingleStringMatcher Matcher) { patterns.push_back(Matcher); } 725ffd83dbSDimitry Andric empty()73e8d8bef9SDimitry Andric bool empty() const { return patterns.empty(); } 745ffd83dbSDimitry Andric 755ffd83dbSDimitry Andric // Match s against the patterns. 765ffd83dbSDimitry Andric bool match(llvm::StringRef s) const; 770b57cec5SDimitry Andric }; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric } // namespace lld 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric #endif 82