Convert a few std::string with StringRef. NFC.
[oota-llvm.git] / tools / llvm-ar / llvm-ar.cpp
index 785cabd08ff87ab814e35102ed09dc0939a499cb..22fa291fbabafda4028d986de999ec9774928a60 100644 (file)
@@ -47,7 +47,7 @@ static StringRef ToolName;
 static const char *TemporaryOutput;
 static int TmpArchiveFD = -1;
 
-// fail - Show the error message and exit.
+// Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
   outs() << ToolName << ": " << Error << ".\n";
   if (TmpArchiveFD != -1)
@@ -76,9 +76,9 @@ static cl::opt<bool> MRI("M", cl::desc(""));
 
 std::string Options;
 
-// MoreHelp - Provide additional help output explaining the operations and
-// modifiers of llvm-ar. This object instructs the CommandLine library
-// to print the text of the constructor when the --help option is given.
+// Provide additional help output explaining the operations and modifiers of
+// llvm-ar. This object instructs the CommandLine library to print the text of
+// the constructor when the --help option is given.
 static cl::extrahelp MoreHelp(
   "\nOPERATIONS:\n"
   "  d[NsS]       - delete file(s) from the archive\n"
@@ -136,9 +136,9 @@ static std::string ArchiveName;
 
 // This variable holds the list of member files to proecess, as given
 // on the command line.
-static std::vector<std::string> Members;
+static std::vector<StringRef> Members;
 
-// show_help - Show the error message, the help message and exit.
+// Show the error message, the help message and exit.
 LLVM_ATTRIBUTE_NORETURN static void
 show_help(const std::string &msg) {
   errs() << ToolName << ": " << msg << "\n\n";
@@ -146,8 +146,8 @@ show_help(const std::string &msg) {
   std::exit(1);
 }
 
-// getRelPos - Extract the member filename from the command line for
-// the [relpos] argument associated with a, b, and i modifiers
+// Extract the member filename from the command line for the [relpos] argument
+// associated with a, b, and i modifiers
 static void getRelPos() {
   if(RestOfArgs.size() == 0)
     show_help("Expected [relpos] for a, b, or i modifier");
@@ -162,7 +162,7 @@ static void getOptions() {
   RestOfArgs.erase(RestOfArgs.begin());
 }
 
-// getArchive - Get the archive file name from the command line
+// Get the archive file name from the command line
 static void getArchive() {
   if(RestOfArgs.size() == 0)
     show_help("An archive name must be specified");
@@ -170,17 +170,17 @@ static void getArchive() {
   RestOfArgs.erase(RestOfArgs.begin());
 }
 
-// getMembers - Copy over remaining items in RestOfArgs to our Members vector
-// This is just for clarity.
+// Copy over remaining items in RestOfArgs to our Members vector
 static void getMembers() {
-  if(RestOfArgs.size() > 0)
-    Members = std::vector<std::string>(RestOfArgs);
+  for (auto &Arg : RestOfArgs)
+    Members.push_back(Arg);
 }
 
 namespace {
 enum class MRICommand { AddMod, Create, Save, End, Invalid };
 }
 
+static std::vector<std::string> MRIMembers;
 static ArchiveOperation parseMRIScript() {
   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
   failIfError(Buf.getError());
@@ -200,7 +200,7 @@ static ArchiveOperation parseMRIScript() {
 
     switch (Command) {
     case MRICommand::AddMod:
-      Members.push_back(Rest);
+      MRIMembers.push_back(Rest);
       break;
     case MRICommand::Create:
       Create = true;
@@ -223,12 +223,16 @@ static ArchiveOperation parseMRIScript() {
   // Nothing to do if not saved.
   if (!Saved)
     exit(0);
+
+  for (auto &M : MRIMembers)
+    Members.push_back(M);
+
   return ReplaceOrInsert;
 }
 
-// parseCommandLine - Parse the command line options as presented and return the
-// operation specified. Process all modifiers and check to make sure that
-// constraints on modifier/operation pairs have not been violated.
+// Parse the command line options as presented and return the operation
+// specified. Process all modifiers and check to make sure that constraints on
+// modifier/operation pairs have not been violated.
 static ArchiveOperation parseCommandLine() {
   if (MRI) {
     if (!RestOfArgs.empty())
@@ -338,8 +342,8 @@ static void doPrint(StringRef Name, object::Archive::child_iterator I) {
   outs().write(Data.data(), Data.size());
 }
 
-// putMode - utility function for printing out the file mode when the 't'
-// operation is in verbose mode.
+// Utility function for printing out the file mode when the 't' operation is in
+// verbose mode.
 static void printMode(unsigned mode) {
   if (mode & 004)
     outs() << "r";
@@ -460,20 +464,20 @@ class NewArchiveIterator {
 
   object::Archive::child_iterator OldI;
 
-  std::string NewFilename;
+  StringRef NewFilename;
   mutable int NewFD;
   mutable sys::fs::file_status NewStatus;
 
 public:
   NewArchiveIterator(object::Archive::child_iterator I, StringRef Name);
-  NewArchiveIterator(std::string *I, StringRef Name);
+  NewArchiveIterator(StringRef I, StringRef Name);
   NewArchiveIterator();
   bool isNewMember() const;
   StringRef getName() const;
 
   object::Archive::child_iterator getOld() const;
 
-  const char *getNew() const;
+  StringRef getNew() const;
   int getFD() const;
   const sys::fs::file_status &getStatus() const;
 };
@@ -485,8 +489,8 @@ NewArchiveIterator::NewArchiveIterator(object::Archive::child_iterator I,
                                        StringRef Name)
     : IsNewMember(false), Name(Name), OldI(I) {}
 
-NewArchiveIterator::NewArchiveIterator(std::string *NewFilename, StringRef Name)
-    : IsNewMember(true), Name(Name), NewFilename(*NewFilename), NewFD(-1) {}
+NewArchiveIterator::NewArchiveIterator(StringRef NewFilename, StringRef Name)
+    : IsNewMember(true), Name(Name), NewFilename(NewFilename), NewFD(-1) {}
 
 StringRef NewArchiveIterator::getName() const { return Name; }
 
@@ -497,9 +501,9 @@ object::Archive::child_iterator NewArchiveIterator::getOld() const {
   return OldI;
 }
 
-const char *NewArchiveIterator::getNew() const {
+StringRef NewArchiveIterator::getNew() const {
   assert(IsNewMember);
-  return NewFilename.c_str();
+  return NewFilename;
 }
 
 int NewArchiveIterator::getFD() const {
@@ -544,16 +548,17 @@ enum InsertAction {
   IA_MoveNewMember
 };
 
-static InsertAction
-computeInsertAction(ArchiveOperation Operation,
-                    object::Archive::child_iterator I, StringRef Name,
-                    std::vector<std::string>::iterator &Pos) {
+static InsertAction computeInsertAction(ArchiveOperation Operation,
+                                        object::Archive::child_iterator I,
+                                        StringRef Name,
+                                        std::vector<StringRef>::iterator &Pos) {
   if (Operation == QuickAppend || Members.empty())
     return IA_AddOldMember;
 
-  std::vector<std::string>::iterator MI = std::find_if(
-      Members.begin(), Members.end(),
-      [Name](StringRef Path) { return Name == sys::path::filename(Path); });
+  auto MI =
+      std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
+        return Name == sys::path::filename(Path);
+      });
 
   if (MI == Members.end())
     return IA_AddOldMember;
@@ -616,14 +621,14 @@ computeNewArchiveMembers(ArchiveOperation Operation,
           InsertPos = Pos + 1;
       }
 
-      std::vector<std::string>::iterator MemberI = Members.end();
+      std::vector<StringRef>::iterator MemberI = Members.end();
       InsertAction Action = computeInsertAction(Operation, I, Name, MemberI);
       switch (Action) {
       case IA_AddOldMember:
         addMember(Ret, I, Name);
         break;
       case IA_AddNewMeber:
-        addMember(Ret, &*MemberI, Name);
+        addMember(Ret, *MemberI, Name);
         break;
       case IA_Delete:
         break;
@@ -631,7 +636,7 @@ computeNewArchiveMembers(ArchiveOperation Operation,
         addMember(Moved, I, Name);
         break;
       case IA_MoveNewMember:
-        addMember(Moved, &*MemberI, Name);
+        addMember(Moved, *MemberI, Name);
         break;
       }
       if (MemberI != Members.end())
@@ -653,11 +658,10 @@ computeNewArchiveMembers(ArchiveOperation Operation,
 
   Ret.insert(Ret.begin() + InsertPos, Members.size(), NewArchiveIterator());
   int Pos = InsertPos;
-  for (std::vector<std::string>::iterator I = Members.begin(),
-         E = Members.end();
-       I != E; ++I, ++Pos) {
-    StringRef Name = sys::path::filename(*I);
-    addMember(Ret, &*I, Name, Pos);
+  for (auto &Member : Members) {
+    StringRef Name = sys::path::filename(Member);
+    addMember(Ret, Member, Name, Pos);
+    ++Pos;
   }
 
   return Ret;
@@ -825,7 +829,7 @@ static void performWriteOperation(ArchiveOperation Operation,
     MemoryBufferRef MemberRef;
 
     if (Member.isNewMember()) {
-      const char *Filename = Member.getNew();
+      StringRef Filename = Member.getNew();
       int FD = Member.getFD();
       const sys::fs::file_status &Status = Member.getStatus();
       ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
@@ -870,7 +874,7 @@ static void performWriteOperation(ArchiveOperation Operation,
 
     MemoryBufferRef File = Members[MemberNum];
     if (I->isNewMember()) {
-      const char *FileName = I->getNew();
+      StringRef FileName = I->getNew();
       const sys::fs::file_status &Status = I->getStatus();
 
       StringRef Name = sys::path::filename(FileName);
@@ -945,52 +949,6 @@ static void performOperation(ArchiveOperation Operation,
   llvm_unreachable("Unknown operation.");
 }
 
-static int ar_main(char **argv);
-static int ranlib_main();
-
-// main - main program for llvm-ar .. see comments in the code
-int main(int argc, char **argv) {
-  ToolName = argv[0];
-  // Print a stack trace if we signal out.
-  sys::PrintStackTraceOnErrorSignal();
-  PrettyStackTraceProgram X(argc, argv);
-  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
-
-  // Have the command line options parsed and handle things
-  // like --help and --version.
-  cl::ParseCommandLineOptions(argc, argv,
-    "LLVM Archiver (llvm-ar)\n\n"
-    "  This program archives bitcode files into single libraries\n"
-  );
-
-  llvm::InitializeAllTargetInfos();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmParsers();
-
-  StringRef Stem = sys::path::stem(ToolName);
-  if (Stem.find("ar") != StringRef::npos)
-    return ar_main(argv);
-  if (Stem.find("ranlib") != StringRef::npos)
-    return ranlib_main();
-  fail("Not ranlib or ar!");
-}
-
-static int performOperation(ArchiveOperation Operation);
-
-int ranlib_main() {
-  if (RestOfArgs.size() != 1)
-    fail(ToolName + "takes just one archive as argument");
-  ArchiveName = RestOfArgs[0];
-  return performOperation(CreateSymTab);
-}
-
-int ar_main(char **argv) {
-  // Do our own parsing of the command line because the CommandLine utility
-  // can't handle the grouped positional parameters without a dash.
-  ArchiveOperation Operation = parseCommandLine();
-  return performOperation(Operation);
-}
-
 static int performOperation(ArchiveOperation Operation) {
   // Create or open the archive object.
   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
@@ -1028,3 +986,43 @@ static int performOperation(ArchiveOperation Operation) {
   performOperation(Operation, nullptr);
   return 0;
 }
+
+int ar_main(char **argv) {
+  // Do our own parsing of the command line because the CommandLine utility
+  // can't handle the grouped positional parameters without a dash.
+  ArchiveOperation Operation = parseCommandLine();
+  return performOperation(Operation);
+}
+
+int ranlib_main() {
+  if (RestOfArgs.size() != 1)
+    fail(ToolName + "takes just one archive as argument");
+  ArchiveName = RestOfArgs[0];
+  return performOperation(CreateSymTab);
+}
+
+int main(int argc, char **argv) {
+  ToolName = argv[0];
+  // Print a stack trace if we signal out.
+  sys::PrintStackTraceOnErrorSignal();
+  PrettyStackTraceProgram X(argc, argv);
+  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
+
+  // Have the command line options parsed and handle things
+  // like --help and --version.
+  cl::ParseCommandLineOptions(argc, argv,
+    "LLVM Archiver (llvm-ar)\n\n"
+    "  This program archives bitcode files into single libraries\n"
+  );
+
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllTargetMCs();
+  llvm::InitializeAllAsmParsers();
+
+  StringRef Stem = sys::path::stem(ToolName);
+  if (Stem.find("ar") != StringRef::npos)
+    return ar_main(argv);
+  if (Stem.find("ranlib") != StringRef::npos)
+    return ranlib_main();
+  fail("Not ranlib or ar!");
+}