From 54195c1a9d8ac23d2dc1dfca818384cd26abe19b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 23 Oct 2005 05:26:26 +0000 Subject: [PATCH] Move static functions from .h file, reduce #includes, pass strings by const&, use LowercaseString from StringExtras.h, remove extraneous space from help output. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23891 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/SubtargetFeature.cpp | 144 ++++++++++++++++++++++---------- 1 file changed, 99 insertions(+), 45 deletions(-) diff --git a/lib/Target/SubtargetFeature.cpp b/lib/Target/SubtargetFeature.cpp index 29afeff8778..a9ed84f7898 100644 --- a/lib/Target/SubtargetFeature.cpp +++ b/lib/Target/SubtargetFeature.cpp @@ -12,18 +12,55 @@ //===----------------------------------------------------------------------===// #include "llvm/Target/SubtargetFeature.h" - -#include +#include "llvm/ADT/StringExtras.h" #include -#include #include #include - +#include using namespace llvm; -/// Splits a string of comma separated items in to a vector of strings. -void SubtargetFeatures::Split(std::vector &V, - const std::string &S) { +//===----------------------------------------------------------------------===// +// Static Helper Functions +//===----------------------------------------------------------------------===// + +/// hasFlag - Determine if a feature has a flag; '+' or '-' +/// +static inline bool hasFlag(const std::string &Feature) { + assert(!Feature.empty() && "Empty string"); + // Get first character + char Ch = Feature[0]; + // Check if first character is '+' or '-' flag + return Ch == '+' || Ch =='-'; +} + +/// StripFlag - Return string stripped of flag. +/// +static inline std::string StripFlag(const std::string &Feature) { + return hasFlag(Feature) ? Feature.substr(1) : Feature; +} + +/// isEnabled - Return true if enable flag; '+'. +/// +static inline bool isEnabled(const std::string &Feature) { + assert(!Feature.empty() && "Empty string"); + // Get first character + char Ch = Feature[0]; + // Check if first character is '+' for enabled + return Ch == '+'; +} + +/// PrependFlag - Return a string with a prepended flag; '+' or '-'. +/// +static inline std::string PrependFlag(const std::string &Feature, + bool IsEnabled) { + assert(!Feature.empty() && "Empty string"); + if (hasFlag(Feature)) return Feature; + return std::string(IsEnabled ? "+" : "-") + Feature; +} + +/// Split - Splits a string of comma separated items in to a vector of strings. +/// +static void Split(std::vector &V, const std::string &S) { // Start at beginning of string. size_t Pos = 0; while (true) { @@ -43,7 +80,8 @@ void SubtargetFeatures::Split(std::vector &V, } /// Join a vector of strings to a string with a comma separating each element. -std::string SubtargetFeatures::Join(const std::vector &V) { +/// +static std::string Join(const std::vector &V) { // Start with empty string. std::string Result; // If the vector is not empty @@ -62,33 +100,19 @@ std::string SubtargetFeatures::Join(const std::vector &V) { return Result; } -/// Convert a string to lowercase. -std::string SubtargetFeatures::toLower(const std::string &S) { - // Copy the string - std::string Result = S; - // For each character in string - for (size_t i = 0; i < Result.size(); i++) { - // Convert character to lowercase - Result[i] = std::tolower(Result[i]); - } - // Return the lowercased string - return Result; -} - /// Adding features. void SubtargetFeatures::AddFeature(const std::string &String, bool IsEnabled) { // Don't add empty features if (!String.empty()) { // Convert to lowercase, prepend flag and add to vector - Features.push_back(PrependFlag(toLower(String), IsEnabled)); + Features.push_back(PrependFlag(LowercaseString(String), IsEnabled)); } } /// Find item in array using binary search. -const SubtargetFeatureKV * -SubtargetFeatures::Find(const std::string &S, - const SubtargetFeatureKV *A, size_t L) { +static const SubtargetFeatureKV *Find(const std::string &S, + const SubtargetFeatureKV *A, size_t L) { // Determine the end of the array const SubtargetFeatureKV *Hi = A + L; // Binary search the array @@ -100,29 +124,59 @@ SubtargetFeatures::Find(const std::string &S, } /// Display help for feature choices. -void SubtargetFeatures::Help(const char *Heading, - const SubtargetFeatureKV *Table, size_t TableSize) { - // Determine the length of the longest key - size_t MaxLen = 0; - for (size_t i = 0; i < TableSize; i++) - MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); - // Print heading - std::cerr << "Help for " << Heading << " choices\n\n"; - // For each feature - for (size_t i = 0; i < TableSize; i++) { - // Compute required padding - size_t Pad = MaxLen - std::strlen(Table[i].Key) + 1; - // Print details - std::cerr << Table[i].Key << std::string(Pad, ' ') << " - " - << Table[i].Desc << "\n"; - } - // Wrap it up - std::cerr << "\n\n"; - // Leave tool - exit(1); +/// +static void Help(const char *Heading, const SubtargetFeatureKV *Table, + size_t TableSize) { + // Determine the length of the longest key + size_t MaxLen = 0; + for (size_t i = 0; i < TableSize; i++) + MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); + // Print heading + std::cerr << "Help for " << Heading << " choices:\n\n"; + // For each feature + for (size_t i = 0; i < TableSize; i++) { + // Compute required padding + size_t Pad = MaxLen - std::strlen(Table[i].Key); + // Print details + std::cerr << Table[i].Key << std::string(Pad, ' ') << " - " + << Table[i].Desc << "\n"; + } + // Wrap it up + std::cerr << "\n\n"; + // Leave tool + exit(1); +} + +//===----------------------------------------------------------------------===// +// SubtargetFeatures Implementation +//===----------------------------------------------------------------------===// + +SubtargetFeatures::SubtargetFeatures(const std::string &Initial) { + // Break up string into separate features + Split(Features, Initial); } + +std::string SubtargetFeatures::getString() const { + return Join(Features); +} +void SubtargetFeatures::setString(const std::string &Initial) { + // Throw out old features + Features.clear(); + // Break up string into separate features + Split(Features, LowercaseString(Initial)); +} + +/// setCPU - Set the CPU string. Replaces previous setting. Setting to "" +/// clears CPU. +void SubtargetFeatures::setCPU(const std::string &String) { + Features[0] = LowercaseString(String); +} + + + /// Parse feature string for quick usage. +/// uint32_t SubtargetFeatures::Parse(const std::string &String, const std::string &DefaultCPU, const SubtargetFeatureKV *CPUTable, -- 2.34.1