[llvm] r185697 - Don't create an archive if, for example, we are asked to print the index.

Rafael Espindola rafael.espindola at gmail.com
Fri Jul 5 06:03:08 PDT 2013


Author: rafael
Date: Fri Jul  5 08:03:07 2013
New Revision: 185697

URL: http://llvm.org/viewvc/llvm-project?rev=185697&view=rev
Log:
Don't create an archive if, for example, we are asked to print the index.

Added:
    llvm/trunk/test/Object/ar-create.test
Modified:
    llvm/trunk/tools/llvm-ar/llvm-ar.cpp

Added: llvm/trunk/test/Object/ar-create.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/ar-create.test?rev=185697&view=auto
==============================================================================
--- llvm/trunk/test/Object/ar-create.test (added)
+++ llvm/trunk/test/Object/ar-create.test Fri Jul  5 08:03:07 2013
@@ -0,0 +1,17 @@
+Test which operations create an archive and which don't.
+
+RUN: touch %t
+RUN: rm -f %t.foo.a
+RUN: not llvm-ar p %t.foo.a %t 2>&1 | FileCheck %s
+RUN: not llvm-ar d %t.foo.a %t 2>&1 | FileCheck %s
+RUN: not llvm-ar m %t.foo.a %t 2>&1 | FileCheck %s
+RUN: not llvm-ar t %t.foo.a %t 2>&1 | FileCheck %s
+RUN: not llvm-ar x %t.foo.a %t 2>&1 | FileCheck %s
+
+RUN: llvm-ar q %t.foo.a %t 2>&1 | FileCheck --check-prefix=CREATE %s
+RUN: rm -f %t.foo.a
+RUN: llvm-ar r %t.foo.a %t 2>&1 | FileCheck --check-prefix=CREATE %s
+RUN: rm -f %t.foo.a
+
+CHECK: .foo.a': No such file or directory
+CREATE: creating {{.*}}.foo.a

Modified: llvm/trunk/tools/llvm-ar/llvm-ar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/llvm-ar.cpp?rev=185697&r1=185696&r2=185697&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ar/llvm-ar.cpp (original)
+++ llvm/trunk/tools/llvm-ar/llvm-ar.cpp Fri Jul  5 08:03:07 2013
@@ -642,6 +642,21 @@ doReplaceOrInsert(std::string* ErrMsg) {
   return false;
 }
 
+bool shouldCreateArchive(ArchiveOperation Op) {
+  switch (Op) {
+  case Print:
+  case Delete:
+  case Move:
+  case DisplayTable:
+  case Extract:
+    return false;
+
+  case QuickAppend:
+  case ReplaceOrInsert:
+    return true;
+  }
+}
+
 // main - main program for llvm-ar .. see comments in the code
 int main(int argc, char **argv) {
   program_name = argv[0];
@@ -665,13 +680,15 @@ int main(int argc, char **argv) {
   ArchiveOperation Operation = parseCommandLine();
 
   // Create or open the archive object.
-  if (!llvm::sys::fs::exists(ArchiveName)) {
+  if (shouldCreateArchive(Operation) && !llvm::sys::fs::exists(ArchiveName)) {
     // Produce a warning if we should and we're creating the archive
     if (!Create)
       errs() << argv[0] << ": creating " << ArchiveName << "\n";
     TheArchive = Archive::CreateEmpty(ArchiveName, Context);
     TheArchive->writeToDisk();
-  } else {
+  }
+
+  if (!TheArchive) {
     std::string Error;
     TheArchive = Archive::OpenAndLoad(ArchiveName, Context, &Error);
     if (TheArchive == 0) {





More information about the llvm-commits mailing list