[llvm] 76e0ab2 - [FileCheck] - Refactor the code related to string arrays. NFCI.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 20 04:55:03 PDT 2020


Author: Georgii Rymar
Date: 2020-04-20T14:54:49+03:00
New Revision: 76e0ab23f683693c05c5baa365365d7abb22d7c8

URL: https://github.com/llvm/llvm-project/commit/76e0ab23f683693c05c5baa365365d7abb22d7c8
DIFF: https://github.com/llvm/llvm-project/commit/76e0ab23f683693c05c5baa365365d7abb22d7c8.diff

LOG: [FileCheck] - Refactor the code related to string arrays. NFCI.

There are few `std::vector<std::string>` members in
`FileCheckRequest`. This patch changes these arrays to `std::vector<StringRef>`
and refactors the code related to cleanup/improve/simplify it.

Differential revision: https://reviews.llvm.org/D78202

Added: 
    

Modified: 
    llvm/include/llvm/Support/FileCheck.h
    llvm/lib/Support/FileCheck.cpp
    llvm/lib/Support/FileCheckImpl.h
    llvm/unittests/Support/FileCheckTest.cpp
    llvm/utils/FileCheck/FileCheck.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/FileCheck.h b/llvm/include/llvm/Support/FileCheck.h
index 1150882b0b3e..0d1f92bbbf80 100644
--- a/llvm/include/llvm/Support/FileCheck.h
+++ b/llvm/include/llvm/Support/FileCheck.h
@@ -24,10 +24,10 @@ namespace llvm {
 
 /// Contains info about various FileCheck options.
 struct FileCheckRequest {
-  std::vector<std::string> CheckPrefixes;
+  std::vector<StringRef> CheckPrefixes;
   bool NoCanonicalizeWhiteSpace = false;
-  std::vector<std::string> ImplicitCheckNot;
-  std::vector<std::string> GlobalDefines;
+  std::vector<StringRef> ImplicitCheckNot;
+  std::vector<StringRef> GlobalDefines;
   bool AllowEmptyInput = false;
   bool MatchFullLines = false;
   bool IgnoreCase = false;

diff  --git a/llvm/lib/Support/FileCheck.cpp b/llvm/lib/Support/FileCheck.cpp
index 7b70112e3978..b89cdfe4520d 100644
--- a/llvm/lib/Support/FileCheck.cpp
+++ b/llvm/lib/Support/FileCheck.cpp
@@ -1282,13 +1282,13 @@ bool FileCheck::readCheckFile(
   PatternContext->createLineVariable();
 
   std::vector<Pattern> ImplicitNegativeChecks;
-  for (const auto &PatternString : Req.ImplicitCheckNot) {
+  for (StringRef PatternString : Req.ImplicitCheckNot) {
     // Create a buffer with fake command line content in order to display the
     // command line option responsible for the specific implicit CHECK-NOT.
     std::string Prefix = "-implicit-check-not='";
     std::string Suffix = "'";
     std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
-        Prefix + PatternString + Suffix, "command line");
+        (Prefix + PatternString + Suffix).str(), "command line");
 
     StringRef PatternInBuffer =
         CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
@@ -1418,15 +1418,11 @@ bool FileCheck::readCheckFile(
       (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
     errs() << "error: no check strings found with prefix"
            << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
-    auto I = Req.CheckPrefixes.begin();
-    auto E = Req.CheckPrefixes.end();
-    if (I != E) {
-      errs() << "\'" << *I << ":'";
-      ++I;
+    for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
+      if (I != 0)
+        errs() << ", ";
+      errs() << "\'" << Req.CheckPrefixes[I] << ":'";
     }
-    for (; I != E; ++I)
-      errs() << ", \'" << *I << ":'";
-
     errs() << '\n';
     return true;
   }
@@ -1889,7 +1885,7 @@ bool FileCheck::ValidateCheckPrefixes() {
 
   for (StringRef Prefix : Req.CheckPrefixes) {
     // Reject empty prefixes.
-    if (Prefix == "")
+    if (Prefix.empty())
       return false;
 
     if (!PrefixSet.insert(Prefix).second)
@@ -1913,18 +1909,17 @@ Regex FileCheck::buildCheckPrefixRegex() {
   // We already validated the contents of CheckPrefixes so just concatenate
   // them as alternatives.
   SmallString<32> PrefixRegexStr;
-  for (StringRef Prefix : Req.CheckPrefixes) {
-    if (Prefix != Req.CheckPrefixes.front())
+  for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
+    if (I != 0)
       PrefixRegexStr.push_back('|');
-
-    PrefixRegexStr.append(Prefix);
+    PrefixRegexStr.append(Req.CheckPrefixes[I]);
   }
 
   return Regex(PrefixRegexStr);
 }
 
 Error FileCheckPatternContext::defineCmdlineVariables(
-    std::vector<std::string> &CmdlineDefines, SourceMgr &SM) {
+    ArrayRef<StringRef> CmdlineDefines, SourceMgr &SM) {
   assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
          "Overriding defined variable with command-line variable definitions");
 

diff  --git a/llvm/lib/Support/FileCheckImpl.h b/llvm/lib/Support/FileCheckImpl.h
index e65cfeb26959..ac9fb2cc7702 100644
--- a/llvm/lib/Support/FileCheckImpl.h
+++ b/llvm/lib/Support/FileCheckImpl.h
@@ -407,7 +407,7 @@ class FileCheckPatternContext {
   /// command line, passed as a vector of [#]VAR=VAL strings in
   /// \p CmdlineDefines. \returns an error list containing diagnostics against
   /// \p SM for all definition parsing failures, if any, or Success otherwise.
-  Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
+  Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines,
                                SourceMgr &SM);
 
   /// Create @LINE pseudo variable. Value is set when pattern are being

diff  --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp
index 634172826ab2..6b0eee4b36c7 100644
--- a/llvm/unittests/Support/FileCheckTest.cpp
+++ b/llvm/unittests/Support/FileCheckTest.cpp
@@ -528,9 +528,7 @@ class PatternTester {
 
 public:
   PatternTester() {
-    std::vector<std::string> GlobalDefines;
-    GlobalDefines.emplace_back(std::string("#FOO=42"));
-    GlobalDefines.emplace_back(std::string("BAR=BAZ"));
+    std::vector<StringRef> GlobalDefines = {"#FOO=42", "BAR=BAZ"};
     // An ASSERT_FALSE would make more sense but cannot be used in a
     // constructor.
     EXPECT_THAT_ERROR(Context.defineCmdlineVariables(GlobalDefines, SM),
@@ -849,9 +847,7 @@ TEST_F(FileCheckTest, Match) {
 TEST_F(FileCheckTest, Substitution) {
   SourceMgr SM;
   FileCheckPatternContext Context;
-  std::vector<std::string> GlobalDefines;
-  GlobalDefines.emplace_back(std::string("FOO=BAR"));
-  EXPECT_THAT_ERROR(Context.defineCmdlineVariables(GlobalDefines, SM),
+  EXPECT_THAT_ERROR(Context.defineCmdlineVariables({"FOO=BAR"}, SM),
                     Succeeded());
 
   // Substitution of an undefined string variable fails and error holds that
@@ -890,72 +886,53 @@ TEST_F(FileCheckTest, Substitution) {
 
 TEST_F(FileCheckTest, FileCheckContext) {
   FileCheckPatternContext Cxt;
-  std::vector<std::string> GlobalDefines;
   SourceMgr SM;
 
   // No definition.
-  EXPECT_THAT_ERROR(Cxt.defineCmdlineVariables(GlobalDefines, SM), Succeeded());
+  EXPECT_THAT_ERROR(Cxt.defineCmdlineVariables({}, SM), Succeeded());
 
   // Missing equal sign.
-  GlobalDefines.emplace_back(std::string("LocalVar"));
   expectDiagnosticError("missing equal sign in global definition",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("#LocalNumVar"));
+                        Cxt.defineCmdlineVariables({"LocalVar"}, SM));
   expectDiagnosticError("missing equal sign in global definition",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
+                        Cxt.defineCmdlineVariables({"#LocalNumVar"}, SM));
 
   // Empty variable name.
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("=18"));
   expectDiagnosticError("empty variable name",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("#=18"));
+                        Cxt.defineCmdlineVariables({"=18"}, SM));
   expectDiagnosticError("empty variable name",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
+                        Cxt.defineCmdlineVariables({"#=18"}, SM));
 
   // Invalid variable name.
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("18LocalVar=18"));
   expectDiagnosticError("invalid variable name",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("#18LocalNumVar=18"));
+                        Cxt.defineCmdlineVariables({"18LocalVar=18"}, SM));
   expectDiagnosticError("invalid variable name",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
+                        Cxt.defineCmdlineVariables({"#18LocalNumVar=18"}, SM));
 
   // Name conflict between pattern and numeric variable.
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("LocalVar=18"));
-  GlobalDefines.emplace_back(std::string("#LocalVar=36"));
-  expectDiagnosticError("string variable with name 'LocalVar' already exists",
-                        Cxt.defineCmdlineVariables(GlobalDefines, SM));
+  expectDiagnosticError(
+      "string variable with name 'LocalVar' already exists",
+      Cxt.defineCmdlineVariables({"LocalVar=18", "#LocalVar=36"}, SM));
   Cxt = FileCheckPatternContext();
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("#LocalNumVar=18"));
-  GlobalDefines.emplace_back(std::string("LocalNumVar=36"));
   expectDiagnosticError(
       "numeric variable with name 'LocalNumVar' already exists",
-      Cxt.defineCmdlineVariables(GlobalDefines, SM));
+      Cxt.defineCmdlineVariables({"#LocalNumVar=18", "LocalNumVar=36"}, SM));
   Cxt = FileCheckPatternContext();
 
   // Invalid numeric value for numeric variable.
-  GlobalDefines.clear();
-  GlobalDefines.emplace_back(std::string("#LocalNumVar=x"));
-  expectUndefErrors({"x"}, Cxt.defineCmdlineVariables(GlobalDefines, SM));
+  expectUndefErrors({"x"}, Cxt.defineCmdlineVariables({"#LocalNumVar=x"}, SM));
 
   // Define local variables from command-line.
-  GlobalDefines.clear();
+  std::vector<StringRef> GlobalDefines;
   // Clear local variables to remove dummy numeric variable x that
   // parseNumericSubstitutionBlock would have created and stored in
   // GlobalNumericVariableTable.
   Cxt.clearLocalVars();
-  GlobalDefines.emplace_back(std::string("LocalVar=FOO"));
-  GlobalDefines.emplace_back(std::string("EmptyVar="));
-  GlobalDefines.emplace_back(std::string("#LocalNumVar1=18"));
-  GlobalDefines.emplace_back(std::string("#%x,LocalNumVar2=LocalNumVar1+2"));
-  GlobalDefines.emplace_back(std::string("#LocalNumVar3=0xc"));
+  GlobalDefines.emplace_back("LocalVar=FOO");
+  GlobalDefines.emplace_back("EmptyVar=");
+  GlobalDefines.emplace_back("#LocalNumVar1=18");
+  GlobalDefines.emplace_back("#%x,LocalNumVar2=LocalNumVar1+2");
+  GlobalDefines.emplace_back("#LocalNumVar3=0xc");
   ASSERT_THAT_ERROR(Cxt.defineCmdlineVariables(GlobalDefines, SM), Succeeded());
 
   // Create @LINE pseudo numeric variable and check it is present by matching
@@ -1045,8 +1022,8 @@ TEST_F(FileCheckTest, FileCheckContext) {
   Cxt.clearLocalVars();
 
   // Redefine global variables and check variables are defined again.
-  GlobalDefines.emplace_back(std::string("$GlobalVar=BAR"));
-  GlobalDefines.emplace_back(std::string("#$GlobalNumVar=36"));
+  GlobalDefines.emplace_back("$GlobalVar=BAR");
+  GlobalDefines.emplace_back("#$GlobalNumVar=36");
   ASSERT_THAT_ERROR(Cxt.defineCmdlineVariables(GlobalDefines, SM), Succeeded());
   StringRef GlobalVarStr = "$GlobalVar";
   StringRef GlobalNumVarRef = bufferize(SM, "$GlobalNumVar");

diff  --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index ef6c62b5ab5b..3f7d77711ee0 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -562,14 +562,14 @@ int main(int argc, char **argv) {
   }
 
   FileCheckRequest Req;
-  for (auto Prefix : CheckPrefixes)
+  for (StringRef Prefix : CheckPrefixes)
     Req.CheckPrefixes.push_back(Prefix);
 
-  for (auto CheckNot : ImplicitCheckNot)
+  for (StringRef CheckNot : ImplicitCheckNot)
     Req.ImplicitCheckNot.push_back(CheckNot);
 
   bool GlobalDefineError = false;
-  for (auto G : GlobalDefines) {
+  for (StringRef G : GlobalDefines) {
     size_t EqIdx = G.find('=');
     if (EqIdx == std::string::npos) {
       errs() << "Missing equal sign in command-line definition '-D" << G


        


More information about the llvm-commits mailing list