[update_cc_test_checks] Don't attach CHECK lines to function declarations

Previously we were adding the CHECK lines to both definitions and
declarations. Update the JSON AST dump parsing code to skip all
FunctionDecls without an "inner" node (i.e. no body).

Reviewed By: MaskRay, greened
Differential Revision: https://reviews.llvm.org/D73708
diff --git a/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c
new file mode 100644
index 0000000..8e2e4f6
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c
@@ -0,0 +1,17 @@
+// Check that the CHECK lines are generated before the definition and not the declaration
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s
+
+int foo();
+
+void empty_function();
+
+int main() {
+  empty_function();
+  return foo();
+}
+
+int foo() {
+  return 1;
+}
+
+void empty_function() {}
diff --git a/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c.expected b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c.expected
new file mode 100644
index 0000000..07503be
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/Inputs/def-and-decl.c.expected
@@ -0,0 +1,34 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// Check that the CHECK lines are generated before the definition and not the declaration
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s
+
+int foo();
+
+void empty_function();
+
+// CHECK-LABEL: @main(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    store i32 0, i32* [[RETVAL]], align 4
+// CHECK-NEXT:    call void @empty_function()
+// CHECK-NEXT:    [[CALL:%.*]] = call i32 @foo()
+// CHECK-NEXT:    ret i32 [[CALL]]
+//
+int main() {
+  empty_function();
+  return foo();
+}
+
+// CHECK-LABEL: @foo(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    ret i32 1
+//
+int foo() {
+  return 1;
+}
+
+// CHECK-LABEL: @empty_function(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    ret void
+//
+void empty_function() {}
diff --git a/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/def-and-decl.test b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/def-and-decl.test
new file mode 100644
index 0000000..c91706d
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_cc_test_checks/def-and-decl.test
@@ -0,0 +1,7 @@
+## Test that CHECK lines are generated before the definion and not the declaration
+
+# RUN: cp %S/Inputs/def-and-decl.c %t.c && %update_cc_test_checks %t.c
+# RUN: diff -u %S/Inputs/def-and-decl.c.expected %t.c
+## Check that re-running update_cc_test_checks doesn't change the output
+# RUN: %update_cc_test_checks %t.c
+# RUN: diff -u %S/Inputs/def-and-decl.c.expected %t.c
diff --git a/llvm/utils/update_cc_test_checks.py b/llvm/utils/update_cc_test_checks.py
index 98e8e37..21cc5b4 100755
--- a/llvm/utils/update_cc_test_checks.py
+++ b/llvm/utils/update_cc_test_checks.py
@@ -76,6 +76,10 @@
     if line is None:
       common.debug('Skipping function without line number:', node['name'], '@', node['loc'])
       return
+    # If there is no 'inner' object, it is a function declaration -> skip
+    if 'inner' not in node:
+      common.debug('Skipping function without body:', node['name'], '@', node['loc'])
+      return
     spell = node['name']
     mangled = node.get('mangledName', spell)
     ret[int(line)-1] = (spell, mangled)