[llvm] 7de277d - [llvm-ar] Improve MRI script CREATE command handling

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 27 03:11:36 PDT 2022


Author: gbreynoo
Date: 2022-06-27T11:11:16+01:00
New Revision: 7de277d684fdbb12f5bfa761efe7dbbed681137d

URL: https://github.com/llvm/llvm-project/commit/7de277d684fdbb12f5bfa761efe7dbbed681137d
DIFF: https://github.com/llvm/llvm-project/commit/7de277d684fdbb12f5bfa761efe7dbbed681137d.diff

LOG: [llvm-ar] Improve MRI script CREATE command handling

I discovered that when compared to GNU the llvm-ar MRI script parsing of
CREATE could lead to some strange behaviour. This fix improves the error
message in the case when no archive name is given and will not allow the
adding of members until CREATE is called. Along with this change I added
more testing of the CREATE command.

Differential Revision: https://reviews.llvm.org/D128055

Added: 
    llvm/test/tools/llvm-ar/mri-create.test

Modified: 
    llvm/test/tools/llvm-ar/mri-addlib.test
    llvm/test/tools/llvm-ar/mri-addmod.test
    llvm/tools/llvm-ar/llvm-ar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-ar/mri-addlib.test b/llvm/test/tools/llvm-ar/mri-addlib.test
index ee692ea9fbddd..2d566ebec1d81 100644
--- a/llvm/test/tools/llvm-ar/mri-addlib.test
+++ b/llvm/test/tools/llvm-ar/mri-addlib.test
@@ -1,34 +1,44 @@
 ## Test the ADDLIB MRI command.
 
-# RUN: rm -rf %t && mkdir -p %t
-# RUN: yaml2obj %s -o %t/f.o
-# RUN: llvm-ar r %t/f.a %t/f.o
+# RUN: rm -rf %t && split-file %s %t
+# RUN: cd %t
+# RUN: yaml2obj %s -o f.o
+# RUN: llvm-ar r f.a f.o
 
 ## Merge contents of archives.
-# RUN: echo "CREATE %t/addlib.a" > %t/addlib.mri
-# RUN: echo "ADDLIB %t/f.a" >> %t/addlib.mri
-# RUN: echo "SAVE" >> %t/addlib.mri
-# RUN: llvm-ar -M < %t/addlib.mri
-# RUN: llvm-nm --print-armap %t/addlib.a | FileCheck --check-prefix=SYMS %s
-# RUN: llvm-ar t %t/addlib.a | FileCheck --check-prefix=FILES %s
+# RUN: llvm-ar -M < add-lib.mri
+# RUN: llvm-nm --print-armap add-lib.a | FileCheck --check-prefix=SYMS %s
+# RUN: llvm-ar t add-lib.a | FileCheck --check-prefix=FILES %s
 
 # SYMS: f in {{.*}}
 # FILES: f.o
 
 ## ADDLIB with non-archive file.
-# RUN: echo "CREATE %t/badlib.a" > %t/badlib.mri
-# RUN: echo "ADDLIB %s" >> %t/badlib.mri
-# RUN: echo "SAVE" >> %t/badlib.mri
-# RUN: not llvm-ar -M < %t/badlib.mri 2>&1 | FileCheck --check-prefix=PARSE %s
-# RUN: not ls %t/badlib.a
+# RUN: not llvm-ar -M < bad-lib.mri 2>&1 | FileCheck --check-prefix=PARSE %s
+# RUN: not ls bad-lib.a
 
 # PARSE: error: script line 2: could not parse library
 
 ## No create command.
-# RUN: echo "ADDLIB %t/f.a" > %t/nocreate.mri
-# RUN: echo "SAVE" >> %t/nocreate.mri
-# RUN: not llvm-ar -M < %t/nocreate.mri
+# RUN: not llvm-ar -M < no-create.mri 2>&1 | FileCheck --check-prefix=NOCREATE %s
 
+# NOCREATE: error: script line 1: no output archive has been opened
+
+## ADDLIB with missing file.
+# RUN: not llvm-ar -M < missing.mri 2>&1 | FileCheck -DMSG=%errc_ENOENT %s --check-prefix=MISSING
+# RUN: not ls missing.a
+
+# MISSING: error: script line 2: could not open library missing-lib.a: [[MSG]]
+
+## ADD same lib twice.
+# RUN: llvm-ar -M < duplicate.mri
+# RUN: llvm-nm --print-armap duplicate.a | FileCheck --check-prefix=SYMS2 %s
+# RUN: llvm-ar t duplicate.a | FileCheck --check-prefix=FILES2 %s
+
+# SYMS2-COUNT-2: f in {{.*}}
+# FILES2-COUNT-2: f.o
+
+#--- f.yaml
 --- !ELF
 FileHeader:
   Class:   ELFCLASS64
