From 61de142984aa6cb1acfe4725183193d04e437420 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 5 Jul 2013 13:03:07 +0000 Subject: [PATCH] Don't create an archive if, for example, we are asked to print the index. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185697 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Object/ar-create.test | 17 +++++++++++++++++ tools/llvm-ar/llvm-ar.cpp | 21 +++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/Object/ar-create.test diff --git a/test/Object/ar-create.test b/test/Object/ar-create.test new file mode 100644 index 00000000000..40e391da029 --- /dev/null +++ b/test/Object/ar-create.test @@ -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 diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp index 53707154910..515f8991e84 100644 --- a/tools/llvm-ar/llvm-ar.cpp +++ b/tools/llvm-ar/llvm-ar.cpp @@ -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) {