]> granicus.if.org Git - llvm/commitdiff
[IR] Add getArg() method to Function class
authorTeresa Johnson <tejohnson@google.com>
Thu, 1 Aug 2019 15:31:40 +0000 (15:31 +0000)
committerTeresa Johnson <tejohnson@google.com>
Thu, 1 Aug 2019 15:31:40 +0000 (15:31 +0000)
Adds a method which, when called with function.getArg(i), returns an
Argument* to the i'th argument.

Patch by Henry Wildermuth

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367576 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Function.h
unittests/IR/FunctionTest.cpp

index 7fa61e12f43112b33ba435864ac3343aeee1967c..cee8d6c973f03d29b8710ace3650ef958c93f29c 100644 (file)
@@ -710,6 +710,12 @@ public:
     return Arguments + NumArgs;
   }
 
+  Argument* getArg(unsigned i) const {
+    assert (i < NumArgs && "getArg() out of range!");
+    CheckLazyArguments();
+    return Arguments + i;
+  }
+
   iterator_range<arg_iterator> args() {
     return make_range(arg_begin(), arg_end());
   }
index 6f3e8f9beb1aadf22aa711fd7ffab97faa5bfed3..b5662d2ca992a13db0779bbcc54a346e2bf7a22d 100644 (file)
@@ -33,6 +33,14 @@ TEST(FunctionTest, hasLazyArguments) {
   // The argument list should be populated at first access.
   (void)F->arg_begin();
   EXPECT_FALSE(F->hasLazyArguments());
+
+  // Checking that getArg gets the arguments from F1 in the correct order.
+  unsigned i = 0;
+  for (Argument &A : F->args()) {
+    EXPECT_EQ(&A, F->getArg(i));
+    ++i;
+  }
+  EXPECT_FALSE(F->hasLazyArguments());
 }
 
 TEST(FunctionTest, stealArgumentListFrom) {