@@ -43,3 +53,30 @@ Symbols:
       Binding: STB_GLOBAL
       Section: .text
 ...
+#--- add-lib.mri
+CREATE add-lib.a
+ADDLIB f.a
+SAVE
+
+#--- text.txt
+I AM NOT AN ARCHIVE
+
+#--- bad-lib.mri
+CREATE bad-lib.a
+ADDLIB text.txt
+SAVE
+
+#--- no-create.mri
+ADDLIB f.a
+SAVE
+
+#--- missing.mri
+CREATE missing.a
+ADDLIB missing-lib.a
+SAVE
+
+#--- duplicate.mri
+CREATE duplicate.a
+ADDLIB f.a
+ADDLIB f.a
+SAVE

diff  --git a/llvm/test/tools/llvm-ar/mri-addmod.test b/llvm/test/tools/llvm-ar/mri-addmod.test
index c26414fad0b8c..824e6f13a1504 100644
--- a/llvm/test/tools/llvm-ar/mri-addmod.test
+++ b/llvm/test/tools/llvm-ar/mri-addmod.test
@@ -1,16 +1,36 @@
 ## Test the ADDMOD MRI command.
 
-# RUN: rm -rf %t && mkdir -p %t
-# RUN: yaml2obj %s -o %t/f.o
+# RUN: rm -rf %t && split-file %s %t
+# RUN: cd %t
+# RUN: yaml2obj %s -o f.o
 
-# RUN: echo "CREATE %t/addmod.a" > %t/addmod.mri
-# RUN: echo "ADDMOD %t/f.o" >> %t/addmod.mri
-# RUN: echo "SAVE" >> %t/addmod.mri
-# RUN: llvm-ar -M < %t/addmod.mri
-# RUN: llvm-nm --print-armap %t/addmod.a | FileCheck %s
+# RUN: llvm-ar -M < add-mod.mri
+# RUN: llvm-nm --print-armap add-mod.a | FileCheck --check-prefix=SYMS %s
+# RUN: llvm-ar t add-mod.a | FileCheck --check-prefix=FILES %s
 
-# CHECK: f in f.o
+# SYMS: f in {{.*}}
+# FILES: f.o
 
+## No create command.
+# RUN: not llvm-ar -M < no-create.mri 2>&1 | FileCheck --check-prefix=NOCREATE %s
+
+# NOCREATE: error: script line 1: no output archive has been opened
+
+## ADDMOD with missing file.
+# RUN: not llvm-ar -M < missing.mri 2>&1 | FileCheck -DMSG=%errc_ENOENT %s --check-prefix=MISSING
+# RUN: not ls missing.a
+
+# MISSING: error: script line 2: missing.o: [[MSG]]
+
+## ADD same file twice.
+# RUN: llvm-ar -M < duplicate.mri
+# RUN: llvm-nm --print-armap duplicate.a | FileCheck --check-prefix=SYMS2 %s
+# RUN: llvm-ar t duplicate.a | FileCheck --check-prefix=FILES2 %s
+
+# SYMS2-COUNT-2: f in {{.*}}
+# FILES2-COUNT-2: f.o
+
+#--- f.yaml
 --- !ELF
 FileHeader:
   Class:   ELFCLASS64
@@ -25,3 +45,22 @@ Symbols:
       Binding: STB_GLOBAL
       Section: .text
 ...
+#--- add-mod.mri
+CREATE add-mod.a
+ADDMOD f.o
+SAVE
+
+#--- no-create.mri
+ADDMOD f.o
+SAVE
+
+#--- missing.mri
+CREATE missing.a
+ADDMOD missing.o
+SAVE
+
+#--- duplicate.mri
+CREATE duplicate.a
+ADDMOD f.o
+ADDMOD f.o
+SAVE

