From fef483667f3c793d6aff81c69972549406e27ff6 Mon Sep 17 00:00:00 2001
From: "Duncan P. N. Exon Smith" <dexonsmith@apple.com>
Date: Tue, 28 Apr 2015 01:07:33 +0000
Subject: [PATCH] DebugInfo: Support up to 2^16 arguments in a subprogram

Support up to 2^16 arguments to a function.  If we do hit the limit,
assert out rather than restarting at 0 as we've done historically.

This fixes PR23332.  A clang test will follow.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235955 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/IR/DebugInfoMetadata.cpp  |  7 ++-----
 unittests/IR/MetadataTest.cpp | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/lib/IR/DebugInfoMetadata.cpp b/lib/IR/DebugInfoMetadata.cpp
index f6f2ff2d20c..4be35636cf8 100644
--- a/lib/IR/DebugInfoMetadata.cpp
+++ b/lib/IR/DebugInfoMetadata.cpp
@@ -456,11 +456,8 @@ MDLocalVariable *MDLocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
                                           Metadata *Type, unsigned Arg,
                                           unsigned Flags, StorageType Storage,
                                           bool ShouldCreate) {
-  // Truncate Arg to 8 bits.
-  //
-  // FIXME: This is gross (and should be changed to an assert or removed), but
-  // it matches historical behaviour for now.
-  Arg &= (1u << 8) - 1;
+  // 64K ought to be enough for any frontend.
+  assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
 
   assert(Scope && "Expected scope");
   assert(isCanonical(Name) && "Expected canonical MDString");
diff --git a/unittests/IR/MetadataTest.cpp b/unittests/IR/MetadataTest.cpp
index cb882e76f41..60d2506cbf9 100644
--- a/unittests/IR/MetadataTest.cpp
+++ b/unittests/IR/MetadataTest.cpp
@@ -1849,6 +1849,26 @@ TEST_F(MDLocalVariableTest, get) {
   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
 }
 
+TEST_F(MDLocalVariableTest, getArg256) {
+  EXPECT_EQ(255u, MDLocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
+                                       getSubprogram(), "", getFile(), 0,
+                                       nullptr, 255, 0)
+                      ->getArg());
+  EXPECT_EQ(256u, MDLocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
+                                       getSubprogram(), "", getFile(), 0,
+                                       nullptr, 256, 0)
+                      ->getArg());
+  EXPECT_EQ(257u, MDLocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
+                                       getSubprogram(), "", getFile(), 0,
+                                       nullptr, 257, 0)
+                      ->getArg());
+  unsigned Max = UINT16_MAX;
+  EXPECT_EQ(Max, MDLocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
+                                      getSubprogram(), "", getFile(), 0,
+                                      nullptr, Max, 0)
+                     ->getArg());
+}
+
 typedef MetadataTest MDExpressionTest;
 
 TEST_F(MDExpressionTest, get) {
-- 
2.34.1