[llvm-ar] Flatten thin archives.

Summary:
Normal behavior for GNU ar is to flatten thin archives when adding them to another thin archive, i.e. add the members directly instead of nesting the archive.

Some refactoring done as part of this patch to ease things:
 - Consolidate `addMember`/`addLibMember` methods
 - Rename `addMember` to `addChildMember` to make it more visibly different at the call site that an archive child is passed instead of a regular member
 - Pass in a separate vector and splice it back into position instead of passing a vector + optional Pos (which makes expanding libs tricky)

This fixes PR37530 as raised by https://github.com/ClangBuiltLinux/linux/issues/279.

Reviewers: mstorsjo, pcc, ruiu

Reviewed By: mstorsjo

Subscribers: llvm-commits, tpimh, nickdesaulniers

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

llvm-svn: 351120
diff --git a/llvm/test/tools/llvm-ar/Inputs/a-plus-b.a b/llvm/test/tools/llvm-ar/Inputs/a-plus-b.a
new file mode 100644
index 0000000..a58406e
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/a-plus-b.a
@@ -0,0 +1,6 @@
+!<thin>
+//                                              14        `
+a.txt/
+b.txt/
+/0              0           0     0     644     11        `
+/7              0           0     0     644     11        `
diff --git a/llvm/test/tools/llvm-ar/Inputs/a.txt b/llvm/test/tools/llvm-ar/Inputs/a.txt
new file mode 100644
index 0000000..12d91b6
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/a.txt
@@ -0,0 +1 @@
+a-contents
diff --git a/llvm/test/tools/llvm-ar/Inputs/b.txt b/llvm/test/tools/llvm-ar/Inputs/b.txt
new file mode 100644
index 0000000..aee476f
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/b.txt
@@ -0,0 +1 @@
+b-contents
diff --git a/llvm/test/tools/llvm-ar/Inputs/c.txt b/llvm/test/tools/llvm-ar/Inputs/c.txt
new file mode 100644
index 0000000..a80e36bc3
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/c.txt
@@ -0,0 +1 @@
+c-contents
diff --git a/llvm/test/tools/llvm-ar/Inputs/d.txt b/llvm/test/tools/llvm-ar/Inputs/d.txt
new file mode 100644
index 0000000..20d7aa0
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/d.txt
@@ -0,0 +1 @@
+d-contents
diff --git a/llvm/test/tools/llvm-ar/Inputs/nested-thin-archive.a b/llvm/test/tools/llvm-ar/Inputs/nested-thin-archive.a
new file mode 100644
index 0000000..312e8de
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/Inputs/nested-thin-archive.a
@@ -0,0 +1,7 @@
+!<thin>
+//                                              20        `
+a-plus-b.a/
+c.txt/
+
+/0              0           0     0     644     202       `
+/12             0           0     0     644     11        `
diff --git a/llvm/test/tools/llvm-ar/flatten-thin-archive-recursive.test b/llvm/test/tools/llvm-ar/flatten-thin-archive-recursive.test
new file mode 100644
index 0000000..fdd752d
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/flatten-thin-archive-recursive.test
@@ -0,0 +1,13 @@
+# Since llvm-ar cannot create thin archives that contain any thin archives,
+# nested-thin-archive.a is a manually constructed thin archive that contains
+# another (unflattened) thin archive.
+# This test ensures that flat archives are recursively flattened.
+
+RUN: rm -f %t.a
+RUN: llvm-ar rcsT %t.a %S/Inputs/nested-thin-archive.a %S/Inputs/d.txt
+RUN: llvm-ar t %t.a | FileCheck %s
+
+CHECK:      a.txt
+CHECK-NEXT: b.txt
+CHECK-NEXT: c.txt
+CHECK-NEXT: d.txt
diff --git a/llvm/test/tools/llvm-ar/flatten-thin-archive.test b/llvm/test/tools/llvm-ar/flatten-thin-archive.test
new file mode 100644
index 0000000..430f48f
--- /dev/null
+++ b/llvm/test/tools/llvm-ar/flatten-thin-archive.test
@@ -0,0 +1,18 @@
+# This test creates a thin archive that contains a thin archive, a regular
+# archive, and a file.
+#
+# The inner thin archive should be flattened, but the regular archive should
+# not. The order of members in the archive should match the input order, with
+# flattened members appearing together.
+
+RUN: touch %t-a.txt %t-b.txt %t-c.txt %t-d.txt %t-e.txt
+RUN: rm -f %t-a-plus-b.a %t.a
+RUN: llvm-ar rcsT %t-a-plus-b.a %t-a.txt %t-b.txt
+RUN: llvm-ar rcs %t-d-plus-e.a %t-d.txt %t-e.txt
+RUN: llvm-ar rcsT %t.a %t-a-plus-b.a %t-c.txt %t-d-plus-e.a
+RUN: llvm-ar t %t.a | FileCheck %s
+
+CHECK:      a.txt
+CHECK-NEXT: b.txt
+CHECK-NEXT: c.txt
+CHECK-NEXT: -d-plus-e.a