diff  --git a/llvm/test/tools/llvm-ar/mri-create.test b/llvm/test/tools/llvm-ar/mri-create.test
new file mode 100644
index 0000000000000..d44601f20e7d2
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/mri-create.test
@@ -0,0 +1,113 @@
+## Test the CREATE MRI command.
+
+# RUN: rm -rf %t && mkdir -p %t/path/ && split-file %s %t
+# RUN: cd %t
+
+# RUN: llvm-ar -M < create.mri
+# RUN: llvm-ar -M < thin.mri
+
+## Test use of CREATE with no archive name.
+# RUN: not llvm-ar -M < none.mri 2>&1 | FileCheck --check-prefix=NONE %s
+# RUN: not llvm-ar -M < thin-none.mri 2>&1 | FileCheck --check-prefix=NONE %s
+# RUN: not llvm-ar -M < none-add.mri 2>&1 | FileCheck --check-prefix=NONE %s
+# RUN: not llvm-ar -M < thin-none-add.mri 2>&1 | FileCheck --check-prefix=NONE %s
+
+# NONE: error: script line 1: missing archive name
+
+## Test use of paths with CREATE.
+# RUN: llvm-ar -M < path.mri
+# RUN: ls path/path.a
+# RUN: llvm-ar -M < thin-path.mri
+# RUN: ls path/thin-path.a
+
+## Test bad paths with CREATE.
+# RUN: not llvm-ar -M < bad.mri 2>&1 | FileCheck --check-prefix=BAD -DMSG=%errc_ENOENT %s
+# RUN: not llvm-ar -M < thin-bad.mri 2>&1 | FileCheck --check-prefix=BAD -DMSG=%errc_ENOENT %s
+# BAD: error: bad/bad.a: [[MSG]]
+
+## Test duplicate use of CREATE.
+# RUN: not llvm-ar -M < create2.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=2
+# RUN: not llvm-ar -M < thin2.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=2
+# RUN: not llvm-ar -M < mix1.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=2
+# RUN: not llvm-ar -M < mix2.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=2
+# RUN: not llvm-ar -M < save.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=3
+# RUN: not llvm-ar -M < thin-save.mri 2>&1 | FileCheck --check-prefix=MULTIPLE %s -DLINE=3
+
+# MULTIPLE: error: script line [[LINE]]: editing multiple archives not supported
+
+#--- create.mri
+CREATE create.a
+SAVE
+
+#--- thin.mri
+CREATETHIN thin.a
+SAVE thin.mri
+
+#--- none.mri
+CREATE
+SAVE
+
+#--- thin-none.mri
+CREATETHIN
+SAVE
+
+#--- foo.txt
+FOO
+
+#--- none-add.mri
+CREATE
+ADDMOD foo.txt
+SAVE
+
+#--- thin-none-add.mri
+CREATETHIN
+ADDMOD foo.txt
+SAVE
+
+#--- path.mri
+CREATE path/path.a
+SAVE
+
+#--- thin-path.mri
+CREATETHIN path/thin-path.a
+SAVE
+
+#--- bad.mri
+CREATE bad/bad.a
+SAVE
+
+#--- thin-bad.mri
+CREATETHIN bad/bad.a
+SAVE
+
+#--- create2.mri
+CREATE create1.a
+CREATE create2.a
+SAVE
+
+#--- thin2.mri
+CREATETHIN thin1.a
+CREATETHIN thin2.a
+SAVE
+
+#--- mix1.mri
+CREATE mix1.a
+CREATETHIN mixthin1.a
+SAVE
+
+#--- mix2.mri
+CREATETHIN mixthin2.a
+CREATE mix2.a
+SAVE
+
+#--- save.mri
+CREATE create1.a
+SAVE
+CREATE create2.a
+SAVE
+
+#--- thin-save.mri
+CREATETHIN create1.a
+SAVE
+CREATETHIN create2.a
+SAVE

diff  --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index abd10f1b500b0..b4f5e5b05762e 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -1062,6 +1062,8 @@ static void runMRIScript() {
 
     switch (Command) {
     case MRICommand::AddLib: {
+      if (!Create)
+        fail("no output archive has been opened");
       object::Archive &Lib = readLibrary(Rest);
       {
         Error Err = Error::success();
@@ -1072,6 +1074,8 @@ static void runMRIScript() {
       break;
     }
     case MRICommand::AddMod:
+      if (!Create)
+        fail("no output archive has been opened");
       addMember(NewMembers, Rest);
       break;
     case MRICommand::CreateThin:
@@ -1084,6 +1088,8 @@ static void runMRIScript() {
       if (Saved)
         fail("file already saved");
       ArchiveName = std::string(Rest);
+      if (ArchiveName.empty())
+        fail("missing archive name");
       break;
     case MRICommand::Delete: {
       llvm::erase_if(NewMembers, [=](NewArchiveMember &M) {


        


More information about the llvm-commits mailing list